gosu 0.7.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (249) hide show
  1. data/COPYING.txt +29 -0
  2. data/Gosu/Async.hpp +48 -0
  3. data/Gosu/Audio.hpp +145 -0
  4. data/Gosu/AutoLink.hpp +16 -0
  5. data/Gosu/Bitmap.hpp +85 -0
  6. data/Gosu/ButtonsMac.hpp +114 -0
  7. data/Gosu/ButtonsWin.hpp +111 -0
  8. data/Gosu/ButtonsX.hpp +115 -0
  9. data/Gosu/Color.hpp +172 -0
  10. data/Gosu/Directories.hpp +36 -0
  11. data/Gosu/Font.hpp +59 -0
  12. data/Gosu/Fwd.hpp +31 -0
  13. data/Gosu/Gosu.hpp +26 -0
  14. data/Gosu/Graphics.hpp +86 -0
  15. data/Gosu/GraphicsBase.hpp +45 -0
  16. data/Gosu/IO.hpp +255 -0
  17. data/Gosu/Image.hpp +148 -0
  18. data/Gosu/ImageData.hpp +45 -0
  19. data/Gosu/Input.hpp +116 -0
  20. data/Gosu/Math.hpp +95 -0
  21. data/Gosu/Platform.hpp +61 -0
  22. data/Gosu/RotFlip.hpp +116 -0
  23. data/Gosu/Sockets.hpp +129 -0
  24. data/Gosu/Text.hpp +47 -0
  25. data/Gosu/TextInput.hpp +57 -0
  26. data/Gosu/Timing.hpp +16 -0
  27. data/Gosu/Utility.hpp +24 -0
  28. data/Gosu/WinUtility.hpp +76 -0
  29. data/Gosu/Window.hpp +84 -0
  30. data/GosuImpl/Async.cpp +37 -0
  31. data/GosuImpl/AudioFmod.cpp +417 -0
  32. data/GosuImpl/AudioSDL.cpp +255 -0
  33. data/GosuImpl/DirectoriesMac.mm +38 -0
  34. data/GosuImpl/DirectoriesUnix.cpp +48 -0
  35. data/GosuImpl/DirectoriesWin.cpp +42 -0
  36. data/GosuImpl/FileUnix.cpp +100 -0
  37. data/GosuImpl/FileWin.cpp +83 -0
  38. data/GosuImpl/Graphics/Bitmap.cpp +116 -0
  39. data/GosuImpl/Graphics/BitmapBMP.cpp +232 -0
  40. data/GosuImpl/Graphics/BitmapColorKey.cpp +39 -0
  41. data/GosuImpl/Graphics/BitmapPNG.cpp +276 -0
  42. data/GosuImpl/Graphics/BitmapUtils.cpp +67 -0
  43. data/GosuImpl/Graphics/BlockAllocator.cpp +127 -0
  44. data/GosuImpl/Graphics/BlockAllocator.hpp +34 -0
  45. data/GosuImpl/Graphics/Color.cpp +126 -0
  46. data/GosuImpl/Graphics/Common.hpp +21 -0
  47. data/GosuImpl/Graphics/DrawOp.hpp +154 -0
  48. data/GosuImpl/Graphics/Font.cpp +110 -0
  49. data/GosuImpl/Graphics/Graphics.cpp +295 -0
  50. data/GosuImpl/Graphics/Image.cpp +159 -0
  51. data/GosuImpl/Graphics/LargeImageData.cpp +115 -0
  52. data/GosuImpl/Graphics/LargeImageData.hpp +37 -0
  53. data/GosuImpl/Graphics/RotFlip.cpp +184 -0
  54. data/GosuImpl/Graphics/TexChunk.cpp +77 -0
  55. data/GosuImpl/Graphics/TexChunk.hpp +40 -0
  56. data/GosuImpl/Graphics/Text.cpp +223 -0
  57. data/GosuImpl/Graphics/TextMac.cpp +242 -0
  58. data/GosuImpl/Graphics/TextPangoFT.cpp +186 -0
  59. data/GosuImpl/Graphics/TextWin.cpp +172 -0
  60. data/GosuImpl/Graphics/Texture.cpp +104 -0
  61. data/GosuImpl/Graphics/Texture.hpp +34 -0
  62. data/GosuImpl/IO.cpp +48 -0
  63. data/GosuImpl/InputMac.mm +677 -0
  64. data/GosuImpl/InputWin.cpp +444 -0
  65. data/GosuImpl/InputX.cpp +158 -0
  66. data/GosuImpl/MacUtility.hpp +48 -0
  67. data/GosuImpl/Math.cpp +49 -0
  68. data/GosuImpl/RubyGosu.swg +474 -0
  69. data/GosuImpl/RubyGosuStub.mm +17 -0
  70. data/GosuImpl/RubyGosu_DllMain.cxx +30 -0
  71. data/GosuImpl/RubyGosu_wrap.cxx +8521 -0
  72. data/GosuImpl/RubyGosu_wrap.h +31 -0
  73. data/GosuImpl/Sockets/CommSocket.cpp +304 -0
  74. data/GosuImpl/Sockets/ListenerSocket.cpp +60 -0
  75. data/GosuImpl/Sockets/MessageSocket.cpp +136 -0
  76. data/GosuImpl/Sockets/Socket.cpp +145 -0
  77. data/GosuImpl/Sockets/Sockets.hpp +66 -0
  78. data/GosuImpl/TextInputMac.mm +207 -0
  79. data/GosuImpl/TextInputWin.cpp +197 -0
  80. data/GosuImpl/TextInputX.cpp +201 -0
  81. data/GosuImpl/TextTTFWin.cpp +247 -0
  82. data/GosuImpl/TimingUnix.cpp +17 -0
  83. data/GosuImpl/TimingWin.cpp +28 -0
  84. data/GosuImpl/Utility.cpp +140 -0
  85. data/GosuImpl/WinMain.cpp +69 -0
  86. data/GosuImpl/WinUtility.cpp +137 -0
  87. data/GosuImpl/WindowMac.mm +466 -0
  88. data/GosuImpl/WindowWin.cpp +447 -0
  89. data/GosuImpl/WindowX.cpp +392 -0
  90. data/GosuImpl/X11vroot.h +118 -0
  91. data/README.txt +13 -0
  92. data/Rakefile +178 -0
  93. data/examples/ChipmunkIntegration.rb +275 -0
  94. data/examples/CptnRuby.rb +231 -0
  95. data/examples/MoreChipmunkAndRMagick.rb +155 -0
  96. data/examples/OpenGLIntegration.rb +232 -0
  97. data/examples/RMagickIntegration.rb +449 -0
  98. data/examples/TextInput.cpp +170 -0
  99. data/examples/TextInput.rb +139 -0
  100. data/examples/Tutorial.cpp +215 -0
  101. data/examples/Tutorial.rb +137 -0
  102. data/examples/media/Beep.wav +0 -0
  103. data/examples/media/CptnRuby Gem.png +0 -0
  104. data/examples/media/CptnRuby Map.txt +25 -0
  105. data/examples/media/CptnRuby Tileset.png +0 -0
  106. data/examples/media/CptnRuby.png +0 -0
  107. data/examples/media/Cursor.png +0 -0
  108. data/examples/media/Earth.png +0 -0
  109. data/examples/media/Explosion.wav +0 -0
  110. data/examples/media/LargeStar.png +0 -0
  111. data/examples/media/Sky.jpg +0 -0
  112. data/examples/media/Smoke.png +0 -0
  113. data/examples/media/Soldier.png +0 -0
  114. data/examples/media/Space.png +0 -0
  115. data/examples/media/Star.png +0 -0
  116. data/examples/media/Starfighter.bmp +0 -0
  117. data/linux/Makefile.in +98 -0
  118. data/linux/configure +5658 -0
  119. data/linux/configure.ac +126 -0
  120. data/linux/extconf.rb +11 -0
  121. data/mac/English.lproj/InfoPlist.strings +0 -0
  122. data/mac/Gosu-Info.plist +26 -0
  123. data/mac/Gosu.xcodeproj/project.pbxproj +1194 -0
  124. data/mac/RubyGosu Template-Info.plist +26 -0
  125. data/mac/libboost_thread_1_34_1_universal.a +0 -0
  126. data/mac/libboost_thread_d_1_34_1_universal.a +0 -0
  127. data/mac/libfmod_universal.a +0 -0
  128. data/mac/libpng_universal.a +0 -0
  129. data/mac/libz_universal.a +0 -0
  130. data/reference/Async_8hpp-source.html +70 -0
  131. data/reference/Audio_8hpp-source.html +114 -0
  132. data/reference/Audio_8hpp.html +50 -0
  133. data/reference/AutoLink_8hpp-source.html +38 -0
  134. data/reference/AutoLink_8hpp.html +34 -0
  135. data/reference/Bitmap_8hpp-source.html +85 -0
  136. data/reference/Bitmap_8hpp.html +58 -0
  137. data/reference/ButtonsMac_8hpp-source.html +133 -0
  138. data/reference/ButtonsWin_8hpp-source.html +133 -0
  139. data/reference/ButtonsX_8hpp-source.html +134 -0
  140. data/reference/Color_8hpp-source.html +169 -0
  141. data/reference/Color_8hpp.html +85 -0
  142. data/reference/Directories_8hpp-source.html +42 -0
  143. data/reference/Directories_8hpp.html +46 -0
  144. data/reference/Font_8hpp-source.html +65 -0
  145. data/reference/Font_8hpp.html +41 -0
  146. data/reference/Fwd_8hpp-source.html +52 -0
  147. data/reference/Fwd_8hpp.html +37 -0
  148. data/reference/Gosu_8hpp-source.html +48 -0
  149. data/reference/Gosu_8hpp.html +34 -0
  150. data/reference/GraphicsBase_8hpp-source.html +57 -0
  151. data/reference/GraphicsBase_8hpp.html +56 -0
  152. data/reference/Graphics_8hpp-source.html +96 -0
  153. data/reference/Graphics_8hpp.html +53 -0
  154. data/reference/IO_8hpp-source.html +255 -0
  155. data/reference/IO_8hpp.html +74 -0
  156. data/reference/ImageData_8hpp-source.html +62 -0
  157. data/reference/ImageData_8hpp.html +43 -0
  158. data/reference/Image_8hpp-source.html +126 -0
  159. data/reference/Image_8hpp.html +48 -0
  160. data/reference/Input_8hpp-source.html +118 -0
  161. data/reference/Input_8hpp.html +50 -0
  162. data/reference/Math_8hpp-source.html +92 -0
  163. data/reference/Math_8hpp.html +74 -0
  164. data/reference/Platform_8hpp-source.html +83 -0
  165. data/reference/Platform_8hpp.html +73 -0
  166. data/reference/RotFlip_8hpp-source.html +138 -0
  167. data/reference/RotFlip_8hpp.html +77 -0
  168. data/reference/Sockets_8hpp-source.html +130 -0
  169. data/reference/Sockets_8hpp.html +66 -0
  170. data/reference/TextInput_8hpp-source.html +64 -0
  171. data/reference/TextInput_8hpp.html +41 -0
  172. data/reference/Text_8hpp-source.html +51 -0
  173. data/reference/Text_8hpp.html +46 -0
  174. data/reference/Timing_8hpp-source.html +36 -0
  175. data/reference/Timing_8hpp.html +42 -0
  176. data/reference/Utility_8hpp-source.html +44 -0
  177. data/reference/Utility_8hpp.html +48 -0
  178. data/reference/WinUtility_8hpp-source.html +79 -0
  179. data/reference/WinUtility_8hpp.html +64 -0
  180. data/reference/Window_8hpp-source.html +91 -0
  181. data/reference/Window_8hpp.html +41 -0
  182. data/reference/annotated.html +51 -0
  183. data/reference/classGosu_1_1Audio-members.html +34 -0
  184. data/reference/classGosu_1_1Audio.html +46 -0
  185. data/reference/classGosu_1_1Bitmap-members.html +44 -0
  186. data/reference/classGosu_1_1Bitmap.html +263 -0
  187. data/reference/classGosu_1_1Buffer-members.html +44 -0
  188. data/reference/classGosu_1_1Buffer.html +78 -0
  189. data/reference/classGosu_1_1Buffer.png +0 -0
  190. data/reference/classGosu_1_1Button-members.html +36 -0
  191. data/reference/classGosu_1_1Button.html +143 -0
  192. data/reference/classGosu_1_1Color-members.html +56 -0
  193. data/reference/classGosu_1_1Color.html +387 -0
  194. data/reference/classGosu_1_1File-members.html +41 -0
  195. data/reference/classGosu_1_1File.html +69 -0
  196. data/reference/classGosu_1_1File.png +0 -0
  197. data/reference/classGosu_1_1Font-members.html +39 -0
  198. data/reference/classGosu_1_1Font.html +309 -0
  199. data/reference/classGosu_1_1Graphics-members.html +50 -0
  200. data/reference/classGosu_1_1Graphics.html +234 -0
  201. data/reference/classGosu_1_1Image-members.html +45 -0
  202. data/reference/classGosu_1_1Image.html +518 -0
  203. data/reference/classGosu_1_1ImageData-members.html +37 -0
  204. data/reference/classGosu_1_1ImageData.html +60 -0
  205. data/reference/classGosu_1_1Input-members.html +44 -0
  206. data/reference/classGosu_1_1Input.html +223 -0
  207. data/reference/classGosu_1_1MessageSocket-members.html +40 -0
  208. data/reference/classGosu_1_1MessageSocket.html +233 -0
  209. data/reference/classGosu_1_1Resource-members.html +39 -0
  210. data/reference/classGosu_1_1Resource.html +116 -0
  211. data/reference/classGosu_1_1Resource.png +0 -0
  212. data/reference/classGosu_1_1Sample-members.html +37 -0
  213. data/reference/classGosu_1_1Sample.html +200 -0
  214. data/reference/classGosu_1_1SampleInstance-members.html +38 -0
  215. data/reference/classGosu_1_1SampleInstance.html +169 -0
  216. data/reference/classGosu_1_1Song-members.html +43 -0
  217. data/reference/classGosu_1_1Song.html +260 -0
  218. data/reference/classGosu_1_1TextInput-members.html +38 -0
  219. data/reference/classGosu_1_1TextInput.html +121 -0
  220. data/reference/classGosu_1_1Window-members.html +50 -0
  221. data/reference/classGosu_1_1Window.html +271 -0
  222. data/reference/doxyfile +233 -0
  223. data/reference/doxygen.css +433 -0
  224. data/reference/doxygen.png +0 -0
  225. data/reference/files.html +54 -0
  226. data/reference/functions.html +236 -0
  227. data/reference/functions_enum.html +45 -0
  228. data/reference/functions_func.html +227 -0
  229. data/reference/functions_vars.html +47 -0
  230. data/reference/hierarchy.html +53 -0
  231. data/reference/index.html +26 -0
  232. data/reference/namespaceGosu.html +2890 -0
  233. data/reference/namespaceGosu_1_1Colors.html +70 -0
  234. data/reference/namespaceGosu_1_1Win.html +275 -0
  235. data/reference/namespacemembers.html +216 -0
  236. data/reference/namespacemembers_enum.html +52 -0
  237. data/reference/namespacemembers_eval.html +54 -0
  238. data/reference/namespacemembers_func.html +185 -0
  239. data/reference/namespacemembers_type.html +46 -0
  240. data/reference/namespacemembers_vars.html +46 -0
  241. data/reference/namespaces.html +35 -0
  242. data/reference/tab_b.gif +0 -0
  243. data/reference/tab_l.gif +0 -0
  244. data/reference/tab_r.gif +0 -0
  245. data/reference/tabs.css +102 -0
  246. data/windows/Gosu.sln +29 -0
  247. data/windows/Gosu.vcproj +553 -0
  248. data/windows/RubyGosu.vcproj +138 -0
  249. metadata +305 -0
@@ -0,0 +1,139 @@
1
+ # This example demonstrates the use of the TextInput functionality.
2
+ # One can tab through, or click into the text fields and change it's contents.
3
+
4
+ # At its most basic form, you only need to create a new TextInput instance and
5
+ # set the text_input attribute of your window to it. Until you set this
6
+ # attribute to nil again, the TextInput object will build a text that can be
7
+ # accessed via TextInput#text.
8
+
9
+ # The TextInput object also maintains the position of the caret as the index
10
+ # of the character that it's left to via the caret_pos attribute. Furthermore,
11
+ # if there is a selection, the selection_start attribute yields its beginning,
12
+ # using the same indexing scheme. If there is no selection, selection_start
13
+ # is equal to caret_pos.
14
+
15
+ # A TextInput object is purely abstract, though; drawing the input field is left
16
+ # to the user. In this case, we are subclassing TextInput to add this code.
17
+ # As with most of Gosu, how this is handled is completely left open; the scheme
18
+ # presented here is not mandatory! Gosu only aims to provide enough code for
19
+ # games (or intermediate UI toolkits) to be built upon it.
20
+
21
+ begin
22
+ # In case you use Gosu via RubyGems.
23
+ require 'rubygems'
24
+ rescue LoadError
25
+ # In case you don't.
26
+ end
27
+
28
+ require 'gosu'
29
+
30
+ class TextField < Gosu::TextInput
31
+ # Some constants that define our appearance.
32
+ INACTIVE_COLOR = 0xcc666666
33
+ ACTIVE_COLOR = 0xccff6666
34
+ SELECTION_COLOR = 0xcc0000ff
35
+ CARET_COLOR = 0xffffffff
36
+ PADDING = 5
37
+
38
+ attr_reader :x, :y
39
+
40
+ def initialize(window, font, x, y)
41
+ # TextInput's constructor doesn't expect any arguments.
42
+ super()
43
+
44
+ @window, @font, @x, @y = window, font, x, y
45
+
46
+ # Start with a self-explanatory text in each field.
47
+ self.text = "Click to change text"
48
+ end
49
+
50
+ def draw
51
+ # Depending on whether this is the currently selected input or not, change the
52
+ # background's color.
53
+ if @window.text_input == self then
54
+ background_color = ACTIVE_COLOR
55
+ else
56
+ background_color = INACTIVE_COLOR
57
+ end
58
+ @window.draw_quad(x - PADDING, y - PADDING, background_color,
59
+ x + width + PADDING, y - PADDING, background_color,
60
+ x - PADDING, y + height + PADDING, background_color,
61
+ x + width + PADDING, y + height + PADDING, background_color, 0)
62
+
63
+ # Calculate the position of the caret and the selection start.
64
+ pos_x = x + @font.text_width(self.text[0...self.caret_pos])
65
+ sel_x = x + @font.text_width(self.text[0...self.selection_start])
66
+
67
+ # Draw the selection background, if any; if not, sel_x and pos_x will be
68
+ # the same value, making this quad empty.
69
+ @window.draw_quad(sel_x, y, SELECTION_COLOR,
70
+ pos_x, y, SELECTION_COLOR,
71
+ sel_x, y + height, SELECTION_COLOR,
72
+ pos_x, y + height, SELECTION_COLOR, 0)
73
+
74
+ # Draw the caret; again, only if this is the currently selected field.
75
+ if @window.text_input == self then
76
+ @window.draw_line(pos_x, y, CARET_COLOR,
77
+ pos_x, y + height, CARET_COLOR, 0)
78
+ end
79
+
80
+ # Finally, draw the text itself!
81
+ @font.draw(self.text, x, y, 0)
82
+ end
83
+
84
+ # This text field grows with the text that's being entered.
85
+ # (Without clipping, one has to be a bit creative about this ;) )
86
+ def width
87
+ @font.text_width(self.text)
88
+ end
89
+
90
+ def height
91
+ @font.height
92
+ end
93
+
94
+ # Hit-test for selecting a text field with the mouse.
95
+ def under_point?(mouse_x, mouse_y)
96
+ mouse_x > x - PADDING and mouse_x < x + width + PADDING and
97
+ mouse_y > y - PADDING and mouse_y < y + height + PADDING
98
+ end
99
+ end
100
+
101
+ class TextInputWindow < Gosu::Window
102
+ def initialize
103
+ super(300, 200, false)
104
+ self.caption = "Text Input Example"
105
+
106
+ font = Gosu::Font.new(self, Gosu::default_font_name, 20)
107
+
108
+ # Set up an array of three text fields.
109
+ @text_fields = Array.new(3) { |index| TextField.new(self, font, 50, 30 + index * 50) }
110
+
111
+ @cursor = Gosu::Image.new(self, "media/Cursor.png", false)
112
+ end
113
+
114
+ def draw
115
+ @text_fields.each { |tf| tf.draw }
116
+ @cursor.draw(mouse_x, mouse_y, 0)
117
+ end
118
+
119
+ def button_down(id)
120
+ if id == Gosu::KbTab then
121
+ # Tab key will not be 'eaten' by text fields; use for switching through
122
+ # text fields.
123
+ index = @text_fields.index(self.text_input) || -1
124
+ self.text_input = @text_fields[(index + 1) % @text_fields.size]
125
+ elsif id == Gosu::KbEscape then
126
+ # Escape key will not be 'eaten' by text fields; use for deselecting.
127
+ if self.text_input then
128
+ self.text_input = nil
129
+ else
130
+ close
131
+ end
132
+ elsif id == Gosu::MsLeft then
133
+ # Mouse click: Select text field based on mouse position.
134
+ self.text_input = @text_fields.find { |tf| tf.under_point?(mouse_x, mouse_y) }
135
+ end
136
+ end
137
+ end
138
+
139
+ TextInputWindow.new.show
@@ -0,0 +1,215 @@
1
+ #include <Gosu/Gosu.hpp>
2
+ #include <Gosu/AutoLink.hpp> // Makes life easier for Windows users compiling this.
3
+
4
+ #include <boost/scoped_ptr.hpp> // Pointers used throughout Gosu and this tutorial.
5
+ #include <boost/shared_ptr.hpp> // Read about them, they're moving into standard C++! :)
6
+
7
+ #include <boost/lexical_cast.hpp> // Could also use <sstream>, just for int <-> string conversion
8
+
9
+ #include <cmath>
10
+ #include <cstdlib>
11
+ #include <list>
12
+ #include <vector>
13
+
14
+ enum ZOrder
15
+ {
16
+ zBackground,
17
+ zStars,
18
+ zPlayer,
19
+ zUI
20
+ };
21
+
22
+ typedef std::vector<boost::shared_ptr<Gosu::Image> > Animation;
23
+
24
+ class Star
25
+ {
26
+ Animation* animation;
27
+ Gosu::Color color;
28
+ double posX, posY;
29
+
30
+ public:
31
+ explicit Star(Animation& anim)
32
+ {
33
+ animation = &anim;
34
+
35
+ color.setAlpha(255);
36
+ double red = Gosu::random(40, 255);
37
+ color.setRed(static_cast<Gosu::Color::Channel>(red));
38
+ double green = Gosu::random(40, 255);
39
+ color.setGreen(static_cast<Gosu::Color::Channel>(green));
40
+ double blue = Gosu::random(40, 255);
41
+ color.setBlue(static_cast<Gosu::Color::Channel>(blue));
42
+
43
+ posX = Gosu::random(0, 640);
44
+ posY = Gosu::random(0, 480);
45
+ }
46
+
47
+ double x() const { return posX; }
48
+ double y() const { return posY; }
49
+
50
+ void draw() const
51
+ {
52
+ Gosu::Image& image =
53
+ *animation->at(Gosu::milliseconds() / 100 % animation->size());
54
+
55
+ image.draw(posX - image.width() / 2.0, posY - image.height() / 2.0,
56
+ zStars, 1, 1, color, Gosu::amAdditive);
57
+ }
58
+ };
59
+
60
+ class Player
61
+ {
62
+ boost::scoped_ptr<Gosu::Image> image;
63
+ boost::scoped_ptr<Gosu::Sample> beep;
64
+ double posX, posY, velX, velY, angle;
65
+ unsigned score;
66
+
67
+
68
+ public:
69
+ Player(Gosu::Graphics& graphics, Gosu::Audio& audio)
70
+ {
71
+ std::wstring filename = Gosu::sharedResourcePrefix() + L"media/Starfighter.bmp";
72
+ image.reset(new Gosu::Image(graphics, filename));
73
+
74
+ filename = Gosu::sharedResourcePrefix() + L"media/Beep.wav";
75
+ beep.reset(new Gosu::Sample(audio, filename));
76
+
77
+ posX = posY = velX = velY = angle = 0;
78
+ score = 0;
79
+ }
80
+
81
+ unsigned getScore() const
82
+ {
83
+ return score;
84
+ }
85
+
86
+ void warp(double x, double y)
87
+ {
88
+ posX = x;
89
+ posY = y;
90
+ }
91
+
92
+ void turnLeft()
93
+ {
94
+ angle -= 4.5;
95
+ }
96
+
97
+ void turnRight()
98
+ {
99
+ angle += 4.5;
100
+ }
101
+
102
+ void accelerate()
103
+ {
104
+ velX += Gosu::offsetX(angle, 0.5);
105
+ velY += Gosu::offsetY(angle, 0.5);
106
+ }
107
+
108
+ void move()
109
+ {
110
+ posX += velX;
111
+ while (posX < 0)
112
+ posX += 640;
113
+ while (posX > 640)
114
+ posX -= 640;
115
+
116
+ posY += velY;
117
+ while (posY < 0)
118
+ posY += 480;
119
+ while (posY > 480)
120
+ posY -= 480;
121
+
122
+ velX *= 0.95;
123
+ velY *= 0.95;
124
+ }
125
+
126
+ void draw() const
127
+ {
128
+ image->drawRot(posX, posY, zPlayer, angle);
129
+ }
130
+
131
+ void collectStars(std::list<Star>& stars)
132
+ {
133
+ std::list<Star>::iterator cur = stars.begin();
134
+ while (cur != stars.end())
135
+ {
136
+ if (Gosu::distance(posX, posY, cur->x(), cur->y()) < 35)
137
+ {
138
+ cur = stars.erase(cur);
139
+ score += 10;
140
+ beep->play();
141
+ }
142
+ else
143
+ ++cur;
144
+ }
145
+ }
146
+ };
147
+
148
+ class GameWindow : public Gosu::Window
149
+ {
150
+ boost::scoped_ptr<Gosu::Image> backgroundImage;
151
+ Animation starAnim;
152
+ Gosu::Font font;
153
+
154
+ Player player;
155
+ std::list<Star> stars;
156
+
157
+ public:
158
+ GameWindow()
159
+ : Window(640, 480, false),
160
+ font(graphics(), Gosu::defaultFontName(), 20),
161
+ player(graphics(), audio())
162
+ {
163
+ setCaption(L"Gosu Tutorial Game");
164
+
165
+ std::wstring filename = Gosu::sharedResourcePrefix() + L"media/Space.png";
166
+ backgroundImage.reset(new Gosu::Image(graphics(), filename, false));
167
+
168
+ filename = Gosu::sharedResourcePrefix() + L"media/Star.png";
169
+ Gosu::imagesFromTiledBitmap(graphics(), filename, 25, 25, false, starAnim);
170
+
171
+ player.warp(320, 240);
172
+ }
173
+
174
+ void update()
175
+ {
176
+ if (input().down(Gosu::kbLeft) || input().down(Gosu::gpLeft))
177
+ player.turnLeft();
178
+ if (input().down(Gosu::kbRight) || input().down(Gosu::gpRight))
179
+ player.turnRight();
180
+ if (input().down(Gosu::kbUp) || input().down(Gosu::gpButton0))
181
+ player.accelerate();
182
+ player.move();
183
+ player.collectStars(stars);
184
+
185
+ if (std::rand() % 25 == 0 && stars.size() < 25)
186
+ stars.push_back(Star(starAnim));
187
+ }
188
+
189
+ void draw()
190
+ {
191
+ player.draw();
192
+ backgroundImage->draw(0, 0, zBackground);
193
+
194
+ for (std::list<Star>::const_iterator i = stars.begin();
195
+ i != stars.end(); ++i)
196
+ {
197
+ i->draw();
198
+ }
199
+
200
+ font.draw(L"Score: " + boost::lexical_cast<std::wstring>(player.getScore()),
201
+ 10, 10, zUI, 1, 1, Gosu::Colors::yellow);
202
+ }
203
+
204
+ void buttonDown(Gosu::Button btn)
205
+ {
206
+ if (btn == Gosu::kbEscape)
207
+ close();
208
+ }
209
+ };
210
+
211
+ int main(int argc, char* argv[])
212
+ {
213
+ GameWindow window;
214
+ window.show();
215
+ }
@@ -0,0 +1,137 @@
1
+ begin
2
+ # In case you use Gosu via RubyGems.
3
+ require 'rubygems'
4
+ rescue LoadError
5
+ # In case you don't.
6
+ end
7
+
8
+ require 'gosu'
9
+
10
+ module ZOrder
11
+ Background, Stars, Player, UI = *0..3
12
+ end
13
+
14
+ class Player
15
+ attr_reader :score
16
+
17
+ def initialize(window)
18
+ @image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
19
+ @beep = Gosu::Sample.new(window, "media/Beep.wav")
20
+ @x = @y = @vel_x = @vel_y = @angle = 0.0
21
+ @score = 0
22
+ end
23
+
24
+ def warp(x, y)
25
+ @x, @y = x, y
26
+ end
27
+
28
+ def turn_left
29
+ @angle -= 4.5
30
+ end
31
+
32
+ def turn_right
33
+ @angle += 4.5
34
+ end
35
+
36
+ def accelerate
37
+ @vel_x += Gosu::offset_x(@angle, 0.5)
38
+ @vel_y += Gosu::offset_y(@angle, 0.5)
39
+ end
40
+
41
+ def move
42
+ @x += @vel_x
43
+ @y += @vel_y
44
+ @x %= 640
45
+ @y %= 480
46
+
47
+ @vel_x *= 0.95
48
+ @vel_y *= 0.95
49
+ end
50
+
51
+ def draw
52
+ @image.draw_rot(@x, @y, ZOrder::Player, @angle)
53
+ end
54
+
55
+ def collect_stars(stars)
56
+ stars.reject! do |star|
57
+ if Gosu::distance(@x, @y, star.x, star.y) < 35 then
58
+ @score += 10
59
+ @beep.play
60
+ true
61
+ else
62
+ false
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ class Star
69
+ attr_reader :x, :y
70
+
71
+ def initialize(animation)
72
+ @animation = animation
73
+ @color = Gosu::Color.new(0xff000000)
74
+ @color.red = rand(255 - 40) + 40
75
+ @color.green = rand(255 - 40) + 40
76
+ @color.blue = rand(255 - 40) + 40
77
+ @x = rand * 640
78
+ @y = rand * 480
79
+ end
80
+
81
+ def draw
82
+ img = @animation[Gosu::milliseconds / 100 % @animation.size]
83
+ img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
84
+ ZOrder::Stars, 1, 1, @color, :additive)
85
+ end
86
+ end
87
+
88
+ class GameWindow < Gosu::Window
89
+ def initialize
90
+ super(640, 480, false)
91
+ self.caption = "Gosu Tutorial Game"
92
+
93
+ @background_image = Gosu::Image.new(self, "media/Space.png", true)
94
+
95
+ @player = Player.new(self)
96
+ @player.warp(320, 240)
97
+
98
+ @star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
99
+ @stars = Array.new
100
+
101
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
102
+ end
103
+
104
+ def update
105
+ if button_down? Gosu::Button::KbLeft or button_down? Gosu::Button::GpLeft then
106
+ @player.turn_left
107
+ end
108
+ if button_down? Gosu::Button::KbRight or button_down? Gosu::Button::GpRight then
109
+ @player.turn_right
110
+ end
111
+ if button_down? Gosu::Button::KbUp or button_down? Gosu::Button::GpButton0 then
112
+ @player.accelerate
113
+ end
114
+ @player.move
115
+ @player.collect_stars(@stars)
116
+
117
+ if rand(100) < 4 and @stars.size < 25 then
118
+ @stars.push(Star.new(@star_anim))
119
+ end
120
+ end
121
+
122
+ def draw
123
+ @background_image.draw(0, 0, ZOrder::Background)
124
+ @player.draw
125
+ @stars.each { |star| star.draw }
126
+ @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
127
+ end
128
+
129
+ def button_down(id)
130
+ if id == Gosu::Button::KbEscape then
131
+ close
132
+ end
133
+ end
134
+ end
135
+
136
+ window = GameWindow.new
137
+ window.show