-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcell.h
More file actions
188 lines (156 loc) · 5.22 KB
/
cell.h
File metadata and controls
188 lines (156 loc) · 5.22 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
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
#ifndef CELL_H
#define CELL_H
#include <raylib.h>
#include <stdlib.h>
#include <stdint.h>
#include "format.h"
#include "shape.h"
#include "gdict.h"
#define CELL_DEFAULT \
cell_new(0, 0, 0, 1, S_SQUARE)
typedef struct Cell {
int sprite_id,
script_id;
int vertical_offset,
height;
Shape shape;
} Cell;
/* Creates a new `Cell`. */
static Cell cell_new(int sprite_id, int script_id,
int vertical_offset, int height, Shape cs) {
return (Cell) {
.sprite_id = sprite_id,
.script_id = script_id,
.vertical_offset = vertical_offset,
.height = height,
.shape = cs
};
}
static void _cell_draw_square(Texture tex, Rectangle bounds, Color colour) {
DrawTexturePro(
tex,
(Rectangle) { 0, 0, tex.width, tex.height },
bounds,
(Vector2) { 0, 0 },
0,
colour
);
}
static void _cell_draw_triangle(Texture tex, Rectangle bounds, Color colour) {
// Draw black background for triangle
DrawRectanglePro(bounds, (Vector2) { 0, 0 }, 0, BLACK);
// Define by vertices
Triangle tr = triangle(
(Vector2) { bounds.x + bounds.width / 2, bounds.y },
(Vector2) { bounds.x, bounds.y + bounds.height },
(Vector2) { bounds.x + bounds.width, bounds.y + bounds.height }
);
for (int y = bounds.y; y < bounds.y + bounds.height; y++)
for (int x = bounds.x; x < bounds.x + bounds.width; x++)
if (triangle_point(tr, (Vector2) { x, y })) {
// Get the specific point in the texture
Rectangle chunk = {
.x = (x / bounds.width) * tex.width,
.y = (y / bounds.height) * tex.height,
.width = tex.width / bounds.width,
.height = tex.height / bounds.height
};
// And the point to draw to
Rectangle dest = {
.x = x,
.y = y,
.width = 1,
.height = 1
};
DrawTexturePro(
tex, chunk, dest, (Vector2) { 0, 0 }, 0, colour
);
}
}
static void _cell_draw_circle(Texture tex, Rectangle bounds, Color colour) {
// Draw black background for circle
DrawRectanglePro(bounds, (Vector2) { 0, 0 }, 0, BLACK);
// Establish the circle
Circle cl = circle(
(Vector2) { bounds.x + bounds.width / 2, bounds.y + bounds.height / 2 },
bounds.height > bounds.width
? ((float)bounds.width / 3)
: ((float)bounds.height / 3)
);
for (int y = bounds.y; y < bounds.y + bounds.height; y++)
for (int x = bounds.x; x < bounds.x + bounds.width; x++)
if (circle_point(cl, (Vector2) { x, y })) {
// Get the specific point in the texture
Rectangle chunk = {
.x = (x / bounds.width) * tex.width,
.y = (y / bounds.height) * tex.height,
.width = tex.width / bounds.width,
.height = tex.height / bounds.height
};
// And the point to draw to
Rectangle dest = {
.x = x,
.y = y,
.width = 1,
.height = 1
};
DrawTexturePro(
tex, chunk, dest, (Vector2) { 0, 0 }, 0, colour
);
}
}
/* Draws a `Cell` (2d).
* If `digits_mode`, draws cell data on top of masked texture. */
static void cell_draw(Cell cell, Rectangle bounds, bool digits_mode, Color floor_colour) {
// Get texture from global textures
Array(Texture) textures = gdict_get("textures");
if (!textures)
return;
if (array_len(textures) < cell.sprite_id)
return;
Texture tex;
if (cell.sprite_id != 0)
tex = textures[cell.sprite_id - 1];
else
tex = (Texture) { 0 };
// Colour differently if `digits_mode`
Color colour = digits_mode
? (Color) { 100, 100, 100, 255 }
: WHITE;
// Black square / tex
DrawRectanglePro(bounds, (Vector2) { 0, 0 }, 0, floor_colour);
if (tex.id != 0)
switch (cell.shape) {
case S_SQUARE:
_cell_draw_square(tex, bounds, colour);
break;
case S_TRIANGLE:
_cell_draw_triangle(tex, bounds, colour);
break;
case S_CIRCLE:
_cell_draw_circle(tex, bounds, colour);
break;
}
if (digits_mode) {
// Format sprite ID and script ID
char sprite_id[32], script_id[32];
snprintf(sprite_id, sizeof(sprite_id), "SPR = %d", cell.sprite_id);
snprintf(script_id, sizeof(script_id), "SCR = %d", cell.script_id);
int font_size = (int)(bounds.width / 6);
DrawText(
sprite_id,
bounds.x,
bounds.y,
font_size,
cell.sprite_id > 0 ? GREEN : WHITE
);
DrawText(
script_id,
bounds.x,
bounds.y + (bounds.height - font_size),
font_size,
cell.script_id > 0 ? GREEN : WHITE
);
}
}
#endif