aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy.h
blob: 80f9a719b6886ad798add5fffd4609e8f6de873a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#ifndef TEENSY_H_
#define TEENSY_H_

#include <limits.h>

#include "teensy_common.h"
#include "teensy_list.h"

#define ty_vec2(x, y)        ((ty_Vec2){x, y})
#define ty_vec2i(x, y)       ((ty_Vec2i){x, y})
#define ty_color(R, G, B)    ((ty_Color){R, G, B})
#define ty_rect(x, y, w, h)  ((ty_Rect){x, y, w, h})
#define ty_recti(x, y, w, h) ((ty_Recti){x, y, w, h})
#define ty_img_full(img)     ((ty_Recti){0, 0, (img).width, (img).height})

#define ty_rect_start(rect)  (ty_vec2(rect.x, rect.y))
#define ty_recti_start(rect) (ty_vec2i(rect.x, rect.y))
#define ty_rect_end(rect)    (ty_vec2(rect.x+rect.w, rect.y+rect.h))
#define ty_recti_end(rect)   (ty_vec2i(rect.x+rect.w, rect.y+rect.h))
#define ty_rect_size(rect)   (ty_vec2(rect.w, rect.h))
#define ty_recti_size(rect)  (ty_vec2i(rect.w, rect.h))

#define ty_max(a, b) ((a) > (b) ? (a) : (b))
#define ty_min(a, b) ((a) < (b) ? (a) : (b))
#define ty_clamp(a, mi, ma) (ty_max(ty_min(a, ma), mi))

#define TY_CLIP_NONE     (ty_recti(0, 0, -1, -1))

#define TY_COLOR_BLACK   ty_color(0,   0,   0  )
#define TY_COLOR_RED     ty_color(255, 0,   0  )
#define TY_COLOR_GREEN   ty_color(0,   255, 0  )
#define TY_COLOR_YELLOW  ty_color(255, 255, 0  )
#define TY_COLOR_BLUE    ty_color(0,   0,   255)
#define TY_COLOR_MAGENTA ty_color(255, 0,   255)
#define TY_COLOR_CYAN    ty_color(0,   255, 255)
#define TY_COLOR_WHITE   ty_color(255, 255, 255)
#define TY_COLOR_GRAY    ty_color(128, 128, 128)

// Virtual controller. Keyboard keys are also mapped to this.
typedef uint8_t ty_Button;
enum {
    TY_BTN_LEFT_UP,    // D-pad #1 up
    TY_BTN_LEFT_DOWN,  // D-pad #1 down
    TY_BTN_LEFT_LEFT,  // D-pad #1 left
    TY_BTN_LEFT_RIGHT, // D-pad #1 right

    TY_BTN_RIGHT_UP,    // D-pad #2 up
    TY_BTN_RIGHT_DOWN,  // D-pad #2 down
    TY_BTN_RIGHT_LEFT,  // D-pad #2 left
    TY_BTN_RIGHT_RIGHT, // D-pad #2 right

    TY_BTN_ACTION_1,    // A
    TY_BTN_ACTION_2,    // B
    TY_BTN_ACTION_3,    // X
    TY_BTN_ACTION_4,    // Y

    // For use in the editor

    TY_BTN_DB_CTRL,
    TY_BTN_DB_SHIFT,
    TY_BTN_DB_LMB,
    TY_BTN_DB_RMB,
    TY_BTN_DB_MMB,

    TY_BTN_COUNT,
};

// Tells Teensy what your preferred settings would be. Some settings may not
// always be accurately followed, hence they are hints.
typedef struct {
    int scr_width;
    int scr_height;
    const char *game_title;
    int ticrate;
} ty_Hints;

// Represents an RGB8 color.
typedef struct {
    uint8_t r;
    uint8_t g;
    uint8_t b;
} ty_Color;

// Floating-point 2D vector.
typedef struct {
    float x;
    float y;
} ty_Vec2;

// Fixed-point 2D vector.
typedef struct {
    int x;
    int y;
} ty_Vec2i;

// Floating-point 2D rectangle.
typedef struct {
    float x;
    float y;
    float w;
    float h;
} ty_Rect;

// Fixed-point 2D rectangle.
typedef struct {
    int x;
    int y;
    int w;
    int h;
} ty_Recti;

// Stores an image in RGB8 format. When drawing, any texel of the color #FF00FF
// will be interpreted as transparent.
typedef struct {
    ty_Color *data;
    int width;
    int height;
} ty_Image;

// Stores a list of images that map to ASCII characters. The only required
// character is a space (0x20) since it determines the height of the font.
typedef struct  {
    ty_Image glyphs[CHAR_MAX];
} ty_Font;

void ty_init(ty_Hints hints);
void ty_deinit(void);

// Returns whether a button is being pressed or not.
bool ty_button_down(ty_Button btn);
// Returns whether a button was pressed down this frame or not.
bool ty_button_pressed(ty_Button btn);
// Gets the mouse position; intended to be used as debug or dev tools.
ty_Vec2i ty_mouse_pos(void);

// Whether or not the main loop should continue executing.
bool ty_is_game_running(void);
// Gets the real time in seconds that the game has been running.
double ty_get_time(void);
// Handle events, game logic, etc. Returns the amount of times you should tic.
int ty_tick(void);

// Takes in a string format and a va_list and returns a TEMPORARILY ALLOCATED
// formatted string.
char *ty_format_args(const char *fmt, va_list list);
// Takes in a string format and returns a TEMPORARILY ALLOCATED formatted
// string.
char *ty_format(const char *fmt, ...);

// Checks if a point is within a fixed-point rect.
bool ty_pointi_in_recti(ty_Vec2i point, ty_Recti rect);
// Checks if a point is within a floating-point rect.
bool ty_point_in_rect(ty_Vec2 point, ty_Rect rect);

// Shrinks a rect by `p`.
ty_Recti ty_recti_shrink(ty_Recti rect, int p);
// Grows a rect by `p`.
ty_Recti ty_recti_grow(ty_Recti rect, int p);
// Clamps a rect's bounds to be entirely within another.
ty_Recti ty_recti_clamp(ty_Recti rect, ty_Recti minmax);

// Creates an image using the relevant data (formatted in RGB8). You may free
// the passed data after this call as it is copied.
ty_Image ty_create_image(int w, int h, const ty_Color* data);
void ty_free_image(ty_Image img);
// Returns the specified pixel color on an image. Will emit a fatal error if an
// index is made out of bounds.
ty_Color ty_img_get_pixel(ty_Image img, ty_Vec2i pos);
// Sets a pixel to a color. Will emit a fatal error if an index is made out of
// bounds.
void ty_img_set_pixel(
    ty_Image img,
    ty_Vec2i pos,
    ty_Color color
);

// Adds a glyph to a font. The glyph is an ASCII character.
void ty_font_add_glyph(ty_Font *font, uint8_t c, ty_Image img);
// Calculates the length, in pixels, of the string when drawn with a font.
int ty_font_width(const ty_Font *font, const char *fmt, ...);
// Calculates the font height
int ty_font_height(const ty_Font *font);

// Sets the image that the renderer draws to. Pass NULL to restore the screen.
void ty_draw_set_target(const ty_Image *img);
// Prevents anything from being drawn outside of the passed rect. Pass
// TY_CLIP_NONE to reset the clipspace.
void ty_draw_set_clip(ty_Recti clip);
// Sets the entire render target to one color.
void ty_draw_clear(ty_Color col);
// Draws an image at the specified position. The color #FF00FF will be ignored
// and not drawn.
void ty_draw_image(ty_Image img, ty_Vec2i pos);
// Draws a portion of the image to the specified destination rect on the render
// target. The color #FF00FF will be ignored and not drawn.
void ty_draw_image_ex(
    ty_Image img,
    ty_Recti src,
    ty_Recti dst
);
// Draws an image with a rotation. The color #FF00FF will be ignored and not
// drawn.
void ty_draw_image_rot(
    ty_Image img,
    ty_Vec2i pos,
    ty_Vec2i offset,
    double rot
);

// Draws a axis-aligned rectangle onto the render target.
void ty_draw_rect(ty_Recti rect, ty_Color color);
// Draws a line to the render target.
void ty_draw_line(
    ty_Vec2i start,
    ty_Vec2i end,
    ty_Color color
);

// Draws text using to the screen, using a specific font.
void ty_draw_text(const ty_Font *font, ty_Vec2i pos, const char *text);
// Draws formatted text with arguments from a va_list using to the screen, using
// a specific font.
void ty_draw_text_args(
    const ty_Font *font,
    ty_Vec2i pos,
    const char *fmt,
    va_list args
);
// Draws formatted text using to the screen, using a specific font.
void ty_draw_text_fmt(
    const ty_Font *font,
    ty_Vec2i pos,
    const char *fmt,
    ...
);

// Finalizes any rendering done.
void ty_draw_end(void);

// Pause execution of the game for the amount of time passed in milliseconds.
void ty_sleep(uint64_t ms);

#endif // TEENSY_H_