Imported basics

project directory
client - scene system
server - empty main
SDL_net source
graphical resources
This commit is contained in:
Kayne Ruse
2013-04-28 04:37:14 +10:00
parent 31d5b3f618
commit 79304f24b8
23 changed files with 2263 additions and 0 deletions
+114
View File
@@ -0,0 +1,114 @@
#include "base_scene.h"
#include <stdexcept>
//-------------------------
//Static declarations
//-------------------------
SDL_Surface* BaseScene::screen = nullptr;
//-------------------------
//Public access members
//-------------------------
BaseScene::BaseScene() {
nextScene = SceneList::CONTINUE;
}
BaseScene::~BaseScene() {
//
}
//-------------------------
//Program control
//-------------------------
SDL_Surface* BaseScene::SetScreen(int w, int h, int bpp, Uint32 flags) {
if (!bpp) {
bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
}
screen = SDL_SetVideoMode(w, h, bpp, flags);
if (screen == NULL) {
throw(std::runtime_error("Failed to create the screen surface"));
}
return screen;
}
SDL_Surface* BaseScene::GetScreen() {
return screen;
}
SceneList BaseScene::SetNextScene(SceneList sceneIndex) {
return nextScene = sceneIndex;
}
SceneList BaseScene::GetNextScene() const {
return nextScene;
}
//-------------------------
//Frame loop
//-------------------------
void BaseScene::RunFrame() {
FrameStart();
HandleEvents();
Update();
Render(screen);
SDL_Flip(screen);
FrameEnd();
}
//-------------------------
//Event handlers
//-------------------------
void BaseScene::HandleEvents() {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
QuitEvent();
break;
case SDL_VIDEORESIZE:
SetScreen(event.resize.w, event.resize.h, 0, screen->flags);
break;
case SDL_MOUSEMOTION:
MouseMotion(event.motion);
break;
case SDL_MOUSEBUTTONDOWN:
MouseButtonDown(event.button);
break;
case SDL_MOUSEBUTTONUP:
MouseButtonUp(event.button);
break;
case SDL_KEYDOWN:
KeyDown(event.key);
break;
case SDL_KEYUP:
KeyUp(event.key);
break;
#ifdef USE_EVENT_JOYSTICK
//TODO: joystick/gamepad support
#endif
#ifdef USE_EVENT_UNKNOWN
default:
UnknownEvent(event);
break;
#endif
}//switch
}//while
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef BASESCENE_H_
#define BASESCENE_H_
#include "scene_list.h"
#include "SDL/SDL.h"
class BaseScene {
public:
/* Public access members */
BaseScene();
virtual ~BaseScene();
/* Program control */
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
static SDL_Surface* GetScreen();
SceneList SetNextScene(SceneList sceneIndex);
SceneList GetNextScene() const;
/* Frame loop */
virtual void RunFrame();
protected:
virtual void FrameStart() {}
virtual void FrameEnd() {}
virtual void Update() {}
virtual void Render(SDL_Surface* const screen) {}
/* Event handlers */
virtual void HandleEvents();
virtual void QuitEvent () { SetNextScene(SceneList::QUIT); }
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&) {}
#ifdef USE_EVENT_JOYSTICK
//TODO: joystick/gamepad support
#endif
#ifdef USE_EVENT_UNKNOWN
virtual void UnknownEvent (SDL_Event const&) {}
#endif
private:
static SDL_Surface* screen;
SceneList nextScene;
};
#endif
+20
View File
@@ -0,0 +1,20 @@
#include "scene_manager.h"
#include <stdexcept>
#include <iostream>
using namespace std;
int main(int, char**) {
SceneManager app;
try {
app.Init();
app.Proc();
app.Quit();
}
catch(exception& e) {
cerr << "Fatal error: " << e.what() << endl;
return 1;
}
return 0;
}
+19
View File
@@ -0,0 +1,19 @@
CXXFLAGS+=-std=c++11
DFLAGS=-DDEBUG
LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi
OBJ=base_scene.o scene_manager.o main.o
SRC=test_systems.cpp
all: debug
release: $(OBJ)
$(CXX) $(CXXFLAGS) $(SRC) $(OBJ) $(LIB)
debug: $(OBJ)
$(CXX) $(CXXFLAGS) $(DFLAGS) $(SRC) $(OBJ) $(LIB)
clean:
-$(RM) *.o *.a *.exe
unit:
$(CXX) $(CXXFLAGS) unit.cpp
+14
View File
@@ -0,0 +1,14 @@
#ifndef SCENELIST_H_
#define SCENELIST_H_
enum class SceneList {
//these are reserved
QUIT,
CONTINUE,
FIRST,
//custom indexes
TESTSYSTEMS,
};
#endif
+76
View File
@@ -0,0 +1,76 @@
#include "scene_manager.h"
#include <stdexcept>
//-------------------------
//Scene headers
//-------------------------
//Add the custom scene headers here
#include "test_systems.h"
//-------------------------
//Public access members
//-------------------------
SceneManager::SceneManager() {
activeScene = nullptr;
}
SceneManager::~SceneManager() {
UnloadScene();
}
void SceneManager::Init() {
if (SDL_Init(SDL_INIT_VIDEO))
throw(std::runtime_error("Failed to initialize SDL"));
BaseScene::SetScreen(800, 600);
}
void SceneManager::Proc() {
LoadScene(SceneList::FIRST);
//The main loop
while(activeScene->GetNextScene() != SceneList::QUIT) {
//switch scenes when necessary
if (activeScene->GetNextScene() != SceneList::CONTINUE) {
LoadScene(activeScene->GetNextScene());
continue;
}
//call each user defined function
activeScene->RunFrame();
}
UnloadScene();
}
void SceneManager::Quit() {
UnloadScene();
SDL_Quit();
}
//-------------------------
//Private access members
//-------------------------
void SceneManager::LoadScene(SceneList sceneIndex) {
UnloadScene();
switch(sceneIndex) {
//add scene creation calls here
case SceneList::FIRST:
case SceneList::TESTSYSTEMS:
activeScene = new TestSystems();
break;
default:
throw(std::logic_error("Failed to recognize the scene index"));
}
}
void SceneManager::UnloadScene() {
delete activeScene;
activeScene = nullptr;
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef SCENEMANAGER_H_
#define SCENEMANAGER_H_
#include "scene_list.h"
#include "base_scene.h"
#include "SDL/SDL.h"
class SceneManager {
public:
/* Public access members */
SceneManager();
~SceneManager();
void Init();
void Proc();
void Quit();
private:
/* Private access members */
void LoadScene(SceneList sceneIndex);
void UnloadScene();
BaseScene* activeScene;
};
#endif
+61
View File
@@ -0,0 +1,61 @@
#include "test_systems.h"
//-------------------------
//Public access members
//-------------------------
TestSystems::TestSystems() {
//
}
TestSystems::~TestSystems() {
//
}
//-------------------------
//Frame loop
//-------------------------
void TestSystems::FrameStart() {
//
}
void TestSystems::FrameEnd() {
//
}
void TestSystems::Update() {
//
}
void TestSystems::Render(SDL_Surface* const screen) {
//
}
//-------------------------
//Event handlers
//-------------------------
void TestSystems::MouseMotion(SDL_MouseMotionEvent const& motion) {
//
}
void TestSystems::MouseButtonDown(SDL_MouseButtonEvent const& button) {
//
}
void TestSystems::MouseButtonUp(SDL_MouseButtonEvent const& button) {
//
}
void TestSystems::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) {
case SDLK_ESCAPE:
QuitEvent();
break;
}
}
void TestSystems::KeyUp(SDL_KeyboardEvent const& key) {
//
}
+27
View File
@@ -0,0 +1,27 @@
#ifndef TESTSYSTEMS_H_
#define TESTSYSTEMS_H_
#include "base_scene.h"
class TestSystems : public BaseScene {
public:
/* Public access members */
TestSystems();
virtual ~TestSystems();
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&);
};
#endif
View File