-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
267 lines (245 loc) · 6.6 KB
/
game.cpp
File metadata and controls
267 lines (245 loc) · 6.6 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "game.hpp"
#include "UNO.hpp"
#include "drawing.hpp"
using namespace std;
SDL_Renderer* Drawing::gRenderer = NULL;
SDL_Texture* Drawing::assets = NULL;
bool Game::init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
//Create window
gWindow = SDL_CreateWindow( "UNO Revived", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
Drawing::gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
if( Drawing::gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( Drawing::gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
}
}
}
return success;
}
bool Game::loadMedia()
{
//Loading success flag
bool success = true;
Drawing::assets = loadTexture("assets.png");
gTexture = loadTexture("starting screen.png");
if(Drawing::assets==NULL || gTexture==NULL)
{
printf("Unable to run due to error: %s\n",SDL_GetError());
success =false;
}
s->starting_music("game loaded");
return success;
}
void Game::close()
{
//Free loaded images
SDL_DestroyTexture(Drawing::assets);
Drawing::assets=NULL;
SDL_DestroyTexture(gTexture);
//Destroy window
SDL_DestroyRenderer( Drawing::gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
Drawing::gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
SDL_Texture* Game::loadTexture( std::string path )
{
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
}
else
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( Drawing::gRenderer, loadedSurface );
if( newTexture == NULL )
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
return newTexture;
}
void Game::run( )
{
string status = "menu";
string background = "starting screen.png";
// bool wild_screen = false;
bool quit = false;
SDL_Event e;
// UNO& uno {UNO::get_instance()};
uno.game_initialization();
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
if(e.type == SDL_MOUSEBUTTONDOWN){
int xMouse, yMouse;
SDL_GetMouseState(&xMouse,&yMouse);
cout << xMouse << " & " << yMouse << endl;
if (status == "menu")
{
if (yMouse > 534 && yMouse < 599)
{
if (xMouse > 392 && xMouse < 500)
{
background = "background choosing.png";
gTexture = loadTexture(background);
status = "choosing background";
}
else if (xMouse > 562 && xMouse < 670)
{
background = "UNO Rules.png";
gTexture = loadTexture(background);
status = "reading instructions";
}
}
else if (xMouse > 131 && xMouse < 235 && yMouse > 15 && yMouse < 77)
{
quit = true;
}
}
if (status == "reading instructions")
{
if (xMouse > 23 && xMouse < 296 && yMouse > 561 && yMouse < 594)
{
if (xMouse > 35 && xMouse < 171 && yMouse > 551 && yMouse < 596)
{
background = "background choosing.png";
gTexture = loadTexture(background);
status = "choosing background";
}
}
}
if (status == "choosing background")
{
if (xMouse > 35 && xMouse < 459 && yMouse > 151 && yMouse < 392)
{
s->starting_music(false);
background = "grass background.png";
gTexture = loadTexture(background);
status = "game started";
s->mid_game(true);
}
else if (xMouse > 555 && xMouse < 971 && yMouse > 154 && yMouse < 384)
{
s->starting_music(false);
background = "wooden background.png";
gTexture = loadTexture(background);
status = "game started";
s->mid_game(true);
}
else if (xMouse > 119 && xMouse < 229 && yMouse > 13 && yMouse < 79)
{
quit = true;
}
}
if (status == "game started")
{
// string card_checker;
// bool wild_screen = false;
uno.Click(xMouse, yMouse);
if (xMouse > 25 && xMouse < 189 && yMouse > 20 && yMouse < 73)
{
quit = true;
}
// card_checker = uno.wild_card_check (Card * card, string player)
if (uno.get_wild_by_human_signal() || uno.get_draw_four_by_human_signal())
{
if (uno.get_wild_by_human_signal())
{
gTexture = loadTexture("Wild.png");
}
else if (uno.get_draw_four_by_human_signal())
{
gTexture = loadTexture("Draw_4.png");
}
}
else
{
gTexture = loadTexture(background);
}
if (uno.get_winner() == "Human" || uno.get_winner() == "PC")
{
s->mid_game(false);
status = "game concluded";
}
}
if (status == "game concluded")
{
if (uno.get_winner() == "Human")
{
gTexture = loadTexture("won.png");
s->winsound();
}
else if (uno.get_winner() == "PC")
{
gTexture = loadTexture("lost.png");
s->losemusic();
}
}
}
}
SDL_RenderClear(Drawing::gRenderer); //removes everything from renderer
SDL_RenderCopy(Drawing::gRenderer, gTexture, NULL, NULL);//Draws background to renderer
//***********************draw the objects here********************
if (status == "game started" && !uno.get_wild_by_human_signal() && !uno.get_draw_four_by_human_signal())
{
uno.drawObjects();
}
//****************************************************************
SDL_RenderPresent(Drawing::gRenderer); //displays the updated renderer
SDL_Delay(100); //causes sdl engine to delay for specified miliseconds
}
}