-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
113 lines (87 loc) · 1.94 KB
/
main.cc
File metadata and controls
113 lines (87 loc) · 1.94 KB
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
#include <cstdio>
#include <vector>
#include <string>
#include <SDL2/SDL.h>
#include "config.h"
#include "renderer.h"
#include "context.h"
#include "scenes/scene_manager.h"
class Game {
public:
Game() {}
~Game() {
SDL_DestroyWindow(window_);
SDL_Quit();
}
int init() {
srand(time(0));
if (TTF_Init() != 0) {
return 1;
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS) != 0) {
return 1;
}
window_ = SDL_CreateWindow(
"Bomberman",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL_WINDOW_OPENGL
);
if (window_ == NULL) {
return 2;
}
if (renderer_.init(window_) != 0) {
SDL_DestroyWindow(window_);
SDL_Quit();
return 3;
}
if (renderer_.load_tiles("assets/tiles.bmp") != 0) {
return 4;
}
manager_ = new SceneManager(&renderer_, &context_);
return 0;
}
void start() {
context_.t_now = SDL_GetTicks();
context_.t_diff = 0;
SDL_Event event;
context_.end = false;
// manager_->set_scene(SCENE_GAME_OVER);
manager_->set_scene(SCENE_PLAY);
while (!context_.end) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
context_.end = true;
}
if (event.type == SDL_KEYDOWN) {
context_.key_pressed[event.key.keysym.scancode] = true;
}
if (event.type == SDL_KEYUP) {
context_.key_pressed[event.key.keysym.scancode] = false;
}
}
manager_->render();
int now = SDL_GetTicks();
context_.t_diff = now - context_.t_now;
context_.t_now = now;
renderer_.update();
}
}
private:
SDL_Window *window_;
Renderer renderer_;
Context context_;
SceneManager *manager_;
};
int main(void)
{
Game game;
if (game.init() != 0) {
puts("Game initialization failed.");
return 1;
}
game.start();
return 0;
}