Skip to content

Commit 13c705f

Browse files
committed
Add raygui.h that gives us the ability to easily add GUI elements.
Add a simple menu system according to game state.
1 parent a01e6cf commit 13c705f

File tree

6 files changed

+5617
-14
lines changed

6 files changed

+5617
-14
lines changed

src/Game.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
// Created by shehbaz on 12/6/2023.
33
//
44

5-
6-
75
#include <algorithm>
86

97
#include "Game.h"
108
#include "Grid.h"
119
#include "Cell.h"
1210
#include "DrawComponent.h"
1311

12+
#define RAYGUI_IMPLEMENTATION
13+
#include "raygui.h"
14+
1415
Game::Game(int screenWidth, int screenHeight, const char *title)
1516
:mScreenWidth(screenWidth)
1617
,mScreenHeight(screenHeight)
1718
,mTitle(title)
1819
,mIsRunning(true)
20+
,mGameState(INITIAL)
21+
,mGrid(nullptr)
1922
{
2023
}
2124

@@ -38,7 +41,10 @@ void Game::RunGame()
3841

3942
void Game::ProcessInput()
4043
{
41-
mGrid->ProcessInput(GetMouseX(), GetMouseY());
44+
if (mGameState == PLAYING)
45+
{
46+
mGrid->ProcessInput(GetMouseX(), GetMouseY());
47+
}
4248
}
4349

4450
void Game::UpdateGame()
@@ -52,10 +58,43 @@ void Game::UpdateGame()
5258
void Game::GenerateOutput()
5359
{
5460
BeginDrawing();
55-
ClearBackground(LIGHTGRAY);
56-
for (auto drawCom : mDraws)
61+
// Display menu screen according to game state
62+
switch (mGameState)
5763
{
58-
drawCom->Draw();
64+
case INITIAL:
65+
{
66+
float buttonWidth = 300.f, buttonHeight = 100.f;
67+
float padding = 75.f;
68+
ClearBackground(DARKGRAY);
69+
GuiSetStyle(DEFAULT, TEXT_SIZE, 40);
70+
GuiSetStyle(DEFAULT, TEXT_SPACING, 10);
71+
GuiSetStyle(BUTTON, BASE_COLOR_NORMAL, 0x000000FF);
72+
if (GuiButton({ (mScreenWidth / 2.f) - (buttonWidth / 2.f),
73+
(mScreenHeight / 2.f) - (buttonHeight / 2.f) - padding,
74+
buttonWidth,
75+
buttonHeight },
76+
"START"))
77+
{
78+
printf("START GAME!\n");
79+
mGameState = PLAYING;
80+
}
81+
if (GuiButton({ (mScreenWidth / 2.f) - (buttonWidth / 2.f),
82+
(mScreenHeight / 2.f) - (buttonHeight / 2.f) + buttonHeight,
83+
buttonWidth,
84+
buttonHeight },
85+
"EXIT"))
86+
{
87+
printf("EXIT GAME!\n");
88+
mIsRunning = false;
89+
}
90+
}
91+
break;
92+
case PLAYING:
93+
ClearBackground(LIGHTGRAY);
94+
for (auto drawCom : mDraws)
95+
{
96+
drawCom->Draw();
97+
}
5998
}
6099
EndDrawing();
61100
}
@@ -97,10 +136,8 @@ void Game::RemoveDraw(class DrawComponent *mesh)
97136
}
98137
}
99138

100-
bool Game::IsRunning()
139+
bool Game::IsRunning() const
101140
{
102-
// We can use this method to close the game window with additional conditions
103-
mIsRunning = !WindowShouldClose();
104141
return mIsRunning;
105142
}
106143

src/Game.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ class Game
3434
void AddDraw(class DrawComponent *mesh);
3535
void RemoveDraw(class DrawComponent *mesh);
3636

37+
// Set/Get Game state
38+
void SetGameState(GameState gameState) { mGameState = gameState; }
39+
[[nodiscard]] GameState GetGameState() const { return mGameState; }
40+
3741
// void AddFont(class Font* font);
3842
// void RemoveFont(class Font* font);
3943

4044
// Check if the game window is running
41-
bool IsRunning();
45+
[[nodiscard]] bool IsRunning() const;
4246

4347
private:
4448
// Game window specific

src/Grid.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void Grid::ToggleSeal(Cell* cell)
101101
else if (cell->GetCellType() == MINE)
102102
{
103103
cell->SetCellType(MINE_SEALED);
104+
CheckForWin();
104105
return;
105106
}
106107

@@ -138,7 +139,7 @@ void Grid::SetMines()
138139
}
139140
}
140141

141-
int Grid::GetAdjacentCellsInfo(Cell *cell, std::vector<Cell *>& adjacentCells)
142+
int Grid::GetAdjacentCells(Cell *cell, std::vector<Cell*>& adjacentCells)
142143
{
143144
int numOfMines = 0;
144145

@@ -201,12 +202,13 @@ void Grid::Expose(Cell *cell)
201202
{
202203
// End game
203204
printf("YOU CLICKED A MINE! GAME OVER!\n");
205+
// GetGame()->SetGameState(Game::GAME_OVER);
204206
}
205207
else if (cell->GetCellType() == UNEXPOSE)
206208
{
207209
// Check if its a normal cell
208210
std::vector<Cell*> adjacentCells;
209-
int numOfMines = GetAdjacentCellsInfo(cell, adjacentCells);
211+
int numOfMines = GetAdjacentCells(cell, adjacentCells);
210212
if (numOfMines > 0)
211213
{
212214
// This is an adjacent cell (Base case)
@@ -235,3 +237,18 @@ void Grid::Expose(Cell *cell)
235237
}
236238
}
237239
}
240+
241+
void Grid::CheckForWin()
242+
{
243+
for (auto mine : mMineList)
244+
{
245+
if (mine->GetCellType() == MINE)
246+
{
247+
// There is at least one mine cell that is not sealed
248+
return;
249+
}
250+
}
251+
// Once we get to this point, we know all mines are set to MINE_SEALED state.
252+
// Hence, set game condition to WIN
253+
// GetGame()->SetGameState(Game::WIN);
254+
}

src/Grid.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Grid : public Actor
2828
// void SetAdjacentCellsAround(Cell* cell);
2929
// void GetAdjacentCellsFor(Cell* cell, std::vector<class Cell*>& adjacentCells);
3030
void Expose(Cell* cell);
31-
int GetAdjacentCellsInfo(Cell* cell, std::vector<Cell*>& adjacentCells);
31+
int GetAdjacentCells(Cell* cell, std::vector<Cell*>& adjacentCells);
3232
void ToggleSeal(Cell *cell);
33+
34+
void CheckForWin();
3335
};

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ int main()
55
Game game(1280, 960, "Minesweeper");
66
game.Initialize();
77

8-
while (game.IsRunning())
8+
while (!WindowShouldClose() && game.IsRunning())
99
{
1010
// Run main game loop
1111
game.RunGame();

0 commit comments

Comments
 (0)