diff --git a/assets/image/sv_64.png b/assets/image/sv_64.png new file mode 100644 index 0000000..44860c3 Binary files /dev/null and b/assets/image/sv_64.png differ diff --git a/makefile b/makefile index e772005..b7b7d95 100644 --- a/makefile +++ b/makefile @@ -16,8 +16,16 @@ CC_FLAGS=-std=c++11 \ # Command used at clean target RM = rm +CPP_SRC=src/Main.cpp \ + src/Game.cpp \ + src/State.cpp \ + src/Sprite.cpp \ + src/Music.cpp \ + src/Vec2.cpp \ + src/Rect.cpp + run: - $(CC) src/Main.cpp src/Game.cpp src/State.cpp src/Sprite.cpp src/Music.cpp $(CC_FLAGS) -o $(TARGET) + $(CC) $(CPP_SRC) $(CC_FLAGS) -o $(TARGET) clean: $(RM) $(TARGET) diff --git a/src/Component.h b/src/Component.h new file mode 100644 index 0000000..4388559 --- /dev/null +++ b/src/Component.h @@ -0,0 +1,10 @@ +class Component { + // public: + // Component(GameObject& associated); + // virtual ~Component(); + // void virtual Update(float dt) = 0; + // void virtual Render(float dt) = 0; + // bool virtual Is(const char* type) = 0; + // protected: + // GameObject& associated; +}; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index d2e1228..81a041b 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -92,7 +92,7 @@ Game::Game(const char* title, int width, int height) cout << "Renderer created successfully!" << endl; // Creates state - state = new State(renderer); // @TODO: fix this + state = new State(); } Game& Game::GetInstance() @@ -118,14 +118,13 @@ void Game::Run() cout << endl << "Engine running!" << endl; while (state->QuitRequested() == false) { - // @TODO: define dt in Update(dt) state->Update(0); state->Render(); SDL_RenderPresent(renderer); SDL_Delay(33); // 30 FPS } - this->~Game(); + delete this; } Game::~Game() @@ -140,5 +139,5 @@ Game::~Game() IMG_Quit(); SDL_Quit(); - cout << "Instance deleted successfully!" << endl; + cout << "Game deleted successfully!" << endl; } diff --git a/src/Game.h b/src/Game.h index 5aa97f0..28681ba 100644 --- a/src/Game.h +++ b/src/Game.h @@ -14,14 +14,13 @@ using namespace std; class Game { public: ~Game(); - void Run(); SDL_Renderer* GetRenderer(); State& GetState(); static Game& GetInstance(); + private: Game(const char* title, int width, int height); - static Game* instance; SDL_Window* window; SDL_Renderer* renderer; diff --git a/src/Main.cpp b/src/Main.cpp index 3c40ef8..e22a844 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,7 +1,12 @@ #include "Game.h" +// #include "Vec2.h" +// #include "Rect.h" + int main(int argc, char** argv) { Game::GetInstance().Run(); return 0; } + +// Game::GetInstance().GetRenderer() \ No newline at end of file diff --git a/src/Music.cpp b/src/Music.cpp index 643cd65..bcd98c6 100644 --- a/src/Music.cpp +++ b/src/Music.cpp @@ -13,8 +13,7 @@ Music::Music(const char* file) void Music::Play(int times) { - // @TODO: handle errors - if (Mix_PlayMusic(music, times) < 0) // @TODO: FIX THIS + if (Mix_PlayMusic(music, times) < 0) { cout << "Error playing music" << endl; cout << SDL_GetError() << endl; diff --git a/src/Music.h b/src/Music.h index df30e86..9bcbe9e 100644 --- a/src/Music.h +++ b/src/Music.h @@ -11,12 +11,13 @@ using namespace std; class Music { public: Music(); - ~Music(); // @TODO: fix this + ~Music(); Music(const char* file); void Play(int times = -1); void Stop(int msToStop = 1500); void Open(const char* file); bool IsOpen(); + private: Mix_Music* music; }; diff --git a/src/Rect.cpp b/src/Rect.cpp new file mode 100644 index 0000000..03c5a93 --- /dev/null +++ b/src/Rect.cpp @@ -0,0 +1,9 @@ +#include "Rect.h" + +Rect::Rect(float x, float y, float w, float h) +{ + this->x = x; + this->y = y; + this->w = w; + this->h = h; +} \ No newline at end of file diff --git a/src/Rect.h b/src/Rect.h new file mode 100644 index 0000000..fb91569 --- /dev/null +++ b/src/Rect.h @@ -0,0 +1,8 @@ +class Rect { + public: + Rect(float x = 0, float y = 0, float w = 0, float h = 0); + float x; + float y; + float w; + float h; +}; diff --git a/src/Sprite.cpp b/src/Sprite.cpp index 7f02cd4..89cebf0 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -1,4 +1,5 @@ #include "Sprite.h" +#include "Game.h" Sprite::Sprite() { @@ -11,14 +12,6 @@ Sprite::Sprite(const char* file) Open(file); } -// @TODO: fix this -Sprite::Sprite(const char* file, SDL_Renderer* renderer) -{ - this->renderer = renderer; - texture = nullptr; - Open(file); -} - Sprite::~Sprite() { if (texture != nullptr) @@ -33,9 +26,12 @@ Sprite::~Sprite() void Sprite::Open(const char* file) { if (texture != nullptr) + { SDL_DestroyTexture(texture); + cout << "Previous texture destroyed successfully!" << endl; + } - if ((texture = IMG_LoadTexture(renderer, file)) == nullptr) + if ((texture = IMG_LoadTexture(Game::GetInstance().GetRenderer(), file)) == nullptr) { cout << "Error loading texture " << file << endl; cout << SDL_GetError() << endl; @@ -45,8 +41,7 @@ void Sprite::Open(const char* file) cout << "Texture " << file << " loaded successfully!" << endl; SDL_QueryTexture(texture, nullptr, nullptr, &width, &height); - SetClip(0, 0, width, height); // @TODO: hardcoded x, y - Render(0, 0); // @TODO: hardcoded x, y + SetClip(0, 0, width, height); } void Sprite::SetClip(int x, int y, int width, int height) @@ -65,7 +60,12 @@ void Sprite::Render(int x, int y) dstRect.w = clipRect.w; dstRect.h = clipRect.h; - SDL_RenderCopy(renderer, texture, &clipRect, &dstRect); + if (texture == nullptr || SDL_RenderCopy(Game::GetInstance().GetRenderer(), texture, &clipRect, &dstRect) < 0) + { + cout << "Error rendering copy" << endl; + cout << SDL_GetError() << endl; + exit(1); + } } int Sprite::GetWidth() diff --git a/src/Sprite.h b/src/Sprite.h index 287350f..e7a91c9 100644 --- a/src/Sprite.h +++ b/src/Sprite.h @@ -1,7 +1,6 @@ #ifndef SPRITE_H #define SPRITE_H -#define INCLUDE_SDL #define INCLUDE_SDL_IMAGE #include "SDL_include.h" @@ -13,7 +12,6 @@ class Sprite { public: Sprite(); Sprite(const char* file); - Sprite(const char* file, SDL_Renderer* renderer); // @TODO: fix this ~Sprite(); void Open(const char* file); void SetClip(int x, int y, int w, int h); @@ -27,9 +25,6 @@ class Sprite { int width; int height; SDL_Rect clipRect; - - - SDL_Renderer* renderer; // @TODO: fix this }; #endif // SPRITE_H \ No newline at end of file diff --git a/src/State.cpp b/src/State.cpp index fa9ebb5..dff476a 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -1,25 +1,10 @@ #include "State.h" +#include "Game.h" -State::State() +State::State(): bg("./assets/image/ocean.jpg"), music("./assets/audio/stageState.ogg") { quitRequested = false; - bg = Sprite("path_to_file", renderer); // @TODO: define a sprite - cout << "State created successfully!" << endl; -} - -// @TODO: fix this -State::State(SDL_Renderer* renderer) -{ - this->renderer = renderer; - quitRequested = false; - bg = Sprite("./assets/image/ocean.jpg", renderer); // @TODO: fix this - music = new Music("./assets/audio/stageState.ogg"); // @TODO: fix this - // music1 = Music("./assets/audio/stageState.ogg"); - // music = new Music("./assets/audio/choochoo.wav"); // @TODO: debug - - (*music).Play(1); - // music1.Play(1); - + music.Play(1); cout << "State created successfully!" << endl; } @@ -33,13 +18,13 @@ void State::Update(float dt) if (SDL_QuitRequested() == true) { quitRequested = true; - cout << "Quit requested!" << endl; + cout << "Quit requested!\n" << endl; } } void State::Render() { - bg.Render(0, 0); // @TODO: fix this + bg.Render(0, 0); } bool State::QuitRequested() diff --git a/src/State.h b/src/State.h index 99b3ddf..c873583 100644 --- a/src/State.h +++ b/src/State.h @@ -9,19 +9,15 @@ using namespace std; class State { public: State(); - State(SDL_Renderer* renderer); // @TODO: fix this bool QuitRequested(); void LoadAssets(); void Update(float dt); void Render(); + private: Sprite bg; - Music* music; // @TODO: fix this - Music music1; + Music music; bool quitRequested; - - - SDL_Renderer* renderer; // @TODO: fix this }; #endif // STATE_H \ No newline at end of file diff --git a/src/Vec2.cpp b/src/Vec2.cpp new file mode 100644 index 0000000..d3da08b --- /dev/null +++ b/src/Vec2.cpp @@ -0,0 +1,22 @@ +#include "Vec2.h" + +Vec2::Vec2(float x, float y) +{ + this->x = x; + this->y = y; +} + +Vec2 Vec2::Sum2(Vec2 v) +{ + return Vec2(x + v.x, y + v.y); +} + +Vec2 Vec2::Sub2(Vec2 v) +{ + return Vec2(x - v.x, y - v.y); +} + +Vec2 Vec2::Mult2(float e) +{ + return Vec2(x * e, y * e); +} \ No newline at end of file diff --git a/src/Vec2.h b/src/Vec2.h new file mode 100644 index 0000000..d0bd5b4 --- /dev/null +++ b/src/Vec2.h @@ -0,0 +1,9 @@ +class Vec2 { + public: + Vec2(float x = 0, float y = 0); + Vec2 Sum2(Vec2 v); + Vec2 Sub2(Vec2 v); + Vec2 Mult2(float e); + float x; + float y; +};