ruby2d 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,138 @@
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
+ }
@@ -0,0 +1,48 @@
1
+ // input.c
2
+
3
+ #include "../include/simple2d.h"
4
+
5
+
6
+ /*
7
+ * Get the mouse coordinates relative to the viewport
8
+ */
9
+ void S2D_GetMouseOnViewport(S2D_Window *window, int wx, int wy, int *x, int *y) {
10
+
11
+ double scale; // viewport scale factor
12
+ int w, h; // width and height of scaled viewport
13
+
14
+ switch (window->viewport.mode) {
15
+
16
+ case S2D_FIXED: case S2D_EXPAND:
17
+ *x = wx / (window->orig_width / (double)window->viewport.width);
18
+ *y = wy / (window->orig_height / (double)window->viewport.height);
19
+ break;
20
+
21
+ case S2D_SCALE:
22
+ S2D_GL_GetViewportScale(window, &w, &h, &scale);
23
+ *x = wx * 1 / scale - (window->width - w) / (2.0 * scale);
24
+ *y = wy * 1 / scale - (window->height - h) / (2.0 * scale);
25
+ break;
26
+
27
+ case S2D_STRETCH:
28
+ *x = wx * window->viewport.width / (double)window->width;
29
+ *y = wy * window->viewport.height / (double)window->height;
30
+ break;
31
+ }
32
+ }
33
+
34
+
35
+ /*
36
+ * Show the cursor over the window
37
+ */
38
+ void S2D_ShowCursor() {
39
+ SDL_ShowCursor(SDL_ENABLE);
40
+ }
41
+
42
+
43
+ /*
44
+ * Hide the cursor over the window
45
+ */
46
+ void S2D_HideCursor() {
47
+ SDL_ShowCursor(SDL_DISABLE);
48
+ }
@@ -0,0 +1,114 @@
1
+ // music.c
2
+
3
+ #include "../include/simple2d.h"
4
+
5
+
6
+ /*
7
+ * Create the music
8
+ */
9
+ S2D_Music *S2D_CreateMusic(const char *path) {
10
+ S2D_Init();
11
+
12
+ // Check if music file exists
13
+ if (!S2D_FileExists(path)) {
14
+ S2D_Error("S2D_CreateMusic", "Music file `%s` not found", path);
15
+ return NULL;
16
+ }
17
+
18
+ // Allocate the music structure
19
+ S2D_Music *mus = (S2D_Music *) malloc(sizeof(S2D_Music));
20
+ if (!mus) {
21
+ S2D_Error("S2D_CreateMusic", "Out of memory!");
22
+ return NULL;
23
+ }
24
+
25
+ // Load the music data from file
26
+ mus->data = Mix_LoadMUS(path);
27
+ if (!mus->data) {
28
+ S2D_Error("Mix_LoadMUS", Mix_GetError());
29
+ free(mus);
30
+ return NULL;
31
+ }
32
+
33
+ // Initialize values
34
+ mus->path = path;
35
+
36
+ return mus;
37
+ }
38
+
39
+
40
+ /*
41
+ * Play the music
42
+ */
43
+ void S2D_PlayMusic(S2D_Music *mus, bool loop) {
44
+ if (!mus) return;
45
+
46
+ // If looping, set to -1 times; else 0
47
+ int times = loop ? -1 : 0;
48
+
49
+ // times: 0 == once, -1 == forever
50
+ if (Mix_PlayMusic(mus->data, times) == -1) {
51
+ // No music for you
52
+ S2D_Error("S2D_PlayMusic", Mix_GetError());
53
+ }
54
+ }
55
+
56
+
57
+ /*
58
+ * Pause the playing music
59
+ */
60
+ void S2D_PauseMusic() {
61
+ Mix_PauseMusic();
62
+ }
63
+
64
+
65
+ /*
66
+ * Resume the current music
67
+ */
68
+ void S2D_ResumeMusic() {
69
+ Mix_ResumeMusic();
70
+ }
71
+
72
+
73
+ /*
74
+ * Stop the playing music; interrupts fader effects
75
+ */
76
+ void S2D_StopMusic() {
77
+ Mix_HaltMusic();
78
+ }
79
+
80
+
81
+ /*
82
+ * Get the music volume
83
+ */
84
+ int S2D_GetMusicVolume() {
85
+ // Get music volume as percentage of maximum mix volume
86
+ return ceil(Mix_VolumeMusic(-1) * (100.0 / MIX_MAX_VOLUME));
87
+ }
88
+
89
+
90
+ /*
91
+ * Set the music volume a given percentage
92
+ */
93
+ void S2D_SetMusicVolume(int volume) {
94
+ // Set volume to be a percentage of the maximum mix volume
95
+ Mix_VolumeMusic((volume / 100.0) * MIX_MAX_VOLUME);
96
+ }
97
+
98
+
99
+ /*
100
+ * Fade out the playing music
101
+ */
102
+ void S2D_FadeOutMusic(int ms) {
103
+ Mix_FadeOutMusic(ms);
104
+ }
105
+
106
+
107
+ /*
108
+ * Free the music
109
+ */
110
+ void S2D_FreeMusic(S2D_Music *mus) {
111
+ if (!mus) return;
112
+ Mix_FreeMusic(mus->data);
113
+ free(mus);
114
+ }
@@ -0,0 +1,154 @@
1
+ // shapes.c
2
+
3
+ #include "../include/simple2d.h"
4
+
5
+
6
+ /*
7
+ * Rotate a point around a given point
8
+ * Params:
9
+ * p The point to rotate
10
+ * angle The angle in degrees
11
+ * rx The x coordinate to rotate around
12
+ * ry The y coordinate to rotate around
13
+ */
14
+ S2D_GL_Point S2D_RotatePoint(S2D_GL_Point p, GLfloat angle, GLfloat rx, GLfloat ry) {
15
+
16
+ // Convert from degrees to radians
17
+ angle = angle * M_PI / 180.0;
18
+
19
+ // Get the sine and cosine of the angle
20
+ GLfloat sa = sin(angle);
21
+ GLfloat ca = cos(angle);
22
+
23
+ // Translate point to origin
24
+ p.x -= rx;
25
+ p.y -= ry;
26
+
27
+ // Rotate point
28
+ GLfloat xnew = p.x * ca - p.y * sa;
29
+ GLfloat ynew = p.x * sa + p.y * ca;
30
+
31
+ // Translate point back
32
+ p.x = xnew + rx;
33
+ p.y = ynew + ry;
34
+
35
+ return p;
36
+ }
37
+
38
+
39
+ /*
40
+ * Get the point to be rotated around given a position in a rectangle
41
+ */
42
+ S2D_GL_Point S2D_GetRectRotationPoint(int x, int y, int w, int h, int position) {
43
+
44
+ S2D_GL_Point p;
45
+
46
+ switch (position) {
47
+ case S2D_CENTER:
48
+ p.x = x + (w / 2.0);
49
+ p.y = y + (h / 2.0);
50
+ break;
51
+ case S2D_TOP_LEFT:
52
+ p.x = x;
53
+ p.y = y;
54
+ break;
55
+ case S2D_TOP_RIGHT:
56
+ p.x = x + w;
57
+ p.y = y;
58
+ break;
59
+ case S2D_BOTTOM_LEFT:
60
+ p.x = x;
61
+ p.y = y + h;
62
+ break;
63
+ case S2D_BOTTOM_RIGHT:
64
+ p.x = x + w;
65
+ p.y = y + h;
66
+ break;
67
+ }
68
+
69
+ return p;
70
+ }
71
+
72
+
73
+ /*
74
+ * Draw a triangle
75
+ */
76
+ void S2D_DrawTriangle(GLfloat x1, GLfloat y1,
77
+ GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
78
+ GLfloat x2, GLfloat y2,
79
+ GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
80
+ GLfloat x3, GLfloat y3,
81
+ GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3) {
82
+
83
+ S2D_GL_DrawTriangle(x1, y1, r1, g1, b1, a1,
84
+ x2, y2, r2, g2, b2, a2,
85
+ x3, y3, r3, g3, b3, a3);
86
+ }
87
+
88
+
89
+ /*
90
+ * Draw a quad, using two triangles
91
+ */
92
+ void S2D_DrawQuad(GLfloat x1, GLfloat y1,
93
+ GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
94
+ GLfloat x2, GLfloat y2,
95
+ GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
96
+ GLfloat x3, GLfloat y3,
97
+ GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3,
98
+ GLfloat x4, GLfloat y4,
99
+ GLfloat r4, GLfloat g4, GLfloat b4, GLfloat a4) {
100
+
101
+ S2D_GL_DrawTriangle(x1, y1, r1, g1, b1, a1,
102
+ x2, y2, r2, g2, b2, a2,
103
+ x3, y3, r3, g3, b3, a3);
104
+
105
+ S2D_GL_DrawTriangle(x3, y3, r3, g3, b3, a3,
106
+ x4, y4, r4, g4, b4, a4,
107
+ x1, y1, r1, g1, b1, a1);
108
+ };
109
+
110
+
111
+ /*
112
+ * Draw a line from a quad
113
+ */
114
+ void S2D_DrawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2,
115
+ GLfloat width,
116
+ GLfloat r1, GLfloat g1, GLfloat b1, GLfloat a1,
117
+ GLfloat r2, GLfloat g2, GLfloat b2, GLfloat a2,
118
+ GLfloat r3, GLfloat g3, GLfloat b3, GLfloat a3,
119
+ GLfloat r4, GLfloat g4, GLfloat b4, GLfloat a4) {
120
+
121
+ double length = sqrt(powf(x1 - x2, 2) + powf(y1 - y2, 2));
122
+ double x = ((x2 - x1) / length) * width / 2;
123
+ double y = ((y2 - y1) / length) * width / 2;
124
+
125
+ S2D_DrawQuad(
126
+ x1 - y, y1 + x, r1, g1, b1, a1,
127
+ x1 + y, y1 - x, r2, g2, b2, a2,
128
+ x2 + y, y2 - x, r3, g3, b3, a3,
129
+ x2 - y, y2 + x, r4, g4, b4, a4
130
+ );
131
+ };
132
+
133
+
134
+ /*
135
+ * Draw a circle from triangles
136
+ */
137
+ void S2D_DrawCircle(GLfloat x, GLfloat y, GLfloat radius, int sectors,
138
+ GLfloat r, GLfloat g, GLfloat b, GLfloat a) {
139
+
140
+ double angle = 2 * M_PI / sectors;
141
+
142
+ for (int i = 0; i < sectors; i++) {
143
+
144
+ GLfloat x1 = x + radius * cos(i * angle);
145
+ GLfloat y1 = y + radius * sin(i * angle);
146
+
147
+ GLfloat x2 = x + radius * cos((i - 1) * angle);
148
+ GLfloat y2 = y + radius * sin((i - 1) * angle);
149
+
150
+ S2D_GL_DrawTriangle( x, y, r, g, b, a,
151
+ x1, y1, r, g, b, a,
152
+ x2, y2, r, g, b, a);
153
+ }
154
+ }