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