ruby2d 0.9.4 → 0.11.1
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.
- checksums.yaml +4 -4
- data/assets/include/SDL2/SDL.h +4 -1
- data/assets/include/SDL2/SDL_assert.h +3 -1
- data/assets/include/SDL2/SDL_atomic.h +1 -1
- data/assets/include/SDL2/SDL_audio.h +1 -1
- data/assets/include/SDL2/SDL_bits.h +1 -1
- data/assets/include/SDL2/SDL_blendmode.h +4 -1
- data/assets/include/SDL2/SDL_clipboard.h +1 -1
- data/assets/include/SDL2/SDL_config.h +1 -1
- data/assets/include/SDL2/SDL_config_android.h +7 -3
- data/assets/include/SDL2/SDL_config_iphoneos.h +16 -10
- data/assets/include/SDL2/SDL_config_macosx.h +40 -14
- data/assets/include/SDL2/SDL_config_minimal.h +1 -1
- data/assets/include/SDL2/SDL_config_os2.h +26 -8
- data/assets/include/SDL2/SDL_config_pandora.h +4 -2
- data/assets/include/SDL2/SDL_config_psp.h +2 -1
- data/assets/include/SDL2/SDL_config_windows.h +40 -9
- data/assets/include/SDL2/SDL_config_winrt.h +5 -2
- data/assets/include/SDL2/SDL_config_wiz.h +5 -2
- data/assets/include/SDL2/SDL_copying.h +1 -1
- data/assets/include/SDL2/SDL_cpuinfo.h +32 -3
- data/assets/include/SDL2/SDL_egl.h +4 -1
- data/assets/include/SDL2/SDL_endian.h +6 -3
- data/assets/include/SDL2/SDL_error.h +38 -2
- data/assets/include/SDL2/SDL_events.h +64 -28
- data/assets/include/SDL2/SDL_filesystem.h +1 -1
- data/assets/include/SDL2/SDL_gamecontroller.h +160 -9
- data/assets/include/SDL2/SDL_gesture.h +1 -1
- data/assets/include/SDL2/SDL_haptic.h +10 -1
- data/assets/include/SDL2/SDL_hints.h +316 -4
- data/assets/include/SDL2/SDL_joystick.h +115 -24
- data/assets/include/SDL2/SDL_keyboard.h +1 -1
- data/assets/include/SDL2/SDL_keycode.h +11 -9
- data/assets/include/SDL2/SDL_loadso.h +1 -1
- data/assets/include/SDL2/SDL_locale.h +101 -0
- data/assets/include/SDL2/SDL_log.h +3 -3
- data/assets/include/SDL2/SDL_main.h +1 -1
- data/assets/include/SDL2/SDL_messagebox.h +6 -4
- data/assets/include/SDL2/SDL_metal.h +117 -0
- data/assets/include/SDL2/SDL_misc.h +75 -0
- data/assets/include/SDL2/SDL_mouse.h +1 -1
- data/assets/include/SDL2/SDL_mutex.h +1 -1
- data/assets/include/SDL2/SDL_name.h +1 -1
- data/assets/include/SDL2/SDL_opengl.h +1 -1
- data/assets/include/SDL2/SDL_opengl_glext.h +3 -0
- data/assets/include/SDL2/SDL_opengles.h +1 -1
- data/assets/include/SDL2/SDL_opengles2.h +1 -1
- data/assets/include/SDL2/SDL_pixels.h +25 -16
- data/assets/include/SDL2/SDL_platform.h +1 -1
- data/assets/include/SDL2/SDL_power.h +1 -1
- data/assets/include/SDL2/SDL_quit.h +1 -1
- data/assets/include/SDL2/SDL_rect.h +1 -1
- data/assets/include/SDL2/SDL_render.h +63 -3
- data/assets/include/SDL2/SDL_revision.h +2 -2
- data/assets/include/SDL2/SDL_rwops.h +2 -10
- data/assets/include/SDL2/SDL_scancode.h +1 -1
- data/assets/include/SDL2/SDL_sensor.h +25 -9
- data/assets/include/SDL2/SDL_shape.h +1 -1
- data/assets/include/SDL2/SDL_stdinc.h +44 -4
- data/assets/include/SDL2/SDL_surface.h +11 -2
- data/assets/include/SDL2/SDL_system.h +50 -4
- data/assets/include/SDL2/SDL_syswm.h +29 -2
- data/assets/include/SDL2/SDL_test.h +1 -1
- data/assets/include/SDL2/SDL_test_assert.h +1 -1
- data/assets/include/SDL2/SDL_test_common.h +14 -1
- data/assets/include/SDL2/SDL_test_compare.h +1 -1
- data/assets/include/SDL2/SDL_test_crc32.h +1 -1
- data/assets/include/SDL2/SDL_test_font.h +1 -1
- data/assets/include/SDL2/SDL_test_fuzzer.h +1 -1
- data/assets/include/SDL2/SDL_test_harness.h +1 -1
- data/assets/include/SDL2/SDL_test_images.h +1 -1
- data/assets/include/SDL2/SDL_test_log.h +1 -1
- data/assets/include/SDL2/SDL_test_md5.h +1 -1
- data/assets/include/SDL2/SDL_test_memory.h +1 -1
- data/assets/include/SDL2/SDL_test_random.h +1 -1
- data/assets/include/SDL2/SDL_thread.h +34 -11
- data/assets/include/SDL2/SDL_timer.h +1 -1
- data/assets/include/SDL2/SDL_touch.h +1 -1
- data/assets/include/SDL2/SDL_types.h +1 -1
- data/assets/include/SDL2/SDL_version.h +2 -2
- data/assets/include/SDL2/SDL_video.h +11 -5
- data/assets/include/SDL2/SDL_vulkan.h +6 -8
- data/assets/include/SDL2/begin_code.h +5 -9
- data/assets/include/SDL2/close_code.h +1 -1
- data/assets/macos/lib/libFLAC.a +0 -0
- data/assets/macos/lib/libSDL2.a +0 -0
- data/assets/macos/lib/libSDL2_image.a +0 -0
- data/assets/macos/lib/libSDL2_mixer.a +0 -0
- data/assets/macos/lib/libSDL2_ttf.a +0 -0
- data/assets/macos/lib/libfreetype.a +0 -0
- data/assets/macos/lib/libjpeg.a +0 -0
- data/assets/macos/lib/libmodplug.a +0 -0
- data/assets/macos/lib/libmpg123.a +0 -0
- data/assets/macos/lib/libogg.a +0 -0
- data/assets/macos/lib/libpng16.a +0 -0
- data/assets/macos/lib/libtiff.a +0 -0
- data/assets/macos/lib/libvorbis.a +0 -0
- data/assets/macos/lib/libvorbisfile.a +0 -0
- data/assets/macos/lib/libwebp.a +0 -0
- data/assets/mingw/bin/SDL2.dll +0 -0
- data/assets/mingw/lib/libSDL2.a +0 -0
- data/assets/mingw/lib/libSDL2.dll.a +0 -0
- data/assets/mingw/lib/libSDL2_test.a +0 -0
- data/assets/mingw/lib/libSDL2main.a +0 -0
- 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 +37 -59
- data/ext/ruby2d/font.c +35 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl.c +66 -98
- data/ext/ruby2d/gl2.c +86 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/gl3.c +69 -112
- data/{assets/linux/simple2d/src → ext/ruby2d}/gles.c +22 -81
- data/ext/ruby2d/image.c +58 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/input.c +8 -8
- data/{assets/linux/simple2d/src → ext/ruby2d}/music.c +30 -17
- data/ext/ruby2d/ruby2d.c +449 -439
- data/ext/ruby2d/ruby2d.h +652 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/shapes.c +18 -18
- data/{assets/linux/simple2d/src → ext/ruby2d}/sound.c +39 -14
- data/ext/ruby2d/text.c +22 -0
- data/{assets/linux/simple2d/src → ext/ruby2d}/window.c +63 -63
- data/lib/ruby2d/circle.rb +21 -3
- data/lib/ruby2d/cli/build.rb +4 -7
- data/lib/ruby2d/dsl.rb +16 -9
- data/lib/ruby2d/entity.rb +17 -0
- data/lib/ruby2d/font.rb +23 -3
- data/lib/ruby2d/image.rb +35 -7
- data/lib/ruby2d/line.rb +23 -1
- data/lib/ruby2d/music.rb +5 -0
- data/lib/ruby2d/pixel.rb +17 -0
- data/lib/ruby2d/quad.rb +21 -1
- data/lib/ruby2d/rectangle.rb +12 -3
- data/lib/ruby2d/renderable.rb +4 -20
- data/lib/ruby2d/sound.rb +30 -0
- data/lib/ruby2d/sprite.rb +57 -75
- data/lib/ruby2d/square.rb +10 -1
- data/lib/ruby2d/text.rb +55 -12
- data/lib/ruby2d/texture.rb +28 -0
- data/lib/ruby2d/tileset.rb +87 -0
- data/lib/ruby2d/triangle.rb +19 -1
- data/lib/ruby2d/version.rb +1 -1
- data/lib/ruby2d/vertices.rb +84 -0
- data/lib/ruby2d/window.rb +259 -22
- data/lib/ruby2d.rb +6 -1
- metadata +30 -26
- data/assets/include/simple2d.h +0 -757
- 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/linux/simple2d/src/gl2.c +0 -146
- data/assets/linux/simple2d/src/image.c +0 -138
- data/assets/linux/simple2d/src/sprite.c +0 -147
- data/assets/linux/simple2d/src/text.c +0 -129
- data/assets/macos/lib/libsimple2d.a +0 -0
- data/assets/mingw/lib/libsimple2d.a +0 -0
@@ -1,146 +0,0 @@
|
|
1
|
-
// OpenGL 2.1
|
2
|
-
|
3
|
-
#include "../include/simple2d.h"
|
4
|
-
|
5
|
-
#if !GLES
|
6
|
-
|
7
|
-
|
8
|
-
/*
|
9
|
-
* Applies the projection matrix
|
10
|
-
*/
|
11
|
-
void S2D_GL2_ApplyProjection(int w, int h) {
|
12
|
-
|
13
|
-
// Initialize the projection matrix
|
14
|
-
glMatrixMode(GL_PROJECTION);
|
15
|
-
glLoadIdentity();
|
16
|
-
|
17
|
-
// Multiply the current matrix with the orthographic matrix
|
18
|
-
glOrtho(0.f, w, h, 0.f, -1.f, 1.f);
|
19
|
-
|
20
|
-
// Initialize the model-view matrix
|
21
|
-
glMatrixMode(GL_MODELVIEW);
|
22
|
-
glLoadIdentity();
|
23
|
-
}
|
24
|
-
|
25
|
-
|
26
|
-
/*
|
27
|
-
* Initalize OpenGL
|
28
|
-
*/
|
29
|
-
int S2D_GL2_Init() {
|
30
|
-
|
31
|
-
GLenum error = GL_NO_ERROR;
|
32
|
-
|
33
|
-
// Enable transparency
|
34
|
-
glEnable(GL_BLEND);
|
35
|
-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
36
|
-
|
37
|
-
// Check for errors
|
38
|
-
error = glGetError();
|
39
|
-
if (error != GL_NO_ERROR) {
|
40
|
-
S2D_GL_PrintError("OpenGL initialization failed");
|
41
|
-
return 1;
|
42
|
-
} else {
|
43
|
-
return 0;
|
44
|
-
}
|
45
|
-
}
|
46
|
-
|
47
|
-
|
48
|
-
/*
|
49
|
-
* Draw triangle
|
50
|
-
*/
|
51
|
-
void S2D_GL2_DrawTriangle(GLfloat x1, GLfloat y1,
|
52
|
-
GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
|
53
|
-
GLfloat x2, GLfloat y2,
|
54
|
-
GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
|
55
|
-
GLfloat x3, GLfloat y3,
|
56
|
-
GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
|
57
|
-
|
58
|
-
glBegin(GL_TRIANGLES);
|
59
|
-
glColor4f(r1, g1, b1, a1); glVertex2f(x1, y1);
|
60
|
-
glColor4f(r2, g2, b2, a2); glVertex2f(x2, y2);
|
61
|
-
glColor4f(r3, g3, b3, a3); glVertex2f(x3, y3);
|
62
|
-
glEnd();
|
63
|
-
}
|
64
|
-
|
65
|
-
|
66
|
-
/*
|
67
|
-
* Draw texture
|
68
|
-
*/
|
69
|
-
static void S2D_GL2_DrawTexture(int x, int y, int w, int h,
|
70
|
-
GLfloat angle, GLfloat rx, GLfloat ry,
|
71
|
-
GLfloat r, GLfloat g, GLfloat b, GLfloat a,
|
72
|
-
GLfloat tx1, GLfloat ty1, GLfloat tx2, GLfloat ty2,
|
73
|
-
GLfloat tx3, GLfloat ty3, GLfloat tx4, GLfloat ty4,
|
74
|
-
GLuint texture_id) {
|
75
|
-
|
76
|
-
S2D_GL_Point v1 = { .x = x, .y = y };
|
77
|
-
S2D_GL_Point v2 = { .x = x + w, .y = y };
|
78
|
-
S2D_GL_Point v3 = { .x = x + w, .y = y + h };
|
79
|
-
S2D_GL_Point v4 = { .x = x, .y = y + h };
|
80
|
-
|
81
|
-
// Rotate vertices
|
82
|
-
if (angle != 0) {
|
83
|
-
v1 = S2D_RotatePoint(v1, angle, rx, ry);
|
84
|
-
v2 = S2D_RotatePoint(v2, angle, rx, ry);
|
85
|
-
v3 = S2D_RotatePoint(v3, angle, rx, ry);
|
86
|
-
v4 = S2D_RotatePoint(v4, angle, rx, ry);
|
87
|
-
}
|
88
|
-
|
89
|
-
glEnable(GL_TEXTURE_2D);
|
90
|
-
|
91
|
-
glBindTexture(GL_TEXTURE_2D, texture_id);
|
92
|
-
|
93
|
-
glBegin(GL_QUADS);
|
94
|
-
glColor4f(r, g, b, a);
|
95
|
-
glTexCoord2f(tx1, ty1); glVertex2f(v1.x, v1.y);
|
96
|
-
glTexCoord2f(tx2, ty2); glVertex2f(v2.x, v2.y);
|
97
|
-
glTexCoord2f(tx3, ty3); glVertex2f(v3.x, v3.y);
|
98
|
-
glTexCoord2f(tx4, ty4); glVertex2f(v4.x, v4.y);
|
99
|
-
glEnd();
|
100
|
-
|
101
|
-
glDisable(GL_TEXTURE_2D);
|
102
|
-
}
|
103
|
-
|
104
|
-
|
105
|
-
/*
|
106
|
-
* Draw image
|
107
|
-
*/
|
108
|
-
void S2D_GL2_DrawImage(S2D_Image *img) {
|
109
|
-
S2D_GL2_DrawTexture(
|
110
|
-
img->x, img->y, img->width, img->height,
|
111
|
-
img->rotate, img->rx, img->ry,
|
112
|
-
img->color.r, img->color.g, img->color.b, img->color.a,
|
113
|
-
0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
|
114
|
-
img->texture_id
|
115
|
-
);
|
116
|
-
}
|
117
|
-
|
118
|
-
|
119
|
-
/*
|
120
|
-
* Draw sprite
|
121
|
-
*/
|
122
|
-
void S2D_GL2_DrawSprite(S2D_Sprite *spr) {
|
123
|
-
S2D_GL2_DrawTexture(
|
124
|
-
spr->x, spr->y, spr->width, spr->height,
|
125
|
-
spr->rotate, spr->rx, spr->ry,
|
126
|
-
spr->color.r, spr->color.g, spr->color.b, spr->color.a,
|
127
|
-
spr->tx1, spr->ty1, spr->tx2, spr->ty2, spr->tx3, spr->ty3, spr->tx4, spr->ty4,
|
128
|
-
spr->img->texture_id
|
129
|
-
);
|
130
|
-
}
|
131
|
-
|
132
|
-
|
133
|
-
/*
|
134
|
-
* Draw text
|
135
|
-
*/
|
136
|
-
void S2D_GL2_DrawText(S2D_Text *txt) {
|
137
|
-
S2D_GL2_DrawTexture(
|
138
|
-
txt->x, txt->y, txt->width, txt->height,
|
139
|
-
txt->rotate, txt->rx, txt->ry,
|
140
|
-
txt->color.r, txt->color.g, txt->color.b, txt->color.a,
|
141
|
-
0.f, 0.f, 1.f, 0.f, 1.f, 1.f, 0.f, 1.f,
|
142
|
-
txt->texture_id
|
143
|
-
);
|
144
|
-
}
|
145
|
-
|
146
|
-
#endif
|
@@ -1,138 +0,0 @@
|
|
1
|
-
// image.c
|
2
|
-
|
3
|
-
#include "../include/simple2d.h"
|
4
|
-
|
5
|
-
|
6
|
-
/*
|
7
|
-
* Create an image, given a file path
|
8
|
-
*/
|
9
|
-
S2D_Image *S2D_CreateImage(const char *path) {
|
10
|
-
S2D_Init();
|
11
|
-
|
12
|
-
// Check if image file exists
|
13
|
-
if (!S2D_FileExists(path)) {
|
14
|
-
S2D_Error("S2D_CreateImage", "Image file `%s` not found", path);
|
15
|
-
return NULL;
|
16
|
-
}
|
17
|
-
|
18
|
-
// Allocate the image structure
|
19
|
-
S2D_Image *img = (S2D_Image *) malloc(sizeof(S2D_Image));
|
20
|
-
if (!img) {
|
21
|
-
S2D_Error("S2D_CreateImage", "Out of memory!");
|
22
|
-
return NULL;
|
23
|
-
}
|
24
|
-
|
25
|
-
// Load image from file as SDL_Surface
|
26
|
-
img->surface = IMG_Load(path);
|
27
|
-
if (!img->surface) {
|
28
|
-
S2D_Error("IMG_Load", IMG_GetError());
|
29
|
-
free(img);
|
30
|
-
return NULL;
|
31
|
-
}
|
32
|
-
|
33
|
-
int bits_per_color = img->surface->format->Amask == 0 ?
|
34
|
-
img->surface->format->BitsPerPixel / 3 :
|
35
|
-
img->surface->format->BitsPerPixel / 4;
|
36
|
-
|
37
|
-
if (bits_per_color < 8) {
|
38
|
-
S2D_Log(S2D_WARN, "`%s` has less than 8 bits per color and will likely not render correctly", path, bits_per_color);
|
39
|
-
}
|
40
|
-
|
41
|
-
// Initialize values
|
42
|
-
img->path = path;
|
43
|
-
img->x = 0;
|
44
|
-
img->y = 0;
|
45
|
-
img->color.r = 1.f;
|
46
|
-
img->color.g = 1.f;
|
47
|
-
img->color.b = 1.f;
|
48
|
-
img->color.a = 1.f;
|
49
|
-
img->orig_width = img->surface->w;
|
50
|
-
img->orig_height = img->surface->h;
|
51
|
-
img->width = img->orig_width;
|
52
|
-
img->height = img->orig_height;
|
53
|
-
img->rotate = 0;
|
54
|
-
img->rx = 0;
|
55
|
-
img->ry = 0;
|
56
|
-
img->texture_id = 0;
|
57
|
-
|
58
|
-
// Detect image mode
|
59
|
-
img->format = GL_RGB;
|
60
|
-
if (img->surface->format->BytesPerPixel == 4) {
|
61
|
-
img->format = GL_RGBA;
|
62
|
-
}
|
63
|
-
|
64
|
-
// Flip image bits if BGA
|
65
|
-
|
66
|
-
Uint32 r = img->surface->format->Rmask;
|
67
|
-
Uint32 g = img->surface->format->Gmask;
|
68
|
-
Uint32 a = img->surface->format->Amask;
|
69
|
-
|
70
|
-
if (r&0xFF000000 || r&0xFF0000) {
|
71
|
-
char *p = (char *)img->surface->pixels;
|
72
|
-
int bpp = img->surface->format->BytesPerPixel;
|
73
|
-
int w = img->surface->w;
|
74
|
-
int h = img->surface->h;
|
75
|
-
char tmp;
|
76
|
-
for (int i = 0; i < bpp * w * h; i += bpp) {
|
77
|
-
if (a&0xFF) {
|
78
|
-
tmp = p[i];
|
79
|
-
p[i] = p[i+3];
|
80
|
-
p[i+3] = tmp;
|
81
|
-
}
|
82
|
-
if (g&0xFF0000) {
|
83
|
-
tmp = p[i+1];
|
84
|
-
p[i+1] = p[i+2];
|
85
|
-
p[i+2] = tmp;
|
86
|
-
}
|
87
|
-
if (r&0xFF0000) {
|
88
|
-
tmp = p[i];
|
89
|
-
p[i] = p[i+2];
|
90
|
-
p[i+2] = tmp;
|
91
|
-
}
|
92
|
-
}
|
93
|
-
}
|
94
|
-
|
95
|
-
return img;
|
96
|
-
}
|
97
|
-
|
98
|
-
|
99
|
-
/*
|
100
|
-
* Rotate an image
|
101
|
-
*/
|
102
|
-
void S2D_RotateImage(S2D_Image *img, GLfloat angle, int position) {
|
103
|
-
|
104
|
-
S2D_GL_Point p = S2D_GetRectRotationPoint(
|
105
|
-
img->x, img->y, img->width, img->height, position
|
106
|
-
);
|
107
|
-
|
108
|
-
img->rotate = angle;
|
109
|
-
img->rx = p.x;
|
110
|
-
img->ry = p.y;
|
111
|
-
}
|
112
|
-
|
113
|
-
|
114
|
-
/*
|
115
|
-
* Draw an image
|
116
|
-
*/
|
117
|
-
void S2D_DrawImage(S2D_Image *img) {
|
118
|
-
if (!img) return;
|
119
|
-
|
120
|
-
if (img->texture_id == 0) {
|
121
|
-
S2D_GL_CreateTexture(&img->texture_id, img->format,
|
122
|
-
img->orig_width, img->orig_height,
|
123
|
-
img->surface->pixels, GL_NEAREST);
|
124
|
-
SDL_FreeSurface(img->surface);
|
125
|
-
}
|
126
|
-
|
127
|
-
S2D_GL_DrawImage(img);
|
128
|
-
}
|
129
|
-
|
130
|
-
|
131
|
-
/*
|
132
|
-
* Free an image
|
133
|
-
*/
|
134
|
-
void S2D_FreeImage(S2D_Image *img) {
|
135
|
-
if (!img) return;
|
136
|
-
S2D_GL_FreeTexture(&img->texture_id);
|
137
|
-
free(img);
|
138
|
-
}
|
@@ -1,147 +0,0 @@
|
|
1
|
-
// sprite.c
|
2
|
-
|
3
|
-
#include "../include/simple2d.h"
|
4
|
-
|
5
|
-
|
6
|
-
/*
|
7
|
-
* Create a sprite, given an image file path
|
8
|
-
*/
|
9
|
-
S2D_Sprite *S2D_CreateSprite(const char *path) {
|
10
|
-
|
11
|
-
// Check if image file exists
|
12
|
-
if (!S2D_FileExists(path)) {
|
13
|
-
S2D_Error("S2D_CreateSprite", "Sprite image file `%s` not found", path);
|
14
|
-
return NULL;
|
15
|
-
}
|
16
|
-
|
17
|
-
// Allocate the sprite structure
|
18
|
-
S2D_Sprite *spr = (S2D_Sprite *) malloc(sizeof(S2D_Sprite));
|
19
|
-
if (!spr) {
|
20
|
-
S2D_Error("S2D_CreateSprite", "Out of memory!");
|
21
|
-
return NULL;
|
22
|
-
}
|
23
|
-
|
24
|
-
// Load the sprite image file
|
25
|
-
spr->img = S2D_CreateImage(path);
|
26
|
-
if (!spr->img) {
|
27
|
-
S2D_Error("S2D_CreateSprite", "Cannot create sprite image `%s`", path);
|
28
|
-
free(spr);
|
29
|
-
return NULL;
|
30
|
-
}
|
31
|
-
|
32
|
-
// Initialize values
|
33
|
-
spr->path = path;
|
34
|
-
spr->x = 0;
|
35
|
-
spr->y = 0;
|
36
|
-
spr->color.r = 1.f;
|
37
|
-
spr->color.g = 1.f;
|
38
|
-
spr->color.b = 1.f;
|
39
|
-
spr->color.a = 1.f;
|
40
|
-
spr->width = spr->img->width;
|
41
|
-
spr->height = spr->img->height;
|
42
|
-
spr->clip_width = spr->img->width;
|
43
|
-
spr->clip_height = spr->img->height;
|
44
|
-
spr->rotate = 0;
|
45
|
-
spr->rx = 0;
|
46
|
-
spr->ry = 0;
|
47
|
-
spr->tx1 = 0.f;
|
48
|
-
spr->ty1 = 0.f;
|
49
|
-
spr->tx2 = 1.f;
|
50
|
-
spr->ty2 = 0.f;
|
51
|
-
spr->tx3 = 1.f;
|
52
|
-
spr->ty3 = 1.f;
|
53
|
-
spr->tx4 = 0.f;
|
54
|
-
spr->ty4 = 1.f;
|
55
|
-
|
56
|
-
return spr;
|
57
|
-
}
|
58
|
-
|
59
|
-
|
60
|
-
/*
|
61
|
-
* Clip a sprite
|
62
|
-
*/
|
63
|
-
void S2D_ClipSprite(S2D_Sprite *spr, int x, int y, int w, int h) {
|
64
|
-
if (!spr) return;
|
65
|
-
|
66
|
-
// Calculate ratios
|
67
|
-
// rw = ratio width; rh = ratio height
|
68
|
-
double rw = w / (double)spr->img->width;
|
69
|
-
double rh = h / (double)spr->img->height;
|
70
|
-
|
71
|
-
// Apply ratios to x, y coordinates
|
72
|
-
// cx = crop x coord; cy = crop y coord
|
73
|
-
double cx = x * rw;
|
74
|
-
double cy = y * rh;
|
75
|
-
|
76
|
-
// Convert given width, height to doubles
|
77
|
-
// cw = crop width; ch = crop height
|
78
|
-
double cw = (double)w;
|
79
|
-
double ch = (double)h;
|
80
|
-
|
81
|
-
// Apply ratio to texture width and height
|
82
|
-
// tw = texture width; th = texture height
|
83
|
-
double tw = rw * w;
|
84
|
-
double th = rh * h;
|
85
|
-
|
86
|
-
// Calculate and store sprite texture values
|
87
|
-
|
88
|
-
spr->tx1 = cx / cw;
|
89
|
-
spr->ty1 = cy / ch;
|
90
|
-
|
91
|
-
spr->tx2 = (cx + tw) / cw;
|
92
|
-
spr->ty2 = cy / ch;
|
93
|
-
|
94
|
-
spr->tx3 = (cx + tw) / cw;
|
95
|
-
spr->ty3 = (cy + th) / ch;
|
96
|
-
|
97
|
-
spr->tx4 = cx / cw;
|
98
|
-
spr->ty4 = (cy + th) / ch;
|
99
|
-
|
100
|
-
// Store the sprite dimensions
|
101
|
-
spr->width = (spr->width / (double)spr->clip_width ) * w;
|
102
|
-
spr->height = (spr->height / (double)spr->clip_height) * h;
|
103
|
-
spr->clip_width = w;
|
104
|
-
spr->clip_height = h;
|
105
|
-
}
|
106
|
-
|
107
|
-
|
108
|
-
/*
|
109
|
-
* Rotate a sprite
|
110
|
-
*/
|
111
|
-
void S2D_RotateSprite(S2D_Sprite *spr, GLfloat angle, int position) {
|
112
|
-
|
113
|
-
S2D_GL_Point p = S2D_GetRectRotationPoint(
|
114
|
-
spr->x, spr->y, spr->width, spr->height, position
|
115
|
-
);
|
116
|
-
|
117
|
-
spr->rotate = angle;
|
118
|
-
spr->rx = p.x;
|
119
|
-
spr->ry = p.y;
|
120
|
-
}
|
121
|
-
|
122
|
-
|
123
|
-
/*
|
124
|
-
* Draw a sprite
|
125
|
-
*/
|
126
|
-
void S2D_DrawSprite(S2D_Sprite *spr) {
|
127
|
-
if (!spr) return;
|
128
|
-
|
129
|
-
if (spr->img->texture_id == 0) {
|
130
|
-
S2D_GL_CreateTexture(&spr->img->texture_id, spr->img->format,
|
131
|
-
spr->img->width, spr->img->height,
|
132
|
-
spr->img->surface->pixels, GL_NEAREST);
|
133
|
-
SDL_FreeSurface(spr->img->surface);
|
134
|
-
}
|
135
|
-
|
136
|
-
S2D_GL_DrawSprite(spr);
|
137
|
-
}
|
138
|
-
|
139
|
-
|
140
|
-
/*
|
141
|
-
* Free a sprite
|
142
|
-
*/
|
143
|
-
void S2D_FreeSprite(S2D_Sprite *spr) {
|
144
|
-
if (!spr) return;
|
145
|
-
S2D_FreeImage(spr->img);
|
146
|
-
free(spr);
|
147
|
-
}
|
@@ -1,129 +0,0 @@
|
|
1
|
-
// text.c
|
2
|
-
|
3
|
-
#include "../include/simple2d.h"
|
4
|
-
|
5
|
-
|
6
|
-
/*
|
7
|
-
* Create text, given a font file path, the message, and size
|
8
|
-
*/
|
9
|
-
S2D_Text *S2D_CreateText(const char *font, const char *msg, int size) {
|
10
|
-
S2D_Init();
|
11
|
-
|
12
|
-
// Check if font file exists
|
13
|
-
if (!S2D_FileExists(font)) {
|
14
|
-
S2D_Error("S2D_CreateText", "Font file `%s` not found", font);
|
15
|
-
return NULL;
|
16
|
-
}
|
17
|
-
|
18
|
-
// `msg` cannot be an empty string or NULL for TTF_SizeText
|
19
|
-
if (msg == NULL || strlen(msg) == 0) msg = " ";
|
20
|
-
|
21
|
-
// Allocate the text structure
|
22
|
-
S2D_Text *txt = (S2D_Text *) malloc(sizeof(S2D_Text));
|
23
|
-
if (!txt) {
|
24
|
-
S2D_Error("S2D_CreateText", "Out of memory!");
|
25
|
-
return NULL;
|
26
|
-
}
|
27
|
-
|
28
|
-
// Open the font
|
29
|
-
txt->font_data = TTF_OpenFont(font, size);
|
30
|
-
if (!txt->font_data) {
|
31
|
-
S2D_Error("TTF_OpenFont", TTF_GetError());
|
32
|
-
free(txt);
|
33
|
-
return NULL;
|
34
|
-
}
|
35
|
-
|
36
|
-
// Initialize values
|
37
|
-
txt->font = font;
|
38
|
-
txt->msg = (char *) malloc(strlen(msg) + 1 * sizeof(char));
|
39
|
-
strcpy(txt->msg, msg);
|
40
|
-
txt->x = 0;
|
41
|
-
txt->y = 0;
|
42
|
-
txt->color.r = 1.f;
|
43
|
-
txt->color.g = 1.f;
|
44
|
-
txt->color.b = 1.f;
|
45
|
-
txt->color.a = 1.f;
|
46
|
-
txt->rotate = 0;
|
47
|
-
txt->rx = 0;
|
48
|
-
txt->ry = 0;
|
49
|
-
txt->texture_id = 0;
|
50
|
-
|
51
|
-
// Save the width and height of the text
|
52
|
-
TTF_SizeText(txt->font_data, txt->msg, &txt->width, &txt->height);
|
53
|
-
|
54
|
-
return txt;
|
55
|
-
}
|
56
|
-
|
57
|
-
|
58
|
-
/*
|
59
|
-
* Set the text message
|
60
|
-
*/
|
61
|
-
void S2D_SetText(S2D_Text *txt, const char *msg, ...) {
|
62
|
-
if (!txt) return;
|
63
|
-
|
64
|
-
// `msg` cannot be an empty string or NULL for TTF_SizeText
|
65
|
-
if (msg == NULL || strlen(msg) == 0) msg = " ";
|
66
|
-
|
67
|
-
// Format and store new text string
|
68
|
-
va_list args;
|
69
|
-
va_start(args, msg);
|
70
|
-
free(txt->msg);
|
71
|
-
vasprintf(&txt->msg, msg, args);
|
72
|
-
va_end(args);
|
73
|
-
|
74
|
-
// Save the width and height of the text
|
75
|
-
TTF_SizeText(txt->font_data, txt->msg, &txt->width, &txt->height);
|
76
|
-
|
77
|
-
// Delete the current texture so a new one can be generated
|
78
|
-
S2D_GL_FreeTexture(&txt->texture_id);
|
79
|
-
}
|
80
|
-
|
81
|
-
|
82
|
-
/*
|
83
|
-
* Rotate text
|
84
|
-
*/
|
85
|
-
void S2D_RotateText(S2D_Text *txt, GLfloat angle, int position) {
|
86
|
-
|
87
|
-
S2D_GL_Point p = S2D_GetRectRotationPoint(
|
88
|
-
txt->x, txt->y, txt->width, txt->height, position
|
89
|
-
);
|
90
|
-
|
91
|
-
txt->rotate = angle;
|
92
|
-
txt->rx = p.x;
|
93
|
-
txt->ry = p.y;
|
94
|
-
}
|
95
|
-
|
96
|
-
|
97
|
-
/*
|
98
|
-
* Draw text
|
99
|
-
*/
|
100
|
-
void S2D_DrawText(S2D_Text *txt) {
|
101
|
-
if (!txt) return;
|
102
|
-
|
103
|
-
if (txt->texture_id == 0) {
|
104
|
-
SDL_Color color = { 255, 255, 255 };
|
105
|
-
txt->surface = TTF_RenderText_Blended(txt->font_data, txt->msg, color);
|
106
|
-
if (!txt->surface) {
|
107
|
-
S2D_Error("TTF_RenderText_Blended", TTF_GetError());
|
108
|
-
return;
|
109
|
-
}
|
110
|
-
S2D_GL_CreateTexture(&txt->texture_id, GL_RGBA,
|
111
|
-
txt->width, txt->height,
|
112
|
-
txt->surface->pixels, GL_NEAREST);
|
113
|
-
SDL_FreeSurface(txt->surface);
|
114
|
-
}
|
115
|
-
|
116
|
-
S2D_GL_DrawText(txt);
|
117
|
-
}
|
118
|
-
|
119
|
-
|
120
|
-
/*
|
121
|
-
* Free the text
|
122
|
-
*/
|
123
|
-
void S2D_FreeText(S2D_Text *txt) {
|
124
|
-
if (!txt) return;
|
125
|
-
free(txt->msg);
|
126
|
-
S2D_GL_FreeTexture(&txt->texture_id);
|
127
|
-
TTF_CloseFont(txt->font_data);
|
128
|
-
free(txt);
|
129
|
-
}
|
Binary file
|
Binary file
|