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
#ifndef __SDL_WRAPPER_H__
#define __SDL_WRAPPER_H__
#include "color.h"
#include "scene.h"
#include "list.h"
#include "state.h"
#include "vector.h"
#include <stdbool.h>
// Values passed to a key handler when the given arrow key is pressed
typedef enum {
LEFT_ARROW = 1,
UP_ARROW = 2,
RIGHT_ARROW = 3,
DOWN_ARROW = 4
} arrow_key_t;
/**
* The possible types of key events.
* Enum types in C are much more primitive than in Java; this is equivalent to:
* typedef unsigned int KeyEventType;
* #define KEY_PRESSED 0
* #define KEY_RELEASED 1
*/
typedef enum { KEY_PRESSED, KEY_RELEASED } key_event_type_t;
/**
* A keypress handler.
* When a key is pressed or released, the handler is passed its char value.
* Most keys are passed as their char value, e.g. 'a', '1', or '\r'.
* Arrow keys have the special values listed above.
*
* @param key a character indicating which key was pressed
* @param type the type of key event (KEY_PRESSED or KEY_RELEASED)
* @param held_time if a press event, the time the key has been held in seconds
* @param state the current state of the window
*/
typedef void (*key_handler_t)(state_t *state, char key, key_event_type_t type,
double held_time);
vector_t get_mouse_pos(void);
void get_text_and_rect(void);
/**
* Initializes the SDL window and renderer.
* Must be called once before any of the other SDL functions.
*
* @param min the x and y coordinates of the bottom left of the scene
* @param max the x and y coordinates of the top right of the scene
*/
void sdl_init(vector_t min, vector_t max);
/**
* Processes all SDL events and returns whether the window has been closed.
* This function must be called in order to handle keypresses.
*
* @return true if the window was closed, false otherwise
*/
bool sdl_is_done(state_t *state);
/**
* Clears the screen. Should be called before drawing polygons in each frame.
*/
void sdl_clear(void);
/**
* Draws a polygon from the given list of vertices and a color.
*
* @param points the list of vertices of the polygon
* @param color the color used to fill in the polygon
*/
void sdl_draw_polygon(list_t *points, color_t color);
/**
* Displays the rendered frame on the SDL window.
* Must be called after drawing the polygons in order to show them.
*/
void sdl_show(void);
/**
* Draws all bodies in a scene.
* This internally calls sdl_clear(), sdl_draw_polygon(), and sdl_show(),
* so those functions should not be called directly.
*
* @param scene the scene to draw
*/
void sdl_render_scene(scene_t *scene);
/**
* Registers a function to be called every time a key is pressed.
* Overwrites any existing handler.
*
* Example:
* ```
* void on_key(char key, key_event_type_t type, double held_time) {
* if (type == KEY_PRESSED) {
* switch (key) {
* case 'a':
* printf("A pressed\n");
* break;
* case UP_ARROW:
* printf("UP pressed\n");
* break;
* }
* }
* }
* int main(void) {
* sdl_on_key(on_key);
* while (!sdl_is_done());
* }
* ```
*
* @param handler the function to call with each key press
*/
void sdl_on_key(key_handler_t handler);
/**
* Gets the amount of time that has passed since the last time
* this function was called, in seconds.
*
* @return the number of seconds that have elapsed
*/
double time_since_last_tick(void);
#endif // #ifndef __SDL_WRAPPER_H__