-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.c
executable file
·163 lines (135 loc) · 3.45 KB
/
sprite.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
/*
Pego de https://github.com/batiste/sdl2-game-loop/blob/master/sprite.c
Copyright (C) 2013 [email protected]
Sprite functions
*/
#include "common.c"
struct Sprite
{
SDL_Texture *texture;
struct SDL_Rect source;
struct SDL_Rect destination;
};
typedef struct Sprite Sprite;
struct SpriteTable
{
// a table of pointer of sprite
Sprite **table;
int length;
};
typedef struct SpriteTable SpriteTable;
struct Animation
{
// a table of sprites used for animation
SpriteTable *sprites;
// Uint32 startTick;
// Uint32 currentTick;
int ticksByFrame;
int duration;
};
typedef struct Animation Animation;
struct AnimationInstance
{
Animation *animation;
int startFrame;
int currentTick;
};
Sprite *createSprite(SDL_Texture *texture, int w, int h)
{
Sprite *sp = (Sprite *)malloc(sizeof(Sprite));
sp->texture = texture;
sp->source.x = 0;
sp->source.y = 0;
sp->source.w = w;
sp->source.h = h;
sp->destination.x = 0;
sp->destination.y = 0;
sp->destination.w = w;
sp->destination.h = h;
return sp;
}
Animation *createAnimation(SDL_Texture *texture, SDL_Rect *sprites_start,
int ticksByFrame, int numberSprite)
{
Sprite *sprite;
int j, w, h;
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
int columns = (w - sprites_start->x) / sprites_start->w;
if (numberSprite != 0)
{
columns = MIN(numberSprite, columns);
}
Sprite **table = (Sprite **)malloc(columns * sizeof(Sprite *));
for (j = 0; j < columns; j++)
{
sprite =
createSprite(texture, sprites_start->w, sprites_start->h);
sprite->source.x = j * sprites_start->w + sprites_start->x;
sprite->source.y = sprites_start->y;
table[j] = sprite;
}
SpriteTable *spritetable = (SpriteTable *)malloc(sizeof(SpriteTable));
spritetable->table = table;
spritetable->length = columns;
Animation *anim = (Animation *)malloc(sizeof(Animation));
anim->sprites = spritetable;
anim->ticksByFrame = ticksByFrame;
anim->duration = ticksByFrame * numberSprite;
// anim->startTick = 0;
// anim->currentTick = 0;
return anim;
}
void destroyAnimation(Animation *anim)
{
free(anim->sprites->table);
free(anim->sprites);
free(anim);
}
Sprite *getSpriteFromAnimation(Animation *anim, int frame)
{
int spriteIndex = (frame / anim->ticksByFrame) % anim->sprites->length;
return anim->sprites->table[spriteIndex];
}
void drawSprite(SDL_Renderer *renderer, Sprite *sp)
{
SDL_RenderCopy(renderer, sp->texture, &sp->source, &sp->destination);
}
void drawSpriteAt(SDL_Renderer *renderer, Sprite *sp, int x, int y)
{
SDL_Rect _rect;
_rect.x = x;
_rect.y = y;
_rect.w = sp->destination.w;
_rect.h = sp->destination.h;
SDL_RenderCopy(renderer, sp->texture, &sp->source, &_rect);
}
/* TMX
SpriteTable *splitTextureTable(SDL_Texture *texture, int w, int h)
{
Sprite *sprite;
int i, j, tex_w, tex_h;
SDL_QueryTexture(texture, NULL, NULL, &tex_w, &tex_h);
int columns = tex_w / w;
int lines = tex_h / h;
Sprite **table = (Sprite **)malloc(columns * lines * sizeof(Sprite *));
printf("Texture has lines %d and columns %d\n", lines, columns);
for (i = 0; i < lines; i++)
{
for (j = 0; j < columns; j++)
{
sprite = createSprite(texture, w, h);
sprite->source.x = j * w;
sprite->source.y = i * h;
table[i * columns + j] = sprite;
}
}
SpriteTable *spritetable = (SpriteTable *)malloc(sizeof(SpriteTable));
spritetable->table = table;
spritetable->length = lines * columns;
return spritetable;
}*/
void destroySpriteTable(SpriteTable *table)
{
free(table->table);
free(table);
}