ruby2d 0.8.1 → 0.9.0

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 (192) hide show
  1. checksums.yaml +4 -4
  2. data/assets/README.md +20 -7
  3. data/assets/include/SDL2/SDL.h +135 -0
  4. data/assets/include/SDL2/SDL_assert.h +291 -0
  5. data/assets/include/SDL2/SDL_atomic.h +277 -0
  6. data/assets/include/SDL2/SDL_audio.h +826 -0
  7. data/assets/include/SDL2/SDL_bits.h +112 -0
  8. data/assets/include/SDL2/SDL_blendmode.h +120 -0
  9. data/assets/include/SDL2/SDL_clipboard.h +71 -0
  10. data/assets/include/SDL2/SDL_config.h +421 -0
  11. data/assets/include/SDL2/SDL_cpuinfo.h +188 -0
  12. data/assets/include/SDL2/SDL_egl.h +1673 -0
  13. data/assets/include/SDL2/SDL_endian.h +260 -0
  14. data/assets/include/SDL2/SDL_error.h +76 -0
  15. data/assets/include/SDL2/SDL_events.h +788 -0
  16. data/assets/include/SDL2/SDL_filesystem.h +136 -0
  17. data/assets/include/SDL2/SDL_gamecontroller.h +390 -0
  18. data/assets/include/SDL2/SDL_gesture.h +87 -0
  19. data/assets/include/SDL2/SDL_haptic.h +1238 -0
  20. data/assets/include/SDL2/SDL_hints.h +1135 -0
  21. data/assets/include/SDL2/SDL_image.h +161 -0
  22. data/assets/include/SDL2/SDL_joystick.h +408 -0
  23. data/assets/include/SDL2/SDL_keyboard.h +217 -0
  24. data/assets/include/SDL2/SDL_keycode.h +349 -0
  25. data/assets/include/SDL2/SDL_loadso.h +81 -0
  26. data/assets/include/SDL2/SDL_log.h +211 -0
  27. data/assets/include/SDL2/SDL_main.h +168 -0
  28. data/assets/include/SDL2/SDL_messagebox.h +144 -0
  29. data/assets/include/SDL2/SDL_mixer.h +651 -0
  30. data/assets/include/SDL2/SDL_mouse.h +302 -0
  31. data/assets/include/SDL2/SDL_mutex.h +251 -0
  32. data/assets/include/SDL2/SDL_name.h +33 -0
  33. data/assets/include/SDL2/SDL_opengl.h +2183 -0
  34. data/assets/include/SDL2/SDL_opengl_glext.h +11177 -0
  35. data/assets/include/SDL2/SDL_opengles.h +39 -0
  36. data/assets/include/SDL2/SDL_opengles2.h +52 -0
  37. data/assets/include/SDL2/SDL_opengles2_gl2.h +621 -0
  38. data/assets/include/SDL2/SDL_opengles2_gl2ext.h +2050 -0
  39. data/assets/include/SDL2/SDL_opengles2_gl2platform.h +30 -0
  40. data/assets/include/SDL2/SDL_opengles2_khrplatform.h +282 -0
  41. data/assets/include/SDL2/SDL_pixels.h +470 -0
  42. data/assets/include/SDL2/SDL_platform.h +198 -0
  43. data/assets/include/SDL2/SDL_power.h +75 -0
  44. data/assets/include/SDL2/SDL_quit.h +58 -0
  45. data/assets/include/SDL2/SDL_rect.h +148 -0
  46. data/assets/include/SDL2/SDL_render.h +931 -0
  47. data/assets/include/SDL2/SDL_revision.h +2 -0
  48. data/assets/include/SDL2/SDL_rwops.h +254 -0
  49. data/assets/include/SDL2/SDL_scancode.h +413 -0
  50. data/assets/include/SDL2/SDL_sensor.h +251 -0
  51. data/assets/include/SDL2/SDL_shape.h +144 -0
  52. data/assets/include/SDL2/SDL_stdinc.h +607 -0
  53. data/assets/include/SDL2/SDL_surface.h +553 -0
  54. data/assets/include/SDL2/SDL_system.h +279 -0
  55. data/assets/include/SDL2/SDL_syswm.h +324 -0
  56. data/assets/include/SDL2/SDL_test.h +69 -0
  57. data/assets/include/SDL2/SDL_test_assert.h +105 -0
  58. data/assets/include/SDL2/SDL_test_common.h +188 -0
  59. data/assets/include/SDL2/SDL_test_compare.h +69 -0
  60. data/assets/include/SDL2/SDL_test_crc32.h +124 -0
  61. data/assets/include/SDL2/SDL_test_font.h +81 -0
  62. data/assets/include/SDL2/SDL_test_fuzzer.h +384 -0
  63. data/assets/include/SDL2/SDL_test_harness.h +134 -0
  64. data/assets/include/SDL2/SDL_test_images.h +78 -0
  65. data/assets/include/SDL2/SDL_test_log.h +67 -0
  66. data/assets/include/SDL2/SDL_test_md5.h +129 -0
  67. data/assets/include/SDL2/SDL_test_memory.h +63 -0
  68. data/assets/include/SDL2/SDL_test_random.h +115 -0
  69. data/assets/include/SDL2/SDL_thread.h +343 -0
  70. data/assets/include/SDL2/SDL_timer.h +115 -0
  71. data/assets/include/SDL2/SDL_touch.h +86 -0
  72. data/assets/include/SDL2/SDL_ttf.h +294 -0
  73. data/assets/include/SDL2/SDL_types.h +29 -0
  74. data/assets/include/SDL2/SDL_version.h +162 -0
  75. data/assets/include/SDL2/SDL_video.h +1276 -0
  76. data/assets/include/SDL2/SDL_vulkan.h +278 -0
  77. data/assets/include/SDL2/begin_code.h +167 -0
  78. data/assets/include/SDL2/close_code.h +37 -0
  79. data/assets/include/glew.h +23686 -0
  80. data/assets/include/simple2d.h +735 -0
  81. data/assets/ios/MRuby.framework/Headers/mrbconf.h +3 -5
  82. data/assets/ios/MRuby.framework/Headers/mruby.h +35 -32
  83. data/assets/ios/MRuby.framework/Headers/mruby/array.h +1 -0
  84. data/assets/ios/MRuby.framework/Headers/mruby/boxing_word.h +9 -1
  85. data/assets/ios/MRuby.framework/Headers/mruby/class.h +14 -11
  86. data/assets/ios/MRuby.framework/Headers/mruby/common.h +5 -0
  87. data/assets/ios/MRuby.framework/Headers/mruby/compile.h +4 -3
  88. data/assets/ios/MRuby.framework/Headers/mruby/debug.h +3 -2
  89. data/assets/ios/MRuby.framework/Headers/mruby/dump.h +2 -2
  90. data/assets/ios/MRuby.framework/Headers/mruby/hash.h +38 -5
  91. data/assets/ios/MRuby.framework/Headers/mruby/irep.h +17 -4
  92. data/assets/ios/MRuby.framework/Headers/mruby/object.h +4 -5
  93. data/assets/ios/MRuby.framework/Headers/mruby/opcode.h +47 -139
  94. data/assets/ios/MRuby.framework/Headers/mruby/ops.h +117 -0
  95. data/assets/ios/MRuby.framework/Headers/mruby/proc.h +4 -4
  96. data/assets/ios/MRuby.framework/Headers/mruby/string.h +11 -7
  97. data/assets/ios/MRuby.framework/Headers/mruby/throw.h +4 -4
  98. data/assets/ios/MRuby.framework/Headers/mruby/variable.h +2 -4
  99. data/assets/ios/MRuby.framework/Headers/mruby/version.h +6 -6
  100. data/assets/ios/MRuby.framework/MRuby +0 -0
  101. data/assets/ios/MyApp.xcodeproj/project.pbxproj +24 -18
  102. data/assets/ios/MyApp.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  103. data/assets/ios/main.c +1 -1
  104. data/assets/macos/lib/libFLAC.a +0 -0
  105. data/assets/macos/lib/libSDL2.a +0 -0
  106. data/assets/macos/lib/libSDL2_image.a +0 -0
  107. data/assets/macos/lib/libSDL2_mixer.a +0 -0
  108. data/assets/macos/lib/libSDL2_ttf.a +0 -0
  109. data/assets/macos/lib/libfreetype.a +0 -0
  110. data/assets/macos/lib/libjpeg.a +0 -0
  111. data/assets/macos/lib/libmpg123.a +0 -0
  112. data/assets/macos/lib/libogg.a +0 -0
  113. data/assets/macos/lib/libpng16.a +0 -0
  114. data/assets/macos/lib/libsimple2d.a +0 -0
  115. data/assets/macos/lib/libtiff.a +0 -0
  116. data/assets/macos/lib/libvorbis.a +0 -0
  117. data/assets/macos/lib/libvorbisfile.a +0 -0
  118. data/assets/macos/lib/libwebp.a +0 -0
  119. data/assets/mingw/bin/SDL2.dll +0 -0
  120. data/assets/mingw/bin/SDL2_image.dll +0 -0
  121. data/assets/mingw/bin/SDL2_mixer.dll +0 -0
  122. data/assets/mingw/bin/SDL2_ttf.dll +0 -0
  123. data/assets/mingw/bin/glew32.dll +0 -0
  124. data/assets/mingw/bin/libFLAC-8.dll +0 -0
  125. data/assets/mingw/bin/libfreetype-6.dll +0 -0
  126. data/assets/mingw/bin/libjpeg-9.dll +0 -0
  127. data/assets/mingw/bin/libmodplug-1.dll +0 -0
  128. data/assets/mingw/bin/libmpg123-0.dll +0 -0
  129. data/assets/mingw/bin/libogg-0.dll +0 -0
  130. data/assets/mingw/bin/libopus-0.dll +0 -0
  131. data/assets/mingw/bin/libopusfile-0.dll +0 -0
  132. data/assets/mingw/bin/libpng16-16.dll +0 -0
  133. data/assets/mingw/bin/libtiff-5.dll +0 -0
  134. data/assets/mingw/bin/libvorbis-0.dll +0 -0
  135. data/assets/mingw/bin/libvorbisfile-3.dll +0 -0
  136. data/assets/mingw/bin/libwebp-7.dll +0 -0
  137. data/assets/mingw/bin/zlib1.dll +0 -0
  138. data/assets/mingw/lib/libSDL2.a +0 -0
  139. data/assets/mingw/lib/libSDL2.dll.a +0 -0
  140. data/assets/mingw/lib/libSDL2_image.a +0 -0
  141. data/assets/mingw/lib/libSDL2_image.dll.a +0 -0
  142. data/assets/mingw/lib/libSDL2_mixer.a +0 -0
  143. data/assets/mingw/lib/libSDL2_mixer.dll.a +0 -0
  144. data/assets/mingw/lib/libSDL2_test.a +0 -0
  145. data/assets/mingw/lib/libSDL2_ttf.a +0 -0
  146. data/assets/mingw/lib/libSDL2_ttf.dll.a +0 -0
  147. data/assets/mingw/lib/libSDL2main.a +0 -0
  148. data/assets/mingw/lib/libglew32.a +0 -0
  149. data/assets/mingw/lib/libglew32.dll.a +0 -0
  150. data/assets/mingw/lib/libsimple2d.a +0 -0
  151. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/App Icon - Large.imagestack/Back.imagestacklayer/Content.imageset/Contents.json +4 -0
  152. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/App Icon - Large.imagestack/Front.imagestacklayer/Content.imageset/Contents.json +4 -0
  153. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/App Icon - Large.imagestack/Middle.imagestacklayer/Content.imageset/Contents.json +4 -0
  154. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/App Icon - Small.imagestack/Back.imagestacklayer/Content.imageset/Contents.json +4 -0
  155. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/App Icon - Small.imagestack/Front.imagestacklayer/Content.imageset/Contents.json +4 -0
  156. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/App Icon - Small.imagestack/Middle.imagestacklayer/Content.imageset/Contents.json +4 -0
  157. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/Top Shelf Image Wide.imageset/Contents.json +4 -0
  158. data/assets/tvos/Assets.xcassets/App Icon & Top Shelf Image.brandassets/Top Shelf Image.imageset/Contents.json +4 -0
  159. data/assets/tvos/Assets.xcassets/LaunchImage.launchimage/Contents.json +7 -0
  160. data/assets/tvos/MRuby.framework/Headers/mrbconf.h +3 -5
  161. data/assets/tvos/MRuby.framework/Headers/mruby.h +35 -32
  162. data/assets/tvos/MRuby.framework/Headers/mruby/array.h +1 -0
  163. data/assets/tvos/MRuby.framework/Headers/mruby/boxing_word.h +9 -1
  164. data/assets/tvos/MRuby.framework/Headers/mruby/class.h +14 -11
  165. data/assets/tvos/MRuby.framework/Headers/mruby/common.h +5 -0
  166. data/assets/tvos/MRuby.framework/Headers/mruby/compile.h +4 -3
  167. data/assets/tvos/MRuby.framework/Headers/mruby/debug.h +3 -2
  168. data/assets/tvos/MRuby.framework/Headers/mruby/dump.h +2 -2
  169. data/assets/tvos/MRuby.framework/Headers/mruby/hash.h +38 -5
  170. data/assets/tvos/MRuby.framework/Headers/mruby/irep.h +17 -4
  171. data/assets/tvos/MRuby.framework/Headers/mruby/object.h +4 -5
  172. data/assets/tvos/MRuby.framework/Headers/mruby/opcode.h +47 -139
  173. data/assets/tvos/MRuby.framework/Headers/mruby/ops.h +117 -0
  174. data/assets/tvos/MRuby.framework/Headers/mruby/proc.h +4 -4
  175. data/assets/tvos/MRuby.framework/Headers/mruby/string.h +11 -7
  176. data/assets/tvos/MRuby.framework/Headers/mruby/throw.h +4 -4
  177. data/assets/tvos/MRuby.framework/Headers/mruby/variable.h +2 -4
  178. data/assets/tvos/MRuby.framework/Headers/mruby/version.h +6 -6
  179. data/assets/tvos/MRuby.framework/MRuby +0 -0
  180. data/assets/tvos/MyApp.xcodeproj/project.pbxproj +25 -19
  181. data/assets/tvos/MyApp.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  182. data/assets/tvos/main.c +1 -1
  183. data/ext/ruby2d/extconf.rb +59 -50
  184. data/ext/ruby2d/ruby2d.c +0 -1
  185. data/lib/ruby2d.rb +1 -7
  186. data/lib/ruby2d/cli/launch.rb +1 -1
  187. data/lib/ruby2d/font.rb +32 -7
  188. data/lib/ruby2d/version.rb +1 -1
  189. metadata +132 -6
  190. data/assets/opal.js +0 -21637
  191. data/assets/simple2d.js +0 -1379
  192. data/assets/template.html +0 -18
@@ -1,1379 +0,0 @@
1
- // Simple2D.js — v0.2.0, built 06-04-2017
2
-
3
- // start.js - Open the anonymous function defining the Simple 2D module
4
-
5
- (function(undefined) {
6
-
7
- // Check if Simple 2D is already loaded
8
- if (typeof(this.S2D) !== 'undefined') {
9
- console.warn("Simple 2D already loaded! Loading twice may cause problems.");
10
- return this.S2D;
11
- }
12
-
13
- // Create the Simple 2D module
14
- var S2D = this.S2D = {};
15
-
16
- // ... Simple 2D library starts here ...
17
-
18
-
19
- // simple2d.js
20
-
21
- // Simple 2D OpenGL namespace
22
- S2D.GL = {};
23
-
24
- // Viewport scaling modes
25
- Object.defineProperty(S2D, "FIXED", { value: 1 });
26
- Object.defineProperty(S2D, "SCALE", { value: 2 });
27
- Object.defineProperty(S2D, "STRETCH", { value: 3 });
28
-
29
- // Keyboard events
30
- Object.defineProperty(S2D, "KEY_DOWN", { value: 1 });
31
- Object.defineProperty(S2D, "KEY_HELD", { value: 2 });
32
- Object.defineProperty(S2D, "KEY_UP", { value: 3 });
33
-
34
- // Mouse events
35
- Object.defineProperty(S2D, "MOUSE_DOWN", { value: 1 });
36
- Object.defineProperty(S2D, "MOUSE_UP", { value: 2 });
37
- Object.defineProperty(S2D, "MOUSE_SCROLL", { value: 3 });
38
- Object.defineProperty(S2D, "MOUSE_MOVE", { value: 4 });
39
- Object.defineProperty(S2D, "MOUSE_LEFT", { value: 5 });
40
- Object.defineProperty(S2D, "MOUSE_MIDDLE", { value: 6 });
41
- Object.defineProperty(S2D, "MOUSE_RIGHT", { value: 7 });
42
- Object.defineProperty(S2D, "MOUSE_X1", { value: 8 });
43
- Object.defineProperty(S2D, "MOUSE_X2", { value: 9 });
44
- Object.defineProperty(S2D, "MOUSE_SCROLL_NORMAL", { value: 10 });
45
- Object.defineProperty(S2D, "MOUSE_SCROLL_INVERTED", { value: 11 });
46
-
47
- // Event
48
- S2D.Event = {
49
- which: null,
50
- type: null,
51
- button: null,
52
- key: null,
53
- x: 0,
54
- y: 0,
55
- delta_x: 0,
56
- delta_y: 0,
57
- direction: null
58
- };
59
-
60
- // Color
61
- S2D.Color = {
62
- r: 1.0,
63
- g: 1.0,
64
- b: 1.0,
65
- a: 1.0
66
- };
67
-
68
- // Window
69
- S2D.Window = {
70
- title: null,
71
- width: null,
72
- height: null,
73
- orig_width: null,
74
- orig_height: null,
75
- viewport: {
76
- width: null,
77
- height: null,
78
- mode: null
79
- },
80
- pixel_ratio: null,
81
- update: null,
82
- render: null,
83
- mouse: {
84
- x: 0,
85
- y: 0,
86
- last_x: 0,
87
- last_y: 0
88
- },
89
- on_key: null,
90
- on_mouse: null,
91
- element: null, // The HTML element to append the canvas
92
- canvas: null,
93
- background: null,
94
- frames: 0,
95
- close: false
96
- };
97
-
98
- // Image
99
- S2D.Image = {
100
- texture: null,
101
- data: null,
102
- color: null,
103
- x: 0,
104
- y: 0,
105
- width: null,
106
- height: null,
107
- orig_width: null,
108
- orig_height: null
109
- };
110
-
111
- // Sprite
112
- S2D.Sprite = {
113
- img: null,
114
- color: null,
115
- x: 0,
116
- y: 0,
117
- width: null,
118
- height: null,
119
- clip_width: null,
120
- clip_height: null,
121
- tx1: null,
122
- ty1: null,
123
- tx2: null,
124
- ty2: null,
125
- tx3: null,
126
- ty3: null,
127
- tx4: null,
128
- ty4: null
129
- };
130
-
131
- // Text
132
- S2D.Text = {
133
- texture: null,
134
- data: null,
135
- color: null,
136
- x: 0,
137
- y: 0,
138
- width: null,
139
- height: null,
140
- font: null,
141
- size: null,
142
- msg: null
143
- };
144
-
145
- // Sound
146
- S2D.Sound = {
147
- data: null
148
- };
149
-
150
- // Music
151
- S2D.Music = {
152
- data: null
153
- };
154
-
155
- // Global current music playing
156
- S2D.current_music = null;
157
-
158
- // Collection of keys currently pressed
159
- S2D.keys_down = [];
160
-
161
- // On keyboard starting at top row, left to right
162
- S2D.key_map = {
163
- 27: "Escape",
164
-
165
- 192: "`",
166
- 189: "-",
167
- 187: "=",
168
- 8: "Backspace",
169
-
170
- 9: "Tab",
171
- 219: "[",
172
- 221: "]",
173
- 220: "\\",
174
-
175
- 20: "CapsLock",
176
- 186: ";",
177
- 222: "'",
178
- 13: "Return",
179
-
180
- 16: "Shift",
181
- 188: ",",
182
- 190: ".",
183
- 191: "/",
184
-
185
- 17: "Ctrl",
186
- 18: "Option",
187
- 91: "Left Command",
188
- 32: "Space",
189
- 93: "Right Command",
190
- 37: "Left",
191
- 38: "Up",
192
- 39: "Right",
193
- 40: "Down"
194
- };
195
-
196
- // Web-specific helpers
197
-
198
- // Looks up a key from a given keycode
199
- S2D.GetKey = function(keycode) {
200
- if (typeof(keycode) == "string") {
201
- return keycode;
202
- } else if (S2D.key_map[keycode]) {
203
- return S2D.key_map[keycode];
204
- } else {
205
- return String.fromCharCode(keycode);
206
- }
207
- };
208
-
209
- // Trim transparent pixels from canvas
210
- // Adapted from: https://gist.github.com/remy/784508
211
- S2D.TrimCanvas = function(c) {
212
- var ctx = c.getContext('2d'),
213
- copy = document.createElement('canvas').getContext('2d'),
214
- pixels = ctx.getImageData(0, 0, c.width, c.height),
215
- l = pixels.data.length,
216
- i,
217
- bound = {
218
- top: null,
219
- left: null,
220
- right: null,
221
- bottom: null
222
- },
223
- x, y;
224
-
225
- for (i = 0; i < l; i += 4) {
226
- if (pixels.data[i+3] !== 0) {
227
- x = (i / 4) % c.width;
228
- y = ~~((i / 4) / c.width);
229
-
230
- if (bound.top === null) {
231
- bound.top = y;
232
- }
233
-
234
- if (bound.left === null) {
235
- bound.left = x;
236
- } else if (x < bound.left) {
237
- bound.left = x;
238
- }
239
-
240
- if (bound.right === null) {
241
- bound.right = x;
242
- } else if (bound.right < x) {
243
- bound.right = x;
244
- }
245
-
246
- if (bound.bottom === null) {
247
- bound.bottom = y;
248
- } else if (bound.bottom < y) {
249
- bound.bottom = y;
250
- }
251
- }
252
- }
253
-
254
- var trimHeight = bound.bottom - bound.top,
255
- trimWidth = bound.right - bound.left,
256
- trimmed = ctx.getImageData(bound.left, bound.top, trimWidth, trimHeight);
257
-
258
- copy.canvas.width = trimWidth;
259
- copy.canvas.height = trimHeight;
260
- copy.putImageData(trimmed, 0, 0);
261
-
262
- // open new window with trimmed image:
263
- return copy.canvas;
264
- };
265
-
266
- // Creates a global "addWheelListener" method
267
- // example: addWheelListener(el, function(e) { console.log(e.deltaY); e.preventDefault(); });
268
- // Adapted from: https://developer.mozilla.org/en-US/docs/Web/Events/wheel
269
- (function(window, document) {
270
-
271
- var prefix = "",
272
- _addEventListener, support;
273
-
274
- // detect event model
275
- if (window.addEventListener) {
276
- _addEventListener = "addEventListener";
277
- } else {
278
- _addEventListener = "attachEvent";
279
- prefix = "on";
280
- }
281
-
282
- // detect available wheel event
283
- support = "onwheel" in document.createElement("div") ? "wheel" : // Modern browsers support "wheel"
284
- document.onmousewheel !== undefined ? "mousewheel" : // Webkit and IE support at least "mousewheel"
285
- "DOMMouseScroll"; // let's assume that remaining browsers are older Firefox
286
-
287
- window.addWheelListener = function(elem, callback, useCapture) {
288
- _addWheelListener(elem, support, callback, useCapture);
289
-
290
- // handle MozMousePixelScroll in older Firefox
291
- if (support == "DOMMouseScroll") {
292
- _addWheelListener(elem, "MozMousePixelScroll", callback, useCapture);
293
- }
294
- };
295
-
296
- function _addWheelListener(elem, eventName, callback, useCapture) {
297
- elem[_addEventListener](prefix + eventName, support == "wheel" ? callback : function(originalEvent) {
298
- !originalEvent && (originalEvent = window.event);
299
-
300
- // create a normalized event object
301
- var event = {
302
- // keep a ref to the original event object
303
- originalEvent: originalEvent,
304
- target: originalEvent.target || originalEvent.srcElement,
305
- type: "wheel",
306
- deltaMode: originalEvent.type == "MozMousePixelScroll" ? 0 : 1,
307
- deltaX: 0,
308
- deltaY: 0,
309
- deltaZ: 0,
310
- preventDefault: function() {
311
- originalEvent.preventDefault ?
312
- originalEvent.preventDefault() :
313
- originalEvent.returnValue = false;
314
- }
315
- };
316
-
317
- // calculate deltaY (and deltaX) according to the event
318
- if (support == "mousewheel") {
319
- event.deltaY = -1 / 40 * originalEvent.wheelDelta;
320
- // Webkit also support wheelDeltaX
321
- originalEvent.wheelDeltaX && (event.deltaX = -1 / 40 * originalEvent.wheelDeltaX);
322
- } else {
323
- event.deltaY = originalEvent.detail;
324
- }
325
-
326
- // it's time to fire the callback
327
- return callback(event);
328
-
329
- }, useCapture || false);
330
- }
331
-
332
- })(window, document);
333
-
334
-
335
- // shapes.js
336
-
337
- /*
338
- * Draw a triangle
339
- */
340
- S2D.DrawTriangle = function(x1, y1, r1, g1, b1, a1,
341
- x2, y2, r2, g2, b2, a2,
342
- x3, y3, r3, g3, b3, a3) {
343
-
344
- S2D.GL.DrawTriangle(x1, y1, r1, g1, b1, a1,
345
- x2, y2, r2, g2, b2, a2,
346
- x3, y3, r3, g3, b3, a3);
347
- };
348
-
349
-
350
- /*
351
- * Draw a quad, using two triangles
352
- */
353
- S2D.DrawQuad = function(x1, y1, r1, g1, b1, a1,
354
- x2, y2, r2, g2, b2, a2,
355
- x3, y3, r3, g3, b3, a3,
356
- x4, y4, r4, g4, b4, a4) {
357
-
358
- S2D.GL.DrawTriangle(x1, y1, r1, g1, b1, a1,
359
- x2, y2, r2, g2, b2, a2,
360
- x3, y3, r3, g3, b3, a3);
361
-
362
- S2D.GL.DrawTriangle(x3, y3, r3, g3, b3, a3,
363
- x4, y4, r4, g4, b4, a4,
364
- x1, y1, r1, g1, b1, a1);
365
- };
366
-
367
-
368
- /*
369
- * Draw a line from a quad
370
- */
371
- S2D.DrawLine = function(x1, y1, x2, y2,
372
- width,
373
- r1, g1, b1, a1,
374
- r2, g2, b2, a2,
375
- r3, g3, b3, a3,
376
- r4, g4, b4, a4) {
377
-
378
- var length = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
379
- var x = ((x2 - x1) / length) * width / 2;
380
- var y = ((y2 - y1) / length) * width / 2;
381
-
382
- S2D.DrawQuad(
383
- x1 - y, y1 + x, r1, g1, b1, a1,
384
- x1 + y, y1 - x, r2, g2, b2, a2,
385
- x2 + y, y2 - x, r3, g3, b3, a3,
386
- x2 - y, y2 + x, r4, g4, b4, a4
387
- );
388
- };
389
-
390
-
391
- // image.js
392
-
393
- /*
394
- * Create an image
395
- * Params: path = image file path
396
- */
397
- S2D.CreateImage = function(path, loadedCallback) {
398
-
399
- // TODO: Check if image file exists
400
-
401
- // Create image object
402
- var img = Object.create(S2D.Image);
403
- img.data = new Image();
404
- img.color = Object.create(S2D.Color);
405
-
406
- img.data.onload = function() {
407
- img.texture = S2D.GL.CreateTexture(this);
408
- if (!img.width ) img.width = this.width;
409
- if (!img.height) img.height = this.height;
410
- if (loadedCallback) loadedCallback();
411
- };
412
-
413
- // Causes image to be loaded
414
- img.data.src = path;
415
-
416
- return img;
417
- };
418
-
419
-
420
- /*
421
- * Draw an image
422
- */
423
- S2D.DrawImage = function(img) {
424
- if (!img) return;
425
- S2D.GL.DrawImage(img);
426
- };
427
-
428
-
429
- // sprite.js
430
-
431
- /*
432
- * Create a sprite, given an image file path
433
- */
434
- S2D.CreateSprite = function(path) {
435
-
436
- // TODO: Check if sprite image file exists
437
-
438
- var spr = Object.create(S2D.Sprite);
439
- spr.color = Object.create(S2D.Color);
440
- spr.img = S2D.CreateImage(path, function() {
441
- if (!spr.width ) spr.width = spr.img.width;
442
- if (!spr.height) spr.height = spr.img.height;
443
- spr.clip_width = spr.img.width;
444
- spr.clip_height = spr.img.height;
445
- });
446
-
447
- spr.tx1 = 0.0;
448
- spr.ty1 = 0.0;
449
- spr.tx2 = 1.0;
450
- spr.ty2 = 0.0;
451
- spr.tx3 = 1.0;
452
- spr.ty3 = 1.0;
453
- spr.tx4 = 0.0;
454
- spr.ty4 = 1.0;
455
-
456
- return spr;
457
- };
458
-
459
-
460
- /*
461
- * Clip a sprite
462
- */
463
- S2D.ClipSprite = function(spr, x, y, w, h) {
464
- if (!spr) return;
465
-
466
- // Calculate ratios
467
- // rw = ratio width; rh = ratio height
468
- var rw = w / spr.img.width;
469
- var rh = h / spr.img.height;
470
-
471
- // Apply ratios to x, y coordinates
472
- // cx = crop x coord; cy = crop y coord
473
- var cx = x * rw;
474
- var cy = y * rh;
475
-
476
- // Convert given width, height to doubles
477
- // cw = crop width; ch = crop height
478
- var cw = w;
479
- var ch = h;
480
-
481
- // Apply ratio to texture width and height
482
- // tw = texture width; th = texture height
483
- var tw = rw * w;
484
- var th = rh * h;
485
-
486
- // Calculate and store sprite texture values
487
-
488
- spr.tx1 = cx / cw;
489
- spr.ty1 = cy / ch;
490
-
491
- spr.tx2 = (cx + tw) / cw;
492
- spr.ty2 = cy / ch;
493
-
494
- spr.tx3 = (cx + tw) / cw;
495
- spr.ty3 = (cy + th) / ch;
496
-
497
- spr.tx4 = cx / cw;
498
- spr.ty4 = (cy + th) / ch;
499
-
500
- // Store the sprite dimensions
501
- spr.width = (spr.width / spr.clip_width ) * w;
502
- spr.height = (spr.height / spr.clip_height) * h;
503
- spr.clip_width = w;
504
- spr.clip_height = h;
505
- };
506
-
507
-
508
- /*
509
- * Draw a sprite
510
- */
511
- S2D.DrawSprite = function(spr) {
512
- if (!spr) return;
513
- S2D.GL.DrawSprite(spr);
514
- };
515
-
516
-
517
- // text.js
518
-
519
- /*
520
- * Create text, given a font file path, the message, and size
521
- */
522
- S2D.CreateText = function(font, msg, size) {
523
-
524
- // Create image object
525
- var txt = Object.create(S2D.Text);
526
- txt.color = Object.create(S2D.Color);
527
- txt.font = font ? font : null;
528
- txt.msg = msg;
529
- txt.size = size;
530
-
531
- S2D.SetText(txt, txt.msg);
532
-
533
- return txt;
534
- };
535
-
536
-
537
- /*
538
- * Sets the text message
539
- */
540
- S2D.SetText = function(txt, msg) {
541
- if (msg == "") return; // no need to create a texture
542
-
543
- if (txt.texture) S2D.GL.FreeTexture(txt.texture);
544
-
545
- // Create a canvas element to make a texture
546
- var ctx = document.createElement("canvas").getContext("2d");
547
-
548
- // TODO: Width and height should probably be variable, based on
549
- // `ctx.measureText(msg).width` or something.
550
- var w = 1000;
551
- var h = 1000;
552
-
553
- // Double size of font for high DPI
554
- var size = txt.size * 2;
555
-
556
- // Set context attributes and draw text
557
- ctx.canvas.width = w;
558
- ctx.canvas.height = h;
559
- ctx.font = `${size}px ${txt.font}`;
560
- ctx.textAlign = "right";
561
- ctx.textBaseline = "bottom";
562
- ctx.fillStyle = "white";
563
- ctx.fillText(msg, w, h);
564
-
565
- txt.data = S2D.TrimCanvas(ctx.canvas); // trim the transparent pixels
566
- txt.texture = S2D.GL.CreateTexture(txt.data);
567
- txt.width = txt.data.width / 2; // half size of texture for high DPI
568
- txt.height = txt.data.height / 2;
569
- };
570
-
571
-
572
- /*
573
- * Draw text
574
- */
575
- S2D.DrawText = function(txt) {
576
- if (!txt) return;
577
- if (!txt.texture) S2D.SetText(txt, txt.msg);
578
- S2D.GL.DrawText(txt);
579
- };
580
-
581
-
582
- // sound.js
583
-
584
- /*
585
- * Create a sound, given an audio file path
586
- */
587
- S2D.CreateSound = function(path) {
588
-
589
- // TODO: Check if audio file exists
590
-
591
- var sound = Object.create(S2D.Sound);
592
- sound.data = new Audio(path);
593
-
594
- return sound;
595
- };
596
-
597
-
598
- /*
599
- * Play the sound
600
- */
601
- S2D.PlaySound = function(sound) {
602
- // Clone sound and play so audio can overlap
603
- sound.data.cloneNode(true).play();
604
- };
605
-
606
-
607
- // music.js
608
-
609
- /*
610
- * Create the music, given an audio file path
611
- */
612
- S2D.CreateMusic = function(path) {
613
-
614
- // TODO: Check if audio file exists
615
-
616
- var music = Object.create(S2D.Music);
617
- music.data = new Audio(path);
618
-
619
- return music;
620
- };
621
-
622
-
623
- /*
624
- * Play the music
625
- */
626
- S2D.PlayMusic = function(music, loop) {
627
- S2D.StopMusic();
628
- music.data.loop = loop;
629
- S2D.current_music = music.data;
630
- S2D.current_music.play();
631
- };
632
-
633
-
634
- /*
635
- * Pause the playing music
636
- */
637
- S2D.PauseMusic = function() {
638
- if (!S2D.current_music) return;
639
- S2D.current_music.pause();
640
- };
641
-
642
-
643
- /*
644
- * Resume the current music
645
- */
646
- S2D.ResumeMusic = function() {
647
- if (!S2D.current_music) return;
648
- S2D.current_music.play();
649
- };
650
-
651
-
652
- /*
653
- * Stops the playing music; interrupts fader effects
654
- */
655
- S2D.StopMusic = function() {
656
- if (!S2D.current_music) return;
657
- S2D.current_music.pause();
658
- S2D.current_music.currentTime = 0;
659
- };
660
-
661
-
662
- /*
663
- * Fade out the playing music
664
- */
665
- S2D.FadeOutMusic = function(ms) {
666
- if (!S2D.current_music) return;
667
-
668
- if (S2D.current_music.paused) {
669
- S2D.StopMusic();
670
- return;
671
- }
672
-
673
- var fadeAudio = setInterval(function () {
674
- if (S2D.current_music.volume >= 0.05) {
675
- S2D.current_music.volume -= 0.05;
676
- } else {
677
- S2D.StopMusic();
678
- S2D.current_music.volume = 1.0;
679
- clearInterval(fadeAudio);
680
- }
681
-
682
- }, ms / 20);
683
- };
684
-
685
-
686
- // input.js
687
-
688
- /*
689
- * Get the mouse coordinates relative to the viewport
690
- */
691
- S2D.GetMouseOnViewport = function(win, wx, wy) {
692
-
693
- var scale; // viewport scale factor
694
- var w, h; // width and height of scaled viewport
695
- var x, y; // mouse positions to be returned
696
-
697
- switch (win.viewport.mode) {
698
-
699
- case S2D.FIXED:
700
- x = wx / (win.orig_width / win.viewport.width);
701
- y = wy / (win.orig_height / win.viewport.height);
702
- break;
703
-
704
- case S2D.SCALE:
705
- var o = S2D.GL.GetViewportScale(win);
706
- x = wx * 1 / o.scale - (win.width - o.w) / (2.0 * o.scale);
707
- y = wy * 1 / o.scale - (win.height - o.h) / (2.0 * o.scale);
708
- break;
709
-
710
- case S2D.STRETCH:
711
- x = wx * win.viewport.width / win.width;
712
- y = wy * win.viewport.height / win.height;
713
- break;
714
- }
715
-
716
- return {
717
- x: x,
718
- y: y
719
- };
720
- };
721
-
722
-
723
- /*
724
- * Get the mouse button name from its code
725
- */
726
- S2D.GetMouseButtonName = function(code) {
727
- switch (code) {
728
- case 0:
729
- return S2D.MOUSE_LEFT;
730
- case 1:
731
- return S2D.MOUSE_MIDDLE;
732
- case 2:
733
- return S2D.MOUSE_RIGHT;
734
- case 3:
735
- return S2D.MOUSE_X1;
736
- case 4:
737
- return S2D.MOUSE_X2;
738
- }
739
- };
740
-
741
-
742
- // window.js
743
-
744
- /*
745
- * Create a window
746
- */
747
- S2D.CreateWindow = function(title, width, height, update, render, element, opts) {
748
-
749
- var win = Object.create(S2D.Window);
750
-
751
- win.title = title;
752
- win.width = width;
753
- win.height = height;
754
- win.orig_width = width;
755
- win.orig_height = height;
756
- win.viewport.width = width;
757
- win.viewport.height = height;
758
- win.viewport.mode = S2D.SCALE;
759
- win.update = update;
760
- win.render = render;
761
- win.background = Object.create(S2D.Color);
762
- win.background.r = 0;
763
- win.background.g = 0;
764
- win.background.b = 0;
765
- win.background.a = 1;
766
-
767
- // `element` can be an ID string (e.g. "#game") or an actual DOM element
768
- if (typeof(element) == 'string') {
769
- win.element = document.getElementById(element);
770
- } else {
771
- win.element = element;
772
- }
773
-
774
- return win;
775
- };
776
-
777
-
778
- /*
779
- * Show the window
780
- */
781
- S2D.Show = function(win) {
782
-
783
- // Create the canvas element
784
-
785
- var el = document.createElement('canvas');
786
- win.element.appendChild(el);
787
-
788
- el.setAttribute('width', win.width);
789
- el.setAttribute('height', win.height);
790
- el.innerHTML = "Your browser doesn't appear to support" +
791
- "the <code>&lt;canvas&gt;</code> element.";
792
-
793
- win.canvas = el;
794
-
795
- // Prevent right clicking in canvas
796
- win.canvas.addEventListener("contextmenu", function(e) { e.preventDefault(); });
797
-
798
- // Detect and set up canvas for high DPI
799
-
800
- win.canvas.style.width = win.width + "px";
801
- win.canvas.style.height = win.height + "px";
802
-
803
- var ratio = window.devicePixelRatio ||
804
- window.webkitDevicePixelRatio ||
805
- window.mozDevicePixelRatio ||
806
- window.opDevicePixelRatio || 1;
807
-
808
- win.canvas.width = win.width * devicePixelRatio;
809
- win.canvas.height = win.height * devicePixelRatio;
810
- win.pixel_ratio = ratio;
811
-
812
- // Initialize WebGL
813
- S2D.GL.Init(win);
814
-
815
- S2D.onkeydown = function(e) {
816
- if (win.on_key) {
817
- var key = S2D.GetKey(e.keyCode);
818
- if (!S2D.keys_down.includes(key)) {
819
- S2D.keys_down.push(key);
820
- var event = Object.create(S2D.Event);
821
- event.type = S2D.KEY_DOWN; event.key = key;
822
- win.on_key(event);
823
- }
824
- }
825
- };
826
- document.addEventListener("keydown", S2D.onkeydown);
827
-
828
- S2D.onkeyup = function(e) {
829
- if (win.on_key) {
830
- var key = S2D.GetKey(e.keyCode);
831
- var i = S2D.keys_down.indexOf(key);
832
- if (i > -1) S2D.keys_down.splice(i, 1);
833
- var event = Object.create(S2D.Event);
834
- event.type = S2D.KEY_UP; event.key = key;
835
- win.on_key(event);
836
- }
837
- };
838
- document.addEventListener("keyup", S2D.onkeyup);
839
-
840
- // Clear keys down list when focus is lost
841
- window.addEventListener("blur", function functionName() {
842
- var e = {};
843
- S2D.keys_down.slice().forEach(function(key) {
844
- e.keyCode = key;
845
- S2D.onkeyup(e);
846
- });
847
- });
848
-
849
- S2D.onmousedown = function(e) {
850
- if (win.on_mouse) {
851
- var o = S2D.GetMouseOnViewport(win,
852
- e.pageX - win.canvas.offsetLeft, e.pageY - win.canvas.offsetTop
853
- );
854
- var event = Object.create(S2D.Event);
855
- event.type = S2D.MOUSE_DOWN;
856
- event.button = S2D.GetMouseButtonName(e.button);
857
- event.x = o.x; event.y = o.y;
858
- win.on_mouse(event);
859
- }
860
- };
861
- document.addEventListener("mousedown", S2D.onmousedown);
862
-
863
- S2D.onmouseup = function(e) {
864
- if (win.on_mouse) {
865
- var o = S2D.GetMouseOnViewport(win,
866
- e.pageX - win.canvas.offsetLeft, e.pageY - win.canvas.offsetTop
867
- );
868
- var event = Object.create(S2D.Event);
869
- event.type = S2D.MOUSE_UP;
870
- event.button = S2D.GetMouseButtonName(e.button);
871
- event.x = o.x; event.y = o.y;
872
- win.on_mouse(event);
873
- }
874
- };
875
- document.addEventListener("mouseup", S2D.onmouseup);
876
-
877
- // Get and store mouse position, call mouse move
878
- S2D.onmousemove = function(e) {
879
- var o = S2D.GetMouseOnViewport(win,
880
- e.pageX - win.canvas.offsetLeft, e.pageY - win.canvas.offsetTop
881
- );
882
- win.mouse.x = o.x;
883
- win.mouse.y = o.y;
884
- if (win.on_mouse) {
885
- var event = Object.create(S2D.Event);
886
- event.type = S2D.MOUSE_MOVE;
887
- event.x = o.x; event.y = o.y;
888
- event.delta_x = o.x - win.mouse.last_x; event.delta_y = o.y - win.mouse.last_y;
889
- win.on_mouse(event);
890
- win.mouse.last_x = o.x; win.mouse.last_y = o.y;
891
- }
892
- };
893
- document.addEventListener("mousemove", S2D.onmousemove);
894
-
895
- // Get and store mouse wheel scrolling
896
- S2D.onmousewheel = function(e) {
897
- if (win.on_mouse) {
898
- var event = Object.create(S2D.Event);
899
- event.type = S2D.MOUSE_SCROLL;
900
- event.direction = e.webkitDirectionInvertedFromDevice ?
901
- S2D.MOUSE_SCROLL_INVERTED : S2D.MOUSE_SCROLL_NORMAL;
902
- event.delta_x = e.deltaX;
903
- event.delta_y = e.deltaY;
904
- win.on_mouse(event);
905
- }
906
- e.preventDefault();
907
- };
908
- window.addWheelListener(document, S2D.onmousewheel);
909
-
910
- // Main loop
911
-
912
- var req; // the animation frame request
913
- var start_ms = new Date();
914
- var end_ms = new Date();
915
- var elapsed_ms;
916
-
917
- function mainLoop(win) {
918
-
919
- if (win.close) {
920
- cancelAnimationFrame(req);
921
- return;
922
- }
923
-
924
- S2D.GL.Clear(win.background);
925
-
926
- // Update frame counter
927
- win.frames++;
928
-
929
- // Calculate and store FPS
930
- end_ms = new Date();
931
- elapsed_ms = end_ms.getTime() - start_ms.getTime();
932
- win.fps = win.frames / (elapsed_ms / 1000.0);
933
-
934
- // Detect keys held down
935
- S2D.keys_down.forEach(function(key) {
936
- if (win.on_key) {
937
- var event = Object.create(S2D.Event);
938
- event.type = S2D.KEY_HELD; event.key = key;
939
- win.on_key(event);
940
- }
941
- });
942
-
943
- if (win.update) win.update();
944
- if (win.render) win.render();
945
-
946
- requestAnimationFrame(function() { mainLoop(win); });
947
- }
948
-
949
- req = requestAnimationFrame(function() { mainLoop(win); });
950
- };
951
-
952
-
953
- /*
954
- * Close the window
955
- */
956
- S2D.Close = function(win) {
957
- win.close = true;
958
- // win.canvas.remove();
959
- };
960
-
961
-
962
- // gl.js
963
-
964
- var gl = null, // The WebGL context
965
- canvas, // The HTML canvas element
966
- indices = [0, 1, 2, 2, 3, 0],
967
- // Triangle shader
968
- shaderProgram,
969
- positionLocation,
970
- colorLocation,
971
- // Texture shader
972
- texShaderProgram,
973
- texPositionLocation,
974
- texColorLocation,
975
- texCoordLocation,
976
- samplerLocation;
977
-
978
- var orthoMatrix = [
979
- 0, 0, 0, 0,
980
- 0, 0, 0, 0,
981
- 0, 0, 0, 0,
982
- -1.0, 1.0, -1.0, 1.0
983
- ];
984
-
985
-
986
- /*
987
- * Initialize WebGL
988
- */
989
- S2D.GL.Init = function(win) {
990
-
991
- // Initialize the GL context
992
- try {
993
- // Try to grab the standard context. If it fails, fallback to experimental.
994
- gl = win.canvas.getContext("webgl") || win.canvas.getContext("experimental-webgl");
995
- } catch(e) {
996
- console.log("GL error caught");
997
- }
998
-
999
- // If we don't have a GL context, give up now
1000
- if (!gl) {
1001
- console.error("Unable to initialize WebGL. Your browser may not support it.");
1002
- return null;
1003
- }
1004
-
1005
- S2D.GL.WebGLInit();
1006
- S2D.GL.SetViewport(win);
1007
- };
1008
-
1009
-
1010
- /*
1011
- * Initialize WebGL
1012
- */
1013
- S2D.GL.WebGLInit = function() {
1014
-
1015
- // Enable transparency
1016
- gl.enable(gl.BLEND);
1017
- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
1018
-
1019
- // Vertex shader source string
1020
- var vertexSource = `
1021
- uniform mat4 u_matrix;
1022
- attribute vec4 a_position;
1023
- attribute vec4 a_color;
1024
- attribute vec2 a_texcoord;
1025
- varying vec4 v_color;
1026
- varying vec2 v_texcoord;
1027
- void main(void) {
1028
- v_color = a_color;
1029
- v_texcoord = a_texcoord;
1030
- gl_Position = u_matrix * a_position;
1031
- }`;
1032
-
1033
- // Fragment shader source string
1034
- var fragmentSource = `
1035
- precision mediump float;
1036
- varying vec4 v_color;
1037
- void main(void) {
1038
- gl_FragColor = v_color;
1039
- }`;
1040
-
1041
- // Fragment shader source string for textures
1042
- var texFragmentSource = `
1043
- precision mediump float;
1044
- varying vec4 v_color;
1045
- varying vec2 v_texcoord;
1046
- uniform sampler2D s_texture;
1047
- void main(void) {
1048
- gl_FragColor = texture2D(s_texture, v_texcoord) * v_color;
1049
- }`;
1050
-
1051
- // Load the vertex and fragment shaders
1052
- var vertexShader = S2D.GL.LoadShader( gl.VERTEX_SHADER, vertexSource, "Vertex");
1053
- var fragmentShader = S2D.GL.LoadShader(gl.FRAGMENT_SHADER, fragmentSource, "Fragment");
1054
- var texFragmentShader = S2D.GL.LoadShader(gl.FRAGMENT_SHADER, texFragmentSource, "Texture Fragment");
1055
-
1056
- // Triangle Shader //
1057
-
1058
- // Create the texture shader program object
1059
- shaderProgram = gl.createProgram();
1060
-
1061
- // Attach the shader objects to the program object
1062
- gl.attachShader(shaderProgram, vertexShader);
1063
- gl.attachShader(shaderProgram, fragmentShader);
1064
-
1065
- // Link the shader program
1066
- gl.linkProgram(shaderProgram);
1067
-
1068
- // Check if linked
1069
- if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
1070
- console.error("Unable to initialize the shader program.");
1071
- }
1072
-
1073
- // Get the attribute locations
1074
- positionLocation = gl.getAttribLocation(shaderProgram, "a_position");
1075
- colorLocation = gl.getAttribLocation(shaderProgram, "a_color");
1076
-
1077
- // Texture Shader //
1078
-
1079
- // Create the texture shader program object
1080
- texShaderProgram = gl.createProgram();
1081
-
1082
- // Attach the shader objects to the program object
1083
- gl.attachShader(texShaderProgram, vertexShader);
1084
- gl.attachShader(texShaderProgram, texFragmentShader);
1085
-
1086
- // Link the shader program
1087
- gl.linkProgram(texShaderProgram);
1088
-
1089
- // Check if linked
1090
- if (!gl.getProgramParameter(texShaderProgram, gl.LINK_STATUS)) {
1091
- console.error("Unable to initialize the texture shader program.");
1092
- }
1093
-
1094
- // Get the attribute locations
1095
- texPositionLocation = gl.getAttribLocation(texShaderProgram, "a_position");
1096
- texColorLocation = gl.getAttribLocation(texShaderProgram, "a_color");
1097
- texCoordLocation = gl.getAttribLocation(texShaderProgram, "a_texcoord");
1098
-
1099
- // Get the sampler location
1100
- samplerLocation = gl.getUniformLocation(texShaderProgram, "s_texture");
1101
- };
1102
-
1103
-
1104
- /*
1105
- * Creates a shader object, loads shader string, and compiles.
1106
- */
1107
- S2D.GL.LoadShader = function(type, shaderSrc, shaderName) {
1108
-
1109
- var shader = gl.createShader(type);
1110
-
1111
- gl.shaderSource(shader, shaderSrc);
1112
- gl.compileShader(shader);
1113
-
1114
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
1115
- console.error("Error compiling shader \"" + shaderName + "\":\n" + gl.getShaderInfoLog(shader));
1116
- return null;
1117
- }
1118
-
1119
- return shader;
1120
- };
1121
-
1122
-
1123
- /*
1124
- * Calculate the viewport's scaled width and height
1125
- */
1126
- S2D.GL.GetViewportScale = function(win) {
1127
-
1128
- var s = Math.min(
1129
- win.width / win.viewport.width,
1130
- win.height / win.viewport.height
1131
- );
1132
-
1133
- var w = win.viewport.width * s;
1134
- var h = win.viewport.height * s;
1135
-
1136
- return {
1137
- w: w,
1138
- h: h,
1139
- scale: s
1140
- };
1141
- };
1142
-
1143
-
1144
- /*
1145
- * Sets the viewport and matrix projection
1146
- */
1147
- S2D.GL.SetViewport = function(win) {
1148
-
1149
- var ortho_w = win.viewport.width;
1150
- var ortho_h = win.viewport.height;
1151
- var x, y, w, h; // calculated GL viewport values
1152
-
1153
- x = 0; y = 0; w = win.width; h = win.height;
1154
-
1155
- switch (win.viewport.mode) {
1156
-
1157
- case S2D.FIXED:
1158
- w = win.orig_width;
1159
- h = win.orig_height;
1160
- y = win.height - h;
1161
- break;
1162
-
1163
- case S2D.SCALE:
1164
- var o = S2D.GL.GetViewportScale(win);
1165
- // Center the viewport
1166
- x = win.width / 2.0 - o.w/2.0;
1167
- y = win.height / 2.0 - o.h/2.0;
1168
- break;
1169
-
1170
- case S2D.STRETCH:
1171
- break;
1172
- }
1173
-
1174
- gl.viewport(
1175
- x * win.pixel_ratio,
1176
- y * win.pixel_ratio,
1177
- w * win.pixel_ratio,
1178
- h * win.pixel_ratio
1179
- );
1180
-
1181
- orthoMatrix[0] = 2.0 / ortho_w;
1182
- orthoMatrix[5] = -2.0 / ortho_h;
1183
-
1184
- gl.useProgram(shaderProgram);
1185
-
1186
- gl.uniformMatrix4fv(
1187
- gl.getUniformLocation(shaderProgram, "u_matrix"),
1188
- false, new Float32Array(orthoMatrix)
1189
- );
1190
-
1191
- gl.useProgram(texShaderProgram);
1192
-
1193
- gl.uniformMatrix4fv(
1194
- gl.getUniformLocation(texShaderProgram, "u_matrix"),
1195
- false, new Float32Array(orthoMatrix)
1196
- );
1197
- };
1198
-
1199
-
1200
- /*
1201
- * Clear buffers to given color values
1202
- */
1203
- S2D.GL.Clear = function(clr) {
1204
- gl.clearColor(clr.r, clr.g, clr.b, clr.a);
1205
- gl.clear(gl.COLOR_BUFFER_BIT);
1206
- };
1207
-
1208
-
1209
- /*
1210
- * Creates a texture for rendering
1211
- */
1212
- S2D.GL.CreateTexture = function(data) {
1213
- if (!gl) return;
1214
-
1215
- var texture = gl.createTexture();
1216
-
1217
- // Bind the named texture to a texturing target
1218
- gl.bindTexture(gl.TEXTURE_2D, texture);
1219
-
1220
- // Specifies the 2D texture image
1221
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, data);
1222
-
1223
- // Set the filtering mode
1224
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
1225
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
1226
-
1227
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
1228
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
1229
-
1230
- gl.bindTexture(gl.TEXTURE_2D, null);
1231
-
1232
- return texture;
1233
- };
1234
-
1235
-
1236
- /*
1237
- * Free a texture
1238
- */
1239
- S2D.GL.FreeTexture = function(texture) {
1240
- gl.deleteTexture(texture);
1241
- };
1242
-
1243
-
1244
- /*
1245
- * Draw triangle
1246
- */
1247
- S2D.GL.DrawTriangle = function(x1, y1, r1, g1, b1, a1,
1248
- x2, y2, r2, g2, b2, a2,
1249
- x3, y3, r3, g3, b3, a3) {
1250
-
1251
- var vertices = [
1252
- x1, y1, 0.0,
1253
- x2, y2, 0.0,
1254
- x3, y3, 0.0
1255
- ];
1256
-
1257
- var colors = [
1258
- r1, g1, b1, a1,
1259
- r2, g2, b2, a2,
1260
- r3, g3, b3, a3
1261
- ];
1262
-
1263
- gl.useProgram(shaderProgram);
1264
-
1265
- // Vertex
1266
- gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
1267
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
1268
-
1269
- gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);
1270
- gl.enableVertexAttribArray(positionLocation);
1271
-
1272
- // Colors
1273
- gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
1274
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
1275
-
1276
- gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0);
1277
- gl.enableVertexAttribArray(colorLocation);
1278
-
1279
- // Draw
1280
- gl.drawArrays(gl.TRIANGLES, 0, 3);
1281
- };
1282
-
1283
-
1284
- /*
1285
- * Draw a texture
1286
- */
1287
- S2D.GL.DrawTexture = function(x, y, w, h,
1288
- r, g, b, a,
1289
- tx1, ty1, tx2, ty2, tx3, ty3, tx4, ty4,
1290
- texture) {
1291
-
1292
- var vertices =
1293
- // x, y coords | x, y texture coords
1294
- [ x, y, 0.0, tx1, ty1,
1295
- x + w, y, 0.0, tx2, ty2,
1296
- x + w, y + h, 0.0, tx3, ty3,
1297
- x, y + h, 0.0, tx4, ty4 ];
1298
-
1299
- var colors = [
1300
- r, g, b, a,
1301
- r, g, b, a,
1302
- r, g, b, a,
1303
- r, g, b, a
1304
- ];
1305
-
1306
- gl.useProgram(texShaderProgram);
1307
-
1308
- // Vertex
1309
- gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
1310
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
1311
-
1312
- gl.vertexAttribPointer(texPositionLocation, 3, gl.FLOAT, false, 5*4, 0);
1313
- gl.enableVertexAttribArray(texPositionLocation);
1314
-
1315
- gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 5*4, 3*4);
1316
- gl.enableVertexAttribArray(texCoordLocation);
1317
-
1318
- // Colors
1319
- gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
1320
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
1321
-
1322
- gl.vertexAttribPointer(texColorLocation, 4, gl.FLOAT, false, 0, 0);
1323
- gl.enableVertexAttribArray(texColorLocation);
1324
-
1325
- gl.activeTexture(gl.TEXTURE0);
1326
- gl.bindTexture(gl.TEXTURE_2D, texture);
1327
-
1328
- gl.uniform1i(samplerLocation, 0);
1329
-
1330
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.createBuffer());
1331
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
1332
-
1333
- gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
1334
- };
1335
-
1336
-
1337
- /*
1338
- * Draw image
1339
- */
1340
- S2D.GL.DrawImage = function(img) {
1341
- S2D.GL.DrawTexture(
1342
- img.x, img.y, img.width, img.height,
1343
- img.color.r, img.color.g, img.color.b, img.color.a,
1344
- 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
1345
- img.texture
1346
- );
1347
- };
1348
-
1349
-
1350
- /*
1351
- * Draw sprite
1352
- */
1353
- S2D.GL.DrawSprite = function(spr) {
1354
- S2D.GL.DrawTexture(
1355
- spr.x, spr.y, spr.width, spr.height,
1356
- spr.color.r, spr.color.g, spr.color.b, spr.color.a,
1357
- spr.tx1, spr.ty1, spr.tx2, spr.ty2, spr.tx3, spr.ty3, spr.tx4, spr.ty4,
1358
- spr.img.texture
1359
- );
1360
- };
1361
-
1362
-
1363
- /*
1364
- * Draw text
1365
- */
1366
- S2D.GL.DrawText = function(txt) {
1367
- S2D.GL.DrawTexture(
1368
- txt.x, txt.y, txt.width, txt.height,
1369
- txt.color.r, txt.color.g, txt.color.b, txt.color.a,
1370
- 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
1371
- txt.texture
1372
- );
1373
- };
1374
-
1375
-
1376
- // end.js - Close the anonymous function defining the Simple 2D module
1377
-
1378
- // Call anonymous and the Simple 2D module to the global scope
1379
- }).call(this);