Skip to content

Commit

Permalink
fixed resources, fixed penguin deceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-tomas committed Jul 20, 2022
1 parent 47d6307 commit fc6f717
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
16 changes: 8 additions & 8 deletions header/Resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ using namespace std;

class Resources {
public:
static SDL_Texture* GetImage(const char* file);
static Mix_Music* GetMusic(const char* file);
static Mix_Chunk* GetSound(const char* file);
static TTF_Font* GetFont(const char* file, int fontSize);
static SDL_Texture* GetImage(string file);
static Mix_Music* GetMusic(string file);
static Mix_Chunk* GetSound(string file);
static TTF_Font* GetFont(string file, int fontSize);
static void ClearImages();
static void ClearMusics();
static void ClearSounds();
static void ClearFonts();

private:
static unordered_map<const char*, SDL_Texture*> imageTable;
static unordered_map<const char*, Mix_Music*> musicTable;
static unordered_map<const char*, Mix_Chunk*> soundTable;
static unordered_map<const char*, TTF_Font*> fontTable;
static unordered_map<string, SDL_Texture*> imageTable;
static unordered_map<string, Mix_Music*> musicTable;
static unordered_map<string, Mix_Chunk*> soundTable;
static unordered_map<string, TTF_Font*> fontTable;
};

#endif // RESOURCES_H
8 changes: 4 additions & 4 deletions src/PenguinBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ void PenguinBody::Update(float dt)
pCannon->Shoot();
}

// @TODO: fix FPS issue
float acc = 500.0;
float dec = 300.0;
float maxSpeed = 550.0;

// Accelerates
Expand All @@ -85,9 +85,9 @@ void PenguinBody::Update(float dt)
if (!InputManager::GetInstance().IsKeyDown(W_KEY) &&
!InputManager::GetInstance().IsKeyDown(S_KEY))
{
linearSpeed = linearSpeed > 0 ? linearSpeed - acc * dt : linearSpeed;
linearSpeed = linearSpeed < 0 ? linearSpeed + acc * dt : linearSpeed;
linearSpeed = abs(linearSpeed) <= 0.5 ? 0 : linearSpeed;
linearSpeed = linearSpeed > 0 ? linearSpeed - dec * dt : linearSpeed;
linearSpeed = linearSpeed < 0 ? linearSpeed + dec * dt : linearSpeed;
linearSpeed = abs(linearSpeed) <= 5.0 ? 0 : linearSpeed - 1.0;
}

// Updates sprite and position
Expand Down
24 changes: 12 additions & 12 deletions src/Resources.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "../header/Resources.h"

// Create resources
unordered_map<const char*, SDL_Texture*> Resources::imageTable;
unordered_map<const char*, Mix_Music*> Resources::musicTable;
unordered_map<const char*, Mix_Chunk*> Resources::soundTable;
unordered_map<const char*, TTF_Font*> Resources::fontTable;
unordered_map<string, SDL_Texture*> Resources::imageTable;
unordered_map<string, Mix_Music*> Resources::musicTable;
unordered_map<string, Mix_Chunk*> Resources::soundTable;
unordered_map<string, TTF_Font*> Resources::fontTable;

SDL_Texture* Resources::GetImage(const char* file)
SDL_Texture* Resources::GetImage(string file)
{
// Check if texture is already loaded
if (imageTable.find(file) != imageTable.end())
return imageTable[file];

SDL_Texture* texture;
if ((texture = IMG_LoadTexture(Game::GetInstance().GetRenderer(), file)) == nullptr)
if ((texture = IMG_LoadTexture(Game::GetInstance().GetRenderer(), file.c_str())) == nullptr)
{
cout << "Error loading texture " << file << endl;
cout << SDL_GetError() << endl;
Expand All @@ -28,14 +28,14 @@ SDL_Texture* Resources::GetImage(const char* file)
}
}

Mix_Music* Resources::GetMusic(const char* file)
Mix_Music* Resources::GetMusic(string file)
{
// Check if music is already loaded
if (musicTable.find(file) != musicTable.end())
return musicTable[file];

Mix_Music* music;
if ((music = Mix_LoadMUS(file)) == nullptr)
if ((music = Mix_LoadMUS(file.c_str())) == nullptr)
{
cout << "Error loading music " << file << endl;
cout << SDL_GetError() << endl;
Expand All @@ -50,14 +50,14 @@ Mix_Music* Resources::GetMusic(const char* file)
}
}

Mix_Chunk* Resources::GetSound(const char* file)
Mix_Chunk* Resources::GetSound(string file)
{
// Check if sound is already loaded
if (soundTable.find(file) != soundTable.end())
return soundTable[file];

Mix_Chunk* chunk;
if ((chunk = Mix_LoadWAV(file)) == nullptr)
if ((chunk = Mix_LoadWAV(file.c_str())) == nullptr)
{
cout << "Error loading sound" << endl;
cout << SDL_GetError() << endl;
Expand All @@ -72,7 +72,7 @@ Mix_Chunk* Resources::GetSound(const char* file)
}
}

TTF_Font* Resources::GetFont(const char* file, int fontSize)
TTF_Font* Resources::GetFont(string file, int fontSize)
{
// Concatenates file with fontSize to create key
string fileName = file + to_string(fontSize);
Expand All @@ -81,7 +81,7 @@ TTF_Font* Resources::GetFont(const char* file, int fontSize)
return fontTable[fileName.c_str()];

TTF_Font* font;
if ((font = TTF_OpenFont(file, fontSize)) == nullptr)
if ((font = TTF_OpenFont(file.c_str(), fontSize)) == nullptr)
{
cout << "Error loading font " << file << endl;
cout << SDL_GetError() << endl;
Expand Down
6 changes: 3 additions & 3 deletions src/StageState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ void StageState::LoadAssets()

const char* fontFile = "./assets/font/call_me_maybe.ttf";
const char* textStr = "FPS ";
int fontSize = 20;
int fontSize = 16;
Text::TextStyle style = Text::BLENDED;
SDL_Color color = {185, 35, 35, 255};
SDL_Color color = {212, 15, 15, 255};

Text* text = new Text(*textGo, fontFile, fontSize, style, textStr, color);
textGo->AddComponent(text);
Expand Down Expand Up @@ -144,7 +144,7 @@ void StageState::Update(float dt)
if (objectArray[i]->IsDead())
objectArray.erase(objectArray.begin() + i);

// Updates FPS counter
// Updates FPS counter - Turning into a component might be better
Text* FPS_Text = (Text*) objectArray[i]->GetComponent("Text");
if (FPS_Text != nullptr)
FPS_Text->SetText(("FPS " + to_string(floor(GameData::currentFPS))).c_str());
Expand Down

0 comments on commit fc6f717

Please sign in to comment.