-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
150 lines (135 loc) · 2.97 KB
/
game.cpp
File metadata and controls
150 lines (135 loc) · 2.97 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// contains the function that control the main game loop
// -- initializing/destroying
// -- handling events
// -- rendering
// mainly calls other files' functions
#include "game.h"
#include "board.h"
#include "menu.h"
#include <cmath>
#include <chrono>
#include <iostream>
GameScreen screen;
SDL_Renderer* renderer;
SDL_Window* window;
bool gameRunning;
void run() {
initialize();
int startTicks, endTicks;
int delayTicks;
while (gameRunning) {
startTicks = SDL_GetTicks();
handleEvents();
update();
render();
endTicks = SDL_GetTicks();
delayTicks = 20 - (endTicks - startTicks);
if (delayTicks > 0) SDL_Delay(delayTicks);
}
destroy();
}
void initialize() {
gameRunning = false;
int height = 1080, width = 1920;
SDL_DisplayMode dm;
//SDL_GetDesktopDisplayMode(0, &dm/*);
//int height = dm.h, width = dm.w;*/
std::cout << height << ", " << width << "\n";
int flags = SDL_WINDOW_FULLSCREEN;
//int flags = 0;
if (SDL_Init(SDL_INIT_EVERYTHING) == 0 && IMG_Init(IMG_INIT_PNG)) {
window = SDL_CreateWindow("Shenzhen Solitaire", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, height, width, flags);
if (window) {
std::cout << "Window Created\n";
SDL_SetWindowMinimumSize(window, 100, 100);
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
std::cout << "Renderer Created\n";
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
}
}
else {
std::cout << "SDL Initialization failed, " << SDL_GetError() << "\n";
return;
}
SDL_Surface* image = IMG_Load("imgs/button-icon.png");
SDL_SetWindowIcon(window, image);
srand(time(NULL));
screen = Board;
gameRunning = true;
initBoard();
}
void handleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
gameRunning = false;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_q) {
gameRunning = false;
}
if (event.key.keysym.sym == SDLK_r) {
initBoard();
}
if (event.key.keysym.sym == SDLK_ESCAPE) {
switch (screen) {
case StartMenu:
gameRunning = false;
break;
case MenuOverlay:
screen = Board;
break;
case Board:
screen = MenuOverlay;
break;
}
}
break;
case SDL_MOUSEBUTTONDOWN:
handleMouseDown(event.button);
break;
case SDL_MOUSEBUTTONUP:
handleMouseUp(event.button);
break;
}
}
}
void handleMouseDown(SDL_MouseButtonEvent button) {
if (screen == Board) {
handleMouseDownBoard(button);
}
}
void handleMouseUp(SDL_MouseButtonEvent button) {
if (screen == Board) {
handleMouseUpBoard(button);
}
}
void update() {
if (screen == Board) {
updateBoard();
}
}
void render() {
switch (screen) {
case MenuOverlay:
renderMenuOverlay();
break;
case Board:
renderBoard();
break;
}
SDL_RenderPresent(renderer);
}
void destroy() {
if (renderer) {
SDL_DestroyRenderer(renderer);
}
if (window) {
SDL_DestroyWindow(window);
}
SDL_Quit();
}