rubygame 2.3.0-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. data/CREDITS +60 -0
  2. data/LICENSE +504 -0
  3. data/NEWS +252 -0
  4. data/README +123 -0
  5. data/ROADMAP +109 -0
  6. data/Rakefile +440 -0
  7. data/doc/extended_readme.rdoc +49 -0
  8. data/doc/getting_started.rdoc +47 -0
  9. data/doc/macosx_install.rdoc +70 -0
  10. data/doc/windows_install.rdoc +123 -0
  11. data/ext/rubygame/rubygame_core.so +0 -0
  12. data/ext/rubygame/rubygame_event.c +644 -0
  13. data/ext/rubygame/rubygame_event.h +48 -0
  14. data/ext/rubygame/rubygame_event.obj +0 -0
  15. data/ext/rubygame/rubygame_gfx.c +942 -0
  16. data/ext/rubygame/rubygame_gfx.h +101 -0
  17. data/ext/rubygame/rubygame_gfx.obj +0 -0
  18. data/ext/rubygame/rubygame_gfx.so +0 -0
  19. data/ext/rubygame/rubygame_gl.c +154 -0
  20. data/ext/rubygame/rubygame_gl.h +32 -0
  21. data/ext/rubygame/rubygame_gl.obj +0 -0
  22. data/ext/rubygame/rubygame_image.c +252 -0
  23. data/ext/rubygame/rubygame_image.h +41 -0
  24. data/ext/rubygame/rubygame_image.obj +0 -0
  25. data/ext/rubygame/rubygame_image.so +0 -0
  26. data/ext/rubygame/rubygame_joystick.c +247 -0
  27. data/ext/rubygame/rubygame_joystick.h +41 -0
  28. data/ext/rubygame/rubygame_joystick.obj +0 -0
  29. data/ext/rubygame/rubygame_main.c +155 -0
  30. data/ext/rubygame/rubygame_main.h +36 -0
  31. data/ext/rubygame/rubygame_main.obj +0 -0
  32. data/ext/rubygame/rubygame_mixer.c +1024 -0
  33. data/ext/rubygame/rubygame_mixer.h +36 -0
  34. data/ext/rubygame/rubygame_mixer.obj +0 -0
  35. data/ext/rubygame/rubygame_mixer.so +0 -0
  36. data/ext/rubygame/rubygame_music.c +1017 -0
  37. data/ext/rubygame/rubygame_music.h +29 -0
  38. data/ext/rubygame/rubygame_music.obj +0 -0
  39. data/ext/rubygame/rubygame_screen.c +448 -0
  40. data/ext/rubygame/rubygame_screen.h +43 -0
  41. data/ext/rubygame/rubygame_screen.obj +0 -0
  42. data/ext/rubygame/rubygame_shared.c +272 -0
  43. data/ext/rubygame/rubygame_shared.h +68 -0
  44. data/ext/rubygame/rubygame_shared.obj +0 -0
  45. data/ext/rubygame/rubygame_sound.c +863 -0
  46. data/ext/rubygame/rubygame_sound.h +29 -0
  47. data/ext/rubygame/rubygame_sound.obj +0 -0
  48. data/ext/rubygame/rubygame_surface.c +1151 -0
  49. data/ext/rubygame/rubygame_surface.h +62 -0
  50. data/ext/rubygame/rubygame_surface.obj +0 -0
  51. data/ext/rubygame/rubygame_time.c +183 -0
  52. data/ext/rubygame/rubygame_time.h +32 -0
  53. data/ext/rubygame/rubygame_time.obj +0 -0
  54. data/ext/rubygame/rubygame_ttf.c +599 -0
  55. data/ext/rubygame/rubygame_ttf.h +69 -0
  56. data/ext/rubygame/rubygame_ttf.obj +0 -0
  57. data/ext/rubygame/rubygame_ttf.so +0 -0
  58. data/lib/rubygame.rb +41 -0
  59. data/lib/rubygame/clock.rb +128 -0
  60. data/lib/rubygame/color.rb +79 -0
  61. data/lib/rubygame/color/models/base.rb +111 -0
  62. data/lib/rubygame/color/models/hsl.rb +153 -0
  63. data/lib/rubygame/color/models/hsv.rb +149 -0
  64. data/lib/rubygame/color/models/rgb.rb +78 -0
  65. data/lib/rubygame/color/palettes/css.rb +49 -0
  66. data/lib/rubygame/color/palettes/palette.rb +100 -0
  67. data/lib/rubygame/color/palettes/x11.rb +177 -0
  68. data/lib/rubygame/constants.rb +238 -0
  69. data/lib/rubygame/event.rb +313 -0
  70. data/lib/rubygame/ftor.rb +370 -0
  71. data/lib/rubygame/hotspot.rb +265 -0
  72. data/lib/rubygame/keyconstants.rb +237 -0
  73. data/lib/rubygame/mediabag.rb +94 -0
  74. data/lib/rubygame/named_resource.rb +254 -0
  75. data/lib/rubygame/queue.rb +288 -0
  76. data/lib/rubygame/rect.rb +612 -0
  77. data/lib/rubygame/sfont.rb +223 -0
  78. data/lib/rubygame/sprite.rb +511 -0
  79. data/samples/FreeSans.ttf +0 -0
  80. data/samples/GPL.txt +340 -0
  81. data/samples/README +40 -0
  82. data/samples/chimp.bmp +0 -0
  83. data/samples/chimp.rb +302 -0
  84. data/samples/demo_gl.rb +151 -0
  85. data/samples/demo_gl_tex.rb +197 -0
  86. data/samples/demo_music.rb +77 -0
  87. data/samples/demo_rubygame.rb +296 -0
  88. data/samples/demo_sfont.rb +52 -0
  89. data/samples/demo_ttf.rb +193 -0
  90. data/samples/demo_utf8.rb +53 -0
  91. data/samples/fist.bmp +0 -0
  92. data/samples/load_and_blit.rb +22 -0
  93. data/samples/panda.png +0 -0
  94. data/samples/punch.wav +0 -0
  95. data/samples/ruby.png +0 -0
  96. data/samples/song.ogg +0 -0
  97. data/samples/term16.png +0 -0
  98. data/samples/whiff.wav +0 -0
  99. metadata +164 -0
@@ -0,0 +1,237 @@
1
+ # :enddoc:
2
+ module Rubygame
3
+ ALL_KEYS = {\
4
+ K_BACKSPACE => ["\b", "backspace"],\
5
+ K_TAB => ["\t", "tab"],\
6
+ K_CLEAR => [nil, "clear"],\
7
+ K_RETURN => ["\n", "return", "enter"],\
8
+ K_PAUSE => [nil, "pause"],\
9
+ K_ESCAPE => ["^[", "escape"],\
10
+ K_SPACE => [" ", "space"],\
11
+ K_EXCLAIM => ["!", "exclamation_mark", "exclaim"],\
12
+ K_QUOTEDBL => ["\"", "double_quote"],\
13
+ K_HASH => ["#", "hash", "pound", "bang"],\
14
+ K_DOLLAR => ["$", "dollar"],\
15
+ K_AMPERSAND => ["&", "ampersand", "and"],\
16
+ K_QUOTE => ["'", "quote"],\
17
+ K_LEFTPAREN => ["(", "left_parenthesis", "lparen"],\
18
+ K_RIGHTPAREN => [")", "right_parenthesis","rparen"],\
19
+ K_ASTERISK => ["*", "asterisk"],\
20
+ K_PLUS => ["+", "plus"],\
21
+ K_COMMA => [",", "comma"],\
22
+ K_MINUS => ["-", "minus"],\
23
+ K_PERIOD => [".", "period"],\
24
+ K_SLASH => ["/", "slash"],\
25
+ K_0 => ["0", "numberline_0"],\
26
+ K_1 => ["1", "numberline_1"],\
27
+ K_2 => ["2", "numberline_2"],\
28
+ K_3 => ["3", "numberline_3"],\
29
+ K_4 => ["4", "numberline_4"],\
30
+ K_5 => ["5", "numberline_5"],\
31
+ K_6 => ["6", "numberline_6"],\
32
+ K_7 => ["7", "numberline_7"],\
33
+ K_8 => ["8", "numberline_8"],\
34
+ K_9 => ["9", "numberline_9"],\
35
+ K_COLON => [":", "colon"],\
36
+ K_SEMICOLON => [";", "semicolon"],\
37
+ K_LESS => ["<", "less_than"],\
38
+ K_EQUALS => ["=", "equals"],\
39
+ K_GREATER => [">", "greater_than"],\
40
+ K_QUESTION => ["?", "question_mark", "question"],\
41
+ K_AT => ["@", "at"],\
42
+ K_LEFTBRACKET => ["[", "left_bracket", "lbracket"],\
43
+ K_BACKSLASH => ["\\", "backslash"],\
44
+ K_RIGHTBRACKET => ["]", "right_bracket", "rbracket"],\
45
+ K_CARET => ["^", "caret"],\
46
+ K_UNDERSCORE => ["_", "underscore"],\
47
+ K_BACKQUOTE => ["`", "backquote", "grave"],\
48
+ K_A => ["a"],\
49
+ K_B => ["b"],\
50
+ K_C => ["c"],\
51
+ K_D => ["d"],\
52
+ K_E => ["e"],\
53
+ K_F => ["f"],\
54
+ K_G => ["g"],\
55
+ K_H => ["h"],\
56
+ K_I => ["i"],\
57
+ K_J => ["j"],\
58
+ K_K => ["k"],\
59
+ K_L => ["l"],\
60
+ K_M => ["m"],\
61
+ K_N => ["n"],\
62
+ K_O => ["o"],\
63
+ K_P => ["p"],\
64
+ K_Q => ["q"],\
65
+ K_R => ["r"],\
66
+ K_S => ["s"],\
67
+ K_T => ["t"],\
68
+ K_U => ["u"],\
69
+ K_V => ["v"],\
70
+ K_W => ["w"],\
71
+ K_X => ["x"],\
72
+ K_Y => ["y"],\
73
+ K_Z => ["z"],\
74
+ K_DELETE => [nil, "delete", "del"],\
75
+ K_KP0 => ["0", "kp0", "keypad_0"],\
76
+ K_KP1 => ["1", "kp1", "keypad_1"],\
77
+ K_KP2 => ["2", "kp2", "keypad_2"],\
78
+ K_KP3 => ["3", "kp3", "keypad_3"],\
79
+ K_KP4 => ["4", "kp4", "keypad_4"],\
80
+ K_KP5 => ["5", "kp5", "keypad_5"],\
81
+ K_KP6 => ["6", "kp6", "keypad_6"],\
82
+ K_KP7 => ["7", "kp7", "keypad_7"],\
83
+ K_KP8 => ["8", "kp8", "keypad_8"],\
84
+ K_KP9 => ["9", "kp9", "keypad_9"],\
85
+ K_KP_PERIOD => [".", "period", "keypad_period"],\
86
+ K_KP_DIVIDE => ["/", "divide", "keypad_divide"],\
87
+ K_KP_MULTIPLY => ["*", "multiply", "keypad_multiply"],\
88
+ K_KP_MINUS => ["-", "minus", "keypad_minus"],\
89
+ K_KP_PLUS => ["+", "plus", "keypad_plus"],\
90
+ K_KP_ENTER => ["\n", "enter", "keypad_enter"],\
91
+ K_KP_EQUALS => ["=", "equals", "keypad_equals"],\
92
+ K_UP => [nil, "up"],\
93
+ K_DOWN => [nil, "down"],\
94
+ K_RIGHT => [nil, "right"],\
95
+ K_LEFT => [nil, "left"],\
96
+ K_INSERT => [nil, "insert", "ins"],\
97
+ K_HOME => [nil, "home"],\
98
+ K_END => [nil, "end"],\
99
+ K_PAGEUP => [nil, "page_up", "pgup"],\
100
+ K_PAGEDOWN => [nil, "page_down", "pgdn"],\
101
+ K_F1 => [nil, "f1", "function_1"],\
102
+ K_F2 => [nil, "f2", "function_2"],\
103
+ K_F3 => [nil, "f3", "function_3"],\
104
+ K_F4 => [nil, "f4", "function_4"],\
105
+ K_F5 => [nil, "f5", "function_5"],\
106
+ K_F6 => [nil, "f6", "function_6"],\
107
+ K_F7 => [nil, "f7", "function_7"],\
108
+ K_F8 => [nil, "f8", "function_8"],\
109
+ K_F9 => [nil, "f9", "function_9"],\
110
+ K_F10 => [nil, "f10", "function_10"],\
111
+ K_F11 => [nil, "f11", "function_11"],\
112
+ K_F12 => [nil, "f12", "function_12"],\
113
+ K_F13 => [nil, "f13", "function_13"],\
114
+ K_F14 => [nil, "f14", "function_14"],\
115
+ K_F15 => [nil, "f15", "function_15"],\
116
+ K_NUMLOCK => [nil, "numlock"],\
117
+ K_CAPSLOCK => [nil, "capslock", "caplock"],\
118
+ K_SCROLLOCK => [nil, "scroll_lock", "scrollock", "scrolllock"],\
119
+ K_LSHIFT => [nil, "left_shift", "lshift"],\
120
+ K_RSHIFT => [nil, "right_shift", "rshift"],\
121
+ K_LCTRL => [nil, "left_control", "lctrl"],\
122
+ K_RCTRL => [nil, "right_control", "rctrl"],\
123
+ K_LALT => [nil, "left_alt", "lalt"],\
124
+ K_RALT => [nil, "right_alt", "ralt"],\
125
+ K_LMETA => [nil, "left_meta", "lmeta"],\
126
+ K_RMETA => [nil, "right_meta", "rmeta"],\
127
+ K_LSUPER => [nil, "left_super", "lsuper"],\
128
+ K_RSUPER => [nil, "right_super", "rsuper"],\
129
+ K_MODE => [nil, "mode"],\
130
+ K_HELP => [nil, "help"],\
131
+ K_PRINT => [nil, "print_screen","prt_scr","print"],\
132
+ K_SYSREQ => [nil, "sysrq", "sysreq", "system_request"],\
133
+ K_BREAK => [nil, "break"],\
134
+ K_MENU => [nil, "menu"],\
135
+ K_POWER => [nil, "power"],\
136
+ # I have no idea how to get a Euro symbol here!
137
+ K_EURO => ["\u20ac", "euro"],\
138
+ # just K_WORLD_0 to K_WORLD_95 after this
139
+ K_WORLD_0 => [nil, "world_0"],\
140
+ K_WORLD_1 => [nil, "world_1"],\
141
+ K_WORLD_2 => [nil, "world_2"],\
142
+ K_WORLD_3 => [nil, "world_3"],\
143
+ K_WORLD_4 => [nil, "world_4"],\
144
+ K_WORLD_5 => [nil, "world_5"],\
145
+ K_WORLD_6 => [nil, "world_6"],\
146
+ K_WORLD_7 => [nil, "world_7"],\
147
+ K_WORLD_8 => [nil, "world_8"],\
148
+ K_WORLD_9 => [nil, "world_9"],\
149
+ K_WORLD_10 => [nil, "world_10"],\
150
+ K_WORLD_11 => [nil, "world_11"],\
151
+ K_WORLD_12 => [nil, "world_12"],\
152
+ K_WORLD_13 => [nil, "world_13"],\
153
+ K_WORLD_14 => [nil, "world_14"],\
154
+ K_WORLD_15 => [nil, "world_15"],\
155
+ K_WORLD_16 => [nil, "world_16"],\
156
+ K_WORLD_17 => [nil, "world_17"],\
157
+ K_WORLD_18 => [nil, "world_18"],\
158
+ K_WORLD_19 => [nil, "world_19"],\
159
+ K_WORLD_20 => [nil, "world_20"],\
160
+ K_WORLD_21 => [nil, "world_21"],\
161
+ K_WORLD_22 => [nil, "world_22"],\
162
+ K_WORLD_23 => [nil, "world_23"],\
163
+ K_WORLD_24 => [nil, "world_24"],\
164
+ K_WORLD_25 => [nil, "world_25"],\
165
+ K_WORLD_26 => [nil, "world_26"],\
166
+ K_WORLD_27 => [nil, "world_27"],\
167
+ K_WORLD_28 => [nil, "world_28"],\
168
+ K_WORLD_29 => [nil, "world_29"],\
169
+ K_WORLD_30 => [nil, "world_30"],\
170
+ K_WORLD_31 => [nil, "world_31"],\
171
+ K_WORLD_32 => [nil, "world_32"],\
172
+ K_WORLD_33 => [nil, "world_33"],\
173
+ K_WORLD_34 => [nil, "world_34"],\
174
+ K_WORLD_35 => [nil, "world_35"],\
175
+ K_WORLD_36 => [nil, "world_36"],\
176
+ K_WORLD_37 => [nil, "world_37"],\
177
+ K_WORLD_38 => [nil, "world_38"],\
178
+ K_WORLD_39 => [nil, "world_39"],\
179
+ K_WORLD_40 => [nil, "world_40"],\
180
+ K_WORLD_41 => [nil, "world_41"],\
181
+ K_WORLD_41 => [nil, "world_41"],\
182
+ K_WORLD_42 => [nil, "world_42"],\
183
+ K_WORLD_43 => [nil, "world_43"],\
184
+ K_WORLD_44 => [nil, "world_44"],\
185
+ K_WORLD_45 => [nil, "world_45"],\
186
+ K_WORLD_46 => [nil, "world_46"],\
187
+ K_WORLD_47 => [nil, "world_47"],\
188
+ K_WORLD_48 => [nil, "world_48"],\
189
+ K_WORLD_49 => [nil, "world_49"],\
190
+ K_WORLD_50 => [nil, "world_50"],\
191
+ K_WORLD_51 => [nil, "world_51"],\
192
+ K_WORLD_52 => [nil, "world_52"],\
193
+ K_WORLD_53 => [nil, "world_53"],\
194
+ K_WORLD_54 => [nil, "world_54"],\
195
+ K_WORLD_55 => [nil, "world_55"],\
196
+ K_WORLD_56 => [nil, "world_56"],\
197
+ K_WORLD_57 => [nil, "world_57"],\
198
+ K_WORLD_58 => [nil, "world_58"],\
199
+ K_WORLD_59 => [nil, "world_59"],\
200
+ K_WORLD_60 => [nil, "world_60"],\
201
+ K_WORLD_61 => [nil, "world_61"],\
202
+ K_WORLD_62 => [nil, "world_62"],\
203
+ K_WORLD_63 => [nil, "world_63"],\
204
+ K_WORLD_64 => [nil, "world_64"],\
205
+ K_WORLD_65 => [nil, "world_65"],\
206
+ K_WORLD_66 => [nil, "world_66"],\
207
+ K_WORLD_67 => [nil, "world_67"],\
208
+ K_WORLD_68 => [nil, "world_68"],\
209
+ K_WORLD_69 => [nil, "world_69"],\
210
+ K_WORLD_70 => [nil, "world_70"],\
211
+ K_WORLD_71 => [nil, "world_71"],\
212
+ K_WORLD_72 => [nil, "world_72"],\
213
+ K_WORLD_73 => [nil, "world_73"],\
214
+ K_WORLD_74 => [nil, "world_74"],\
215
+ K_WORLD_75 => [nil, "world_75"],\
216
+ K_WORLD_76 => [nil, "world_76"],\
217
+ K_WORLD_77 => [nil, "world_77"],\
218
+ K_WORLD_78 => [nil, "world_78"],\
219
+ K_WORLD_79 => [nil, "world_79"],\
220
+ K_WORLD_80 => [nil, "world_80"],\
221
+ K_WORLD_81 => [nil, "world_81"],\
222
+ K_WORLD_82 => [nil, "world_82"],\
223
+ K_WORLD_83 => [nil, "world_83"],\
224
+ K_WORLD_84 => [nil, "world_84"],\
225
+ K_WORLD_85 => [nil, "world_85"],\
226
+ K_WORLD_86 => [nil, "world_86"],\
227
+ K_WORLD_87 => [nil, "world_87"],\
228
+ K_WORLD_88 => [nil, "world_88"],\
229
+ K_WORLD_89 => [nil, "world_89"],\
230
+ K_WORLD_90 => [nil, "world_90"],\
231
+ K_WORLD_91 => [nil, "world_91"],\
232
+ K_WORLD_92 => [nil, "world_92"],\
233
+ K_WORLD_93 => [nil, "world_93"],\
234
+ K_WORLD_94 => [nil, "world_94"],\
235
+ K_WORLD_95 => [nil, "world_95"],\
236
+ }
237
+ end
@@ -0,0 +1,94 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2004-2007 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+ require "rubygame"
21
+
22
+ module Rubygame
23
+
24
+ # *NOTE*: you must require 'rubygame/mediabag' manually to gain access to
25
+ # Rubygame::MediaBag. It is not imported with Rubygame by default!
26
+ #
27
+ # A Hash-like class which will load and retain media files (images and
28
+ # sounds), so that the file can be loaded once, but used many times.
29
+ #
30
+ # The first time a file is requested with the #[] method,that file will be
31
+ # loaded into memory. All subsequent requests for the same file will return
32
+ # a reference to the already-loaded version. Ideally, objects should not
33
+ # have to know whether or not the image has been loaded or not.
34
+ class MediaBag
35
+ @@image_ext = %W{bmp gif jpg lbm pcx png pnm ppm pgm pbm tga tif xcf xpm}
36
+ @@sound_ext = %W{wav}
37
+
38
+ def initialize()
39
+ @media = Hash.new
40
+ end
41
+
42
+ # Return a reference to the stored value for key.
43
+ # If there is no value for key, automatically attempt to load key
44
+ # as a filename (guessing the file type based on its extension)
45
+ #
46
+ def [](key)
47
+ @media[key] or load(key)
48
+ rescue Rubygame::SDLError
49
+ nil
50
+ end
51
+
52
+ # Load the file, but only if it has not been previously loaded.
53
+ def load(filename)
54
+ @media[filename] or store( filename, load_file(filename) )
55
+ end
56
+
57
+ # Store value as key, but only if there is no previous value.
58
+ def store(key,value)
59
+ @media[key] ||= value
60
+ end
61
+
62
+ # Forcibly (re)load the file, replacing the previous version in memory
63
+ # (if any).
64
+ def force_load(filename)
65
+ force_store( filename, load_file(filename) )
66
+ end
67
+
68
+ # Forcibly store value as key, replacing the previous value (if any).
69
+ def force_store(key,value)
70
+ @media[key] = value
71
+ end
72
+
73
+ def load_file(filename)
74
+ case File::extname(filename).downcase[1..-1]
75
+ when *(@@image_ext)
76
+ return load_image(filename)
77
+ when *(@@sound_ext)
78
+ return load_sound(filename)
79
+ else
80
+ raise(ArgumentError,"Unrecognized file extension `%s': %s"%
81
+ [File::extname(filename), filename])
82
+ end
83
+ end
84
+
85
+ def load_image(filename)
86
+ return Rubygame::Surface.load_image(filename)
87
+ end
88
+
89
+ def load_sound(filename)
90
+ return Rubygame::Mixer::Sample.load_audio(filename)
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,254 @@
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2004-2008 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+
21
+ module Rubygame
22
+
23
+ # NamedResource is a mix-in module to implement a globally-available
24
+ # resource table, a @name variable and accessors, and a system for
25
+ # automatically loading resources when they are first needed.
26
+ #
27
+ # This module is used for Rubygame::Music, Rubygame::Sound, and
28
+ # Rubygame::Surface. You can use it in your own classes this way:
29
+ #
30
+ # 1. Do 'include Rubygame::NamedResource' in your class definition.
31
+ #
32
+ # 2. Set MyClass.autoload_dirs to an Array of directories to look
33
+ # for files when autoloading. Tip: use File.join to create
34
+ # paths that work on any operating system.
35
+ #
36
+ # 3. Define #autoload to implement the behavior for your class,
37
+ # or leave it as the default if you don't need autoloading.
38
+ # See the docs for #autoload for more information.
39
+ #
40
+ # Here's an example of how you could use this for a class which
41
+ # loads maps from a file:
42
+ #
43
+ # class Map
44
+ # include Rubygame::NamedResource
45
+ #
46
+ # Map.autoload_dirs = [ File.join("maps","world_1"),
47
+ # File.join("maps","custom") ]
48
+ #
49
+ # def autoload( name )
50
+ # # Searches autoload_dirs for the file
51
+ # path = find_file( name )
52
+ #
53
+ # if( path )
54
+ # return load_map( path )
55
+ # else
56
+ # return nil
57
+ # end
58
+ # end
59
+ #
60
+ # def load_map( path )
61
+ # # Your code to do the real loading, then return
62
+ # # the created instance of Map class.
63
+ # # ...
64
+ # return map_instance
65
+ # end
66
+ # end
67
+ #
68
+ # Here's an example of how you could then use the Map class:
69
+ #
70
+ # map = Map["level_1.map"]
71
+ #
72
+ # if( map )
73
+ # start_playing( map )
74
+ # else
75
+ # raise "Oops! The map file for Level 1 doesn't exist!"
76
+ # end
77
+ #
78
+ module NamedResource
79
+
80
+
81
+ # Adds class methods when the NamedResource module is included
82
+ # in a class. (Here, we are assuming that the NamedResource
83
+ # module was included in a class called MyClass.)
84
+ module NamedResourceClassMethods
85
+
86
+ # An Array of paths to check for files. See #find_file.
87
+ attr_accessor :autoload_dirs
88
+
89
+
90
+ # call-seq:
91
+ # MyClass[ name ] -> instance or nil
92
+ #
93
+ # Retrieves an instance of the class from a per-class resource
94
+ # table (Hash).
95
+ #
96
+ # If no object has been saved under the given name, invoke
97
+ # #autoload to try to load a new instance, store it in the
98
+ # Hash table under this name, and sets the instance's @name
99
+ # to this name.
100
+ #
101
+ def []( name )
102
+ result = @resources[name]
103
+
104
+ if result.nil?
105
+ result = autoload(name)
106
+ if result
107
+ self[name] = result
108
+ result.name = name
109
+ end
110
+ end
111
+
112
+ return result
113
+ end
114
+
115
+
116
+ # call-seq:
117
+ # MyClass[ name ] = instance
118
+ #
119
+ # Stores an instance of the class in a per-class resource table
120
+ # (Hash) for future access. If another object is already stored
121
+ # with this name, the old record is lost.
122
+ #
123
+ # May raise: TypeError, if you try to store anything
124
+ # that is not kind of this class.
125
+ #
126
+ def []=( name, value )
127
+ if( value.kind_of? self )
128
+ @resources[name] = value
129
+ else
130
+ raise TypeError, "#{self}#[]= can only store instances of #{self}"
131
+ end
132
+ end
133
+
134
+ # call-seq:
135
+ # MyClass.autoload( name ) -> instance or nil
136
+ #
137
+ # This method is invoked when a non-existing resource is
138
+ # accessed with #[]. By default, this method simply returns
139
+ # nil, effectively disabling autoloading.
140
+ #
141
+ # You should override this method in your class to provide
142
+ # class-specific loading behavior, or leave it as the default if
143
+ # you don't need autoloading. Your method should return either
144
+ # an instance of the class, or nil.
145
+ #
146
+ # NOTE: The #find_file method is useful for getting the full
147
+ # path to a file which matches the name. That's what it's there
148
+ # for, so you should use it!
149
+ #
150
+ def autoload( name )
151
+ nil
152
+ end
153
+
154
+
155
+ # call-seq:
156
+ # MyClass.basename( path ) -> filename
157
+ #
158
+ # Returns the basename for the path (i.e. the
159
+ # filename without the directory). Same as
160
+ # File.basename
161
+ #
162
+ def basename( path )
163
+ File.basename( path )
164
+ end
165
+
166
+
167
+ # call-seq:
168
+ # MyClass.exist?( path ) -> true or false
169
+ #
170
+ # True if the given path points to a file
171
+ # that exists, otherwise false. Same as
172
+ # File.exist?
173
+ #
174
+ def exist?( path )
175
+ File.exist?(path)
176
+ end
177
+
178
+
179
+ # call-seq:
180
+ # MyClass.find_file( filename ) -> path or nil
181
+ #
182
+ # Checks every directory in @autoload_dirs for
183
+ # a file with the given name, and returns the
184
+ # path (directory and name) for the first match.
185
+ #
186
+ # If no directories have a file with that name,
187
+ # return nil.
188
+ #
189
+ def find_file( filename )
190
+ dir = @autoload_dirs.find { |dir|
191
+ exist?( File.join(dir,filename) )
192
+ }
193
+
194
+ if dir
195
+ return File.join(dir,filename)
196
+ else
197
+ return nil
198
+ end
199
+ end
200
+
201
+ end
202
+
203
+
204
+ # Sets up the class when this module is included.
205
+ # Adds the class methods and defines class instance
206
+ # variables.
207
+ def self.included( object ) # :nodoc:
208
+
209
+ class << object
210
+ include NamedResourceClassMethods
211
+ end
212
+
213
+ object.instance_eval do
214
+ @resources = Hash.new
215
+ @autoload_dirs = []
216
+ end
217
+
218
+ end
219
+
220
+
221
+ # Returns the instance's @name. See also #name=.
222
+ def name
223
+ @name
224
+ end
225
+
226
+ #
227
+ # Sets the instance's @name to the given String, or nil to
228
+ # unset the name. See also #name.
229
+ #
230
+ # NOTE: This does not automatically store the instance in the
231
+ # class resource table by name. Use the #[]= class method to do
232
+ # that.
233
+ #
234
+ # The string is dup'ed and frozen before being stored.
235
+ #
236
+ # May raise: TypeError, if new_name is not a String or nil.
237
+ #
238
+ def name=( new_name )
239
+ if new_name.nil?
240
+ return @name = nil
241
+ end
242
+
243
+ unless new_name.kind_of? String
244
+ raise TypeError, "name must be a String (got #{new_name.class})"
245
+ end
246
+
247
+ @name = new_name.dup
248
+ @name.freeze
249
+ end
250
+
251
+
252
+ end
253
+
254
+ end