-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspritesheet.c
More file actions
executable file
·116 lines (104 loc) · 3.12 KB
/
spritesheet.c
File metadata and controls
executable file
·116 lines (104 loc) · 3.12 KB
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
/* spritesheet.c */
#include <stdlib.h>
#include "raylib.h"
#include "spritesheet.h"
#include "card.h"
/*
"Aquarium": {X: 5, Y: 4},
"CardHand": {X: 85, Y: 4},
"Castle": {X: 165, Y: 4},
"JazzCup": {X: 245, Y: 4},
"Fishes": {X: 325, Y: 4},
"FlowerBlack": {X: 405, Y: 4},
"FlowerBlue": {X: 485, Y: 4},
"PalmBeach": {X: 5, Y: 140},
"PatternOne": {X: 85, Y: 140},
"PatternTwo": {X: 165, Y: 140},
"Robot": {X: 245, Y: 140},
"Roses": {X: 325, Y: 140},
"Shell": {X: 405, Y: 140},
*/
struct Spritesheet {
Texture2D texture;
Vector2 *coords;
Vector2 frameSize;
int framesWide;
};
struct Spritesheet* SpritesheetNew(const char * fname, float x, float y, int framesWide)
{
struct Spritesheet* self = calloc(1, sizeof(struct Spritesheet));
if ( !self ) {
return NULL;
}
self->texture = LoadTexture(fname);
self->coords = NULL;
self->frameSize.x = x;
self->frameSize.y = y;
self->framesWide = framesWide;
return self;
}
struct Spritesheet* SpritesheetNewImage(Image img, float x, float y, int framesWide)
{
struct Spritesheet* self = calloc(1, sizeof(struct Spritesheet));
if ( !self ) {
return NULL;
}
self->texture = LoadTextureFromImage(img);
self->coords = NULL;
self->frameSize.x = x;
self->frameSize.y = y;
self->framesWide = framesWide;
return self;
}
struct Spritesheet* SpritesheetNewInfo(const char* fname, float x, float y, Vector2 *coords)
{
struct Spritesheet* self = calloc(1, sizeof(struct Spritesheet));
if (!self) {
return NULL;
}
self->texture = LoadTexture(fname);
self->frameSize.x = x;
self->frameSize.y = y;
self->coords = coords;
return self;
}
void SpritesheetFree(struct Spritesheet *const self)
{
if (!self) {
return;
}
UnloadTexture(self->texture);
free(self);
}
void SpritesheetDraw(struct Spritesheet *const self, int frame, float xScale, float ang, Rectangle r)
{
Rectangle rSrc, rDst;
const float yScale = 1.0f;
const Color c = WHITE;
if (!self) {
// can happen while changing pack
return;
}
if (self->coords) {
rSrc = (Rectangle){.x=self->coords[frame].x, .y=self->coords[frame].y, .width=self->frameSize.x, .height=self->frameSize.y};
// card dimensions are already scaled
} else {
float sx = (frame % self->framesWide) * self->frameSize.x;
float sy = (frame / self->framesWide) * self->frameSize.y;
rSrc = (Rectangle){.x=sx, .y=sy, .width=self->frameSize.x, .height=self->frameSize.y};
}
float dstWidth = r.width * xScale;
float dstHeight = r.height * yScale;
float xOffset = (r.width / 2.0f) - (dstWidth / 2.0f);
rDst = (Rectangle){.x = r.x + xOffset, .y = r.y, .width = dstWidth, .height = dstHeight};
DrawTexturePro(
self->texture,
rSrc,
rDst,
(Vector2){0},//{self->origin.x * scale, self->origin.y * scale},
ang,
c
);
// Rectangle r = {.x=x, .y=y, cardWidth, cardHeight};
// DrawRectangleRounded(r, 0.1, 4, (Color){0,0,127,255});
}