Gama C Library
Gama C API Documentation
gapi.h
Go to the documentation of this file.
1/**
2 * @file gapi.h
3 * @brief Graphics API (GAPI) abstraction layer for Gama.
4 *
5 * This file declares the interface for platform-specific graphics operations.
6 * It provides a set of `gapi_` prefixed functions that are implemented
7 * differently for each target platform (e.g., native desktop, WebAssembly).
8 * This abstraction allows the core Gama engine code to remain platform-agnostic.
9 */
10#pragma once
11
12#include "color.h"
13#include <stdint.h>
14
15/**
16 * @internal
17 * @brief Stores the delta time (time since last frame) in seconds.
18 * This value is updated by `gapi_yield`.
19 */
20double _gm_dt = 0;
21/**
22 * @internal
23 * @brief Stores the total accumulated time since the engine started, in seconds.
24 * This value is updated by `gapi_yield`.
25 */
26double _gm_t = 0;
27
28/**
29 * @brief Retrieves the delta time (time since the last frame) in seconds.
30 * @return The delta time.
31 */
32static inline double gm_dt() { return _gm_dt; }
33/**
34 * @brief Retrieves the total accumulated time since the engine started, in seconds.
35 * @return The total elapsed time.
36 */
37static inline double gm_t() { return _gm_t; }
38
39#ifndef GM_NO_GAPI
40
41/**
42 * @brief Sets the title of the application window.
43 * @param title The null-terminated string for the new window title.
44 */
45extern void
46#ifdef __ZIG_CC__
47 __attribute__((import_module("gapi"), import_name("set_title")))
48#endif
49 gapi_set_title(const char *title);
50
51/**
52 * @brief Resizes the application window.
53 * @param width The new width of the window in pixels.
54 * @param height The new height of the window in pixels.
55 */
56extern void
57#ifdef __ZIG_CC__
58 __attribute__((import_module("gapi"), import_name("resize")))
59#endif
60 gapi_resize(const int32_t width, const int32_t height);
61
62/**
63 * @brief Sets the background color of the application window.
64 * @param background The `gmColor` to set as the background.
65 */
66extern void
67#ifdef __ZIG_CC__
68 __attribute__((import_module("gapi"), import_name("set_background_color")))
69#endif
71
72/**
73 * @brief Toggles fullscreen mode for the application window.
74 * @param fullscreen 1 to enable fullscreen, 0 to disable.
75 */
76extern void
77#ifdef __ZIG_CC__
78 __attribute__((import_module("gapi"), import_name("fullscreen")))
79#endif
80 gapi_fullscreen(const int32_t fullscreen);
81
82// --- Utils ---
83/**
84 * @brief Logs a message to the platform's console.
85 * @param message The null-terminated string to log.
86 */
87extern void
88#ifdef __ZIG_CC__
89 __attribute__((import_module("gapi"), import_name("log")))
90#endif
91 gapi_log(const char *message);
92
93// --- Game Loop ---
94/**
95 * @brief Initializes the Graphics API and the application window.
96 *
97 * This function must be called once at the start of the application.
98 *
99 * @param width The desired width of the window.
100 * @param height The desired height of the window.
101 * @param title The title of the window.
102 * @return 0 on success, non-zero on failure.
103 */
104extern int32_t
105#ifdef __ZIG_CC__
106 __attribute__((import_module("gapi"), import_name("init")))
107#endif
108 gapi_init(const int32_t width, const int32_t height, const char *title);
109
110/**
111 * @brief Yields control to the platform, processes events, and updates timing.
112 *
113 * This function should be called at the end of each frame. It handles
114 * window events, updates input states, swaps buffers, and calculates `_gm_dt`.
115 *
116 * @param dt A pointer to a double where the calculated delta time will be stored.
117 * @return 1 if the application should continue, 0 if it should exit.
118 */
119extern int32_t
120#ifdef __ZIG_CC__
121 __attribute__((import_module("gapi"), import_name("yield")))
122#endif
123 gapi_yield(double *dt); // Changed to int32_t return type
124
125/**
126 * @brief Requests the application to quit.
127 */
128extern void
129#ifdef __ZIG_CC__
130 __attribute__((import_module("gapi"), import_name("quit")))
131#endif
133
134/**
135 * @brief Checks if the application is still running.
136 * @return 1 if running, 0 if quit was requested.
137 */
138extern int32_t
139#ifdef __ZIG_CC__
140 __attribute__((import_module("gapi"), import_name("runs")))
141#endif
143
144// --- Drawing Primitives ---
145/**
146 * @brief Draws a line segment on the screen.
147 * @param x1 The x-coordinate of the start point.
148 * @param y1 The y-coordinate of the start point.
149 * @param x2 The x-coordinate of the end point.
150 * @param y2 The y-coordinate of the end point.
151 * @param thickness The thickness of the line.
152 * @param col The `gmColor` of the line.
153 * @return 0 on success.
154 */
155extern int32_t
156#ifdef __ZIG_CC__
157 __attribute__((import_module("gapi"), import_name("draw_line")))
158#endif
159 gapi_draw_line(double x1, double y1, double x2, double y2, double thickness,
160 gmColor col);
161/**
162 * @brief Draws a filled rectangle on the screen.
163 * @param x The x-coordinate of the center of the rectangle.
164 * @param y The y-coordinate of the center of the rectangle.
165 * @param w The width of the rectangle.
166 * @param h The height of the rectangle.
167 * @param col The `gmColor` of the rectangle.
168 * @return 0 on success.
169 */
170extern int32_t
171#ifdef __ZIG_CC__
172 __attribute__((import_module("gapi"), import_name("draw_rect")))
173#endif
174 gapi_draw_rect(double x, double y, double w, double h, gmColor col);
175
176/**
177 * @brief Draws a filled rectangle with rounded corners on the screen.
178 * @param x The x-coordinate of the center of the rectangle.
179 * @param y The y-coordinate of the center of the rectangle.
180 * @param w The width of the rectangle.
181 * @param h The height of the rectangle.
182 * @param r The corner radius.
183 * @param col The `gmColor` of the rectangle.
184 * @return 0 on success.
185 */
186extern int32_t
187#ifdef __ZIG_CC__
188 __attribute__((import_module("gapi"), import_name("draw_rounded_rect")))
189#endif
190 gapi_draw_rounded_rect(double x, double y, double w, double h, double r,
191 gmColor col);
192
193/**
194 * @brief Draws a filled circle on the screen.
195 * @param center_x The x-coordinate of the center of the circle.
196 * @param center_y The y-coordinate of the center of the circle.
197 * @param radius The radius of the circle.
198 * @param col The `gmColor` of the circle.
199 * @return 0 on success.
200 */
201extern int32_t
202#ifdef __ZIG_CC__
203 __attribute__((import_module("gapi"), import_name("draw_circle")))
204#endif
205 gapi_draw_circle(double center_x, double center_y, double radius,
206 gmColor col);
207
208/**
209 * @brief Draws a filled ellipse on the screen.
210 * @param x The x-coordinate of the center of the ellipse.
211 * @param y The y-coordinate of the center of the ellipse.
212 * @param w The width of the ellipse.
213 * @param h The height of the ellipse.
214 * @param col The `gmColor` of the ellipse.
215 * @return 0 on success.
216 */
217extern int32_t
218#ifdef __ZIG_CC__
219 __attribute__((import_module("gapi"), import_name("draw_ellipse")))
220#endif
221 gapi_draw_ellipse(double x, double y, double w, double h, gmColor col);
222
223/**
224 * @brief Draws a filled triangle on the screen.
225 * @param x1 The x-coordinate of the first vertex.
226 * @param y1 The y-coordinate of the first vertex.
227 * @param x2 The x-coordinate of the second vertex.
228 * @param y2 The y-coordinate of the second vertex.
229 * @param x3 The x-coordinate of the third vertex.
230 * @param y3 The y-coordinate of the third vertex.
231 * @param col The `gmColor` of the triangle.
232 * @return 0 on success.
233 */
234extern int32_t
235#ifdef __ZIG_CC__
236 __attribute__((import_module("gapi"), import_name("draw_triangle")))
237#endif
238 gapi_draw_triangle(double x1, double y1, double x2, double y2, double x3,
239 double y3, gmColor col);
240
241/**
242 * @brief Draws a batch of triangles on the screen.
243 * @param n_triangles The number of triangles in the batch.
244 * @param points An array of `double`s representing the vertices (x1,y1, x2,y2, x3,y3 for each triangle).
245 * @param colors An array of `gmColor`s, one for each triangle.
246 * @return 0 on success.
247 */
248extern int32_t
249#ifdef __ZIG_CC__
250 __attribute__((import_module("gapi"), import_name("draw_triangles")))
251#endif
252 gapi_draw_triangles(uint32_t n_triangles, double *points, gmColor *colors);
253
254/**
255 * @brief Creates a platform-specific image handle from raw pixel data.
256 *
257 * This function is used to upload image data to the GPU or create a drawable
258 * surface that can be used by `gapi_draw_image` functions.
259 *
260 * @param data A pointer to the raw RGBA pixel data.
261 * @param width The width of the image.
262 * @param height The height of the image.
263 * @return A unique handle (ID) for the created image on success, 0 on failure.
264 */
265extern uint32_t
266#ifdef __ZIG_CC__
267 __attribute__((import_module("gapi"), import_name("create_image")))
268#endif
269 gapi_create_image(const unsigned char *data, uint32_t width,
270 uint32_t height);
271
272/**
273 * @brief Draws an image referenced by its handle on the screen.
274 * @param handle The handle of the image to draw, obtained from `gapi_create_image`.
275 * @param x The x-coordinate of the center of the image.
276 * @param y The y-coordinate of the center of the image.
277 * @param width The width to draw the image.
278 * @param height The height to draw the image.
279 * @return 0 on success.
280 */
281extern int32_t
282#ifdef __ZIG_CC__
283 __attribute__((import_module("gapi"), import_name("draw_image")))
284#endif
285 gapi_draw_image(uint32_t handle, double x, double y, double width,
286 double height);
287/**
288 * @brief Draws a part of an image referenced by its handle on the screen.
289 * @param handle The handle of the image to draw from.
290 * @param slice_x The x-coordinate of the top-left corner of the source slice.
291 * @param slice_y The y-coordinate of the top-left corner of the source slice.
292 * @param slice_width The width of the source slice.
293 * @param slice_height The height of the source slice.
294 * @param x The x-coordinate of the center of the destination rectangle.
295 * @param y The y-coordinate of the center of the destination rectangle.
296 * @param width The width to draw the slice.
297 * @param height The height to draw the slice.
298 * @return 0 on success.
299 */
300extern int32_t
301#ifdef __ZIG_CC__
302 __attribute__((import_module("gapi"), import_name("draw_image_part")))
303#endif
304 gapi_draw_image_part(uint32_t handle, uint32_t slice_x, uint32_t slice_y,
305 uint32_t slice_width, uint32_t slice_height, double x,
306 double y, double width, double height);
307
308// --- Text Functions ---
309/**
310 * @brief Draws text on the screen.
311 * @param x The x-coordinate of the center of the text.
312 * @param y The y-coordinate of the center of the text.
313 * @param height The height/size of the text.
314 * @param txt The null-terminated string to draw.
315 * @param font The null-terminated font name to use.
316 * @param style A bitmask for text style (e.g., bold, italic).
317 * @param col The `gmColor` of the text.
318 * @return 0 on success.
319 */
320extern int32_t
321#ifdef __ZIG_CC__
322 __attribute__((import_module("gapi"), import_name("draw_text")))
323#endif
324 gapi_draw_text(double x, double y, double height, const char *txt,
325 const char *font, uint8_t style, gmColor col);
326
327// --- Event Functions ---
328/**
329 * @brief Checks if a specific key is currently pressed.
330 * @param t The type of key ('c' for character, 'a' for arrow, 's' for special).
331 * @param k The key code.
332 * @return 1 if the key is down, 0 otherwise.
333 */
334extern int32_t
335#ifdef __ZIG_CC__
336 __attribute__((import_module("gapi"), import_name("key_down")))
337#endif
338 gapi_key_down(char t, char k);
339
340/**
341 * @brief Checks if the mouse button is currently pressed.
342 * @return 1 if the mouse button is down, 0 otherwise.
343 */
344extern int32_t
345#ifdef __ZIG_CC__
346 __attribute__((import_module("gapi"), import_name("mouse_down")))
347#endif
349
350/**
351 * @brief Retrieves the current mouse cursor position.
352 * @param x A pointer to a double to store the x-coordinate.
353 * @param y A pointer to a double to store the y-coordinate.
354 * @return 0 on success.
355 */
356extern int32_t
357#ifdef __ZIG_CC__
358 __attribute__((import_module("gapi"), import_name("mouse_get")))
359#endif
360 gapi_mouse_get(double *x, double *y);
361
362#endif
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
void gapi_set_title(const char *title)
Sets the title of the application window.
int32_t gapi_mouse_down()
Checks if the mouse button is currently pressed.
double _gm_dt
Definition gapi.h:20
int32_t gapi_draw_rect(double x, double y, double w, double h, gmColor col)
Draws a filled rectangle on the screen.
int32_t gapi_draw_triangles(uint32_t n_triangles, double *points, gmColor *colors)
Draws a batch of triangles on the screen.
void gapi_fullscreen(const int32_t fullscreen)
Toggles fullscreen mode for the application window.
int32_t gapi_draw_rounded_rect(double x, double y, double w, double h, double r, gmColor col)
Draws a filled rectangle with rounded corners on the screen.
double _gm_t
Definition gapi.h:26
int32_t gapi_draw_image(uint32_t handle, double x, double y, double width, double height)
Draws an image referenced by its handle on the screen.
int32_t gapi_init(const int32_t width, const int32_t height, const char *title)
Initializes the Graphics API and the application window.
void gapi_resize(const int32_t width, const int32_t height)
Resizes the application window.
int32_t gapi_key_down(char t, char k)
Checks if a specific key is currently pressed.
int32_t gapi_draw_triangle(double x1, double y1, double x2, double y2, double x3, double y3, gmColor col)
Draws a filled triangle on the screen.
void gapi_log(const char *message)
Logs a message to the platform's console.
uint32_t gapi_create_image(const unsigned char *data, uint32_t width, uint32_t height)
Creates a platform-specific image handle from raw pixel data.
void gapi_quit()
Requests the application to quit.
int32_t gapi_yield(double *dt)
Yields control to the platform, processes events, and updates timing.
int32_t gapi_draw_text(double x, double y, double height, const char *txt, const char *font, uint8_t style, gmColor col)
Draws text on the screen.
int32_t gapi_runs()
Checks if the application is still running.
int32_t gapi_draw_circle(double center_x, double center_y, double radius, gmColor col)
Draws a filled circle on the screen.
int32_t gapi_draw_image_part(uint32_t handle, uint32_t slice_x, uint32_t slice_y, uint32_t slice_width, uint32_t slice_height, double x, double y, double width, double height)
Draws a part of an image referenced by its handle on the screen.
int32_t gapi_draw_ellipse(double x, double y, double w, double h, gmColor col)
Draws a filled ellipse on the screen.
int32_t gapi_draw_line(double x1, double y1, double x2, double y2, double thickness, gmColor col)
Draws a line segment on the screen.
int32_t gapi_mouse_get(double *x, double *y)
Retrieves the current mouse cursor position.
void gapi_set_background_color(const gmColor background)
Sets the background color of the application window.