Skip to content

Commit

Permalink
finished t1
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-tomas committed Jun 22, 2022
1 parent 4293c46 commit 42b5049
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 53 deletions.
Binary file added assets/image/sv_64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 10 additions & 0 deletions src/Component.h
Original file line number Diff line number Diff line change
@@ -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;
};
7 changes: 3 additions & 4 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -140,5 +139,5 @@ Game::~Game()
IMG_Quit();
SDL_Quit();

cout << "Instance deleted successfully!" << endl;
cout << "Game deleted successfully!" << endl;
}
3 changes: 1 addition & 2 deletions src/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 1 addition & 2 deletions src/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Music.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
9 changes: 9 additions & 0 deletions src/Rect.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions src/Rect.h
Original file line number Diff line number Diff line change
@@ -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;
};
24 changes: 12 additions & 12 deletions src/Sprite.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Sprite.h"
#include "Game.h"

Sprite::Sprite()
{
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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()
Expand Down
5 changes: 0 additions & 5 deletions src/Sprite.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef SPRITE_H
#define SPRITE_H

#define INCLUDE_SDL
#define INCLUDE_SDL_IMAGE

#include "SDL_include.h"
Expand All @@ -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);
Expand All @@ -27,9 +25,6 @@ class Sprite {
int width;
int height;
SDL_Rect clipRect;


SDL_Renderer* renderer; // @TODO: fix this
};

#endif // SPRITE_H
25 changes: 5 additions & 20 deletions src/State.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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()
Expand Down
8 changes: 2 additions & 6 deletions src/State.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions src/Vec2.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions src/Vec2.h
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 42b5049

Please sign in to comment.