Spliced in updates to the scene system; program compiles & runs, player's don't move

This commit is contained in:
Kayne Ruse
2013-06-04 20:20:56 +10:00
parent 783e8928d6
commit 231db701e4
19 changed files with 147 additions and 116 deletions
+7 -3
View File
@@ -75,13 +75,17 @@ SceneList BaseScene::GetNextScene() const {
//Frame loop //Frame loop
//------------------------- //-------------------------
void BaseScene::RunFrame() { void BaseScene::RunFrame(double delta) {
FrameStart(); FrameStart();
HandleEvents(); HandleEvents();
Update(); Update(delta);
FrameEnd();
}
void BaseScene::RenderFrame() {
SDL_FillRect(screen, 0, 0);
Render(screen); Render(screen);
SDL_Flip(screen); SDL_Flip(screen);
FrameEnd();
} }
//------------------------- //-------------------------
+25 -4
View File
@@ -1,3 +1,24 @@
/* Copyright: (c) Kayne Ruse 2013
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef BASESCENE_HPP_ #ifndef BASESCENE_HPP_
#define BASESCENE_HPP_ #define BASESCENE_HPP_
@@ -19,17 +40,17 @@ public:
SceneList GetNextScene() const; SceneList GetNextScene() const;
/* Frame loop */ /* Frame loop */
virtual void RunFrame(); virtual void RunFrame(double delta);
virtual void RenderFrame();
protected: protected:
virtual void FrameStart() {} virtual void FrameStart() {}
virtual void HandleEvents();
virtual void Update(double delta) {}
virtual void FrameEnd() {} virtual void FrameEnd() {}
virtual void Update() {}
virtual void Render(SDL_Surface* const screen) {} virtual void Render(SDL_Surface* const screen) {}
/* Event handlers */ /* Event handlers */
virtual void HandleEvents();
virtual void QuitEvent() { SetNextScene(SceneList::QUIT); } virtual void QuitEvent() { SetNextScene(SceneList::QUIT); }
virtual void MouseMotion(SDL_MouseMotionEvent const&) {} virtual void MouseMotion(SDL_MouseMotionEvent const&) {}
virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {} virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {}
+2 -2
View File
@@ -28,11 +28,11 @@ void Combat::FrameStart() {
// //
} }
void Combat::FrameEnd() { void Combat::Update(double delta) {
// //
} }
void Combat::Update() { void Combat::FrameEnd() {
// //
} }
+10 -10
View File
@@ -7,21 +7,21 @@ class Combat : public BaseScene {
public: public:
/* Public access members */ /* Public access members */
Combat(); Combat();
virtual ~Combat(); ~Combat();
protected: protected:
/* Frame loop */ /* Frame loop */
virtual void FrameStart(); void FrameStart();
virtual void FrameEnd(); void Update(double delta);
virtual void Update(); void FrameEnd();
virtual void Render(SDL_Surface* const); void Render(SDL_Surface* const);
/* Event handlers */ /* Event handlers */
virtual void MouseMotion(SDL_MouseMotionEvent const&); void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&); void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
}; };
#endif #endif
+5 -5
View File
@@ -40,11 +40,7 @@ void InGame::FrameStart() {
// //
} }
void InGame::FrameEnd() { void InGame::Update(double delta) {
//
}
void InGame::Update() {
Receive(); Receive();
} }
@@ -87,6 +83,10 @@ void InGame::Receive() {
} }
} }
void InGame::FrameEnd() {
//
}
void InGame::Render(SDL_Surface* const screen) { void InGame::Render(SDL_Surface* const screen) {
// //
} }
+11 -11
View File
@@ -12,22 +12,22 @@ class InGame : public BaseScene {
public: public:
//Public access members //Public access members
InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID); InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
virtual ~InGame(); ~InGame();
protected: protected:
//Frame loop //Frame loop
virtual void FrameStart(); void FrameStart();
virtual void FrameEnd(); void Update(double delta);
virtual void Update(); void Receive();
virtual void Receive(); void FrameEnd();
virtual void Render(SDL_Surface* const); void Render(SDL_Surface* const);
//Event handlers //Event handlers
virtual void MouseMotion(SDL_MouseMotionEvent const&); void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&); void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//members //members
ConfigUtility* configUtil = nullptr; ConfigUtility* configUtil = nullptr;
+9 -9
View File
@@ -31,7 +31,7 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti
listBox.x = 250; listBox.x = 250;
listBox.y = 50; listBox.y = 50;
listBox.w = GetScreen()->w - listBox.x; listBox.w = GetScreen()->w - listBox.x;
listBox.h = font.GetClipH(); listBox.h = font.GetCharH();
//ping the network //ping the network
PingNetwork(); PingNetwork();
@@ -54,11 +54,7 @@ void Lobby::FrameStart() {
// //
} }
void Lobby::FrameEnd() { void Lobby::Update(double delta) {
//
}
void Lobby::Update() {
Receive(); Receive();
} }
@@ -109,6 +105,10 @@ void Lobby::Receive() {
} }
} }
void Lobby::FrameEnd() {
//
}
void Lobby::Render(SDL_Surface* const screen) { void Lobby::Render(SDL_Surface* const screen) {
for (auto it : buttonMap) { for (auto it : buttonMap) {
it.second->DrawTo(screen); it.second->DrawTo(screen);
@@ -118,7 +118,7 @@ void Lobby::Render(SDL_Surface* const screen) {
SDL_Rect clip; SDL_Rect clip;
for (int i = 0; i < serverVector.size(); i++) { for (int i = 0; i < serverVector.size(); i++) {
clip = listBox; clip = listBox;
clip.y += i * font.GetClipH(); clip.y += i * font.GetCharH();
//if a server has been selected, and this is the selected server //if a server has been selected, and this is the selected server
if (selectedServer && selectedServer == &serverVector[i]) { if (selectedServer && selectedServer == &serverVector[i]) {
@@ -155,8 +155,8 @@ void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
} }
//select a server (clicked within the bounds of the server box) //select a server (clicked within the bounds of the server box)
if (button.x > listBox.x && button.y > listBox.y && button.y < serverVector.size() * font.GetClipH() + listBox.y) { if (button.x > listBox.x && button.y > listBox.y && button.y < serverVector.size() * font.GetCharH() + listBox.y) {
selectedServer = &serverVector[(button.y-listBox.y)/font.GetClipH()]; selectedServer = &serverVector[(button.y-listBox.y)/font.GetCharH()];
} }
else { else {
selectedServer = nullptr; selectedServer = nullptr;
+11 -11
View File
@@ -19,22 +19,22 @@ class Lobby : public BaseScene {
public: public:
//Public access members //Public access members
Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID); Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
virtual ~Lobby(); ~Lobby();
protected: protected:
//Frame loop //Frame loop
virtual void FrameStart(); void FrameStart();
virtual void FrameEnd(); void Update(double delta);
virtual void Update(); void Receive();
virtual void Receive(); void FrameEnd();
virtual void Render(SDL_Surface* const); void Render(SDL_Surface* const);
//Event handlers //Event handlers
virtual void MouseMotion(SDL_MouseMotionEvent const&); void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&); void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//utilities //utilities
struct ServerData { struct ServerData {
+7 -2
View File
@@ -18,6 +18,8 @@ MainMenu::MainMenu(ConfigUtility* cUtil, SurfaceManager* sMgr) {
buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start"); buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start");
buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options"); buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options");
buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit"); buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit");
buttonMap["testsystems"] = new Button(50, 250, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "TestSystems");
} }
MainMenu::~MainMenu() { MainMenu::~MainMenu() {
@@ -38,11 +40,11 @@ void MainMenu::FrameStart() {
// //
} }
void MainMenu::FrameEnd() { void MainMenu::Update(double delta) {
// //
} }
void MainMenu::Update() { void MainMenu::FrameEnd() {
// //
} }
@@ -78,6 +80,9 @@ void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) { if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) {
QuitEvent(); QuitEvent();
} }
if (buttonMap["testsystems"]->MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::TESTSYSTEMS);
}
} }
void MainMenu::KeyDown(SDL_KeyboardEvent const& key) { void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
+10 -10
View File
@@ -15,21 +15,21 @@ class MainMenu : public BaseScene {
public: public:
//Public access members //Public access members
MainMenu(ConfigUtility*, SurfaceManager*); MainMenu(ConfigUtility*, SurfaceManager*);
virtual ~MainMenu(); ~MainMenu();
protected: protected:
//Frame loop //Frame loop
virtual void FrameStart(); void FrameStart();
virtual void FrameEnd(); void Update(double delta);
virtual void Update(); void FrameEnd();
virtual void Render(SDL_Surface* const); void Render(SDL_Surface* const);
//Event handlers //Event handlers
virtual void MouseMotion(SDL_MouseMotionEvent const&); void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&); void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//globals //globals
ConfigUtility* configUtil; ConfigUtility* configUtil;
+1 -1
View File
@@ -8,7 +8,7 @@ Player::Player(SDL_Surface* s, int w, int h)
{ {
} }
void Player::Update(int delta) { void Player::Update(double delta) {
if (motion.y > 0) { if (motion.y > 0) {
FaceDirection(Direction::SOUTH); FaceDirection(Direction::SOUTH);
} }
+1 -1
View File
@@ -11,7 +11,7 @@ class Player {
public: public:
Player(SDL_Surface*, int w, int h); Player(SDL_Surface*, int w, int h);
void Update(int delta); void Update(double delta);
void WalkInDirection(Direction); void WalkInDirection(Direction);
+1 -1
View File
@@ -33,7 +33,7 @@ void PlayerManager::Delete(int index) {
playerMap.erase(it); playerMap.erase(it);
} }
void PlayerManager::UpdateAll(int delta) { void PlayerManager::UpdateAll(double delta) {
for (auto it : playerMap) { for (auto it : playerMap) {
it.second->Update(delta); it.second->Update(delta);
} }
+1 -1
View File
@@ -14,7 +14,7 @@ public:
Player* Get(int index); Player* Get(int index);
void Delete(int index); void Delete(int index);
void UpdateAll(int delta); void UpdateAll(double delta);
void DrawAllTo(SDL_Surface* dest); void DrawAllTo(SDL_Surface* dest);
void DeleteAll(); void DeleteAll();
+19 -8
View File
@@ -1,15 +1,14 @@
#include "scene_manager.hpp" #include "scene_manager.hpp"
#include <stdexcept> #include <stdexcept>
#include <chrono>
//------------------------- //-------------------------
//Scene headers //Scene headers
//------------------------- //-------------------------
//Add the custom scene headers here //Add the custom scene headers here
#ifdef DEBUG
#include "test_systems.hpp" #include "test_systems.hpp"
#endif
#include "splash.hpp" #include "splash.hpp"
#include "main_menu.hpp" #include "main_menu.hpp"
@@ -50,6 +49,13 @@ void SceneManager::Init() {
void SceneManager::Proc() { void SceneManager::Proc() {
LoadScene(SceneList::FIRST); LoadScene(SceneList::FIRST);
//prepare the time system
typedef std::chrono::high_resolution_clock Clock;
Clock::duration delta(16 * Clock::duration::period::den / std::milli::den);
Clock::time_point simTime = Clock::now();
Clock::time_point realTime;
//The main loop //The main loop
while(activeScene->GetNextScene() != SceneList::QUIT) { while(activeScene->GetNextScene() != SceneList::QUIT) {
//switch scenes when necessary //switch scenes when necessary
@@ -58,11 +64,18 @@ void SceneManager::Proc() {
continue; continue;
} }
//wipe the screen //update the current time
SDL_FillRect(activeScene->GetScreen(), 0, 0); realTime = Clock::now();
//simulate game time
while (simTime < realTime) {
//call each user defined function //call each user defined function
activeScene->RunFrame(); activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den);
simTime += delta;
}
//draw the game to the screen
activeScene->RenderFrame();
//give the computer a break //give the computer a break
SDL_Delay(10); SDL_Delay(10);
@@ -86,13 +99,11 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
switch(sceneIndex) { switch(sceneIndex) {
//add scene creation calls here //add scene creation calls here
case SceneList::FIRST:
#ifdef DEBUG
case SceneList::TESTSYSTEMS: case SceneList::TESTSYSTEMS:
activeScene = new TestSystems(&configUtil, &surfaceMgr, &netUtil); activeScene = new TestSystems(&configUtil, &surfaceMgr, &netUtil);
break; break;
#endif
case SceneList::FIRST:
case SceneList::SPLASH: case SceneList::SPLASH:
activeScene = new Splash(&configUtil, &surfaceMgr); activeScene = new Splash(&configUtil, &surfaceMgr);
break; break;
+5 -3
View File
@@ -26,7 +26,7 @@ Splash::~Splash() {
#endif #endif
} }
void Splash::RunFrame() { void Splash::RunFrame(double delta) {
//skip any event handling here //skip any event handling here
SDL_Event event; SDL_Event event;
while(SDL_PollEvent(&event)); while(SDL_PollEvent(&event));
@@ -38,12 +38,14 @@ void Splash::RunFrame() {
logo->DrawTo(GetScreen(),x,y); logo->DrawTo(GetScreen(),x,y);
SDL_Flip(GetScreen()); SDL_Flip(GetScreen());
//load the resources ONCE
if (!loaded) { if (!loaded) {
LoadResources();
loaded = true; loaded = true;
LoadResources();
} }
if (clock() - start > CLOCKS_PER_SEC*3) { //wait X seconds
if (Clock::now() - start > std::chrono::duration<int>(1)) {
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
} }
} }
+6 -4
View File
@@ -7,15 +7,17 @@
#include "surface_manager.hpp" #include "surface_manager.hpp"
#include "image.hpp" #include "image.hpp"
#include <ctime> #include <chrono>
class Splash : public BaseScene { class Splash : public BaseScene {
public: public:
Splash(ConfigUtility*, SurfaceManager*); Splash(ConfigUtility*, SurfaceManager*);
virtual ~Splash(); ~Splash();
protected: protected:
virtual void RunFrame(); typedef std::chrono::high_resolution_clock Clock;
void RunFrame(double delta);
void RenderFrame() {};
void LoadResources(); void LoadResources();
@@ -25,7 +27,7 @@ protected:
//members //members
bool loaded = false; bool loaded = false;
time_t start = clock(); Clock::time_point start = Clock::now();
Image* logo = nullptr; Image* logo = nullptr;
}; };
+5 -19
View File
@@ -18,20 +18,6 @@ TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkU
surfaceMgr = sMgr; surfaceMgr = sMgr;
netUtil = nUtil; netUtil = nUtil;
//subscene; load the resources
Splash* splash = new Splash(configUtil, surfaceMgr);
while(splash->GetNextScene() == SceneList::CONTINUE) {
//wipe the screen
SDL_FillRect(splash->GetScreen(), 0, 0);
//call each user defined function
((BaseScene*)(splash))->RunFrame();
//give the computer a break
SDL_Delay(10);
}
delete splash;
SetNextScene(SceneList::CONTINUE);
playerCounter = currentPlayer = 0; playerCounter = currentPlayer = 0;
playerMgr.New(playerCounter++, surfaceMgr->Get("elliot")); playerMgr.New(playerCounter++, surfaceMgr->Get("elliot"));
@@ -57,15 +43,15 @@ void TestSystems::FrameStart() {
frameRate.Calculate(); frameRate.Calculate();
} }
void TestSystems::FrameEnd() { void TestSystems::Update(double delta) {
//
}
void TestSystems::Update() {
// Delta::Calculate(); // Delta::Calculate();
// playerMgr.UpdateAll(Delta::GetTime()); // playerMgr.UpdateAll(Delta::GetTime());
} }
void TestSystems::FrameEnd() {
//
}
string IToS(int i) { string IToS(int i) {
char buffer[20]; char buffer[20];
memset(buffer, 0, 20); memset(buffer, 0, 20);
+10 -10
View File
@@ -18,21 +18,21 @@ class TestSystems : public BaseScene {
public: public:
//Public access members //Public access members
TestSystems(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*); TestSystems(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~TestSystems(); ~TestSystems();
protected: protected:
//Frame loop //Frame loop
virtual void FrameStart(); void FrameStart();
virtual void FrameEnd(); void Update(double delta);
virtual void Update(); void FrameEnd();
virtual void Render(SDL_Surface* const); void Render(SDL_Surface* const);
//Event handlers //Event handlers
virtual void MouseMotion(SDL_MouseMotionEvent const&); void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&); void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//utilities //utilities
void NewPlayer(int index, std::string avatarName, int x, int y); void NewPlayer(int index, std::string avatarName, int x, int y);