Gama C Library
Gama C API Documentation
debug.h
Go to the documentation of this file.
1/**
2 * @file debug.h
3 * @brief Provides macros and functions for debugging output in Gama.
4 *
5 * This file offers utilities to print the state of various Gama data structures
6 * to the console, aiding in debugging and development.
7 */
8#pragma once
9
10#include "3d/position.h"
11#include "color.h"
12#include "position.h" // For gmPos
13#include <stdio.h>
14
15/**
16 * @def gmdi(fn, obj)
17 * @brief Macro to print debug information for an object without a newline.
18 * @param fn The debug formatting function (e.g., `color`, `pos2`, `pos3`, `mesh`).
19 * @param obj The object to print.
20 */
21#define gmdi(fn, obj) gmd(fn, obj, "")
22/**
23 * @def gmdn(fn, obj)
24 * @brief Macro to print debug information for an object with a newline.
25 * @param fn The debug formatting function (e.g., `color`, `pos2`, `pos3`, `mesh`).
26 * @param obj The object to print.
27 */
28#define gmdn(fn, obj) gmd(fn, obj, "\n")
29/**
30 * @def gmd(fn, obj, e)
31 * @brief Generic macro to print debug information for an object.
32 *
33 * This macro calls a specific `gmd_` formatting function, prints the result
34 * to stdout, and appends an optional end string.
35 *
36 * @param fn The debug formatting function to use (e.g., `color`, `pos2`, `pos3`, `mesh`).
37 * @param obj The object to print debug information for.
38 * @param e An optional string to append after the debug output (e.g., "\n").
39 */
40#define gmd(fn, obj, e) \
41 do { \
42 char __gmd_str[200]; \
43 /* Use sizeof the buffer, not sizeof the number 200 */ \
44 gmd_##fn(__gmd_str, sizeof(__gmd_str), obj); \
45 printf("<%s>%s", __gmd_str, e); \
46 } while (0)
47
48/**
49 * @brief Formats a `gmColor` into a human-readable string for debugging.
50 * @param s The buffer to write the formatted string into.
51 * @param n The size of the buffer.
52 * @param c The `gmColor` to format.
53 * @return The number of characters written to the buffer.
54 */
55int gmd_color(char *s, size_t n, gmColor c) {
56 return snprintf(s, n, "gmPos(0x%#08X|r: %d, g: %d, b: %d, a: %d)", c,
57 gm_red(c), gm_green(c), gm_blue(c), gm_alpha(c));
58}
59
60/**
61 * @brief Formats a `gm3Pos` (3D position) into a human-readable string for debugging.
62 * @param s The buffer to write the formatted string into.
63 * @param n The size of the buffer.
64 * @param p The `gm3Pos` to format.
65 * @return The number of characters written to the buffer.
66 */
67int gmd_pos3(char *s, size_t n, gm3Pos p) {
68 return snprintf(s, n, "gm3Pos(x: %.lf, y: %.lf, z: %.lf)", p.x, p.y, p.z);
69}
70/**
71 * @brief Formats a `gmPos` (2D position) into a human-readable string for debugging.
72 * @param s The buffer to write the formatted string into.
73 * @param n The size of the buffer.
74 * @param p The `gmPos` to format.
75 * @return The number of characters written to the buffer.
76 */
77int gmd_pos2(char *s, size_t n, gmPos p) {
78 return snprintf(s, n, "gmPos(x: %.lf, y: %.lf)", p.x, p.y);
79}
80
81#include "3d/mesh.h" // For gm3Mesh
82
83/**
84 * @brief Formats a `gm3Mesh` into a human-readable string for debugging.
85 * @param s The buffer to write the formatted string into.
86 * @param n The size of the buffer.
87 * @param m The `gm3Mesh` to format.
88 * @return The number of characters written to the buffer.
89 */
90int gmd_mesh(char *s, size_t n, gm3Mesh m) {
91 return snprintf(
92 s, n,
93 "gm3Mesh(n_vertices: %zu, n_faces: %zu, n_normals: %zu, n_mtllibs: "
94 "%zu, ...)",
96}
#define gm_blue(col)
Extracts the blue component from a color.
Definition color.h:41
#define gm_green(col)
Extracts the green component from a color.
Definition color.h:34
#define gm_red(col)
Extracts the red component from a color.
Definition color.h:27
#define gm_alpha(col)
Extracts the alpha component from a color.
Definition color.h:48
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
int gmd_mesh(char *s, size_t n, gm3Mesh m)
Formats a gm3Mesh into a human-readable string for debugging.
Definition debug.h:90
int gmd_color(char *s, size_t n, gmColor c)
Formats a gmColor into a human-readable string for debugging.
Definition debug.h:55
int gmd_pos3(char *s, size_t n, gm3Pos p)
Formats a gm3Pos (3D position) into a human-readable string for debugging.
Definition debug.h:67
int gmd_pos2(char *s, size_t n, gmPos p)
Formats a gmPos (2D position) into a human-readable string for debugging.
Definition debug.h:77
Represents a 3D mesh composed of vertices, faces, normals, and texture coordinates.
Definition mesh.h:30
size_t n_mtllibs
Definition mesh.h:44
size_t n_normals
Definition mesh.h:38
size_t n_vertices
Definition mesh.h:32
size_t n_faces
Definition mesh.h:35
Represents a 3D position or vector.
Definition position.h:11
double y
Definition position.h:12
double z
Definition position.h:12
double x
Definition position.h:12
Represents a 2D position or vector.
Definition position.h:8
double x
Definition position.h:9
double y
Definition position.h:9