Skip to content

Commit

Permalink
Add ability to continue and update textures
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-minh committed May 18, 2022
1 parent 7555bf9 commit e4caf55
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 107 deletions.
Binary file modified assets/endBackground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/exit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/startBackground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions include/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Texture.h"
#include "Text.h"
#include "Sound.h"
#include <vector>
using std::vector;

//Jewels in squares
enum Jewels {Destroyed, Red, Green, Blue, Orange, Yellow, Purple, White, Total};
Expand All @@ -13,6 +15,9 @@ enum GameModes {Time, Zen, Endless, Total_Mode};
//Time modes
enum TimeModes {OneMinute, TwoMinutes, FiveMinutes, Total_Time};

//Change selection
enum SelectionChange {ContinueSelection, NewGameSelection, GameSelection, TimeSelection, Total_Selection};

class Engine
{
private:
Expand Down Expand Up @@ -49,6 +54,10 @@ class Engine

//Saved high score from disk
Sint32 savedHighscore[Total_Mode][Total_Time];
//Saved state
Sint32 savedScore;
Uint32 savedTime;
vector<vector<int> > savedBoard;

Timer timer;

Expand All @@ -65,6 +74,8 @@ class Engine
Texture endTexture; //End screen texture

//Texts
Text continueText;
Text newGameText;
Text gameModeText; //Game modes
Text timeModeText; //Time modes
Text scores; //Score
Expand Down
3 changes: 0 additions & 3 deletions include/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class Game
//Check if game is running
bool running;

//Check if choose to exit
bool exit;

static Uint32 callback(Uint32 interval, void* param);

public:
Expand Down
4 changes: 2 additions & 2 deletions include/GameBoard.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef GAME_BOARD_H
#define GAME_BOARD_H
#include "Engine.h"
#include <vector>
using std::vector;

class GameBoard
{
Expand Down Expand Up @@ -54,6 +52,8 @@ class GameBoard
void scoreCalculate();

//Mode select
SDL_Rect continueSelect;
SDL_Rect newGameSelect;
SDL_Rect modeSelect;
SDL_Rect timeSelect;

Expand Down
3 changes: 3 additions & 0 deletions include/Jewels.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Jewel : private GameBoard

//Update game state
void updateGame();

//Save game state
void saveState();
};

#endif
9 changes: 7 additions & 2 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ extern SDL_Window* window;
extern SDL_Renderer* renderer;

//Game state
extern bool gameStarted, gameover;
extern bool gameover;

//Check if a jewel was selected
extern bool pressed, selected;

//Check if forced to quit
extern bool forceQuit;

//Change mode selection
extern bool selectChange;
extern int selectChange;

//Game mode
extern int gameMode;
extern std::string game_mode[];
//Time mode
extern int timeMode;
extern int time_mode[];

//High score
extern Sint32* highscore;
Expand Down
29 changes: 26 additions & 3 deletions src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

Engine::Engine() : WINDOW_WIDTH(800), WINDOW_HEIGHT(600), TITLE("Jewel Match")
{
savedBoard.resize(8, vector<int>(8));
success = true;
if( !init() ) {
Error("Unable to initialize Engine!");
Expand Down Expand Up @@ -121,12 +122,14 @@ bool Engine::initTexture()
bool Engine::initFont()
{
//Open font
if( !gameModeText.openFont(30) || !timeModeText.openFont(23) || !scoreText.openFont(30) || !highscoreText.openFont(30) || !timeText.openFont(30) ||
!scores.openFont(35) || !highscores.openFont(35) || !times.openFont(75) || !startNotice.openFont(100))
if( !continueText.openFont(30) || !newGameText.openFont(30) || !gameModeText.openFont(25) || !timeModeText.openFont(20) ||
!scoreText.openFont(30) || !highscoreText.openFont(30) || !timeText.openFont(30) || !scores.openFont(35) ||
!highscores.openFont(35) || !times.openFont(75) || !startNotice.openFont(100))
return false;

//Load static text
else if(!scoreText.loadText("score") || !highscoreText.loadText("high score") ||
else if(!continueText.loadText("Continue") || !newGameText.loadText("New Game") ||
!scoreText.loadText("score") || !highscoreText.loadText("high score") ||
!timeText.loadText("time") || !startNotice.loadText("START"))
return false;

Expand Down Expand Up @@ -163,6 +166,16 @@ void Engine::initSave()
SDL_RWread(save, &savedHighscore[i][j], sizeof(Sint32), 1);
}
}
SDL_RWread(save, &forceQuit, sizeof(bool), 1);
if(forceQuit) {
SDL_RWread(save, &savedTime, sizeof(Uint32), 1);
SDL_RWread(save, &savedScore, sizeof(Sint32), 1);
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
SDL_RWread(save, &savedBoard[i][j], sizeof(int), 1);
}
}
}

//Close file handler
SDL_RWclose(save);
Expand All @@ -183,6 +196,16 @@ bool Engine::save()
SDL_RWwrite(save, &savedHighscore[i][j], sizeof(Sint32), 1);
}
}
SDL_RWwrite(save, &forceQuit, sizeof(bool), 1);
if(forceQuit) {
SDL_RWwrite(save, &savedTime, sizeof(Uint32), 1);
SDL_RWwrite(save, &savedScore, sizeof(Sint32), 1);
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
SDL_RWwrite(save, &savedBoard[i][j], sizeof(int), 1);
}
}
}

//Close file handler
SDL_RWclose(save);
Expand Down
109 changes: 66 additions & 43 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Game::Game(const int &nRows, const int &nCols) : jewel(nRows, nCols), nRows(nRows), nCols(nCols)
{
gameStarted = exit = false;
running = true;
startGame();

Expand All @@ -12,46 +11,68 @@ Game::Game(const int &nRows, const int &nCols) : jewel(nRows, nCols), nRows(nRow

void Game::startGame()
{
while(!gameStarted && running && SDL_WaitEvent(&e)) {
while(running && SDL_WaitEvent(&e)) {
if(e.type == SDL_QUIT)
running = false;
else {
jewel.renderStart();
if(e.type == SDL_MOUSEMOTION || e.type == SDL_MOUSEBUTTONDOWN) {
SDL_GetMouseState(&mousePos.x, &mousePos.y);
if(SDL_PointInRect(&mousePos, &jewel.modeSelect)) {
selectChange = false;
if(forceQuit) {
if(SDL_PointInRect(&mousePos, &jewel.continueSelect)) {
selectChange = ContinueSelection;
if(e.type == SDL_MOUSEBUTTONDOWN) {
start();
return;
}
}
}
if(SDL_PointInRect(&mousePos, &jewel.newGameSelect)) {
selectChange = NewGameSelection;
if(e.type == SDL_MOUSEBUTTONDOWN) {
forceQuit = false;
start();
return;
}
}
else if(SDL_PointInRect(&mousePos, &jewel.modeSelect)) {
selectChange = GameSelection;
if(e.type == SDL_MOUSEBUTTONDOWN)
gameMode = (gameMode + 1) % Total_Mode;
}
else if(gameMode == Time && SDL_PointInRect(&mousePos, &jewel.timeSelect)) {
selectChange = true;
selectChange = TimeSelection;
if(e.type == SDL_MOUSEBUTTONDOWN)
timeMode = (timeMode + 1) % Total_Time;
}
}
else if(e.type == SDL_KEYDOWN) {
switch(e.key.keysym.sym) {
case SDLK_s: case SDLK_w: case SDLK_DOWN: case SDLK_UP:
if(gameMode == Time)
selectChange ? selectChange = false : selectChange = true;
case SDLK_s: case SDLK_DOWN:
selectChange = (selectChange + 1) % Total_Selection;
break;

case SDLK_w: case SDLK_UP:
selectChange = (Total_Selection + (selectChange - 1)) % Total_Selection;
break;

case SDLK_RIGHT: case SDLK_d:
if(!selectChange)
if(selectChange == GameSelection)
gameMode = (gameMode + 1) % Total_Mode;
else timeMode = (timeMode + 1) % Total_Time;
else if(selectChange == TimeSelection)
timeMode = (timeMode + 1) % Total_Time;
break;

case SDLK_LEFT: case SDLK_a:
if(!selectChange)
if(selectChange == GameSelection)
gameMode = (Total_Mode + (gameMode - 1)) % Total_Mode;
else timeMode = (Total_Time + (timeMode - 1)) % Total_Time;
else if(selectChange == TimeSelection)
timeMode = (Total_Time + (timeMode - 1)) % Total_Time;
break;

case SDLK_RETURN:
start();
break;
return;
}
}
}
Expand All @@ -60,40 +81,38 @@ void Game::startGame()

void Game::endGame()
{
if(gameStarted) {
gameStarted = false;
jewel.engine.music.stopMusic();
if(!exit) {
jewel.renderEnd();
jewel.engine.endSFX.playSFX();
}
}
if(exit) {
jewel.engine.music.stopMusic();
if(forceQuit) {
startGame();
return;
}
if(e.type == SDL_KEYDOWN) {
if(e.key.keysym.sym == SDLK_ESCAPE) {
startGame();
return;
}
else if(e.key.keysym.sym == SDLK_RETURN) {
start();
return;
else {
jewel.renderEnd();
jewel.engine.endSFX.playSFX();
}
while(SDL_WaitEvent(&e)) {
if(e.type == SDL_KEYDOWN) {
if(e.key.keysym.sym == SDLK_ESCAPE) {
startGame();
return;
}
else if(e.key.keysym.sym == SDLK_RETURN) {
start();
return;
}
}
}
}

void Game::start()
{
gameover = exit = false;
gameover = false;
if(gameMode == Time)
highscore = &jewel.engine.savedHighscore[Time][timeMode];
else highscore = &jewel.engine.savedHighscore[gameMode][0];

jewel.engine.startSFX.playSFX();
jewel.startNotice();
gameStarted = true;
jewel.engine.music.playMusic();
timerID = SDL_AddTimer(1000, callback, NULL);
jewel.randomize();
Expand All @@ -103,22 +122,25 @@ void Game::start()
void Game::run()
{
while(running && SDL_WaitEvent(&e)) {
if(e.type == SDL_QUIT)
if(e.type == SDL_QUIT) {
running = false;
if(!jewel.existHint()) {
if(gameMode == Zen)
gameover = true;
else jewel.randomize();
forceQuit = true;
jewel.saveState();
}
if(gameover) {
if(gameStarted) {
SDL_RemoveTimer(timerID);
if(!jewel.existMatch()) {
SDL_Delay(400);
}
jewel.hint.stop();
jewel.needHint = false;
SDL_RemoveTimer(timerID);
if(!jewel.existMatch()) {
SDL_Delay(400);
}
endGame();
}
else if(!jewel.existHint()) {
if(gameMode == Zen)
gameover = true;
else jewel.randomize();
}
else {
if(e.type == SDL_KEYDOWN) {
if(e.key.keysym.sym == SDLK_ESCAPE)
Expand All @@ -134,7 +156,8 @@ void Game::run()
pressed = true;
SDL_GetMouseState(&mousePos.x, &mousePos.y);
if(e.type == SDL_MOUSEBUTTONDOWN && SDL_PointInRect(&mousePos, &jewel.exit)) {
gameover = exit = true;
gameover = forceQuit = true;
jewel.saveState();
}
for(int x_ = 0; x_ < nRows; x_++) {
for(int y_ = 0; y_ < nCols; y_++) {
Expand Down
Loading

0 comments on commit e4caf55

Please sign in to comment.