ruby2d 0.9.4 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/ruby2d +1 -0
- data/{assets/linux/simple2d/src/simple2d.c → ext/ruby2d/common.c} +32 -32
- data/{assets/linux/simple2d/src → ext/ruby2d}/controllers.c +17 -17
- data/ext/ruby2d/extconf.rb +4 -34
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +75 -75
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl2.c +20 -20
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl3.c +29 -29
- data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +26 -26
- data/{assets/linux/simple2d/src → ext/ruby2d}/image.c +16 -16
- data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
- data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +17 -17
- data/ext/ruby2d/ruby2d.c +125 -126
- data/{assets/include/simple2d.h → ext/ruby2d/ruby2d.h} +207 -207
- data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
- data/{assets/linux/simple2d/src → ext/ruby2d}/sound.c +14 -14
- data/{assets/linux/simple2d/src → ext/ruby2d}/sprite.c +16 -16
- data/{assets/linux/simple2d/src → ext/ruby2d}/text.c +22 -22
- data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +61 -61
- data/lib/ruby2d/cli/build.rb +2 -7
- data/lib/ruby2d/version.rb +1 -1
- metadata +21 -26
- data/assets/linux/simple2d/Makefile +0 -260
- data/assets/linux/simple2d/bin/simple2d.sh +0 -1318
- data/assets/linux/simple2d/include/simple2d.h +0 -757
- data/assets/macos/lib/libsimple2d.a +0 -0
- data/assets/mingw/lib/libsimple2d.a +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
// shapes.c
|
2
2
|
|
3
|
-
#include "
|
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
|
-
|
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
|
-
|
42
|
+
R2D_GL_Point R2D_GetRectRotationPoint(int x, int y, int w, int h, int position) {
|
43
43
|
|
44
|
-
|
44
|
+
R2D_GL_Point p;
|
45
45
|
|
46
46
|
switch (position) {
|
47
|
-
case
|
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
|
51
|
+
case R2D_TOP_LEFT:
|
52
52
|
p.x = x;
|
53
53
|
p.y = y;
|
54
54
|
break;
|
55
|
-
case
|
55
|
+
case R2D_TOP_RIGHT:
|
56
56
|
p.x = x + w;
|
57
57
|
p.y = y;
|
58
58
|
break;
|
59
|
-
case
|
59
|
+
case R2D_BOTTOM_LEFT:
|
60
60
|
p.x = x;
|
61
61
|
p.y = y + h;
|
62
62
|
break;
|
63
|
-
case
|
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
|
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
|
-
|
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
|
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
|
-
|
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
|
-
|
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
|
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
|
-
|
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
|
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
|
-
|
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 "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
5
|
|
6
6
|
/*
|
7
7
|
* Create a sound, given an audio file path
|
8
8
|
*/
|
9
|
-
|
10
|
-
|
9
|
+
R2D_Sound *R2D_CreateSound(const char *path) {
|
10
|
+
R2D_Init();
|
11
11
|
|
12
12
|
// Check if sound file exists
|
13
|
-
if (!
|
14
|
-
|
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
|
-
|
19
|
+
R2D_Sound *snd = (R2D_Sound *) malloc(sizeof(R2D_Sound));
|
20
20
|
if (!snd) {
|
21
|
-
|
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
|
-
|
28
|
+
R2D_Error("Mix_LoadWAV", Mix_GetError());
|
29
29
|
free(snd);
|
30
30
|
return NULL;
|
31
31
|
}
|
@@ -40,7 +40,7 @@ S2D_Sound *S2D_CreateSound(const char *path) {
|
|
40
40
|
/*
|
41
41
|
* Play the sound
|
42
42
|
*/
|
43
|
-
void
|
43
|
+
void R2D_PlaySound(R2D_Sound *snd) {
|
44
44
|
if (!snd) return;
|
45
45
|
Mix_PlayChannel(-1, snd->data, 0);
|
46
46
|
}
|
@@ -49,7 +49,7 @@ void S2D_PlaySound(S2D_Sound *snd) {
|
|
49
49
|
/*
|
50
50
|
* Get the sound's volume
|
51
51
|
*/
|
52
|
-
int
|
52
|
+
int R2D_GetSoundVolume(R2D_Sound *snd) {
|
53
53
|
if (!snd) return -1;
|
54
54
|
return ceil(Mix_VolumeChunk(snd->data, -1) * (100.0 / MIX_MAX_VOLUME));
|
55
55
|
}
|
@@ -58,7 +58,7 @@ int S2D_GetSoundVolume(S2D_Sound *snd) {
|
|
58
58
|
/*
|
59
59
|
* Set the sound's volume a given percentage
|
60
60
|
*/
|
61
|
-
void
|
61
|
+
void R2D_SetSoundVolume(R2D_Sound *snd, int volume) {
|
62
62
|
if (!snd) return;
|
63
63
|
// Set volume to be a percentage of the maximum mix volume
|
64
64
|
Mix_VolumeChunk(snd->data, (volume / 100.0) * MIX_MAX_VOLUME);
|
@@ -68,7 +68,7 @@ void S2D_SetSoundVolume(S2D_Sound *snd, int volume) {
|
|
68
68
|
/*
|
69
69
|
* Get the sound mixer volume
|
70
70
|
*/
|
71
|
-
int
|
71
|
+
int R2D_GetSoundMixVolume() {
|
72
72
|
return ceil(Mix_Volume(-1, -1) * (100.0 / MIX_MAX_VOLUME));
|
73
73
|
}
|
74
74
|
|
@@ -76,7 +76,7 @@ int S2D_GetSoundMixVolume() {
|
|
76
76
|
/*
|
77
77
|
* Set the sound mixer volume a given percentage
|
78
78
|
*/
|
79
|
-
void
|
79
|
+
void R2D_SetSoundMixVolume(int volume) {
|
80
80
|
// This sets the volume value across all channels
|
81
81
|
// Set volume to be a percentage of the maximum mix volume
|
82
82
|
Mix_Volume(-1, (volume / 100.0) * MIX_MAX_VOLUME);
|
@@ -86,7 +86,7 @@ void S2D_SetSoundMixVolume(int volume) {
|
|
86
86
|
/*
|
87
87
|
* Free the sound
|
88
88
|
*/
|
89
|
-
void
|
89
|
+
void R2D_FreeSound(R2D_Sound *snd) {
|
90
90
|
if (!snd) return;
|
91
91
|
Mix_FreeChunk(snd->data);
|
92
92
|
free(snd);
|
@@ -1,30 +1,30 @@
|
|
1
1
|
// sprite.c
|
2
2
|
|
3
|
-
#include "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
5
|
|
6
6
|
/*
|
7
7
|
* Create a sprite, given an image file path
|
8
8
|
*/
|
9
|
-
|
9
|
+
R2D_Sprite *R2D_CreateSprite(const char *path) {
|
10
10
|
|
11
11
|
// Check if image file exists
|
12
|
-
if (!
|
13
|
-
|
12
|
+
if (!R2D_FileExists(path)) {
|
13
|
+
R2D_Error("R2D_CreateSprite", "Sprite image file `%s` not found", path);
|
14
14
|
return NULL;
|
15
15
|
}
|
16
16
|
|
17
17
|
// Allocate the sprite structure
|
18
|
-
|
18
|
+
R2D_Sprite *spr = (R2D_Sprite *) malloc(sizeof(R2D_Sprite));
|
19
19
|
if (!spr) {
|
20
|
-
|
20
|
+
R2D_Error("R2D_CreateSprite", "Out of memory!");
|
21
21
|
return NULL;
|
22
22
|
}
|
23
23
|
|
24
24
|
// Load the sprite image file
|
25
|
-
spr->img =
|
25
|
+
spr->img = R2D_CreateImage(path);
|
26
26
|
if (!spr->img) {
|
27
|
-
|
27
|
+
R2D_Error("R2D_CreateSprite", "Cannot create sprite image `%s`", path);
|
28
28
|
free(spr);
|
29
29
|
return NULL;
|
30
30
|
}
|
@@ -60,7 +60,7 @@ S2D_Sprite *S2D_CreateSprite(const char *path) {
|
|
60
60
|
/*
|
61
61
|
* Clip a sprite
|
62
62
|
*/
|
63
|
-
void
|
63
|
+
void R2D_ClipSprite(R2D_Sprite *spr, int x, int y, int w, int h) {
|
64
64
|
if (!spr) return;
|
65
65
|
|
66
66
|
// Calculate ratios
|
@@ -108,9 +108,9 @@ void S2D_ClipSprite(S2D_Sprite *spr, int x, int y, int w, int h) {
|
|
108
108
|
/*
|
109
109
|
* Rotate a sprite
|
110
110
|
*/
|
111
|
-
void
|
111
|
+
void R2D_RotateSprite(R2D_Sprite *spr, GLfloat angle, int position) {
|
112
112
|
|
113
|
-
|
113
|
+
R2D_GL_Point p = R2D_GetRectRotationPoint(
|
114
114
|
spr->x, spr->y, spr->width, spr->height, position
|
115
115
|
);
|
116
116
|
|
@@ -123,25 +123,25 @@ void S2D_RotateSprite(S2D_Sprite *spr, GLfloat angle, int position) {
|
|
123
123
|
/*
|
124
124
|
* Draw a sprite
|
125
125
|
*/
|
126
|
-
void
|
126
|
+
void R2D_DrawSprite(R2D_Sprite *spr) {
|
127
127
|
if (!spr) return;
|
128
128
|
|
129
129
|
if (spr->img->texture_id == 0) {
|
130
|
-
|
130
|
+
R2D_GL_CreateTexture(&spr->img->texture_id, spr->img->format,
|
131
131
|
spr->img->width, spr->img->height,
|
132
132
|
spr->img->surface->pixels, GL_NEAREST);
|
133
133
|
SDL_FreeSurface(spr->img->surface);
|
134
134
|
}
|
135
135
|
|
136
|
-
|
136
|
+
R2D_GL_DrawSprite(spr);
|
137
137
|
}
|
138
138
|
|
139
139
|
|
140
140
|
/*
|
141
141
|
* Free a sprite
|
142
142
|
*/
|
143
|
-
void
|
143
|
+
void R2D_FreeSprite(R2D_Sprite *spr) {
|
144
144
|
if (!spr) return;
|
145
|
-
|
145
|
+
R2D_FreeImage(spr->img);
|
146
146
|
free(spr);
|
147
147
|
}
|
@@ -1,17 +1,17 @@
|
|
1
1
|
// text.c
|
2
2
|
|
3
|
-
#include "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
5
|
|
6
6
|
/*
|
7
7
|
* Create text, given a font file path, the message, and size
|
8
8
|
*/
|
9
|
-
|
10
|
-
|
9
|
+
R2D_Text *R2D_CreateText(const char *font, const char *msg, int size) {
|
10
|
+
R2D_Init();
|
11
11
|
|
12
12
|
// Check if font file exists
|
13
|
-
if (!
|
14
|
-
|
13
|
+
if (!R2D_FileExists(font)) {
|
14
|
+
R2D_Error("R2D_CreateText", "Font file `%s` not found", font);
|
15
15
|
return NULL;
|
16
16
|
}
|
17
17
|
|
@@ -19,16 +19,16 @@ S2D_Text *S2D_CreateText(const char *font, const char *msg, int size) {
|
|
19
19
|
if (msg == NULL || strlen(msg) == 0) msg = " ";
|
20
20
|
|
21
21
|
// Allocate the text structure
|
22
|
-
|
22
|
+
R2D_Text *txt = (R2D_Text *) malloc(sizeof(R2D_Text));
|
23
23
|
if (!txt) {
|
24
|
-
|
24
|
+
R2D_Error("R2D_CreateText", "Out of memory!");
|
25
25
|
return NULL;
|
26
26
|
}
|
27
27
|
|
28
28
|
// Open the font
|
29
29
|
txt->font_data = TTF_OpenFont(font, size);
|
30
30
|
if (!txt->font_data) {
|
31
|
-
|
31
|
+
R2D_Error("TTF_OpenFont", TTF_GetError());
|
32
32
|
free(txt);
|
33
33
|
return NULL;
|
34
34
|
}
|
@@ -49,7 +49,7 @@ S2D_Text *S2D_CreateText(const char *font, const char *msg, int size) {
|
|
49
49
|
txt->texture_id = 0;
|
50
50
|
|
51
51
|
// Save the width and height of the text
|
52
|
-
|
52
|
+
TTF_SizeUTF8(txt->font_data, txt->msg, &txt->width, &txt->height);
|
53
53
|
|
54
54
|
return txt;
|
55
55
|
}
|
@@ -58,10 +58,10 @@ S2D_Text *S2D_CreateText(const char *font, const char *msg, int size) {
|
|
58
58
|
/*
|
59
59
|
* Set the text message
|
60
60
|
*/
|
61
|
-
void
|
61
|
+
void R2D_SetText(R2D_Text *txt, const char *msg, ...) {
|
62
62
|
if (!txt) return;
|
63
63
|
|
64
|
-
// `msg` cannot be an empty string or NULL for
|
64
|
+
// `msg` cannot be an empty string or NULL for TTF_SizeUTF8
|
65
65
|
if (msg == NULL || strlen(msg) == 0) msg = " ";
|
66
66
|
|
67
67
|
// Format and store new text string
|
@@ -72,19 +72,19 @@ void S2D_SetText(S2D_Text *txt, const char *msg, ...) {
|
|
72
72
|
va_end(args);
|
73
73
|
|
74
74
|
// Save the width and height of the text
|
75
|
-
|
75
|
+
TTF_SizeUTF8(txt->font_data, txt->msg, &txt->width, &txt->height);
|
76
76
|
|
77
77
|
// Delete the current texture so a new one can be generated
|
78
|
-
|
78
|
+
R2D_GL_FreeTexture(&txt->texture_id);
|
79
79
|
}
|
80
80
|
|
81
81
|
|
82
82
|
/*
|
83
83
|
* Rotate text
|
84
84
|
*/
|
85
|
-
void
|
85
|
+
void R2D_RotateText(R2D_Text *txt, GLfloat angle, int position) {
|
86
86
|
|
87
|
-
|
87
|
+
R2D_GL_Point p = R2D_GetRectRotationPoint(
|
88
88
|
txt->x, txt->y, txt->width, txt->height, position
|
89
89
|
);
|
90
90
|
|
@@ -97,33 +97,33 @@ void S2D_RotateText(S2D_Text *txt, GLfloat angle, int position) {
|
|
97
97
|
/*
|
98
98
|
* Draw text
|
99
99
|
*/
|
100
|
-
void
|
100
|
+
void R2D_DrawText(R2D_Text *txt) {
|
101
101
|
if (!txt) return;
|
102
102
|
|
103
103
|
if (txt->texture_id == 0) {
|
104
104
|
SDL_Color color = { 255, 255, 255 };
|
105
|
-
txt->surface =
|
105
|
+
txt->surface = TTF_RenderUTF8_Blended(txt->font_data, txt->msg, color);
|
106
106
|
if (!txt->surface) {
|
107
|
-
|
107
|
+
R2D_Error("TTF_RenderUTF8_Blended", TTF_GetError());
|
108
108
|
return;
|
109
109
|
}
|
110
|
-
|
110
|
+
R2D_GL_CreateTexture(&txt->texture_id, GL_RGBA,
|
111
111
|
txt->width, txt->height,
|
112
112
|
txt->surface->pixels, GL_NEAREST);
|
113
113
|
SDL_FreeSurface(txt->surface);
|
114
114
|
}
|
115
115
|
|
116
|
-
|
116
|
+
R2D_GL_DrawText(txt);
|
117
117
|
}
|
118
118
|
|
119
119
|
|
120
120
|
/*
|
121
121
|
* Free the text
|
122
122
|
*/
|
123
|
-
void
|
123
|
+
void R2D_FreeText(R2D_Text *txt) {
|
124
124
|
if (!txt) return;
|
125
125
|
free(txt->msg);
|
126
|
-
|
126
|
+
R2D_GL_FreeTexture(&txt->texture_id);
|
127
127
|
TTF_CloseFont(txt->font_data);
|
128
128
|
free(txt);
|
129
129
|
}
|
@@ -1,25 +1,25 @@
|
|
1
1
|
// window.c
|
2
2
|
|
3
|
-
#include "
|
3
|
+
#include "ruby2d.h"
|
4
4
|
|
5
5
|
|
6
6
|
/*
|
7
7
|
* Create a window
|
8
8
|
*/
|
9
|
-
|
10
|
-
|
9
|
+
R2D_Window *R2D_CreateWindow(const char *title, int width, int height,
|
10
|
+
R2D_Update update, R2D_Render render, int flags) {
|
11
11
|
|
12
|
-
|
12
|
+
R2D_Init();
|
13
13
|
|
14
14
|
SDL_DisplayMode dm;
|
15
15
|
SDL_GetCurrentDisplayMode(0, &dm);
|
16
|
-
|
16
|
+
R2D_Log(R2D_INFO, "Current display mode is %dx%dpx @ %dhz", dm.w, dm.h, dm.refresh_rate);
|
17
17
|
|
18
|
-
width = width ==
|
19
|
-
height = 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
|
-
|
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 =
|
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
|
56
|
+
int R2D_Show(R2D_Window *window) {
|
57
57
|
|
58
58
|
if (!window) {
|
59
|
-
|
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)
|
72
|
-
if (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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
168
|
-
.type =
|
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
|
-
|
177
|
-
.type =
|
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
|
-
|
186
|
-
|
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 ?
|
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
|
-
|
198
|
-
.type =
|
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
|
-
|
208
|
-
|
209
|
-
.type =
|
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
|
-
|
219
|
-
.which = e.caxis.which, .type =
|
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 && !
|
228
|
-
|
229
|
-
.which = e.jaxis.which, .type =
|
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
|
-
|
238
|
+
R2D_Event event = {
|
239
239
|
.which = e.cbutton.which, .button = e.cbutton.button
|
240
240
|
};
|
241
|
-
event.type = e.type == SDL_CONTROLLERBUTTONDOWN ?
|
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 && !
|
248
|
-
|
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 ?
|
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
|
-
|
258
|
-
|
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 (
|
263
|
-
|
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
|
-
|
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
|
-
|
277
|
+
R2D_GL_SetViewport(window);
|
278
278
|
break;
|
279
279
|
}
|
280
280
|
break;
|
281
281
|
|
282
282
|
case SDL_QUIT:
|
283
|
-
|
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
|
-
|
296
|
-
.type =
|
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
|
-
|
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
|
-
|
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
|
338
|
-
|
337
|
+
void R2D_SetIcon(R2D_Window *window, const char *icon) {
|
338
|
+
R2D_Image *img = R2D_CreateImage(icon);
|
339
339
|
if (img) {
|
340
340
|
window->icon = icon;
|
341
341
|
SDL_SetWindowIcon(window->sdl, img->surface);
|
342
|
-
|
342
|
+
R2D_FreeImage(img);
|
343
343
|
} else {
|
344
|
-
|
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
|
352
|
+
void R2D_Screenshot(R2D_Window *window, const char *path) {
|
353
353
|
|
354
354
|
#if GLES
|
355
|
-
|
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
|
-
|
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
|
396
|
+
int R2D_Close(R2D_Window *window) {
|
397
397
|
if (!window->close) {
|
398
|
-
|
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
|
409
|
-
|
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);
|