-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsprites.c
222 lines (196 loc) · 5.96 KB
/
sprites.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "include/sprites.h"
#include <stdlib.h>
#include <stdio.h>
#include <tmx.h>
#include <raylib.h>
#include <string.h>
#define ANIMATION
void *raylib_tex_loader(const char *path) {
Texture2D *returnValue = malloc(sizeof(Texture2D));
*returnValue = LoadTexture(path);
return returnValue;
}
void raylib_free_tex(void *ptr) {
UnloadTexture(*((Texture2D *) ptr));
free(ptr);
}
Color int_to_color(int color) {
tmx_col_bytes res = tmx_col_to_bytes(color);
return *((Color*)&res);
}
void draw_polyline(double offset_x, double offset_y, double **points, int points_count, Color color) {
int i;
for (i=1; i<points_count; i++) {
DrawLineEx((Vector2){offset_x + points[i-1][0], offset_y + points[i-1][1]},
(Vector2){offset_x + points[i][0], offset_y + points[i][1]},
LINE_THICKNESS, color);
}
}
void draw_polygon(double offset_x, double offset_y, double **points, int points_count, Color color) {
draw_polyline(offset_x, offset_y, points, points_count, color);
if (points_count > 2) {
DrawLineEx((Vector2){offset_x + points[0][0], offset_y + points[0][1]},
(Vector2){offset_x + points[points_count-1][0], offset_y + points[points_count-1][1]},
LINE_THICKNESS, color);
}
}
void draw_objects(tmx_object_group *objgr) {
tmx_object *head = objgr->head;
Color color = int_to_color(objgr->color);
while (head) {
if (head->visible) {
if (head->obj_type == OT_SQUARE) {
DrawRectangleLinesEx((Rectangle){head->x, head->y, head->width, head->height}, LINE_THICKNESS, color);
}
else if (head->obj_type == OT_POLYGON) {
draw_polygon(head->x, head->y, head->content.shape->points, head->content.shape->points_len, color);
}
else if (head->obj_type == OT_POLYLINE) {
draw_polyline(head->x, head->y, head->content.shape->points, head->content.shape->points_len, color);
}
else if (head->obj_type == OT_ELLIPSE) {
DrawEllipseLines(head->x + head->width/2.0, head->y + head->height/2.0, head->width/2.0, head->height/2.0, color);
}
}
head = head->next;
}
}
void draw_image_layer(tmx_image *image) {
Texture2D *texture = (Texture2D*)image->resource_image;
DrawTexture(*texture, 0, 0, WHITE);
}
void draw_tile(void *image, unsigned int sx, unsigned int sy, unsigned int sw, unsigned int sh,
unsigned int dx, unsigned int dy, float opacity, unsigned int flags) {
Texture2D *texture = (Texture2D*)image;
int op = 0xFF * opacity;
Rectangle sourceRect = {sx, sy, sw, sh};
Rectangle destRect = {dx, dy, sw, sh};
Vector2 origin = {0.0, 0.0};
float rotation = 0.0;
switch (flags)
{
case TMX_FLIPPED_DIAGONALLY:
sourceRect.height *= -1;
rotation = 90.0;
break;
case TMX_FLIPPED_VERTICALLY:
sourceRect.height *= -1;
break;
case TMX_FLIPPED_VERTICALLY | TMX_FLIPPED_DIAGONALLY:
rotation = -90.0;
break;
case TMX_FLIPPED_HORIZONTALLY:
sourceRect.width *= -1;
break;
case TMX_FLIPPED_HORIZONTALLY | TMX_FLIPPED_DIAGONALLY:
rotation = 90.0;
break;
case TMX_FLIPPED_HORIZONTALLY | TMX_FLIPPED_VERTICALLY:
rotation = 180.0;
break;
case TMX_FLIPPED_HORIZONTALLY | TMX_FLIPPED_VERTICALLY | TMX_FLIPPED_DIAGONALLY:
sourceRect.width *= -1;
rotation = -90.0;
break;
default:
origin.x = 0.0;
origin.y = 0.0;
rotation = 0.0;
break;
}
if (rotation != 0.0) {
origin.x = sourceRect.width / 2.0;
origin.y = sourceRect.height / 2.0;
destRect.x += sourceRect.width / 2.0;
destRect.y += sourceRect.height / 2.0;
}
DrawTexturePro(*texture, sourceRect, destRect, origin, rotation, WHITE);
}
float actualTimeUpdate(int update){
static float timeCounter = 0;
if (update) {
if (timeCounter > 1e8) {
timeCounter = 0;
}
timeCounter += GetFrameTime();
}
return timeCounter;
}
void draw_layer(tmx_map *map, tmx_layer *layer) {
unsigned long i, j;
unsigned int gid, animLen = 0, x, y, w, h, flags;
float op;
tmx_tileset *ts;
tmx_image *im;
tmx_anim_frame *anim;
void* image;
op = layer->opacity;
tmx_tile *nextTile;
for (i=0; i<map->height; i++) {
for (j=0; j<map->width; j++) {
gid = (layer->content.gids[(i*map->width)+j]) & TMX_FLIP_BITS_REMOVAL;
nextTile = map->tiles[gid];
//printf("AnimGid before: %d\n", gid);
if (nextTile == NULL) continue;
im = nextTile->image;
anim = nextTile->animation;
animLen = nextTile->animation_len;
tmx_tileset *myTs = nextTile->tileset;
#ifdef ANIMATION
if (animLen > 0 && !im) {
/* printf("FrameTime: %f\n", GetFrameTime());
printf("My timeCounter: %f\n", actualTimeUpdate(0));
printf("My duration: %d\n", anim->duration); */
int myFrame = (int) ((actualTimeUpdate(0)*1000.0) / (anim->duration)) % (animLen);
unsigned int AnimGid = (anim[myFrame].tile_id) & TMX_FLIP_BITS_REMOVAL;
/* printf("AnimGid: %d\n", AnimGid);
printf("My animation Lenght: %d\n", animLen);
printf("My frame: %d\n", myFrame);
*/
if ((myTs->tiles) + AnimGid == NULL) {
printf("NULL tile\n");
}
else {
nextTile = (myTs->tiles) + AnimGid;
}
}
#endif
x = nextTile->ul_x;
y = nextTile->ul_y;
w = myTs->tile_width;
h = myTs->tile_height;
if (im) {
image = im->resource_image;
}
else {
image = myTs->image->resource_image;
}
flags = (layer->content.gids[(i*map->width)+j]) & ~TMX_FLIP_BITS_REMOVAL;
draw_tile(image, x, y, w, h, j*myTs->tile_width, i*myTs->tile_height, op, flags);
}
}
}
void draw_all_layers(tmx_map *map, tmx_layer *layers) {
while (layers) {
if (layers->visible) {
if (layers->type == L_GROUP) {
draw_all_layers(map, layers->content.group_head); // recursive call
}
else if (layers->type == L_OBJGR) {
draw_objects(layers->content.objgr);
}
else if (layers->type == L_IMAGE) {
draw_image_layer(layers->content.image);
}
else if (layers->type == L_LAYER) {
draw_layer(map, layers);
}
}
layers = layers->next;
}
}
void render_map(tmx_map *map) {
actualTimeUpdate(1);
ClearBackground(int_to_color(map->backgroundcolor));
draw_all_layers(map, map->ly_head);
}