Skip to content

Commit 8683dad

Browse files
committed
Some optimizations
1 parent 95b051f commit 8683dad

8 files changed

Lines changed: 8 additions & 16 deletions

File tree

include/Apple.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Apple
99
Apple() : mPosX(0), mPosY(0), mVisible(false) {}
1010

1111
void Reset();
12-
void Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager);
12+
void Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager) const;
1313
void SetX(int pos);
1414
void SetY(int pos);
1515
void SetVisibility(bool visible);

include/AssetsManager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class AssetsManager
1111
public:
1212
void LoadAssets(SDL_Renderer* renderer, std::filesystem::path path);
1313
void UnloadAssets();
14-
SDL_Texture* GetTexture(std::string textureId) const;
15-
TTF_Font* GetFont(std::string fontId) const;
14+
SDL_Texture* GetTexture(const std::string& textureId) const;
15+
TTF_Font* GetFont(const std::string& fontId) const;
1616

1717
private:
1818
std::unordered_map<std::string, SDL_Texture*> mTextures;

include/Snake.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Snake
2222
void Move();
2323
void Grow();
2424
void Turn(Direction direction);
25-
void Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager);
25+
void Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager) const;
2626
bool IsSelfOrWallColliding() const;
2727
int PosX() const;
2828
int PosY() const;

src/Apple.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void Apple::Reset()
88
mVisible = false;
99
}
1010

11-
void Apple::Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager)
11+
void Apple::Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager) const
1212
{
1313
if (!mVisible)
1414
return;

src/AssetsManager.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ void AssetsManager::LoadAssets(SDL_Renderer* renderer, std::filesystem::path dir
1818
if (extension == ".png")
1919
{
2020
SDL_Texture* texture = IMG_LoadTexture(renderer, path.c_str());
21-
2221
if (!texture)
2322
{
2423
std::cerr << "Failed to load texture " << path << ": " << SDL_GetError() << std::endl;
@@ -31,7 +30,6 @@ void AssetsManager::LoadAssets(SDL_Renderer* renderer, std::filesystem::path dir
3130
if (extension == ".ttf")
3231
{
3332
TTF_Font* font = TTF_OpenFont(path.c_str(), 24);
34-
3533
if (!font)
3634
{
3735
std::cerr << "Failed to load font " << path << ": " << SDL_GetError() << std::endl;
@@ -55,10 +53,9 @@ void AssetsManager::UnloadAssets()
5553
mFonts.clear();
5654
}
5755

58-
SDL_Texture* AssetsManager::GetTexture(std::string textureId) const
56+
SDL_Texture* AssetsManager::GetTexture(const std::string& textureId) const
5957
{
6058
auto texture = mTextures.find(textureId);
61-
6259
if (texture == mTextures.end())
6360
{
6461
std::cerr << textureId << " is an invalid texture identifier!" << std::endl;
@@ -68,10 +65,9 @@ SDL_Texture* AssetsManager::GetTexture(std::string textureId) const
6865
return texture->second;
6966
}
7067

71-
TTF_Font* AssetsManager::GetFont(std::string fontId) const
68+
TTF_Font* AssetsManager::GetFont(const std::string& fontId) const
7269
{
7370
auto font = mFonts.find(fontId);
74-
7571
if (font == mFonts.end())
7672
{
7773
std::cerr << fontId << " is an invalid font identifier!" << std::endl;

src/Game.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ void Game::Init()
2727

2828
mWindow = SDL_CreateWindow(Config::GameTitle, Config::ScreenX, Config::ScreenY,
2929
Config::ScreenWidth, Config::ScreenHeight, Config::WindowFlags);
30-
3130
if (!mWindow)
3231
{
3332
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
@@ -36,7 +35,6 @@ void Game::Init()
3635
}
3736

3837
mRenderer = SDL_CreateRenderer(mWindow, -1, Config::RendererFlags);
39-
4038
if (!mRenderer)
4139
{
4240
std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl;
@@ -72,7 +70,6 @@ void Game::Run()
7270
RenderScene();
7371

7472
frameTime = SDL_GetTicks64() - frameStart;
75-
7673
if (frameTime < FrameDuration)
7774
SDL_Delay(FrameDuration - frameTime);
7875
}

src/Main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <SDL.h>
21
#include "Game.hpp"
32

43
int main(int argc, char** argv)

src/Snake.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void Snake::Turn(Direction direction)
8282
mNextDirection = direction;
8383
}
8484

85-
void Snake::Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager)
85+
void Snake::Draw(SDL_Renderer* renderer, const AssetsManager& assetsManager) const
8686
{
8787
SDL_Texture* curveTexture = assetsManager.GetTexture("SnakeCurve");
8888
SDL_Texture* headTexture = assetsManager.GetTexture("SnakeHead");

0 commit comments

Comments
 (0)