Skip to content

Commit

Permalink
Minor refactor and warning fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ufrshubham committed Jun 30, 2024
1 parent 13abc82 commit 383ac7b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 40 deletions.
10 changes: 5 additions & 5 deletions include/AssetMan.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <map>
#include <unordered_map>
#include <memory>
#include <string>

Expand All @@ -14,10 +14,10 @@ namespace Engine
class AssetMan
{
private:
std::map<int, std::unique_ptr<sf::Texture>> m_textures;
std::map<int, std::unique_ptr<sf::Font>> m_fonts;
std::map<int, std::unique_ptr<sf::Music>> m_soundTracks;
std::map<int, std::unique_ptr<sf::SoundBuffer>> m_soundEffects;
std::unordered_map<int, std::unique_ptr<sf::Texture>> m_textures;
std::unordered_map<int, std::unique_ptr<sf::Font>> m_fonts;
std::unordered_map<int, std::unique_ptr<sf::Music>> m_soundTracks;
std::unordered_map<int, std::unique_ptr<sf::SoundBuffer>> m_soundEffects;

public:
AssetMan();
Expand Down
2 changes: 2 additions & 0 deletions include/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Game

void Run();

static const int TILE_SIZE = 16;

private:
void ModifyCurrentWorkingDirectory() const;
};
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
### Features

- Snake can be controlled with arrow keys
- Snake automatically advances 16px
- Snake automatically advances with a fixed tile size
- Food gets randomly placed
- Snake can eat food to grow in length
- Each food increase player score by 1 point
Expand Down
12 changes: 6 additions & 6 deletions src/GameOver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ void GameOver::Init()
m_gameOverTitle.setString("Game Over");
m_gameOverTitle.setOrigin(m_gameOverTitle.getLocalBounds().width / 2,
m_gameOverTitle.getLocalBounds().height / 2);
m_gameOverTitle.setPosition(m_context->m_window->getSize().x / 2,
m_context->m_window->getSize().y / 2 - 150.f);
m_gameOverTitle.setPosition((float)m_context->m_window->getSize().x / 2,
(float)m_context->m_window->getSize().y / 2 - 150.f);

// Play Button
m_retryButton.setFont(m_context->m_assets->GetFont(MAIN_FONT));
m_retryButton.setString("Retry");
m_retryButton.setOrigin(m_retryButton.getLocalBounds().width / 2,
m_retryButton.getLocalBounds().height / 2);
m_retryButton.setPosition(m_context->m_window->getSize().x / 2,
m_context->m_window->getSize().y / 2 - 25.f);
m_retryButton.setPosition((float)m_context->m_window->getSize().x / 2,
(float)m_context->m_window->getSize().y / 2 - 25.f);
m_retryButton.setCharacterSize(20);

// Exit Button
m_exitButton.setFont(m_context->m_assets->GetFont(MAIN_FONT));
m_exitButton.setString("Exit");
m_exitButton.setOrigin(m_exitButton.getLocalBounds().width / 2,
m_exitButton.getLocalBounds().height / 2);
m_exitButton.setPosition(m_context->m_window->getSize().x / 2,
m_context->m_window->getSize().y / 2 + 25.f);
m_exitButton.setPosition((float)m_context->m_window->getSize().x / 2,
(float)m_context->m_window->getSize().y / 2 + 25.f);
m_exitButton.setCharacterSize(20);

m_deathSfx.setBuffer(m_context->m_assets->GetSoundEffect(DEATH_SFX));
Expand Down
34 changes: 17 additions & 17 deletions src/GamePlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
GamePlay::GamePlay(std::shared_ptr<Context> &context)
: m_context(context),
m_score(0),
m_snakeDirection({16.f, 0.f}),
m_newSnakeDirection({16.f, 0.f}),
m_snakeDirection({Game::TILE_SIZE, 0.f}),
m_newSnakeDirection({Game::TILE_SIZE, 0.f}),
m_elapsedTime(sf::Time::Zero),
m_isPaused(false),
m_bgm(m_context->m_assets->GetSoundTrack(MAIN_SOUND_TRACK))
{
srand(time(nullptr));
srand((unsigned int)time(nullptr));
}

GamePlay::~GamePlay()
Expand All @@ -38,16 +38,16 @@ void GamePlay::Init()
wall.setTexture(m_context->m_assets->GetTexture(WALL));
}

m_walls[0].setTextureRect({0, 0, (int)m_context->m_window->getSize().x, 16});
m_walls[1].setTextureRect({0, 0, (int)m_context->m_window->getSize().x, 16});
m_walls[1].setPosition(0, m_context->m_window->getSize().y - 16);
m_walls[0].setTextureRect({0, 0, (int)m_context->m_window->getSize().x, Game::TILE_SIZE});
m_walls[1].setTextureRect({0, 0, (int)m_context->m_window->getSize().x, Game::TILE_SIZE});
m_walls[1].setPosition(0, m_context->m_window->getSize().y - (float)Game::TILE_SIZE);

m_walls[2].setTextureRect({0, 0, 16, (int)m_context->m_window->getSize().y});
m_walls[3].setTextureRect({0, 0, 16, (int)m_context->m_window->getSize().y});
m_walls[3].setPosition(m_context->m_window->getSize().x - 16, 0);
m_walls[2].setTextureRect({0, 0, Game::TILE_SIZE, (int)m_context->m_window->getSize().y});
m_walls[3].setTextureRect({0, 0, Game::TILE_SIZE, (int)m_context->m_window->getSize().y});
m_walls[3].setPosition(m_context->m_window->getSize().x - (float)Game::TILE_SIZE, 0);

m_food.setTexture(m_context->m_assets->GetTexture(FOOD));
m_food.setPosition(m_context->m_window->getSize().x / 2, m_context->m_window->getSize().y / 2);
m_food.setPosition((float)m_context->m_window->getSize().x / 2, (float)m_context->m_window->getSize().y / 2);

m_snake.Init(m_context->m_assets->GetTexture(SNAKE));

Expand All @@ -73,16 +73,16 @@ void GamePlay::ProcessInput()
switch (event.key.code)
{
case sf::Keyboard::Up:
m_newSnakeDirection = {0.f, -16.f};
m_newSnakeDirection = {0.f, -Game::TILE_SIZE};
break;
case sf::Keyboard::Down:
m_newSnakeDirection = {0.f, 16.f};
m_newSnakeDirection = {0.f, Game::TILE_SIZE};
break;
case sf::Keyboard::Left:
m_newSnakeDirection = {-16.f, 0.f};
m_newSnakeDirection = {-Game::TILE_SIZE, 0.f};
break;
case sf::Keyboard::Right:
m_newSnakeDirection = {16.f, 0.f};
m_newSnakeDirection = {Game::TILE_SIZE, 0.f};
break;
case sf::Keyboard::Escape:
m_context->m_states->Add(std::make_unique<PauseGame>(m_context));
Expand Down Expand Up @@ -126,10 +126,10 @@ void GamePlay::Update(const sf::Time& deltaTime)

int x = 0,
y = 0;
x = std::clamp<int>(rand() % m_context->m_window->getSize().x, 16, m_context->m_window->getSize().x - 2 * 16);
y = std::clamp<int>(rand() % m_context->m_window->getSize().y, 16, m_context->m_window->getSize().y - 2 * 16);
x = std::clamp<int>(rand() % m_context->m_window->getSize().x, Game::TILE_SIZE, m_context->m_window->getSize().x - 2 * Game::TILE_SIZE);
y = std::clamp<int>(rand() % m_context->m_window->getSize().y, Game::TILE_SIZE, m_context->m_window->getSize().y - 2 * Game::TILE_SIZE);

m_food.setPosition(x, y);
m_food.setPosition((float)x, (float)y);
m_score += 1;
m_scoreText.setString("Score : " + std::to_string(m_score));
}
Expand Down
12 changes: 6 additions & 6 deletions src/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ void MainMenu::Init()
m_gameTitle.setString("Snake Game");
m_gameTitle.setOrigin(m_gameTitle.getLocalBounds().width / 2,
m_gameTitle.getLocalBounds().height / 2);
m_gameTitle.setPosition(m_context->m_window->getSize().x / 2,
m_context->m_window->getSize().y / 2 - 150.f);
m_gameTitle.setPosition((float)m_context->m_window->getSize().x / 2,
(float)m_context->m_window->getSize().y / 2 - 150.f);

// Play Button
m_playButton.setFont(m_context->m_assets->GetFont(MAIN_FONT));
m_playButton.setString("Play");
m_playButton.setOrigin(m_playButton.getLocalBounds().width / 2,
m_playButton.getLocalBounds().height / 2);
m_playButton.setPosition(m_context->m_window->getSize().x / 2,
m_context->m_window->getSize().y / 2 - 25.f);
m_playButton.setPosition((float)m_context->m_window->getSize().x / 2,
(float)m_context->m_window->getSize().y / 2 - 25.f);
m_playButton.setCharacterSize(20);

// Exit Button
m_exitButton.setFont(m_context->m_assets->GetFont(MAIN_FONT));
m_exitButton.setString("Exit");
m_exitButton.setOrigin(m_exitButton.getLocalBounds().width / 2,
m_exitButton.getLocalBounds().height / 2);
m_exitButton.setPosition(m_context->m_window->getSize().x / 2,
m_context->m_window->getSize().y / 2 + 25.f);
m_exitButton.setPosition((float)m_context->m_window->getSize().x / 2,
(float)m_context->m_window->getSize().y / 2 + 25.f);
m_exitButton.setCharacterSize(20);
}

Expand Down
4 changes: 2 additions & 2 deletions src/PauseGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void PauseGame::Init()
m_pauseTitle.setString("Paused");
m_pauseTitle.setOrigin(m_pauseTitle.getLocalBounds().width / 2,
m_pauseTitle.getLocalBounds().height / 2);
m_pauseTitle.setPosition(m_context->m_window->getSize().x / 2,
m_context->m_window->getSize().y / 2);
m_pauseTitle.setPosition((float)m_context->m_window->getSize().x / 2,
(float)m_context->m_window->getSize().y / 2);
}

void PauseGame::ProcessInput()
Expand Down
7 changes: 4 additions & 3 deletions src/Snake.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Snake.hpp"
#include "Game.hpp"

Snake::Snake() : m_body(std::list<sf::Sprite>(4))
{
Expand All @@ -12,12 +13,12 @@ Snake::~Snake()

void Snake::Init(const sf::Texture &texture)
{
float x = 16.f;
float x = Game::TILE_SIZE;
for (auto &piece : m_body)
{
piece.setTexture(texture);
piece.setPosition({x, 16.f});
x += 16.f;
piece.setPosition({x, (float)Game::TILE_SIZE});
x += Game::TILE_SIZE;
}
}

Expand Down

0 comments on commit 383ac7b

Please sign in to comment.