allegro4r 0.0.1

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 (59) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +58 -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/extconf.rb +11 -0
  59. metadata +112 -0
@@ -0,0 +1,87 @@
1
+ #
2
+ # Example program (C Version) for the Allegro library, by Shawn Hargreaves.
3
+ # (Ruby port by Jason Frey)
4
+ #
5
+ # This program moves a circle across the screen, first with a
6
+ # double buffer and then using page flips.
7
+ #
8
+
9
+ require 'rubygems'
10
+ require 'allegro4r'
11
+ include Allegro4r::API
12
+
13
+ exit 1 if allegro_init != 0
14
+ install_timer
15
+ install_keyboard
16
+
17
+ # Some platforms do page flipping by making one large screen that you
18
+ # can then scroll, while others give you several smaller, unique
19
+ # surfaces. If you use the create_video_bitmap() function, the same
20
+ # code can work on either kind of platform, but you have to be careful
21
+ # how you set the video mode in the first place. We want two pages of
22
+ # 320x200 video memory, but if we just ask for that, on DOS Allegro
23
+ # might use a VGA driver that won't later be able to give us a second
24
+ # page of vram. But if we ask for the full 320x400 virtual screen that
25
+ # we want, the call will fail when using DirectX drivers that can't do
26
+ # this. So we try two different mode sets, first asking for the 320x400
27
+ # size, and if that doesn't work, for 320x200.
28
+ if set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 400) != 0
29
+ if set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0
30
+ if set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0
31
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
32
+ allegro_message("Unable to set any graphic mode\n%s\n" % allegro_error)
33
+ exit 1
34
+ end
35
+ end
36
+ end
37
+
38
+ set_palette(desktop_palette)
39
+
40
+ # allocate the memory buffer
41
+ buffer = create_bitmap(SCREEN_W(), SCREEN_H())
42
+
43
+ # first with a double buffer...
44
+ clear_keybuf
45
+ c = retrace_count + 32
46
+ while retrace_count - c <= SCREEN_W() + 32
47
+ clear_to_color(buffer, makecol(255, 255, 255))
48
+ circlefill(buffer, retrace_count - c, SCREEN_H()/2, 32, makecol(0, 0, 0))
49
+ textprintf_ex(buffer, font, 0, 0, makecol(0, 0, 0), -1, "Double buffered (%s)" % gfx_driver.name)
50
+ blit(buffer, screen, 0, 0, 0, 0, SCREEN_W(), SCREEN_H())
51
+
52
+ break if keypressed
53
+ end
54
+
55
+ destroy_bitmap(buffer)
56
+
57
+ # now create two video memory bitmaps for the page flipping
58
+ page1 = create_video_bitmap(SCREEN_W(), SCREEN_H())
59
+ page2 = create_video_bitmap(SCREEN_W(), SCREEN_H())
60
+
61
+ if page1.nil? || page2.nil?
62
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
63
+ allegro_message("Unable to create two video memory pages\n")
64
+ exit 1
65
+ end
66
+
67
+ active_page = page2
68
+
69
+ # do the animation using page flips...
70
+ clear_keybuf
71
+ (-32..SCREEN_W() + 32).each do |c|
72
+ clear_to_color(active_page, makecol(255, 255, 255))
73
+ circlefill(active_page, c, SCREEN_H()/2, 32, makecol(0, 0, 0))
74
+ textprintf_ex(active_page, font, 0, 0, makecol(0, 0, 0), -1, "Page flipping (%s)" % gfx_driver.name)
75
+ show_video_bitmap(active_page)
76
+
77
+ if (active_page == page1)
78
+ active_page = page2
79
+ else
80
+ active_page = page1
81
+ end
82
+
83
+ break if keypressed
84
+ end
85
+
86
+ destroy_bitmap(page1)
87
+ destroy_bitmap(page2)
@@ -0,0 +1,70 @@
1
+ #
2
+ # Example program (C version) for the Allegro library, by Evert Glebbeek.
3
+ # (Ruby port by Jason Frey)
4
+ #
5
+ # This is a very simple program showing how to load and manipulate fonts.
6
+ #
7
+
8
+ require 'rubygems'
9
+ require 'allegro4r'
10
+ include Allegro4r::API
11
+
12
+ # you should always do this at the start of Allegro programs
13
+ exit 1 if allegro_init != 0
14
+
15
+ # set up the keyboard handler
16
+ install_keyboard
17
+
18
+ # set a graphics mode sized 320x200
19
+ if set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0
20
+ if set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0
21
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
22
+ allegro_message("Unable to set any graphic mode\n%s\n" % allegro_error)
23
+ exit 1
24
+ end
25
+ end
26
+
27
+ # set the color palette
28
+ set_palette(desktop_palette)
29
+
30
+ # clear the screen to white
31
+ clear_to_color(screen, makecol(255, 255, 255))
32
+
33
+ # We will use the lower case letters from Allegro's normal font and the
34
+ # uppercase letters from the font in unifont.dat
35
+ f1 = load_font("unifont.dat", nil, nil)
36
+ if f1.nil?
37
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
38
+ allegro_message("Cannot find unifont.dat in current directory.\n")
39
+ exit 1
40
+ end
41
+
42
+ # Extract character ranges
43
+ f2 = extract_font_range(font, ' '[0], ?A - 1)
44
+ f3 = extract_font_range(f1, ?A, ?Z);
45
+ f4 = extract_font_range(font, ?Z + 1, ?z)
46
+
47
+ # Merge fonts
48
+ Allegro4r::API.font = merge_fonts(f4, f5 = merge_fonts(f2, f3))
49
+
50
+ # Destroy temporary fonts
51
+ destroy_font(f1)
52
+ destroy_font(f2)
53
+ destroy_font(f3)
54
+ destroy_font(f4)
55
+ destroy_font(f5)
56
+
57
+ # you don't need to do this, but on some platforms (eg. Windows) things
58
+ # will be drawn more quickly if you always acquire the screen before
59
+ # trying to draw onto it.
60
+ acquire_screen
61
+
62
+ # write some text to the screen with black letters and transparent
63
+ # background
64
+ textout_centre_ex(screen, font, "Hello, world!", SCREEN_W()/2, SCREEN_H()/2, makecol(0, 0, 0), -1)
65
+
66
+ # you must always release bitmaps before calling any input functions
67
+ release_screen
68
+
69
+ # wait for a key press
70
+ readkey
@@ -0,0 +1,46 @@
1
+ #
2
+ # Example program (C version) for the Allegro library, by Shawn Hargreaves.
3
+ # (Ruby port by Jason Frey)
4
+ #
5
+ # This is a very simple program showing how to get into graphics
6
+ # mode and draw text onto the screen.
7
+ #
8
+
9
+ require 'rubygems'
10
+ require 'allegro4r'
11
+ include Allegro4r::API
12
+
13
+ # you should always do this at the start of Allegro programs
14
+ exit 1 if allegro_init != 0
15
+
16
+ # set up the keyboard handler
17
+ install_keyboard
18
+
19
+ # set a graphics mode sized 320x200
20
+ if set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0
21
+ if set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0
22
+ set_gfx_mode(GFX_TEXT, 0, 0, 0, 0)
23
+ allegro_message("Unable to set any graphic mode\n%s\n" % allegro_error)
24
+ exit 1
25
+ end
26
+ end
27
+
28
+ # set the color palette
29
+ set_palette(desktop_palette)
30
+
31
+ # clear the screen to white
32
+ clear_to_color(screen, makecol(255, 255, 255))
33
+
34
+ # you don't need to do this, but on some platforms (eg. Windows) things
35
+ # will be drawn more quickly if you always acquire the screen before
36
+ # trying to draw onto it.
37
+ acquire_screen
38
+
39
+ # write some text to the screen with black letters and transparent background
40
+ textout_centre_ex(screen, font, "Hello, world!", SCREEN_W()/2, SCREEN_H()/2, makecol(0, 0, 0), -1)
41
+
42
+ # you must always release bitmaps before calling any input functions
43
+ release_screen
44
+
45
+ # wait for a key press
46
+ readkey
@@ -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;