Gama C Library
Gama C API Documentation
Loading...
Searching...
No Matches
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// NOTE: The order is important, major, minor, patch
12#define GAMA_VERSION_MAJOR 0
13#define GAMA_VERSION_MINOR 1
14#define GAMA_VERSION_PATCH 0
15
16#include "animate.h"
17#include "body.h"
18#include "draw.h"
19#include "gapi.h"
20#include "key.h"
21#include "physics.h"
22#include "sprite.h"
23#include "system.h"
24#include "widgets.h"
25#include <stdio.h>
26
27/**
28 * @brief Puts the window in fullscreen.
29 *
30 * @param fullscreen Enable or disable fullscreen.
31 */
32void gm_fullscreen(int fullscreen) { return gapi_fullscreen(fullscreen); }
33
34/**
35 * @brief Draws gama logo.
36 *
37 * @param x The x position to draw the logo.
38 * @param y The y position to draw the logo.
39 * @param s The logo size.
40 */
41void gm_logo(double x, double y, double s) {
42 double top_thickness = 0.15 * s;
43 double left_thickness = 0.1 * s;
44 double ratio = 0.6;
45 // top bar
46 gm_draw_rectangle(x, y + s / 2 - top_thickness / 2, s * ratio, top_thickness,
47 GM_GAMA);
48 gm_draw_rectangle(x + (-s / 2) + (left_thickness / 2) + (s / 2 * (1 - ratio)),
49 y, left_thickness, s, GM_GAMA);
50}
51
52void gm_log(const char *txt) { return gapi_log(txt); }
53
54/**
55 * @brief Checks if the main game loop should continue running.
56 * @return 1 if the window is open and the game should continue, 0 otherwise.
57 * @deprecated This function is deprecated and will be removed. Use gm_yield()
58 * instead.
59 */
60static inline int gm_runs() { return gapi_runs(); }
61
63void gm_show_fps(int show) { __gm_show_fps = show; }
64
65void _gm_fps() {
66 static const double alpha = 0.9;
67 static double _fps = 0;
68 static double dt = 1;
69 static double _display_fps = 0;
70 dt += gm_dt();
71 double fps = 1 / gm_dt();
72 if (_fps == 0)
73 _fps = 60;
74 else
75 _fps = (_fps * alpha) + (fps * (1 - alpha));
76 if (dt >= 0.5) {
77 dt = 0;
78 _display_fps = _fps;
79 }
80
81 if (__gm_show_fps) {
82 char fps_text[20];
83 snprintf(fps_text, sizeof(fps_text), "fps: %.2lf", _display_fps);
84 gmw_frame(0.9, -0.9, 0.4, 0.1);
85 gm_draw_text(0.9, -0.9, fps_text, "", 0.1, GM_WHITE);
86 }
87}
88
89/**
90 * @brief Processes events, updates input state, and prepares for the next
91 * frame.
92 *
93 * This function should be called at the end of the main game loop. It handles
94 * window events, polls for input, updates mouse and keyboard states, and
95 * swaps the graphics buffers.
96 *
97 * @return 1 if the game should continue to the next frame, 0 if the window has
98 * been closed.
99 * @example
100 * while (gm_yield()) {
101 * // Your game logic and rendering here
102 * }
103 */
104static inline int gm_yield() {
105 if (gapi_yield(&_gm_dt)) {
106 _gm_t += _gm_dt;
107 gapi_get_mouse_move(&gm_mouse.movement.x, &gm_mouse.movement.y);
108 gapi_mouse_get(&gm_mouse.position.x, &gm_mouse.position.y);
109 gm_mouse.down = gapi_mouse_down();
110 gm_mouse.pressed = gapi_mouse_pressed();
111 _gm_fps();
112 return 1;
113 } else
114 return 0;
115}
116
117/**
118 * @brief Closes the window and terminates the Gama engine.
119 */
120static inline void gm_quit() { return gapi_quit(); }
121
122/**
123 * @brief Waits for all pending graphics operations to complete.
124 *
125 * This can be used for synchronization purposes, but it is rarely needed as
126 * gm_yield() handles buffer swapping automatically.
127 */
128void gm_sync() { return gapi_wait_queue(); }
129
130/**
131 * @brief Sets the background color of the window.
132 * @param c The color to set as the background.
133 */
135 return gapi_set_bg_color(gm_red(c), gm_green(c), gm_blue(c), gm_alpha(c));
136}
137
138void gm_resize(int width, int height) { return gapi_resize(width, height); }
139
140/**
141 * @brief Initializes the Gama engine and opens a window.
142 *
143 * This must be the first Gama function called. It sets up the graphics context
144 * and creates a window with the specified dimensions and title.
145 *
146 * @param width The width of the window in pixels.
147 * @param height The height of the window in pixels.
148 * @param title The title of the window.
149 */
150void gm_init(int width, int height, const char *title) {
151 int code = gapi_init(width, height, title);
152 char msg[100];
153
154 if (code != 0) {
155 snprintf(msg, sizeof(msg),
156 "Error starting gama, initialization exited with non zero code %d",
157 code);
158 gapi_log(msg);
159 printf("%s", msg);
160 }
162 gm_logo(0, 0, 2);
163 gm_yield();
164}
165
166/**
167 * @brief Sleep(wait) for aproximately the specified number of milliseconds
168 *
169 * @param miliseconds the number of milliseconds to sleep
170 */
171void gm_sleep(int milliseconds);
172
173#ifdef _WIN32
174#include <windows.h>
175void gm_sleep(int milliseconds) { Sleep(milliseconds); }
176#else
177#include <unistd.h>
178void gm_sleep(int milliseconds) { usleep(milliseconds * 1000); }
179#endif
Functions for animating values with various easing functions.
#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_GAMA
The official Gama brand color.
Definition color.h:126
#define GM_BLACK
Black color.
Definition color.h:166
#define gm_alpha(col)
Extracts the alpha component from a color.
Definition color.h:36
#define GM_WHITE
White color.
Definition color.h:851
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.
Definition draw.h:135
int32_t gm_draw_rectangle(double x, double y, double w, double h, gmColor c)
Draws a rectangle.
Definition draw.h:47
int gmw_frame(double x, double y, double width, double height)
Creates and renders a frame widget (a bordered panel).
Definition frame.h:67
void gm_log(const char *txt)
Definition gama.h:52
void gm_show_fps(int show)
Definition gama.h:63
int __gm_show_fps
Definition gama.h:62
void _gm_fps()
Definition gama.h:65
void gm_resize(int width, int height)
Definition gama.h:138
void gm_logo(double x, double y, double s)
Draws gama logo.
Definition gama.h:41
void gm_sleep(int milliseconds)
Sleep(wait) for aproximately the specified number of milliseconds.
Definition gama.h:178
void gm_fullscreen(int fullscreen)
Puts the window in fullscreen.
Definition gama.h:32
void gm_background(gmColor c)
Sets the background color of the window.
Definition gama.h:134
void gm_sync()
Waits for all pending graphics operations to complete.
Definition gama.h:128
void gm_init(int width, int height, const char *title)
Initializes the Gama engine and opens a window.
Definition gama.h:150
int32_t gapi_mouse_down()
double _gm_dt
Definition gapi.h:6
void gapi_fullscreen(const int32_t fullscreen)
int32_t gapi_mouse_pressed()
void gapi_set_bg_color(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a)
double _gm_t
Definition gapi.h:7
int32_t gapi_init(const int32_t width, const int32_t height, const char *title)
void gapi_wait_queue()
void gapi_resize(const int32_t width, const int32_t height)
void gapi_log(const char *message)
void gapi_get_mouse_move(double *x, double *y)
void gapi_quit()
int32_t gapi_yield(double *dt)
int32_t gapi_runs()
struct _gmMouse gm_mouse
Definition gapi.h:19
int32_t gapi_mouse_get(double *x, double *y)