Compare commits
81 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 65135b4b86 | |||
| 51a0974d25 | |||
| a23156c425 | |||
| c03743ed0c | |||
| ee3877b906 | |||
| c4bcced984 | |||
| 40a40b4e11 | |||
| 43dadcdbb8 | |||
| 28b491587c | |||
| 977c2b24fb | |||
| 99bbe4be3a | |||
| 4dc05f8daf | |||
| b7205c278a | |||
| 6bee9b021d | |||
| a49b0eb1fe | |||
| 2089105695 | |||
| f667f503e7 | |||
| 50bef9736c | |||
| 6d1ba24404 | |||
| ebd8c5e6fc | |||
| 42f9c5e1df | |||
| 3f2fcdf9e1 | |||
| 2235f307e7 | |||
| 23782ff4e3 | |||
| 2a46f82f84 | |||
| 6a32599a69 | |||
| 0ca84a8653 | |||
| 66b2d6566c | |||
| 56375d64b6 | |||
| 9608761cd5 | |||
| c1d03d1cef | |||
| 09c88c7232 | |||
| ddb93cfcf1 | |||
| 42787dcb69 | |||
| fcb17a8116 | |||
| 2dc21f64fd | |||
| f049c96df7 | |||
| d9ffa22b76 | |||
| 24654d9e17 | |||
| 7ad855348f | |||
| d833b76856 | |||
| 3232925ccd | |||
| e4ffba80aa | |||
| a4d184ca75 | |||
| 89179626be | |||
| fc381348a5 | |||
| 5e99192fbf | |||
| 33adb4b1a5 | |||
| 33474cc6c0 | |||
| e833129983 | |||
| fd65fec5f7 | |||
| 69f03cd250 | |||
| 27ed91688a | |||
| 68ec7323a3 | |||
| 7b11bf22cb | |||
| d83a536f35 | |||
| ea761fb5bb | |||
| 685ca94335 | |||
| 5b2fd80a61 | |||
| d1aac9ffd8 | |||
| 3d92fb77b3 | |||
| 009e7b845b | |||
| 752dcadfa1 | |||
| 9599d82db3 | |||
| 7fef2501a3 | |||
| 419c9d8765 | |||
| a0fa874a29 | |||
| 5110ebc1b2 | |||
| 843053d307 | |||
| 2148c1f13e | |||
| f3ec4d4d8e | |||
| cbd388f4ed | |||
| 37a9e4268b | |||
| dfe0b4985a | |||
| 7ca7f7f015 | |||
| c21a95f3e9 | |||
| 0a48131de4 | |||
| 78326a3bbd | |||
| 253e9ec8fc | |||
| a1b248d1d7 | |||
| 2757911399 |
+2
-5
@@ -1,6 +1,6 @@
|
|||||||
#Editor generated files
|
#Editor generated files
|
||||||
*.sln
|
#*.sln
|
||||||
*.vcproj
|
#*.vcproj
|
||||||
*.suo
|
*.suo
|
||||||
*.ncb
|
*.ncb
|
||||||
*.user
|
*.user
|
||||||
@@ -9,9 +9,6 @@
|
|||||||
Release/
|
Release/
|
||||||
Debug/
|
Debug/
|
||||||
Out/
|
Out/
|
||||||
release/
|
|
||||||
debug/
|
|
||||||
out/
|
|
||||||
|
|
||||||
#Project generated files
|
#Project generated files
|
||||||
*.db
|
*.db
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
[submodule "bin"]
|
|
||||||
path = bin
|
|
||||||
url = https://github.com/krgamestudios/bin.git
|
|
||||||
[submodule "TurtleGUI"]
|
|
||||||
path = TurtleGUI
|
|
||||||
url = https://github.com/krgamestudios/TurtleGUI.git
|
|
||||||
[submodule "TurtleMap"]
|
|
||||||
path = TurtleMap
|
|
||||||
url = https://github.com/krgamestudios/TurtleMap.git
|
|
||||||
@@ -1,51 +1,23 @@
|
|||||||
## Outline
|
_This project is currently in it's early prototype stage. You can see other versions in various branches or tagged as prototype-X._
|
||||||
|
|
||||||
Tortuga is a 2D MMORPG featuring permadeath, with an emphasis on multiplayer cooperation, exploration and customization. The game runs on customizable public and private servers.
|
Tortuga is a 2D multiplayer role playing game set in a large archipelago occupied by warring pirate clans. The emphasis of this game is on multiplayer cooperation, competition, and server customization. The game runs on highly customizable server software that can support up to 150 simultaneous players or more. The player characters are tied to the server where they are created and are susceptible to permadeath: deletion of a character upon death.
|
||||||
|
|
||||||
This game is inspired by classic 2D RPGs (Final Fantasy, The Legend of Zelda), as well as more modern sandboxes and MMOs (Minecraft, EVE Online). This project is currently independently created and funded, with the goal of creating a game that will engage the players and inspire a large community.
|
## Dependencies
|
||||||
|
|
||||||
## Releases
|
* [SDL](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
||||||
|
* [SDL_net](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension
|
||||||
|
|
||||||
* The most recent stable build for Windows can be found [here](https://dl.dropboxusercontent.com/u/46669050/Tortuga-win.rar).
|
## Instructions
|
||||||
* The most recent stable build for Linux can be found [here](https://dl.dropboxusercontent.com/u/46669050/Tortuga-linux.tar).
|
|
||||||
|
|
||||||
## Patreon
|
* This project uses C++11, which is available via GNU (or MinGW) 4.7, or Visual Studio 2012. Personally, I'm using MinGW 4.7.2.
|
||||||
|
* I'm trying to keep this as IDE agnostic as possible, so if you use an IDE, please add it's files to .gitignore.
|
||||||
If you would like to support the development of Tortuga, please consider becoming a patron below.
|
* You can read more details on the Tortuga wiki [here](https://github.com/Ratstail91/Tortuga/wiki).
|
||||||
|
|
||||||
* [My Patreon Page](https://patreon.com/user?u=2811779)
|
|
||||||
|
|
||||||
## Documentation
|
|
||||||
|
|
||||||
* [Tortuga Wiki](https://github.com/Ratstail91/Tortuga/wiki) - Full documentation (incomplete)
|
|
||||||
* [Tortuga Bug Tracker](https://github.com/Ratstail91/Tortuga/issues) - A list of all known bugs and issues
|
|
||||||
|
|
||||||
## External Dependencies
|
|
||||||
|
|
||||||
* [SDL 2.0](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
|
||||||
* [SDL_image 2.0](https://www.libsdl.org/projects/SDL_image/) - An SDL Extension for loading multiple image file formats
|
|
||||||
* [SDL_net 2.0](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension
|
|
||||||
* [SDL_ttf 2.0](https://www.libsdl.org/projects/SDL_ttf/) - An SDL extention for rendering fonts
|
|
||||||
* [lua 5.2](http://www.lua.org/) - The lua programming language
|
|
||||||
* [SQLite3](http://www.sqlite.org/) - A lightweight SQL database engine
|
|
||||||
|
|
||||||
## Tools
|
|
||||||
|
|
||||||
* [WinRAR](http://www.rarlab.com/) - A free archive tool; needed for Windows distribution
|
|
||||||
* [tar](http://www.gnu.org/software/tar/manual/) - The GNU archive tool; needed for Linux distribution
|
|
||||||
* [Dropbox](https://www.dropbox.com/) - For hosting and distribution
|
|
||||||
|
|
||||||
## Using and Including
|
|
||||||
|
|
||||||
* [Coolvetica Typeface](http://typodermicfonts.com/coolvetica/)
|
|
||||||
|
|
||||||
## Copyright
|
## Copyright
|
||||||
|
|
||||||
(Future versions (to be determined) may be released under a modified version of the [Uplink Developer's License](http://www.introversion.co.uk/uplink/developer/license.html).)
|
|
||||||
|
|
||||||
The current version of Tortuga is released under the [zlib license](http://en.wikipedia.org/wiki/Zlib_License).
|
The current version of Tortuga is released under the [zlib license](http://en.wikipedia.org/wiki/Zlib_License).
|
||||||
|
|
||||||
Copyright (c) 2013-2016 Kayne Ruse
|
Copyright (c) 2013 Kayne Ruse
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
@@ -54,8 +26,3 @@ Permission is granted to anyone to use this software for any purpose, including
|
|||||||
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.
|
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.
|
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.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
### Items not made by me
|
|
||||||
|
|
||||||
* [Coolvetica Font](http://typodermicfonts.com/coolvetica/)
|
|
||||||
* Creative Commons Artwork from [Artsader](http://www.moosader.com/artsader/)
|
|
||||||
|
|||||||
-1
Submodule TurtleGUI deleted from 13e810df0a
-1
Submodule TurtleMap deleted from d6672e59d0
-1
Submodule bin deleted from 08e1382e66
+101
-71
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -21,89 +21,119 @@
|
|||||||
*/
|
*/
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
SDL_Renderer* BaseScene::rendererHandle = nullptr;
|
#include <stdexcept>
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Static declarations
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
SDL_Surface* BaseScene::screen = nullptr;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Public access members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
BaseScene::BaseScene() {
|
BaseScene::BaseScene() {
|
||||||
//EMPTY
|
nextScene = SceneList::CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseScene::~BaseScene() {
|
BaseScene::~BaseScene() {
|
||||||
//EMPTY
|
//
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::RenderFrame(SDL_Renderer* renderer) {
|
|
||||||
//EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::SetRenderer(SDL_Renderer* r) {
|
|
||||||
rendererHandle = r;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Renderer* BaseScene::GetRenderer() {
|
|
||||||
return rendererHandle;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::SetSceneSignal(SceneSignal signal) {
|
|
||||||
sceneSignal = signal;
|
|
||||||
}
|
|
||||||
|
|
||||||
SceneSignal BaseScene::GetSceneSignal() {
|
|
||||||
return sceneSignal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//frame phases
|
//Program control
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void BaseScene::FrameStart() {
|
SDL_Surface* BaseScene::SetScreen(int w, int h, int bpp, Uint32 flags) {
|
||||||
//EMPTY
|
if (!bpp) {
|
||||||
}
|
bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
|
||||||
|
|
||||||
void BaseScene::Update() {
|
|
||||||
//EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::FrameEnd() {
|
|
||||||
//EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//input events
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void BaseScene::QuitEvent() {
|
|
||||||
sceneSignal = SceneSignal::QUIT;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|
||||||
//EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
|
||||||
//EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
|
||||||
//EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::MouseWheel(SDL_MouseWheelEvent const& event) {
|
|
||||||
//EMPTY
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseScene::KeyDown(SDL_KeyboardEvent const& event) {
|
|
||||||
//preference as a default
|
|
||||||
switch(event.keysym.sym) {
|
|
||||||
case SDLK_ESCAPE:
|
|
||||||
QuitEvent();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
screen = SDL_SetVideoMode(w, h, bpp, flags);
|
||||||
|
|
||||||
|
if (!screen) {
|
||||||
|
throw(std::runtime_error("Failed to create the screen surface"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return screen;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseScene::KeyUp(SDL_KeyboardEvent const& event) {
|
SDL_Surface* BaseScene::GetScreen() {
|
||||||
//EMPTY
|
return screen;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BaseScene::TextInput(SDL_TextInputEvent const& event) {
|
SceneList BaseScene::SetNextScene(SceneList sceneIndex) {
|
||||||
//EMPTY
|
return nextScene = sceneIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
SceneList BaseScene::GetNextScene() const {
|
||||||
|
return nextScene;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Frame loop
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void BaseScene::RunFrame(double delta) {
|
||||||
|
FrameStart();
|
||||||
|
HandleEvents();
|
||||||
|
Update(delta);
|
||||||
|
FrameEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BaseScene::RenderFrame() {
|
||||||
|
SDL_FillRect(screen, 0, 0);
|
||||||
|
Render(screen);
|
||||||
|
SDL_Flip(screen);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//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
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-28
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,44 +19,56 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef BASESCENE_HPP_
|
||||||
|
#define BASESCENE_HPP_
|
||||||
|
|
||||||
#include "scene_signal.hpp"
|
#include "scene_list.hpp"
|
||||||
|
|
||||||
#include "SDL2/SDL.h"
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
class BaseScene {
|
class BaseScene {
|
||||||
public:
|
public:
|
||||||
|
/* Public access members */
|
||||||
BaseScene();
|
BaseScene();
|
||||||
virtual ~BaseScene();
|
virtual ~BaseScene();
|
||||||
|
|
||||||
virtual void RenderFrame(SDL_Renderer*);
|
/* Program control */
|
||||||
static void SetRenderer(SDL_Renderer*);
|
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||||
SceneSignal GetSceneSignal();
|
static SDL_Surface* GetScreen();
|
||||||
|
|
||||||
//frame phases
|
SceneList SetNextScene(SceneList sceneIndex);
|
||||||
virtual void FrameStart();
|
SceneList GetNextScene() const;
|
||||||
virtual void Update();
|
|
||||||
virtual void FrameEnd();
|
|
||||||
|
|
||||||
//input events
|
/* Frame loop */
|
||||||
virtual void QuitEvent();
|
virtual void RunFrame(double delta);
|
||||||
virtual void MouseMotion(SDL_MouseMotionEvent const& event);
|
virtual void RenderFrame();
|
||||||
virtual void MouseButtonDown(SDL_MouseButtonEvent const& event);
|
|
||||||
virtual void MouseButtonUp(SDL_MouseButtonEvent const& event);
|
|
||||||
virtual void MouseWheel(SDL_MouseWheelEvent const& event);
|
|
||||||
virtual void KeyDown(SDL_KeyboardEvent const& event);
|
|
||||||
virtual void KeyUp(SDL_KeyboardEvent const& event);
|
|
||||||
virtual void TextInput(SDL_TextInputEvent const& event);
|
|
||||||
|
|
||||||
//TODO: joystick and controller events
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//control
|
virtual void FrameStart() {}
|
||||||
static SDL_Renderer* GetRenderer();
|
virtual void HandleEvents();
|
||||||
void SetSceneSignal(SceneSignal);
|
virtual void Update(double delta) {}
|
||||||
|
virtual void FrameEnd() {}
|
||||||
|
virtual void Render(SDL_Surface* const screen) {}
|
||||||
|
|
||||||
|
/* Event handlers */
|
||||||
|
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:
|
private:
|
||||||
static SDL_Renderer* rendererHandle;
|
static SDL_Surface* screen;
|
||||||
SceneSignal sceneSignal = SceneSignal::CONTINUE;
|
SceneList nextScene;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
+104
-294
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -21,352 +21,162 @@
|
|||||||
*/
|
*/
|
||||||
#include "client_application.hpp"
|
#include "client_application.hpp"
|
||||||
|
|
||||||
#include "serial_packet.hpp"
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
//for handling platforms
|
|
||||||
#include "SDL2/SDL_syswm.h"
|
|
||||||
#include "SDL2/SDL_version.h"
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Scene headers
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
//Add the custom scene headers here
|
||||||
|
#include "in_combat.hpp"
|
||||||
|
#include "in_world.hpp"
|
||||||
|
#include "lobby.hpp"
|
||||||
|
#include "main_menu.hpp"
|
||||||
|
#include "option_screen.hpp"
|
||||||
|
#include "splash_screen.hpp"
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ClientApplication::Init(int argc, char* argv[]) {
|
ClientApplication::ClientApplication() {
|
||||||
std::cout << "Beginning " << argv[0] << std::endl;
|
//
|
||||||
|
}
|
||||||
|
|
||||||
//load the prerequisites
|
ClientApplication::~ClientApplication() {
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
UnloadScene();
|
||||||
config.Load("rsc/config.cfg", false, argc, argv);
|
}
|
||||||
|
|
||||||
//-------------------------
|
/* ClientApplication::Init()
|
||||||
//Initialize SDL
|
* This function initializes the entire program. There are a number of things
|
||||||
//-------------------------
|
* that could go wrong here, which is why there is such an unusual order of
|
||||||
|
* operations.
|
||||||
|
* Important things to note:
|
||||||
|
* The APIs are initiated here.
|
||||||
|
* The global objects are initialized here.
|
||||||
|
* The game's screen is created here, based on information loaded from the config file.
|
||||||
|
* The ConfigUtility's call to Load() also ensures that the "rsc\" folder is in the directory. It's easy to forget it.
|
||||||
|
*/
|
||||||
|
|
||||||
if (SDL_Init(0)) {
|
void ClientApplication::Init() {
|
||||||
std::ostringstream msg;
|
//load the config file
|
||||||
msg << "Failed to initialize SDL: " << SDL_GetError();
|
try {
|
||||||
throw(std::runtime_error(msg.str()));
|
configUtil->Load("rsc/config.cfg");
|
||||||
|
}
|
||||||
|
catch(std::runtime_error& e) {
|
||||||
|
std::string s = e.what();
|
||||||
|
s += "; Ensure that the \"rsc\" directory is present";
|
||||||
|
throw(std::runtime_error(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//initialize the APIs
|
||||||
//create and check the window
|
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
//-------------------------
|
throw(std::runtime_error("Failed to initialize SDL"));
|
||||||
|
|
||||||
//get the config values
|
|
||||||
int w = config.Int("client.screen.w");
|
|
||||||
int h = config.Int("client.screen.h");
|
|
||||||
int f = config.Boolean("client.screen.f") ? SDL_WINDOW_FULLSCREEN : 0;
|
|
||||||
|
|
||||||
//BUG: fullscreen is disabled
|
|
||||||
f = 0;
|
|
||||||
|
|
||||||
//default sizes
|
|
||||||
w = w ? w : 800;
|
|
||||||
h = h ? h : 600;
|
|
||||||
|
|
||||||
window = SDL_CreateWindow(argv[0], SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, f);
|
|
||||||
|
|
||||||
if (!window) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to create the window: " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Initialized the window" << std::endl;
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//DEBUG: detecting platforms, versions & appropriate fonts
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
SDL_SysWMinfo windowInfo;
|
|
||||||
SDL_VERSION(&windowInfo.version);
|
|
||||||
if (SDL_GetWindowWMInfo(window, &windowInfo)) {
|
|
||||||
//
|
|
||||||
std::string platform;
|
|
||||||
std::string fontPath;
|
|
||||||
|
|
||||||
//get the info
|
|
||||||
switch(windowInfo.subsystem) {
|
|
||||||
case SDL_SYSWM_WINDOWS:
|
|
||||||
platform = "Microsoft Windows";
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_SYSWM_X11:
|
|
||||||
platform = "X Window System";
|
|
||||||
break;
|
|
||||||
|
|
||||||
//NOTE: OS X is currently unsupported, but it could be
|
|
||||||
case SDL_SYSWM_COCOA:
|
|
||||||
platform = "Apple OS X";
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
platform = "an unsupported platform";
|
|
||||||
}
|
|
||||||
|
|
||||||
//final output
|
|
||||||
std::cout << "SDL Version ";
|
|
||||||
std::cout << (int)windowInfo.version.major << ".";
|
|
||||||
std::cout << (int)windowInfo.version.minor << ".";
|
|
||||||
std::cout << (int)windowInfo.version.patch << " on ";
|
|
||||||
std::cout << platform << std::endl;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to retrieve window info: " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//create and check the renderer
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
|
||||||
|
|
||||||
if (!renderer) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to create the renderer: " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//screen scaling
|
|
||||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
|
|
||||||
SDL_RenderSetLogicalSize(renderer, w, h);
|
|
||||||
|
|
||||||
//set the hook for the renderer
|
|
||||||
BaseScene::SetRenderer(renderer);
|
|
||||||
|
|
||||||
std::cout << "Initialized the renderer" << std::endl;
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Initialize the APIs
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//initialize SDL_net
|
|
||||||
if (SDLNet_Init()) {
|
if (SDLNet_Init()) {
|
||||||
std::ostringstream msg;
|
throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||||
msg << "Failed to initialize SDL_net 2.0: " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
|
||||||
UDPNetworkUtility::GetSingleton().Open(0);
|
|
||||||
|
|
||||||
std::cout << "Initialized SDL_net 2.0" << std::endl;
|
|
||||||
|
|
||||||
//setting up SDL2_ttf
|
|
||||||
if (TTF_Init()) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to initialize SDL_ttf 2.0: " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Initialized SDL_ttf 2.0" << std::endl;
|
//create the screen
|
||||||
|
Uint32 flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
|
||||||
|
flags |= configUtil->Bool("screen.f") ? SDL_FULLSCREEN : 0;
|
||||||
|
|
||||||
//-------------------------
|
BaseScene::SetScreen(
|
||||||
//debug output
|
configUtil->Int("screen.w"),
|
||||||
//-------------------------
|
configUtil->Int("screen.h"),
|
||||||
|
SDL_GetVideoInfo()->vfmt->BitsPerPixel,
|
||||||
|
flags);
|
||||||
|
|
||||||
#define DEBUG_INTERNAL_VAR(x) std::cout << "\t" << #x << ": " << x << std::endl;
|
//initiate the remaining singletons
|
||||||
|
netUtil->Open(0, sizeof(Packet));
|
||||||
std::cout << "Internal sizes:" << std::endl;
|
|
||||||
|
|
||||||
DEBUG_INTERNAL_VAR(NETWORK_VERSION);
|
|
||||||
DEBUG_INTERNAL_VAR(sizeof(Region::type_t));
|
|
||||||
DEBUG_INTERNAL_VAR(sizeof(Region));
|
|
||||||
DEBUG_INTERNAL_VAR(REGION_WIDTH);
|
|
||||||
DEBUG_INTERNAL_VAR(REGION_HEIGHT);
|
|
||||||
DEBUG_INTERNAL_VAR(REGION_DEPTH);
|
|
||||||
DEBUG_INTERNAL_VAR(REGION_TILE_FOOTPRINT);
|
|
||||||
DEBUG_INTERNAL_VAR(REGION_SOLID_FOOTPRINT);
|
|
||||||
DEBUG_INTERNAL_VAR(PACKET_STRING_SIZE);
|
|
||||||
DEBUG_INTERNAL_VAR(PACKET_BUFFER_SIZE);
|
|
||||||
DEBUG_INTERNAL_VAR(MAX_PACKET_SIZE);
|
|
||||||
DEBUG_INTERNAL_VAR(static_cast<int>(SerialPacketType::LAST));
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//BUGFIX
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
SDL_StopTextInput();
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//finalize the startup
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
std::cout << "Startup completed successfully" << std::endl;
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//debugging
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientApplication::Proc() {
|
void ClientApplication::Proc() {
|
||||||
//load the first scene
|
LoadScene(SceneList::FIRST);
|
||||||
ProcessSceneSignal(SceneSignal::FIRST);
|
|
||||||
|
|
||||||
//fixed frame rate
|
//prepare the time system
|
||||||
typedef std::chrono::steady_clock Clock;
|
typedef std::chrono::high_resolution_clock Clock;
|
||||||
|
|
||||||
|
Clock::duration delta(5 * Clock::duration::period::den / std::milli::den);
|
||||||
Clock::time_point simTime = Clock::now();
|
Clock::time_point simTime = Clock::now();
|
||||||
Clock::time_point realTime;
|
Clock::time_point realTime;
|
||||||
constexpr std::chrono::duration<int, std::milli> frameDelay(16); //~60FPS
|
|
||||||
|
|
||||||
//the game loop continues until the scenes signal QUIT
|
//The main loop
|
||||||
while(activeScene->GetSceneSignal() != SceneSignal::QUIT) {
|
while(activeScene->GetNextScene() != SceneList::QUIT) {
|
||||||
//switch scenes if necessary
|
//switch scenes when necessary
|
||||||
if(activeScene->GetSceneSignal() != SceneSignal::CONTINUE) {
|
if (activeScene->GetNextScene() != SceneList::CONTINUE) {
|
||||||
ProcessSceneSignal(activeScene->GetSceneSignal());
|
LoadScene(activeScene->GetNextScene());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//update the current time
|
//update the current time & give the computer as much of a break as possible
|
||||||
realTime = Clock::now();
|
while ((realTime = Clock::now()) < simTime) {
|
||||||
|
|
||||||
//simulate the game or give the machine a break
|
|
||||||
if (simTime < realTime) {
|
|
||||||
while(simTime < realTime) {
|
|
||||||
//call the user defined functions
|
|
||||||
activeScene->FrameStart();
|
|
||||||
ProcessEvents();
|
|
||||||
activeScene->Update();
|
|
||||||
activeScene->FrameEnd();
|
|
||||||
|
|
||||||
//step to the next frame
|
|
||||||
simTime += frameDelay;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
SDL_Delay(1);
|
SDL_Delay(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_RenderClear(renderer);
|
//simulate game time
|
||||||
activeScene->RenderFrame(renderer);
|
while (simTime < realTime) {
|
||||||
SDL_RenderPresent(renderer);
|
//call each user defined function
|
||||||
|
activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den);
|
||||||
|
simTime += delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
//draw the game to the screen
|
||||||
|
activeScene->RenderFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
//cleanup
|
UnloadScene();
|
||||||
ClearScene();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientApplication::Quit() {
|
void ClientApplication::Quit() {
|
||||||
//clean up after the program
|
//clean up the singletons
|
||||||
std::cout << "Shutting down" << std::endl;
|
netUtil->Close();
|
||||||
UDPNetworkUtility::GetSingleton().Close();
|
surfaceMgr->FreeAll();
|
||||||
TTF_Quit();
|
|
||||||
|
//clean up the scene
|
||||||
|
UnloadScene();
|
||||||
|
|
||||||
|
//deinitialize the APIs
|
||||||
SDLNet_Quit();
|
SDLNet_Quit();
|
||||||
BaseScene::SetRenderer(nullptr);
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
std::cout << "Clean exit" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Scene management
|
//Private access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void ClientApplication::ProcessEvents() {
|
void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||||
SDL_Event event;
|
UnloadScene();
|
||||||
while(SDL_PollEvent(&event)) {
|
|
||||||
switch(event.type) {
|
|
||||||
case SDL_QUIT:
|
|
||||||
activeScene->QuitEvent();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_MOUSEMOTION:
|
switch(sceneIndex) {
|
||||||
activeScene->MouseMotion(event.motion);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
|
||||||
activeScene->MouseButtonDown(event.button);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONUP:
|
|
||||||
activeScene->MouseButtonUp(event.button);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_MOUSEWHEEL:
|
|
||||||
activeScene->MouseWheel(event.wheel);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
|
||||||
activeScene->KeyDown(event.key);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_KEYUP:
|
|
||||||
activeScene->KeyUp(event.key);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDL_TEXTINPUT:
|
|
||||||
activeScene->TextInput(event.text);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//TODO: (9) joystick and controller events
|
|
||||||
|
|
||||||
//window events are handled internally
|
|
||||||
case SDL_WINDOWEVENT:
|
|
||||||
switch(event.window.event) {
|
|
||||||
case SDL_WINDOWEVENT_RESIZED:
|
|
||||||
SDL_RenderSetLogicalSize(renderer, event.window.data1, event.window.data2);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Add the custom scene headers here
|
|
||||||
#include "splash_screen.hpp"
|
|
||||||
#include "main_menu.hpp"
|
|
||||||
#include "options_menu.hpp"
|
|
||||||
#include "lobby_menu.hpp"
|
|
||||||
#include "world.hpp"
|
|
||||||
#include "combat.hpp"
|
|
||||||
#include "disconnected_screen.hpp"
|
|
||||||
|
|
||||||
void ClientApplication::ProcessSceneSignal(SceneSignal signal) {
|
|
||||||
//BUG: #16 Resources are being reloaded between scenes
|
|
||||||
ClearScene();
|
|
||||||
switch(signal) {
|
|
||||||
//add scene creation calls here
|
//add scene creation calls here
|
||||||
case SceneSignal::FIRST:
|
case SceneList::INCOMBAT:
|
||||||
case SceneSignal::SPLASHSCREEN:
|
activeScene = new InCombat();
|
||||||
activeScene = new SplashScreen(window);
|
|
||||||
break;
|
break;
|
||||||
case SceneSignal::MAINMENU:
|
case SceneList::INWORLD:
|
||||||
|
activeScene = new InWorld();
|
||||||
|
break;
|
||||||
|
case SceneList::LOBBY:
|
||||||
|
activeScene = new Lobby();
|
||||||
|
break;
|
||||||
|
case SceneList::MAINMENU:
|
||||||
activeScene = new MainMenu();
|
activeScene = new MainMenu();
|
||||||
break;
|
break;
|
||||||
case SceneSignal::OPTIONSMENU:
|
case SceneList::OPTIONSCREEN:
|
||||||
activeScene = new OptionsMenu();
|
activeScene = new OptionScreen();
|
||||||
break;
|
break;
|
||||||
case SceneSignal::LOBBYMENU:
|
case SceneList::FIRST:
|
||||||
activeScene = new LobbyMenu(&clientIndex, &accountIndex);
|
case SceneList::SPLASHSCREEN:
|
||||||
|
activeScene = new SplashScreen();
|
||||||
break;
|
break;
|
||||||
case SceneSignal::WORLD:
|
|
||||||
activeScene = new World(&clientIndex, &accountIndex);
|
default:
|
||||||
break;
|
throw(std::logic_error("Failed to recognize the scene index"));
|
||||||
case SceneSignal::COMBAT:
|
|
||||||
activeScene = new Combat(&clientIndex, &accountIndex);
|
|
||||||
break;
|
|
||||||
case SceneSignal::DISCONNECTEDSCREEN:
|
|
||||||
activeScene = new DisconnectedScreen();
|
|
||||||
break;
|
|
||||||
default: {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to recognize the scene signal: " << signal;
|
|
||||||
throw(std::logic_error(msg.str()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientApplication::ClearScene() {
|
void ClientApplication::UnloadScene() {
|
||||||
delete activeScene;
|
delete activeScene;
|
||||||
activeScene = nullptr;
|
activeScene = nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,41 +19,44 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef CLIENTAPPLICATION_HPP_
|
||||||
|
#define CLIENTAPPLICATION_HPP_
|
||||||
|
|
||||||
|
#include "scene_list.hpp"
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
#include "scene_signal.hpp"
|
|
||||||
#include "singleton.hpp"
|
#include "singleton.hpp"
|
||||||
|
#include "packet.hpp"
|
||||||
|
#include "information_manager.hpp"
|
||||||
|
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
#include "surface_manager.hpp"
|
||||||
#include "udp_network_utility.hpp"
|
#include "udp_network_utility.hpp"
|
||||||
|
|
||||||
#include "SDL2/SDL.h"
|
#include "SDL/SDL.h"
|
||||||
#include "SDL2/SDL_net.h"
|
|
||||||
#include "SDL2/SDL_ttf.h"
|
|
||||||
|
|
||||||
class ClientApplication: public Singleton<ClientApplication> {
|
class ClientApplication {
|
||||||
public:
|
public:
|
||||||
void Init(int argc, char* argv[]);
|
/* Public access members */
|
||||||
|
ClientApplication();
|
||||||
|
~ClientApplication();
|
||||||
|
|
||||||
|
void Init();
|
||||||
void Proc();
|
void Proc();
|
||||||
void Quit();
|
void Quit();
|
||||||
|
|
||||||
|
ClientApplication(ClientApplication const&) = delete;
|
||||||
|
ClientApplication(ClientApplication const&&) = delete;
|
||||||
private:
|
private:
|
||||||
friend Singleton<ClientApplication>;
|
/* Private access members */
|
||||||
|
void LoadScene(SceneList sceneIndex);
|
||||||
ClientApplication() = default;
|
void UnloadScene();
|
||||||
~ClientApplication() = default;
|
|
||||||
|
|
||||||
//scene management
|
|
||||||
void ProcessEvents();
|
|
||||||
void ProcessSceneSignal(SceneSignal);
|
|
||||||
void ClearScene();
|
|
||||||
|
|
||||||
BaseScene* activeScene = nullptr;
|
BaseScene* activeScene = nullptr;
|
||||||
|
|
||||||
//TODO: (9) build a "window" class?
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
SDL_Window* window = nullptr;
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
SDL_Renderer* renderer = nullptr;
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
|
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||||
|
};
|
||||||
|
|
||||||
//global scene parameters
|
#endif
|
||||||
int clientIndex = -1;
|
|
||||||
int accountIndex = -1;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -1,145 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "image.hpp"
|
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <map>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
template<typename T = class Image>
|
|
||||||
class CompositeImage {
|
|
||||||
public:
|
|
||||||
CompositeImage() = default;
|
|
||||||
~CompositeImage() = default;
|
|
||||||
|
|
||||||
void Load(SDL_Renderer* const, std::string spriteDir, std::list<std::string> nameList);
|
|
||||||
void SetTextures(std::map<std::string, Image>& templateImages);
|
|
||||||
void Free();
|
|
||||||
|
|
||||||
void DrawTo(SDL_Renderer* const, Sint16 x, Sint16 y, double scaleX = 1.0, double scaleY = 1.0);
|
|
||||||
|
|
||||||
//accessors & mutators
|
|
||||||
T* Find(std::string name);
|
|
||||||
|
|
||||||
bool Enable(std::string name);
|
|
||||||
bool Disable(std::string name);
|
|
||||||
void EnableAll();
|
|
||||||
void DisableAll();
|
|
||||||
|
|
||||||
std::map<std::string, std::pair<bool, T>>* GetTemplateImages();
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::map<std::string, std::pair<bool, T>> imageMap;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void CompositeImage<T>::Load(SDL_Renderer* const renderer, std::string spriteDir, std::list<std::string> nameList) {
|
|
||||||
for (auto& it : nameList) {
|
|
||||||
imageMap[it].first = true;
|
|
||||||
imageMap[it].second.Load(renderer, spriteDir + it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void CompositeImage<T>::SetTextures(std::map<std::string, Image>& templateImages) {
|
|
||||||
for (auto& it : templateImages) {
|
|
||||||
imageMap[it.first].first = true;
|
|
||||||
imageMap[it.first].second.SetTexture(it.second.GetTexture());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void CompositeImage<T>::Free() {
|
|
||||||
imageMap.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void CompositeImage<T>::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) {
|
|
||||||
//draw all members, regardless of internal ordering
|
|
||||||
for (auto& it : imageMap) {
|
|
||||||
if (it.second.first) {
|
|
||||||
it.second.second.DrawTo(dest, x, y, scaleX, scaleY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//accessors & mutators
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
T* CompositeImage<T>::Find(std::string name) {
|
|
||||||
auto it = imageMap.find(name);
|
|
||||||
if (it == imageMap.end()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return &it->second.second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
bool CompositeImage<T>::Enable(std::string name) {
|
|
||||||
auto it = imageMap.find(name);
|
|
||||||
if (it == imageMap.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
imageMap[name].first = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
bool CompositeImage<T>::Disable(std::string name) {
|
|
||||||
auto it = imageMap.find(name);
|
|
||||||
if (it == imageMap.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
imageMap[name].first = false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void CompositeImage<T>::EnableAll() {
|
|
||||||
for (auto& it : imageMap) {
|
|
||||||
it.second.first = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void CompositeImage<T>::DisableAll() {
|
|
||||||
for (auto& it : imageMap) {
|
|
||||||
it.second.first = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::map<std::string, std::pair<bool, T>>* CompositeImage<T>::GetTemplateImages() {
|
|
||||||
return &imageMap;
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "barrier_manager.hpp"
|
|
||||||
|
|
||||||
void BarrierManager::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) {
|
|
||||||
for (auto& it : elementMap) {
|
|
||||||
it.second.DrawTo(dest, x, y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::LoadBaseImage(SDL_Renderer* renderer, std::string fname) {
|
|
||||||
baseImage.Load(renderer, fname);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::UnloadBaseImage() {
|
|
||||||
baseImage.Free();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::LoadTemplateImages(SDL_Renderer* renderer, std::string spriteDir, std::list<std::string> names) {
|
|
||||||
//sprite names are file names only
|
|
||||||
for (auto& it : names) {
|
|
||||||
templateImages.emplace(it, Image(renderer, spriteDir + it));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::UnloadTemplateImages() {
|
|
||||||
templateImages.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
BaseBarrier* BarrierManager::Create(int index) {
|
|
||||||
elementMap.emplace(index, BaseBarrier(baseImage, templateImages));
|
|
||||||
return &elementMap[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::Unload(int i) {
|
|
||||||
elementMap.erase(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::UnloadAll() {
|
|
||||||
elementMap.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BarrierManager::UnloadIf(std::function<bool(std::pair<const int, BaseBarrier const&>)> fn) {
|
|
||||||
std::map<int, BaseBarrier>::iterator it = elementMap.begin();
|
|
||||||
while (it != elementMap.end()) {
|
|
||||||
if (fn(*it)) {
|
|
||||||
it = elementMap.erase(it);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int BarrierManager::Size() {
|
|
||||||
return elementMap.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
BaseBarrier* BarrierManager::Find(int i) {
|
|
||||||
std::map<int, BaseBarrier>::iterator it = elementMap.find(i);
|
|
||||||
|
|
||||||
if (it == elementMap.end()) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &it->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<int, BaseBarrier>* BarrierManager::GetContainer() {
|
|
||||||
return &elementMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::map<std::string, Image>* BarrierManager::GetTemplateContainer() {
|
|
||||||
return &templateImages;
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "base_barrier.hpp"
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <list>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class BarrierManager {
|
|
||||||
public:
|
|
||||||
BarrierManager() = default;
|
|
||||||
~BarrierManager() = default;
|
|
||||||
|
|
||||||
void DrawTo(SDL_Renderer* const, Sint16 x, Sint16 y, double scaleX = 1.0, double scaleY = 1.0);
|
|
||||||
|
|
||||||
//NOTE: don't use these while you have barriers loaded
|
|
||||||
void LoadBaseImage(SDL_Renderer* renderer, std::string fname);
|
|
||||||
void UnloadBaseImage();
|
|
||||||
void LoadTemplateImages(SDL_Renderer* renderer, std::string spriteDir, std::list<std::string> names);
|
|
||||||
void UnloadTemplateImages();
|
|
||||||
|
|
||||||
BaseBarrier* Create(int index);
|
|
||||||
void Unload(int i);
|
|
||||||
void UnloadAll();
|
|
||||||
void UnloadIf(std::function<bool(std::pair<const int, BaseBarrier const&>)> fn);
|
|
||||||
|
|
||||||
int Size();
|
|
||||||
|
|
||||||
BaseBarrier* Find(int i);
|
|
||||||
std::map<int, BaseBarrier>* GetContainer();
|
|
||||||
std::map<std::string, Image>* GetTemplateContainer();
|
|
||||||
|
|
||||||
private:
|
|
||||||
Image baseImage;
|
|
||||||
std::map<std::string, Image> templateImages;
|
|
||||||
std::map<int, BaseBarrier> elementMap;
|
|
||||||
};
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "base_barrier.hpp"
|
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
BaseBarrier::BaseBarrier(Image& argBaseImage, std::map<std::string, Image>& templateImages) {
|
|
||||||
baseImage.SetTexture(argBaseImage.GetTexture());
|
|
||||||
composite.SetTextures(templateImages);
|
|
||||||
memset(status, 0, sizeof(int) * 8);
|
|
||||||
CorrectSprite();
|
|
||||||
}
|
|
||||||
|
|
||||||
BaseBarrier::~BaseBarrier() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseBarrier::CorrectSprite() {
|
|
||||||
//TODO: link status to sprite
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
//setup the name
|
|
||||||
std::ostringstream os;
|
|
||||||
os << "slot " << i+1;
|
|
||||||
|
|
||||||
switch(status[i]) {
|
|
||||||
case 0:
|
|
||||||
composite.Disable(os.str() + " green.png");
|
|
||||||
composite.Disable(os.str() + " red.png");
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
composite.Enable(os.str() + " green.png");
|
|
||||||
composite.Disable(os.str() + " red.png");
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
composite.Disable(os.str() + " green.png");
|
|
||||||
composite.Enable(os.str() + " red.png");
|
|
||||||
break;
|
|
||||||
default: {
|
|
||||||
std::ostringstream os;
|
|
||||||
os << "index " << i << ", value " << status[i] << std::endl;
|
|
||||||
throw(std::runtime_error("Unknown graphical status in barrier; " + os.str()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseBarrier::Update() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void BaseBarrier::DrawTo(SDL_Renderer* const dest, int camX, int camY) {
|
|
||||||
//ignore the default sprite for now
|
|
||||||
baseImage.DrawTo(dest, origin.x - camX, origin.y - camY);
|
|
||||||
composite.DrawTo(dest, origin.x - camX, origin.y - camY);
|
|
||||||
}
|
|
||||||
|
|
||||||
int BaseBarrier::SetStatus(int k, int v) {
|
|
||||||
if (k >= 8 || k < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return status[k] = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
int BaseBarrier::FindStatus(int k) {
|
|
||||||
if (k >= 8 || k < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return status[k];
|
|
||||||
}
|
|
||||||
|
|
||||||
int* BaseBarrier::SetStatusArray(int* ptr) {
|
|
||||||
memcpy(status, ptr, sizeof(int) * 8);
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
int* BaseBarrier::GetStatusArray() {
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
CompositeImage<>* BaseBarrier::GetComposite() {
|
|
||||||
return &composite;
|
|
||||||
}
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "base_character.hpp"
|
|
||||||
|
|
||||||
//TODO: (3) remove this
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//graphics
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void BaseCharacter::CorrectSprite() {
|
|
||||||
//NOTE: These must correspond to the sprite sheet in use
|
|
||||||
if (motion.y > 0) {
|
|
||||||
sprite.SetIndexY(0);
|
|
||||||
}
|
|
||||||
else if (motion.y < 0) {
|
|
||||||
sprite.SetIndexY(1);
|
|
||||||
}
|
|
||||||
else if (motion.x > 0) {
|
|
||||||
sprite.SetIndexY(3);
|
|
||||||
}
|
|
||||||
else if (motion.x < 0) {
|
|
||||||
sprite.SetIndexY(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//animation
|
|
||||||
if (motion != 0) {
|
|
||||||
sprite.SetDelay(0.1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sprite.SetDelay(0);
|
|
||||||
sprite.SetIndexX(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//metadata
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
int BaseCharacter::SetOwner(int i) {
|
|
||||||
return owner = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
int BaseCharacter::GetOwner() {
|
|
||||||
return owner;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCharacter::SetHandle(std::string s) {
|
|
||||||
return handle = s;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCharacter::GetHandle() const {
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCharacter::SetAvatar(SDL_Renderer* const renderer, std::string s) {
|
|
||||||
avatar = s;
|
|
||||||
sprite.Load(renderer, ConfigUtility::GetSingleton()["dir.sprites"] + avatar, CHARACTER_CELLS_X, CHARACTER_CELLS_Y);
|
|
||||||
return avatar;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCharacter::GetAvatar() const {
|
|
||||||
return avatar;
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
//components
|
|
||||||
#include "character_defines.hpp"
|
|
||||||
#include "entity.hpp"
|
|
||||||
|
|
||||||
//std namespace
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class BaseCharacter: public Entity {
|
|
||||||
public:
|
|
||||||
BaseCharacter() = default;
|
|
||||||
virtual ~BaseCharacter() = default;
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
void CorrectSprite();
|
|
||||||
|
|
||||||
//metadata
|
|
||||||
int SetOwner(int i);
|
|
||||||
int GetOwner();
|
|
||||||
std::string SetHandle(std::string s);
|
|
||||||
std::string GetHandle() const;
|
|
||||||
std::string SetAvatar(SDL_Renderer* const, std::string s);
|
|
||||||
std::string GetAvatar() const;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
//metadata
|
|
||||||
int owner;
|
|
||||||
std::string handle;
|
|
||||||
std::string avatar;
|
|
||||||
};
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "base_creature.hpp"
|
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
void BaseCreature::CorrectSprite() {
|
|
||||||
//TODO: (9) BaseCreature::CorrectSprite()
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCreature::SetHandle(std::string s) {
|
|
||||||
return handle = s;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCreature::GetHandle() const {
|
|
||||||
return handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCreature::SetAvatar(SDL_Renderer* const renderer, std::string s) {
|
|
||||||
avatar = s;
|
|
||||||
sprite.Load(renderer, ConfigUtility::GetSingleton()["dir.sprites"] + avatar, 4, 4);
|
|
||||||
return avatar;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BaseCreature::GetAvatar() const {
|
|
||||||
return avatar;
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "entity.hpp"
|
|
||||||
|
|
||||||
void Entity::Update() {
|
|
||||||
origin += motion;
|
|
||||||
sprite.Update(0.016);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Entity::DrawTo(SDL_Renderer* const renderer, int camX, int camY) {
|
|
||||||
sprite.DrawTo(renderer, origin.x - camX, origin.y - camY);
|
|
||||||
}
|
|
||||||
|
|
||||||
SpriteSheet* Entity::GetSprite() {
|
|
||||||
return &sprite;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//accessors & mutators
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
Vector2 Entity::SetOrigin(Vector2 v) {
|
|
||||||
return origin = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector2 Entity::SetMotion(Vector2 v) {
|
|
||||||
return motion = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
BoundingBox Entity::SetBounds(BoundingBox b) {
|
|
||||||
return bounds = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector2 Entity::GetOrigin() const {
|
|
||||||
return origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector2 Entity::GetMotion() const {
|
|
||||||
return motion;
|
|
||||||
}
|
|
||||||
|
|
||||||
BoundingBox Entity::GetBounds() const {
|
|
||||||
return bounds;
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. .. ../../common/global_defines ../../common/utilities ../../TurtleGUI
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#output
|
|
||||||
OUTDIR=..
|
|
||||||
OUT=$(addprefix $(OUTDIR)/,client.a)
|
|
||||||
|
|
||||||
#targets
|
|
||||||
all: $(OBJ) $(OUT)
|
|
||||||
ar -crs $(OUT) $(OBJ)
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUT): | $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR):
|
|
||||||
mkdir $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUTDIR):
|
|
||||||
mkdir $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "fatal_error.hpp"
|
|
||||||
|
|
||||||
//DOCS: This empty file is used to force an object file
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class fatal_error: public std::runtime_error {
|
|
||||||
public:
|
|
||||||
explicit fatal_error(const std::string& str): runtime_error(str) {}
|
|
||||||
explicit fatal_error(const char* cstr): runtime_error(cstr) {}
|
|
||||||
};
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "heartbeat_utility.hpp"
|
|
||||||
|
|
||||||
#include "channels.hpp"
|
|
||||||
#include "ip_operators.hpp"
|
|
||||||
|
|
||||||
//heartbeat system
|
|
||||||
void HeartbeatUtility::hPing(ServerPacket* const argPacket) {
|
|
||||||
ServerPacket newPacket;
|
|
||||||
newPacket.type = SerialPacketType::PONG;
|
|
||||||
network.SendTo(argPacket->srcAddress, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void HeartbeatUtility::hPong(ServerPacket* const argPacket) {
|
|
||||||
if (*network.GetIPAddress(Channels::SERVER) != argPacket->srcAddress) {
|
|
||||||
throw(std::runtime_error("Heartbeat message received from an unknown source"));
|
|
||||||
}
|
|
||||||
attemptedBeats = 0;
|
|
||||||
lastBeat = Clock::now();
|
|
||||||
}
|
|
||||||
|
|
||||||
int HeartbeatUtility::CheckHeartBeat() {
|
|
||||||
//check the connection (heartbeat)
|
|
||||||
if (Clock::now() - lastBeat > std::chrono::seconds(3)) {
|
|
||||||
if (attemptedBeats > 2) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
ServerPacket newPacket;
|
|
||||||
newPacket.type = SerialPacketType::PING;
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
|
|
||||||
attemptedBeats++;
|
|
||||||
lastBeat = Clock::now();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,43 +19,72 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#include "splash_screen.hpp"
|
#include "in_combat.hpp"
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
SplashScreen::SplashScreen(SDL_Window* w) {
|
InCombat::InCombat() {
|
||||||
//fit the screen to the logo
|
#ifdef DEBUG
|
||||||
//NOTE: not using this window trick
|
cout << "entering InCombat" << endl;
|
||||||
window = w;
|
#endif
|
||||||
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
|
||||||
|
|
||||||
logo.Load(GetRenderer(), ConfigUtility::GetSingleton()["dir.logos"] + "krstudios.png");
|
|
||||||
|
|
||||||
startTick = std::chrono::steady_clock::now();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SplashScreen::~SplashScreen() {
|
InCombat::~InCombat() {
|
||||||
//
|
#ifdef DEBUG
|
||||||
|
cout << "leaving InCombat" << endl;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Frame loop
|
//Frame loop
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void SplashScreen::FrameStart() {
|
void InCombat::FrameStart() {
|
||||||
//TODO: (0) config flag to change the delay
|
//
|
||||||
if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(3)) {
|
}
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
|
void InCombat::Update(double delta) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InCombat::FrameEnd() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InCombat::Render(SDL_Surface* const screen) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Event handlers
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InCombat::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InCombat::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InCombat::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
QuitEvent();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SplashScreen::RenderFrame(SDL_Renderer* renderer) {
|
void InCombat::KeyUp(SDL_KeyboardEvent const& key) {
|
||||||
//TODO: version information
|
//
|
||||||
int w = 0, h = 0;
|
|
||||||
SDL_RenderGetLogicalSize(renderer, &w, &h);
|
|
||||||
logo.DrawTo(renderer, (w - logo.GetClipW()) / 2, (h - logo.GetClipH()) / 2);
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,24 +19,30 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef INCOMBAT_HPP_
|
||||||
|
#define INCOMBAT_HPP_
|
||||||
|
|
||||||
#include "entity.hpp"
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
class BaseCreature: public Entity {
|
class InCombat : public BaseScene {
|
||||||
public:
|
public:
|
||||||
BaseCreature() = default;
|
/* Public access members */
|
||||||
virtual ~BaseCreature() = default;
|
InCombat();
|
||||||
|
~InCombat();
|
||||||
void CorrectSprite();
|
|
||||||
|
|
||||||
std::string SetHandle(std::string s);
|
|
||||||
std::string GetHandle() const;
|
|
||||||
std::string SetAvatar(SDL_Renderer* const, std::string s);
|
|
||||||
std::string GetAvatar() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//metadata
|
/* Frame loop */
|
||||||
std::string handle;
|
void FrameStart();
|
||||||
std::string avatar;
|
void Update(double delta);
|
||||||
|
void FrameEnd();
|
||||||
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
|
/* Event handlers */
|
||||||
|
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
|
||||||
@@ -0,0 +1,326 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "in_world.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Quick and dirty
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
static std::string itos(int i) {
|
||||||
|
char buffer[20];
|
||||||
|
snprintf(buffer, 20, "%d", i);
|
||||||
|
return std::string(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Public access members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
InWorld::InWorld() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "entering InWorld" << endl;
|
||||||
|
#endif
|
||||||
|
cout << "Client Index: " << infoMgr->GetClientIndex() << endl;
|
||||||
|
font.SetSurface(surfaceMgr->Get("font"));
|
||||||
|
|
||||||
|
//debugging
|
||||||
|
Packet p;
|
||||||
|
p.meta.type = Packet::Type::PLAYER_NEW;
|
||||||
|
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||||
|
|
||||||
|
snprintf(p.playerInfo.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
|
||||||
|
snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
|
||||||
|
p.playerInfo.position.x = 50;
|
||||||
|
p.playerInfo.position.y = 50;
|
||||||
|
p.playerInfo.motion.x = 0;
|
||||||
|
p.playerInfo.motion.y = 0;
|
||||||
|
|
||||||
|
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||||
|
|
||||||
|
//request a sync
|
||||||
|
p.meta.type = Packet::Type::SYNCHRONIZE;
|
||||||
|
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||||
|
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||||
|
}
|
||||||
|
|
||||||
|
InWorld::~InWorld() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "leaving InWorld" << endl;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Frame loop
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void InWorld::FrameStart() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::Update(double delta) {
|
||||||
|
while(HandlePacket(popNetworkPacket()));
|
||||||
|
|
||||||
|
for (auto& it : playerCharacters) {
|
||||||
|
it.second.Update(delta);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::FrameEnd() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::Render(SDL_Surface* const screen) {
|
||||||
|
clockFrameRate();
|
||||||
|
|
||||||
|
for (auto& it : playerCharacters) {
|
||||||
|
it.second.DrawTo(screen);
|
||||||
|
}
|
||||||
|
|
||||||
|
//since we're using this twice, make a tmp var
|
||||||
|
string fps = itos(getFrameRate());
|
||||||
|
font.DrawStringTo(fps, screen, screen->w - fps.size() * font.GetCharW(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Event handlers
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void InWorld::QuitEvent() {
|
||||||
|
//ensure that the client is disconnected properly
|
||||||
|
ExitGame();
|
||||||
|
SetNextScene(SceneList::QUIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
|
//general
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
ExitGame();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//player movement
|
||||||
|
if (infoMgr->GetPlayerIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_w:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::NORTH);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
case SDLK_s:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::SOUTH);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
case SDLK_a:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::WEST);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
case SDLK_d:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::EAST);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||||
|
//player movement reversed
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_w:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::SOUTH);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
case SDLK_s:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::NORTH);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
case SDLK_a:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::EAST);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
case SDLK_d:
|
||||||
|
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::WEST);
|
||||||
|
SendState();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Utilities
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
int InWorld::HandlePacket(Packet p) {
|
||||||
|
switch(p.meta.type) {
|
||||||
|
case Packet::Type::NONE:
|
||||||
|
//DO NOTHING
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
case Packet::Type::PING:
|
||||||
|
//quick pong
|
||||||
|
p.meta.type = Packet::Type::PONG;
|
||||||
|
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||||
|
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||||
|
break;
|
||||||
|
case Packet::Type::PONG:
|
||||||
|
//
|
||||||
|
break;
|
||||||
|
// case Packet::Type::BROADCAST_REQUEST:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
// case Packet::Type::BROADCAST_RESPONSE:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
// case Packet::Type::JOIN_REQUEST:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
// case Packet::Type::JOIN_RESPONSE:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
case Packet::Type::DISCONNECT:
|
||||||
|
HandleDisconnection(p);
|
||||||
|
break;
|
||||||
|
// case Packet::Type::SYNCHRONIZE:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
case Packet::Type::PLAYER_NEW:
|
||||||
|
AddPlayer(p);
|
||||||
|
break;
|
||||||
|
case Packet::Type::PLAYER_DELETE:
|
||||||
|
RemovePlayer(p);
|
||||||
|
break;
|
||||||
|
case Packet::Type::PLAYER_UPDATE:
|
||||||
|
UpdatePlayer(p);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type))));
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::Disconnect() {
|
||||||
|
Packet p;
|
||||||
|
|
||||||
|
//delete the player
|
||||||
|
p.meta.type = Packet::Type::PLAYER_DELETE;
|
||||||
|
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||||
|
p.playerInfo.index = infoMgr->GetPlayerIndex();
|
||||||
|
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||||
|
|
||||||
|
//disconnect
|
||||||
|
p.meta.type = Packet::Type::DISCONNECT;
|
||||||
|
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||||
|
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||||
|
|
||||||
|
netUtil->Unbind(GAME_CHANNEL);
|
||||||
|
|
||||||
|
//reset the client
|
||||||
|
infoMgr->ResetClientIndex();
|
||||||
|
infoMgr->ResetPlayerIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::ExitGame() {
|
||||||
|
Disconnect();
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
endQueueThread();
|
||||||
|
cout << "The game session has ended" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::HandleDisconnection(Packet& disconnect) {
|
||||||
|
Disconnect();
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
endQueueThread();
|
||||||
|
cout << "You have been disconnected" << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::AddPlayer(Packet& p) {
|
||||||
|
if (playerCharacters.find(p.playerInfo.index) != playerCharacters.end()) {
|
||||||
|
throw(runtime_error("Duplicate players detected"));
|
||||||
|
}
|
||||||
|
|
||||||
|
//position
|
||||||
|
playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position);
|
||||||
|
playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion);
|
||||||
|
|
||||||
|
//sprite
|
||||||
|
playerCharacters[p.playerInfo.index].GetSprite()->SetSurface(surfaceMgr->Get(p.playerInfo.avatar), 32, 48);
|
||||||
|
playerCharacters[p.playerInfo.index].FaceDirection();
|
||||||
|
|
||||||
|
//is it this player?
|
||||||
|
if (p.meta.clientIndex == infoMgr->GetClientIndex()) {
|
||||||
|
infoMgr->SetPlayerIndex(p.playerInfo.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
//debugging
|
||||||
|
cout << "New player, index " << p.playerInfo.index << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::RemovePlayer(Packet& p) {
|
||||||
|
if (playerCharacters.find(p.playerInfo.index) == playerCharacters.end()) {
|
||||||
|
throw(runtime_error("Player to delete not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
playerCharacters.erase(p.playerInfo.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::UpdatePlayer(Packet& p) {
|
||||||
|
if (playerCharacters.find(p.playerInfo.index) == playerCharacters.end()) {
|
||||||
|
AddPlayer(p);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position);
|
||||||
|
playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion);
|
||||||
|
playerCharacters[p.playerInfo.index].FaceDirection();
|
||||||
|
}
|
||||||
|
|
||||||
|
void InWorld::SendState() {
|
||||||
|
//send the state of this player's character
|
||||||
|
if (infoMgr->GetPlayerIndex() == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Packet p;
|
||||||
|
p.meta.type = Packet::Type::PLAYER_UPDATE;
|
||||||
|
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||||
|
p.playerInfo.index = infoMgr->GetPlayerIndex();
|
||||||
|
p.playerInfo.position = playerCharacters[infoMgr->GetPlayerIndex()].GetPosition();
|
||||||
|
p.playerInfo.motion = playerCharacters[infoMgr->GetPlayerIndex()].GetMotion();
|
||||||
|
|
||||||
|
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
/* 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 INWORLD_HPP_
|
||||||
|
#define INWORLD_HPP_
|
||||||
|
|
||||||
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
|
#include "defines.hpp"
|
||||||
|
#include "singleton.hpp"
|
||||||
|
#include "packet.hpp"
|
||||||
|
#include "network_queue.hpp"
|
||||||
|
#include "information_manager.hpp"
|
||||||
|
#include "player_character.hpp"
|
||||||
|
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
#include "surface_manager.hpp"
|
||||||
|
#include "udp_network_utility.hpp"
|
||||||
|
#include "button.hpp"
|
||||||
|
#include "raster_font.hpp"
|
||||||
|
#include "frame_rate.hpp"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class InWorld : public BaseScene {
|
||||||
|
public:
|
||||||
|
//Public access members
|
||||||
|
InWorld();
|
||||||
|
~InWorld();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//Frame loop
|
||||||
|
void FrameStart();
|
||||||
|
void Update(double delta);
|
||||||
|
void FrameEnd();
|
||||||
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
|
//Event handlers
|
||||||
|
void QuitEvent();
|
||||||
|
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
|
||||||
|
int HandlePacket(Packet);
|
||||||
|
void Disconnect();
|
||||||
|
void ExitGame();
|
||||||
|
|
||||||
|
void HandleDisconnection(Packet&);
|
||||||
|
|
||||||
|
void AddPlayer(Packet&);
|
||||||
|
void RemovePlayer(Packet&);
|
||||||
|
void UpdatePlayer(Packet&);
|
||||||
|
|
||||||
|
void SendState();
|
||||||
|
|
||||||
|
//services
|
||||||
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
|
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||||
|
|
||||||
|
//members
|
||||||
|
RasterFont font;
|
||||||
|
std::map<int, PlayerCharacter> playerCharacters;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,21 +19,22 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef INFORMATIONMANAGER_HPP_
|
||||||
|
#define INFORMATIONMANAGER_HPP_
|
||||||
|
|
||||||
#include "base_character.hpp"
|
class InformationManager {
|
||||||
#include "bounding_box.hpp"
|
|
||||||
#include "vector2.hpp"
|
|
||||||
|
|
||||||
#include <list>
|
|
||||||
|
|
||||||
class LocalCharacter: public BaseCharacter {
|
|
||||||
public:
|
public:
|
||||||
LocalCharacter() = default;
|
int SetClientIndex(int i) { return clientIndex = i; }
|
||||||
virtual ~LocalCharacter() = default;
|
int GetClientIndex() { return clientIndex; }
|
||||||
|
void ResetClientIndex() { clientIndex = -1; }
|
||||||
bool ProcessCollisionGrid(std::list<BoundingBox>);
|
|
||||||
|
|
||||||
|
//one player at a time
|
||||||
|
int SetPlayerIndex(int i) { return playerIndex = i; }
|
||||||
|
int GetPlayerIndex() { return playerIndex; }
|
||||||
|
void ResetPlayerIndex() { playerIndex = -1; }
|
||||||
private:
|
private:
|
||||||
//NOTE: NO MEMBERS
|
int clientIndex = -1;
|
||||||
|
int playerIndex = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "lobby.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Quick and dirty
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
static std::string itos(int i) {
|
||||||
|
char buffer[20];
|
||||||
|
snprintf(buffer, 20, "%d", i);
|
||||||
|
return std::string(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Public access members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
Lobby::Lobby() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "entering Lobby" << endl;
|
||||||
|
#endif
|
||||||
|
refreshButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Refresh");
|
||||||
|
joinButton.Setup(50, 50 + surfaceMgr->Get("button")->h/3, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Join");
|
||||||
|
backButton.Setup(50, 50 + surfaceMgr->Get("button")->h/3 * 2, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back");
|
||||||
|
|
||||||
|
font.SetSurface(surfaceMgr->Get("font"));
|
||||||
|
listBox.x = 280;
|
||||||
|
listBox.y = 50;
|
||||||
|
listBox.w = GetScreen()->w - listBox.x - 50;
|
||||||
|
listBox.h = font.GetCharH();
|
||||||
|
|
||||||
|
serverList.push_back({"foo",{0,0}});
|
||||||
|
serverList.push_back({"bar",{0,0}});
|
||||||
|
serverList.push_back({"foobar",{0,0}});
|
||||||
|
|
||||||
|
flushNetworkQueue();
|
||||||
|
beginQueueThread();
|
||||||
|
BroadcastNetwork();
|
||||||
|
}
|
||||||
|
|
||||||
|
Lobby::~Lobby() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "leaving Lobby" << endl;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Frame loop
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void Lobby::FrameStart() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::Update(double delta) {
|
||||||
|
while(HandlePacket(popNetworkPacket()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::FrameEnd() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::Render(SDL_Surface* const screen) {
|
||||||
|
refreshButton.DrawTo(screen);
|
||||||
|
joinButton.DrawTo(screen);
|
||||||
|
backButton.DrawTo(screen);
|
||||||
|
|
||||||
|
for (int i = 0; i < serverList.size(); i++) {
|
||||||
|
if (selectedServer == &serverList[i]) {
|
||||||
|
//draw the highlight box
|
||||||
|
SDL_Rect r = listBox;
|
||||||
|
r.y += i * font.GetCharH();
|
||||||
|
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 127, 39));
|
||||||
|
}
|
||||||
|
font.DrawStringTo(serverList[i].name, screen, listBox.x, listBox.y + i * font.GetCharH());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Event handlers
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
|
refreshButton.MouseMotion(motion);
|
||||||
|
joinButton.MouseMotion(motion);
|
||||||
|
backButton.MouseMotion(motion);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||||
|
refreshButton.MouseButtonDown(button);
|
||||||
|
joinButton.MouseButtonDown(button);
|
||||||
|
backButton.MouseButtonDown(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
|
if (refreshButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
|
BroadcastNetwork();
|
||||||
|
selectedServer = nullptr;
|
||||||
|
}
|
||||||
|
else if (joinButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
|
if (selectedServer) {
|
||||||
|
ConnectToServer(selectedServer);
|
||||||
|
selectedServer = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
endQueueThread();
|
||||||
|
selectedServer = nullptr;
|
||||||
|
}
|
||||||
|
else if (
|
||||||
|
//clicked within bounds TODO: make the damn collision system
|
||||||
|
button.x > listBox.x &&
|
||||||
|
button.y > listBox.y &&
|
||||||
|
button.x < listBox.x + listBox.w &&
|
||||||
|
button.y < listBox.y + (listBox.h * serverList.size())
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//selecting a server
|
||||||
|
selectedServer = &serverList[(button.y - listBox.y) / listBox.h];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//lose focus on a server
|
||||||
|
selectedServer = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Utilities
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
int Lobby::HandlePacket(Packet p) {
|
||||||
|
switch(p.meta.type) {
|
||||||
|
case Packet::Type::NONE:
|
||||||
|
//DO NOTHING
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
|
case Packet::Type::PING:
|
||||||
|
//quick pong
|
||||||
|
p.meta.type = Packet::Type::PONG;
|
||||||
|
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||||
|
break;
|
||||||
|
case Packet::Type::PONG:
|
||||||
|
//
|
||||||
|
break;
|
||||||
|
// case Packet::Type::BROADCAST_REQUEST:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
case Packet::Type::BROADCAST_RESPONSE:
|
||||||
|
PushServer(p);
|
||||||
|
break;
|
||||||
|
// case Packet::Type::JOIN_REQUEST:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
case Packet::Type::JOIN_RESPONSE:
|
||||||
|
BeginGame(p);
|
||||||
|
break;
|
||||||
|
// case Packet::Type::DISCONNECT:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
// case Packet::Type::SYNCHRONIZE:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
// case Packet::Type::PLAYER_NEW:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
// case Packet::Type::PLAYER_DELETE:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
// case Packet::Type::PLAYER_UPDATE:
|
||||||
|
// //
|
||||||
|
// break;
|
||||||
|
default:
|
||||||
|
throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type))));
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::BroadcastNetwork() {
|
||||||
|
Packet p;
|
||||||
|
p.meta.type = Packet::Type::BROADCAST_REQUEST;
|
||||||
|
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet));
|
||||||
|
serverList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::PushServer(Packet& bcast) {
|
||||||
|
ServerEntry entry;
|
||||||
|
entry.name = bcast.serverInfo.name;
|
||||||
|
entry.address = bcast.meta.address;
|
||||||
|
serverList.push_back(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::ConnectToServer(ServerEntry* server) {
|
||||||
|
//_attempt_ to connect to a server
|
||||||
|
if (!server) {
|
||||||
|
throw(runtime_error("No server received"));
|
||||||
|
}
|
||||||
|
Packet p;
|
||||||
|
p.meta.type = Packet::Type::JOIN_REQUEST;
|
||||||
|
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Lobby::BeginGame(Packet& response) {
|
||||||
|
//should be downloading the resources here as well
|
||||||
|
infoMgr->SetClientIndex(response.meta.clientIndex);
|
||||||
|
netUtil->Bind(&response.meta.address, GAME_CHANNEL);
|
||||||
|
SetNextScene(SceneList::INWORLD);
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
/* 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 LOBBY_HPP_
|
||||||
|
#define LOBBY_HPP_
|
||||||
|
|
||||||
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
|
#include "defines.hpp"
|
||||||
|
#include "singleton.hpp"
|
||||||
|
|
||||||
|
#include "packet.hpp"
|
||||||
|
#include "network_queue.hpp"
|
||||||
|
#include "information_manager.hpp"
|
||||||
|
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
#include "surface_manager.hpp"
|
||||||
|
#include "udp_network_utility.hpp"
|
||||||
|
#include "button.hpp"
|
||||||
|
#include "raster_font.hpp"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct ServerEntry {
|
||||||
|
std::string name;
|
||||||
|
IPaddress address;
|
||||||
|
//TODO: player count
|
||||||
|
};
|
||||||
|
|
||||||
|
class Lobby : public BaseScene {
|
||||||
|
public:
|
||||||
|
//Public access members
|
||||||
|
Lobby();
|
||||||
|
~Lobby();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//Frame loop
|
||||||
|
void FrameStart();
|
||||||
|
void Update(double delta);
|
||||||
|
void FrameEnd();
|
||||||
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
|
//Event handlers
|
||||||
|
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
|
||||||
|
int HandlePacket(Packet);
|
||||||
|
void BroadcastNetwork();
|
||||||
|
void PushServer(Packet&);
|
||||||
|
void ConnectToServer(ServerEntry*);
|
||||||
|
void BeginGame(Packet&);
|
||||||
|
|
||||||
|
//services
|
||||||
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
|
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||||
|
|
||||||
|
//members
|
||||||
|
Button refreshButton;
|
||||||
|
Button joinButton;
|
||||||
|
Button backButton;
|
||||||
|
|
||||||
|
RasterFont font;
|
||||||
|
SDL_Rect listBox;
|
||||||
|
std::vector<ServerEntry> serverList;
|
||||||
|
ServerEntry* selectedServer = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+12
-24
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -21,39 +21,27 @@
|
|||||||
*/
|
*/
|
||||||
#include "client_application.hpp"
|
#include "client_application.hpp"
|
||||||
|
|
||||||
//singletons
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int, char**) {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "Beginning program" << endl;
|
||||||
|
#endif
|
||||||
try {
|
try {
|
||||||
//create the singletons
|
ClientApplication app;
|
||||||
ConfigUtility::CreateSingleton();
|
app.Init();
|
||||||
UDPNetworkUtility::CreateSingleton();
|
|
||||||
|
|
||||||
//call the client's routines
|
|
||||||
ClientApplication::CreateSingleton();
|
|
||||||
ClientApplication& app = ClientApplication::GetSingleton();
|
|
||||||
|
|
||||||
app.Init(argc, argv);
|
|
||||||
app.Proc();
|
app.Proc();
|
||||||
app.Quit();
|
app.Quit();
|
||||||
|
|
||||||
//control the position of the app's destructor
|
|
||||||
ClientApplication::DeleteSingleton();
|
|
||||||
|
|
||||||
//delete the singletons
|
|
||||||
ConfigUtility::DeleteSingleton();
|
|
||||||
UDPNetworkUtility::DeleteSingleton();
|
|
||||||
}
|
}
|
||||||
catch(exception& e) {
|
catch(exception& e) {
|
||||||
cerr << "Fatal exception thrown: " << e.what() << endl;
|
cerr << "Fatal error: " << e.what() << endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "Clean exit" << endl;
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,103 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "main_menu.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Public access members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
MainMenu::MainMenu() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "entering MainMenu" << endl;
|
||||||
|
#endif
|
||||||
|
startButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start");
|
||||||
|
optionsButton.Setup(50, 50 + surfaceMgr->Get("button")->h/3, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options");
|
||||||
|
quitButton.Setup(50, 50 + surfaceMgr->Get("button")->h/3 * 2, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit");
|
||||||
|
}
|
||||||
|
|
||||||
|
MainMenu::~MainMenu() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "leaving MainMenu" << endl;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Frame loop
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void MainMenu::FrameStart() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenu::Update(double delta) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenu::FrameEnd() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenu::Render(SDL_Surface* const screen) {
|
||||||
|
startButton.DrawTo(screen);
|
||||||
|
optionsButton.DrawTo(screen);
|
||||||
|
quitButton.DrawTo(screen);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Event handlers
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void MainMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
|
startButton.MouseMotion(motion);
|
||||||
|
optionsButton.MouseMotion(motion);
|
||||||
|
quitButton.MouseMotion(motion);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||||
|
startButton.MouseButtonDown(button);
|
||||||
|
optionsButton.MouseButtonDown(button);
|
||||||
|
quitButton.MouseButtonDown(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
|
if (startButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
|
SetNextScene(SceneList::LOBBY);
|
||||||
|
}
|
||||||
|
if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
|
SetNextScene(SceneList::OPTIONSCREEN);
|
||||||
|
}
|
||||||
|
if (quitButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
|
QuitEvent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
QuitEvent();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,42 +19,39 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef MAINMENU_HPP_
|
||||||
|
#define MAINMENU_HPP_
|
||||||
|
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
#include "button.hpp"
|
#include "singleton.hpp"
|
||||||
#include "image.hpp"
|
|
||||||
#include "text_box.hpp"
|
|
||||||
|
|
||||||
#include "SDL2/SDL_ttf.h"
|
#include "surface_manager.hpp"
|
||||||
|
#include "button.hpp"
|
||||||
|
|
||||||
class MainMenu : public BaseScene {
|
class MainMenu : public BaseScene {
|
||||||
public:
|
public:
|
||||||
//Public access members
|
/* Public access members */
|
||||||
MainMenu();
|
MainMenu();
|
||||||
~MainMenu();
|
~MainMenu();
|
||||||
|
|
||||||
void RenderFrame(SDL_Renderer* renderer) override;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//frame phases
|
/* Frame loop */
|
||||||
void FrameStart() override;
|
void FrameStart();
|
||||||
void Update() override;
|
void Update(double delta);
|
||||||
void FrameEnd() override;
|
void FrameEnd();
|
||||||
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
//input events
|
/* Event handlers */
|
||||||
void MouseMotion(SDL_MouseMotionEvent const& event) override;
|
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const& event) override;
|
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const& event) override;
|
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
void MouseWheel(SDL_MouseWheelEvent const& event) override;
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
void KeyDown(SDL_KeyboardEvent const& event) override;
|
|
||||||
void KeyUp(SDL_KeyboardEvent const& event) override;
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
|
|
||||||
//members
|
|
||||||
Image buttonImage;
|
|
||||||
TTF_Font* font = nullptr;
|
|
||||||
Button startButton;
|
Button startButton;
|
||||||
Button optionsButton;
|
Button optionsButton;
|
||||||
Button quitButton;
|
Button quitButton;
|
||||||
TextBox textBox;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+8
-28
@@ -1,27 +1,15 @@
|
|||||||
#include directories
|
#config
|
||||||
INCLUDES+=. entities scenes ../common/debugging ../common/global_defines ../common/network ../common/network/packet_types ../common/utilities ../TurtleGUI ../TurtleMap
|
LOCALLIBS=../lib/libCommon.a
|
||||||
|
LIB=$(LOCALLIBS) -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL
|
||||||
#libraries
|
INCLUDES=../common
|
||||||
#the order of the $(LIBS) is important, at least for MinGW
|
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||||
LIBS+=client.a ../libcommon.a ../libturtlegui.a ../libturtlemap.a -lSDL2_net
|
|
||||||
ifeq ($(OS),Windows_NT)
|
|
||||||
LIBS+=-lwsock32 -liphlpapi -lmingw32
|
|
||||||
endif
|
|
||||||
LIBS+=-lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf
|
|
||||||
|
|
||||||
#flags
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
ifeq ($(shell uname), Linux)
|
|
||||||
#read data about the current install
|
|
||||||
CXXFLAGS+=$(shell sdl-config --cflags --static-libs)
|
|
||||||
endif
|
|
||||||
|
|
||||||
#source
|
#source
|
||||||
CXXSRC=$(wildcard *.cpp)
|
SRC=$(wildcard *.cpp)
|
||||||
|
|
||||||
#objects
|
#objects
|
||||||
OBJDIR=obj
|
OBJDIR=obj
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||||
|
|
||||||
#output
|
#output
|
||||||
OUTDIR=../out
|
OUTDIR=../out
|
||||||
@@ -29,9 +17,7 @@ OUT=$(addprefix $(OUTDIR)/,client)
|
|||||||
|
|
||||||
#targets
|
#targets
|
||||||
all: $(OBJ) $(OUT)
|
all: $(OBJ) $(OUT)
|
||||||
$(MAKE) -C entities
|
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIB)
|
||||||
$(MAKE) -C scenes
|
|
||||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
$(OBJ): | $(OBJDIR)
|
||||||
|
|
||||||
@@ -47,12 +33,6 @@ $(OBJDIR)/%.o: %.cpp
|
|||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
|
||||||
$(RM) *.o *.a *.exe
|
$(RM) *.o *.a *.exe
|
||||||
else ifeq ($(shell uname), Linux)
|
|
||||||
find . -type f -name *.o -exec rm -f -r -v {} \;
|
|
||||||
find . -type f -name *.a -exec rm -f -r -v {} \;
|
|
||||||
rm -f -v out/client out/server
|
|
||||||
endif
|
|
||||||
|
|
||||||
rebuild: clean all
|
rebuild: clean all
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "option_screen.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Public access members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
OptionScreen::OptionScreen() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "entering OptionScreen" << endl;
|
||||||
|
#endif
|
||||||
|
backButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back");
|
||||||
|
}
|
||||||
|
|
||||||
|
OptionScreen::~OptionScreen() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "leaving OptionScreen" << endl;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Frame loop
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void OptionScreen::Render(SDL_Surface* const screen) {
|
||||||
|
backButton.DrawTo(screen);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Event handlers
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void OptionScreen::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
|
backButton.MouseMotion(motion);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OptionScreen::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||||
|
backButton.MouseButtonDown(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OptionScreen::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
|
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OptionScreen::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,34 +19,33 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef OPTIONSCREEN_HPP_
|
||||||
|
#define OPTIONSCREEN_HPP_
|
||||||
|
|
||||||
#include "entity.hpp"
|
#include "base_scene.hpp"
|
||||||
|
#include "singleton.hpp"
|
||||||
|
|
||||||
#include "composite_image.hpp"
|
#include "surface_manager.hpp"
|
||||||
|
#include "button.hpp"
|
||||||
|
|
||||||
class BaseBarrier: public Entity {
|
class OptionScreen : public BaseScene {
|
||||||
public:
|
public:
|
||||||
BaseBarrier() = default;
|
/* Public access members */
|
||||||
BaseBarrier(Image& baseImage, std::map<std::string, Image>& templateImages);
|
OptionScreen();
|
||||||
virtual ~BaseBarrier();
|
~OptionScreen();
|
||||||
|
|
||||||
void CorrectSprite();
|
|
||||||
void Update();
|
|
||||||
void DrawTo(SDL_Renderer* const, int camX, int camY);
|
|
||||||
|
|
||||||
int SetStatus(int, int);
|
|
||||||
int FindStatus(int);
|
|
||||||
|
|
||||||
int* SetStatusArray(int*);
|
|
||||||
int* GetStatusArray();
|
|
||||||
|
|
||||||
CompositeImage<>* GetComposite();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//metadata
|
/* Frame loop */
|
||||||
int status[8];
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
Image baseImage;
|
/* Event handlers */
|
||||||
CompositeImage<> composite;
|
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
|
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
|
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
|
Button backButton;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "player_character.hpp"
|
||||||
|
|
||||||
|
void PlayerCharacter::Update(double delta) {
|
||||||
|
if (limitSpeed) {
|
||||||
|
constexpr double d = 1.0/sqrt(2);
|
||||||
|
position += motion * delta * d;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
position += motion * delta;
|
||||||
|
}
|
||||||
|
sprite.Update(delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerCharacter::MoveDirection(CardinalDirection cd) {
|
||||||
|
//shift the movement in this direction
|
||||||
|
switch(cd) {
|
||||||
|
case CardinalDirection::NORTH:
|
||||||
|
if (motion.y >= 0) {
|
||||||
|
motion.y -= WALKING_SPEED;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CardinalDirection::SOUTH:
|
||||||
|
if (motion.y <= 0) {
|
||||||
|
motion.y += WALKING_SPEED;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CardinalDirection::WEST:
|
||||||
|
if (motion.x >= 0) {
|
||||||
|
motion.x -= WALKING_SPEED;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case CardinalDirection::EAST:
|
||||||
|
if (motion.x <= 0) {
|
||||||
|
motion.x += WALKING_SPEED;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//face the correct direction
|
||||||
|
FaceDirection();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerCharacter::FaceDirection(CardinalDirection cd) {
|
||||||
|
switch(cd) {
|
||||||
|
case CardinalDirection::NORTH:
|
||||||
|
sprite.SetCurrentStrip(1);
|
||||||
|
break;
|
||||||
|
case CardinalDirection::SOUTH:
|
||||||
|
sprite.SetCurrentStrip(0);
|
||||||
|
break;
|
||||||
|
case CardinalDirection::WEST:
|
||||||
|
sprite.SetCurrentStrip(2);
|
||||||
|
break;
|
||||||
|
case CardinalDirection::EAST:
|
||||||
|
sprite.SetCurrentStrip(3);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerCharacter::FaceDirection() {
|
||||||
|
//base the direction on the character's movement
|
||||||
|
if (motion.y < 0) {
|
||||||
|
FaceDirection(CardinalDirection::NORTH);
|
||||||
|
}
|
||||||
|
if (motion.y > 0) {
|
||||||
|
FaceDirection(CardinalDirection::SOUTH);
|
||||||
|
}
|
||||||
|
if (motion.x < 0) {
|
||||||
|
FaceDirection(CardinalDirection::WEST);
|
||||||
|
}
|
||||||
|
if (motion.x > 0) {
|
||||||
|
FaceDirection(CardinalDirection::EAST);
|
||||||
|
}
|
||||||
|
CheckSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerCharacter::CheckSpeed() {
|
||||||
|
//diagonal
|
||||||
|
if (motion.x != 0 && motion.y != 0) {
|
||||||
|
sprite.SetDelay(0.1);
|
||||||
|
limitSpeed = true;
|
||||||
|
}
|
||||||
|
//cardinal
|
||||||
|
else if (motion != 0) {
|
||||||
|
sprite.SetDelay(0.1);
|
||||||
|
limitSpeed = false;
|
||||||
|
}
|
||||||
|
//not moving
|
||||||
|
else {
|
||||||
|
sprite.SetDelay(0);
|
||||||
|
sprite.SetCurrentFrame(0);
|
||||||
|
limitSpeed = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,36 +19,45 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef PLAYERCHARACTER_HPP_
|
||||||
|
#define PLAYERCHARACTER_HPP_
|
||||||
|
|
||||||
#include "bounding_box.hpp"
|
|
||||||
#include "sprite_sheet.hpp"
|
|
||||||
#include "vector2.hpp"
|
#include "vector2.hpp"
|
||||||
|
#include "sprite_sheet.hpp"
|
||||||
|
#include "defines.hpp"
|
||||||
|
|
||||||
//The base class for all objects in the world
|
class PlayerCharacter {
|
||||||
//TODO: (9) write a better hierarchy
|
|
||||||
class Entity {
|
|
||||||
public:
|
public:
|
||||||
virtual void Update();
|
PlayerCharacter() = default;
|
||||||
virtual void DrawTo(SDL_Renderer* const, int camX, int camY);
|
~PlayerCharacter() = default;
|
||||||
|
|
||||||
SpriteSheet* GetSprite();
|
void Update(double delta);
|
||||||
|
|
||||||
//accessors & mutators
|
void MoveDirection(CardinalDirection);
|
||||||
Vector2 SetOrigin(Vector2 v);
|
void FaceDirection(CardinalDirection);
|
||||||
Vector2 SetMotion(Vector2 v);
|
void FaceDirection();
|
||||||
BoundingBox SetBounds(BoundingBox b);
|
|
||||||
|
|
||||||
Vector2 GetOrigin() const;
|
void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); }
|
||||||
Vector2 GetMotion() const;
|
|
||||||
BoundingBox GetBounds() const;
|
|
||||||
|
|
||||||
protected:
|
//accessors and mutators
|
||||||
Entity() = default;
|
Vector2 SetPosition(Vector2 v) { return position = v; }
|
||||||
virtual ~Entity() = default;
|
Vector2 ShiftPosition(Vector2 v) { return position += v; }
|
||||||
|
Vector2 GetPosition() { return position; }
|
||||||
|
|
||||||
SpriteSheet sprite;
|
Vector2 SetMotion(Vector2 v) { return motion = v; }
|
||||||
Vector2 origin;
|
Vector2 ShiftMotion(Vector2 v) { return motion += v; }
|
||||||
|
Vector2 GetMotion() { return motion; }
|
||||||
|
|
||||||
|
SpriteSheet* GetSprite() { return &sprite; }
|
||||||
|
private:
|
||||||
|
void CheckSpeed();
|
||||||
|
|
||||||
|
Vector2 position;
|
||||||
Vector2 motion;
|
Vector2 motion;
|
||||||
BoundingBox bounds;
|
SpriteSheet sprite;
|
||||||
|
|
||||||
|
//for moving diagonal
|
||||||
|
bool limitSpeed = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,20 +19,22 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef SCENELIST_HPP_
|
||||||
|
#define SCENELIST_HPP_
|
||||||
|
|
||||||
enum SceneSignal {
|
enum class SceneList {
|
||||||
//reserved members for internal use
|
//these are reserved
|
||||||
QUIT = -1,
|
QUIT,
|
||||||
CONTINUE = 0,
|
CONTINUE,
|
||||||
FIRST = 1,
|
FIRST,
|
||||||
|
|
||||||
//custom scenes
|
//custom indexes
|
||||||
SPLASHSCREEN,
|
INCOMBAT,
|
||||||
|
INWORLD,
|
||||||
|
LOBBY,
|
||||||
MAINMENU,
|
MAINMENU,
|
||||||
OPTIONSMENU,
|
OPTIONSCREEN,
|
||||||
LOBBYMENU,
|
SPLASHSCREEN,
|
||||||
WORLD,
|
};
|
||||||
COMBAT,
|
|
||||||
DISCONNECTEDSCREEN,
|
#endif
|
||||||
};
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "combat.hpp"
|
|
||||||
|
|
||||||
//Public access members
|
|
||||||
Combat::Combat(int* const argClientIndex, int* const argAccountIndex) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
Combat::~Combat() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::RenderFrame(SDL_Renderer* renderer) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//frame phases
|
|
||||||
void Combat::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::Update() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//input events
|
|
||||||
void Combat::QuitEvent() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::MouseWheel(SDL_MouseWheelEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::KeyDown(SDL_KeyboardEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Combat::KeyUp(SDL_KeyboardEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "base_scene.hpp"
|
|
||||||
|
|
||||||
class Combat: public BaseScene {
|
|
||||||
public:
|
|
||||||
//Public access members
|
|
||||||
Combat(int* const argClientIndex, int* const argAccountIndex);
|
|
||||||
~Combat();
|
|
||||||
|
|
||||||
void RenderFrame(SDL_Renderer* renderer) override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
//frame phases
|
|
||||||
void FrameStart() override;
|
|
||||||
void Update() override;
|
|
||||||
void FrameEnd() override;
|
|
||||||
|
|
||||||
//input events
|
|
||||||
void QuitEvent();
|
|
||||||
void MouseMotion(SDL_MouseMotionEvent const& event) override;
|
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseWheel(SDL_MouseWheelEvent const& event) override;
|
|
||||||
void KeyDown(SDL_KeyboardEvent const& event) override;
|
|
||||||
void KeyUp(SDL_KeyboardEvent const& event) override;
|
|
||||||
};
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "disconnected_screen.hpp"
|
|
||||||
|
|
||||||
#include "channels.hpp"
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
DisconnectedScreen::DisconnectedScreen() {
|
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
|
||||||
|
|
||||||
//setup the utility objects
|
|
||||||
//TODO: (1) resource tool, to prevent reloading like this
|
|
||||||
image.Load(GetRenderer(), config["dir.interface"] + "button_blue.png");
|
|
||||||
font = TTF_OpenFont(config["client.font"].c_str(), 12);
|
|
||||||
|
|
||||||
//check that the font loaded
|
|
||||||
if (!font) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to load a font file; " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//setup the button
|
|
||||||
backButton.SetBackgroundTexture(GetRenderer(), image.GetTexture());
|
|
||||||
backButton.SetText(GetRenderer(), font, WHITE, "Back");
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
backButton.SetX(50);
|
|
||||||
backButton.SetY(50);
|
|
||||||
|
|
||||||
//set the disconnection message text
|
|
||||||
textLine.SetX(50);
|
|
||||||
textLine.SetY(30);
|
|
||||||
textLine.SetText(GetRenderer(), font, WHITE, config["client.disconnectMessage"]);
|
|
||||||
|
|
||||||
//full reset
|
|
||||||
UDPNetworkUtility::GetSingleton().Unbind(Channels::SERVER);
|
|
||||||
|
|
||||||
//auto return
|
|
||||||
startTick = std::chrono::steady_clock::now();
|
|
||||||
}
|
|
||||||
|
|
||||||
DisconnectedScreen::~DisconnectedScreen() {
|
|
||||||
TTF_CloseFont(font);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void DisconnectedScreen::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::Update() {
|
|
||||||
if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(10)) {
|
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Eat incoming packets
|
|
||||||
while(UDPNetworkUtility::GetSingleton().Receive());
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::RenderFrame(SDL_Renderer* renderer) {
|
|
||||||
backButton.DrawTo(renderer);
|
|
||||||
textLine.DrawTo(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void DisconnectedScreen::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|
||||||
backButton.MouseMotion(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
|
||||||
backButton.MouseButtonDown(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
|
||||||
if (backButton.MouseButtonUp(event) == Button::State::RELEASED) {
|
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::MouseWheel(SDL_MouseWheelEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::KeyDown(SDL_KeyboardEvent const& event) {
|
|
||||||
switch(event.keysym.sym) {
|
|
||||||
case SDLK_ESCAPE:
|
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DisconnectedScreen::KeyUp(SDL_KeyboardEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "base_scene.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "text_line.hpp"
|
|
||||||
|
|
||||||
#include "SDL2/SDL_ttf.h"
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
class DisconnectedScreen : public BaseScene {
|
|
||||||
public:
|
|
||||||
//Public access members
|
|
||||||
DisconnectedScreen();
|
|
||||||
~DisconnectedScreen();
|
|
||||||
|
|
||||||
void RenderFrame(SDL_Renderer* renderer) override;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
//frame phases
|
|
||||||
void FrameStart() override;
|
|
||||||
void Update() override;
|
|
||||||
void FrameEnd() override;
|
|
||||||
|
|
||||||
//input events
|
|
||||||
void MouseMotion(SDL_MouseMotionEvent const& event) override;
|
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseWheel(SDL_MouseWheelEvent const& event) override;
|
|
||||||
void KeyDown(SDL_KeyboardEvent const& event) override;
|
|
||||||
void KeyUp(SDL_KeyboardEvent const& event) override;
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
Image image;
|
|
||||||
TTF_Font* font = nullptr;
|
|
||||||
Button backButton;
|
|
||||||
TextLine textLine;
|
|
||||||
|
|
||||||
//auto return
|
|
||||||
std::chrono::steady_clock::time_point startTick;
|
|
||||||
};
|
|
||||||
@@ -1,424 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "lobby_menu.hpp"
|
|
||||||
|
|
||||||
#include "channels.hpp"
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex):
|
|
||||||
clientIndex(*argClientIndex),
|
|
||||||
accountIndex(*argAccountIndex)
|
|
||||||
{
|
|
||||||
//preemptive reset
|
|
||||||
clientIndex = -1;
|
|
||||||
accountIndex = -1;
|
|
||||||
|
|
||||||
//setup the utility objects
|
|
||||||
buttonImage.Load(GetRenderer(), config["dir.interface"] + "button_blue.png");
|
|
||||||
font = TTF_OpenFont(config["client.font"].c_str(), 12);
|
|
||||||
|
|
||||||
//check that the font loaded
|
|
||||||
if (!font) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to load a font file; " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//setup the buttons
|
|
||||||
searchButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
|
||||||
searchButton.SetText(GetRenderer(), font, WHITE, "Search");
|
|
||||||
joinButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
|
||||||
joinButton.SetText(GetRenderer(), font, WHITE, "Join");
|
|
||||||
backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
|
||||||
backButton.SetText(GetRenderer(), font, WHITE, "Back");
|
|
||||||
|
|
||||||
//set the button positions (assumed)
|
|
||||||
searchButton.SetX(50);
|
|
||||||
searchButton.SetY(50);
|
|
||||||
joinButton.SetX(50);
|
|
||||||
joinButton.SetY(70);
|
|
||||||
backButton.SetX(50);
|
|
||||||
backButton.SetY(90);
|
|
||||||
|
|
||||||
//setup the text fields
|
|
||||||
username.SetText(GetRenderer(), font, WHITE, config["client.username"]);
|
|
||||||
password.SetText(GetRenderer(), font, WHITE, config["client.password"]);
|
|
||||||
handle.SetText(GetRenderer(), font, WHITE, config["client.handle"]);
|
|
||||||
avatar.SetText(GetRenderer(), font, WHITE, config["client.avatar"]);
|
|
||||||
|
|
||||||
username.SetBounds(BoundingBox{0, 0, 300, 20});
|
|
||||||
password.SetBounds(BoundingBox{0, 0, 300, 20});
|
|
||||||
handle.SetBounds(BoundingBox{0, 0, 300, 20});
|
|
||||||
avatar.SetBounds(BoundingBox{0, 0, 300, 20});
|
|
||||||
|
|
||||||
username.SetX(50);
|
|
||||||
username.SetY(110);
|
|
||||||
password.SetX(50);
|
|
||||||
password.SetY(130);
|
|
||||||
handle.SetX(50);
|
|
||||||
handle.SetY(150);
|
|
||||||
avatar.SetX(50);
|
|
||||||
avatar.SetY(170);
|
|
||||||
|
|
||||||
//pseudo-list selection
|
|
||||||
//TODO: move this into the UI library?
|
|
||||||
boundingBox = {300, 50, 200, 12};
|
|
||||||
|
|
||||||
//hacked together a highlight box
|
|
||||||
highlightImage.Create(GetRenderer(), 300, 12, {49, 150, 5, 255});
|
|
||||||
|
|
||||||
//Eat incoming packets
|
|
||||||
while(network.Receive());
|
|
||||||
|
|
||||||
//Initial broadcast
|
|
||||||
SendBroadcastRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
LobbyMenu::~LobbyMenu() {
|
|
||||||
TTF_CloseFont(font);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void LobbyMenu::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::Update() {
|
|
||||||
//suck in and process all waiting packets
|
|
||||||
SerialPacket* packetBuffer = reinterpret_cast<SerialPacket*>(new char[MAX_PACKET_SIZE]);
|
|
||||||
while(network.Receive(packetBuffer)) {
|
|
||||||
HandlePacket(packetBuffer);
|
|
||||||
}
|
|
||||||
delete reinterpret_cast<char*>(packetBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::RenderFrame(SDL_Renderer* renderer) {
|
|
||||||
//TODO: (2) I need a proper UI system for the entire client and the editor
|
|
||||||
|
|
||||||
//UI
|
|
||||||
searchButton.DrawTo(renderer);
|
|
||||||
joinButton.DrawTo(renderer);
|
|
||||||
backButton.DrawTo(renderer);
|
|
||||||
|
|
||||||
username.DrawTo(renderer);
|
|
||||||
password.DrawTo(renderer);
|
|
||||||
handle.DrawTo(renderer);
|
|
||||||
avatar.DrawTo(renderer);
|
|
||||||
|
|
||||||
//TODO: (3) draw headers for the server list
|
|
||||||
//TODO: (3) ping/delay displayed in the server list
|
|
||||||
for (int i = 0; i < serverVector.size(); i++) {
|
|
||||||
//draw the selected server's highlight
|
|
||||||
if (selection == &serverVector[i]) {
|
|
||||||
highlightImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
|
|
||||||
}
|
|
||||||
|
|
||||||
//draw the server's info
|
|
||||||
serverVector[i].nameImage.SetX(boundingBox.x);
|
|
||||||
serverVector[i].nameImage.SetY(boundingBox.y + boundingBox.h * i);
|
|
||||||
serverVector[i].nameImage.DrawTo(renderer);
|
|
||||||
|
|
||||||
serverVector[i].playerCountImage.SetX(boundingBox.x+276);
|
|
||||||
serverVector[i].playerCountImage.SetY(boundingBox.y + boundingBox.h * i);
|
|
||||||
serverVector[i].playerCountImage.DrawTo(renderer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|
||||||
searchButton.MouseMotion(event);
|
|
||||||
joinButton.MouseMotion(event);
|
|
||||||
backButton.MouseMotion(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
|
||||||
searchButton.MouseButtonDown(event);
|
|
||||||
joinButton.MouseButtonDown(event);
|
|
||||||
backButton.MouseButtonDown(event);
|
|
||||||
|
|
||||||
if (username.MouseButtonDown(event)) {
|
|
||||||
//GUI trick
|
|
||||||
if (!username.GetText().compare(config["client.username"])) {
|
|
||||||
username.SetText(GetRenderer(), font, WHITE, "");
|
|
||||||
}
|
|
||||||
SDL_StartTextInput();
|
|
||||||
}
|
|
||||||
if (password.MouseButtonDown(event)) {
|
|
||||||
//GUI trick
|
|
||||||
if (!password.GetText().compare(config["client.password"])) {
|
|
||||||
password.SetText(GetRenderer(), font, WHITE, "");
|
|
||||||
}
|
|
||||||
SDL_StartTextInput();
|
|
||||||
}
|
|
||||||
if (handle.MouseButtonDown(event)) {
|
|
||||||
//GUI trick
|
|
||||||
if (!handle.GetText().compare(config["client.handle"])) {
|
|
||||||
handle.SetText(GetRenderer(), font, WHITE, "");
|
|
||||||
}
|
|
||||||
SDL_StartTextInput();
|
|
||||||
}
|
|
||||||
if (avatar.MouseButtonDown(event)) {
|
|
||||||
//GUI trick
|
|
||||||
if (!avatar.GetText().compare(config["client.avatar"])) {
|
|
||||||
avatar.SetText(GetRenderer(), font, WHITE, "");
|
|
||||||
}
|
|
||||||
SDL_StartTextInput();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
|
||||||
if (searchButton.MouseButtonUp(event) == Button::State::RELEASED) {
|
|
||||||
SendBroadcastRequest();
|
|
||||||
}
|
|
||||||
if (joinButton.MouseButtonUp(event) == Button::State::RELEASED && selection && selection->compatible) {
|
|
||||||
SendJoinRequest();
|
|
||||||
}
|
|
||||||
if (backButton.MouseButtonUp(event) == Button::State::RELEASED) {
|
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
}
|
|
||||||
|
|
||||||
//has the user selected a server on the list?
|
|
||||||
BoundingBox tmpBox = boundingBox;
|
|
||||||
tmpBox.h *= serverVector.size(); //within the list bounds
|
|
||||||
if (tmpBox.CheckOverlap({event.x, event.y})) {
|
|
||||||
//NOTE: this memory trick requires a vector
|
|
||||||
selection = &serverVector[(event.y - boundingBox.y)/boundingBox.h];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
selection = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::MouseWheel(SDL_MouseWheelEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& event) {
|
|
||||||
switch(event.keysym.sym) {
|
|
||||||
case SDLK_ESCAPE:
|
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SDLK_BACKSPACE:
|
|
||||||
//easier than mucking about with SDL_TextEditEvent
|
|
||||||
if (username.GetFocus()) {
|
|
||||||
username.PopChars(GetRenderer(), font, WHITE, 1);
|
|
||||||
}
|
|
||||||
if (password.GetFocus()) {
|
|
||||||
password.PopChars(GetRenderer(), font, WHITE, 1);
|
|
||||||
}
|
|
||||||
if (handle.GetFocus()) {
|
|
||||||
handle.PopChars(GetRenderer(), font, WHITE, 1);
|
|
||||||
}
|
|
||||||
if (avatar.GetFocus()) {
|
|
||||||
avatar.PopChars(GetRenderer(), font, WHITE, 1);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::KeyUp(SDL_KeyboardEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::TextInput(SDL_TextInputEvent const& event) {
|
|
||||||
if (username.GetFocus()) {
|
|
||||||
username.PushText(GetRenderer(), font, WHITE, std::string(event.text));
|
|
||||||
}
|
|
||||||
if (password.GetFocus()) {
|
|
||||||
password.PushText(GetRenderer(), font, WHITE, std::string(event.text));
|
|
||||||
}
|
|
||||||
if (handle.GetFocus()) {
|
|
||||||
handle.PushText(GetRenderer(), font, WHITE, std::string(event.text));
|
|
||||||
}
|
|
||||||
if (avatar.GetFocus()) {
|
|
||||||
avatar.PushText(GetRenderer(), font, WHITE, std::string(event.text));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Network handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
|
|
||||||
switch(argPacket->type) {
|
|
||||||
//responses
|
|
||||||
case SerialPacketType::BROADCAST_RESPONSE:
|
|
||||||
HandleBroadcastResponse(static_cast<ServerPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
case SerialPacketType::JOIN_RESPONSE:
|
|
||||||
HandleJoinResponse(static_cast<ClientPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
case SerialPacketType::LOGIN_RESPONSE:
|
|
||||||
HandleLoginResponse(static_cast<ClientPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
|
|
||||||
//rejections
|
|
||||||
case SerialPacketType::JOIN_REJECTION:
|
|
||||||
HandleJoinRejection(static_cast<TextPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
case SerialPacketType::LOGIN_REJECTION:
|
|
||||||
HandleLoginRejection(static_cast<TextPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
|
|
||||||
//handle errors
|
|
||||||
default: {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Unknown SerialPacketType encountered in LobbyMenu: " << static_cast<int>(argPacket->type);
|
|
||||||
throw(std::runtime_error( msg.str() ));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
|
|
||||||
//extract the data
|
|
||||||
ServerInfo newServer;
|
|
||||||
|
|
||||||
newServer.address = argPacket->srcAddress;
|
|
||||||
newServer.name = argPacket->name;
|
|
||||||
newServer.playerCount = argPacket->playerCount;
|
|
||||||
newServer.version = argPacket->version;
|
|
||||||
newServer.compatible = newServer.version == NETWORK_VERSION;
|
|
||||||
|
|
||||||
//push
|
|
||||||
serverVector.push_back(newServer);
|
|
||||||
|
|
||||||
//BUGFIX: since TextLine lacks the memory management of Image, I'll wait until after the line is in the vector to handle these
|
|
||||||
|
|
||||||
//fancy colors
|
|
||||||
SDL_Color color;
|
|
||||||
if (newServer.compatible) {
|
|
||||||
color = {255, 255, 255, 255};
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
color = {255, 0, 0, 255};
|
|
||||||
}
|
|
||||||
|
|
||||||
//fancy itoa
|
|
||||||
auto itoa_base10 = [](int i) -> std::string {
|
|
||||||
char str[20];
|
|
||||||
sprintf(str, "%d", i);
|
|
||||||
return std::string(str);
|
|
||||||
};
|
|
||||||
|
|
||||||
//text graphics
|
|
||||||
serverVector.back().nameImage.SetText(GetRenderer(), font, color, newServer.name);
|
|
||||||
serverVector.back().playerCountImage.SetText(GetRenderer(), font, color, itoa_base10(newServer.playerCount));
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
|
|
||||||
//save the server's data
|
|
||||||
clientIndex = argPacket->clientIndex;
|
|
||||||
network.Bind(argPacket->srcAddress, Channels::SERVER);
|
|
||||||
|
|
||||||
//request login data
|
|
||||||
SendLoginRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::HandleLoginResponse(ClientPacket* const argPacket) {
|
|
||||||
if (argPacket->clientIndex != clientIndex) {
|
|
||||||
throw(std::runtime_error("Client index invalid during login"));
|
|
||||||
}
|
|
||||||
accountIndex = argPacket->accountIndex;
|
|
||||||
SetSceneSignal(SceneSignal::WORLD);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::HandleJoinRejection(TextPacket* const argPacket) {
|
|
||||||
//NOTE: NEVER HAPPENS
|
|
||||||
throw(std::runtime_error("HandleJoinRejection"));
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::HandleLoginRejection(TextPacket* const argPacket) {
|
|
||||||
config["client.disconnectMessage"] = std::string() + "Join request rejected: " + argPacket->text;
|
|
||||||
SetSceneSignal(SceneSignal::DISCONNECTEDSCREEN);
|
|
||||||
|
|
||||||
//avoid crashes from the heartbeat system
|
|
||||||
ClientPacket newPacket;
|
|
||||||
newPacket.type = SerialPacketType::DISCONNECT_REQUEST;
|
|
||||||
newPacket.clientIndex = clientIndex;
|
|
||||||
network.SendTo(argPacket->srcAddress, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//server control
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void LobbyMenu::SendBroadcastRequest() {
|
|
||||||
//broadcast to the home server, and to the LAN
|
|
||||||
ClientPacket packet;
|
|
||||||
|
|
||||||
packet.type = SerialPacketType::BROADCAST_REQUEST;
|
|
||||||
network.SendTo(config["server.home"].c_str(), config.Int("server.port"), &packet);
|
|
||||||
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet);
|
|
||||||
|
|
||||||
//reset the server list
|
|
||||||
serverVector.clear();
|
|
||||||
selection = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::SendJoinRequest() {
|
|
||||||
//BUG: 101 received in LobbyMenu on failed join
|
|
||||||
//pack the packet
|
|
||||||
ClientPacket packet;
|
|
||||||
packet.type = SerialPacketType::JOIN_REQUEST;
|
|
||||||
|
|
||||||
//join the selected server
|
|
||||||
network.SendTo(selection->address, &packet);
|
|
||||||
selection = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::SendLoginRequest() {
|
|
||||||
//NOTE: high cohesion
|
|
||||||
//TODO: (9) have a separate login screen
|
|
||||||
ClientPacket packet;
|
|
||||||
packet.type = SerialPacketType::LOGIN_REQUEST;
|
|
||||||
packet.clientIndex = clientIndex;
|
|
||||||
strncpy(packet.username, username.GetText().c_str(), PACKET_STRING_SIZE+1);
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &packet);
|
|
||||||
|
|
||||||
//TODO: remove
|
|
||||||
config["client.username"] = username.GetText();
|
|
||||||
config["client.password"] = password.GetText();
|
|
||||||
config["client.handle"] = handle.GetText();
|
|
||||||
config["client.avatar"] = avatar.GetText();
|
|
||||||
}
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
//graphics & ui
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
#include "bounding_box.hpp"
|
|
||||||
#include "text_field.hpp"
|
|
||||||
#include "text_line.hpp"
|
|
||||||
|
|
||||||
#include "SDL2/SDL_ttf.h"
|
|
||||||
|
|
||||||
//utilities
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
#include "serial_packet.hpp"
|
|
||||||
|
|
||||||
//client
|
|
||||||
#include "base_scene.hpp"
|
|
||||||
|
|
||||||
//STL
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class LobbyMenu : public BaseScene {
|
|
||||||
public:
|
|
||||||
//Public access members
|
|
||||||
LobbyMenu(int* const argClientIndex, int* const argAccountIndex);
|
|
||||||
~LobbyMenu();
|
|
||||||
|
|
||||||
void RenderFrame(SDL_Renderer* renderer) override;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
//frame phases
|
|
||||||
void FrameStart() override;
|
|
||||||
void Update() override;
|
|
||||||
void FrameEnd() override;
|
|
||||||
|
|
||||||
//input events
|
|
||||||
void MouseMotion(SDL_MouseMotionEvent const& event) override;
|
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseWheel(SDL_MouseWheelEvent const& event) override;
|
|
||||||
void KeyDown(SDL_KeyboardEvent const& event) override;
|
|
||||||
void KeyUp(SDL_KeyboardEvent const& event) override;
|
|
||||||
void TextInput(SDL_TextInputEvent const& event) override;
|
|
||||||
|
|
||||||
//Network handlers
|
|
||||||
void HandlePacket(SerialPacket* const);
|
|
||||||
void HandleBroadcastResponse(ServerPacket* const);
|
|
||||||
void HandleJoinResponse(ClientPacket* const);
|
|
||||||
void HandleLoginResponse(ClientPacket* const);
|
|
||||||
void HandleJoinRejection(TextPacket* const);
|
|
||||||
void HandleLoginRejection(TextPacket* const);
|
|
||||||
|
|
||||||
//server control
|
|
||||||
void SendBroadcastRequest();
|
|
||||||
void SendJoinRequest();
|
|
||||||
void SendLoginRequest();
|
|
||||||
|
|
||||||
//shared parameters
|
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
|
||||||
UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton();
|
|
||||||
int& clientIndex;
|
|
||||||
int& accountIndex;
|
|
||||||
|
|
||||||
//define the list object
|
|
||||||
struct ServerInfo {
|
|
||||||
//graphics
|
|
||||||
TextLine nameImage;
|
|
||||||
TextLine playerCountImage;
|
|
||||||
|
|
||||||
//networking
|
|
||||||
IPaddress address;
|
|
||||||
std::string name;
|
|
||||||
int playerCount;
|
|
||||||
int version;
|
|
||||||
bool compatible;
|
|
||||||
};
|
|
||||||
|
|
||||||
//members
|
|
||||||
Image buttonImage;
|
|
||||||
Image highlightImage;
|
|
||||||
TTF_Font* font = nullptr;
|
|
||||||
Button searchButton;
|
|
||||||
Button joinButton;
|
|
||||||
Button backButton;
|
|
||||||
TextField username;
|
|
||||||
TextField password;
|
|
||||||
TextField handle;
|
|
||||||
TextField avatar;
|
|
||||||
|
|
||||||
std::vector<ServerInfo> serverVector;
|
|
||||||
ServerInfo* selection = nullptr;
|
|
||||||
|
|
||||||
BoundingBox boundingBox;
|
|
||||||
};
|
|
||||||
@@ -1,150 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "main_menu.hpp"
|
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
MainMenu::MainMenu() {
|
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
|
||||||
|
|
||||||
//setup the utility objects
|
|
||||||
buttonImage.Load(GetRenderer(), config["dir.interface"] + "button_blue.png");
|
|
||||||
font = TTF_OpenFont(config["client.font"].c_str(), 12);
|
|
||||||
|
|
||||||
//check that the font loaded
|
|
||||||
if (!font) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to load a font file; " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//setup the buttons
|
|
||||||
startButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
|
||||||
startButton.SetText(GetRenderer(), font, WHITE, "Start");
|
|
||||||
optionsButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
|
||||||
optionsButton.SetText(GetRenderer(), font, WHITE, "Options");
|
|
||||||
quitButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
|
||||||
quitButton.SetText(GetRenderer(), font, WHITE, "Quit");
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
startButton.SetX(50);
|
|
||||||
startButton.SetY(50 + 20 * 0);
|
|
||||||
optionsButton.SetX(50);
|
|
||||||
optionsButton.SetY(50 + 20 * 1);
|
|
||||||
quitButton.SetX(50);
|
|
||||||
quitButton.SetY(50 + 20 * 2);
|
|
||||||
|
|
||||||
//text box
|
|
||||||
int h = -1;
|
|
||||||
SDL_RenderGetLogicalSize(GetRenderer(), nullptr, &h);
|
|
||||||
|
|
||||||
textBox.SetX(50);
|
|
||||||
textBox.SetY(h-100);
|
|
||||||
|
|
||||||
textBox.PushLine(GetRenderer(), font, WHITE, "Thanks for playing!");
|
|
||||||
textBox.PushLine(GetRenderer(), font, WHITE, "You can get the latest version at: ");
|
|
||||||
textBox.PushLine(GetRenderer(), font, WHITE, "krgamestudios.com"); //TODO: (9) click to open the website/update
|
|
||||||
|
|
||||||
//debug
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
MainMenu::~MainMenu() {
|
|
||||||
TTF_CloseFont(font);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void MainMenu::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::Update() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::RenderFrame(SDL_Renderer* renderer) {
|
|
||||||
startButton.DrawTo(renderer);
|
|
||||||
optionsButton.DrawTo(renderer);
|
|
||||||
quitButton.DrawTo(renderer);
|
|
||||||
textBox.DrawTo(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void MainMenu::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|
||||||
startButton.MouseMotion(event);
|
|
||||||
optionsButton.MouseMotion(event);
|
|
||||||
quitButton.MouseMotion(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
|
||||||
startButton.MouseButtonDown(event);
|
|
||||||
optionsButton.MouseButtonDown(event);
|
|
||||||
quitButton.MouseButtonDown(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
|
||||||
//TODO: (2) Buttons should only register as "selected" when the left button is used
|
|
||||||
if (startButton.MouseButtonUp(event) == Button::State::RELEASED) {
|
|
||||||
SetSceneSignal(SceneSignal::LOBBYMENU);
|
|
||||||
}
|
|
||||||
if (optionsButton.MouseButtonUp(event) == Button::State::RELEASED) {
|
|
||||||
SetSceneSignal(SceneSignal::OPTIONSMENU);
|
|
||||||
}
|
|
||||||
if (quitButton.MouseButtonUp(event) == Button::State::RELEASED) {
|
|
||||||
QuitEvent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::MouseWheel(SDL_MouseWheelEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::KeyDown(SDL_KeyboardEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainMenu::KeyUp(SDL_KeyboardEvent const& event) {
|
|
||||||
switch(event.keysym.sym) {
|
|
||||||
case SDLK_ESCAPE:
|
|
||||||
QuitEvent();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. .. ../entities ../../common/debugging ../../common/global_defines ../../common/network ../../common/network/packet_types ../../common/utilities ../../TurtleGUI ../../TurtleMap
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#output
|
|
||||||
OUTDIR=..
|
|
||||||
OUT=$(addprefix $(OUTDIR)/,client.a)
|
|
||||||
|
|
||||||
#targets
|
|
||||||
all: $(OBJ) $(OUT)
|
|
||||||
ar -crs $(OUT) $(OBJ)
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUT): | $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR):
|
|
||||||
mkdir $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUTDIR):
|
|
||||||
mkdir $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "options_menu.hpp"
|
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
constexpr SDL_Color WHITE = {255, 255, 255, 255};
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
OptionsMenu::OptionsMenu() {
|
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
|
||||||
|
|
||||||
//setup the utility objects
|
|
||||||
buttonImage.Load(GetRenderer(), config["dir.interface"] + "button_blue.png");
|
|
||||||
font = TTF_OpenFont(config["client.font"].c_str(), 12);
|
|
||||||
|
|
||||||
//check that the font loaded
|
|
||||||
if (!font) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Failed to load a font file; " << SDL_GetError();
|
|
||||||
throw(std::runtime_error(msg.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
//setup the button
|
|
||||||
backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
|
|
||||||
backButton.SetText(GetRenderer(), font, WHITE, "Back");
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
backButton.SetX(50);
|
|
||||||
backButton.SetY(50);
|
|
||||||
|
|
||||||
//text line
|
|
||||||
textLine.SetX(50);
|
|
||||||
textLine.SetY(30);
|
|
||||||
textLine.SetText(GetRenderer(), font, WHITE, "Am I making any progress?");
|
|
||||||
}
|
|
||||||
|
|
||||||
OptionsMenu::~OptionsMenu() {
|
|
||||||
TTF_CloseFont(font);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void OptionsMenu::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::Update() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::RenderFrame(SDL_Renderer* renderer) {
|
|
||||||
backButton.DrawTo(renderer);
|
|
||||||
textLine.DrawTo(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void OptionsMenu::MouseMotion(SDL_MouseMotionEvent const& event) {
|
|
||||||
backButton.MouseMotion(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
|
||||||
backButton.MouseButtonDown(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
|
||||||
if (backButton.MouseButtonUp(event) == Button::State::RELEASED) {
|
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::MouseWheel(SDL_MouseWheelEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::KeyDown(SDL_KeyboardEvent const& event) {
|
|
||||||
switch(event.keysym.sym) {
|
|
||||||
case SDLK_ESCAPE:
|
|
||||||
SetSceneSignal(SceneSignal::MAINMENU);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::KeyUp(SDL_KeyboardEvent const& event) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "button.hpp"
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "text_line.hpp"
|
|
||||||
|
|
||||||
#include "base_scene.hpp"
|
|
||||||
|
|
||||||
//NOTE: The options screen needs to be USED
|
|
||||||
class OptionsMenu : public BaseScene {
|
|
||||||
public:
|
|
||||||
//Public access members
|
|
||||||
OptionsMenu();
|
|
||||||
~OptionsMenu();
|
|
||||||
|
|
||||||
void RenderFrame(SDL_Renderer* renderer) override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
//frame phases
|
|
||||||
void FrameStart() override;
|
|
||||||
void Update() override;
|
|
||||||
void FrameEnd() override;
|
|
||||||
|
|
||||||
//input events
|
|
||||||
void MouseMotion(SDL_MouseMotionEvent const& event) override;
|
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseWheel(SDL_MouseWheelEvent const& event) override;
|
|
||||||
void KeyDown(SDL_KeyboardEvent const& event) override;
|
|
||||||
void KeyUp(SDL_KeyboardEvent const& event) override;
|
|
||||||
|
|
||||||
//members
|
|
||||||
Image buttonImage;
|
|
||||||
TTF_Font* font = nullptr;
|
|
||||||
Button backButton;
|
|
||||||
TextLine textLine;
|
|
||||||
};
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,171 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
//maps
|
|
||||||
#include "region_pager_base.hpp"
|
|
||||||
|
|
||||||
//utilities
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
#include "serial_packet.hpp"
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
#include "tile_sheet.hpp"
|
|
||||||
#include "text_line.hpp"
|
|
||||||
|
|
||||||
//common
|
|
||||||
#include "frame_rate.hpp"
|
|
||||||
|
|
||||||
//client
|
|
||||||
#include "barrier_manager.hpp"
|
|
||||||
#include "base_scene.hpp"
|
|
||||||
#include "base_barrier.hpp"
|
|
||||||
#include "base_creature.hpp"
|
|
||||||
#include "heartbeat_utility.hpp"
|
|
||||||
#include "local_character.hpp"
|
|
||||||
|
|
||||||
#include "SDL2/SDL.h"
|
|
||||||
#include "SDL2/SDL_net.h"
|
|
||||||
#include "SDL2/SDL_ttf.h"
|
|
||||||
|
|
||||||
//STL
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
class World: public BaseScene {
|
|
||||||
public:
|
|
||||||
//Public access members
|
|
||||||
World(int* const argClientIndex, int* const argAccountIndex);
|
|
||||||
~World();
|
|
||||||
|
|
||||||
void RenderFrame(SDL_Renderer* renderer) override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
//frame phases
|
|
||||||
void FrameStart() override;
|
|
||||||
void Update() override;
|
|
||||||
void FrameEnd() override;
|
|
||||||
|
|
||||||
//input events
|
|
||||||
void QuitEvent();
|
|
||||||
void MouseMotion(SDL_MouseMotionEvent const& event) override;
|
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const& event) override;
|
|
||||||
void MouseWheel(SDL_MouseWheelEvent const& event) override;
|
|
||||||
void KeyDown(SDL_KeyboardEvent const& event) override;
|
|
||||||
void KeyUp(SDL_KeyboardEvent const& event) override;
|
|
||||||
|
|
||||||
//handle incoming traffic
|
|
||||||
void HandlePacket(SerialPacket* const);
|
|
||||||
|
|
||||||
//basic connections
|
|
||||||
void SendLogoutRequest();
|
|
||||||
void SendDisconnectRequest();
|
|
||||||
void SendAdminDisconnectForced();
|
|
||||||
void SendAdminShutdownRequest();
|
|
||||||
|
|
||||||
void hLogoutResponse(ClientPacket* const);
|
|
||||||
void hDisconnectResponse(ClientPacket* const);
|
|
||||||
void hAdminDisconnectForced(ClientPacket* const);
|
|
||||||
|
|
||||||
//map management
|
|
||||||
void SendRegionRequest(int roomIndex, int x, int y);
|
|
||||||
void hRegionContent(RegionPacket* const);
|
|
||||||
void UpdateMap();
|
|
||||||
|
|
||||||
//character management
|
|
||||||
void hCharacterUpdate(CharacterPacket* const);
|
|
||||||
void hCharacterCreate(CharacterPacket* const);
|
|
||||||
void hCharacterUnload(CharacterPacket* const);
|
|
||||||
void hQueryCharacterExists(CharacterPacket* const);
|
|
||||||
void hCharacterMovement(CharacterPacket* const);
|
|
||||||
void hCharacterRejection(TextPacket* const);
|
|
||||||
|
|
||||||
//creature management
|
|
||||||
void hCreatureUpdate(CreaturePacket* const);
|
|
||||||
void hCreatureCreate(CreaturePacket* const);
|
|
||||||
void hCreatureUnload(CreaturePacket* const);
|
|
||||||
void hQueryCreatureExists(CreaturePacket* const);
|
|
||||||
void hCreatureMovement(CreaturePacket* const);
|
|
||||||
|
|
||||||
//barrier management
|
|
||||||
void hBarrierUpdate(BarrierPacket* const);
|
|
||||||
void hBarrierCreate(BarrierPacket* const);
|
|
||||||
void hBarrierUnload(BarrierPacket* const);
|
|
||||||
void hQueryBarrierExists(BarrierPacket* const);
|
|
||||||
|
|
||||||
//chat
|
|
||||||
//TODO: ui chat engine
|
|
||||||
void hTextBroadcast(TextPacket* const);
|
|
||||||
void hTextSpeech(TextPacket* const);
|
|
||||||
void hTextWhisper(TextPacket* const);
|
|
||||||
|
|
||||||
//general gameplay
|
|
||||||
void SendLocalCharacterMovement();
|
|
||||||
std::list<BoundingBox> GenerateCollisionGrid(Entity*, int tileWidth, int tileHeight);
|
|
||||||
|
|
||||||
//indexes
|
|
||||||
int& clientIndex;
|
|
||||||
int& accountIndex;
|
|
||||||
int characterIndex = -1;
|
|
||||||
int roomIndex = -1;
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
TileSheet tileSheet;
|
|
||||||
|
|
||||||
//map
|
|
||||||
RegionPagerBase regionPager;
|
|
||||||
|
|
||||||
//UI
|
|
||||||
Image buttonImage;
|
|
||||||
TTF_Font* font = nullptr;
|
|
||||||
Button disconnectButton;
|
|
||||||
Button shutdownButton;
|
|
||||||
FrameRate fps;
|
|
||||||
TextLine fpsTextLine;
|
|
||||||
|
|
||||||
//the camera structure
|
|
||||||
struct {
|
|
||||||
int x = 0, y = 0;
|
|
||||||
int width = 0, height = 0;
|
|
||||||
int marginX = 0, marginY = 0;
|
|
||||||
} camera;
|
|
||||||
|
|
||||||
//entities
|
|
||||||
BarrierManager barrierMgr;
|
|
||||||
std::map<int, BaseCharacter> characterMap;
|
|
||||||
std::map<int, BaseCreature> creatureMap;
|
|
||||||
LocalCharacter* localCharacter = nullptr;
|
|
||||||
|
|
||||||
//heartbeat
|
|
||||||
HeartbeatUtility heartbeatUtility;
|
|
||||||
typedef std::chrono::steady_clock Clock;
|
|
||||||
Clock::time_point queryTime = Clock::now() - std::chrono::seconds(4); //back 4 seconds to trigger automatically
|
|
||||||
|
|
||||||
//ugly references; I hate this
|
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
|
||||||
UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton();
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "splash_screen.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Public access members
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
SplashScreen::SplashScreen() {
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "entering SplashScreen" << endl;
|
||||||
|
#endif
|
||||||
|
logo.SetSurface(surfaceMgr->Load("splash-logo", configUtil->String("logos") + "/krstudios.bmp"));
|
||||||
|
}
|
||||||
|
|
||||||
|
SplashScreen::~SplashScreen() {
|
||||||
|
surfaceMgr->Free("splash-logo");
|
||||||
|
#ifdef DEBUG
|
||||||
|
cout << "leaving SplashScreen" << endl;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------
|
||||||
|
//Frame loop
|
||||||
|
//-------------------------
|
||||||
|
|
||||||
|
void SplashScreen::RunFrame(double delta) {
|
||||||
|
HandleEvents();
|
||||||
|
if (!loaded) {
|
||||||
|
//never repeat this
|
||||||
|
loaded = true;
|
||||||
|
|
||||||
|
//quick draw
|
||||||
|
RenderFrame();
|
||||||
|
|
||||||
|
LoadResources();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std::chrono::steady_clock::now() - start > std::chrono::duration<int>(1)) {
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SplashScreen::RenderFrame() {
|
||||||
|
SDL_FillRect(GetScreen(), 0, 0);
|
||||||
|
int x = (GetScreen()->w - logo.GetClipW()) / 2;
|
||||||
|
int y = (GetScreen()->h - logo.GetClipH()) / 2;
|
||||||
|
logo.DrawTo(GetScreen(), x, y);
|
||||||
|
SDL_Flip(GetScreen());
|
||||||
|
}
|
||||||
|
|
||||||
|
void SplashScreen::LoadResources() {
|
||||||
|
//standard
|
||||||
|
surfaceMgr->Load("font", configUtil->String("fonts") + "/pk_white_8.bmp");
|
||||||
|
surfaceMgr->Load("button", configUtil->String("interface") + "/button_menu.bmp");
|
||||||
|
|
||||||
|
//debugging
|
||||||
|
surfaceMgr->Load("elliot", configUtil->String("sprites") + "/elliot2.bmp");
|
||||||
|
surfaceMgr->Load("coa", configUtil->String("sprites") + "/coa2.bmp");
|
||||||
|
surfaceMgr->Load("flower", configUtil->String("sprites") + "/aniflower.bmp");
|
||||||
|
surfaceMgr->Load("terrain", configUtil->String("tilesets") + "/terrain.bmp");
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,30 +19,35 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef SPLASHSCREEN_HPP_
|
||||||
|
#define SPLASHSCREEN_HPP_
|
||||||
|
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
#include "singleton.hpp"
|
||||||
|
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
#include "surface_manager.hpp"
|
||||||
#include "image.hpp"
|
#include "image.hpp"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
class SplashScreen : public BaseScene {
|
class SplashScreen : public BaseScene {
|
||||||
public:
|
public:
|
||||||
//Public access members
|
/* Public access members */
|
||||||
SplashScreen(SDL_Window*);
|
SplashScreen();
|
||||||
~SplashScreen();
|
~SplashScreen();
|
||||||
|
|
||||||
void RenderFrame(SDL_Renderer* renderer) override;
|
protected:
|
||||||
|
/* Frame loop */
|
||||||
|
void RunFrame(double delta);
|
||||||
|
void RenderFrame();
|
||||||
|
void LoadResources();
|
||||||
|
|
||||||
private:
|
bool loaded = false;
|
||||||
//Frame loop
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
void FrameStart() override;
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
|
|
||||||
//members
|
|
||||||
std::chrono::steady_clock::time_point startTick;
|
|
||||||
Image logo;
|
Image logo;
|
||||||
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
||||||
//screws with the window
|
|
||||||
SDL_Window* window = nullptr;
|
|
||||||
int windowWidth, windowHeight;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,186 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "sprite_sheet.hpp"
|
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
SpriteSheet& SpriteSheet::operator=(SpriteSheet const& rhs) {
|
|
||||||
//don't screw yourself
|
|
||||||
if (this == &rhs) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free();
|
|
||||||
|
|
||||||
//Copy the other SpriteSheet's stuff
|
|
||||||
texture = rhs.texture;
|
|
||||||
clip = rhs.clip;
|
|
||||||
local = false;
|
|
||||||
countX = rhs.countX;
|
|
||||||
countY = rhs.countY;
|
|
||||||
indexX = rhs.indexX;
|
|
||||||
indexY = rhs.indexY;
|
|
||||||
delay = rhs.delay;
|
|
||||||
tick = rhs.tick;
|
|
||||||
}
|
|
||||||
|
|
||||||
SpriteSheet& SpriteSheet::operator=(SpriteSheet&& rhs) {
|
|
||||||
//don't screw yourself
|
|
||||||
if (this == &rhs) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Free();
|
|
||||||
|
|
||||||
//Steal the other SpriteSheet's stuff
|
|
||||||
texture = rhs.texture;
|
|
||||||
clip = rhs.clip;
|
|
||||||
local = rhs.local;
|
|
||||||
countX = rhs.countX;
|
|
||||||
countY = rhs.countY;
|
|
||||||
indexX = rhs.indexX;
|
|
||||||
indexY = rhs.indexY;
|
|
||||||
delay = rhs.delay;
|
|
||||||
tick = rhs.tick;
|
|
||||||
|
|
||||||
rhs.texture = nullptr;
|
|
||||||
rhs.clip = {0, 0, 0, 0};
|
|
||||||
rhs.local = false;
|
|
||||||
rhs.countX = 0;
|
|
||||||
rhs.countY = 0;
|
|
||||||
rhs.indexX = 0;
|
|
||||||
rhs.indexY = 0;
|
|
||||||
rhs.delay = 0.0;
|
|
||||||
rhs.tick = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpriteSheet::Update(double delta) {
|
|
||||||
//if the delay has passed
|
|
||||||
if (delay && (tick += delta) >= delay) {
|
|
||||||
//if the index is out of bounds
|
|
||||||
if (++indexX >= countX) {
|
|
||||||
indexX = 0;
|
|
||||||
}
|
|
||||||
tick = 0;
|
|
||||||
}
|
|
||||||
//modify area drawn
|
|
||||||
clip.x = indexX * clip.w;
|
|
||||||
clip.y = indexY * clip.h;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Texture* SpriteSheet::Load(SDL_Renderer* r, std::string fname, Uint16 cx, Uint16 cy) {
|
|
||||||
//call the base function
|
|
||||||
Image::Load(r, fname);
|
|
||||||
|
|
||||||
//set the metadata
|
|
||||||
countX = cx;
|
|
||||||
countY = cy;
|
|
||||||
|
|
||||||
//assume clip.x and clip.y were set to the size of the texture
|
|
||||||
//reduce the w & h to the size of one cell
|
|
||||||
clip.w = clip.w / countX;
|
|
||||||
clip.h = clip.h / countY;
|
|
||||||
|
|
||||||
indexX = indexY = 0;
|
|
||||||
delay = tick = 0.0;
|
|
||||||
|
|
||||||
return texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Texture* SpriteSheet::Create(SDL_Renderer* r, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy) {
|
|
||||||
//call the base function
|
|
||||||
Image::Create(r, w, h);
|
|
||||||
|
|
||||||
//set the metadata
|
|
||||||
countX = cx;
|
|
||||||
countY = cy;
|
|
||||||
|
|
||||||
//assume clip.x and clip.y were set to the size of the texture
|
|
||||||
//reduce the w & h to the size of one cell
|
|
||||||
clip.w = clip.w / countX;
|
|
||||||
clip.h = clip.h / countY;
|
|
||||||
|
|
||||||
indexX = indexY = 0;
|
|
||||||
delay = tick = 0.0;
|
|
||||||
|
|
||||||
return texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Texture* SpriteSheet::SetTexture(SDL_Texture* ptr, Uint16 cx, Uint16 cy) {
|
|
||||||
//call the base function
|
|
||||||
Image::SetTexture(ptr);
|
|
||||||
|
|
||||||
//set the metadata
|
|
||||||
countX = cx;
|
|
||||||
countY = cy;
|
|
||||||
|
|
||||||
//assume clip.x and clip.y were set to the size of the texture
|
|
||||||
//reduce the w & h to the size of one cell
|
|
||||||
clip.w = clip.w / countX;
|
|
||||||
clip.h = clip.h / countY;
|
|
||||||
|
|
||||||
indexX = indexY = 0;
|
|
||||||
delay = tick = 0.0;
|
|
||||||
|
|
||||||
return texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpriteSheet::Free() {
|
|
||||||
Image::Free();
|
|
||||||
countX = countY = 0;
|
|
||||||
indexX = indexY = 0;
|
|
||||||
delay = tick = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetCountX(Uint16 i) {
|
|
||||||
indexX = 0;
|
|
||||||
return countX = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetCountY(Uint16 i) {
|
|
||||||
indexY = 0;
|
|
||||||
return countY = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetIndexX(Uint16 i) {
|
|
||||||
if (i > countX) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Cannot set index 'x' to " << i;
|
|
||||||
throw(std::out_of_range(msg.str()));
|
|
||||||
}
|
|
||||||
return indexX = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetIndexY(Uint16 i) {
|
|
||||||
if (i > countY) {
|
|
||||||
std::ostringstream msg;
|
|
||||||
msg << "Cannot set index 'y' to " << i;
|
|
||||||
throw(std::invalid_argument(msg.str()));
|
|
||||||
}
|
|
||||||
return indexY = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
double SpriteSheet::SetDelay(double d) {
|
|
||||||
tick = 0;
|
|
||||||
return delay = d;
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "image.hpp"
|
|
||||||
|
|
||||||
class SpriteSheet : public Image {
|
|
||||||
public:
|
|
||||||
SpriteSheet() = default;
|
|
||||||
SpriteSheet(SpriteSheet const& rhs) { *this = rhs; }
|
|
||||||
SpriteSheet(SpriteSheet&& rhs) { *this = std::move(rhs); }
|
|
||||||
SpriteSheet(SDL_Renderer* r, std::string fname, Uint16 cx, Uint16 cy)
|
|
||||||
{ Load(r, fname, cx, cy); }
|
|
||||||
SpriteSheet(SDL_Renderer* r, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy)
|
|
||||||
{ Create(r, w, h, cx, cy); }
|
|
||||||
SpriteSheet(SDL_Texture* p, Uint16 cx, Uint16 cy)
|
|
||||||
{ SetTexture(p, cx, cy); }
|
|
||||||
~SpriteSheet() = default;
|
|
||||||
|
|
||||||
SpriteSheet& operator=(SpriteSheet const&);
|
|
||||||
SpriteSheet& operator=(SpriteSheet&&);
|
|
||||||
|
|
||||||
void Update(double delta);
|
|
||||||
|
|
||||||
SDL_Texture* Load(SDL_Renderer*, std::string fname, Uint16 cx, Uint16 cy);
|
|
||||||
SDL_Texture* Create(SDL_Renderer*, Uint16 w, Uint16 h, Uint16 cx, Uint16 cy);
|
|
||||||
SDL_Texture* SetTexture(SDL_Texture*, Uint16 cx, Uint16 cy);
|
|
||||||
void Free() override;
|
|
||||||
|
|
||||||
Uint16 SetCountX(Uint16);
|
|
||||||
Uint16 SetCountY(Uint16);
|
|
||||||
Uint16 SetIndexX(Uint16);
|
|
||||||
Uint16 SetIndexY(Uint16);
|
|
||||||
|
|
||||||
Uint16 GetCountX() const { return countX; }
|
|
||||||
Uint16 GetCountY() const { return countY; }
|
|
||||||
Uint16 GetIndexX() const { return indexX; }
|
|
||||||
Uint16 GetIndexY() const { return indexY; }
|
|
||||||
|
|
||||||
double SetDelay(double d);
|
|
||||||
double GetDelay() const { return delay; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
Uint16 countX = 0, countY = 0, indexX = 0, indexY = 0;
|
|
||||||
double delay = 0.0, tick = 0.0;
|
|
||||||
|
|
||||||
//disable access
|
|
||||||
using Image::Load;
|
|
||||||
using Image::Create;
|
|
||||||
using Image::SetTexture;
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "button.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
void Button::Setup(Sint16 i, Sint16 j, SDL_Surface* bg, SDL_Surface* fg, std::string t) {
|
||||||
|
x = i;
|
||||||
|
y = j;
|
||||||
|
SetSurfaces(bg, fg);
|
||||||
|
SetText(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
|
return CalcState(motion.x, motion.y, motion.state & SDL_BUTTON_LMASK);
|
||||||
|
}
|
||||||
|
|
||||||
|
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||||
|
if (button.button == SDL_BUTTON_LEFT) {
|
||||||
|
return CalcState(button.x, button.y, true);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
|
if (button.button == SDL_BUTTON_LEFT) {
|
||||||
|
return CalcState(button.x, button.y, false);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Button::DrawTo(SDL_Surface* const dest) {
|
||||||
|
image.DrawTo(dest, x, y);
|
||||||
|
font.DrawStringTo(text, dest, textX + x, textY + y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Button::SetSurfaces(SDL_Surface* bg, SDL_Surface* fg) {
|
||||||
|
//graphical stuff
|
||||||
|
image.SetSurface(bg);
|
||||||
|
image.SetClipH(image.GetClipH() / 3); //3 phases, vertical storage
|
||||||
|
font.SetSurface(fg);
|
||||||
|
|
||||||
|
//reset textX & textY
|
||||||
|
SetText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Button::SetText(std::string t) {
|
||||||
|
//one line, cache the position
|
||||||
|
text = t;
|
||||||
|
textX = (image.GetClipW() / 2) - (font.GetCharW() * text.size() / 2);
|
||||||
|
textY = (image.GetClipH() / 2) - (font.GetCharH() / 2);
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) {
|
||||||
|
if (i < x || i > (x + image.GetClipW()) ||
|
||||||
|
j < y || j > (y + image.GetClipH())
|
||||||
|
) {
|
||||||
|
image.SetClipY(0);
|
||||||
|
return state = State::NORMAL;
|
||||||
|
}
|
||||||
|
if (leftPressed) {
|
||||||
|
image.SetClipY(image.GetClipH()*2);
|
||||||
|
return state = State::PRESSED;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
image.SetClipY(image.GetClipH());
|
||||||
|
return state = State::HOVER;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/* 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 BUTTON_HPP_
|
||||||
|
#define BUTTON_HPP_
|
||||||
|
|
||||||
|
#include "image.hpp"
|
||||||
|
#include "raster_font.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
//3-phases, no toggle, centred text
|
||||||
|
class Button {
|
||||||
|
public:
|
||||||
|
enum class State {
|
||||||
|
NORMAL, HOVER, PRESSED
|
||||||
|
};
|
||||||
|
|
||||||
|
Button() = default;
|
||||||
|
Button(Sint16 x, Sint16 y, SDL_Surface* bg, SDL_Surface* fg, std::string t = "") { Setup(x, y, bg, fg, t); }
|
||||||
|
|
||||||
|
void Setup(Sint16 x, Sint16 y, SDL_Surface* bg, SDL_Surface* fg, std::string text = "");
|
||||||
|
|
||||||
|
//return the current state
|
||||||
|
State MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
|
State MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
|
State MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
|
State GetState() const { return state; }
|
||||||
|
|
||||||
|
//yet another draw function
|
||||||
|
void DrawTo(SDL_Surface* const);
|
||||||
|
|
||||||
|
//simple accessors and mutators
|
||||||
|
Sint16 SetX(Sint16 i) { return x = i; }
|
||||||
|
Sint16 SetY(Sint16 i) { return y = i; }
|
||||||
|
Sint16 GetX() const { return x; }
|
||||||
|
Sint16 GetY() const { return y; }
|
||||||
|
|
||||||
|
void SetSurfaces(SDL_Surface* bg, SDL_Surface* fg);
|
||||||
|
|
||||||
|
std::string SetText(std::string t);
|
||||||
|
std::string GetText() const { return text; }
|
||||||
|
|
||||||
|
//raw access, be careful
|
||||||
|
Image* GetImage() { return ℑ }
|
||||||
|
RasterFont* GetFont() { return &font; }
|
||||||
|
|
||||||
|
//debug
|
||||||
|
Sint16 GetTextX() const { return textX; }
|
||||||
|
Sint16 GetTextY() const { return textY; }
|
||||||
|
private:
|
||||||
|
State CalcState(Sint16 x, Sint16 y, bool leftPressed);
|
||||||
|
|
||||||
|
Sint16 x = 0, y = 0;
|
||||||
|
Sint16 textX = 0, textY = 0; //prevent recalc every loop
|
||||||
|
Image image;
|
||||||
|
RasterFont font;
|
||||||
|
State state = State::NORMAL;
|
||||||
|
std::string text;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void ConfigUtility::Load(string fname) {
|
||||||
|
ifstream is(fname);
|
||||||
|
|
||||||
|
if (!is.is_open()) {
|
||||||
|
throw(runtime_error("Failed to open config file"));
|
||||||
|
}
|
||||||
|
|
||||||
|
string key, val;
|
||||||
|
|
||||||
|
for (;;) { //forever
|
||||||
|
//eat whitespace
|
||||||
|
while(isspace(is.peek()))
|
||||||
|
is.ignore();
|
||||||
|
|
||||||
|
//end of file
|
||||||
|
if (is.eof())
|
||||||
|
break;
|
||||||
|
|
||||||
|
//skip comment lines
|
||||||
|
if (is.peek() == '#') {
|
||||||
|
while(is.peek() != '\n' && !is.eof()) {
|
||||||
|
is.ignore();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//read in the pair
|
||||||
|
getline(is, key,'=');
|
||||||
|
getline(is, val);
|
||||||
|
|
||||||
|
//trim the strings at the start & end
|
||||||
|
while(key.size() && isspace(*key.begin())) key.erase(0, 1);
|
||||||
|
while(val.size() && isspace(*val.begin())) val.erase(0, 1);
|
||||||
|
|
||||||
|
while(key.size() && isspace(*(key.end()-1))) key.erase(key.end() - 1);
|
||||||
|
while(val.size() && isspace(*(val.end()-1))) val.erase(val.end() - 1);
|
||||||
|
|
||||||
|
//allow empty/wiped values
|
||||||
|
if (key.size() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//save the pair
|
||||||
|
table[key] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,41 +19,50 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef CONFIGUTILITY_HPP_
|
||||||
|
#define CONFIGUTILITY_HPP_
|
||||||
|
|
||||||
#include "bounding_box.hpp"
|
#include <map>
|
||||||
#include "entity.hpp"
|
|
||||||
#include "vector2.hpp"
|
|
||||||
|
|
||||||
#include "lua.hpp"
|
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
class TriggerData {
|
class ConfigUtility {
|
||||||
public:
|
public:
|
||||||
TriggerData() = default;
|
ConfigUtility() {}
|
||||||
~TriggerData() = default;
|
ConfigUtility(std::string s) { Load(s); }
|
||||||
|
|
||||||
std::string SetHandle(std::string);
|
void Load(std::string fname);
|
||||||
std::string GetHandle() const;
|
|
||||||
|
|
||||||
Vector2 SetOrigin(Vector2 v);
|
std::string String(std::string s) {
|
||||||
Vector2 GetOrigin();
|
return table[s];
|
||||||
|
}
|
||||||
BoundingBox SetBoundingBox(BoundingBox b);
|
const char* CString(std::string s) {
|
||||||
BoundingBox GetBoundingBox();
|
return table[s].c_str();
|
||||||
|
}
|
||||||
int SetScriptReference(int i);
|
int Integer(std::string s) {
|
||||||
int GetScriptReference();
|
return atoi(table[s].c_str());
|
||||||
|
}
|
||||||
//NOTE: exclusion list is currently used for entities that have already "fired" the trigger
|
double Double(std::string s) {
|
||||||
std::list<Entity*>* GetExclusionList();
|
return atof(table[s].c_str());
|
||||||
|
}
|
||||||
|
bool Boolean(std::string s) {
|
||||||
|
return table[s] == "true";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string& operator[](std::string s) {
|
||||||
|
return table[s];
|
||||||
|
}
|
||||||
|
int Int(std::string s) {
|
||||||
|
return Integer(s);
|
||||||
|
}
|
||||||
|
int Bool(std::string s) {
|
||||||
|
return Boolean(s);
|
||||||
|
}
|
||||||
|
std::map<std::string, std::string>* GetMap() {
|
||||||
|
return &table;
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
std::string handle;
|
std::map<std::string, std::string> table;
|
||||||
Vector2 origin;
|
|
||||||
BoundingBox bounds;
|
|
||||||
int scriptRef = LUA_NOREF;
|
|
||||||
std::list<Entity*> exclusionList;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
class FrameRate {
|
|
||||||
public:
|
|
||||||
typedef std::chrono::high_resolution_clock Clock;
|
|
||||||
|
|
||||||
FrameRate() = default;
|
|
||||||
|
|
||||||
//DOCS: for usage purposes, this returns -1 unless a new framerate value is set
|
|
||||||
int Calculate() {
|
|
||||||
frameCount++;
|
|
||||||
if (Clock::now() - tick >= std::chrono::duration<int>(1)) {
|
|
||||||
lastFrameRate = frameCount;
|
|
||||||
frameCount = 0;
|
|
||||||
tick = Clock::now();
|
|
||||||
return lastFrameRate;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetFrameRate() { return lastFrameRate; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
int frameCount = 0;
|
|
||||||
int lastFrameRate = 0;
|
|
||||||
Clock::time_point tick = Clock::now();
|
|
||||||
};
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=.
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#targets
|
|
||||||
all: $(OBJ) $(OUT)
|
|
||||||
ar -crs $(OUT) $(OBJ)
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUT): | $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR):
|
|
||||||
mkdir $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUTDIR):
|
|
||||||
mkdir $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "timer.hpp"
|
|
||||||
|
|
||||||
Timer::Timer(): start(Timer::Clock::now()) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
Timer::Timer(std::string s): name(s), start(Timer::Clock::now()) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void Timer::Start() {
|
|
||||||
start = Clock::now();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Timer::Stop() {
|
|
||||||
timeSpan = Clock::now() - start;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, Timer& t) {
|
|
||||||
os << t.GetName() << ": ";
|
|
||||||
os << std::chrono::duration_cast<std::chrono::microseconds>(t.GetTime()).count();
|
|
||||||
os << "us";
|
|
||||||
return os;
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <string>
|
|
||||||
#include <ostream>
|
|
||||||
|
|
||||||
class Timer {
|
|
||||||
public:
|
|
||||||
typedef std::chrono::high_resolution_clock Clock;
|
|
||||||
|
|
||||||
Timer();
|
|
||||||
Timer(std::string s);
|
|
||||||
~Timer() = default;
|
|
||||||
|
|
||||||
void Start();
|
|
||||||
void Stop();
|
|
||||||
|
|
||||||
//accessors and mutators
|
|
||||||
Clock::duration GetTime() { return timeSpan; }
|
|
||||||
|
|
||||||
std::string SetName(std::string s) { return name = s; }
|
|
||||||
std::string GetName() { return name; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string name;
|
|
||||||
Clock::time_point start;
|
|
||||||
Clock::duration timeSpan;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, Timer& t);
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,8 +19,16 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef DEFINES_HPP_
|
||||||
|
#define DEFINES_HPP_
|
||||||
|
|
||||||
enum Channels {
|
#define GAME_CHANNEL 0
|
||||||
SERVER = 0
|
#define CHAT_CHANNEL 1
|
||||||
|
|
||||||
|
#define WALKING_SPEED 140
|
||||||
|
|
||||||
|
enum class CardinalDirection {
|
||||||
|
NORTH, SOUTH, EAST, WEST
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,24 +19,26 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#include "frame_rate.hpp"
|
||||||
|
|
||||||
#include "server_packet.hpp"
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
class HeartbeatUtility {
|
typedef std::chrono::high_resolution_clock Clock;
|
||||||
public:
|
|
||||||
//heartbeat system
|
|
||||||
void hPing(ServerPacket* const);
|
|
||||||
void hPong(ServerPacket* const);
|
|
||||||
|
|
||||||
int CheckHeartBeat();
|
static int frameCount = 0;
|
||||||
|
static int lastFrameRate = 0;
|
||||||
|
static Clock::time_point tick = Clock::now();
|
||||||
|
|
||||||
private:
|
int clockFrameRate() {
|
||||||
UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton();
|
frameCount++;
|
||||||
typedef std::chrono::steady_clock Clock;
|
if (Clock::now() - tick >= std::chrono::duration<int>(1)) {
|
||||||
Clock::time_point lastBeat = Clock::now();
|
lastFrameRate = frameCount;
|
||||||
int attemptedBeats = 0;
|
frameCount = 0;
|
||||||
};
|
tick = Clock::now();
|
||||||
|
}
|
||||||
|
return lastFrameRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getFrameRate() {
|
||||||
|
return lastFrameRate;
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,8 +19,10 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef FRAMERATE_HPP_
|
||||||
|
#define FRAMERATE_HPP_
|
||||||
|
|
||||||
#include <string>
|
int clockFrameRate();
|
||||||
|
int getFrameRate();
|
||||||
|
|
||||||
int getFileHash(std::string fname);
|
#endif
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
//the bounds for the objects, mapped to the default sprites
|
|
||||||
constexpr int BARRIER_BOUNDS_X = 0;
|
|
||||||
constexpr int BARRIER_BOUNDS_Y = 0;
|
|
||||||
constexpr int BARRIER_BOUNDS_WIDTH = 96;
|
|
||||||
constexpr int BARRIER_BOUNDS_HEIGHT = 96;
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
//the speeds that the characters move
|
|
||||||
constexpr double CHARACTER_WALKING_SPEED = 2.24;
|
|
||||||
constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0);
|
|
||||||
constexpr double CHARACTER_WALKING_NEGATIVE_MOD = 1.0 - CHARACTER_WALKING_MOD;
|
|
||||||
|
|
||||||
//the bounds for the character objects, mapped to the default sprites
|
|
||||||
constexpr int CHARACTER_BOUNDS_X = 0;
|
|
||||||
constexpr int CHARACTER_BOUNDS_Y = 16;
|
|
||||||
constexpr int CHARACTER_BOUNDS_WIDTH = 32;
|
|
||||||
constexpr int CHARACTER_BOUNDS_HEIGHT = 32;
|
|
||||||
|
|
||||||
//the character's sprite format
|
|
||||||
constexpr int CHARACTER_CELLS_X = 4;
|
|
||||||
constexpr int CHARACTER_CELLS_Y = 4;
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
//the speeds that the characters move
|
|
||||||
constexpr double CREATURE_WALKING_SPEED = 1.0;
|
|
||||||
constexpr double CREATURE_WALKING_MOD = 1.0/sqrt(2.0);
|
|
||||||
constexpr double CREATURE_WALKING_NEGATIVE_MOD = 1.0 - CREATURE_WALKING_MOD;
|
|
||||||
|
|
||||||
//the bounds for the character objects, mapped to the default sprites
|
|
||||||
constexpr int CREATURE_BOUNDS_X = 0;
|
|
||||||
constexpr int CREATURE_BOUNDS_Y = 0;
|
|
||||||
constexpr int CREATURE_BOUNDS_WIDTH = 32;
|
|
||||||
constexpr int CREATURE_BOUNDS_HEIGHT = 32;
|
|
||||||
|
|
||||||
//the character's sprite format
|
|
||||||
constexpr int CREATURE_CELLS_X = 4;
|
|
||||||
constexpr int CREATURE_CELLS_Y = 4;
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
constexpr int INFLUENCE_RADIUS = 1000;
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
enum ItemType {
|
|
||||||
//basics
|
|
||||||
POTION = 101,
|
|
||||||
|
|
||||||
//weapons
|
|
||||||
SWORD = 201,
|
|
||||||
DAGGER = 202,
|
|
||||||
STAFF = 203
|
|
||||||
};
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=.
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#targets
|
|
||||||
all: $(OBJ) $(OUT)
|
|
||||||
ar -crs $(OUT) $(OBJ)
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUT): | $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR):
|
|
||||||
mkdir $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUTDIR):
|
|
||||||
mkdir $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "image.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
SDL_Surface* Image::LoadSurface(std::string fname) {
|
||||||
|
SDL_Surface* p = SDL_LoadBMP(fname.c_str());
|
||||||
|
if (!p) {
|
||||||
|
throw(std::runtime_error(std::string() + "Failed to load file: " + fname));
|
||||||
|
}
|
||||||
|
surface = p;
|
||||||
|
SetTransparentColor(255, 0, 255); //default
|
||||||
|
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||||
|
local = true;
|
||||||
|
return surface;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::FreeSurface() {
|
||||||
|
if (local) {
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
local = false;
|
||||||
|
}
|
||||||
|
surface = nullptr;
|
||||||
|
clip = {0, 0, 0, 0};
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Surface* Image::SetSurface(SDL_Surface* p) {
|
||||||
|
if (!p) {
|
||||||
|
throw(std::invalid_argument("No surface pointer provided"));
|
||||||
|
}
|
||||||
|
surface = p;
|
||||||
|
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||||
|
local = false;
|
||||||
|
return surface;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) {
|
||||||
|
if (!surface) {
|
||||||
|
throw(std::logic_error("No image surface to draw"));
|
||||||
|
}
|
||||||
|
SDL_Rect sclip = clip, dclip = {x,y};
|
||||||
|
SDL_BlitSurface(surface, &sclip, dest, &dclip);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::SetTransparentColor(Uint8 r, Uint8 g, Uint8 b) {
|
||||||
|
if (!surface) {
|
||||||
|
throw(std::logic_error("Failed to set the transparent color"));
|
||||||
|
}
|
||||||
|
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::ClearTransparentColor() {
|
||||||
|
if (!surface) {
|
||||||
|
throw(std::logic_error("Failed to clear the transparent color"));
|
||||||
|
}
|
||||||
|
SDL_SetColorKey(surface, 0, 0);
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
/* 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 IMAGE_HPP_
|
||||||
|
#define IMAGE_HPP_
|
||||||
|
|
||||||
|
#include "SDL/SDL.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Image {
|
||||||
|
public:
|
||||||
|
Image() = default;
|
||||||
|
Image(SDL_Surface* p) { SetSurface(p); }
|
||||||
|
~Image() { FreeSurface(); }
|
||||||
|
|
||||||
|
SDL_Surface* LoadSurface(std::string fname);
|
||||||
|
void FreeSurface();
|
||||||
|
|
||||||
|
SDL_Surface* SetSurface(SDL_Surface*);
|
||||||
|
SDL_Surface* GetSurface() const { return surface; }
|
||||||
|
|
||||||
|
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
||||||
|
|
||||||
|
//Clip handlers
|
||||||
|
SDL_Rect SetClip(SDL_Rect r) { return clip = r; }
|
||||||
|
SDL_Rect GetClip() const { return clip; }
|
||||||
|
|
||||||
|
Sint16 SetClipX(Sint16 x) { return clip.x = x; }
|
||||||
|
Sint16 SetClipY(Sint16 y) { return clip.y = y; }
|
||||||
|
Uint16 SetClipW(Uint16 w) { return clip.w = w; }
|
||||||
|
Uint16 SetClipH(Uint16 h) { return clip.h = h; }
|
||||||
|
|
||||||
|
Sint16 GetClipX() const { return clip.x; }
|
||||||
|
Sint16 GetClipY() const { return clip.y; }
|
||||||
|
Uint16 GetClipW() const { return clip.w; }
|
||||||
|
Uint16 GetClipH() const { return clip.h; }
|
||||||
|
|
||||||
|
bool GetLocal() { return local; }
|
||||||
|
|
||||||
|
void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b);
|
||||||
|
void ClearTransparentColor();
|
||||||
|
protected:
|
||||||
|
SDL_Surface* surface = nullptr;
|
||||||
|
SDL_Rect clip = {0, 0, 0, 0};
|
||||||
|
bool local = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
+27
-15
@@ -1,26 +1,38 @@
|
|||||||
|
#config
|
||||||
|
LOCALLIBS=
|
||||||
|
LIB=
|
||||||
|
INCLUDES=
|
||||||
|
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||||
|
|
||||||
|
#source
|
||||||
|
SRC=$(wildcard *.cpp)
|
||||||
|
|
||||||
|
#objects
|
||||||
|
OBJDIR=obj
|
||||||
|
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||||
|
|
||||||
#output
|
#output
|
||||||
export OUTDIR=../..
|
OUTDIR=../lib
|
||||||
export OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
OUT=$(addprefix $(OUTDIR)/,libCommon.a)
|
||||||
|
|
||||||
all: $(OUTDIR)
|
#targets
|
||||||
$(MAKE) -C debugging
|
all: $(OBJ) $(OUT)
|
||||||
$(MAKE) -C global_defines
|
ar -crs $(OUT) $(OBJ)
|
||||||
$(MAKE) -C network
|
|
||||||
$(MAKE) -C utilities
|
|
||||||
|
|
||||||
debug: export CXXFLAGS+=-g
|
$(OBJ): | $(OBJDIR)
|
||||||
debug: clean all
|
|
||||||
|
$(OUT): | $(OUTDIR)
|
||||||
|
|
||||||
|
$(OBJDIR):
|
||||||
|
mkdir $(OBJDIR)
|
||||||
|
|
||||||
$(OUTDIR):
|
$(OUTDIR):
|
||||||
mkdir $(OUTDIR)
|
mkdir $(OUTDIR)
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: %.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
|
||||||
$(RM) *.o *.a *.exe
|
$(RM) *.o *.a *.exe
|
||||||
else ifeq ($(shell uname), Linux)
|
|
||||||
find . -type f -name *.o -exec rm -f -r -v {} \;
|
|
||||||
find . -type f -name *.a -exec rm -f -r -v {} \;
|
|
||||||
rm -f -v out/client out/server
|
|
||||||
endif
|
|
||||||
|
|
||||||
rebuild: clean all
|
rebuild: clean all
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. packet_types ../global_defines ../utilities ../../TurtleGUI ../../TurtleMap
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#targets
|
|
||||||
all: $(OBJ) $(OUT)
|
|
||||||
ar -crs $(OUT) $(OBJ)
|
|
||||||
$(MAKE) -C packet_types
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUT): | $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR):
|
|
||||||
mkdir $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUTDIR):
|
|
||||||
mkdir $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "barrier_packet.hpp"
|
|
||||||
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
void serializeBarrier(void* buffer, BarrierPacket* packet) {
|
|
||||||
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the barrier
|
|
||||||
serialCopy(&buffer, &packet->barrierIndex, sizeof(int));
|
|
||||||
|
|
||||||
//bounds
|
|
||||||
serialCopy(&buffer, &packet->bounds.x, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.y, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.w, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.h, sizeof(int));
|
|
||||||
|
|
||||||
|
|
||||||
//location
|
|
||||||
serialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->motion.x, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->motion.y, sizeof(double));
|
|
||||||
|
|
||||||
//graphical data
|
|
||||||
serialCopy(&buffer, packet->status, sizeof(int) * 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeBarrier(void* buffer, BarrierPacket* packet) {
|
|
||||||
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the barrier
|
|
||||||
deserialCopy(&buffer, &packet->barrierIndex, sizeof(int));
|
|
||||||
|
|
||||||
//bounds
|
|
||||||
deserialCopy(&buffer, &packet->bounds.x, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.y, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.w, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.h, sizeof(int));
|
|
||||||
|
|
||||||
|
|
||||||
//location
|
|
||||||
deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->motion.x, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->motion.y, sizeof(double));
|
|
||||||
|
|
||||||
//graphical data
|
|
||||||
deserialCopy(&buffer, packet->status, sizeof(int) * 8);
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "bounding_box.hpp"
|
|
||||||
#include "vector2.hpp"
|
|
||||||
|
|
||||||
struct BarrierPacket : SerialPacketBase {
|
|
||||||
//identify the barrier
|
|
||||||
int barrierIndex;
|
|
||||||
BoundingBox bounds;
|
|
||||||
|
|
||||||
//location
|
|
||||||
int roomIndex;
|
|
||||||
Vector2 origin;
|
|
||||||
Vector2 motion;
|
|
||||||
|
|
||||||
//graphical data: 0 blank, 1 green, 2 red
|
|
||||||
int status[8];
|
|
||||||
};
|
|
||||||
|
|
||||||
void serializeBarrier(void* buffer, BarrierPacket* packet);
|
|
||||||
void deserializeBarrier(void* buffer, BarrierPacket* packet);
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "character_packet.hpp"
|
|
||||||
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
void serializeCharacter(void* buffer, CharacterPacket* packet) {
|
|
||||||
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the character
|
|
||||||
serialCopy(&buffer, &packet->characterIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
|
|
||||||
serialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//the owner
|
|
||||||
serialCopy(&buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
|
|
||||||
//location
|
|
||||||
serialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
|
|
||||||
serialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
|
|
||||||
serialCopy(&buffer, &packet->motion.x, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->motion.y, sizeof(double));
|
|
||||||
|
|
||||||
serialCopy(&buffer, &packet->bounds.x, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.y, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.w, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.h, sizeof(int));
|
|
||||||
|
|
||||||
//gameplay components: equipment, items, buffs, debuffs...
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeCharacter(void* buffer, CharacterPacket* packet) {
|
|
||||||
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the character
|
|
||||||
deserialCopy(&buffer, &packet->characterIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
|
|
||||||
deserialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//the owner
|
|
||||||
deserialCopy(&buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
|
|
||||||
//location
|
|
||||||
deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
|
|
||||||
deserialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
|
|
||||||
deserialCopy(&buffer, &packet->motion.x, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->motion.y, sizeof(double));
|
|
||||||
|
|
||||||
deserialCopy(&buffer, &packet->bounds.x, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.y, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.w, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.h, sizeof(int));
|
|
||||||
|
|
||||||
//gameplay components: equipment, items, buffs, debuffs...
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "bounding_box.hpp"
|
|
||||||
#include "vector2.hpp"
|
|
||||||
|
|
||||||
struct CharacterPacket : SerialPacketBase {
|
|
||||||
//identify the character
|
|
||||||
int characterIndex;
|
|
||||||
char handle[PACKET_STRING_SIZE];
|
|
||||||
char avatar[PACKET_STRING_SIZE];
|
|
||||||
|
|
||||||
//the owner
|
|
||||||
int accountIndex;
|
|
||||||
|
|
||||||
//location
|
|
||||||
int roomIndex;
|
|
||||||
Vector2 origin;
|
|
||||||
Vector2 motion;
|
|
||||||
BoundingBox bounds;
|
|
||||||
};
|
|
||||||
|
|
||||||
void serializeCharacter(void* buffer, CharacterPacket* packet);
|
|
||||||
void deserializeCharacter(void* buffer, CharacterPacket* packet);
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "client_packet.hpp"
|
|
||||||
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
void serializeClient(void* buffer, ClientPacket* packet) {
|
|
||||||
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
serialCopy(&buffer, &packet->clientIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, packet->username, PACKET_STRING_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeClient(void* buffer, ClientPacket* packet) {
|
|
||||||
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
deserialCopy(&buffer, &packet->clientIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, packet->username, PACKET_STRING_SIZE);
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
struct ClientPacket : SerialPacketBase {
|
|
||||||
int clientIndex;
|
|
||||||
int accountIndex;
|
|
||||||
char username[PACKET_STRING_SIZE];
|
|
||||||
//TODO: (3) password, auth token
|
|
||||||
};
|
|
||||||
|
|
||||||
void serializeClient(void* buffer, ClientPacket* packet);
|
|
||||||
void deserializeClient(void* buffer, ClientPacket* packet);
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "creature_packet.hpp"
|
|
||||||
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
void serializeCreature(void* buffer, CreaturePacket* packet) {
|
|
||||||
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the creature
|
|
||||||
serialCopy(&buffer, &packet->creatureIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
|
|
||||||
serialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//bounds
|
|
||||||
serialCopy(&buffer, &packet->bounds.x, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.y, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.w, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->bounds.h, sizeof(int));
|
|
||||||
|
|
||||||
|
|
||||||
//location
|
|
||||||
serialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->motion.x, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->motion.y, sizeof(double));
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeCreature(void* buffer, CreaturePacket* packet) {
|
|
||||||
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the creature
|
|
||||||
deserialCopy(&buffer, &packet->creatureIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, packet->handle, PACKET_STRING_SIZE);
|
|
||||||
deserialCopy(&buffer, packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//bounds
|
|
||||||
deserialCopy(&buffer, &packet->bounds.x, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.y, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.w, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->bounds.h, sizeof(int));
|
|
||||||
|
|
||||||
|
|
||||||
//location
|
|
||||||
deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->motion.x, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->motion.y, sizeof(double));
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "bounding_box.hpp"
|
|
||||||
#include "vector2.hpp"
|
|
||||||
|
|
||||||
struct CreaturePacket : SerialPacketBase {
|
|
||||||
//identify the creature
|
|
||||||
int creatureIndex;
|
|
||||||
char handle[PACKET_STRING_SIZE];
|
|
||||||
char avatar[PACKET_STRING_SIZE];
|
|
||||||
BoundingBox bounds;
|
|
||||||
|
|
||||||
//location
|
|
||||||
int roomIndex;
|
|
||||||
Vector2 origin;
|
|
||||||
Vector2 motion;
|
|
||||||
};
|
|
||||||
|
|
||||||
void serializeCreature(void* buffer, CreaturePacket* packet);
|
|
||||||
void deserializeCreature(void* buffer, CreaturePacket* packet);
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. .. ../../global_defines ../../utilities ../../../TurtleGUI ../../../TurtleMap
|
|
||||||
LIBS+=
|
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
|
||||||
|
|
||||||
#source
|
|
||||||
CXXSRC=$(wildcard *.cpp)
|
|
||||||
|
|
||||||
#objects
|
|
||||||
OBJDIR=obj
|
|
||||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
|
||||||
|
|
||||||
#targets
|
|
||||||
all: $(OBJ) $(OUT)
|
|
||||||
ar -crs ../$(OUT) $(OBJ)
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUT): | $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR):
|
|
||||||
mkdir $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUTDIR):
|
|
||||||
mkdir $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "region_packet.hpp"
|
|
||||||
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
void serializeRegion(void* buffer, RegionPacket* packet) {
|
|
||||||
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//format
|
|
||||||
serialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->x, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->y, sizeof(int));
|
|
||||||
|
|
||||||
if (packet->type != SerialPacketType::REGION_CONTENT) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//tiles
|
|
||||||
for (int i = 0; i < REGION_WIDTH; i++) {
|
|
||||||
for (int j = 0; j < REGION_HEIGHT; j++) {
|
|
||||||
for (int k = 0; k < REGION_DEPTH; k++) {
|
|
||||||
*reinterpret_cast<Region::type_t*>(buffer) = packet->region->GetTile(i, j, k);
|
|
||||||
buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//solids
|
|
||||||
serialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeRegion(void* buffer, RegionPacket* packet) {
|
|
||||||
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//format
|
|
||||||
deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->x, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->y, sizeof(int));
|
|
||||||
|
|
||||||
if (packet->type != SerialPacketType::REGION_CONTENT) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//an object to work on
|
|
||||||
packet->region = new Region(packet->x, packet->y);
|
|
||||||
|
|
||||||
//tiles
|
|
||||||
for (int i = 0; i < REGION_WIDTH; i++) {
|
|
||||||
for (int j = 0; j < REGION_HEIGHT; j++) {
|
|
||||||
for (int k = 0; k < REGION_DEPTH; k++) {
|
|
||||||
packet->region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer));
|
|
||||||
buffer = reinterpret_cast<char*>(buffer) + sizeof(Region::type_t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//solids
|
|
||||||
deserialCopy(&buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
//define the memory footprint for the region's members
|
|
||||||
constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH;
|
|
||||||
constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0);
|
|
||||||
constexpr int REGION_METADATA_FOOTPRINT = sizeof(int) * 3;
|
|
||||||
|
|
||||||
struct RegionPacket : SerialPacketBase {
|
|
||||||
//location/identify the region
|
|
||||||
int roomIndex;
|
|
||||||
int x, y;
|
|
||||||
|
|
||||||
//the data
|
|
||||||
Region* region;
|
|
||||||
};
|
|
||||||
|
|
||||||
void serializeRegion(void* buffer, RegionPacket* packet);
|
|
||||||
void deserializeRegion(void* buffer, RegionPacket* packet);
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
//sanity check
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_type.hpp"
|
|
||||||
|
|
||||||
#include "SDL2/SDL_net.h"
|
|
||||||
|
|
||||||
constexpr int PACKET_STRING_SIZE = 100;
|
|
||||||
|
|
||||||
struct SerialPacketBase {
|
|
||||||
//members
|
|
||||||
SerialPacketType type;
|
|
||||||
IPaddress srcAddress;
|
|
||||||
|
|
||||||
virtual ~SerialPacketBase() {};
|
|
||||||
};
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "server_packet.hpp"
|
|
||||||
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
void serializeServer(void* buffer, ServerPacket* packet) {
|
|
||||||
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the server
|
|
||||||
serialCopy(&buffer, &packet->version, sizeof(int));
|
|
||||||
serialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
|
|
||||||
serialCopy(&buffer, &packet->playerCount, sizeof(int));
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeServer(void* buffer, ServerPacket* packet) {
|
|
||||||
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the server
|
|
||||||
deserialCopy(&buffer, &packet->version, sizeof(int));
|
|
||||||
deserialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
|
|
||||||
deserialCopy(&buffer, &packet->playerCount, sizeof(int));
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
struct ServerPacket : SerialPacketBase {
|
|
||||||
//identify the server
|
|
||||||
char name[PACKET_STRING_SIZE];
|
|
||||||
int playerCount;
|
|
||||||
int version;
|
|
||||||
};
|
|
||||||
|
|
||||||
void serializeServer(void* buffer, ServerPacket* packet);
|
|
||||||
void deserializeServer(void* buffer, ServerPacket* packet);
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "text_packet.hpp"
|
|
||||||
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
void serializeText(void* buffer, TextPacket* packet) {
|
|
||||||
serialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//content
|
|
||||||
serialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
|
|
||||||
serialCopy(&buffer, packet->text, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//location
|
|
||||||
serialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
serialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
serialCopy(&buffer, &packet->range, sizeof(int));
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeText(void* buffer, TextPacket* packet) {
|
|
||||||
deserialCopy(&buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//content
|
|
||||||
deserialCopy(&buffer, packet->name, PACKET_STRING_SIZE);
|
|
||||||
deserialCopy(&buffer, packet->text, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//location
|
|
||||||
deserialCopy(&buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
deserialCopy(&buffer, &packet->origin.x, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->origin.y, sizeof(double));
|
|
||||||
deserialCopy(&buffer, &packet->range, sizeof(int));
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "vector2.hpp"
|
|
||||||
|
|
||||||
struct TextPacket : SerialPacketBase {
|
|
||||||
char name[PACKET_STRING_SIZE];
|
|
||||||
char text[PACKET_STRING_SIZE];
|
|
||||||
int roomIndex;
|
|
||||||
Vector2 origin;
|
|
||||||
int range;
|
|
||||||
};
|
|
||||||
|
|
||||||
void serializeText(void* buffer, TextPacket* packet);
|
|
||||||
void deserializeText(void* buffer, TextPacket* packet);
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
#include "barrier_packet.hpp"
|
|
||||||
#include "character_packet.hpp"
|
|
||||||
#include "client_packet.hpp"
|
|
||||||
#include "creature_packet.hpp"
|
|
||||||
#include "region_packet.hpp"
|
|
||||||
#include "server_packet.hpp"
|
|
||||||
#include "text_packet.hpp"
|
|
||||||
|
|
||||||
//SerialPacketBase is defined in serial_packet_base.hpp
|
|
||||||
typedef SerialPacketBase SerialPacket;
|
|
||||||
|
|
||||||
//DOCS: NETWORK_VERSION is used to discern compatible servers and clients
|
|
||||||
constexpr int NETWORK_VERSION = 20161209;
|
|
||||||
|
|
||||||
union MaxPacket {
|
|
||||||
BarrierPacket a;
|
|
||||||
CharacterPacket b;
|
|
||||||
ClientPacket c;
|
|
||||||
CreaturePacket d;
|
|
||||||
RegionPacket e;
|
|
||||||
ServerPacket f;
|
|
||||||
TextPacket g;
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr int MAX_PACKET_SIZE = sizeof(MaxPacket);
|
|
||||||
|
|
||||||
/* DOCS: PACKET_BUFFER_SIZE is the memory required to store serialized data
|
|
||||||
* DOCS: SerialPacketType::REGION_CONTENT is currently the largest packet type
|
|
||||||
* Serialized RegionPacket structure:
|
|
||||||
* SerialPacketType
|
|
||||||
* room index (int)
|
|
||||||
* X & Y position (int)
|
|
||||||
* tile data (3 layers)
|
|
||||||
* solid data (bitset)
|
|
||||||
*/
|
|
||||||
|
|
||||||
constexpr int PACKET_BUFFER_SIZE =
|
|
||||||
sizeof(SerialPacketType) +
|
|
||||||
REGION_METADATA_FOOTPRINT +
|
|
||||||
REGION_TILE_FOOTPRINT +
|
|
||||||
REGION_SOLID_FOOTPRINT;
|
|
||||||
@@ -1,195 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
/* DOCS: The headers indicate what packet type is used for each message
|
|
||||||
* different messages under the same header will carry different amounts of
|
|
||||||
* valid data, but it will still be carried in that packet's format.
|
|
||||||
* FORMAT_* is for internal use, deviding the different format bounds.
|
|
||||||
*/
|
|
||||||
|
|
||||||
enum class SerialPacketType {
|
|
||||||
//default: there is something wrong
|
|
||||||
NONE = 0,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//ServerPacket
|
|
||||||
// name, player count, version
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
FORMAT_SERVER = 100,
|
|
||||||
|
|
||||||
//heartbeat
|
|
||||||
PING = 101,
|
|
||||||
PONG = 102,
|
|
||||||
|
|
||||||
//Used for finding available servers
|
|
||||||
BROADCAST_REQUEST = 103,
|
|
||||||
BROADCAST_RESPONSE = 104,
|
|
||||||
|
|
||||||
FORMAT_END_SERVER = 199,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//ClientPacket
|
|
||||||
// client index, account index, username
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
FORMAT_CLIENT = 200,
|
|
||||||
|
|
||||||
//Connecting to a server as a client
|
|
||||||
JOIN_REQUEST = 201,
|
|
||||||
JOIN_RESPONSE = 202,
|
|
||||||
|
|
||||||
//disconnect from the server
|
|
||||||
DISCONNECT_REQUEST = 203,
|
|
||||||
DISCONNECT_RESPONSE = 204,
|
|
||||||
ADMIN_DISCONNECT_FORCED = 205,
|
|
||||||
|
|
||||||
//load the account
|
|
||||||
LOGIN_REQUEST = 206,
|
|
||||||
LOGIN_RESPONSE = 207,
|
|
||||||
|
|
||||||
//unload the account
|
|
||||||
LOGOUT_REQUEST = 208,
|
|
||||||
LOGOUT_RESPONSE = 209,
|
|
||||||
|
|
||||||
//shut down the server
|
|
||||||
ADMIN_SHUTDOWN_REQUEST = 210,
|
|
||||||
|
|
||||||
FORMAT_END_CLIENT = 299,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//RegionPacket
|
|
||||||
// room index, x, y, raw data
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
FORMAT_REGION = 300,
|
|
||||||
|
|
||||||
//map data
|
|
||||||
REGION_REQUEST = 301,
|
|
||||||
REGION_CONTENT = 302,
|
|
||||||
|
|
||||||
FORMAT_END_REGION = 399,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//CharacterPacket
|
|
||||||
// character index,
|
|
||||||
// handle, avatar,
|
|
||||||
// account index (owner),
|
|
||||||
// room index, origin, motion
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
FORMAT_CHARACTER = 400,
|
|
||||||
|
|
||||||
//full data update
|
|
||||||
CHARACTER_UPDATE = 401,
|
|
||||||
|
|
||||||
//character management
|
|
||||||
CHARACTER_CREATE = 402,
|
|
||||||
CHARACTER_DELETE = 403,
|
|
||||||
CHARACTER_LOAD = 404,
|
|
||||||
CHARACTER_UNLOAD = 405,
|
|
||||||
|
|
||||||
//find out info from the server
|
|
||||||
QUERY_CHARACTER_EXISTS = 406,
|
|
||||||
|
|
||||||
//actions taken
|
|
||||||
CHARACTER_MOVEMENT = 407,
|
|
||||||
|
|
||||||
FORMAT_END_CHARACTER = 499,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//CreaturePacket
|
|
||||||
// creature index,
|
|
||||||
// handle, avatar
|
|
||||||
// bounds
|
|
||||||
// room index, origin, motion
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
FORMAT_CREATURE = 500,
|
|
||||||
|
|
||||||
//full data update
|
|
||||||
CREATURE_UPDATE = 501,
|
|
||||||
|
|
||||||
//character management
|
|
||||||
CREATURE_CREATE = 502,
|
|
||||||
CREATURE_UNLOAD = 503,
|
|
||||||
|
|
||||||
//find out info from the server
|
|
||||||
QUERY_CREATURE_EXISTS = 504,
|
|
||||||
|
|
||||||
//actions taken
|
|
||||||
CREATURE_MOVEMENT = 505,
|
|
||||||
|
|
||||||
FORMAT_END_CREATURE = 599,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//TextPacket
|
|
||||||
// name, text
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
FORMAT_TEXT = 600,
|
|
||||||
|
|
||||||
//general speech
|
|
||||||
TEXT_BROADCAST = 601,
|
|
||||||
TEXT_SPEECH = 602,
|
|
||||||
TEXT_WHISPER = 603,
|
|
||||||
|
|
||||||
//rejection/error messages
|
|
||||||
JOIN_REJECTION = 604,
|
|
||||||
LOGIN_REJECTION = 605,
|
|
||||||
REGION_REJECTION = 606,
|
|
||||||
CHARACTER_REJECTION = 607,
|
|
||||||
CREATURE_REJECTION = 608,
|
|
||||||
SHUTDOWN_REJECTION = 609,
|
|
||||||
QUERY_REJECTION = 610,
|
|
||||||
|
|
||||||
FORMAT_END_TEXT = 699,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//BarrierPacket
|
|
||||||
// barrier index,
|
|
||||||
// bounds,
|
|
||||||
// roomIndex, origin, motion
|
|
||||||
// status
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
FORMAT_BARRIER = 700,
|
|
||||||
|
|
||||||
BARRIER_UPDATE = 701,
|
|
||||||
|
|
||||||
BARRIER_CREATE = 702,
|
|
||||||
BARRIER_UNLOAD = 703,
|
|
||||||
|
|
||||||
QUERY_BARRIER_EXISTS = 704,
|
|
||||||
|
|
||||||
BARRIER_ENTRY = 705,
|
|
||||||
BARRIER_EXIT = 706,
|
|
||||||
|
|
||||||
FORMAT_END_BARRIER = 799,
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//not used
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
LAST = 800
|
|
||||||
};
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
//packet types
|
|
||||||
#include "barrier_packet.hpp"
|
|
||||||
#include "character_packet.hpp"
|
|
||||||
#include "client_packet.hpp"
|
|
||||||
#include "creature_packet.hpp"
|
|
||||||
#include "region_packet.hpp"
|
|
||||||
#include "server_packet.hpp"
|
|
||||||
#include "text_packet.hpp"
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
//macros
|
|
||||||
#define BOUNDS(type, lower, upper) ((type) > (lower) && (type) < (upper))
|
|
||||||
|
|
||||||
//raw memory copy
|
|
||||||
void serialCopy(void** buffer, void* data, int size) {
|
|
||||||
memcpy(*buffer, data, size);
|
|
||||||
*buffer = reinterpret_cast<char*>(*buffer) + size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserialCopy(void** buffer, void* data, int size) {
|
|
||||||
memcpy(data, *buffer, size);
|
|
||||||
*buffer = reinterpret_cast<char*>(*buffer) + size;
|
|
||||||
}
|
|
||||||
|
|
||||||
//DOCS: The server and client MUST use the correct packet types
|
|
||||||
|
|
||||||
//main switch functions
|
|
||||||
void serializePacket(void* buffer, SerialPacketBase* packet) {
|
|
||||||
if (BOUNDS(packet->type, SerialPacketType::FORMAT_SERVER, SerialPacketType::FORMAT_END_SERVER)) {
|
|
||||||
serializeServer(buffer, static_cast<ServerPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(packet->type, SerialPacketType::FORMAT_CLIENT, SerialPacketType::FORMAT_END_CLIENT)) {
|
|
||||||
serializeClient(buffer, static_cast<ClientPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(packet->type, SerialPacketType::FORMAT_REGION, SerialPacketType::FORMAT_END_REGION)) {
|
|
||||||
serializeRegion(buffer, static_cast<RegionPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(packet->type, SerialPacketType::FORMAT_CHARACTER, SerialPacketType::FORMAT_END_CHARACTER)) {
|
|
||||||
serializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(packet->type, SerialPacketType::FORMAT_CREATURE, SerialPacketType::FORMAT_END_CREATURE)) {
|
|
||||||
serializeCreature(buffer, static_cast<CreaturePacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(packet->type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) {
|
|
||||||
serializeText(buffer, static_cast<TextPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(packet->type, SerialPacketType::FORMAT_BARRIER, SerialPacketType::FORMAT_END_BARRIER)) {
|
|
||||||
serializeBarrier(buffer, static_cast<BarrierPacket*>(packet));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializePacket(void* buffer, SerialPacketBase* packet) {
|
|
||||||
//find the type, so that you can actually deserialize the packet!
|
|
||||||
SerialPacketType type;
|
|
||||||
memcpy(&type, buffer, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
if (BOUNDS(type, SerialPacketType::FORMAT_SERVER, SerialPacketType::FORMAT_END_SERVER)) {
|
|
||||||
deserializeServer(buffer, static_cast<ServerPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(type, SerialPacketType::FORMAT_CLIENT, SerialPacketType::FORMAT_END_CLIENT)) {
|
|
||||||
deserializeClient(buffer, static_cast<ClientPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(type, SerialPacketType::FORMAT_REGION, SerialPacketType::FORMAT_END_REGION)) {
|
|
||||||
deserializeRegion(buffer, static_cast<RegionPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(type, SerialPacketType::FORMAT_CHARACTER, SerialPacketType::FORMAT_END_CHARACTER)) {
|
|
||||||
deserializeCharacter(buffer, static_cast<CharacterPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(type, SerialPacketType::FORMAT_CREATURE, SerialPacketType::FORMAT_END_CREATURE)) {
|
|
||||||
deserializeCreature(buffer, static_cast<CreaturePacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(type, SerialPacketType::FORMAT_TEXT, SerialPacketType::FORMAT_END_TEXT)) {
|
|
||||||
deserializeText(buffer, static_cast<TextPacket*>(packet));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (BOUNDS(type, SerialPacketType::FORMAT_BARRIER, SerialPacketType::FORMAT_END_BARRIER)) {
|
|
||||||
deserializeBarrier(buffer, static_cast<BarrierPacket*>(packet));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
//raw memory copy
|
|
||||||
void serialCopy(void** buffer, void* data, int size);
|
|
||||||
void deserialCopy(void** buffer, void* data, int size);
|
|
||||||
|
|
||||||
//primary functions
|
|
||||||
void serializePacket(void* buffer, SerialPacketBase* packet);
|
|
||||||
void deserializePacket(void* buffer, SerialPacketBase* packet);
|
|
||||||
@@ -1,224 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
|
||||||
*
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
#include "serial_packet.hpp"
|
|
||||||
#include "serial_utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
//DOCS: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network
|
|
||||||
//NOTE: don't confuse SerialPacketBase with UDPpacket
|
|
||||||
|
|
||||||
void UDPNetworkUtility::Open(int port) {
|
|
||||||
socket = SDLNet_UDP_Open(port);
|
|
||||||
packet = SDLNet_AllocPacket(PACKET_BUFFER_SIZE);
|
|
||||||
if (!socket || !packet) {
|
|
||||||
Close();
|
|
||||||
throw(std::runtime_error("Failed to open UDPNetworkUtility"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void UDPNetworkUtility::Close() {
|
|
||||||
SDLNet_UDP_Close(socket);
|
|
||||||
SDLNet_FreePacket(packet);
|
|
||||||
socket = nullptr;
|
|
||||||
packet = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//bind to a channel
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
int UDPNetworkUtility::Bind(const char* ip, int port, int channel) {
|
|
||||||
IPaddress add;
|
|
||||||
if (SDLNet_ResolveHost(&add, ip, port) == -1) {
|
|
||||||
throw(std::runtime_error("Failed to resolve a host"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Bind(add, channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::Bind(IPaddress add, int channel) {
|
|
||||||
int ret = SDLNet_UDP_Bind(socket, channel, &add);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
throw(std::runtime_error("Failed to bind to a channel"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UDPNetworkUtility::Unbind(int channel) {
|
|
||||||
SDLNet_UDP_Unbind(socket, channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//send a buffer
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendTo(const char* ip, int port, void* data, int len) {
|
|
||||||
IPaddress add;
|
|
||||||
if (SDLNet_ResolveHost(&add, ip, port) == -1) {
|
|
||||||
throw(std::runtime_error("Failed to resolve a host"));
|
|
||||||
}
|
|
||||||
|
|
||||||
SendTo(add, data, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendTo(IPaddress add, void* data, int len) {
|
|
||||||
if (len > packet->maxlen) {
|
|
||||||
throw(std::runtime_error("The buffer is to large for the UDPpacket"));
|
|
||||||
}
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
memcpy(packet->data, data, len);
|
|
||||||
packet->len = len;
|
|
||||||
packet->address = add;
|
|
||||||
|
|
||||||
int ret = SDLNet_UDP_Send(socket, -1, packet);
|
|
||||||
|
|
||||||
if (ret <= 0) {
|
|
||||||
throw(std::runtime_error("Failed to send a packet"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendTo(int channel, void* data, int len) {
|
|
||||||
if (len > packet->maxlen) {
|
|
||||||
throw(std::runtime_error("The buffer is to large for the UDPpacket"));
|
|
||||||
}
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
memcpy(packet->data, data, len);
|
|
||||||
packet->len = len;
|
|
||||||
|
|
||||||
int ret = SDLNet_UDP_Send(socket, channel, packet);
|
|
||||||
|
|
||||||
if (ret <= 0) {
|
|
||||||
throw(std::runtime_error("Failed to send a packet"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendToAllChannels(void* data, int len) {
|
|
||||||
if (len > packet->maxlen) {
|
|
||||||
throw(std::runtime_error("The buffer is to large for the UDPpacket"));
|
|
||||||
}
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
memcpy(packet->data, data, len);
|
|
||||||
packet->len = len;
|
|
||||||
|
|
||||||
int sent = 0;
|
|
||||||
|
|
||||||
//send to all bound channels
|
|
||||||
for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) {
|
|
||||||
if (SDLNet_UDP_GetPeerAddress(socket, i)) {
|
|
||||||
sent += SDLNet_UDP_Send(socket, i, packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sent;
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::Receive() {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
int ret = SDLNet_UDP_Recv(socket, packet);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
throw(std::runtime_error("Unknown network error occured"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//send a SerialPacketBase
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacketBase* serialPacket) {
|
|
||||||
IPaddress add;
|
|
||||||
if (SDLNet_ResolveHost(&add, ip, port) == -1) {
|
|
||||||
throw(std::runtime_error("Failed to resolve a host"));
|
|
||||||
}
|
|
||||||
|
|
||||||
SendTo(add, serialPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendTo(IPaddress add, SerialPacketBase* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
serializePacket(packet->data, serialPacket);
|
|
||||||
packet->len = PACKET_BUFFER_SIZE;
|
|
||||||
packet->address = add;
|
|
||||||
|
|
||||||
int ret = SDLNet_UDP_Send(socket, -1, packet);
|
|
||||||
|
|
||||||
if (ret <= 0) {
|
|
||||||
throw(std::runtime_error("Failed to send a packet"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendTo(int channel, SerialPacketBase* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
serializePacket(packet->data, serialPacket);
|
|
||||||
packet->len = PACKET_BUFFER_SIZE;
|
|
||||||
|
|
||||||
int ret = SDLNet_UDP_Send(socket, channel, packet);
|
|
||||||
|
|
||||||
if (ret <= 0) {
|
|
||||||
throw(std::runtime_error("Failed to send a packet"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendToAllChannels(SerialPacketBase* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
serializePacket(packet->data, serialPacket);
|
|
||||||
packet->len = PACKET_BUFFER_SIZE;
|
|
||||||
|
|
||||||
int sent = 0;
|
|
||||||
|
|
||||||
//send to all bound channels
|
|
||||||
for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) {
|
|
||||||
if (SDLNet_UDP_GetPeerAddress(socket, i)) {
|
|
||||||
sent += SDLNet_UDP_Send(socket, i, packet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sent;
|
|
||||||
}
|
|
||||||
|
|
||||||
int UDPNetworkUtility::Receive(SerialPacketBase* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
int ret = SDLNet_UDP_Recv(socket, packet);
|
|
||||||
deserializePacket(packet->data, serialPacket);
|
|
||||||
serialPacket->srcAddress = packet->address;
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
throw(std::runtime_error("Unknown network error occured"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
#include "network_queue.hpp"
|
||||||
|
#include "singleton.hpp"
|
||||||
|
|
||||||
|
#include "udp_network_utility.hpp"
|
||||||
|
|
||||||
|
#include "SDL/SDL_thread.h"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
|
static SDL_sem* lock = SDL_CreateSemaphore(1);
|
||||||
|
static SDL_Thread* queueThread = nullptr;
|
||||||
|
|
||||||
|
static std::deque<Packet> queue;
|
||||||
|
|
||||||
|
static bool running = false;
|
||||||
|
|
||||||
|
static int networkQueue(void*) {
|
||||||
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
|
while(running) {
|
||||||
|
SDL_SemWait(lock);
|
||||||
|
while(netUtil->Receive()) {
|
||||||
|
Packet p;
|
||||||
|
memcpy(&p, netUtil->GetInData(), sizeof(Packet));
|
||||||
|
p.meta.address = netUtil->GetInPacket()->address;
|
||||||
|
queue.push_back(p);
|
||||||
|
}
|
||||||
|
SDL_SemPost(lock);
|
||||||
|
SDL_Delay(10);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void beginQueueThread() {
|
||||||
|
if (running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
running = true;
|
||||||
|
if (!(queueThread = SDL_CreateThread(networkQueue, nullptr))) {
|
||||||
|
throw(std::runtime_error("Failed to create the network thread"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void endQueueThread() {
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
running = false;
|
||||||
|
SDL_WaitThread(queueThread, nullptr);
|
||||||
|
queueThread = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void killQueueThread() {
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
running = false;
|
||||||
|
SDL_KillThread(queueThread);
|
||||||
|
queueThread = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Packet peekNetworkPacket() {
|
||||||
|
SDL_SemWait(lock);
|
||||||
|
Packet p;
|
||||||
|
if (queue.size() > 0) {
|
||||||
|
Packet p = queue[0];
|
||||||
|
}
|
||||||
|
SDL_SemPost(lock);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
Packet popNetworkPacket() {
|
||||||
|
SDL_SemWait(lock);
|
||||||
|
Packet p;
|
||||||
|
if (queue.size() > 0) {
|
||||||
|
p = queue[0];
|
||||||
|
queue.pop_front();
|
||||||
|
}
|
||||||
|
SDL_SemPost(lock);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void flushNetworkQueue() {
|
||||||
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
|
SDL_SemWait(lock);
|
||||||
|
while(netUtil->Receive());
|
||||||
|
queue.clear();
|
||||||
|
SDL_SemPost(lock);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013-2016
|
/* Copyright: (c) Kayne Ruse 2013
|
||||||
*
|
*
|
||||||
* This software is provided 'as-is', without any express or implied
|
* This software is provided 'as-is', without any express or implied
|
||||||
* warranty. In no event will the authors be held liable for any damages
|
* warranty. In no event will the authors be held liable for any damages
|
||||||
@@ -19,17 +19,16 @@
|
|||||||
* 3. This notice may not be removed or altered from any source
|
* 3. This notice may not be removed or altered from any source
|
||||||
* distribution.
|
* distribution.
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#ifndef NETWORKQUEUE_HPP_
|
||||||
|
#define NETWORKQUEUE_HPP_
|
||||||
|
|
||||||
#include "item_data.hpp"
|
#include "packet.hpp"
|
||||||
|
|
||||||
#include <list>
|
void beginQueueThread();
|
||||||
|
void endQueueThread();
|
||||||
|
void killQueueThread();
|
||||||
|
Packet peekNetworkPacket();
|
||||||
|
Packet popNetworkPacket();
|
||||||
|
void flushNetworkQueue();
|
||||||
|
|
||||||
class Inventory {
|
#endif
|
||||||
public:
|
|
||||||
Inventory() = default;
|
|
||||||
~Inventory() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::list<ItemData> itemList;
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/* 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 PACKETTYPE_HPP_
|
||||||
|
#define PACKETTYPE_HPP_
|
||||||
|
|
||||||
|
#include "vector2.hpp"
|
||||||
|
|
||||||
|
#include "SDL/SDL_net.h"
|
||||||
|
|
||||||
|
#define PACKET_STRING_SIZE 100
|
||||||
|
|
||||||
|
#pragma pack(push, 0)
|
||||||
|
|
||||||
|
union Packet {
|
||||||
|
//the type of packet being sent
|
||||||
|
enum class Type {
|
||||||
|
NONE = 0,
|
||||||
|
|
||||||
|
PING = 1,
|
||||||
|
PONG = 2,
|
||||||
|
BROADCAST_REQUEST = 3,
|
||||||
|
BROADCAST_RESPONSE = 4,
|
||||||
|
JOIN_REQUEST = 5,
|
||||||
|
JOIN_RESPONSE = 6,
|
||||||
|
DISCONNECT = 7,
|
||||||
|
|
||||||
|
SYNCHRONIZE = 8,
|
||||||
|
|
||||||
|
PLAYER_NEW = 9,
|
||||||
|
PLAYER_DELETE = 10,
|
||||||
|
PLAYER_UPDATE = 11,
|
||||||
|
};
|
||||||
|
|
||||||
|
//metadata on the packet itself
|
||||||
|
struct Metadata {
|
||||||
|
Type type;
|
||||||
|
IPaddress address;
|
||||||
|
int clientIndex;
|
||||||
|
}meta;
|
||||||
|
|
||||||
|
//information about the server
|
||||||
|
struct ServerInformation {
|
||||||
|
Metadata meta;
|
||||||
|
//TODO: version info
|
||||||
|
char name[PACKET_STRING_SIZE];
|
||||||
|
//TODO: player count
|
||||||
|
}serverInfo;
|
||||||
|
|
||||||
|
//information about a specific player
|
||||||
|
struct PlayerInformation {
|
||||||
|
Metadata meta;
|
||||||
|
int index;
|
||||||
|
char handle[PACKET_STRING_SIZE];
|
||||||
|
char avatar[PACKET_STRING_SIZE];
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 motion;
|
||||||
|
//TODO Playerdata
|
||||||
|
}playerInfo;
|
||||||
|
|
||||||
|
//zero the packet
|
||||||
|
Packet() {
|
||||||
|
meta.type = Type::NONE;
|
||||||
|
meta.address.host = 0;
|
||||||
|
meta.address.port = 0;
|
||||||
|
meta.clientIndex = -1;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
#endif
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user