From 231db701e439de4893261dbd553323621f54293f Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Tue, 4 Jun 2013 20:20:56 +1000 Subject: [PATCH] Spliced in updates to the scene system; program compiles & runs, player's don't move --- client/base_scene.cpp | 10 +++++++--- client/base_scene.hpp | 29 +++++++++++++++++++++++++---- client/combat.cpp | 4 ++-- client/combat.hpp | 20 ++++++++++---------- client/in_game.cpp | 10 +++++----- client/in_game.hpp | 22 +++++++++++----------- client/lobby.cpp | 18 +++++++++--------- client/lobby.hpp | 22 +++++++++++----------- client/main_menu.cpp | 9 +++++++-- client/main_menu.hpp | 20 ++++++++++---------- client/player.cpp | 2 +- client/player.hpp | 2 +- client/player_manager.cpp | 2 +- client/player_manager.hpp | 2 +- client/scene_manager.cpp | 29 ++++++++++++++++++++--------- client/splash.cpp | 8 +++++--- client/splash.hpp | 10 ++++++---- client/test_systems.cpp | 24 +++++------------------- client/test_systems.hpp | 20 ++++++++++---------- 19 files changed, 147 insertions(+), 116 deletions(-) diff --git a/client/base_scene.cpp b/client/base_scene.cpp index 7e1db60..ec1aabe 100644 --- a/client/base_scene.cpp +++ b/client/base_scene.cpp @@ -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(); } //------------------------- diff --git a/client/base_scene.hpp b/client/base_scene.hpp index 42f30a7..cbb26ca 100644 --- a/client/base_scene.hpp +++ b/client/base_scene.hpp @@ -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&) {} diff --git a/client/combat.cpp b/client/combat.cpp index a40b144..7b0ae16 100644 --- a/client/combat.cpp +++ b/client/combat.cpp @@ -28,11 +28,11 @@ void Combat::FrameStart() { // } -void Combat::FrameEnd() { +void Combat::Update(double delta) { // } -void Combat::Update() { +void Combat::FrameEnd() { // } diff --git a/client/combat.hpp b/client/combat.hpp index ad73ca9..fb521f3 100644 --- a/client/combat.hpp +++ b/client/combat.hpp @@ -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 diff --git a/client/in_game.cpp b/client/in_game.cpp index 070d832..82acb29 100644 --- a/client/in_game.cpp +++ b/client/in_game.cpp @@ -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) { // } diff --git a/client/in_game.hpp b/client/in_game.hpp index ec69d41..6a37fb9 100644 --- a/client/in_game.hpp +++ b/client/in_game.hpp @@ -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; diff --git a/client/lobby.cpp b/client/lobby.cpp index aa1dc39..44f7d17 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -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; diff --git a/client/lobby.hpp b/client/lobby.hpp index 5d1a0fa..41e2c24 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -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 { diff --git a/client/main_menu.cpp b/client/main_menu.cpp index 15cbc73..57675f7 100644 --- a/client/main_menu.cpp +++ b/client/main_menu.cpp @@ -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) { diff --git a/client/main_menu.hpp b/client/main_menu.hpp index 993c7db..e2d4c7a 100644 --- a/client/main_menu.hpp +++ b/client/main_menu.hpp @@ -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; diff --git a/client/player.cpp b/client/player.cpp index 162cc59..d499f85 100644 --- a/client/player.cpp +++ b/client/player.cpp @@ -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); } diff --git a/client/player.hpp b/client/player.hpp index e7a14e7..f6db5f9 100644 --- a/client/player.hpp +++ b/client/player.hpp @@ -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); diff --git a/client/player_manager.cpp b/client/player_manager.cpp index e217bdf..37b11d8 100644 --- a/client/player_manager.cpp +++ b/client/player_manager.cpp @@ -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); } diff --git a/client/player_manager.hpp b/client/player_manager.hpp index 9d5e3cf..00f8f59 100644 --- a/client/player_manager.hpp +++ b/client/player_manager.hpp @@ -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(); diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index c324624..6fcde5a 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -1,15 +1,14 @@ #include "scene_manager.hpp" #include +#include //------------------------- //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; diff --git a/client/splash.cpp b/client/splash.cpp index 922d358..032af99 100644 --- a/client/splash.cpp +++ b/client/splash.cpp @@ -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(1)) { SetNextScene(SceneList::MAINMENU); } } diff --git a/client/splash.hpp b/client/splash.hpp index a79ee0a..554d111 100644 --- a/client/splash.hpp +++ b/client/splash.hpp @@ -7,15 +7,17 @@ #include "surface_manager.hpp" #include "image.hpp" -#include +#include 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; }; diff --git a/client/test_systems.cpp b/client/test_systems.cpp index bcbaab9..96c8522 100644 --- a/client/test_systems.cpp +++ b/client/test_systems.cpp @@ -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); diff --git a/client/test_systems.hpp b/client/test_systems.hpp index ec1461e..d91cd90 100644 --- a/client/test_systems.hpp +++ b/client/test_systems.hpp @@ -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);