crisp-game-lib-arduboy
cglab.h
Go to the documentation of this file.
1
2#ifndef CGLAB_H
3#define CGLAB_H
4
5#ifdef __cplusplus
6#define EXTERNC extern "C"
7#else
8#define EXTERNC extern
9#endif
10
11#include <Arduino.h>
12
13#include "machineDependent.h"
14#include "vector.h"
15
16enum COLOR_ENUM {
17 TRANSPARENT = -1,
18 DARK1,
19 DARK2,
20 DARK3,
21 LIGHT1,
22 LIGHT2,
23 LIGHT3,
24 INVERT1,
25 INVERT2,
26 COLOR_COUNT,
27 DEFAULT_COLOR = LIGHT1,
28};
29
30enum SOUND_ENUM {
31 COIN = 0,
32 LASER,
33 EXPLOSION,
34 POWER_UP,
35 HIT,
36 JUMP,
37 SELECT,
38 RANDOM,
39 CLICK,
40 SOUND_EFFECT_TYPE_COUNT,
41 BGM = SOUND_EFFECT_TYPE_COUNT,
42};
43
44#define CHARACTER_WIDTH 6
45#define CHARACTER_HEIGHT 6
46
47#define TEXT_PATTERN_COUNT 94
48#define MAX_CHARACTER_PATTERN_COUNT 26
49
50#define HIT_BOX_INDEX_COLOR_BASE 0
51#define HIT_BOX_INDEX_TEXT_BASE (HIT_BOX_INDEX_COLOR_BASE + COLOR_COUNT)
52#define HIT_BOX_INDEX_CHARACTER_BASE (HIT_BOX_INDEX_TEXT_BASE + TEXT_PATTERN_COUNT)
53
54#ifndef M_PI
55#define M_PI 3.14159265358979323846
56#endif
58
59typedef struct {
60 uint8_t x:4;
61 uint8_t y:4;
62 uint8_t w:4;
63 uint8_t h:4;
65
66typedef struct {
67 uint8_t grid[CHARACTER_WIDTH];
70
71typedef struct {
72 uint16_t isColliding[8];
73} Collision;
74
76EXTERNC uint16_t ticks;
77EXTERNC float score;
78EXTERNC float difficulty;
79EXTERNC int8_t color;
80EXTERNC float thickness;
81EXTERNC float barCenterPosRatio;
82EXTERNC bool hasCollision;
83
84EXTERNC bool colRect(Collision *cl, uint8_t color);
85EXTERNC bool colText(Collision *cl, char text);
86EXTERNC bool colCharacter(Collision *cl, char character);
87EXTERNC Collision rect(float x, float y, float w, float h);
88EXTERNC Collision box(float x, float y, float w, float h);
89EXTERNC Collision line(float x1, float y1, float x2, float y2);
90EXTERNC Collision bar(float x, float y, float length, float angle);
91EXTERNC Collision arc(float centerX, float centerY, float radius,
92 float angleFrom, float angleTo);
93EXTERNC Collision text(char *msg, float x, float y);
94EXTERNC Collision constText(const char *msg, float x, float y);
95EXTERNC Collision character(char character, float x, float y);
96EXTERNC void play(uint8_t type);
97EXTERNC void addScore(float value, float x, float y);
98EXTERNC float rnd(float high, float low);
99EXTERNC int16_t rndi(int16_t high, int16_t low);
100EXTERNC void gameOver(void);
101EXTERNC void particle(float x, float y, float count, float speed, float angle,
102 float angleWidth);
103EXTERNC bool btn(uint8_t button);
104EXTERNC bool btnp(uint8_t button);
105EXTERNC bool btnr(uint8_t button);
106
107EXTERNC float clamp(float v, float low, float high);
108EXTERNC float wrap(float v, float low, float high);
109EXTERNC void enableSound(void);
110EXTERNC void disableSound(void);
111EXTERNC void toggleSound(void);
112
113EXTERNC void setupGame(const char *title, const char *description,
114 const CharacterData *characters, const uint8_t **sounds,
115 void (*update)(void));
116EXTERNC void initGame(void);
117EXTERNC bool updateGame(void);
119
121#define FOR_EACH(array, index) \
122 for (uint8_t index = 0; index < sizeof(array) / sizeof(array[0]); index++)
124#define ASSIGN_ARRAY_ITEM(array, index, type, item) type *item = &array[index]
126#define SKIP_IS_NOT_ALIVE(item) \
127 if (!item->isAlive) continue
129#define TIMES(count, index) for (uint8_t index = 0; index < count; index++)
132#define COUNT_IS_ALIVE(array, counter) \
133 uint8_t counter = 0; \
134 do { \
135 for (uint8_t i = 0; i < sizeof(array) / sizeof(array[0]); i++) { \
136 if (array[i].isAlive) { \
137 counter++; \
138 } \
139 } \
140 } while (0)
142#define INIT_UNALIVED_ARRAY(array) \
143 do { \
144 for (uint8_t i = 0; i < sizeof(array) / sizeof(array[0]); i++) { \
145 array[i].isAlive = false; \
146 } \
147 } while (0)
149#define RNDPM() (rndi(0, 2) * 2 - 1)
150
151#endif
Collision line(float x1, float y1, float x2, float y2)
Draw a line. Returns information on objects that collided while drawing.
Definition: cglab.c:197
bool updateGame(void)
Update game frames if it's every 1/60 second timing.
Definition: cglab.c:736
void particle(float x, float y, float count, float speed, float angle, float angleWidth)
Add particles.
Definition: cglab.c:515
float difficulty
Definition: cglab.c:21
void initGame(void)
Initialize game.
Definition: cglab.c:724
float clamp(float v, float low, float high)
Clamp a value to [low, high].
Definition: cglab.c:554
float barCenterPosRatio
Definition: cglab.c:31
bool colCharacter(Collision *cl, char character)
Whether it's colliding with characters.
Definition: cglab.c:109
float wrap(float v, float low, float high)
Wrap a value to [low, high).
Definition: cglab.c:559
int8_t color
Definition: cglab.c:26
Collision character(char character, float x, float y)
Definition: cglab.c:344
bool btn(uint8_t button)
Whether the button is being pressed. Button are INPUT_*.
Definition: cglab.c:530
uint16_t ticks
A variable incremented by one every 1/60 of a second (readonly).
Definition: cglab.c:16
void play(uint8_t type)
Definition: cglab.c:378
bool colText(Collision *cl, char text)
Whether it's colliding with texts.
Definition: cglab.c:100
void addScore(float value, float x, float y)
Definition: cglab.c:479
void gameOver(void)
Transit to the game-over state.
Definition: cglab.c:705
float score
Game score.
Definition: cglab.c:18
bool btnp(uint8_t button)
Whether the button is just pressed now. Button are INPUT_*.
Definition: cglab.c:535
float thickness
Set the thickness for drawing line(), bar() and arc().
Definition: cglab.c:28
Collision bar(float x, float y, float length, float angle)
Draw a bar. Returns information on objects that collided while drawing.
Definition: cglab.c:207
Collision box(float x, float y, float w, float h)
Draw a box. Returns information on objects that collided while drawing.
Definition: cglab.c:170
bool colRect(Collision *cl, uint8_t color)
Whether it's colliding with rects.
Definition: cglab.c:91
bool btnr(uint8_t button)
Whether the button is just released now. Button are INPUT_*.
Definition: cglab.c:540
float rnd(float low, float high)
Get a random float value of [low, high).
Definition: cglab.c:546
Collision arc(float centerX, float centerY, float radius, float angleFrom, float angleTo)
Draw a arc. Returns information on objects that collided while drawing.
Definition: cglab.c:221
Collision rect(float x, float y, float w, float h)
Draw a rectangle. Returns information on objects that collided while drawing.
Definition: cglab.c:160
void setupGame(const char *_title, const char *_description, const CharacterData *_characters, const uint8_t **_sounds, void(*_update)(void))
Setup game.
Definition: cglab.c:711
void enableSound(void)
Enable playing background music and sound effects.
Definition: cglab.c:383
void toggleSound(void)
Toggle sound playing flag.
Definition: cglab.c:393
bool hasCollision
Definition: cglab.c:34
void disableSound(void)
Disable playing background music and sound effects.
Definition: cglab.c:388
int16_t rndi(int16_t low, int16_t high)
Get a random int value of [low, high - 1].
Definition: cglab.c:551
Collision constText(const char *msg, float x, float y)
Draw a const text. Returns information on objects that collided while drawing.
Definition: cglab.c:337
Collision text(char *msg, float x, float y)
Draw a text. Returns information on objects that collided while drawing.
Definition: cglab.c:332
uint8_t x
Definition: cglab.h:60
CharacterHitBox hitBox
Definition: cglab.h:68
uint8_t w
Definition: cglab.h:62
uint8_t y
Definition: cglab.h:61
uint8_t h
Definition: cglab.h:63
Definition: cglab.h:66
Definition: cglab.h:59
Definition: cglab.h:71
float angleTo(Vector *vec, float x, float y)
Definition: vector.c:36