Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Experimento con emscripten y SDL

1178 views
1
#ifndef ENGINE_H
2
#define ENGINE_H
3
4
#include <SDL2/SDL.h>
5
#include <SDL2/SDL_image.h>
6
#include <SDL/SDL_ttf.h>
7
#include <emscripten.h>
8
#include <stdbool.h>
9
10
#include "input_processor.h"
11
#include "sprite.h"
12
#include "actor.h"
13
14
typedef struct engine engine;
15
16
typedef enum DIRECTION
17
{
18
DIRECTION_N,
19
DIRECTION_E,
20
DIRECTION_S,
21
DIRECTION_W
22
} DIRECTION;
23
24
enum
25
{
26
SAND_TEXTURE,
27
SPRITES_TEXTURE,
28
STATUS_BAR_TEXTURE,
29
NUM_TEXTURES
30
};
31
32
typedef enum SPRITES_DECALS
33
{
34
TAIL_S,
35
TAIL_W,
36
HEAD_N,
37
HEAD_E,
38
BODY_SE,
39
BODY_SW,
40
BODY_NW,
41
BLANK,
42
TAIL_E,
43
TAIL_N,
44
HEAD_W,
45
HEAD_S,
46
BODY_N,
47
BODY_E,
48
BODY_NE,
49
APPLE,
50
NUM_SPRITES_DECALS
51
} SPRITES_DECALS;
52
53
enum {
54
MAX_SNAKE_SEGMENTS = 50,
55
TILE_DIMENSION = 40,
56
STATUS_BAR_HEIGHT = 2, // given in tiles
57
STATUS_TEXT_SIZE = 30
58
};
59
60
#define STATUS_TEXT_FONT "assets/edosz.ttf"
61
62
#define INITIAL_SNAKE_SECONDS_PER_UPDATE 0.2
63
#define MINIMUM_SNAKE_SECONDS_PER_UPDATE 0.005
64
#define SNAKE_SECONDS_PER_UPDATE_INCREMENT 0.01
65
66
#include "apple_actor.h"
67
#include "snake_actor.h"
68
#include "background_actor.h"
69
70
struct engine
71
{
72
unsigned int fps;
73
unsigned int current_frame;
74
SDL_Window *window;
75
SDL_Renderer *renderer;
76
unsigned int w;
77
unsigned int h;
78
79
Uint32 start_time;
80
bool should_start_logic_loop;
81
unsigned int whole_frames_to_do;
82
83
actor_list *render_list;
84
actor_list *logic_list;
85
86
SDL_Texture *textures[NUM_TEXTURES];
87
decal sprites_decals[NUM_SPRITES_DECALS];
88
decal sand_decal;
89
90
apple_actor apple_actor;
91
background_actor background_actor;
92
// status_bar_actor status_bar_actor;
93
snake_actor snake_actor;
94
95
unsigned int score;
96
unsigned int *occupied_gridpoints;
97
int grid_width;
98
int grid_height;
99
};
100
101
engine eng;
102
103
engine *engine_init(
104
unsigned int w,
105
unsigned int h);
106
107
void engine_destroy();
108
109
void engine_start();
110
111
void engine_reset();
112
113
void engine_get_grid_coord(const int *pixel_coord, int *grid_coord);
114
void engine_get_pixel_coord(const int *grid_coord, int *pixel_coord);
115
void engine_apply_boundary_conditions(int *grid_coords);
116
117
#endif
118
119