-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inital commit, halfway thru chapter 4
- Loading branch information
0 parents
commit 14dc632
Showing
20 changed files
with
604 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "Enemy.h" | ||
Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams) | ||
{ | ||
} | ||
void Enemy::draw() | ||
{ | ||
SDLGameObject::draw(); | ||
} | ||
|
||
void Enemy::update() | ||
{ | ||
//m_x++; | ||
//m_y++; | ||
m_position.setX(m_position.getX()+1); | ||
m_position.setY(m_position.getY()+1); | ||
m_currentFrame = m_width * int(((SDL_GetTicks()/100)%6)); | ||
} | ||
|
||
void Enemy::clean() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef ENEMY_H | ||
#define ENEMY_H | ||
#include "SDLGameObject.h" | ||
class Enemy : public SDLGameObject | ||
{ | ||
public: | ||
Enemy(const LoaderParams* pParams); | ||
virtual void draw(); | ||
virtual void update(); | ||
virtual void clean(); | ||
}; | ||
#endif //ENEMY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "Game.h" | ||
#include "Enemy.h" | ||
#include "Player.h" | ||
|
||
Game* Game::s_pInstance = 0; | ||
|
||
bool Game::init(const char* title, int xpos, int ypos, int height, int width, int flags) | ||
{ | ||
if(SDL_Init(SDL_INIT_EVERYTHING) >= 0) | ||
{ | ||
m_bRunning = initGlobals(title, xpos,ypos,height,width,flags); | ||
|
||
if(m_bRunning) | ||
{ | ||
m_gameObjects.push_back(new Player(new LoaderParams(100,100,76,53,"animate"))); | ||
m_gameObjects.push_back(new Enemy(new LoaderParams(200,200,76,53,"animate"))); | ||
|
||
m_bRunning = TheTextureManager::instance()->load("assets/doggy.png","animate", m_pRenderer); | ||
} | ||
} | ||
else | ||
{ | ||
m_bRunning = false; | ||
} | ||
|
||
return m_bRunning; | ||
} | ||
|
||
void Game::render() | ||
{ | ||
//clear the window | ||
SDL_RenderClear(m_pRenderer); | ||
|
||
//game objects | ||
for(std::vector<GameObject*>::size_type i = 0; i != m_gameObjects.size(); i++) | ||
{ | ||
m_gameObjects[i]->draw(); | ||
} | ||
|
||
//show the window | ||
SDL_RenderPresent(m_pRenderer); | ||
} | ||
|
||
void Game::update() | ||
{ | ||
for(std::vector<GameObject*>::size_type i = 0; i != m_gameObjects.size(); i++) | ||
{ | ||
m_gameObjects[i]->update(); | ||
} | ||
} | ||
|
||
void Game::handleEvents() | ||
{ | ||
//do nothing yet | ||
SDL_Event event; | ||
if(SDL_PollEvent(&event)) | ||
{ | ||
switch(event.type) | ||
{ | ||
case SDL_QUIT: | ||
m_bRunning = false; | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
void Game::clean() | ||
{ | ||
SDL_DestroyWindow(m_pWindow); | ||
SDL_DestroyRenderer(m_pRenderer); | ||
SDL_Quit(); | ||
} | ||
|
||
bool Game::running() | ||
{ | ||
return m_bRunning; | ||
} | ||
|
||
bool Game::initGlobals(const char* title, int xpos, int ypos, int height, int width, int flags) | ||
{ | ||
m_pWindow = SDL_CreateWindow( | ||
title, | ||
xpos, | ||
ypos, | ||
height, | ||
width, | ||
flags | ||
); | ||
|
||
if(m_pWindow != 0) | ||
{ | ||
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0); | ||
//set to white | ||
SDL_SetRenderDrawColor(m_pRenderer,0,0,0,255); | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef GAME_H | ||
#define GAME_H | ||
#include <vector> | ||
#include <SDL2/SDL.h> | ||
#include "GameObject.h" | ||
|
||
class Game | ||
{ | ||
public: | ||
~Game() {} | ||
|
||
bool init(const char* title, int xpos, int ypos, int height, int width, int flags); | ||
void render(); | ||
void update(); | ||
void handleEvents(); | ||
void clean(); | ||
|
||
//private mutators | ||
bool running(); | ||
|
||
static Game* instance() | ||
{ | ||
if(s_pInstance == 0) | ||
{ | ||
s_pInstance = new Game(); | ||
} | ||
return s_pInstance; | ||
} | ||
|
||
SDL_Renderer* getRenderer() const { return m_pRenderer; } | ||
|
||
private: | ||
Game() {} | ||
static Game* s_pInstance; | ||
|
||
|
||
SDL_Texture* m_pTexture; | ||
SDL_Rect m_sourceRectangle; | ||
SDL_Rect m_destinationRectangle; | ||
|
||
int m_currentFrame; | ||
|
||
//game object vector | ||
std::vector<GameObject*> m_gameObjects; | ||
|
||
SDL_Window* m_pWindow; | ||
SDL_Renderer* m_pRenderer; | ||
bool m_bRunning; | ||
|
||
bool initGlobals(const char* title, int xpos, int ypos, int height, int width, int flags); | ||
}; | ||
|
||
typedef Game TheGame; | ||
#endif // GAME_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef GAMEOBJECT_H | ||
#define GAMEOBJECT_H | ||
#include "LoaderParams.h" | ||
class GameObject | ||
{ | ||
public: | ||
virtual void draw()=0; | ||
virtual void update()=0; | ||
virtual void clean()=0; | ||
protected: | ||
GameObject(const LoaderParams* pParams) {} | ||
virtual ~GameObject() {} | ||
}; | ||
#endif // GAMEOBJECT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef LOADERPARAMS_H | ||
#define LOADERPARAMS_H | ||
#include <string> | ||
class LoaderParams | ||
{ | ||
public: | ||
LoaderParams(int x, int y, int width, int height, std::string textureID) : | ||
m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) | ||
{ | ||
} | ||
|
||
int getX() const | ||
{ | ||
return m_x; | ||
} | ||
|
||
int getY() const | ||
{ | ||
return m_y; | ||
} | ||
|
||
int getWidth() const | ||
{ | ||
return m_width; | ||
} | ||
|
||
int getHeight() const | ||
{ | ||
return m_height; | ||
} | ||
|
||
std::string getTextureID() const | ||
{ | ||
return m_textureID; | ||
} | ||
private: | ||
int m_x; | ||
int m_y; | ||
int m_height; | ||
int m_width; | ||
std::string m_textureID; | ||
}; | ||
#endif //LOADERPARAMS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#OBJS specifies which files to compile as part of the project | ||
OBJS = main.cpp Game.cpp Player.cpp Enemy.cpp SDLGameObject.cpp TextureManager.cpp Vector2D.cpp | ||
#CC specifies which compiler we're using | ||
CC = g++ | ||
|
||
#COMPILER_FLAGS specifies the additional compilation options we're using | ||
# -w suppresses all warnings | ||
COMPILER_FLAGS = -w | ||
|
||
#LINKER_FLAGS specifies the libraries we're linking against | ||
LINKER_FLAGS = -lSDL2 -lSDL2_image | ||
|
||
#OBJ_NAME specifies the name of our exectuable | ||
OBJ_NAME = Movement | ||
|
||
#This is the target that compiles our executable | ||
all : $(OBJS) | ||
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "Player.h" | ||
Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams) | ||
{ | ||
} | ||
void Player::draw() | ||
{ | ||
SDLGameObject::draw(); | ||
} | ||
|
||
void Player::update() | ||
{ | ||
m_currentFrame = m_width * int(((SDL_GetTicks()/100)%6)); | ||
|
||
m_acceleration.setX(0.00001); | ||
m_acceleration.setY(0.00001); | ||
|
||
SDLGameObject::update(); | ||
} | ||
|
||
void Player::clean() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef PLAYER_H | ||
#define PLAYER_H | ||
#include "SDLGameObject.h" | ||
class Player : public SDLGameObject | ||
{ | ||
public: | ||
Player(const LoaderParams* pParams); | ||
virtual void draw(); | ||
virtual void update(); | ||
virtual void clean(); | ||
}; | ||
#endif //PLAYER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "SDLGameObject.h" | ||
#include "Game.h" | ||
SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams), m_position(pParams->getX(), pParams->getY()), m_velocity(0,0), m_acceleration(0,0) | ||
{ | ||
m_width = pParams->getWidth(); | ||
m_height = pParams->getHeight(); | ||
m_textureID = pParams->getTextureID(); | ||
m_currentRow = 1; | ||
m_currentFrame = 1; | ||
} | ||
|
||
void SDLGameObject::draw() | ||
{ | ||
TheTextureManager::instance()->drawFrame( | ||
m_textureID, | ||
(int)m_position.getX(), | ||
(int)m_position.getY(), | ||
m_width, | ||
m_height, | ||
m_currentRow, | ||
m_currentFrame, | ||
TheGame::instance()->getRenderer() | ||
); | ||
} | ||
void SDLGameObject::update() | ||
{ | ||
m_velocity += m_acceleration; | ||
m_position += m_velocity; | ||
} | ||
void SDLGameObject::clean() | ||
{ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef SDLGAMEOBJECT_H | ||
#define SDLGAMEOBJECT_H | ||
#include "LoaderParams.h" | ||
#include "GameObject.h" | ||
#include "TextureManager.h" | ||
#include "Vector2D.h" | ||
#include <SDL2/SDL.h> | ||
class SDLGameObject : public GameObject | ||
{ | ||
public: | ||
SDLGameObject( const LoaderParams* pParams); | ||
virtual void draw(); | ||
virtual void update(); | ||
virtual void clean(); | ||
protected: | ||
Vector2D m_position; | ||
Vector2D m_velocity; | ||
Vector2D m_acceleration; | ||
|
||
int m_width; | ||
int m_height; | ||
int m_currentRow; | ||
int m_currentFrame; | ||
std::string m_textureID; | ||
}; | ||
#endif // SDLGAMEOBJECT_H |
Oops, something went wrong.