Gama C Library
Gama C API Documentation
/home/engon/gama/gama/lib/gama/gama.h

Processes events, updates input state, and prepares for the next frame.

Processes events, updates input state, and prepares for the next frame.This function should be called at the end of the main game loop. It handles window events, polls for input, updates mouse and keyboard states, and swaps the graphics buffers.

Returns
1 if the game should continue to the next frame, 0 if the window has been closed.

while (gm_yield()) { // Your game logic and rendering here }

/**
* @file gama.h
* @brief Core header file for the Gama engine.
*
* This file includes all the necessary headers and provides the main
* functions for initializing the engine, managing the game loop, and handling
* basic window operations.
*/
#pragma once
#include "draw.h"
#include "gapi.h"
#include "stdio.h"
#include "widgets/frame.h"
#ifdef GM_ARGC_MAIN
int main(int, char **);
#else
int main();
#endif
/**
* @internal
* @brief Main entry point for the Gama application, called by the platform runner.
* This function calls the user-defined main().
*/
int32_t
#ifdef __ZIG_CC__
__attribute__((export_name("gama_run")))
#endif
#ifdef GM_ARGC_MAIN
return main(0, NULL);
#else
return main();
#endif
}
/**
* @brief Puts the window in fullscreen.
*
* @param fullscreen Boolean flag to enable (1) or disable (0) fullscreen.
*/
void gm_fullscreen(int fullscreen) { return gapi_fullscreen(fullscreen); }
/**
* @brief Draws the Gama logo.
*
* @param x The center x position to draw the logo.
* @param y The center y position to draw the logo.
* @param s The size (side length) of the logo.
*/
void gm_logo(double x, double y, double s) {
double top_thickness = 0.15 * s;
double left_thickness = 0.1 * s;
double ratio = 0.6;
// top bar
gm_draw_rectangle(x, y + s / 2 - top_thickness / 2, s * ratio, top_thickness,
gm_draw_rectangle(x + (-s / 2) + (left_thickness / 2) + (s / 2 * (1 - ratio)),
y, left_thickness, s, GM_GAMA);
}
/**
* @brief Logs a message to the platform's console.
* @param txt The text message to log.
*/
void gm_log(const char *txt) { return gapi_log(txt); }
/**
* @brief Checks if the main game loop should continue running.
* @return 1 if the window is open and the game should continue, 0 otherwise.
* @deprecated This function is deprecated and will be removed. Use gm_yield()
* instead.
*/
static inline int gm_runs() { return gapi_runs(); }
/**
* @brief Enables or disables the built-in FPS counter display.
* @param show Boolean flag to show (1) or hide (0) the FPS counter.
*/
int __gm_show_fps = 0;
void gm_show_fps(int show) { __gm_show_fps = show; }
/**
* @brief Processes events, updates input state, and prepares for the next
* frame.
*
* This function should be called at the end of the main game loop. It handles
* window events, polls for input, updates mouse and keyboard states, and
* swaps the graphics buffers.
*
* @return 1 if the game should continue to the next frame, 0 if the window has
* been closed.
* @example
* while (gm_yield()) {
* // Your game logic and rendering here
* }
*/
static inline int gm_yield() {
static const double alpha = 2.0 / 3.0;
static double _fps = 0;
static double dt = 1;
static double _display_fps = 0;
double current_dt = gm_dt();
dt += current_dt;
double fps = 1 / current_dt;
if (current_dt == 0)
fps = _fps;
if (_fps == 0)
_fps = 60;
else
_fps = (_fps * alpha) + (fps * (1 - alpha));
if (dt >= 0.5) {
dt = 0;
_display_fps = _fps;
}
char fps_text[20] = {0}; // ERROR: use fps
snprintf(fps_text, sizeof(fps_text), "fps: %.2f", _display_fps);
gmw_frame(0.9, -0.9, 0.4, 0.1);
gm_draw_text(0.9, -0.9, fps_text, "", 0.1, GM_WHITE);
}
const int ret = gapi_yield(&_gm_dt);
gm_mouse.lastPosition = gm_mouse.position;
gapi_mouse_get(&gm_mouse.position.x, &gm_mouse.position.y);
gm_mouse.movement.x = gm_mouse.position.x - gm_mouse.lastPosition.x;
gm_mouse.movement.y = gm_mouse.position.y - gm_mouse.lastPosition.y;
static int last_mouse_down = 0;
gm_mouse.clicked = !last_mouse_down && gm_mouse.down;
last_mouse_down = gm_mouse.down;
return ret;
}
/**
* @brief Closes the window and terminates the Gama engine.
*/
static inline void gm_quit() { return gapi_quit(); }
/**
* @brief Sets the background color of the window.
* @param c The color to set as the background.
*/
/**
* @brief Resizes the application window.
* @param width The new width of the window in pixels.
* @param height The new height of the window in pixels.
*/
void gm_resize(int width, int height) { return gapi_resize(width, height); }
/**
* @brief Initializes the Gama engine and opens a window.
*
* This must be the first Gama function called. It sets up the graphics context
* and creates a window with the specified dimensions and title.
*
* @param width The width of the window in pixels. Use 0 for automatic sizing.
* @param height The height of the window in pixels. Use 0 for automatic sizing.
* @param title The title of the window.
*/
void gm_init(int width, int height, const char *title) {
int code = gapi_init(width, height, title);
char msg[100];
if (code != 0) {
snprintf(msg, sizeof(msg),
"Error starting gama, initialization exited with non zero code %d",
code);
gapi_log(msg);
}
}
/**
* @brief Pauses execution for a specified duration.
*
* This function blocks the calling thread for approximately the given number
* of milliseconds.
*
* @param milliseconds The number of milliseconds to sleep.
*/
void gm_sleep(int milliseconds);
#ifdef __ZIG_CC__
void gm_sleep(int m) {};
#else
#ifdef _WIN32
#include <windows.h>
void gm_sleep(int milliseconds) { Sleep(milliseconds); }
#else
#include <unistd.h>
void gm_sleep(int milliseconds) { usleep(milliseconds * 1000); }
#endif
#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