Gama C Library
Gama C API Documentation
gama.h
Go to the documentation of this file.
1/**
2 * @file gama.h
3 * @brief Core header file for the Gama engine.
4 *
5 * This file includes all the necessary headers and provides the main
6 * functions for initializing the engine, managing the game loop, and handling
7 * basic window operations.
8 */
9
10#pragma once
11
12#include "draw.h"
13#include "gapi.h"
14#include "stdio.h"
15#include "widgets/frame.h"
16
17#ifdef GM_ARGC_MAIN
18int main(int, char **);
19#else
20int main();
21#endif
22
23/**
24 * @internal
25 * @brief Main entry point for the Gama application, called by the platform runner.
26 * This function calls the user-defined main().
27 */
28int32_t
29#ifdef __ZIG_CC__
30 __attribute__((export_name("gama_run")))
31#endif
33#ifdef GM_ARGC_MAIN
34 return main(0, NULL);
35#else
36 return main();
37#endif
38}
39
40/**
41 * @brief Puts the window in fullscreen.
42 *
43 * @param fullscreen Boolean flag to enable (1) or disable (0) fullscreen.
44 */
45void gm_fullscreen(int fullscreen) { return gapi_fullscreen(fullscreen); }
46
47/**
48 * @brief Draws the Gama logo.
49 *
50 * @param x The center x position to draw the logo.
51 * @param y The center y position to draw the logo.
52 * @param s The size (side length) of the logo.
53 */
54void gm_logo(double x, double y, double s) {
55 double top_thickness = 0.15 * s;
56 double left_thickness = 0.1 * s;
57 double ratio = 0.6;
58 // top bar
59 gm_draw_rectangle(x, y + s / 2 - top_thickness / 2, s * ratio, top_thickness,
60 GM_GAMA);
61 gm_draw_rectangle(x + (-s / 2) + (left_thickness / 2) + (s / 2 * (1 - ratio)),
62 y, left_thickness, s, GM_GAMA);
63}
64
65
66/**
67 * @brief Logs a message to the platform's console.
68 * @param txt The text message to log.
69 */
70void gm_log(const char *txt) { return gapi_log(txt); }
71
72/**
73 * @brief Checks if the main game loop should continue running.
74 * @return 1 if the window is open and the game should continue, 0 otherwise.
75 * @deprecated This function is deprecated and will be removed. Use gm_yield()
76 * instead.
77 */
78static inline int gm_runs() { return gapi_runs(); }
79
80/**
81 * @brief Enables or disables the built-in FPS counter display.
82 * @param show Boolean flag to show (1) or hide (0) the FPS counter.
83 */
85void gm_show_fps(int show) { __gm_show_fps = show; }
86
87/**
88 * @brief Processes events, updates input state, and prepares for the next
89 * frame.
90 *
91 * This function should be called at the end of the main game loop. It handles
92 * window events, polls for input, updates mouse and keyboard states, and
93 * swaps the graphics buffers.
94 *
95 * @return 1 if the game should continue to the next frame, 0 if the window has
96 * been closed.
97 * @example
98 * while (gm_yield()) {
99 * // Your game logic and rendering here
100 * }
101 */
102static inline int gm_yield() {
103 static const double alpha = 2.0 / 3.0;
104 static double _fps = 0;
105 static double dt = 1;
106 static double _display_fps = 0;
107 double current_dt = gm_dt();
108 dt += current_dt;
109 double fps = 1 / current_dt;
110 if (current_dt == 0)
111 fps = _fps;
112 if (_fps == 0)
113 _fps = 60;
114 else
115 _fps = (_fps * alpha) + (fps * (1 - alpha));
116 if (dt >= 0.5) {
117 dt = 0;
118 _display_fps = _fps;
119 }
120
121 if (__gm_show_fps) {
122 char fps_text[20] = {0}; // ERROR: use fps
123 snprintf(fps_text, sizeof(fps_text), "fps: %.2f", _display_fps);
124 gmw_frame(0.9, -0.9, 0.4, 0.1);
125 gm_draw_text(0.9, -0.9, fps_text, "", 0.1, GM_WHITE);
126 }
127
128 const int ret = gapi_yield(&_gm_dt);
129 _gm_t += _gm_dt;
130 gm_mouse.lastPosition = gm_mouse.position;
131 gapi_mouse_get(&gm_mouse.position.x, &gm_mouse.position.y);
132 gm_mouse.movement.x = gm_mouse.position.x - gm_mouse.lastPosition.x;
133 gm_mouse.movement.y = gm_mouse.position.y - gm_mouse.lastPosition.y;
134 gm_mouse.down = gapi_mouse_down();
135 static int last_mouse_down = 0;
136 gm_mouse.clicked = !last_mouse_down && gm_mouse.down;
137 last_mouse_down = gm_mouse.down;
138
139 return ret;
140}
141
142/**
143 * @brief Closes the window and terminates the Gama engine.
144 */
145static inline void gm_quit() { return gapi_quit(); }
146
147/**
148 * @brief Sets the background color of the window.
149 * @param c The color to set as the background.
150 */
152
153
154/**
155 * @brief Resizes the application window.
156 * @param width The new width of the window in pixels.
157 * @param height The new height of the window in pixels.
158 */
159void gm_resize(int width, int height) { return gapi_resize(width, height); }
160
161/**
162 * @brief Initializes the Gama engine and opens a window.
163 *
164 * This must be the first Gama function called. It sets up the graphics context
165 * and creates a window with the specified dimensions and title.
166 *
167 * @param width The width of the window in pixels. Use 0 for automatic sizing.
168 * @param height The height of the window in pixels. Use 0 for automatic sizing.
169 * @param title The title of the window.
170 */
171void gm_init(int width, int height, const char *title) {
172 int code = gapi_init(width, height, title);
173 char msg[100];
174
175 if (code != 0) {
176 snprintf(msg, sizeof(msg),
177 "Error starting gama, initialization exited with non zero code %d",
178 code);
179 gapi_log(msg);
180 }
182}
183
184/**
185 * @brief Pauses execution for a specified duration.
186 *
187 * This function blocks the calling thread for approximately the given number
188 * of milliseconds.
189 *
190 * @param milliseconds The number of milliseconds to sleep.
191 */
192void gm_sleep(int milliseconds);
193
194#ifdef __ZIG_CC__
195void gm_sleep(int m) {};
196#else
197#ifdef _WIN32
198#include <windows.h>
199void gm_sleep(int milliseconds) { Sleep(milliseconds); }
200#else
201#include <unistd.h>
202void gm_sleep(int milliseconds) { usleep(milliseconds * 1000); }
203#endif
204#endif
#define GM_GAMA
The official Gama brand color.
Definition color.h:167
#define GM_BLACK
Black color.
Definition color.h:207
#define GM_WHITE
White color.
Definition color.h:892
uint32_t gmColor
Type definition for color values, stored as a 32-bit unsigned integer. The color components are packe...
Definition color.h:13
Functions for drawing shapes, text, and images.
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
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
Defines the theme and functionality for a frame widget.
int gmw_frame(double x, double y, double width, double height)
Creates and renders a frame widget (a bordered panel).
Definition frame.h:80
void gm_log(const char *txt)
Logs a message to the platform's console.
Definition gama.h:70
void gm_show_fps(int show)
Definition gama.h:85
int __gm_show_fps
Enables or disables the built-in FPS counter display.
Definition gama.h:84
void gm_resize(int width, int height)
Resizes the application window.
Definition gama.h:159
void gm_logo(double x, double y, double s)
Draws the Gama logo.
Definition gama.h:54
void gm_sleep(int milliseconds)
Pauses execution for a specified duration.
Definition gama.h:202
void gm_fullscreen(int fullscreen)
Puts the window in fullscreen.
Definition gama.h:45
void gm_background(gmColor c)
Sets the background color of the window.
Definition gama.h:151
int main()
int32_t gama_run()
Definition gama.h:32
void gm_init(int width, int height, const char *title)
Initializes the Gama engine and opens a window.
Definition gama.h:171
Graphics API (GAPI) abstraction layer for Gama.
int32_t gapi_mouse_down()
Checks if the mouse button is currently pressed.
double _gm_dt
Definition gapi.h:20
void gapi_fullscreen(const int32_t fullscreen)
Toggles fullscreen mode for the application window.
double _gm_t
Definition gapi.h:26
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.
void gapi_log(const char *message)
Logs a message to the platform's console.
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_runs()
Checks if the application is still running.
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.
struct _gmMouse gm_mouse
Definition mouse.h:32