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
//-------------------------
void BaseScene::RunFrame() {
void BaseScene::RunFrame(double delta) {
FrameStart();
HandleEvents();
Update();
Update(delta);
FrameEnd();
}
void BaseScene::RenderFrame() {
SDL_FillRect(screen, 0, 0);
Render(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_
#define BASESCENE_HPP_
@@ -19,17 +40,17 @@ public:
SceneList GetNextScene() const;
/* Frame loop */
virtual void RunFrame();
virtual void RunFrame(double delta);
virtual void RenderFrame();
protected:
virtual void FrameStart() {}
virtual void HandleEvents();
virtual void Update(double delta) {}
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&) {}
+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 access members */
Combat();
virtual ~Combat();
~Combat();
protected:
/* Frame loop */
virtual void FrameStart();
virtual void FrameEnd();
virtual void Update();
virtual void Render(SDL_Surface* const);
void FrameStart();
void Update(double delta);
void FrameEnd();
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&);
void MouseMotion(SDL_MouseMotionEvent const&);
void MouseButtonDown(SDL_MouseButtonEvent const&);
void MouseButtonUp(SDL_MouseButtonEvent const&);
void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&);
};
#endif
+5 -5
View File
@@ -40,11 +40,7 @@ void InGame::FrameStart() {
//
}
void InGame::FrameEnd() {
//
}
void InGame::Update() {
void InGame::Update(double delta) {
Receive();
}
@@ -87,6 +83,10 @@ void InGame::Receive() {
}
}
void InGame::FrameEnd() {
//
}
void InGame::Render(SDL_Surface* const screen) {
//
}
+11 -11
View File
@@ -12,22 +12,22 @@ class InGame : public BaseScene {
public:
//Public access members
InGame(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
virtual ~InGame();
~InGame();
protected:
//Frame loop
virtual void FrameStart();
virtual void FrameEnd();
virtual void Update();
virtual void Receive();
virtual void Render(SDL_Surface* const);
void FrameStart();
void Update(double delta);
void Receive();
void FrameEnd();
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&);
void MouseMotion(SDL_MouseMotionEvent const&);
void MouseButtonDown(SDL_MouseButtonEvent const&);
void MouseButtonUp(SDL_MouseButtonEvent const&);
void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&);
//members
ConfigUtility* configUtil = nullptr;
+9 -9
View File
@@ -31,7 +31,7 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti
listBox.x = 250;
listBox.y = 50;
listBox.w = GetScreen()->w - listBox.x;
listBox.h = font.GetClipH();
listBox.h = font.GetCharH();
//ping the network
PingNetwork();
@@ -54,11 +54,7 @@ void Lobby::FrameStart() {
//
}
void Lobby::FrameEnd() {
//
}
void Lobby::Update() {
void Lobby::Update(double delta) {
Receive();
}
@@ -109,6 +105,10 @@ void Lobby::Receive() {
}
}
void Lobby::FrameEnd() {
//
}
void Lobby::Render(SDL_Surface* const screen) {
for (auto it : buttonMap) {
it.second->DrawTo(screen);
@@ -118,7 +118,7 @@ void Lobby::Render(SDL_Surface* const screen) {
SDL_Rect clip;
for (int i = 0; i < serverVector.size(); i++) {
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 (selectedServer && selectedServer == &serverVector[i]) {
@@ -155,8 +155,8 @@ void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
SetNextScene(SceneList::MAINMENU);
}
//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) {
selectedServer = &serverVector[(button.y-listBox.y)/font.GetClipH()];
if (button.x > listBox.x && button.y > listBox.y && button.y < serverVector.size() * font.GetCharH() + listBox.y) {
selectedServer = &serverVector[(button.y-listBox.y)/font.GetCharH()];
}
else {
selectedServer = nullptr;
+11 -11
View File
@@ -19,22 +19,22 @@ class Lobby : public BaseScene {
public:
//Public access members
Lobby(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*, int* playerID);
virtual ~Lobby();
~Lobby();
protected:
//Frame loop
virtual void FrameStart();
virtual void FrameEnd();
virtual void Update();
virtual void Receive();
virtual void Render(SDL_Surface* const);
void FrameStart();
void Update(double delta);
void Receive();
void FrameEnd();
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&);
void MouseMotion(SDL_MouseMotionEvent const&);
void MouseButtonDown(SDL_MouseButtonEvent const&);
void MouseButtonUp(SDL_MouseButtonEvent const&);
void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&);
//utilities
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["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["testsystems"] = new Button(50, 250, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "TestSystems");
}
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) {
QuitEvent();
}
if (buttonMap["testsystems"]->MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::TESTSYSTEMS);
}
}
void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
+10 -10
View File
@@ -15,21 +15,21 @@ class MainMenu : public BaseScene {
public:
//Public access members
MainMenu(ConfigUtility*, SurfaceManager*);
virtual ~MainMenu();
~MainMenu();
protected:
//Frame loop
virtual void FrameStart();
virtual void FrameEnd();
virtual void Update();
virtual void Render(SDL_Surface* const);
void FrameStart();
void Update(double delta);
void FrameEnd();
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&);
void MouseMotion(SDL_MouseMotionEvent const&);
void MouseButtonDown(SDL_MouseButtonEvent const&);
void MouseButtonUp(SDL_MouseButtonEvent const&);
void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&);
//globals
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) {
FaceDirection(Direction::SOUTH);
}
+1 -1
View File
@@ -11,7 +11,7 @@ class Player {
public:
Player(SDL_Surface*, int w, int h);
void Update(int delta);
void Update(double delta);
void WalkInDirection(Direction);
+1 -1
View File
@@ -33,7 +33,7 @@ void PlayerManager::Delete(int index) {
playerMap.erase(it);
}
void PlayerManager::UpdateAll(int delta) {
void PlayerManager::UpdateAll(double delta) {
for (auto it : playerMap) {
it.second->Update(delta);
}
+1 -1
View File
@@ -14,7 +14,7 @@ public:
Player* Get(int index);
void Delete(int index);
void UpdateAll(int delta);
void UpdateAll(double delta);
void DrawAllTo(SDL_Surface* dest);
void DeleteAll();
+20 -9
View File
@@ -1,15 +1,14 @@
#include "scene_manager.hpp"
#include <stdexcept>
#include <chrono>
//-------------------------
//Scene headers
//-------------------------
//Add the custom scene headers here
#ifdef DEBUG
#include "test_systems.hpp"
#endif
#include "splash.hpp"
#include "main_menu.hpp"
@@ -50,6 +49,13 @@ void SceneManager::Init() {
void SceneManager::Proc() {
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
while(activeScene->GetNextScene() != SceneList::QUIT) {
//switch scenes when necessary
@@ -58,11 +64,18 @@ void SceneManager::Proc() {
continue;
}
//wipe the screen
SDL_FillRect(activeScene->GetScreen(), 0, 0);
//update the current time
realTime = Clock::now();
//call each user defined function
activeScene->RunFrame();
//simulate game time
while (simTime < realTime) {
//call each user defined function
activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den);
simTime += delta;
}
//draw the game to the screen
activeScene->RenderFrame();
//give the computer a break
SDL_Delay(10);
@@ -86,13 +99,11 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
switch(sceneIndex) {
//add scene creation calls here
case SceneList::FIRST:
#ifdef DEBUG
case SceneList::TESTSYSTEMS:
activeScene = new TestSystems(&configUtil, &surfaceMgr, &netUtil);
break;
#endif
case SceneList::FIRST:
case SceneList::SPLASH:
activeScene = new Splash(&configUtil, &surfaceMgr);
break;
+5 -3
View File
@@ -26,7 +26,7 @@ Splash::~Splash() {
#endif
}
void Splash::RunFrame() {
void Splash::RunFrame(double delta) {
//skip any event handling here
SDL_Event event;
while(SDL_PollEvent(&event));
@@ -38,12 +38,14 @@ void Splash::RunFrame() {
logo->DrawTo(GetScreen(),x,y);
SDL_Flip(GetScreen());
//load the resources ONCE
if (!loaded) {
LoadResources();
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);
}
}
+6 -4
View File
@@ -7,15 +7,17 @@
#include "surface_manager.hpp"
#include "image.hpp"
#include <ctime>
#include <chrono>
class Splash : public BaseScene {
public:
Splash(ConfigUtility*, SurfaceManager*);
virtual ~Splash();
~Splash();
protected:
virtual void RunFrame();
typedef std::chrono::high_resolution_clock Clock;
void RunFrame(double delta);
void RenderFrame() {};
void LoadResources();
@@ -25,7 +27,7 @@ protected:
//members
bool loaded = false;
time_t start = clock();
Clock::time_point start = Clock::now();
Image* logo = nullptr;
};
+5 -19
View File
@@ -18,20 +18,6 @@ TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkU
surfaceMgr = sMgr;
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;
playerMgr.New(playerCounter++, surfaceMgr->Get("elliot"));
@@ -57,15 +43,15 @@ void TestSystems::FrameStart() {
frameRate.Calculate();
}
void TestSystems::FrameEnd() {
//
}
void TestSystems::Update() {
void TestSystems::Update(double delta) {
// Delta::Calculate();
// playerMgr.UpdateAll(Delta::GetTime());
}
void TestSystems::FrameEnd() {
//
}
string IToS(int i) {
char buffer[20];
memset(buffer, 0, 20);
+10 -10
View File
@@ -18,21 +18,21 @@ class TestSystems : public BaseScene {
public:
//Public access members
TestSystems(ConfigUtility*, SurfaceManager*, UDPNetworkUtility*);
virtual ~TestSystems();
~TestSystems();
protected:
//Frame loop
virtual void FrameStart();
virtual void FrameEnd();
virtual void Update();
virtual void Render(SDL_Surface* const);
void FrameStart();
void Update(double delta);
void FrameEnd();
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&);
void MouseMotion(SDL_MouseMotionEvent const&);
void MouseButtonDown(SDL_MouseButtonEvent const&);
void MouseButtonUp(SDL_MouseButtonEvent const&);
void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&);
//utilities
void NewPlayer(int index, std::string avatarName, int x, int y);