ruby2d 0.9.4 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/assets/include/SDL2/SDL.h +4 -1
  3. data/assets/include/SDL2/SDL_assert.h +3 -1
  4. data/assets/include/SDL2/SDL_atomic.h +1 -1
  5. data/assets/include/SDL2/SDL_audio.h +1 -1
  6. data/assets/include/SDL2/SDL_bits.h +1 -1
  7. data/assets/include/SDL2/SDL_blendmode.h +4 -1
  8. data/assets/include/SDL2/SDL_clipboard.h +1 -1
  9. data/assets/include/SDL2/SDL_config.h +1 -1
  10. data/assets/include/SDL2/SDL_config_android.h +7 -3
  11. data/assets/include/SDL2/SDL_config_iphoneos.h +16 -10
  12. data/assets/include/SDL2/SDL_config_macosx.h +40 -14
  13. data/assets/include/SDL2/SDL_config_minimal.h +1 -1
  14. data/assets/include/SDL2/SDL_config_os2.h +26 -8
  15. data/assets/include/SDL2/SDL_config_pandora.h +4 -2
  16. data/assets/include/SDL2/SDL_config_psp.h +2 -1
  17. data/assets/include/SDL2/SDL_config_windows.h +40 -9
  18. data/assets/include/SDL2/SDL_config_winrt.h +5 -2
  19. data/assets/include/SDL2/SDL_config_wiz.h +5 -2
  20. data/assets/include/SDL2/SDL_copying.h +1 -1
  21. data/assets/include/SDL2/SDL_cpuinfo.h +32 -3
  22. data/assets/include/SDL2/SDL_egl.h +4 -1
  23. data/assets/include/SDL2/SDL_endian.h +6 -3
  24. data/assets/include/SDL2/SDL_error.h +38 -2
  25. data/assets/include/SDL2/SDL_events.h +64 -28
  26. data/assets/include/SDL2/SDL_filesystem.h +1 -1
  27. data/assets/include/SDL2/SDL_gamecontroller.h +160 -9
  28. data/assets/include/SDL2/SDL_gesture.h +1 -1
  29. data/assets/include/SDL2/SDL_haptic.h +10 -1
  30. data/assets/include/SDL2/SDL_hints.h +316 -4
  31. data/assets/include/SDL2/SDL_joystick.h +115 -24
  32. data/assets/include/SDL2/SDL_keyboard.h +1 -1
  33. data/assets/include/SDL2/SDL_keycode.h +11 -9
  34. data/assets/include/SDL2/SDL_loadso.h +1 -1
  35. data/assets/include/SDL2/SDL_locale.h +101 -0
  36. data/assets/include/SDL2/SDL_log.h +3 -3
  37. data/assets/include/SDL2/SDL_main.h +1 -1
  38. data/assets/include/SDL2/SDL_messagebox.h +6 -4
  39. data/assets/include/SDL2/SDL_metal.h +117 -0
  40. data/assets/include/SDL2/SDL_misc.h +75 -0
  41. data/assets/include/SDL2/SDL_mouse.h +1 -1
  42. data/assets/include/SDL2/SDL_mutex.h +1 -1
  43. data/assets/include/SDL2/SDL_name.h +1 -1
  44. data/assets/include/SDL2/SDL_opengl.h +1 -1
  45. data/assets/include/SDL2/SDL_opengl_glext.h +3 -0
  46. data/assets/include/SDL2/SDL_opengles.h +1 -1
  47. data/assets/include/SDL2/SDL_opengles2.h +1 -1
  48. data/assets/include/SDL2/SDL_pixels.h +25 -16
  49. data/assets/include/SDL2/SDL_platform.h +1 -1
  50. data/assets/include/SDL2/SDL_power.h +1 -1
  51. data/assets/include/SDL2/SDL_quit.h +1 -1
  52. data/assets/include/SDL2/SDL_rect.h +1 -1
  53. data/assets/include/SDL2/SDL_render.h +63 -3
  54. data/assets/include/SDL2/SDL_revision.h +2 -2
  55. data/assets/include/SDL2/SDL_rwops.h +2 -10
  56. data/assets/include/SDL2/SDL_scancode.h +1 -1
  57. data/assets/include/SDL2/SDL_sensor.h +25 -9
  58. data/assets/include/SDL2/SDL_shape.h +1 -1
  59. data/assets/include/SDL2/SDL_stdinc.h +44 -4
  60. data/assets/include/SDL2/SDL_surface.h +11 -2
  61. data/assets/include/SDL2/SDL_system.h +50 -4
  62. data/assets/include/SDL2/SDL_syswm.h +29 -2
  63. data/assets/include/SDL2/SDL_test.h +1 -1
  64. data/assets/include/SDL2/SDL_test_assert.h +1 -1
  65. data/assets/include/SDL2/SDL_test_common.h +14 -1
  66. data/assets/include/SDL2/SDL_test_compare.h +1 -1
  67. data/assets/include/SDL2/SDL_test_crc32.h +1 -1
  68. data/assets/include/SDL2/SDL_test_font.h +1 -1
  69. data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
  70. data/assets/include/SDL2/SDL_test_harness.h +1 -1
  71. data/assets/include/SDL2/SDL_test_images.h +1 -1
  72. data/assets/include/SDL2/SDL_test_log.h +1 -1
  73. data/assets/include/SDL2/SDL_test_md5.h +1 -1
  74. data/assets/include/SDL2/SDL_test_memory.h +1 -1
  75. data/assets/include/SDL2/SDL_test_random.h +1 -1
  76. data/assets/include/SDL2/SDL_thread.h +34 -11
  77. data/assets/include/SDL2/SDL_timer.h +1 -1
  78. data/assets/include/SDL2/SDL_touch.h +1 -1
  79. data/assets/include/SDL2/SDL_types.h +1 -1
  80. data/assets/include/SDL2/SDL_version.h +2 -2
  81. data/assets/include/SDL2/SDL_video.h +11 -5
  82. data/assets/include/SDL2/SDL_vulkan.h +6 -8
  83. data/assets/include/SDL2/begin_code.h +5 -9
  84. data/assets/include/SDL2/close_code.h +1 -1
  85. data/assets/macos/lib/libFLAC.a +0 -0
  86. data/assets/macos/lib/libSDL2.a +0 -0
  87. data/assets/macos/lib/libSDL2_image.a +0 -0
  88. data/assets/macos/lib/libSDL2_mixer.a +0 -0
  89. data/assets/macos/lib/libSDL2_ttf.a +0 -0
  90. data/assets/macos/lib/libfreetype.a +0 -0
  91. data/assets/macos/lib/libjpeg.a +0 -0
  92. data/assets/macos/lib/libmodplug.a +0 -0
  93. data/assets/macos/lib/libmpg123.a +0 -0
  94. data/assets/macos/lib/libogg.a +0 -0
  95. data/assets/macos/lib/libpng16.a +0 -0
  96. data/assets/macos/lib/libtiff.a +0 -0
  97. data/assets/macos/lib/libvorbis.a +0 -0
  98. data/assets/macos/lib/libvorbisfile.a +0 -0
  99. data/assets/macos/lib/libwebp.a +0 -0
  100. data/assets/mingw/bin/SDL2.dll +0 -0
  101. data/assets/mingw/lib/libSDL2.a +0 -0
  102. data/assets/mingw/lib/libSDL2.dll.a +0 -0
  103. data/assets/mingw/lib/libSDL2_test.a +0 -0
  104. data/assets/mingw/lib/libSDL2main.a +0 -0
  105. data/bin/ruby2d +1 -0
  106. data/{assets/linux/simple2d/src/simple2d.c → ext/ruby2d/common.c} +32 -32
  107. data/{assets/linux/simple2d/src → ext/ruby2d}/controllers.c +17 -17
  108. data/ext/ruby2d/extconf.rb +37 -59
  109. data/ext/ruby2d/font.c +35 -0
  110. data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +66 -98
  111. data/ext/ruby2d/gl2.c +86 -0
  112. data/{assets/linux/simple2d/src → ext/ruby2d}/gl3.c +69 -112
  113. data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +22 -81
  114. data/ext/ruby2d/image.c +58 -0
  115. data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
  116. data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +30 -17
  117. data/ext/ruby2d/ruby2d.c +449 -439
  118. data/ext/ruby2d/ruby2d.h +652 -0
  119. data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
  120. data/{assets/linux/simple2d/src → ext/ruby2d}/sound.c +39 -14
  121. data/ext/ruby2d/text.c +22 -0
  122. data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +63 -63
  123. data/lib/ruby2d/circle.rb +21 -3
  124. data/lib/ruby2d/cli/build.rb +4 -7
  125. data/lib/ruby2d/dsl.rb +16 -9
  126. data/lib/ruby2d/entity.rb +17 -0
  127. data/lib/ruby2d/font.rb +23 -3
  128. data/lib/ruby2d/image.rb +35 -7
  129. data/lib/ruby2d/line.rb +23 -1
  130. data/lib/ruby2d/music.rb +5 -0
  131. data/lib/ruby2d/pixel.rb +17 -0
  132. data/lib/ruby2d/quad.rb +21 -1
  133. data/lib/ruby2d/rectangle.rb +12 -3
  134. data/lib/ruby2d/renderable.rb +4 -20
  135. data/lib/ruby2d/sound.rb +30 -0
  136. data/lib/ruby2d/sprite.rb +57 -75
  137. data/lib/ruby2d/square.rb +10 -1
  138. data/lib/ruby2d/text.rb +55 -12
  139. data/lib/ruby2d/texture.rb +28 -0
  140. data/lib/ruby2d/tileset.rb +87 -0
  141. data/lib/ruby2d/triangle.rb +19 -1
  142. data/lib/ruby2d/version.rb +1 -1
  143. data/lib/ruby2d/vertices.rb +84 -0
  144. data/lib/ruby2d/window.rb +259 -22
  145. data/lib/ruby2d.rb +6 -1
  146. metadata +30 -26
  147. data/assets/include/simple2d.h +0 -757
  148. data/assets/linux/simple2d/Makefile +0 -260
  149. data/assets/linux/simple2d/bin/simple2d.sh +0 -1318
  150. data/assets/linux/simple2d/include/simple2d.h +0 -757
  151. data/assets/linux/simple2d/src/gl2.c +0 -146
  152. data/assets/linux/simple2d/src/image.c +0 -138
  153. data/assets/linux/simple2d/src/sprite.c +0 -147
  154. data/assets/linux/simple2d/src/text.c +0 -129
  155. data/assets/macos/lib/libsimple2d.a +0 -0
  156. data/assets/mingw/lib/libsimple2d.a +0 -0
@@ -1,6 +1,6 @@
1
1
  // shapes.c
2
2
 
3
- #include "../include/simple2d.h"
3
+ #include "ruby2d.h"
4
4
 
5
5
 
6
6
  /*
@@ -11,7 +11,7 @@
11
11
  * rx The x coordinate to rotate around
12
12
  * ry The y coordinate to rotate around
13
13
  */
14
- S2D_GL_Point S2D_RotatePoint(S2D_GL_Point p, GLfloat angle, GLfloat rx, GLfloat ry) {
14
+ R2D_GL_Point R2D_RotatePoint(R2D_GL_Point p, GLfloat angle, GLfloat rx, GLfloat ry) {
15
15
 
16
16
  // Convert from degrees to radians
17
17
  angle = angle * M_PI / 180.0;
@@ -39,28 +39,28 @@ S2D_GL_Point S2D_RotatePoint(S2D_GL_Point p, GLfloat angle, GLfloat rx, GLfloat
39
39
  /*
40
40
  * Get the point to be rotated around given a position in a rectangle
41
41
  */
42
- S2D_GL_Point S2D_GetRectRotationPoint(int x, int y, int w, int h, int position) {
42
+ R2D_GL_Point R2D_GetRectRotationPoint(int x, int y, int w, int h, int position) {
43
43
 
44
- S2D_GL_Point p;
44
+ R2D_GL_Point p;
45
45
 
46
46
  switch (position) {
47
- case S2D_CENTER:
47
+ case R2D_CENTER:
48
48
  p.x = x + (w / 2.0);
49
49
  p.y = y + (h / 2.0);
50
50
  break;
51
- case S2D_TOP_LEFT:
51
+ case R2D_TOP_LEFT:
52
52
  p.x = x;
53
53
  p.y = y;
54
54
  break;
55
- case S2D_TOP_RIGHT:
55
+ case R2D_TOP_RIGHT:
56
56
  p.x = x + w;
57
57
  p.y = y;
58
58
  break;
59
- case S2D_BOTTOM_LEFT:
59
+ case R2D_BOTTOM_LEFT:
60
60
  p.x = x;
61
61
  p.y = y + h;
62
62
  break;
63
- case S2D_BOTTOM_RIGHT:
63
+ case R2D_BOTTOM_RIGHT:
64
64
  p.x = x + w;
65
65
  p.y = y + h;
66
66
  break;
@@ -73,14 +73,14 @@ S2D_GL_Point S2D_GetRectRotationPoint(int x, int y, int w, int h, int position)
73
73
  /*
74
74
  * Draw a triangle
75
75
  */
76
- void S2D_DrawTriangle(GLfloat x1, GLfloat y1,
76
+ void R2D_DrawTriangle(GLfloat x1, GLfloat y1,
77
77
  GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
78
78
  GLfloat x2, GLfloat y2,
79
79
  GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
80
80
  GLfloat x3, GLfloat y3,
81
81
  GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
82
82
 
83
- S2D_GL_DrawTriangle(x1, y1, r1, g1, b1, a1,
83
+ R2D_GL_DrawTriangle(x1, y1, r1, g1, b1, a1,
84
84
  x2, y2, r2, g2, b2, a2,
85
85
  x3, y3, r3, g3, b3, a3);
86
86
  }
@@ -89,7 +89,7 @@ void S2D_DrawTriangle(GLfloat x1, GLfloat y1,
89
89
  /*
90
90
  * Draw a quad, using two triangles
91
91
  */
92
- void S2D_DrawQuad(GLfloat x1, GLfloat y1,
92
+ void R2D_DrawQuad(GLfloat x1, GLfloat y1,
93
93
  GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
94
94
  GLfloat x2, GLfloat y2,
95
95
  GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
@@ -98,11 +98,11 @@ void S2D_DrawQuad(GLfloat x1, GLfloat y1,
98
98
  GLfloat x4, GLfloat y4,
99
99
  GLfloat r4, GLfloat g4, GLfloat b4, GLfloat a4) {
100
100
 
101
- S2D_GL_DrawTriangle(x1, y1, r1, g1, b1, a1,
101
+ R2D_GL_DrawTriangle(x1, y1, r1, g1, b1, a1,
102
102
  x2, y2, r2, g2, b2, a2,
103
103
  x3, y3, r3, g3, b3, a3);
104
104
 
105
- S2D_GL_DrawTriangle(x3, y3, r3, g3, b3, a3,
105
+ R2D_GL_DrawTriangle(x3, y3, r3, g3, b3, a3,
106
106
  x4, y4, r4, g4, b4, a4,
107
107
  x1, y1, r1, g1, b1, a1);
108
108
  };
@@ -111,7 +111,7 @@ void S2D_DrawQuad(GLfloat x1, GLfloat y1,
111
111
  /*
112
112
  * Draw a line from a quad
113
113
  */
114
- void S2D_DrawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
114
+ void R2D_DrawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
115
115
  GLfloat width,
116
116
  GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
117
117
  GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
@@ -122,7 +122,7 @@ void S2D_DrawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
122
122
  double x = ((x2 - x1) / length) * width / 2;
123
123
  double y = ((y2 - y1) / length) * width / 2;
124
124
 
125
- S2D_DrawQuad(
125
+ R2D_DrawQuad(
126
126
  x1 - y, y1 + x, r1, g1, b1, a1,
127
127
  x1 + y, y1 - x, r2, g2, b2, a2,
128
128
  x2 + y, y2 - x, r3, g3, b3, a3,
@@ -134,7 +134,7 @@ void S2D_DrawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
134
134
  /*
135
135
  * Draw a circle from triangles
136
136
  */
137
- void S2D_DrawCircle(GLfloat x, GLfloat y, GLfloat radius, int sectors,
137
+ void R2D_DrawCircle(GLfloat x, GLfloat y, GLfloat radius, int sectors,
138
138
  GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
139
139
 
140
140
  double angle = 2 * M_PI / sectors;
@@ -147,7 +147,7 @@ void S2D_DrawCircle(GLfloat x, GLfloat y, GLfloat radius, int sectors,
147
147
  GLfloat x2 = x + radius * cos((i - 1) * angle);
148
148
  GLfloat y2 = y + radius * sin((i - 1) * angle);
149
149
 
150
- S2D_GL_DrawTriangle( x, y, r, g, b, a,
150
+ R2D_GL_DrawTriangle( x, y, r, g, b, a,
151
151
  x1, y1, r, g, b, a,
152
152
  x2, y2, r, g, b, a);
153
153
  }
@@ -1,31 +1,31 @@
1
1
  // sound.c
2
2
 
3
- #include "../include/simple2d.h"
3
+ #include "ruby2d.h"
4
4
 
5
5
 
6
6
  /*
7
7
  * Create a sound, given an audio file path
8
8
  */
9
- S2D_Sound *S2D_CreateSound(const char *path) {
10
- S2D_Init();
9
+ R2D_Sound *R2D_CreateSound(const char *path) {
10
+ R2D_Init();
11
11
 
12
12
  // Check if sound file exists
13
- if (!S2D_FileExists(path)) {
14
- S2D_Error("S2D_CreateSound", "Sound file `%s` not found", path);
13
+ if (!R2D_FileExists(path)) {
14
+ R2D_Error("R2D_CreateSound", "Sound file `%s` not found", path);
15
15
  return NULL;
16
16
  }
17
17
 
18
18
  // Allocate the sound structure
19
- S2D_Sound *snd = (S2D_Sound *) malloc(sizeof(S2D_Sound));
19
+ R2D_Sound *snd = (R2D_Sound *) malloc(sizeof(R2D_Sound));
20
20
  if (!snd) {
21
- S2D_Error("S2D_CreateSound", "Out of memory!");
21
+ R2D_Error("R2D_CreateSound", "Out of memory!");
22
22
  return NULL;
23
23
  }
24
24
 
25
25
  // Load the sound data from file
26
26
  snd->data = Mix_LoadWAV(path);
27
27
  if (!snd->data) {
28
- S2D_Error("Mix_LoadWAV", Mix_GetError());
28
+ R2D_Error("Mix_LoadWAV", Mix_GetError());
29
29
  free(snd);
30
30
  return NULL;
31
31
  }
@@ -40,16 +40,41 @@ S2D_Sound *S2D_CreateSound(const char *path) {
40
40
  /*
41
41
  * Play the sound
42
42
  */
43
- void S2D_PlaySound(S2D_Sound *snd) {
43
+ void R2D_PlaySound(R2D_Sound *snd) {
44
44
  if (!snd) return;
45
45
  Mix_PlayChannel(-1, snd->data, 0);
46
46
  }
47
47
 
48
48
 
49
+ /*
50
+ * Get the sound's length in seconds
51
+ */
52
+ int R2D_GetSoundLength(R2D_Sound *snd) {
53
+ float points = 0;
54
+ float frames = 0;
55
+ int frequency = 0;
56
+ Uint16 format = 0;
57
+ int channels = 0;
58
+
59
+ // Populate the frequency, format and channel variables
60
+ if (!Mix_QuerySpec(&frequency, &format, &channels)) return -1; // Querying audio details failed
61
+ if (!snd) return -1;
62
+
63
+ // points = bytes / samplesize
64
+ points = (snd->data->alen / ((format & 0xFF) / 8));
65
+
66
+ // frames = sample points / channels
67
+ frames = (points / channels);
68
+
69
+ // frames / frequency is seconds of audio
70
+ return ceil(frames / frequency);
71
+ }
72
+
73
+
49
74
  /*
50
75
  * Get the sound's volume
51
76
  */
52
- int S2D_GetSoundVolume(S2D_Sound *snd) {
77
+ int R2D_GetSoundVolume(R2D_Sound *snd) {
53
78
  if (!snd) return -1;
54
79
  return ceil(Mix_VolumeChunk(snd->data, -1) * (100.0 / MIX_MAX_VOLUME));
55
80
  }
@@ -58,7 +83,7 @@ int S2D_GetSoundVolume(S2D_Sound *snd) {
58
83
  /*
59
84
  * Set the sound's volume a given percentage
60
85
  */
61
- void S2D_SetSoundVolume(S2D_Sound *snd, int volume) {
86
+ void R2D_SetSoundVolume(R2D_Sound *snd, int volume) {
62
87
  if (!snd) return;
63
88
  // Set volume to be a percentage of the maximum mix volume
64
89
  Mix_VolumeChunk(snd->data, (volume / 100.0) * MIX_MAX_VOLUME);
@@ -68,7 +93,7 @@ void S2D_SetSoundVolume(S2D_Sound *snd, int volume) {
68
93
  /*
69
94
  * Get the sound mixer volume
70
95
  */
71
- int S2D_GetSoundMixVolume() {
96
+ int R2D_GetSoundMixVolume() {
72
97
  return ceil(Mix_Volume(-1, -1) * (100.0 / MIX_MAX_VOLUME));
73
98
  }
74
99
 
@@ -76,7 +101,7 @@ int S2D_GetSoundMixVolume() {
76
101
  /*
77
102
  * Set the sound mixer volume a given percentage
78
103
  */
79
- void S2D_SetSoundMixVolume(int volume) {
104
+ void R2D_SetSoundMixVolume(int volume) {
80
105
  // This sets the volume value across all channels
81
106
  // Set volume to be a percentage of the maximum mix volume
82
107
  Mix_Volume(-1, (volume / 100.0) * MIX_MAX_VOLUME);
@@ -86,7 +111,7 @@ void S2D_SetSoundMixVolume(int volume) {
86
111
  /*
87
112
  * Free the sound
88
113
  */
89
- void S2D_FreeSound(S2D_Sound *snd) {
114
+ void R2D_FreeSound(R2D_Sound *snd) {
90
115
  if (!snd) return;
91
116
  Mix_FreeChunk(snd->data);
92
117
  free(snd);
data/ext/ruby2d/text.c ADDED
@@ -0,0 +1,22 @@
1
+ // text.c
2
+
3
+ #include "ruby2d.h"
4
+
5
+
6
+ /*
7
+ * Create a SDL_Surface that contains the pixel data to render text, given a font and message
8
+ */
9
+ SDL_Surface *R2D_TextCreateSurface(TTF_Font *font, const char *message) {
10
+ // `msg` cannot be an empty string or NULL for TTF_SizeText
11
+ if (message == NULL || strlen(message) == 0) message = " ";
12
+
13
+ SDL_Color color = {255, 255, 255};
14
+ SDL_Surface *surface = TTF_RenderUTF8_Blended(font, message, color);
15
+ if (!surface)
16
+ {
17
+ R2D_Error("TTF_RenderUTF8_Blended", TTF_GetError());
18
+ return NULL;
19
+ }
20
+
21
+ return surface;
22
+ }
@@ -1,25 +1,25 @@
1
1
  // window.c
2
2
 
3
- #include "../include/simple2d.h"
3
+ #include "ruby2d.h"
4
4
 
5
5
 
6
6
  /*
7
7
  * Create a window
8
8
  */
9
- S2D_Window *S2D_CreateWindow(const char *title, int width, int height,
10
- S2D_Update update, S2D_Render render, int flags) {
9
+ R2D_Window *R2D_CreateWindow(const char *title, int width, int height,
10
+ R2D_Update update, R2D_Render render, int flags) {
11
11
 
12
- S2D_Init();
12
+ R2D_Init();
13
13
 
14
14
  SDL_DisplayMode dm;
15
15
  SDL_GetCurrentDisplayMode(0, &dm);
16
- S2D_Log(S2D_INFO, "Current display mode is %dx%dpx @ %dhz", dm.w, dm.h, dm.refresh_rate);
16
+ R2D_Log(R2D_INFO, "Current display mode is %dx%dpx @ %dhz", dm.w, dm.h, dm.refresh_rate);
17
17
 
18
- width = width == S2D_DISPLAY_WIDTH ? dm.w : width;
19
- height = height == S2D_DISPLAY_HEIGHT ? dm.h : height;
18
+ width = width == R2D_DISPLAY_WIDTH ? dm.w : width;
19
+ height = height == R2D_DISPLAY_HEIGHT ? dm.h : height;
20
20
 
21
21
  // Allocate window and set default values
22
- S2D_Window *window = (S2D_Window *) malloc(sizeof(S2D_Window));
22
+ R2D_Window *window = (R2D_Window *) malloc(sizeof(R2D_Window));
23
23
  window->sdl = NULL;
24
24
  window->glcontext = NULL;
25
25
  window->title = title;
@@ -29,7 +29,7 @@ S2D_Window *S2D_CreateWindow(const char *title, int width, int height,
29
29
  window->orig_height = height;
30
30
  window->viewport.width = width;
31
31
  window->viewport.height = height;
32
- window->viewport.mode = S2D_SCALE;
32
+ window->viewport.mode = R2D_SCALE;
33
33
  window->update = update;
34
34
  window->render = render;
35
35
  window->flags = flags;
@@ -53,10 +53,10 @@ S2D_Window *S2D_CreateWindow(const char *title, int width, int height,
53
53
  /*
54
54
  * Show the window
55
55
  */
56
- int S2D_Show(S2D_Window *window) {
56
+ int R2D_Show(R2D_Window *window) {
57
57
 
58
58
  if (!window) {
59
- S2D_Error("S2D_Show", "Window cannot be shown (because it's NULL)");
59
+ R2D_Error("R2D_Show", "Window cannot be shown (because it's NULL)");
60
60
  return 1;
61
61
  }
62
62
 
@@ -68,8 +68,8 @@ int S2D_Show(S2D_Window *window) {
68
68
  SDL_WINDOW_OPENGL | window->flags // flags
69
69
  );
70
70
 
71
- if (!window->sdl) S2D_Error("SDL_CreateWindow", SDL_GetError());
72
- if (window->icon) S2D_SetIcon(window, window->icon);
71
+ if (!window->sdl) R2D_Error("SDL_CreateWindow", SDL_GetError());
72
+ if (window->icon) R2D_SetIcon(window, window->icon);
73
73
 
74
74
  // The window created by SDL might not actually be the requested size.
75
75
  // If it's not the same, retrieve and store the actual window size.
@@ -77,7 +77,7 @@ int S2D_Show(S2D_Window *window) {
77
77
  SDL_GetWindowSize(window->sdl, &actual_width, &actual_height);
78
78
 
79
79
  if ((window->width != actual_width) || (window->height != actual_height)) {
80
- S2D_Log(S2D_INFO,
80
+ R2D_Log(R2D_INFO,
81
81
  "Scaling window to %ix%i (requested size was %ix%i)",
82
82
  actual_width, actual_height, window->width, window->height
83
83
  );
@@ -89,7 +89,7 @@ int S2D_Show(S2D_Window *window) {
89
89
 
90
90
  // Set Up OpenGL /////////////////////////////////////////////////////////////
91
91
 
92
- S2D_GL_Init(window);
92
+ R2D_GL_Init(window);
93
93
 
94
94
  // SDL 2.0.10 and macOS 10.15 fix ////////////////////////////////////////////
95
95
 
@@ -116,7 +116,7 @@ int S2D_Show(S2D_Window *window) {
116
116
  // Enable VSync
117
117
  if (window->vsync) {
118
118
  if (!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1")) {
119
- S2D_Log(S2D_WARN, "VSync cannot be enabled");
119
+ R2D_Log(R2D_WARN, "VSync cannot be enabled");
120
120
  }
121
121
  }
122
122
 
@@ -128,7 +128,7 @@ int S2D_Show(S2D_Window *window) {
128
128
 
129
129
  // Clear Frame /////////////////////////////////////////////////////////////
130
130
 
131
- S2D_GL_Clear(window->background);
131
+ R2D_GL_Clear(window->background);
132
132
 
133
133
  // Set FPS /////////////////////////////////////////////////////////////////
134
134
 
@@ -164,8 +164,8 @@ int S2D_Show(S2D_Window *window) {
164
164
 
165
165
  case SDL_KEYDOWN:
166
166
  if (window->on_key && e.key.repeat == 0) {
167
- S2D_Event event = {
168
- .type = S2D_KEY_DOWN, .key = SDL_GetScancodeName(e.key.keysym.scancode)
167
+ R2D_Event event = {
168
+ .type = R2D_KEY_DOWN, .key = SDL_GetScancodeName(e.key.keysym.scancode)
169
169
  };
170
170
  window->on_key(event);
171
171
  }
@@ -173,8 +173,8 @@ int S2D_Show(S2D_Window *window) {
173
173
 
174
174
  case SDL_KEYUP:
175
175
  if (window->on_key) {
176
- S2D_Event event = {
177
- .type = S2D_KEY_UP, .key = SDL_GetScancodeName(e.key.keysym.scancode)
176
+ R2D_Event event = {
177
+ .type = R2D_KEY_UP, .key = SDL_GetScancodeName(e.key.keysym.scancode)
178
178
  };
179
179
  window->on_key(event);
180
180
  }
@@ -182,11 +182,11 @@ int S2D_Show(S2D_Window *window) {
182
182
 
183
183
  case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
184
184
  if (window->on_mouse) {
185
- S2D_GetMouseOnViewport(window, e.button.x, e.button.y, &mx, &my);
186
- S2D_Event event = {
185
+ R2D_GetMouseOnViewport(window, e.button.x, e.button.y, &mx, &my);
186
+ R2D_Event event = {
187
187
  .button = e.button.button, .x = mx, .y = my
188
188
  };
189
- event.type = e.type == SDL_MOUSEBUTTONDOWN ? S2D_MOUSE_DOWN : S2D_MOUSE_UP;
189
+ event.type = e.type == SDL_MOUSEBUTTONDOWN ? R2D_MOUSE_DOWN : R2D_MOUSE_UP;
190
190
  event.dblclick = e.button.clicks == 2 ? true : false;
191
191
  window->on_mouse(event);
192
192
  }
@@ -194,8 +194,8 @@ int S2D_Show(S2D_Window *window) {
194
194
 
195
195
  case SDL_MOUSEWHEEL:
196
196
  if (window->on_mouse) {
197
- S2D_Event event = {
198
- .type = S2D_MOUSE_SCROLL, .direction = e.wheel.direction,
197
+ R2D_Event event = {
198
+ .type = R2D_MOUSE_SCROLL, .direction = e.wheel.direction,
199
199
  .delta_x = e.wheel.x, .delta_y = -e.wheel.y
200
200
  };
201
201
  window->on_mouse(event);
@@ -204,9 +204,9 @@ int S2D_Show(S2D_Window *window) {
204
204
 
205
205
  case SDL_MOUSEMOTION:
206
206
  if (window->on_mouse) {
207
- S2D_GetMouseOnViewport(window, e.motion.x, e.motion.y, &mx, &my);
208
- S2D_Event event = {
209
- .type = S2D_MOUSE_MOVE,
207
+ R2D_GetMouseOnViewport(window, e.motion.x, e.motion.y, &mx, &my);
208
+ R2D_Event event = {
209
+ .type = R2D_MOUSE_MOVE,
210
210
  .x = mx, .y = my, .delta_x = e.motion.xrel, .delta_y = e.motion.yrel
211
211
  };
212
212
  window->on_mouse(event);
@@ -215,8 +215,8 @@ int S2D_Show(S2D_Window *window) {
215
215
 
216
216
  case SDL_CONTROLLERAXISMOTION:
217
217
  if (window->on_controller) {
218
- S2D_Event event = {
219
- .which = e.caxis.which, .type = S2D_AXIS,
218
+ R2D_Event event = {
219
+ .which = e.caxis.which, .type = R2D_AXIS,
220
220
  .axis = e.caxis.axis, .value = e.caxis.value
221
221
  };
222
222
  window->on_controller(event);
@@ -224,9 +224,9 @@ int S2D_Show(S2D_Window *window) {
224
224
  break;
225
225
 
226
226
  case SDL_JOYAXISMOTION:
227
- if (window->on_controller && !S2D_IsController(e.jbutton.which)) {
228
- S2D_Event event = {
229
- .which = e.jaxis.which, .type = S2D_AXIS,
227
+ if (window->on_controller && !R2D_IsController(e.jbutton.which)) {
228
+ R2D_Event event = {
229
+ .which = e.jaxis.which, .type = R2D_AXIS,
230
230
  .axis = e.jaxis.axis, .value = e.jaxis.value
231
231
  };
232
232
  window->on_controller(event);
@@ -235,35 +235,35 @@ int S2D_Show(S2D_Window *window) {
235
235
 
236
236
  case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONUP:
237
237
  if (window->on_controller) {
238
- S2D_Event event = {
238
+ R2D_Event event = {
239
239
  .which = e.cbutton.which, .button = e.cbutton.button
240
240
  };
241
- event.type = e.type == SDL_CONTROLLERBUTTONDOWN ? S2D_BUTTON_DOWN : S2D_BUTTON_UP;
241
+ event.type = e.type == SDL_CONTROLLERBUTTONDOWN ? R2D_BUTTON_DOWN : R2D_BUTTON_UP;
242
242
  window->on_controller(event);
243
243
  }
244
244
  break;
245
245
 
246
246
  case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP:
247
- if (window->on_controller && !S2D_IsController(e.jbutton.which)) {
248
- S2D_Event event = {
247
+ if (window->on_controller && !R2D_IsController(e.jbutton.which)) {
248
+ R2D_Event event = {
249
249
  .which = e.jbutton.which, .button = e.jbutton.button
250
250
  };
251
- event.type = e.type == SDL_JOYBUTTONDOWN ? S2D_BUTTON_DOWN : S2D_BUTTON_UP;
251
+ event.type = e.type == SDL_JOYBUTTONDOWN ? R2D_BUTTON_DOWN : R2D_BUTTON_UP;
252
252
  window->on_controller(event);
253
253
  }
254
254
  break;
255
255
 
256
256
  case SDL_JOYDEVICEADDED:
257
- S2D_Log(S2D_INFO, "Controller connected (%i total)", SDL_NumJoysticks());
258
- S2D_OpenControllers();
257
+ R2D_Log(R2D_INFO, "Controller connected (%i total)", SDL_NumJoysticks());
258
+ R2D_OpenControllers();
259
259
  break;
260
260
 
261
261
  case SDL_JOYDEVICEREMOVED:
262
- if (S2D_IsController(e.jdevice.which)) {
263
- S2D_Log(S2D_INFO, "Controller #%i: %s removed (%i remaining)", e.jdevice.which, SDL_GameControllerName(SDL_GameControllerFromInstanceID(e.jdevice.which)), SDL_NumJoysticks());
262
+ if (R2D_IsController(e.jdevice.which)) {
263
+ R2D_Log(R2D_INFO, "Controller #%i: %s removed (%i remaining)", e.jdevice.which, SDL_GameControllerName(SDL_GameControllerFromInstanceID(e.jdevice.which)), SDL_NumJoysticks());
264
264
  SDL_GameControllerClose(SDL_GameControllerFromInstanceID(e.jdevice.which));
265
265
  } else {
266
- S2D_Log(S2D_INFO, "Controller #%i: %s removed (%i remaining)", e.jdevice.which, SDL_JoystickName(SDL_JoystickFromInstanceID(e.jdevice.which)), SDL_NumJoysticks());
266
+ R2D_Log(R2D_INFO, "Controller #%i: %s removed (%i remaining)", e.jdevice.which, SDL_JoystickName(SDL_JoystickFromInstanceID(e.jdevice.which)), SDL_NumJoysticks());
267
267
  SDL_JoystickClose(SDL_JoystickFromInstanceID(e.jdevice.which));
268
268
  }
269
269
  break;
@@ -274,13 +274,13 @@ int S2D_Show(S2D_Window *window) {
274
274
  // Store new window size, set viewport
275
275
  window->width = e.window.data1;
276
276
  window->height = e.window.data2;
277
- S2D_GL_SetViewport(window);
277
+ R2D_GL_SetViewport(window);
278
278
  break;
279
279
  }
280
280
  break;
281
281
 
282
282
  case SDL_QUIT:
283
- S2D_Close(window);
283
+ R2D_Close(window);
284
284
  break;
285
285
  }
286
286
  }
@@ -292,8 +292,8 @@ int S2D_Show(S2D_Window *window) {
292
292
  for (int i = 0; i < num_keys; i++) {
293
293
  if (window->on_key) {
294
294
  if (key_state[i] == 1) {
295
- S2D_Event event = {
296
- .type = S2D_KEY_HELD, .key = SDL_GetScancodeName(i)
295
+ R2D_Event event = {
296
+ .type = R2D_KEY_HELD, .key = SDL_GetScancodeName(i)
297
297
  };
298
298
  window->on_key(event);
299
299
  }
@@ -303,7 +303,7 @@ int S2D_Show(S2D_Window *window) {
303
303
  // Get and store mouse position relative to the viewport
304
304
  int wx, wy; // mouse x, y coordinates relative to the window
305
305
  SDL_GetMouseState(&wx, &wy);
306
- S2D_GetMouseOnViewport(window, wx, wy, &window->mouse.x, &window->mouse.y);
306
+ R2D_GetMouseOnViewport(window, wx, wy, &window->mouse.x, &window->mouse.y);
307
307
 
308
308
  // Update Window State /////////////////////////////////////////////////////
309
309
 
@@ -321,7 +321,7 @@ int S2D_Show(S2D_Window *window) {
321
321
  // Draw Frame //////////////////////////////////////////////////////////////
322
322
 
323
323
  // Render and flush all OpenGL buffers
324
- S2D_GL_FlushBuffers();
324
+ R2D_GL_FlushBuffers();
325
325
 
326
326
  // Swap buffers to display drawn contents in the window
327
327
  SDL_GL_SwapWindow(window->sdl);
@@ -334,14 +334,14 @@ int S2D_Show(S2D_Window *window) {
334
334
  /*
335
335
  * Set the icon for the window
336
336
  */
337
- void S2D_SetIcon(S2D_Window *window, const char *icon) {
338
- S2D_Image *img = S2D_CreateImage(icon);
339
- if (img) {
337
+ void R2D_SetIcon(R2D_Window *window, const char *icon) {
338
+ SDL_Surface *iconSurface = R2D_CreateImageSurface(icon);
339
+ if (iconSurface) {
340
340
  window->icon = icon;
341
- SDL_SetWindowIcon(window->sdl, img->surface);
342
- S2D_FreeImage(img);
341
+ SDL_SetWindowIcon(window->sdl, iconSurface);
342
+ SDL_FreeSurface(iconSurface);
343
343
  } else {
344
- S2D_Log(S2D_WARN, "Could not set window icon");
344
+ R2D_Log(R2D_WARN, "Could not set window icon");
345
345
  }
346
346
  }
347
347
 
@@ -349,10 +349,10 @@ void S2D_SetIcon(S2D_Window *window, const char *icon) {
349
349
  /*
350
350
  * Take a screenshot of the window
351
351
  */
352
- void S2D_Screenshot(S2D_Window *window, const char *path) {
352
+ void R2D_Screenshot(R2D_Window *window, const char *path) {
353
353
 
354
354
  #if GLES
355
- S2D_Error("S2D_Screenshot", "Not supported in OpenGL ES");
355
+ R2D_Error("R2D_Screenshot", "Not supported in OpenGL ES");
356
356
  #else
357
357
  // Create a surface the size of the window
358
358
  SDL_Surface *surface = SDL_CreateRGBSurface(
@@ -368,7 +368,7 @@ void S2D_Screenshot(S2D_Window *window, const char *path) {
368
368
 
369
369
  void *temp_row = (void *)malloc(surface->pitch);
370
370
  if (!temp_row) {
371
- S2D_Error("S2D_Screenshot", "Out of memory!");
371
+ R2D_Error("R2D_Screenshot", "Out of memory!");
372
372
  SDL_FreeSurface(surface);
373
373
  return;
374
374
  }
@@ -393,9 +393,9 @@ void S2D_Screenshot(S2D_Window *window, const char *path) {
393
393
  /*
394
394
  * Close the window
395
395
  */
396
- int S2D_Close(S2D_Window *window) {
396
+ int R2D_Close(R2D_Window *window) {
397
397
  if (!window->close) {
398
- S2D_Log(S2D_INFO, "Closing window");
398
+ R2D_Log(R2D_INFO, "Closing window");
399
399
  window->close = true;
400
400
  }
401
401
  return 0;
@@ -405,8 +405,8 @@ int S2D_Close(S2D_Window *window) {
405
405
  /*
406
406
  * Free all resources
407
407
  */
408
- int S2D_FreeWindow(S2D_Window *window) {
409
- S2D_Close(window);
408
+ int R2D_FreeWindow(R2D_Window *window) {
409
+ R2D_Close(window);
410
410
  SDL_GL_DeleteContext(window->glcontext);
411
411
  SDL_DestroyWindow(window->sdl);
412
412
  free(window);
data/lib/ruby2d/circle.rb CHANGED
@@ -10,10 +10,10 @@ module Ruby2D
10
10
  @x = opts[:x] || 25
11
11
  @y = opts[:y] || 25
12
12
  @z = opts[:z] || 0
13
- @radius = opts[:radius] || 25
14
- @sectors = opts[:sectors] || 20
13
+ @radius = opts[:radius] || 50
14
+ @sectors = opts[:sectors] || 30
15
15
  self.color = opts[:color] || 'white'
16
- self.opacity = opts[:opacity] if opts[:opacity]
16
+ self.color.opacity = opts[:opacity] if opts[:opacity]
17
17
  add
18
18
  end
19
19
 
@@ -21,5 +21,23 @@ module Ruby2D
21
21
  Math.sqrt((x - @x)**2 + (y - @y)**2) <= @radius
22
22
  end
23
23
 
24
+ def self.draw(opts = {})
25
+ Window.render_ready_check
26
+
27
+ ext_draw([
28
+ opts[:x], opts[:y], opts[:radius], opts[:sectors],
29
+ opts[:color][0], opts[:color][1], opts[:color][2], opts[:color][3]
30
+ ])
31
+ end
32
+
33
+ private
34
+
35
+ def render
36
+ self.class.ext_draw([
37
+ @x, @y, @radius, @sectors,
38
+ @color.r, @color.g, @color.b, @color.a
39
+ ])
40
+ end
41
+
24
42
  end
25
43
  end