-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
75 lines (61 loc) · 1.68 KB
/
main.cpp
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
#include <SDL.h>
#include <SDL2_gfxPrimitives.h>
#include <SDL_events.h>
#include <SDL_keycode.h>
#include <SDL_render.h>
#include <SDL_timer.h>
#include <SDL_video.h>
#include <ctime>
#include <iostream>
#include <unordered_map>
#include "color.hpp"
#include "common.h"
#include "state/machine.hpp"
#include "state/state.hpp"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
float fps = 0;
Uint32 start_time, frame_time;
struct main_loop_data_t {
StateMachine& m;
SDL_Renderer* renderer;
};
void main_loop(void* d) {
main_loop_data_t* data = reinterpret_cast<main_loop_data_t*>(d);
frame_time = SDL_GetTicks() - start_time;
start_time = SDL_GetTicks();
fps = (frame_time > 0) ? 1000.0f / frame_time : 0.0f;
data->m.handleEvents();
data->m.update();
data->m.render(data->renderer);
}
int main() {
const int width = 1000;
const int height = 1000;
srand(time(NULL));
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width, height,
SDL_WINDOW_SHOWN);
SDL_Renderer* renderer =
SDL_CreateRenderer(window, 0, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, Color::Black.r, Color::Black.g,
Color::Black.b, Color::Black.a);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
StateMachine machine;
main_loop_data_t data = {machine, renderer};
start_time = SDL_GetTicks();
#ifdef __EMSCRIPTEN__
void* d = &data;
emscripten_set_main_loop_arg(main_loop, d, 0, 1);
#else
while (1) {
// do stuff
SDL_Delay(15);
main_loop(&data);
}
#endif
return 0;
}