allegro4r 0.0.1-x86-mswin32-60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +59 -0
  3. data/README.txt +94 -0
  4. data/examples/exdbuf.rb +58 -0
  5. data/examples/exfixed.rb +46 -0
  6. data/examples/exflame.rb +200 -0
  7. data/examples/exflip.rb +87 -0
  8. data/examples/exfont.rb +70 -0
  9. data/examples/exhello.rb +46 -0
  10. data/examples/exjoy.rb +206 -0
  11. data/examples/exkeys.rb +216 -0
  12. data/examples/exmem.rb +50 -0
  13. data/examples/exmidi.rb +97 -0
  14. data/examples/exmouse.rb +149 -0
  15. data/examples/expal.rb +70 -0
  16. data/examples/expat.rb +62 -0
  17. data/examples/exsample.rb +89 -0
  18. data/examples/extimer.rb +84 -0
  19. data/examples/unifont.dat +0 -0
  20. data/ext/a4r_API_BITMAP.c +27 -0
  21. data/ext/a4r_API_DIGI_DRIVER.c +14 -0
  22. data/ext/a4r_API_GFX_DRIVER.c +14 -0
  23. data/ext/a4r_API_JOYSTICK_AXIS_INFO.c +53 -0
  24. data/ext/a4r_API_JOYSTICK_BUTTON_INFO.c +27 -0
  25. data/ext/a4r_API_JOYSTICK_DRIVER.c +14 -0
  26. data/ext/a4r_API_JOYSTICK_INFO.c +84 -0
  27. data/ext/a4r_API_JOYSTICK_STICK_INFO.c +62 -0
  28. data/ext/a4r_API_KEYBOARD_DRIVER.c +14 -0
  29. data/ext/a4r_API_MIDI_DRIVER.c +14 -0
  30. data/ext/a4r_API_MOUSE_DRIVER.c +14 -0
  31. data/ext/a4r_API_PALETTE.c +63 -0
  32. data/ext/a4r_API_RGB.c +118 -0
  33. data/ext/a4r_API_TIMER_DRIVER.c +14 -0
  34. data/ext/a4r_API_bitmap_objects.c +310 -0
  35. data/ext/a4r_API_blitting_and_sprites.c +86 -0
  36. data/ext/a4r_API_digital_sample_routines.c +83 -0
  37. data/ext/a4r_API_direct_access_to_video_memory.c +102 -0
  38. data/ext/a4r_API_drawing_primitives.c +114 -0
  39. data/ext/a4r_API_file_and_compression_routines.c +27 -0
  40. data/ext/a4r_API_fixed_point_math_routines.c +98 -0
  41. data/ext/a4r_API_fonts.c +147 -0
  42. data/ext/a4r_API_graphics_modes.c +155 -0
  43. data/ext/a4r_API_joystick_routines.c +213 -0
  44. data/ext/a4r_API_keyboard_routines.c +420 -0
  45. data/ext/a4r_API_misc.c +133 -0
  46. data/ext/a4r_API_mouse_routines.c +220 -0
  47. data/ext/a4r_API_music_routines_midi.c +147 -0
  48. data/ext/a4r_API_palette_routines.c +112 -0
  49. data/ext/a4r_API_sound_init_routines.c +29 -0
  50. data/ext/a4r_API_text_output.c +178 -0
  51. data/ext/a4r_API_timer_routines.c +250 -0
  52. data/ext/a4r_API_transparency_and_patterned_drawing.c +87 -0
  53. data/ext/a4r_API_truecolor_pixel_formats.c +44 -0
  54. data/ext/a4r_API_unicode_routines.c +53 -0
  55. data/ext/a4r_API_using_allegro.c +98 -0
  56. data/ext/allegro4r.c +866 -0
  57. data/ext/allegro4r.h +311 -0
  58. data/ext/allegro4r.so +0 -0
  59. data/ext/extconf.rb +11 -0
  60. metadata +113 -0
data/examples/exjoy.rb ADDED
@@ -0,0 +1,206 @@
1
+ #
2
+ # Example program (C Version) for the Allegro library, by Grzegorz Adam Hankiewicz.
3
+ # (Ruby port by Jason Frey)
4
+ #
5
+ # This program uses the Allegro library to detect and read the value
6
+ # of a joystick. The output of the program is a small target sight
7
+ # on the screen which you can move. At the same time the program will
8
+ # tell you what you are doing with the joystick (moving or firing).
9
+
10
+ require 'rubygems'
11
+ require 'allegro4r'
12
+ include Allegro4r::API
13
+
14
+ x = 160; y = 100 # these will be used to show the target sight
15
+ analogmode = false
16
+
17
+ exit 1 if allegro_init != 0 # you NEED this man! ;-)
18
+
19
+ install_keyboard # ahh... read the docs. I will explain only
20
+ # joystick specific routines
21
+
22
+ if set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0
23
+ if set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0
24
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
25
+ allegro_message("Unable to set any graphic mode\n%s\n" % allegro_error)
26
+ exit 1
27
+ end
28
+ end
29
+
30
+ set_palette(default_palette)
31
+ clear_bitmap(screen)
32
+ textout_centre_ex(screen, font, "Please center the joystick", SCREEN_W()/2, SCREEN_H()/2 - 36, palette_color[255], 0)
33
+ textout_centre_ex(screen, font, "and press a key.", SCREEN_W()/2, SCREEN_H()/2 - 20, palette_color[255], 0)
34
+
35
+ exit 0 if (readkey & 0xFF) == 27
36
+
37
+ # the first thing is to initialise the joystick driver
38
+ if install_joystick(JOY_TYPE_AUTODETECT) != 0
39
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
40
+ allegro_message("Error initialising joystick\n%s\n", allegro_error)
41
+ exit 1
42
+ end
43
+
44
+ # make sure that we really do have a joystick
45
+ if num_joysticks == 0
46
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
47
+ allegro_message("Error: joystick not found\n")
48
+ exit 1
49
+ end
50
+
51
+ # before using the joystick, we have to calibrate it. This loop only
52
+ # calibrates joystick number 0, but you could do the same thing for
53
+ # other sticks if they are present (the num_joysticks variable will
54
+ # tell you how many there are).
55
+ while joy[0].flags & JOYFLAG_CALIBRATE != 0
56
+ msg = calibrate_joystick_name(0)
57
+
58
+ clear_bitmap(screen)
59
+ textout_centre_ex(screen, font, msg, SCREEN_W()/2, 64, palette_color[255], 0)
60
+ textout_centre_ex(screen, font, "and press a key.", SCREEN_W()/2, 80, palette_color[255], 0)
61
+
62
+ exit 0 if (readkey & 0xFF) == 27
63
+
64
+ if calibrate_joystick(0) != 0
65
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
66
+ allegro_message("Error calibrating joystick!\n")
67
+ exit 1
68
+ end
69
+ end
70
+
71
+ # if this joystick supports analogue input, ask the user whether to
72
+ # use digital or analogue mode. If it is only a digital pad, we don't
73
+ # bother with this question.
74
+ #
75
+ if joy[0].stick[0].flags & JOYFLAG_ANALOGUE != 0
76
+ clear_bitmap(screen)
77
+ textout_centre_ex(screen, font, "Now press 'D' to use a digital", SCREEN_W()/2, 64, palette_color[255], 0)
78
+ textout_centre_ex(screen, font, "joystick or 'A' for analogue mode.", SCREEN_W()/2, 80, palette_color[255], 0)
79
+
80
+ loop do
81
+ c = readkey & 0xFF
82
+
83
+ if c == ?d || c == ?D
84
+ analogmode = false
85
+ break
86
+ elsif c == ?a || c == ?A
87
+ analogmode = true
88
+ break
89
+ elsif c == 27
90
+ exit 0
91
+ end
92
+ end
93
+ else
94
+ analogmode = false
95
+ end
96
+
97
+ drawing_mode(DRAW_MODE_XOR, nil, 0, 0)
98
+ clear_keybuf
99
+
100
+ bmp = create_bitmap(320, 200)
101
+ clear_bitmap(bmp)
102
+
103
+ loop do
104
+ poll_joystick # we HAVE to do this to read the joystick
105
+
106
+ clear_bitmap(bmp)
107
+
108
+ textout_centre_ex(bmp, font, joystick_driver.name, 160, 150, palette_color[255], 0)
109
+
110
+ if (analogmode)
111
+ textout_centre_ex(bmp, font, "Analog mode selected", 160, 160, palette_color[255], 0)
112
+ else
113
+ textout_centre_ex(bmp, font, "Digital mode selected", 160, 160, palette_color[255], 0)
114
+ end
115
+
116
+ textout_centre_ex(bmp, font, "Move the joystick all around", 160, 170, palette_color[255], 0)
117
+ textout_centre_ex(bmp, font, "Press any key to exit", 160, 180, palette_color[255], 0)
118
+ textout_centre_ex(bmp, font, "Made by Grzegorz Adam Hankiewicz", 160, 190, palette_color[255], 0)
119
+
120
+ # if we detect any buttons, we print a message on the screen
121
+ (0...joy[0].num_buttons).each do |c|
122
+ if (joy[0].button[c].b)
123
+ textprintf_centre_ex(bmp, font, 160, c*10, palette_color[15], 0, "%s pressed" % joy[0].button[c].name)
124
+ end
125
+ end
126
+
127
+ if !analogmode
128
+ # now we have to check individually every possible movement
129
+ # and actualize the coordinates of the target sight.
130
+ if joy[0].stick[0].axis[0].d1
131
+ x -= 1 if x > 0
132
+ textout_centre_ex(bmp, font, "Left", 120, 100, palette_color[255], 0)
133
+ end
134
+ if joy[0].stick[0].axis[0].d2
135
+ x += 1 if x < 319
136
+ textout_centre_ex(bmp, font, "Right", 200, 100, palette_color[255], 0)
137
+ end
138
+ if joy[0].stick[0].axis[1].d1
139
+ y -= 1 if y > 0
140
+ textout_centre_ex(bmp, font, "Up", 160, 70, palette_color[255], 0)
141
+ end
142
+ if joy[0].stick[0].axis[1].d2
143
+ y += 1 if y < 199
144
+ textout_centre_ex(bmp, font, "Down", 160, 130, palette_color[255], 0)
145
+ end
146
+ else
147
+ # yeah! Remember the 'ifs' of the digital part? This looks
148
+ # much better, only 2 lines.
149
+ x += joy[0].stick[0].axis[0].pos/40
150
+ y += joy[0].stick[0].axis[1].pos/40
151
+
152
+ # for informational purposes, show the input values on screen */
153
+ textprintf_ex(bmp, font, 0, 0, palette_color[255], 0, "Axis 0: %d" % joy[0].stick[0].axis[0].pos)
154
+ textprintf_ex(bmp, font, 0, 10, palette_color[255], 0, "Axis 1: %d" % joy[0].stick[0].axis[1].pos)
155
+
156
+ # by checking if the values were positive or negative, we
157
+ # can know in which the direction the user pulled the joy.
158
+ if joy[0].stick[0].axis[0].pos/40 < 0
159
+ textout_centre_ex(bmp, font, "Left", 120, 100, palette_color[255], 0)
160
+ end
161
+
162
+ if joy[0].stick[0].axis[0].pos/40 > 0
163
+ textout_centre_ex(bmp, font, "Right", 200, 100, palette_color[255], 0)
164
+ end
165
+
166
+ if joy[0].stick[0].axis[1].pos/40 < 0
167
+ textout_centre_ex(bmp, font, "Up", 160, 70, palette_color[255], 0)
168
+ end
169
+
170
+ if joy[0].stick[0].axis[1].pos/40 > 0
171
+ textout_centre_ex(bmp, font, "Down", 160, 130, palette_color[255], 0)
172
+ end
173
+
174
+ # WARNING! An analog joystick can move more than 1 pixel at
175
+ # a time and the checks we did with the digital part don't
176
+ # work any longer because the steps of the target sight could
177
+ # 'jump' over the limits.
178
+ # To avoid this, we just check if the target sight has gone
179
+ # out of the screen. If yes, we put it back at the border.
180
+ x = 319 if x > 319
181
+
182
+ x = 0 if x < 0
183
+
184
+ y = 0 if y < 0
185
+
186
+ y = 199 if y > 199
187
+ end
188
+
189
+ # this draws the target sight.
190
+ circle(bmp, x, y, 5, palette_color[255])
191
+ putpixel(bmp, x, y, palette_color[255])
192
+ putpixel(bmp, x+1, y, palette_color[255])
193
+ putpixel(bmp, x, y+1, palette_color[255])
194
+ putpixel(bmp, x-1, y, palette_color[255])
195
+ putpixel(bmp, x, y-1, palette_color[255])
196
+ putpixel(bmp, x+5, y, palette_color[255])
197
+ putpixel(bmp, x, y+5, palette_color[255])
198
+ putpixel(bmp, x-5, y, palette_color[255])
199
+ putpixel(bmp, x, y-5, palette_color[255])
200
+
201
+ blit(bmp, screen, 0, 0, SCREEN_W()/2 - 160, SCREEN_H()/2 - 100, 320, 200)
202
+
203
+ break unless !keypressed
204
+ end
205
+
206
+ destroy_bitmap(bmp)
@@ -0,0 +1,216 @@
1
+ #
2
+ # Example program (C Version) for the Allegro library, by Shawn Hargreaves.
3
+ # (Ruby port by Jason Frey)
4
+ #
5
+ # This program demonstrates how to access the keyboard. The
6
+ # first part shows the basic use of readkey(). The second part
7
+ # shows how to extract the ASCII value. Next come the scan codes.
8
+ # The fourth test detects modifier keys like alt or shift. The
9
+ # fifth test requires some focus to be passed. The final step
10
+ # shows how to use the global key array to read simultaneous
11
+ # key presses.
12
+ # The last method to detect key presses are keyboard callbacks.
13
+ # This is demonstrated by by installing a keyboard callback,
14
+ # which marks all pressed keys by drawing to a grid.
15
+ #
16
+
17
+ require 'rubygems'
18
+ require 'allegro4r'
19
+ include Allegro4r::API
20
+
21
+ key_names = [
22
+ "(none)", "KEY_A", "KEY_B", "KEY_C",
23
+ "KEY_D", "KEY_E", "KEY_F", "KEY_G",
24
+ "KEY_H", "KEY_I", "KEY_J", "KEY_K",
25
+ "KEY_L", "KEY_M", "KEY_N", "KEY_O",
26
+ "KEY_P", "KEY_Q", "KEY_R", "KEY_S",
27
+ "KEY_T", "KEY_U", "KEY_V", "KEY_W",
28
+ "KEY_X", "KEY_Y", "KEY_Z", "KEY_0",
29
+ "KEY_1", "KEY_2", "KEY_3", "KEY_4",
30
+ "KEY_5", "KEY_6", "KEY_7", "KEY_8",
31
+ "KEY_9", "KEY_0_PAD", "KEY_1_PAD", "KEY_2_PAD",
32
+ "KEY_3_PAD", "KEY_4_PAD", "KEY_5_PAD", "KEY_6_PAD",
33
+ "KEY_7_PAD", "KEY_8_PAD", "KEY_9_PAD", "KEY_F1",
34
+ "KEY_F2", "KEY_F3", "KEY_F4", "KEY_F5",
35
+ "KEY_F6", "KEY_F7", "KEY_F8", "KEY_F9",
36
+ "KEY_F10", "KEY_F11", "KEY_F12", "KEY_ESC",
37
+ "KEY_TILDE", "KEY_MINUS", "KEY_EQUALS", "KEY_BACKSPACE",
38
+ "KEY_TAB", "KEY_OPENBRACE", "KEY_CLOSEBRACE", "KEY_ENTER",
39
+ "KEY_COLON", "KEY_QUOTE", "KEY_BACKSLASH", "KEY_BACKSLASH2",
40
+ "KEY_COMMA", "KEY_STOP", "KEY_SLASH", "KEY_SPACE",
41
+ "KEY_INSERT", "KEY_DEL", "KEY_HOME", "KEY_END",
42
+ "KEY_PGUP", "KEY_PGDN", "KEY_LEFT", "KEY_RIGHT",
43
+ "KEY_UP", "KEY_DOWN", "KEY_SLASH_PAD", "KEY_ASTERISK",
44
+ "KEY_MINUS_PAD", "KEY_PLUS_PAD", "KEY_DEL_PAD", "KEY_ENTER_PAD",
45
+ "KEY_PRTSCR", "KEY_PAUSE", "KEY_ABNT_C1", "KEY_YEN",
46
+ "KEY_KANA", "KEY_CONVERT", "KEY_NOCONVERT", "KEY_AT",
47
+ "KEY_CIRCUMFLEX", "KEY_COLON2", "KEY_KANJI", "KEY_EQUALS_PAD",
48
+ "KEY_BACKQUOTE", "KEY_SEMICOLON", "KEY_COMMAND", "KEY_UNKNOWN1",
49
+ "KEY_UNKNOWN2", "KEY_UNKNOWN3", "KEY_UNKNOWN4", "KEY_UNKNOWN5",
50
+ "KEY_UNKNOWN6", "KEY_UNKNOWN7", "KEY_UNKNOWN8", "KEY_LSHIFT",
51
+ "KEY_RSHIFT", "KEY_LCONTROL", "KEY_RCONTROL", "KEY_ALT",
52
+ "KEY_ALTGR", "KEY_LWIN", "KEY_RWIN", "KEY_MENU",
53
+ "KEY_SCRLOCK", "KEY_NUMLOCK", "KEY_CAPSLOCK", "KEY_MAX"
54
+ ]
55
+
56
+ # Keyboard callback. We are very evil and draw to the screen from within
57
+ # the callback. Don't do this in your own programs ;)
58
+ def keypress_handler(scancode)
59
+ i = scancode & 0x7f
60
+ x = SCREEN_W() - 100 * 3 + (i % 3) * 100
61
+ y = SCREEN_H() / 2 + (i / 3 - 21) * 10
62
+ color = scancode & 0x80 != 0 ? makecol(255, 255, 0) : makecol(128, 0, 0)
63
+ rectfill(screen, x, y, x + 95, y + 8, color)
64
+ str = ustrzncpy(scancode_to_name(i), 12)
65
+ textprintf_ex(screen, font, x + 1, y + 1, makecol(0, 0, 0), -1, str)
66
+ end
67
+
68
+ # helper function for making more room on the screen
69
+ def scroll
70
+ blit(screen, screen, 0, 32, 0, 24, SCREEN_W() / 2, SCREEN_H() - 32)
71
+ rectfill(screen, 0, SCREEN_H() - 16, SCREEN_W() / 2, SCREEN_H() - 1, makecol(255, 255, 255))
72
+ end
73
+
74
+ exit 1 if allegro_init != 0
75
+ install_keyboard
76
+ install_timer
77
+
78
+ if set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0
79
+ if set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0
80
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
81
+ allegro_message("Unable to set any graphic mode\n%s\n" % allegro_error)
82
+ exit 1
83
+ end
84
+ end
85
+
86
+ set_palette(desktop_palette)
87
+
88
+ clear_to_color(screen, makecol(255, 255, 255))
89
+
90
+ # Draw the initial keys grid by simulating release of every key.
91
+ (0...KEY_MAX).each do |k|
92
+ keypress_handler(k + 0x80)
93
+ end
94
+
95
+ # Install our keyboard callback.
96
+ # TODO: Have not been able to figure out how to get the keyboard interrupt stuff
97
+ # to work with Ruby, so I'm temporarily skipping this part.
98
+ # LOCK_FUNCTION(:keypress_handler)
99
+ # Allegro4r::API.keyboard_lowlevel_callback = self.method(:keypress_handler)
100
+
101
+ acquire_screen
102
+ textprintf_centre_ex(screen, font, SCREEN_W()/2, 8, makecol(0, 0, 0), makecol(255, 255, 255), "Driver: %s" % keyboard_driver.name)
103
+
104
+ # keyboard input can be accessed with the readkey function
105
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Press some keys (ESC to finish)")
106
+ scroll
107
+
108
+ loop do
109
+ release_screen
110
+ k = readkey
111
+ acquire_screen
112
+ scroll
113
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "readkey returned %-6d (0x%04X)" % [k, k])
114
+ break unless (k & 0xFF) != 27
115
+ end
116
+
117
+ # the ASCII code is in the low byte of the return value
118
+ scroll; scroll; scroll
119
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Press some more keys (ESC to finish)")
120
+ scroll
121
+
122
+ loop do
123
+ release_screen
124
+ k = readkey
125
+ acquire_screen
126
+ scroll
127
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "ASCII code is %d" % (k & 0xFF))
128
+ break unless (k & 0xFF) != 27
129
+ end
130
+
131
+ # the hardware scan code is in the high byte of the return value
132
+ scroll; scroll; scroll
133
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Press some more keys (ESC to finish)")
134
+ scroll
135
+
136
+ loop do
137
+ release_screen
138
+ k = readkey
139
+ acquire_screen
140
+ scroll
141
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Scan code is %d (%s)" % [k>>8, key_names[k>>8]])
142
+ break unless (k & 0xFF) != 27
143
+ end
144
+
145
+ # key qualifiers are stored in the key_shifts variable. Note that this
146
+ # version of the code uses ureadkey instead of readkey: that is
147
+ # necessary if you want to access Unicode characters from outside
148
+ # the normal ASCII range, for example to support Russian or Chinese.
149
+ scroll; scroll; scroll
150
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Press some more keys (ESC to finish)")
151
+ scroll
152
+
153
+ loop do
154
+ release_screen
155
+ k = ureadkey(nil)
156
+ acquire_screen
157
+ buf = ""
158
+ buf << "shift " if (key_shifts & KB_SHIFT_FLAG != 0)
159
+ buf << "ctrl " if (key_shifts & KB_CTRL_FLAG != 0)
160
+ buf << "alt " if (key_shifts & KB_ALT_FLAG != 0)
161
+ buf << "lwin " if (key_shifts & KB_LWIN_FLAG != 0)
162
+ buf << "rwin " if (key_shifts & KB_RWIN_FLAG != 0)
163
+ buf << "menu " if (key_shifts & KB_MENU_FLAG != 0)
164
+ buf << "command " if (key_shifts & KB_COMMAND_FLAG != 0)
165
+ buf << usprintf("'%c' [0x%02x]" % [k != 0 ? k : ' '[0], k])
166
+ buf << " caps" if (key_shifts & KB_CAPSLOCK_FLAG != 0)
167
+ buf << " num" if (key_shifts & KB_NUMLOCK_FLAG != 0)
168
+ buf << " scrl" if (key_shifts & KB_SCROLOCK_FLAG != 0)
169
+ scroll
170
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), buf)
171
+ break unless k != 27
172
+ end
173
+
174
+ # various scan codes are defined in allegro.h as KEY_* constants
175
+ scroll; scroll; scroll
176
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Press F6")
177
+ scroll
178
+
179
+ release_screen
180
+ k = readkey
181
+ acquire_screen
182
+
183
+ while (k>>8) != KEY_F6 && (k>>8) != KEY_ESC
184
+ scroll
185
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Wrong key, stupid! I said press F6")
186
+ release_screen
187
+ k = readkey
188
+ acquire_screen
189
+ end
190
+
191
+ # for detecting multiple simultaneous key presses, use the key[] array
192
+ scroll; scroll; scroll
193
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), "Press a combination of numbers")
194
+ scroll; scroll
195
+
196
+ release_screen
197
+
198
+ buf = ' ' * 10
199
+ loop do
200
+ buf[0] = key[KEY_0] ? '0' : ' '
201
+ buf[1] = key[KEY_1] ? '1' : ' '
202
+ buf[2] = key[KEY_2] ? '2' : ' '
203
+ buf[3] = key[KEY_3] ? '3' : ' '
204
+ buf[4] = key[KEY_4] ? '4' : ' '
205
+ buf[5] = key[KEY_5] ? '5' : ' '
206
+ buf[6] = key[KEY_6] ? '6' : ' '
207
+ buf[7] = key[KEY_7] ? '7' : ' '
208
+ buf[8] = key[KEY_8] ? '8' : ' '
209
+ buf[9] = key[KEY_9] ? '9' : ' '
210
+ textprintf_ex(screen, font, 8, SCREEN_H()-16, makecol(0, 0, 0), makecol(255, 255, 255), buf)
211
+ rest(1)
212
+ break unless !keypressed || (readkey >> 8) != KEY_ESC
213
+ end
214
+
215
+ clear_keybuf
216
+ # Allegro4r::API.keyboard_lowlevel_callback = nil;
data/examples/exmem.rb ADDED
@@ -0,0 +1,50 @@
1
+ #
2
+ # Example program (C Version) for the Allegro library, by Shawn Hargreaves.
3
+ # (Ruby port by Jason Frey)
4
+ #
5
+ # This program demonstrates the use of memory bitmaps. It creates
6
+ # a small temporary bitmap in memory, draws some circles onto it,
7
+ # and then blits lots of copies of it onto the screen.
8
+ #
9
+
10
+ require 'rubygems'
11
+ require 'allegro4r'
12
+ include Allegro4r::API
13
+
14
+ exit 1 if allegro_init != 0
15
+ install_keyboard
16
+
17
+ if set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0
18
+ if set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0
19
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
20
+ allegro_message("Unable to set any graphic mode\n%s\n" % allegro_error)
21
+ exit 1
22
+ end
23
+ end
24
+
25
+ set_palette(desktop_palette)
26
+
27
+ # make a memory bitmap sized 20x20
28
+ memory_bitmap = create_bitmap(20, 20)
29
+
30
+ # draw some circles onto it
31
+ clear_bitmap(memory_bitmap)
32
+ (0..15).each do |x|
33
+ circle(memory_bitmap, 10, 10, x, palette_color[x])
34
+ end
35
+
36
+ # blit lots of copies of it onto the screen
37
+ acquire_screen
38
+
39
+ (0...SCREEN_H()).step(20) do |y|
40
+ (0...SCREEN_W()).step(20) do |x|
41
+ blit(memory_bitmap, screen, 0, 0, x, y, 20, 20)
42
+ end
43
+ end
44
+
45
+ release_screen
46
+
47
+ # free the memory bitmap
48
+ destroy_bitmap(memory_bitmap)
49
+
50
+ readkey
@@ -0,0 +1,97 @@
1
+ #
2
+ # Example program (C Version) for the Allegro library, by Shawn Hargreaves.
3
+ # (Ruby port by Jason Frey)
4
+ #
5
+ # This program demonstrates how to play MIDI files.
6
+ #
7
+
8
+ require 'rubygems'
9
+ require 'allegro4r'
10
+ include Allegro4r::API
11
+
12
+ paused = false
13
+ done = false
14
+
15
+ exit 1 if allegro_init != 0
16
+
17
+ if ARGV.length != 1
18
+ allegro_message("Usage: 'ruby exmidi.rb filename.mid'\n")
19
+ exit 1
20
+ end
21
+
22
+ install_keyboard
23
+ install_timer
24
+
25
+ # install a MIDI sound driver
26
+ if install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, nil) != 0
27
+ allegro_message("Error initialising sound system\n%s\n" % allegro_error)
28
+ exit 1
29
+ end
30
+
31
+ # read in the MIDI file
32
+ the_music = load_midi(ARGV[0])
33
+ if the_music.nil?
34
+ allegro_message("Error reading MIDI file '%s'\n" % ARGV[0])
35
+ exit 1
36
+ end
37
+ length = get_midi_length(the_music)
38
+ beats = -midi_pos # get_midi_length updates midi_pos to the negative
39
+ # number of beats (quarter notes) in the midi.
40
+
41
+ if set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0
42
+ if set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0
43
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
44
+ allegro_message("Unable to set any graphic mode\n%s\n" % allegro_error)
45
+ exit 1
46
+ end
47
+ end
48
+
49
+ # try to continue in the background
50
+ if set_display_switch_mode(SWITCH_BACKGROUND)
51
+ set_display_switch_mode(SWITCH_BACKAMNESIA)
52
+ end
53
+
54
+ set_palette(desktop_palette)
55
+ background_color = makecol(255, 255, 255)
56
+ text_color = makecol(0, 0, 0)
57
+ clear_to_color(screen, background_color)
58
+ th = text_height(font)
59
+ x = SCREEN_W() / 2
60
+
61
+ textprintf_centre_ex(screen, font, x, SCREEN_H() / 3, text_color, -1, "Driver: %s" % midi_driver.name)
62
+ textprintf_centre_ex(screen, font, x, SCREEN_H() / 2, text_color, -1, "Playing %s" % get_filename(ARGV[0]))
63
+
64
+ # start up the MIDI file
65
+ play_midi(the_music, true)
66
+
67
+ y = 2 * SCREEN_H() / 3
68
+ tw = text_length(font, "0000:00 / 0000:00")
69
+ # wait for a key press
70
+ while !done
71
+ # P key pauses/resumes, any other key exits.
72
+ while keypressed
73
+ k = readkey & 255
74
+ if k == ?p
75
+ paused = !paused
76
+ if paused
77
+ midi_pause
78
+ textprintf_centre_ex(screen, font, x, y + th * 3, text_color, -1, "P A U S E D")
79
+ else
80
+ midi_resume
81
+ rectfill(screen, x - tw / 2, y + th * 3, x + tw / 2, y + th * 4, background_color)
82
+ end
83
+ else
84
+ done = true
85
+ end
86
+ end
87
+ pos = midi_time
88
+ beat = midi_pos
89
+ rectfill(screen, x - tw / 2, y, x + tw / 2, y + th * 2, background_color)
90
+ textprintf_centre_ex(screen, font, x, y, text_color, -1, "%d:%02d / %d:%02d" % [pos / 60, pos % 60, length / 60, length % 60])
91
+ textprintf_centre_ex(screen, font, x, y + th, text_color, -1, "beat %d / %d" % [beat, beats])
92
+ # We have nothing else to do.
93
+ rest(100)
94
+ end
95
+
96
+ # destroy the MIDI file
97
+ destroy_midi(the_music)