-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
156 lines (122 loc) · 3 KB
/
Copy pathMain.cpp
File metadata and controls
156 lines (122 loc) · 3 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
151
152
153
154
155
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "LoadTex.h"
#include "LoadTimer.h"
#include "StateMachines.h"
#include "Options.h"
#include "Game.h"
#include <string>
SDL_Window* mainWindow = nullptr;
SDL_Renderer* mainRenderer = nullptr;
void init(int scrWidth, int scrHeight)
{
//init SDL Subsystem
SDL_Init(SDL_INIT_VIDEO);
//init Window system
mainWindow = SDL_CreateWindow("NN's Minesweeper Clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, scrWidth, scrHeight, SDL_WINDOW_SHOWN | SDL_RENDERER_PRESENTVSYNC);
//init Renderer
mainRenderer = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(mainRenderer, 0x00, 0x00, 0x00, 0xff);
//init SDL_IMG
int imgFlags = IMG_INIT_PNG;
IMG_Init(imgFlags);
//init SDL_TTF
TTF_Init();
}
void destroyBeforeClose(SDL_Window* window, SDL_Renderer*& renderer)
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
TTF_Quit();
}
int main(int args, char* argv[])
{
//pre-initialization (game-specific)
APPSTATE state(APPSTATE::INIT);
//figure out where resolution should be delt with
int screenWidth = 1050;
int screenHeight = 630;
//initialize game and event system
Game game;
SDL_Event ev;
//initialize window
init(screenWidth, screenHeight);
//Modify state machine to handle application state.
state = APPSTATE::GAME;
//select the renderer for the scene
game.SelectRenderer(*mainRenderer);
game.selectEventSystem(ev);
//load objects for renderer
game.LoadObjects();
game.SetDifficulty();
Options options;
options.SelectRenderer(*mainRenderer);
options.selectEventSystem(ev);
options.LoadObjects();
SDL_Rect frameDebugger = { 20, screenHeight - 50, 32, 32 };
while (state != APPSTATE::EXIT)
{
while (SDL_PollEvent(&ev) != 0)
{
if (ev.type == SDL_QUIT)
{
state = APPSTATE::EXIT;
}
else
{
if (state == APPSTATE::GAME)
game.RunEvents();
if (state == APPSTATE::OPTIONS)
options.RunEvents();
}
}
SDL_SetRenderDrawColor(mainRenderer, 0x00, 0x00, 0x00, 0xff);
SDL_RenderClear(mainRenderer);
if (game.optionsSelected == true)
{
state = APPSTATE::OPTIONS;
}
if (state == APPSTATE::GAME)
{
game.Update();
game.RunRenderer();
}
if (state == APPSTATE::OPTIONS)
{
game.RunRenderer();
options.RunRenderer();
}
if (options.done == true)
{
state = APPSTATE::GAME;
game.optionsSelected = false;
if (options.diff == 1)
{
game.gameDifficulty = Difficulty::EASY;
}
if (options.diff == 2)
{
game.gameDifficulty = Difficulty::NORMAL;
}
if (options.diff == 3)
{
game.gameDifficulty = Difficulty::HARD;
}
game.ResetGame();
options.done = false;
}
if (options.cancelled == true)
{
state = APPSTATE::GAME;
game.optionsSelected = false;
options.cancelled = false;
}
SDL_RenderPresent(mainRenderer);
}
destroyBeforeClose(mainWindow, mainRenderer);
return 0;
}