-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
80 lines (71 loc) · 1.87 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
76
77
78
79
80
#include <iostream>
#include <SDL2/SDL_pixels.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_ttf.h>
#include "helpers/constants.h"
#include "game/screens/screens.h"
#include "game/screens/start_screen.h"
using std::cout;
using std::endl;
using Screens::Screen;
using Screens::StartScreen;
int main(){
if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
cout << "SDL_Init Error: " << SDL_GetError() << endl;
return 1;
}
if(TTF_Init() == -1){
cout << "TTF_Init Error: " << TTF_GetError() << endl;
return 1;
}
SDL_Window* window = SDL_CreateWindow(
"snake game",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIDTH,
HEIGHT,
SDL_WINDOW_ALLOW_HIGHDPI);
if(window == nullptr){
cout << "Failed on create window: " << TTF_GetError() << endl;
return 1;
}
SDL_Renderer* render = SDL_CreateRenderer(window, 1, 0);
if(render == nullptr){
cout << "Failed on create render: " << TTF_GetError() << endl;
return 1;
}
Screen* screen = new StartScreen(render);
bool game_loop = true;
while(game_loop){
SDL_Event event;
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT)
game_loop = false;
else if(event.type == SDL_KEYDOWN){
if(event.key.keysym.sym == SDLK_ESCAPE)
game_loop = false;
Screen* changed_screen = screen->key_event(event.key.keysym.sym);
if(changed_screen != nullptr){
delete screen;
screen = changed_screen;
continue;
}
}
}
SDL_RenderClear(render);
screen->execute(game_loop);
SDL_RenderPresent(render);
}
screen->close_event();
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();
TTF_Quit();
delete screen;
return 0;
}