Moved working systems into InGame
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
#ifndef DEFINES_H_
|
||||
#define DEFINES_H_
|
||||
|
||||
enum class Direction {
|
||||
NORTH = 1, SOUTH = 2,
|
||||
EAST = 3, WEST = 4
|
||||
};
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -14,7 +14,7 @@ SDL_Surface* Image::SetSurface(SDL_Surface* p) {
|
||||
return surface;
|
||||
}
|
||||
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* p, SDL_Rect r) {
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* const p, SDL_Rect r) {
|
||||
surface = p;
|
||||
clip = r;
|
||||
return surface;
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ public:
|
||||
SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect);
|
||||
SDL_Surface* GetSurface() const;
|
||||
|
||||
void DrawTo(SDL_Surface*, Sint16 x, Sint16 y);
|
||||
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
||||
|
||||
//Clip handlers
|
||||
SDL_Rect SetClip(SDL_Rect r) { return clip = r; }
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
#include "in_game.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InGame::InGame() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering InGame" << endl;
|
||||
#endif
|
||||
surfaceMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp");
|
||||
surfaceMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp");
|
||||
|
||||
player = new Player(surfaceMgr.Get("player"), 32, 48);
|
||||
}
|
||||
|
||||
InGame::~InGame() {
|
||||
delete player;
|
||||
surfaceMgr.FreeAll();
|
||||
#ifdef DEBUG
|
||||
cout << "leaving InGame" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void InGame::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void InGame::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void InGame::Update() {
|
||||
delta.Calculate();
|
||||
player->Update(delta.GetDelta());
|
||||
}
|
||||
|
||||
void InGame::Render(SDL_Surface* const screen) {
|
||||
SDL_FillRect(screen, 0, 0);
|
||||
player->DrawTo(screen);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void InGame::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
|
||||
void InGame::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void InGame::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void InGame::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
break;
|
||||
case SDLK_w:
|
||||
player->WalkInDirection(Direction::NORTH);
|
||||
break;
|
||||
case SDLK_s:
|
||||
player->WalkInDirection(Direction::SOUTH);
|
||||
break;
|
||||
case SDLK_a:
|
||||
player->WalkInDirection(Direction::WEST);
|
||||
break;
|
||||
case SDLK_d:
|
||||
player->WalkInDirection(Direction::EAST);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InGame::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_w:
|
||||
player->WalkInDirection(Direction::SOUTH);
|
||||
break;
|
||||
case SDLK_s:
|
||||
player->WalkInDirection(Direction::NORTH);
|
||||
break;
|
||||
case SDLK_a:
|
||||
player->WalkInDirection(Direction::EAST);
|
||||
break;
|
||||
case SDLK_d:
|
||||
player->WalkInDirection(Direction::WEST);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef INGAME_H_
|
||||
#define INGAME_H_
|
||||
|
||||
#include "base_scene.h"
|
||||
|
||||
#include "delta.h"
|
||||
#include "player.h"
|
||||
#include "surface_manager.h"
|
||||
|
||||
class InGame : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InGame();
|
||||
virtual ~InGame();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
virtual void FrameStart();
|
||||
virtual void FrameEnd();
|
||||
virtual void Update();
|
||||
virtual void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
virtual void MouseMotion (SDL_MouseMotionEvent const&);
|
||||
virtual void MouseButtonDown (SDL_MouseButtonEvent const&);
|
||||
virtual void MouseButtonUp (SDL_MouseButtonEvent const&);
|
||||
virtual void KeyDown (SDL_KeyboardEvent const&);
|
||||
virtual void KeyUp (SDL_KeyboardEvent const&);
|
||||
|
||||
//members
|
||||
Delta delta;
|
||||
SurfaceManager surfaceMgr;
|
||||
Player* player;
|
||||
};
|
||||
|
||||
#endif
|
||||
+4
-2
@@ -1,7 +1,7 @@
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG
|
||||
LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi
|
||||
OBJ=base_scene.o scene_manager.o main.o
|
||||
SRC=test_systems.cpp
|
||||
OBJ=base_scene.o scene_manager.o main.o surface_manager.o image.o sprite_sheet.o player.o
|
||||
SRC=test_systems.cpp in_game.cpp
|
||||
|
||||
all: debug
|
||||
|
||||
@@ -14,5 +14,7 @@ debug: $(OBJ)
|
||||
clean:
|
||||
-$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
|
||||
unit:
|
||||
$(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp sprite_sheet.cpp player.cpp -lmingw32 -lSDLmain -lSDL
|
||||
|
||||
+2
-6
@@ -1,16 +1,12 @@
|
||||
#ifndef PLAYER_H_
|
||||
#define PLAYER_H_
|
||||
|
||||
#include "defines.h"
|
||||
#include "vector2.h"
|
||||
#include "sprite_sheet.h"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
enum class Direction {
|
||||
NORTH, SOUTH,
|
||||
EAST, WEST
|
||||
};
|
||||
|
||||
class Player {
|
||||
public:
|
||||
Player(SDL_Surface*, int w, int h);
|
||||
@@ -27,7 +23,7 @@ public:
|
||||
Vector2 ShiftMotion(Vector2 v) { return motion += v; }
|
||||
Vector2 GetMotion() const { return motion; }
|
||||
|
||||
void DrawTo(SDL_Surface* s) { sprite.DrawTo(s, position.x, position.y); }
|
||||
void DrawTo(SDL_Surface* const s) { sprite.DrawTo(s, position.x, position.y); }
|
||||
void FaceDirection(Direction);
|
||||
|
||||
SpriteSheet* GetSpriteSheet() { return &sprite; };
|
||||
|
||||
@@ -9,6 +9,7 @@ enum class SceneList {
|
||||
|
||||
//custom indexes
|
||||
TESTSYSTEMS,
|
||||
INGAME,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
//Add the custom scene headers here
|
||||
#include "test_systems.h"
|
||||
#include "in_game.h"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
@@ -61,6 +62,9 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
|
||||
switch(sceneIndex) {
|
||||
//add scene creation calls here
|
||||
case SceneList::FIRST:
|
||||
case SceneList::INGAME:
|
||||
activeScene = new InGame();
|
||||
break;
|
||||
case SceneList::TESTSYSTEMS:
|
||||
activeScene = new TestSystems();
|
||||
break;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "sprite_sheet.h"
|
||||
|
||||
SpriteSheet::SpriteSheet(SDL_Surface* s, int w, int h)
|
||||
: Image(s, {0, 0, (Uint16)w, (Uint16)h})
|
||||
SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h)
|
||||
: Image(s, {0, 0, w, h})
|
||||
{
|
||||
currentFrame = 0; maxFrames = GetSurface()->w / GetClipW();
|
||||
currentStrip = 0; maxStrips = GetSurface()->h / GetClipH();
|
||||
@@ -18,3 +18,10 @@ void SpriteSheet::Update(int delta) {
|
||||
SetClipX(currentFrame * GetClipW());
|
||||
SetClipY(currentStrip * GetClipH());
|
||||
}
|
||||
|
||||
void SpriteSheet::SetSurface(SDL_Surface* s, Uint16 w, Uint16 h) {
|
||||
Image::SetSurface(s, {0, 0, w, h});
|
||||
currentFrame = 0; maxFrames = GetSurface()->w / GetClipW();
|
||||
currentStrip = 0; maxStrips = GetSurface()->h / GetClipH();
|
||||
interval = ticks = 0;
|
||||
}
|
||||
@@ -5,13 +5,19 @@
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class SpriteSheet : public Image {
|
||||
class SpriteSheet : protected Image {
|
||||
public:
|
||||
SpriteSheet(SDL_Surface*, int w, int h);
|
||||
SpriteSheet(SDL_Surface*, Uint16 w, Uint16 h);
|
||||
virtual ~SpriteSheet() {}
|
||||
|
||||
void Update(int delta);
|
||||
|
||||
void SetSurface(SDL_Surface*, Uint16 w, Uint16 h);
|
||||
|
||||
using Image::GetSurface;
|
||||
using Image::DrawTo;
|
||||
|
||||
//these aren't regulated; be careful
|
||||
int SetWidth(int i) { return SetClipW(i); };
|
||||
int SetHeight(int i) { return SetClipH(i); };
|
||||
int SetFrames(int i) { currentFrame = 0; return maxFrames = i; };
|
||||
|
||||
@@ -7,10 +7,7 @@ SurfaceManager::SurfaceManager() {
|
||||
}
|
||||
|
||||
SurfaceManager::~SurfaceManager() {
|
||||
for (auto it : surfaceMap) {
|
||||
SDL_FreeSurface(it.second);
|
||||
}
|
||||
surfaceMap.clear();
|
||||
FreeAll();
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) {
|
||||
@@ -53,6 +50,13 @@ void SurfaceManager::Free(std::string key) {
|
||||
}
|
||||
}
|
||||
|
||||
void SurfaceManager::FreeAll() {
|
||||
for (auto it : surfaceMap) {
|
||||
SDL_FreeSurface(it.second);
|
||||
}
|
||||
surfaceMap.clear();
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) {
|
||||
SDL_Surface* ptr = SDL_LoadBMP(fname.c_str());
|
||||
if (ptr == nullptr) {
|
||||
|
||||
@@ -16,6 +16,7 @@ public:
|
||||
SDL_Surface* Get(std::string key);
|
||||
SDL_Surface* Set(std::string key, SDL_Surface* ptr);
|
||||
void Free(std::string key);
|
||||
void FreeAll();
|
||||
private:
|
||||
SDL_Surface* LoadSurface(std::string key, std::string fname);
|
||||
typedef std::map<std::string, SDL_Surface*> mapType;
|
||||
|
||||
-124
@@ -1,131 +1,7 @@
|
||||
#include "delta.h"
|
||||
#include "player.h"
|
||||
#include "sprite_sheet.h"
|
||||
#include "image.h"
|
||||
#include "surface_manager.h"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int go(int, char**) {
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
SDL_Surface* screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||
|
||||
SurfaceManager sMgr;
|
||||
|
||||
//load all resources
|
||||
sMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp");
|
||||
sMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp");
|
||||
sMgr.Load("tileset", "rsc/graphics/tilesets/MishMash.bmp");
|
||||
|
||||
Player player(sMgr.Get("player"), 32, 48);
|
||||
Image tiles(sMgr.Get("tileset"));
|
||||
SpriteSheet rose(sMgr.Get("flower"), 32, 32);
|
||||
|
||||
player.GetSpriteSheet()->SetInterval(200);
|
||||
rose.SetInterval(200);
|
||||
|
||||
// vector<SpriteSheet> flowerVector;
|
||||
// for (int i = 0; i < 100; i++) {
|
||||
// SpriteSheet ss(sMgr.Get("flower"), 32, 32);
|
||||
// ss.SetInterval(i%5 + 95);
|
||||
// flowerVector.push_back(ss);
|
||||
// }
|
||||
|
||||
Delta delta;
|
||||
|
||||
bool running = true;
|
||||
while (running) {
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_QUIT:
|
||||
running = false;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
switch(event.key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
running = false;
|
||||
break;
|
||||
case SDLK_w:
|
||||
case SDLK_UP:
|
||||
player.WalkInDirection(Direction::NORTH);
|
||||
break;
|
||||
case SDLK_s:
|
||||
case SDLK_DOWN:
|
||||
player.WalkInDirection(Direction::SOUTH);
|
||||
break;
|
||||
case SDLK_a:
|
||||
case SDLK_LEFT:
|
||||
player.WalkInDirection(Direction::WEST);
|
||||
break;
|
||||
case SDLK_d:
|
||||
case SDLK_RIGHT:
|
||||
player.WalkInDirection(Direction::EAST);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
switch(event.key.keysym.sym) {
|
||||
case SDLK_w:
|
||||
player.WalkInDirection(Direction::SOUTH);
|
||||
break;
|
||||
case SDLK_s:
|
||||
player.WalkInDirection(Direction::NORTH);
|
||||
break;
|
||||
case SDLK_a:
|
||||
player.WalkInDirection(Direction::EAST);
|
||||
break;
|
||||
case SDLK_d:
|
||||
player.WalkInDirection(Direction::WEST);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delta.Calculate();
|
||||
|
||||
player.Update(delta.GetDelta());
|
||||
rose.Update(0);
|
||||
|
||||
// for (int i = 0; i < 100; i++) {
|
||||
// flowerVector[i].Update(delta.GetDelta());
|
||||
// }
|
||||
|
||||
SDL_FillRect(screen, 0, 0);
|
||||
|
||||
tiles.DrawTo(screen, 0, 0);
|
||||
player.DrawTo(screen);
|
||||
rose.DrawTo(screen, 50, 100);
|
||||
|
||||
// for (int i = 0; i < 100; i++) {
|
||||
// flowerVector[i].DrawTo(screen, 20+i*4, i*4);
|
||||
// }
|
||||
|
||||
SDL_Flip(screen);
|
||||
|
||||
//debugging
|
||||
// cout << player.GetSpriteSheet()->GetCurrentFrame() << endl;
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int SDL_main(int argc, char* argv[]) {
|
||||
try {
|
||||
go(argc, argv);
|
||||
}
|
||||
catch(exception& e) {
|
||||
cerr << "Error: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user