Gama C Library
Gama C API Documentation
draw.h
Go to the documentation of this file.
1/**
2 * @file draw.h
3 * @brief Functions for drawing shapes, text, and images.
4 *
5 * This file provides a set of functions for immediate-mode rendering of
6 * various primitives, as well as helper functions to draw physics bodies
7 * (`gmBody`). All coordinates are in world space.
8 */
9
10#pragma once
11
12#include "body.h"
13#include "color.h"
14#include "gapi.h"
15#include "image.h" // For gmImage
16#include <stdint.h>
17
18// ---------------------------------------------------------------------------
19// ------------------------- Immediate-Mode Primitives -----------------------
20// ---------------------------------------------------------------------------
21
22/**
23 * @internal
24 * @brief Toggles a cache state. Currently unused.
25 */
26int gm_cache(unsigned int id) {
27 (void)id; // Parameter is unused for now.
28 static int on = 0;
29 on = !on;
30 return on;
31}
32
33/**
34 * @brief Draws a line segment.
35 * @param x1 The x-coordinate of the starting point.
36 * @param y1 The y-coordinate of the starting point.
37 * @param x2 The x-coordinate of the ending point.
38 * @param y2 The y-coordinate of the ending point.
39 * @param thickness The thickness of the line in pixels.
40 * @param c The color of the line.
41 * @return An identifier for the drawing command.
42 */
43int32_t gm_draw_line(double x1, double y1, double x2, double y2,
44 double thickness, gmColor c) {
45 return gapi_draw_line(x1, y1, x2, y2, thickness, c);
46}
47
48/**
49 * @brief Draws a rectangle centered at a point.
50 * @param x The x-coordinate of the center of the rectangle.
51 * @param y The y-coordinate of the center of the rectangle.
52 * @param w The width of the rectangle.
53 * @param h The height of the rectangle.
54 * @param c The color of the rectangle.
55 * @return An identifier for the drawing command.
56 */
57int32_t gm_draw_rectangle(double x, double y, double w, double h, gmColor c) {
58 return gapi_draw_rect(x, y, w, h, c);
59}
60
61/**
62 * @brief Draws a rectangle with rounded corners centered at a point.
63 * @param x The x-coordinate of the center of the rectangle.
64 * @param y The y-coordinate of the center of the rectangle.
65 * @param w The width of the rectangle.
66 * @param h The height of the rectangle.
67 * @param r The corner radius.
68 * @param c The color of the rectangle.
69 * @return An identifier for the drawing command.
70 */
71int32_t gm_draw_rounded_rectangle(double x, double y, double w, double h,
72 double r, gmColor c) {
73 return gapi_draw_rounded_rect(x, y, w, h, r, c);
74}
75
76/**
77 * @brief Draws a circle.
78 * @param center_x The x-coordinate of the center of the circle.
79 * @param center_y The y-coordinate of the center of the circle.
80 * @param radius The radius of the circle.
81 * @param c The color of the circle.
82 * @return An identifier for the drawing command.
83 */
84int32_t gm_draw_circle(double center_x, double center_y, double radius,
85 gmColor c) {
86 return gapi_draw_circle(center_x, center_y, radius, c);
87}
88
89/**
90 * @brief Draws an ellipse centered at a point.
91 * @param x The x-coordinate of the center of the ellipse.
92 * @param y The y-coordinate of the center of the ellipse.
93 * @param w The total width of the ellipse.
94 * @param h The total height of the ellipse.
95 * @param c The color of the ellipse.
96 * @return An identifier for the drawing command.
97 */
98int32_t gm_draw_ellipse(double x, double y, double w, double h, gmColor c) {
99 return gapi_draw_ellipse(x, y, w, h, c);
100}
101
102/**
103 * @brief Draws a triangle.
104 * @param x1 The x-coordinate of the first vertex.
105 * @param y1 The y-coordinate of the first vertex.
106 * @param x2 The x-coordinate of the second vertex.
107 * @param y2 The y-coordinate of the second vertex.
108 * @param x3 The x-coordinate of the third vertex.
109 * @param y3 The y-coordinate of the third vertex.
110 * @param c The color of the triangle.
111 * @return An identifier for the drawing command.
112 */
113int32_t gm_draw_triangle(double x1, double y1, double x2, double y2, double x3,
114 double y3, gmColor c) {
115 return gapi_draw_triangle(x1, y1, x2, y2, x3, y3, c);
116}
117
118/**
119 * @brief Draws an image centered at a point.
120 * @param img The image to draw.
121 * @param x The x-coordinate of the center of the image.
122 * @param y The y-coordinate of the center of the image.
123 * @param w The width to draw the image.
124 * @param h The height to draw the image.
125 * @return An identifier for the drawing command.
126 */
127int32_t gm_draw_image(gmImage img, double x, double y, double w, double h) {
128 return gapi_draw_image(img.handle, x, y, w, h);
129}
130/**
131 * @brief Draws text centered at a point.
132 * @param x The x-coordinate for the center of the text.
133 * @param y The y-coordinate for the center of the text.
134 * @param text The null-terminated string to draw.
135 * @param font The null-terminated font name to use (can be empty for default).
136 * @param font_size The size of the font.
137 * @param c The color of the text.
138 * @return An identifier for the drawing command.
139 */
140int32_t gm_draw_text(double x, double y, const char *text, const char *font,
141 double font_size, gmColor c) {
142 return gapi_draw_text(x, y, font_size, text, font, 0, c);
143}
144
145// ---------------------------------------------------------------------------
146// ------------------------- Object-Based Helpers ----------------------------
147// ---------------------------------------------------------------------------
148
149/**
150 * @brief Draws a physics body based on its collider type.
151 *
152 * This function checks the body's collider type and calls the appropriate
153 * drawing function (e.g., gm_draw_rectangle for GM_COLLIDER_RECT).
154 *
155 * @param body A pointer to the body to draw.
156 * @param c The color to draw the body.
157 */
158void gm_draw_body(const gmBody *body, gmColor c) {
159 if (body == NULL || !body->is_active) {
160 return;
161 }
162 switch (body->collider_type) {
163 case GM_COLLIDER_RECT:
164 gm_draw_rectangle(body->position.x, body->position.y, body->width,
165 body->height, c);
166 break;
168 gm_draw_circle(body->position.x, body->position.y, body->radius, c);
169 break;
170 }
171}
172
173/**
174 * @brief Draws a rectangular physics body.
175 * @param body A pointer to the body to draw.
176 * @param c The color to draw the body.
177 */
178void gm_draw_rect_body(const gmBody *body, gmColor c) {
179 gm_draw_rectangle(body->position.x, body->position.y, body->width,
180 body->height, c);
181}
182
183/**
184 * @brief Draws an array of rectangular physics bodies.
185 * @param bodies A pointer to the array of bodies.
186 * @param number The number of bodies in the array.
187 * @param c The color to draw the bodies.
188 */
189void gm_draw_rect_bodies(const gmBody *bodies, size_t number, gmColor c) {
190 for (size_t i = 0; i < number; i++)
191 gm_draw_rect_body(&bodies[i], c);
192}
193
194/**
195 * @brief Draws a rectangular physics body with rounded corners.
196 * @param body A pointer to the body to draw.
197 * @param radius The corner radius.
198 * @param c The color to draw the body.
199 */
200void gm_draw_round_rect_body(const gmBody *body, double radius, gmColor c) {
202 body->height, radius, c);
203}
204
205/**
206 * @brief Draws a circular physics body.
207 *
208 * If the body is not a circle collider, it approximates the radius.
209 *
210 * @param body A pointer to the body to draw.
211 * @param c The color to draw the body.
212 */
213void gm_draw_circle_body(const gmBody *body, gmColor c) {
214 double radius = body->collider_type == GM_COLLIDER_CIRCLE
215 ? body->radius
216 : (body->width + body->height) / 4.0;
217 gm_draw_circle(body->position.x, body->position.y, radius, c);
218}
219
220/**
221 * @brief Draws an array of circular physics bodies.
222 * @param bodies A pointer to the array of bodies.
223 * @param number The number of bodies in the array.
224 * @param c The color to draw the bodies.
225 */
226void gm_draw_circle_bodies(const gmBody *bodies, size_t number, gmColor c) {
227 for (size_t i = 0; i < number; i++)
228 gm_draw_circle_body(&bodies[i], c);
229}
230
231/**
232 * @brief Draws an elliptical physics body.
233 * @param body A pointer to the body to draw.
234 * @param c The color to draw the body.
235 */
236void gm_draw_ellipse_body(const gmBody *body, gmColor c) {
237 if (body == NULL || !body->is_active)
238 return;
239 gm_draw_ellipse(body->position.x, body->position.y, body->width, body->height,
240 c);
241}
242
243/**
244 * @brief Draws an array of elliptical physics bodies.
245 * @param bodies A pointer to the array of bodies.
246 * @param number The number of bodies in the array.
247 * @param c The color to draw the bodies.
248 */
249void gm_draw_ellipse_bodies(const gmBody *bodies, size_t number, gmColor c) {
250 for (size_t i = 0; i < number; i++)
251 gm_draw_ellipse_body(&bodies[i], c);
252}
253
254/**
255 * @brief Draws a triangular physics body.
256 *
257 * The body's position is the first vertex. The other two vertices are
258 * specified as offsets from the first.
259 *
260 * @param body A pointer to the body to draw.
261 * @param x2_offset The x-offset of the second vertex.
262 * @param y2_offset The y-offset of the second vertex.
263 * @param x3_offset The x-offset of the third vertex.
264 * @param y3_offset The y-offset of the third vertex.
265 * @param c The color to draw the body.
266 */
267void gm_draw_triangle_body(const gmBody *body, double x2_offset,
268 double y2_offset, double x3_offset, double y3_offset,
269 gmColor c) {
270 if (body == NULL || !body->is_active)
271 return;
272 gm_draw_triangle(body->position.x, body->position.y,
273 body->position.x + x2_offset, body->position.y + y2_offset,
274 body->position.x + x3_offset, body->position.y + y3_offset,
275 c);
276}
277
278/**
279 * @brief Draws an array of triangular physics bodies.
280 * @param bodies A pointer to the array of bodies.
281 * @param number The number of bodies in the array.
282 * @param x2_offset The x-offset of the second vertex for all triangles.
283 * @param y2_offset The y-offset of the second vertex for all triangles.
284 * @param x3_offset The x-offset of the third vertex for all triangles.
285 * @param y3_offset The y-offset of the third vertex for all triangles.
286 * @param c The color to draw the bodies.
287 */
288void gm_draw_triangle_bodies(const gmBody *bodies, size_t number,
289 double x2_offset, double y2_offset,
290 double x3_offset, double y3_offset, gmColor c) {
291 for (size_t i = 0; i < number; i++)
292 gm_draw_triangle_body(&bodies[i], x2_offset, y2_offset, x3_offset,
293 y3_offset, c);
294}
295
296/**
297 * @brief Draws an image at a physics body's position.
298 * @param body A pointer to the body.
299 * @param img The image to draw.
300 */
301void gm_draw_image_body(const gmBody *body, gmImage img) {
302 if (body == NULL || !body->is_active)
303 return;
304 gm_draw_image(img, body->position.x, body->position.y, body->width,
305 body->height);
306}
307
308/**
309 * @brief Draws the same image for an array of physics bodies.
310 * @param bodies A pointer to the array of bodies.
311 * @param number The number of bodies in the array.
312 * @param img The image to draw.
313 */
314void gm_draw_image_bodies(const gmBody *bodies, size_t number, gmImage img) {
315 for (size_t i = 0; i < number; i++)
316 gm_draw_image_body(&bodies[i], img);
317}
318
319/**
320 * @brief Draws text at a physics body's position.
321 * @param body A pointer to the body.
322 * @param text The null-terminated string to draw.
323 * @param font_size The size of the font.
324 * @param c The color of the text.
325 */
326void gm_draw_text_body(const gmBody *body, const char *text, const char *font,
327 double font_size, gmColor c) {
328 if (body == NULL || !body->is_active)
329 return;
330 gm_draw_text(body->position.x, body->position.y, text, font, font_size, c);
331}
332
333/**
334 * @brief Draws the same text for an array of physics bodies.
335 * @param bodies A pointer to the array of bodies.
336 * @param number The number of bodies in the array.
337 * @param text The null-terminated string to draw.
338 * @param font_size The size of the font.
339 * @param c The color of the text.
340 */
341void gm_draw_text_bodies(const gmBody *bodies, size_t number, const char *text,
342 const char *font, double font_size, gmColor c) {
343 for (size_t i = 0; i < number; i++)
344 gm_draw_text_body(&bodies[i], text, font, font_size, c);
345}
@ GM_COLLIDER_RECT
Definition body.h:18
@ GM_COLLIDER_CIRCLE
Definition body.h:17
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
int32_t gm_draw_circle(double center_x, double center_y, double radius, gmColor c)
Draws a circle.
Definition draw.h:84
void gm_draw_triangle_bodies(const gmBody *bodies, size_t number, double x2_offset, double y2_offset, double x3_offset, double y3_offset, gmColor c)
Draws an array of triangular physics bodies.
Definition draw.h:288
void gm_draw_circle_bodies(const gmBody *bodies, size_t number, gmColor c)
Draws an array of circular physics bodies.
Definition draw.h:226
int32_t gm_draw_text(double x, double y, const char *text, const char *font, double font_size, gmColor c)
Draws text centered at a point.
Definition draw.h:140
void gm_draw_rect_body(const gmBody *body, gmColor c)
Draws a rectangular physics body.
Definition draw.h:178
int32_t gm_draw_ellipse(double x, double y, double w, double h, gmColor c)
Draws an ellipse centered at a point.
Definition draw.h:98
int32_t gm_draw_rectangle(double x, double y, double w, double h, gmColor c)
Draws a rectangle centered at a point.
Definition draw.h:57
void gm_draw_ellipse_bodies(const gmBody *bodies, size_t number, gmColor c)
Draws an array of elliptical physics bodies.
Definition draw.h:249
void gm_draw_image_bodies(const gmBody *bodies, size_t number, gmImage img)
Draws the same image for an array of physics bodies.
Definition draw.h:314
void gm_draw_image_body(const gmBody *body, gmImage img)
Draws an image at a physics body's position.
Definition draw.h:301
int32_t gm_draw_line(double x1, double y1, double x2, double y2, double thickness, gmColor c)
Draws a line segment.
Definition draw.h:43
void gm_draw_round_rect_body(const gmBody *body, double radius, gmColor c)
Draws a rectangular physics body with rounded corners.
Definition draw.h:200
void gm_draw_text_body(const gmBody *body, const char *text, const char *font, double font_size, gmColor c)
Draws text at a physics body's position.
Definition draw.h:326
int32_t gm_draw_triangle(double x1, double y1, double x2, double y2, double x3, double y3, gmColor c)
Draws a triangle.
Definition draw.h:113
void gm_draw_ellipse_body(const gmBody *body, gmColor c)
Draws an elliptical physics body.
Definition draw.h:236
void gm_draw_triangle_body(const gmBody *body, double x2_offset, double y2_offset, double x3_offset, double y3_offset, gmColor c)
Draws a triangular physics body.
Definition draw.h:267
void gm_draw_text_bodies(const gmBody *bodies, size_t number, const char *text, const char *font, double font_size, gmColor c)
Draws the same text for an array of physics bodies.
Definition draw.h:341
int32_t gm_draw_rounded_rectangle(double x, double y, double w, double h, double r, gmColor c)
Draws a rectangle with rounded corners centered at a point.
Definition draw.h:71
void gm_draw_rect_bodies(const gmBody *bodies, size_t number, gmColor c)
Draws an array of rectangular physics bodies.
Definition draw.h:189
int gm_cache(unsigned int id)
Definition draw.h:26
int32_t gm_draw_image(gmImage img, double x, double y, double w, double h)
Draws an image centered at a point.
Definition draw.h:127
void gm_draw_circle_body(const gmBody *body, gmColor c)
Draws a circular physics body.
Definition draw.h:213
void gm_draw_body(const gmBody *body, gmColor c)
Draws a physics body based on its collider type.
Definition draw.h:158
Graphics API (GAPI) abstraction layer for Gama.
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_rounded_rect(double x, double y, double w, double h, double r, gmColor col)
Draws a filled rectangle with rounded corners on the screen.
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_draw_triangle(double x1, double y1, double x2, double y2, double x3, double y3, gmColor col)
Draws a filled triangle on the screen.
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_draw_circle(double center_x, double center_y, double radius, gmColor col)
Draws a filled circle 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.
Structure representing a physics body with properties for collision and movement.
Definition body.h:25
double width
Definition body.h:35
gmColliderType collider_type
Definition body.h:30
double height
Definition body.h:35
uint8_t is_active
Definition body.h:27
double radius
Definition body.h:35
gmPos position
Definition body.h:31
A handle to a GPU-managed image or texture.
Definition image.h:97
uint32_t handle
Definition image.h:98
double x
Definition position.h:9
double y
Definition position.h:9