crisp-game-lib-portable
cglp.h
Go to the documentation of this file.
1
2#ifndef CGLP_H
3#define CGLP_H
4
5#ifdef __cplusplus
6#define EXTERNC extern "C"
7#else
8#define EXTERNC extern
9#endif
10
11#include <math.h>
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdio.h>
15
16#include "machineDependent.h"
17#include "vector.h"
18
19#define FPS 60
20
21#define COLOR_COUNT 15
22#define TRANSPARENT -1
23#define WHITE 0
24#define RED 1
25#define GREEN 2
26#define YELLOW 3
27#define BLUE 4
28#define PURPLE 5
29#define CYAN 6
30#define BLACK 7
31#define LIGHT_RED 8
32#define LIGHT_GREEN 9
33#define LIGHT_YELLOW 10
34#define LIGHT_BLUE 11
35#define LIGHT_PURPLE 12
36#define LIGHT_CYAN 13
37#define LIGHT_BLACK 14
38
39#define CHARACTER_WIDTH 6
40#define CHARACTER_HEIGHT 6
41
42#define SOUND_EFFECT_TYPE_COUNT 9
43#define COIN 0
44#define LASER 1
45#define EXPLOSION 2
46#define POWER_UP 3
47#define HIT 4
48#define JUMP 5
49#define SELECT 6
50#define RANDOM 7
51#define CLICK 8
52
53#define TEXT_PATTERN_COUNT 94
54#define MAX_CHARACTER_PATTERN_COUNT 26
55#define ASCII_CHARACTER_COUNT 127
56#define MAX_CACHED_CHARACTER_PATTERN_COUNT 128
57
58#ifndef M_PI
59#define M_PI 3.14159265358979323846
60#endif
62
63typedef struct {
64 bool rect[COLOR_COUNT];
65 bool text[ASCII_CHARACTER_COUNT];
66 bool character[ASCII_CHARACTER_COUNT];
68
69typedef struct {
71} Collision;
72
73typedef struct {
78
79typedef struct {
89} Input;
90
91typedef struct {
96} Options;
97
98typedef struct {
103
105EXTERNC int ticks;
106EXTERNC float score;
107EXTERNC float difficulty;
108EXTERNC int color;
109EXTERNC float thickness;
110EXTERNC float barCenterPosRatio;
112EXTERNC bool hasCollision;
113EXTERNC float tempo;
114EXTERNC Input input;
115EXTERNC Input currentInput;
116EXTERNC bool isInMenu;
117EXTERNC Collision rect(float x, float y, float w, float h);
118EXTERNC Collision box(float x, float y, float w, float h);
119EXTERNC Collision line(float x1, float y1, float x2, float y2);
120EXTERNC Collision bar(float x, float y, float length, float angle);
121EXTERNC Collision arc(float centerX, float centerY, float radius,
122 float angleFrom, float angleTo);
123EXTERNC Collision text(char *msg, float x, float y);
124EXTERNC Collision character(char *msg, float x, float y);
125EXTERNC void play(int type);
126EXTERNC void addScore(float value, float x, float y);
127EXTERNC float rnd(float high, float low);
128EXTERNC int rndi(int high, int low);
129EXTERNC void gameOver();
130EXTERNC void particle(float x, float y, float count, float speed, float angle,
131 float angleWidth);
132
133EXTERNC float clamp(float v, float low, float high);
134EXTERNC float wrap(float v, float low, float high);
135EXTERNC char *intToChar(int v);
136EXTERNC void consoleLog(char *format, ...);
137EXTERNC void enableSound();
138EXTERNC void disableSound();
139EXTERNC void toggleSound();
140EXTERNC void goToMenu();
141EXTERNC void restartGame(int gameIndex);
142
143typedef struct {
144 unsigned char r;
145 unsigned char g;
146 unsigned char b;
147} ColorRgb;
148
149EXTERNC int currentColorIndex;
150EXTERNC ColorRgb colorRgbs[COLOR_COUNT];
151EXTERNC int getHashFromString(char *str);
152
153EXTERNC void initGame();
154EXTERNC void setButtonState(bool left, bool right, bool up, bool down, bool b,
155 bool a);
156EXTERNC void updateFrame();
158
160#define FOR_EACH(array, index) \
161 for (int index = 0; index < sizeof(array) / sizeof(array[0]); index++)
163#define ASSIGN_ARRAY_ITEM(array, index, type, item) type *item = &array[index]
165#define SKIP_IS_NOT_ALIVE(item) \
166 if (!item->isAlive) continue
168#define TIMES(count, index) for (int index = 0; index < count; index++)
171#define COUNT_IS_ALIVE(array, counter) \
172 int counter = 0; \
173 do { \
174 for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++) { \
175 if (array[i].isAlive) { \
176 counter++; \
177 } \
178 } \
179 } while (0)
181#define INIT_UNALIVED_ARRAY(array) \
182 do { \
183 for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++) { \
184 array[i].isAlive = false; \
185 } \
186 } while (0)
188#define RNDPM() (rndi(0, 2) * 2 - 1)
189
190#include "menu.h"
191
192#endif
EMSCRIPTEN_KEEPALIVE void updateFrame()
Definition: cglp.c:1016
void gameOver()
Transit to the game-over state.
Definition: cglp.c:951
Collision line(float x1, float y1, float x2, float y2)
Draw a line. Returns information on objects that collided while drawing.
Definition: cglp.c:225
Input currentInput
Input state (used to get input on title screen) (readonly).
Definition: cglp.c:56
int color
Definition: cglp.c:40
void particle(float x, float y, float count, float speed, float angle, float angleWidth)
Add particles.
Definition: cglp.c:624
EMSCRIPTEN_KEEPALIVE void initGame()
Definition: cglp.c:1002
float tempo
Tempo of background music.
Definition: cglp.c:52
float difficulty
Definition: cglp.c:34
int rndi(int low, int high)
Get a random int value of [low, high - 1].
Definition: cglp.c:746
float clamp(float v, float low, float high)
Clamp a value to [low, high].
Definition: cglp.c:759
float barCenterPosRatio
Definition: cglp.c:45
float wrap(float v, float low, float high)
Wrap a value to [low, high).
Definition: cglp.c:762
void play(int type)
Definition: cglp.c:529
bool isInMenu
Set to true when the menu screen is open (readonly).
Definition: cglp.c:58
CharacterOptions characterOptions
Options for drawing text() and character().
Definition: cglp.c:47
void consoleLog(char *format,...)
Definition: cglp.c:750
void addScore(float value, float x, float y)
Definition: cglp.c:591
void disableSound()
Disable playing background music and sound effects.
Definition: cglp.c:535
float score
Game score.
Definition: cglp.c:31
int ticks
A variable incremented by one every 1/60 of a second (readonly).
Definition: cglp.c:29
void enableSound()
Enable playing background music and sound effects.
Definition: cglp.c:532
void toggleSound()
Toggle sound playing flag.
Definition: cglp.c:541
float thickness
Set the thickness for drawing line(), bar() and arc().
Definition: cglp.c:42
Collision bar(float x, float y, float length, float angle)
Draw a bar. Returns information on objects that collided while drawing.
Definition: cglp.c:235
Collision box(float x, float y, float w, float h)
Draw a box. Returns information on objects that collided while drawing.
Definition: cglp.c:190
char * intToChar(int v)
Convert a value of type int to a value of type char*.
Definition: cglp.c:779
float rnd(float low, float high)
Get a random float value of [low, high).
Definition: cglp.c:743
Collision arc(float centerX, float centerY, float radius, float angleFrom, float angleTo)
Draw a arc. Returns information on objects that collided while drawing.
Definition: cglp.c:249
Input input
Input state from the buttons (readonly).
Definition: cglp.c:54
Collision rect(float x, float y, float w, float h)
Definition: cglp.c:180
EMSCRIPTEN_KEEPALIVE void setButtonState(bool left, bool right, bool up, bool down, bool b, bool a)
Definition: cglp.c:670
bool hasCollision
Definition: cglp.c:50
Collision character(char *msg, float x, float y)
Definition: cglp.c:451
void restartGame(int gameIndex)
Restart another game specified by gameIndex.
Definition: cglp.c:992
Collision text(char *msg, float x, float y)
Draw a text. Returns information on objects that collided while drawing.
Definition: cglp.c:444
void goToMenu()
Go to the game selection menu screen.
Definition: cglp.c:983
ButtonState right
Definition: cglp.h:81
bool isPressed
Definition: cglp.h:86
ButtonState left
Definition: cglp.h:80
bool isDarkColor
Definition: cglp.h:95
Collisions isColliding
Definition: cglp.h:70
ButtonState up
Definition: cglp.h:82
bool isJustPressed
Definition: cglp.h:75
int viewSizeY
Definition: cglp.h:93
int viewSizeX
Definition: cglp.h:92
ButtonState down
Definition: cglp.h:83
ButtonState a
Definition: cglp.h:85
bool isJustReleased
Definition: cglp.h:88
int soundSeed
Definition: cglp.h:94
bool isJustReleased
Definition: cglp.h:76
bool isMirrorX
Definition: cglp.h:99
bool isPressed
Definition: cglp.h:74
int rotation
Definition: cglp.h:101
ButtonState b
Definition: cglp.h:84
bool isJustPressed
Definition: cglp.h:87
bool isMirrorY
Definition: cglp.h:100
Definition: cglp.h:73
Definition: cglp.h:98
Definition: cglp.h:69
Definition: cglp.h:63
Definition: cglp.h:79
Definition: cglp.h:91
float angleTo(Vector *vec, float x, float y)
Definition: vector.c:36