summaryrefslogtreecommitdiff
path: root/src/TZR.h
blob: 33c6af8930ff6a41a7969d745de7ae8e5b2c3561 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#pragma once
#include <limits.h>
#include <SDL2/SDL_main.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_scancode.h>
#include <stdbool.h>
#include <stddef.h>

/* headers/TZR_types.h */

enum TZR_ResourceType {
	TZR_RES_RAW,
	TZR_RES_IMAGE,
	___TZR_RES_COUNT
};

enum TZR_EventType {
	TZR_EV_QUIT,
	TZR_EV_KEYDOWN,
	TZR_EV_KEYUP
};

enum TZR_KeyState {
	TZR_KEYSTATE_UP,
	TZR_KEYSTATE_DOWN,
	TZR_KEYSTATE_RELEASE,
	TZR_KEYSTATE_PRESS
};

typedef unsigned int TZR_Uint;
typedef struct TZR_Config TZR_Config;
typedef struct TZR_Point TZR_Point;
typedef struct TZR_Color TZR_Color;
typedef struct TZR_Rect TZR_Rect;
typedef enum TZR_ResourceType TZR_ResourceType;
typedef struct TZR_Raw TZR_Raw;
typedef struct TZR_Image TZR_Image;
typedef struct TZR_Resource TZR_Resource;
typedef enum TZR_EventType TZR_EventType;
typedef struct TZR_Event TZR_Event;
typedef struct TZR_DrawImageArgs TZR_DrawImageArgs;
typedef enum TZR_KeyState TZR_KeyState;

struct TZR_Config {
	int _;
	int width;
	int height;
	int target_fps;
	bool pixel_perfect;
	const char *title;
};

struct TZR_Point {
	int x;
	int y;
};

struct TZR_Color {
	int _;
	float r;
	float g;
	float b;
	float a;
};

struct TZR_Rect {
	TZR_Point from;
	TZR_Point to;
};

struct TZR_Raw {
	void *data;
	size_t size;
};

struct TZR_Image {
	int width;
	int height;
	SDL_Texture *ptr;
};

struct TZR_Resource {
	TZR_ResourceType type;
	char *path; /* allocated and freed by TZR; can be NULL */
	long mtime;
	union {
		TZR_Raw raw; /* raw file data */
		TZR_Image image;
	};
};

struct TZR_Event {
	TZR_EventType type;
	int button;
};

struct TZR_DrawImageArgs {
	int _;
	TZR_Uint id;
	int x;
	int y;
	int ix;
	int iy;
	int w;
	int h;
	float r;
	float sx;
	float sy;
	bool center;
};
/* headers/TZR_globals.h */

extern TZR_Config ___tzr_config;
extern TZR_Color ___tzr_color;
extern TZR_Resource *___tzr_resources;
extern size_t ___tzr_resources_capacity;
extern size_t ___tzr_resources_size;
extern SDL_Window *___tzr_window;
extern SDL_Renderer *___tzr_renderer;
extern SDL_Texture *___tzr_target;
extern unsigned long ___tzr_tick;
extern unsigned long ___tzr_next_time;
extern unsigned long ___tzr_min_dt;
extern int ___tzr_should_quit;
extern int ___tzr_mouse_x;
extern int ___tzr_mouse_y;
extern const char *___tzr_command[___TZR_RES_COUNT];
extern TZR_KeyState ___tzr_keystates[SDL_NUM_SCANCODES];
/* headers/TZR_resource.h */

#define TZR_RES TZR_LoadResource

/* Return 0 on error. */
[[nodiscard]]
TZR_Uint TZR_LoadResourceFromMemory(TZR_ResourceType type, const void *data, int size);
[[nodiscard]]
TZR_Uint TZR_LoadResourceTyped(TZR_ResourceType type, const char *path);

/* Return 0 on error; try to autodetect resource type from file extension. */
[[nodiscard]]
TZR_Uint TZR_LoadResource(const char *path);

/* Doesn't handle invalid ID. Pointer might get invalidated by resource
 * creation calls, use with caution. */
TZR_Resource *TZR_GetResourcePointer(TZR_Uint id);

/* Doesn't handle invalid ID. Pointer might get invalidated by resource
 * creation calls, use with caution. */
TZR_Raw *TZR_GetRawResource(TZR_Uint id);

/* Doesn't handle invalid ID. */
TZR_ResourceType TZR_GetResourceType(TZR_Uint id);

/* Doesn't handle invalid ID. */
const char *TZR_GetResourcePath(TZR_Uint id);

/* Doesn't handle invalid ID. */
int TZR_GetImageWidth(TZR_Uint id);

/* Doesn't handle invalid ID. */
int TZR_GetImageHeight(TZR_Uint id);

/* Reload ressource. Returns -1 on error and doesn't apply changes. */
int TZR_ReloadResource(TZR_Uint id);

/* Watch for changes on managed resources and hotreload if appropriate. */
void TZR_ResourcesWatch(void);

/* Used internaly. */
[[nodiscard]]
int TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size);
void TZR_DestroyResource(TZR_Resource *res, int free_path);
/* headers/TZR_events.h */

/* Write event data to `e`. Return 0 when event queue is empty. */
int TZR_PollEvent(TZR_Event *e);

/* Drain queued events with TZR_PollEvent and call TZR_ResourcesWatch. */
void TZR_CycleEvents(void);
/* headers/TZR_render.h */

/* All draw calls should happen between TZR_DrawBegin and TZR_DrawEnd.
 * Return -1 on error. */
[[nodiscard]]
int TZR_DrawBegin(void);

/* Return -1 on error. */
[[nodiscard]]
int TZR_DrawEnd(void);

/* Return -1 on error. */
#define TZR_DrawSetColor(...) _TZR_DrawSetColor(&(const TZR_Color){ \
	.r=-1.0f, .g=-1.0f, .b=-1.0f, .a=-1.0f, ._=0, __VA_ARGS__ })
#ifdef TZR_PARANOID
[[nodiscard]]
#endif
int _TZR_DrawSetColor(const TZR_Color *color);

/* Return -1 on error. */
#ifdef TZR_PARANOID
[[nodiscard]]
#endif
int TZR_DrawClear(void);

/* Return -1 on error. Draw point on `x`;`y` in the framebuffer. */
#ifdef TZR_PARANOID
[[nodiscard]]
#endif
int TZR_DrawPoint(int x, int y);

/* Return -1 on error. Draw line between `x0`;`y0` and `x1`;`y1` in the
 * framebuffer. */
#ifdef TZR_PARANOID
[[nodiscard]]
#endif
int TZR_DrawLine(int x0, int y0, int x1, int y1);

/* Return -1 on error. Draw rectangle at `x`;`y` position of size `w`x`h` in the
 * framebuffer. */
#ifdef TZR_PARANOID
[[nodiscard]]
#endif
int TZR_DrawRectangle(bool fill, int x, int y, int w, int h);

/* Return -1 on error. Draw texture ressource `id` at `x`;`y` position of
 * the framebuffer. */
#define TZR_DrawImage(...) _TZR_DrawImage(&(const TZR_DrawImageArgs){ \
	.x=0, .y=0, .ix=0, .iy=0, .w=INT_MIN, .h=INT_MIN, .r=0.0f, .sx=1.0f, \
	.sy=1.0f, .center=false, ._=0, __VA_ARGS__ })
#ifdef TZR_PARANOID
[[nodiscard]]
#endif
int _TZR_DrawImage(const TZR_DrawImageArgs *args);
/* headers/TZR_keystate.h */

TZR_KeyState TZR_GetKeyState(int scancode);
bool TZR_IsKeyDown(int scancode);
bool TZR_IsKeyUp(int scancode);
bool TZR_IsKeyReleased(int scancode);
bool TZR_IsKeyPressed(int scancode);
/* headers/TZR.h */

/* TZR manages all loaded resources internally and tries to avoid duplicate.
 * A resource can be loaded multiple times if asked, identification doesn't rely
 * on filepath. In doubt, use TZR_LoadResource ONCE per asset and store the ID.
 * Resources are exposed as ID instead of pointer, as a future proof measure in
 * case TZR needs to rearrange memory at runtime in the future. */

#define TZR_Init(...) _TZR_Init(&(const TZR_Config){ \
	.width=256, \
	.height=224, \
	.target_fps=60, \
	.pixel_perfect=true, \
	.title="TZR", \
	._=0, __VA_ARGS__ })

/* Should be called only once before usage of any other function unless
 * otherwise specified. On error this calls TZR_Quit and returns -1.
 * `config` will be duplicated internally and can be safely discarded. */
[[nodiscard]]
int _TZR_Init(const TZR_Config *config);

/* Destroy and free everything created by TZR. */
void TZR_Quit(void);

/* Return non-zero value when a quit event has been received. */
int TZR_ShouldQuit(void);

unsigned long TZR_GetTick(void);

/* commit hash: 8b92076145893cfdaf3e648e0324b163c1a6d28e */