Compare commits
81 Commits
release-0.5
..
old
| 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
-2
@@ -1,6 +1,6 @@
|
|||||||
#Editor generated files
|
#Editor generated files
|
||||||
*.sln
|
#*.sln
|
||||||
*.vcproj
|
#*.vcproj
|
||||||
*.suo
|
*.suo
|
||||||
*.ncb
|
*.ncb
|
||||||
*.user
|
*.user
|
||||||
|
|||||||
@@ -1,29 +1,23 @@
|
|||||||
The most recent stable build for Windows can be found [here](https://dl.dropboxusercontent.com/u/46669050/Tortuga.rar).
|
_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 multiplayer JRPG featuring permadeath (deletion of a character upon death). The emphasis of this game is on multiplayer cooperation, exploration and customization. The game runs on customizable server software that can support up to 150 simultaneous players or more.
|
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, as well as more modern sandbox MMOs. This project is currently independently created and funded, with the goal of creating a game that will engage user's imagination and inspire a large modding community.
|
## Dependencies
|
||||||
|
|
||||||
## Documentation
|
* [SDL](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
||||||
|
* [SDL_net](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension
|
||||||
|
|
||||||
Tortuga's full documentation can be found in a separate branch, see [Tortuga/docs](https://github.com/Ratstail91/Tortuga/tree/docs).
|
## Instructions
|
||||||
For Tortuga's primary documentation, please read the [Tortuga Game Design Document](https://github.com/Ratstail91/Tortuga/blob/docs/Tortuga%20Game%20Design%20Document.docx?raw=true).
|
|
||||||
For a list of known bugs, see the [GitHub bug tracker](https://github.com/Ratstail91/Tortuga/issues).
|
|
||||||
|
|
||||||
## External Dependencies
|
* 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.
|
||||||
* [SDL 1.2](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
* You can read more details on the Tortuga wiki [here](https://github.com/Ratstail91/Tortuga/wiki).
|
||||||
* [SDL_net 1.2](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension
|
|
||||||
* [lua 5.2](http://www.lua.org/) - The lua programming language
|
|
||||||
* [SQLite3](http://www.sqlite.org/) - A lightweight SQL database engine
|
|
||||||
|
|
||||||
## 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, 2014 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.
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -34,7 +34,7 @@ SDL_Surface* BaseScene::screen = nullptr;
|
|||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
BaseScene::BaseScene() {
|
BaseScene::BaseScene() {
|
||||||
//
|
nextScene = SceneList::CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseScene::~BaseScene() {
|
BaseScene::~BaseScene() {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -28,18 +28,18 @@
|
|||||||
|
|
||||||
class BaseScene {
|
class BaseScene {
|
||||||
public:
|
public:
|
||||||
//Public access members
|
/* Public access members */
|
||||||
BaseScene();
|
BaseScene();
|
||||||
virtual ~BaseScene();
|
virtual ~BaseScene();
|
||||||
|
|
||||||
//Program control
|
/* Program control */
|
||||||
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
|
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||||
static SDL_Surface* GetScreen();
|
static SDL_Surface* GetScreen();
|
||||||
|
|
||||||
SceneList SetNextScene(SceneList sceneIndex);
|
SceneList SetNextScene(SceneList sceneIndex);
|
||||||
SceneList GetNextScene() const;
|
SceneList GetNextScene() const;
|
||||||
|
|
||||||
//Frame loop
|
/* Frame loop */
|
||||||
virtual void RunFrame(double delta);
|
virtual void RunFrame(double delta);
|
||||||
virtual void RenderFrame();
|
virtual void RenderFrame();
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ protected:
|
|||||||
virtual void FrameEnd() {}
|
virtual void FrameEnd() {}
|
||||||
virtual void Render(SDL_Surface* const screen) {}
|
virtual void Render(SDL_Surface* const screen) {}
|
||||||
|
|
||||||
//Event handlers
|
/* Event handlers */
|
||||||
virtual void QuitEvent() { SetNextScene(SceneList::QUIT); }
|
virtual void QuitEvent() { SetNextScene(SceneList::QUIT); }
|
||||||
virtual void MouseMotion(SDL_MouseMotionEvent const&) {}
|
virtual void MouseMotion(SDL_MouseMotionEvent const&) {}
|
||||||
virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {}
|
virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {}
|
||||||
@@ -68,7 +68,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static SDL_Surface* screen;
|
static SDL_Surface* screen;
|
||||||
SceneList nextScene = SceneList::CONTINUE;
|
SceneList nextScene;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
void Character::Update(double delta) {
|
|
||||||
if (motion.x && motion.y) {
|
|
||||||
origin += motion * delta * CHARACTER_WALKING_MOD;
|
|
||||||
}
|
|
||||||
else if (motion != 0) {
|
|
||||||
origin += motion * delta;
|
|
||||||
}
|
|
||||||
sprite.Update(delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Character::DrawTo(SDL_Surface* const dest, int camX, int camY) {
|
|
||||||
sprite.DrawTo(dest, origin.x - camX, origin.y - camY);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Character::CorrectSprite() {
|
|
||||||
//NOTE: These must correspond to the sprite sheet in use
|
|
||||||
if (motion.y > 0) {
|
|
||||||
sprite.SetYIndex(0);
|
|
||||||
}
|
|
||||||
else if (motion.y < 0) {
|
|
||||||
sprite.SetYIndex(1);
|
|
||||||
}
|
|
||||||
else if (motion.x > 0) {
|
|
||||||
sprite.SetYIndex(3);
|
|
||||||
}
|
|
||||||
else if (motion.x < 0) {
|
|
||||||
sprite.SetYIndex(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
//animation
|
|
||||||
if (motion != 0) {
|
|
||||||
sprite.SetDelay(0.1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
sprite.SetDelay(0);
|
|
||||||
sprite.SetXIndex(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 CHARACTER_HPP_
|
|
||||||
#define CHARACTER_HPP_
|
|
||||||
|
|
||||||
//components
|
|
||||||
#include "character_defines.hpp"
|
|
||||||
#include "vector2.hpp"
|
|
||||||
#include "bounding_box.hpp"
|
|
||||||
#include "statistics.hpp"
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
#include "sprite_sheet.hpp"
|
|
||||||
|
|
||||||
//std namespace
|
|
||||||
#include <string>
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
class Character {
|
|
||||||
public:
|
|
||||||
Character() = default;
|
|
||||||
~Character() = default;
|
|
||||||
|
|
||||||
void Update(double delta);
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
void DrawTo(SDL_Surface* const, int camX, int camY);
|
|
||||||
void CorrectSprite();
|
|
||||||
SpriteSheet* GetSprite() { return &sprite; }
|
|
||||||
|
|
||||||
//gameplay
|
|
||||||
Statistics* GetStats() { return &stats; }
|
|
||||||
|
|
||||||
//accessors and mutators
|
|
||||||
|
|
||||||
//metadata
|
|
||||||
int SetOwner(int i) { return owner = i; }
|
|
||||||
int GetOwner() { return owner; }
|
|
||||||
std::string SetHandle(std::string s) { return handle = s; }
|
|
||||||
std::string GetHandle() const { return handle; }
|
|
||||||
std::string SetAvatar(std::string s) { return avatar = s; }
|
|
||||||
std::string GetAvatar() const { return avatar; }
|
|
||||||
|
|
||||||
//position
|
|
||||||
Vector2 SetOrigin(Vector2 v) { return origin = v; }
|
|
||||||
Vector2 GetOrigin() const { return origin; }
|
|
||||||
Vector2 SetMotion(Vector2 v) { return motion = v; }
|
|
||||||
Vector2 GetMotion() const { return motion; }
|
|
||||||
BoundingBox SetBounds(BoundingBox b) { return bounds = b; }
|
|
||||||
BoundingBox GetBounds() { return bounds; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
//graphics
|
|
||||||
SpriteSheet sprite;
|
|
||||||
|
|
||||||
//base statistics
|
|
||||||
Statistics stats;
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs
|
|
||||||
|
|
||||||
//metadata
|
|
||||||
int owner;
|
|
||||||
std::string handle;
|
|
||||||
std::string avatar;
|
|
||||||
|
|
||||||
//position
|
|
||||||
Vector2 origin = {0.0,0.0};
|
|
||||||
Vector2 motion = {0.0,0.0};
|
|
||||||
BoundingBox bounds;
|
|
||||||
};
|
|
||||||
|
|
||||||
//tmp
|
|
||||||
#include <map>
|
|
||||||
typedef std::map<int, Character> CharacterMap;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
* arising from the use of this software.
|
* arising from the use of this software.
|
||||||
*
|
*
|
||||||
* Permission is granted to anyone to use this software for any purpose,
|
* Permission is granted to anyone to use this software for any purpose,
|
||||||
* including commercial ClientApplications, and to alter it and redistribute it
|
* including commercial applications, and to alter it and redistribute it
|
||||||
* freely, subject to the following restrictions:
|
* freely, subject to the following restrictions:
|
||||||
*
|
*
|
||||||
* 1. The origin of this software must not be misrepresented; you must not
|
* 1. The origin of this software must not be misrepresented; you must not
|
||||||
@@ -21,106 +21,84 @@
|
|||||||
*/
|
*/
|
||||||
#include "client_application.hpp"
|
#include "client_application.hpp"
|
||||||
|
|
||||||
#include "serial.hpp"
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Scene headers
|
//Scene headers
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
//Add the custom scene headers here
|
//Add the custom scene headers here
|
||||||
#include "splash_screen.hpp"
|
|
||||||
#include "main_menu.hpp"
|
|
||||||
#include "options_menu.hpp"
|
|
||||||
#include "lobby_menu.hpp"
|
|
||||||
#include "in_world.hpp"
|
|
||||||
#include "in_combat.hpp"
|
#include "in_combat.hpp"
|
||||||
#include "clean_up.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");
|
}
|
||||||
|
|
||||||
//-------------------------
|
/* ClientApplication::Init()
|
||||||
//Initialize the APIs
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
//initialize SDL
|
void ClientApplication::Init() {
|
||||||
|
//load the config file
|
||||||
|
try {
|
||||||
|
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
|
||||||
if (SDL_Init(SDL_INIT_VIDEO)) {
|
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
throw(std::runtime_error("Failed to initialize SDL"));
|
throw(std::runtime_error("Failed to initialize SDL"));
|
||||||
}
|
}
|
||||||
std::cout << "Initialized SDL" << std::endl;
|
|
||||||
|
|
||||||
//initialize SDL_net
|
|
||||||
if (SDLNet_Init()) {
|
if (SDLNet_Init()) {
|
||||||
throw(std::runtime_error("Failed to initialize SDL_net"));
|
throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||||
}
|
}
|
||||||
network.Open(0);
|
|
||||||
std::cout << "Initialized SDL_net" << std::endl;
|
|
||||||
|
|
||||||
//-------------------------
|
//create the screen
|
||||||
//Setup the screen
|
Uint32 flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
|
||||||
//-------------------------
|
flags |= configUtil->Bool("screen.f") ? SDL_FULLSCREEN : 0;
|
||||||
|
|
||||||
int w = config.Int("client.screen.w");
|
BaseScene::SetScreen(
|
||||||
int h = config.Int("client.screen.h");
|
configUtil->Int("screen.w"),
|
||||||
int f = config.Bool("client.screen.f") ? SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN : SDL_HWSURFACE|SDL_DOUBLEBUF;
|
configUtil->Int("screen.h"),
|
||||||
|
SDL_GetVideoInfo()->vfmt->BitsPerPixel,
|
||||||
|
flags);
|
||||||
|
|
||||||
BaseScene::SetScreen(w ? w : 800, h ? h : 600, 0, f);
|
//initiate the remaining singletons
|
||||||
std::cout << "Initialized the screen" << std::endl;
|
netUtil->Open(0, sizeof(Packet));
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//debug output
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//TODO: enable/disable these with a switch
|
|
||||||
#define DEBUG_OUTPUT_VAR(x) std::cout << "\t" << #x << ": " << x << std::endl;
|
|
||||||
|
|
||||||
std::cout << "Internal sizes:" << std::endl;
|
|
||||||
|
|
||||||
DEBUG_OUTPUT_VAR(sizeof(Region::type_t));
|
|
||||||
DEBUG_OUTPUT_VAR(sizeof(Region));
|
|
||||||
DEBUG_OUTPUT_VAR(REGION_WIDTH);
|
|
||||||
DEBUG_OUTPUT_VAR(REGION_HEIGHT);
|
|
||||||
DEBUG_OUTPUT_VAR(REGION_DEPTH);
|
|
||||||
DEBUG_OUTPUT_VAR(REGION_SOLID_FOOTPRINT);
|
|
||||||
DEBUG_OUTPUT_VAR(REGION_FOOTPRINT);
|
|
||||||
DEBUG_OUTPUT_VAR(PACKET_BUFFER_SIZE);
|
|
||||||
DEBUG_OUTPUT_VAR(MAX_PACKET_SIZE);
|
|
||||||
|
|
||||||
#undef DEBUG_OUTPUT_VAR
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//finalize the startup
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
std::cout << "Startup completed successfully" << std::endl;
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//debugging
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientApplication::Proc() {
|
void ClientApplication::Proc() {
|
||||||
LoadScene(SceneList::FIRST);
|
LoadScene(SceneList::FIRST);
|
||||||
|
|
||||||
//prepare the time system
|
//prepare the time system
|
||||||
typedef std::chrono::steady_clock Clock;
|
typedef std::chrono::high_resolution_clock Clock;
|
||||||
|
|
||||||
std::chrono::duration<int, std::milli> delta(16);
|
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;
|
||||||
|
|
||||||
@@ -132,13 +110,15 @@ void ClientApplication::Proc() {
|
|||||||
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) {
|
||||||
|
SDL_Delay(1);
|
||||||
|
}
|
||||||
|
|
||||||
//simulate game time
|
//simulate game time
|
||||||
while (simTime < realTime) {
|
while (simTime < realTime) {
|
||||||
//call each user defined function
|
//call each user defined function
|
||||||
activeScene->RunFrame(double(delta.count()) / std::chrono::duration<int, std::milli>::period::den);
|
activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den);
|
||||||
simTime += delta;
|
simTime += delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,11 +130,16 @@ void ClientApplication::Proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ClientApplication::Quit() {
|
void ClientApplication::Quit() {
|
||||||
std::cout << "Shutting down" << std::endl;
|
//clean up the singletons
|
||||||
network.Close();
|
netUtil->Close();
|
||||||
|
surfaceMgr->FreeAll();
|
||||||
|
|
||||||
|
//clean up the scene
|
||||||
|
UnloadScene();
|
||||||
|
|
||||||
|
//deinitialize the APIs
|
||||||
SDLNet_Quit();
|
SDLNet_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
std::cout << "Clean exit" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -163,30 +148,29 @@ void ClientApplication::Quit() {
|
|||||||
|
|
||||||
void ClientApplication::LoadScene(SceneList sceneIndex) {
|
void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||||
UnloadScene();
|
UnloadScene();
|
||||||
|
|
||||||
switch(sceneIndex) {
|
switch(sceneIndex) {
|
||||||
//add scene creation calls here
|
//add scene creation calls here
|
||||||
case SceneList::FIRST:
|
case SceneList::INCOMBAT:
|
||||||
case SceneList::SPLASHSCREEN:
|
activeScene = new InCombat();
|
||||||
activeScene = new SplashScreen();
|
break;
|
||||||
|
case SceneList::INWORLD:
|
||||||
|
activeScene = new InWorld();
|
||||||
|
break;
|
||||||
|
case SceneList::LOBBY:
|
||||||
|
activeScene = new Lobby();
|
||||||
break;
|
break;
|
||||||
case SceneList::MAINMENU:
|
case SceneList::MAINMENU:
|
||||||
activeScene = new MainMenu();
|
activeScene = new MainMenu();
|
||||||
break;
|
break;
|
||||||
case SceneList::OPTIONSMENU:
|
case SceneList::OPTIONSCREEN:
|
||||||
activeScene = new OptionsMenu();
|
activeScene = new OptionScreen();
|
||||||
break;
|
break;
|
||||||
case SceneList::LOBBYMENU:
|
case SceneList::FIRST:
|
||||||
activeScene = new LobbyMenu(&network, &clientIndex, &accountIndex);
|
case SceneList::SPLASHSCREEN:
|
||||||
break;
|
activeScene = new SplashScreen();
|
||||||
case SceneList::INWORLD:
|
|
||||||
activeScene = new InWorld(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap);
|
|
||||||
break;
|
|
||||||
case SceneList::INCOMBAT:
|
|
||||||
activeScene = new InCombat(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap);
|
|
||||||
break;
|
|
||||||
case SceneList::CLEANUP:
|
|
||||||
activeScene = new CleanUp(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw(std::logic_error("Failed to recognize the scene index"));
|
throw(std::logic_error("Failed to recognize the scene index"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -24,40 +24,39 @@
|
|||||||
|
|
||||||
#include "scene_list.hpp"
|
#include "scene_list.hpp"
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
#include "character.hpp"
|
|
||||||
|
|
||||||
#include "singleton.hpp"
|
#include "singleton.hpp"
|
||||||
|
#include "packet.hpp"
|
||||||
|
#include "information_manager.hpp"
|
||||||
|
|
||||||
#include <map>
|
#include "config_utility.hpp"
|
||||||
|
#include "surface_manager.hpp"
|
||||||
|
#include "udp_network_utility.hpp"
|
||||||
|
|
||||||
class ClientApplication: public Singleton<ClientApplication> {
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
|
class ClientApplication {
|
||||||
public:
|
public:
|
||||||
//public methods
|
/* Public access members */
|
||||||
void Init(int argc, char** argv);
|
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 */
|
||||||
|
|
||||||
ClientApplication() = default;
|
|
||||||
~ClientApplication() = default;
|
|
||||||
|
|
||||||
//Private access members
|
|
||||||
void LoadScene(SceneList sceneIndex);
|
void LoadScene(SceneList sceneIndex);
|
||||||
void UnloadScene();
|
void UnloadScene();
|
||||||
|
|
||||||
BaseScene* activeScene = nullptr;
|
BaseScene* activeScene = nullptr;
|
||||||
|
|
||||||
//shared parameters
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
UDPNetworkUtility network;
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
int clientIndex = -1;
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
int accountIndex = -1;
|
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||||
int characterIndex = -1;
|
|
||||||
|
|
||||||
CharacterMap characterMap;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,33 +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() {
|
InCombat::InCombat() {
|
||||||
logo.LoadSurface(ConfigUtility::GetSingleton()["dir.logos"] + "krstudios.bmp");
|
#ifdef DEBUG
|
||||||
startTick = std::chrono::steady_clock::now();
|
cout << "entering InCombat" << endl;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
SplashScreen::~SplashScreen() {
|
InCombat::~InCombat() {
|
||||||
//
|
#ifdef DEBUG
|
||||||
|
cout << "leaving InCombat" << endl;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Frame loop
|
//Frame loop
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void SplashScreen::Update(double delta) {
|
void InCombat::FrameStart() {
|
||||||
if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(1)) {
|
//
|
||||||
SetNextScene(SceneList::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::Render(SDL_Surface* const screen) {
|
void InCombat::KeyUp(SDL_KeyboardEvent const& key) {
|
||||||
logo.DrawTo(screen, (screen->w - logo.GetClipW()) / 2, (screen->h - logo.GetClipH()) / 2);
|
//
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,40 +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.
|
||||||
*/
|
*/
|
||||||
#ifndef OPTIONSMENU_HPP_
|
#ifndef INCOMBAT_HPP_
|
||||||
#define OPTIONSMENU_HPP_
|
#define INCOMBAT_HPP_
|
||||||
|
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
#include "image.hpp"
|
class InCombat : public BaseScene {
|
||||||
#include "raster_font.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
|
|
||||||
//TODO: The options screen needs to be USED
|
|
||||||
class OptionsMenu : public BaseScene {
|
|
||||||
public:
|
public:
|
||||||
//Public access members
|
/* Public access members */
|
||||||
OptionsMenu();
|
InCombat();
|
||||||
~OptionsMenu();
|
~InCombat();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//Frame loop
|
/* Frame loop */
|
||||||
void FrameStart();
|
void FrameStart();
|
||||||
void Update(double delta);
|
void Update(double delta);
|
||||||
void FrameEnd();
|
void FrameEnd();
|
||||||
void Render(SDL_Surface* const);
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
//Event handlers
|
/* Event handlers */
|
||||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
void KeyDown(SDL_KeyboardEvent const&);
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
void KeyUp(SDL_KeyboardEvent const&);
|
void KeyUp(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
//members
|
|
||||||
Image image;
|
|
||||||
RasterFont font;
|
|
||||||
Button backButton;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#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));
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,38 @@
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
#ifndef INCOMBAT_HPP_
|
#ifndef INWORLD_HPP_
|
||||||
#define INCOMBAT_HPP_
|
#define INWORLD_HPP_
|
||||||
|
|
||||||
//network
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "raster_font.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
|
|
||||||
//common
|
|
||||||
#include "frame_rate.hpp"
|
|
||||||
|
|
||||||
#include "character.hpp"
|
|
||||||
|
|
||||||
//client
|
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
class InCombat : public BaseScene {
|
#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:
|
||||||
//Public access members
|
//Public access members
|
||||||
InCombat(
|
InWorld();
|
||||||
UDPNetworkUtility* const argNetwork,
|
~InWorld();
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex,
|
|
||||||
int* const argCharacterIndex,
|
|
||||||
CharacterMap* argCharacterMap
|
|
||||||
);
|
|
||||||
~InCombat();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//Frame loop
|
//Frame loop
|
||||||
void FrameStart();
|
void FrameStart();
|
||||||
void Update(double delta);
|
void Update(double delta);
|
||||||
void FrameEnd();
|
void FrameEnd();
|
||||||
void RenderFrame();
|
|
||||||
void Render(SDL_Surface* const);
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
//Event handlers
|
//Event handlers
|
||||||
@@ -66,29 +61,28 @@ protected:
|
|||||||
void KeyDown(SDL_KeyboardEvent const&);
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
void KeyUp(SDL_KeyboardEvent const&);
|
void KeyUp(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
//Network handlers
|
//Utilities
|
||||||
void HandlePacket(SerialPacket* const);
|
int HandlePacket(Packet);
|
||||||
void HandleDisconnect(SerialPacket* const);
|
void Disconnect();
|
||||||
|
void ExitGame();
|
||||||
|
|
||||||
//Server control
|
void HandleDisconnection(Packet&);
|
||||||
void RequestSynchronize();
|
|
||||||
void SendPlayerUpdate();
|
|
||||||
void RequestDisconnect();
|
|
||||||
void RequestShutdown();
|
|
||||||
|
|
||||||
//shared parameters
|
void AddPlayer(Packet&);
|
||||||
UDPNetworkUtility& network;
|
void RemovePlayer(Packet&);
|
||||||
int& clientIndex;
|
void UpdatePlayer(Packet&);
|
||||||
int& accountIndex;
|
|
||||||
int& characterIndex;
|
|
||||||
CharacterMap& characterMap;
|
|
||||||
|
|
||||||
//graphics
|
void SendState();
|
||||||
//TODO: graphics
|
|
||||||
|
|
||||||
//UI
|
//services
|
||||||
//TODO: UI
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
FrameRate fps;
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
|
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||||
|
|
||||||
|
//members
|
||||||
|
RasterFont font;
|
||||||
|
std::map<int, PlayerCharacter> playerCharacters;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
/* 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,33 +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.
|
||||||
*/
|
*/
|
||||||
#ifndef ENEMYMANAGER_HPP_
|
#ifndef INFORMATIONMANAGER_HPP_
|
||||||
#define ENEMYMANAGER_HPP_
|
#define INFORMATIONMANAGER_HPP_
|
||||||
|
|
||||||
#include "enemy_data.hpp"
|
class InformationManager {
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
|
||||||
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
class EnemyManager {
|
|
||||||
public:
|
public:
|
||||||
EnemyManager() = default;
|
int SetClientIndex(int i) { return clientIndex = i; }
|
||||||
~EnemyManager() = default;
|
int GetClientIndex() { return clientIndex; }
|
||||||
|
void ResetClientIndex() { clientIndex = -1; }
|
||||||
//public access methods
|
|
||||||
//TODO
|
|
||||||
|
|
||||||
//accessors and mutators
|
|
||||||
EnemyData* GetEnemy(int uid);
|
|
||||||
std::map<int, EnemyData>* GetContainer();
|
|
||||||
|
|
||||||
lua_State* SetLuaState(lua_State*);
|
|
||||||
lua_State* GetLuaState();
|
|
||||||
|
|
||||||
|
//one player at a time
|
||||||
|
int SetPlayerIndex(int i) { return playerIndex = i; }
|
||||||
|
int GetPlayerIndex() { return playerIndex; }
|
||||||
|
void ResetPlayerIndex() { playerIndex = -1; }
|
||||||
private:
|
private:
|
||||||
std::map<int, EnemyData> enemyMap;
|
int clientIndex = -1;
|
||||||
lua_State* luaState = nullptr;
|
int playerIndex = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#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);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,33 +19,38 @@
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
#ifndef LOBBYMENU_HPP_
|
#ifndef LOBBY_HPP_
|
||||||
#define LOBBYMENU_HPP_
|
#define LOBBY_HPP_
|
||||||
|
|
||||||
//graphics & ui
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "raster_font.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
|
|
||||||
//utilities
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
//client
|
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
//STL
|
#include "defines.hpp"
|
||||||
#include <vector>
|
#include "singleton.hpp"
|
||||||
|
|
||||||
class LobbyMenu : public BaseScene {
|
#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:
|
||||||
//Public access members
|
//Public access members
|
||||||
LobbyMenu(
|
Lobby();
|
||||||
UDPNetworkUtility* const argNetwork,
|
~Lobby();
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex
|
|
||||||
);
|
|
||||||
~LobbyMenu();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//Frame loop
|
//Frame loop
|
||||||
@@ -61,39 +66,28 @@ protected:
|
|||||||
void KeyDown(SDL_KeyboardEvent const&);
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
void KeyUp(SDL_KeyboardEvent const&);
|
void KeyUp(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
//Network handlers
|
//utilities
|
||||||
void HandlePacket(SerialPacket* const);
|
int HandlePacket(Packet);
|
||||||
void HandleBroadcastResponse(ServerPacket* const);
|
void BroadcastNetwork();
|
||||||
void HandleJoinResponse(ClientPacket* const);
|
void PushServer(Packet&);
|
||||||
|
void ConnectToServer(ServerEntry*);
|
||||||
|
void BeginGame(Packet&);
|
||||||
|
|
||||||
//shared parameters
|
//services
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
UDPNetworkUtility& network;
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
int& clientIndex;
|
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||||
int& accountIndex;
|
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||||
|
|
||||||
//members
|
//members
|
||||||
Image image;
|
Button refreshButton;
|
||||||
|
Button joinButton;
|
||||||
|
Button backButton;
|
||||||
|
|
||||||
RasterFont font;
|
RasterFont font;
|
||||||
Button search;
|
|
||||||
Button join;
|
|
||||||
Button back;
|
|
||||||
|
|
||||||
//server list
|
|
||||||
struct ServerInformation {
|
|
||||||
IPaddress address;
|
|
||||||
std::string name;
|
|
||||||
int playerCount;
|
|
||||||
int version;
|
|
||||||
bool compatible;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<ServerInformation> serverInfo;
|
|
||||||
|
|
||||||
//a terrible hack, forgive me
|
|
||||||
//I'd love a proper gui system for this
|
|
||||||
SDL_Rect listBox;
|
SDL_Rect listBox;
|
||||||
ServerInformation* selection = nullptr;
|
std::vector<ServerEntry> serverList;
|
||||||
|
ServerEntry* selectedServer = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+11
-17
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,33 +21,27 @@
|
|||||||
*/
|
*/
|
||||||
#include "client_application.hpp"
|
#include "client_application.hpp"
|
||||||
|
|
||||||
//singletons
|
|
||||||
#include "config_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;
|
||||||
ClientApplication::Create();
|
app.Init();
|
||||||
ConfigUtility::Create();
|
|
||||||
|
|
||||||
//call the server's routines
|
|
||||||
ClientApplication& app = ClientApplication::GetSingleton();
|
|
||||||
app.Init(argc, argv);
|
|
||||||
app.Proc();
|
app.Proc();
|
||||||
app.Quit();
|
app.Quit();
|
||||||
|
|
||||||
//delete the singletons
|
|
||||||
ConfigUtility::Delete();
|
|
||||||
ClientApplication::Delete();
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,47 +21,27 @@
|
|||||||
*/
|
*/
|
||||||
#include "main_menu.hpp"
|
#include "main_menu.hpp"
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
MainMenu::MainMenu() {
|
MainMenu::MainMenu() {
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
#ifdef DEBUG
|
||||||
|
cout << "entering MainMenu" << endl;
|
||||||
//setup the utility objects
|
#endif
|
||||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
startButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start");
|
||||||
image.SetClipH(image.GetClipH()/3);
|
optionsButton.Setup(50, 50 + surfaceMgr->Get("button")->h/3, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options");
|
||||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
quitButton.Setup(50, 50 + surfaceMgr->Get("button")->h/3 * 2, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit");
|
||||||
|
|
||||||
//pass the utility objects
|
|
||||||
startButton.SetImage(&image);
|
|
||||||
startButton.SetFont(&font);
|
|
||||||
optionsButton.SetImage(&image);
|
|
||||||
optionsButton.SetFont(&font);
|
|
||||||
quitButton.SetImage(&image);
|
|
||||||
quitButton.SetFont(&font);
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
startButton.SetX(50);
|
|
||||||
startButton.SetY(50 + image.GetClipH() * 0);
|
|
||||||
optionsButton.SetX(50);
|
|
||||||
optionsButton.SetY(50 + image.GetClipH() * 1);
|
|
||||||
quitButton.SetX(50);
|
|
||||||
quitButton.SetY(50 + image.GetClipH() * 2);
|
|
||||||
|
|
||||||
//set the button texts
|
|
||||||
startButton.SetText("Start");
|
|
||||||
optionsButton.SetText("Options");
|
|
||||||
quitButton.SetText("Quit");
|
|
||||||
|
|
||||||
//debug
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainMenu::~MainMenu() {
|
MainMenu::~MainMenu() {
|
||||||
//
|
#ifdef DEBUG
|
||||||
|
cout << "leaving MainMenu" << endl;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
@@ -103,12 +83,11 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
//TODO: Buttons should only register as "selected" when the left button is used
|
|
||||||
if (startButton.MouseButtonUp(button) == Button::State::HOVER) {
|
if (startButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
SetNextScene(SceneList::LOBBYMENU);
|
SetNextScene(SceneList::LOBBY);
|
||||||
}
|
}
|
||||||
if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) {
|
if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
SetNextScene(SceneList::OPTIONSMENU);
|
SetNextScene(SceneList::OPTIONSCREEN);
|
||||||
}
|
}
|
||||||
if (quitButton.MouseButtonUp(button) == Button::State::HOVER) {
|
if (quitButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
QuitEvent();
|
QuitEvent();
|
||||||
@@ -116,9 +95,9 @@ void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
//
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
QuitEvent();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -23,34 +23,32 @@
|
|||||||
#define MAINMENU_HPP_
|
#define MAINMENU_HPP_
|
||||||
|
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
#include "singleton.hpp"
|
||||||
|
|
||||||
#include "image.hpp"
|
#include "surface_manager.hpp"
|
||||||
#include "raster_font.hpp"
|
|
||||||
#include "button.hpp"
|
#include "button.hpp"
|
||||||
|
|
||||||
class MainMenu : public BaseScene {
|
class MainMenu : public BaseScene {
|
||||||
public:
|
public:
|
||||||
//Public access members
|
/* Public access members */
|
||||||
MainMenu();
|
MainMenu();
|
||||||
~MainMenu();
|
~MainMenu();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//Frame loop
|
/* Frame loop */
|
||||||
void FrameStart();
|
void FrameStart();
|
||||||
void Update(double delta);
|
void Update(double delta);
|
||||||
void FrameEnd();
|
void FrameEnd();
|
||||||
void Render(SDL_Surface* const);
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
//Event handlers
|
/* Event handlers */
|
||||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
void KeyDown(SDL_KeyboardEvent const&);
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
void KeyUp(SDL_KeyboardEvent const&);
|
|
||||||
|
|
||||||
//members
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
Image image;
|
|
||||||
RasterFont font;
|
|
||||||
Button startButton;
|
Button startButton;
|
||||||
Button optionsButton;
|
Button optionsButton;
|
||||||
Button quitButton;
|
Button quitButton;
|
||||||
+7
-7
@@ -1,14 +1,15 @@
|
|||||||
#config
|
#config
|
||||||
INCLUDES+=. scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/ui ../common/utilities
|
LOCALLIBS=../lib/libCommon.a
|
||||||
LIBS+=client.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua
|
LIB=$(LOCALLIBS) -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL
|
||||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
INCLUDES=../common
|
||||||
|
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||||
|
|
||||||
#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
|
||||||
@@ -16,8 +17,7 @@ OUT=$(addprefix $(OUTDIR)/,client)
|
|||||||
|
|
||||||
#targets
|
#targets
|
||||||
all: $(OBJ) $(OUT)
|
all: $(OBJ) $(OUT)
|
||||||
$(MAKE) -C scenes
|
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIB)
|
||||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
$(OBJ): | $(OBJDIR)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,82 +19,59 @@
|
|||||||
* 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 "options_menu.hpp"
|
#include "option_screen.hpp"
|
||||||
|
|
||||||
#include "config_utility.hpp"
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
OptionsMenu::OptionsMenu() {
|
OptionScreen::OptionScreen() {
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
#ifdef DEBUG
|
||||||
|
cout << "entering OptionScreen" << endl;
|
||||||
//setup the utility objects
|
#endif
|
||||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
backButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back");
|
||||||
image.SetClipH(image.GetClipH()/3);
|
|
||||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
|
||||||
|
|
||||||
//pass the utility objects
|
|
||||||
backButton.SetImage(&image);
|
|
||||||
backButton.SetFont(&font);
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
backButton.SetX(50);
|
|
||||||
backButton.SetY(50 + image.GetClipH() * 0);
|
|
||||||
|
|
||||||
//set the button texts
|
|
||||||
backButton.SetText("Back");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OptionsMenu::~OptionsMenu() {
|
OptionScreen::~OptionScreen() {
|
||||||
//
|
#ifdef DEBUG
|
||||||
|
cout << "leaving OptionScreen" << endl;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Frame loop
|
//Frame loop
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void OptionsMenu::FrameStart() {
|
void OptionScreen::Render(SDL_Surface* const screen) {
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::Update(double delta) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void OptionsMenu::Render(SDL_Surface* const screen) {
|
|
||||||
backButton.DrawTo(screen);
|
backButton.DrawTo(screen);
|
||||||
|
|
||||||
font.DrawStringTo("Oh, were you looking for the options screen?", screen, 50, 30);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Event handlers
|
//Event handlers
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
void OptionsMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
void OptionScreen::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
backButton.MouseMotion(motion);
|
backButton.MouseMotion(motion);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OptionsMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
void OptionScreen::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||||
backButton.MouseButtonDown(button);
|
backButton.MouseButtonDown(button);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OptionsMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
void OptionScreen::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||||
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||||
SetNextScene(SceneList::MAINMENU);
|
SetNextScene(SceneList::MAINMENU);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OptionsMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
void OptionScreen::KeyDown(SDL_KeyboardEvent const& key) {
|
||||||
//
|
switch(key.keysym.sym) {
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
SetNextScene(SceneList::MAINMENU);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OptionsMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
/* 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,29 +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.
|
||||||
*/
|
*/
|
||||||
#ifndef ENEMYDATA_HPP_
|
#ifndef OPTIONSCREEN_HPP_
|
||||||
#define ENEMYDATA_HPP_
|
#define OPTIONSCREEN_HPP_
|
||||||
|
|
||||||
#include "vector2.hpp"
|
#include "base_scene.hpp"
|
||||||
#include "statistics.hpp"
|
#include "singleton.hpp"
|
||||||
|
|
||||||
//std namespace
|
#include "surface_manager.hpp"
|
||||||
#include <string>
|
#include "button.hpp"
|
||||||
|
|
||||||
struct EnemyData {
|
class OptionScreen : public BaseScene {
|
||||||
//metadata
|
public:
|
||||||
std::string handle;
|
/* Public access members */
|
||||||
std::string avatar;
|
OptionScreen();
|
||||||
|
~OptionScreen();
|
||||||
|
|
||||||
//gameplay
|
protected:
|
||||||
Statistics stats;
|
/* Frame loop */
|
||||||
|
void Render(SDL_Surface* const);
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs, rewards
|
/* Event handlers */
|
||||||
|
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
|
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
|
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
//active gameplay members
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
//NOTE: these are lost when unloaded
|
Button backButton;
|
||||||
int tableIndex;
|
|
||||||
int atbGauge = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
/* 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 PLAYERCHARACTER_HPP_
|
||||||
|
#define PLAYERCHARACTER_HPP_
|
||||||
|
|
||||||
|
#include "vector2.hpp"
|
||||||
|
#include "sprite_sheet.hpp"
|
||||||
|
#include "defines.hpp"
|
||||||
|
|
||||||
|
class PlayerCharacter {
|
||||||
|
public:
|
||||||
|
PlayerCharacter() = default;
|
||||||
|
~PlayerCharacter() = default;
|
||||||
|
|
||||||
|
void Update(double delta);
|
||||||
|
|
||||||
|
void MoveDirection(CardinalDirection);
|
||||||
|
void FaceDirection(CardinalDirection);
|
||||||
|
void FaceDirection();
|
||||||
|
|
||||||
|
void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); }
|
||||||
|
|
||||||
|
//accessors and mutators
|
||||||
|
Vector2 SetPosition(Vector2 v) { return position = v; }
|
||||||
|
Vector2 ShiftPosition(Vector2 v) { return position += v; }
|
||||||
|
Vector2 GetPosition() { return position; }
|
||||||
|
|
||||||
|
Vector2 SetMotion(Vector2 v) { return motion = v; }
|
||||||
|
Vector2 ShiftMotion(Vector2 v) { return motion += v; }
|
||||||
|
Vector2 GetMotion() { return motion; }
|
||||||
|
|
||||||
|
SpriteSheet* GetSprite() { return &sprite; }
|
||||||
|
private:
|
||||||
|
void CheckSpeed();
|
||||||
|
|
||||||
|
Vector2 position;
|
||||||
|
Vector2 motion;
|
||||||
|
SpriteSheet sprite;
|
||||||
|
|
||||||
|
//for moving diagonal
|
||||||
|
bool limitSpeed = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -29,13 +29,12 @@ enum class SceneList {
|
|||||||
FIRST,
|
FIRST,
|
||||||
|
|
||||||
//custom indexes
|
//custom indexes
|
||||||
SPLASHSCREEN,
|
|
||||||
MAINMENU,
|
|
||||||
OPTIONSMENU,
|
|
||||||
LOBBYMENU,
|
|
||||||
INWORLD,
|
|
||||||
INCOMBAT,
|
INCOMBAT,
|
||||||
CLEANUP,
|
INWORLD,
|
||||||
|
LOBBY,
|
||||||
|
MAINMENU,
|
||||||
|
OPTIONSCREEN,
|
||||||
|
SPLASHSCREEN,
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,134 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 "clean_up.hpp"
|
|
||||||
|
|
||||||
#include "channels.hpp"
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
CleanUp::CleanUp(
|
|
||||||
UDPNetworkUtility* const argNetwork,
|
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex,
|
|
||||||
int* const argCharacterIndex,
|
|
||||||
CharacterMap* argCharacterMap
|
|
||||||
):
|
|
||||||
network(*argNetwork),
|
|
||||||
clientIndex(*argClientIndex),
|
|
||||||
accountIndex(*argAccountIndex),
|
|
||||||
characterIndex(*argCharacterIndex),
|
|
||||||
characterMap(*argCharacterMap)
|
|
||||||
{
|
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
|
||||||
|
|
||||||
//setup the utility objects
|
|
||||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
|
||||||
image.SetClipH(image.GetClipH()/3);
|
|
||||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
|
||||||
|
|
||||||
//pass the utility objects
|
|
||||||
backButton.SetImage(&image);
|
|
||||||
backButton.SetFont(&font);
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
backButton.SetX(50);
|
|
||||||
backButton.SetY(50 + image.GetClipH() * 0);
|
|
||||||
|
|
||||||
//set the button texts
|
|
||||||
backButton.SetText("Back");
|
|
||||||
|
|
||||||
//full reset
|
|
||||||
network.Unbind(Channels::SERVER);
|
|
||||||
clientIndex = -1;
|
|
||||||
accountIndex = -1;
|
|
||||||
characterIndex = -1;
|
|
||||||
// combatMap.clear();
|
|
||||||
characterMap.clear();
|
|
||||||
// enemyMap.clear();
|
|
||||||
|
|
||||||
//auto return
|
|
||||||
startTick = std::chrono::steady_clock::now();
|
|
||||||
}
|
|
||||||
|
|
||||||
CleanUp::~CleanUp() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void CleanUp::Update(double delta) {
|
|
||||||
if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(10)) {
|
|
||||||
QuitEvent();
|
|
||||||
}
|
|
||||||
|
|
||||||
//BUGFIX: Eat incoming packets
|
|
||||||
while(network.Receive());
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanUp::RenderFrame() {
|
|
||||||
SDL_FillRect(GetScreen(), 0, 0);
|
|
||||||
Render(GetScreen());
|
|
||||||
SDL_Flip(GetScreen());
|
|
||||||
fps.Calculate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanUp::Render(SDL_Surface* const screen) {
|
|
||||||
backButton.DrawTo(screen);
|
|
||||||
font.DrawStringTo("You have been disconnected.", screen, 50, 30);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void CleanUp::QuitEvent() {
|
|
||||||
SetNextScene(SceneList::QUIT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanUp::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
|
||||||
backButton.MouseMotion(motion);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanUp::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
|
||||||
backButton.MouseButtonDown(button);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanUp::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|
||||||
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
|
||||||
SetNextScene(SceneList::MAINMENU);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanUp::KeyDown(SDL_KeyboardEvent const& key) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void CleanUp::KeyUp(SDL_KeyboardEvent const& key) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 CLEANUP_HPP_
|
|
||||||
#define CLEANUP_HPP_
|
|
||||||
|
|
||||||
//network
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "raster_font.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
|
|
||||||
//common
|
|
||||||
#include "frame_rate.hpp"
|
|
||||||
|
|
||||||
#include "character.hpp"
|
|
||||||
|
|
||||||
//client
|
|
||||||
#include "base_scene.hpp"
|
|
||||||
|
|
||||||
//std namespace
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
class CleanUp : public BaseScene {
|
|
||||||
public:
|
|
||||||
//Public access members
|
|
||||||
CleanUp(
|
|
||||||
UDPNetworkUtility* const argNetwork,
|
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex,
|
|
||||||
int* const argCharacterIndex,
|
|
||||||
CharacterMap* argCharacterMap
|
|
||||||
);
|
|
||||||
~CleanUp();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
//Frame loop
|
|
||||||
void Update(double delta);
|
|
||||||
void RenderFrame();
|
|
||||||
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&);
|
|
||||||
|
|
||||||
//shared parameters
|
|
||||||
UDPNetworkUtility& network;
|
|
||||||
int& clientIndex;
|
|
||||||
int& accountIndex;
|
|
||||||
int& characterIndex;
|
|
||||||
CharacterMap& characterMap;
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
Image image;
|
|
||||||
RasterFont font;
|
|
||||||
|
|
||||||
//UI
|
|
||||||
Button backButton;
|
|
||||||
FrameRate fps;
|
|
||||||
|
|
||||||
//auto return
|
|
||||||
std::chrono::steady_clock::time_point startTick;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,220 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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_combat.hpp"
|
|
||||||
|
|
||||||
#include "channels.hpp"
|
|
||||||
#include "utility.hpp"
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
InCombat::InCombat(
|
|
||||||
UDPNetworkUtility* const argNetwork,
|
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex,
|
|
||||||
int* const argCharacterIndex,
|
|
||||||
CharacterMap* argCharacterMap
|
|
||||||
):
|
|
||||||
network(*argNetwork),
|
|
||||||
clientIndex(*argClientIndex),
|
|
||||||
accountIndex(*argAccountIndex),
|
|
||||||
characterIndex(*argCharacterIndex),
|
|
||||||
characterMap(*argCharacterMap)
|
|
||||||
{
|
|
||||||
/* //setup the utility objects
|
|
||||||
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
|
||||||
buttonImage.SetClipH(buttonImage.GetClipH()/3);
|
|
||||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
|
||||||
|
|
||||||
//pass the utility objects
|
|
||||||
backButton.SetImage(&buttonImage);
|
|
||||||
backButton.SetFont(&font);
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
backButton.SetX(50);
|
|
||||||
backButton.SetY(50 + buttonImage.GetClipH() * 0);
|
|
||||||
|
|
||||||
//set the button texts
|
|
||||||
backButton.SetText("Back");
|
|
||||||
|
|
||||||
//request a sync
|
|
||||||
RequestSynchronize();
|
|
||||||
*/
|
|
||||||
//debug
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
InCombat::~InCombat() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InCombat::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::Update(double delta) {
|
|
||||||
//suck in and process all waiting packets
|
|
||||||
SerialPacket* packetBuffer = static_cast<SerialPacket*>(malloc(MAX_PACKET_SIZE));
|
|
||||||
while(network.Receive(packetBuffer)) {
|
|
||||||
HandlePacket(packetBuffer);
|
|
||||||
}
|
|
||||||
free(static_cast<void*>(packetBuffer));
|
|
||||||
|
|
||||||
//TODO: more
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::RenderFrame() {
|
|
||||||
SDL_FillRect(GetScreen(), 0, 0);
|
|
||||||
Render(GetScreen());
|
|
||||||
SDL_Flip(GetScreen());
|
|
||||||
fps.Calculate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::Render(SDL_Surface* const screen) {
|
|
||||||
//TODO: draw the background
|
|
||||||
|
|
||||||
//TODO: draw the characters
|
|
||||||
|
|
||||||
//TODO: draw the enemies
|
|
||||||
|
|
||||||
//TODO: draw the UI
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InCombat::QuitEvent() {
|
|
||||||
//exit the game AND the server
|
|
||||||
RequestDisconnect();
|
|
||||||
SetNextScene(SceneList::QUIT);
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::KeyUp(SDL_KeyboardEvent const& key) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Network handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InCombat::HandlePacket(SerialPacket* const argPacket) {
|
|
||||||
switch(argPacket->type) {
|
|
||||||
case SerialPacketType::DISCONNECT:
|
|
||||||
HandleDisconnect(argPacket);
|
|
||||||
break;
|
|
||||||
//handle errors
|
|
||||||
default:
|
|
||||||
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InCombat: " + to_string_custom(static_cast<int>(argPacket->type)) ));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::HandleDisconnect(SerialPacket* const) {
|
|
||||||
SetNextScene(SceneList::CLEANUP);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: more network handlers
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Server control
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InCombat::RequestSynchronize() {
|
|
||||||
ClientPacket newPacket;
|
|
||||||
|
|
||||||
//request a sync
|
|
||||||
newPacket.type = SerialPacketType::SYNCHRONIZE;
|
|
||||||
newPacket.clientIndex = clientIndex;
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::SendPlayerUpdate() {
|
|
||||||
CharacterPacket newPacket;
|
|
||||||
|
|
||||||
//pack the packet
|
|
||||||
newPacket.type = SerialPacketType::CHARACTER_UPDATE;
|
|
||||||
|
|
||||||
newPacket.characterIndex = characterIndex;
|
|
||||||
//handle, avatar
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
// newPacket.roomIndex = localCharacter->roomIndex;
|
|
||||||
// newPacket.origin = localCharacter->origin;
|
|
||||||
// newPacket.motion = localCharacter->motion;
|
|
||||||
// newPacket.stats = localCharacter->stats;
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::RequestDisconnect() {
|
|
||||||
ClientPacket newPacket;
|
|
||||||
|
|
||||||
//send a disconnect request
|
|
||||||
newPacket.type = SerialPacketType::DISCONNECT;
|
|
||||||
newPacket.clientIndex = clientIndex;
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InCombat::RequestShutdown() {
|
|
||||||
ClientPacket newPacket;
|
|
||||||
|
|
||||||
//send a shutdown request
|
|
||||||
newPacket.type = SerialPacketType::SHUTDOWN;
|
|
||||||
newPacket.clientIndex = clientIndex;
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
@@ -1,479 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 "channels.hpp"
|
|
||||||
#include "utility.hpp"
|
|
||||||
#include "config_utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cmath>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
InWorld::InWorld(
|
|
||||||
UDPNetworkUtility* const argNetwork,
|
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex,
|
|
||||||
int* const argCharacterIndex,
|
|
||||||
CharacterMap* argCharacterMap
|
|
||||||
):
|
|
||||||
network(*argNetwork),
|
|
||||||
clientIndex(*argClientIndex),
|
|
||||||
accountIndex(*argAccountIndex),
|
|
||||||
characterIndex(*argCharacterIndex),
|
|
||||||
characterMap(*argCharacterMap)
|
|
||||||
{
|
|
||||||
ConfigUtility& config = ConfigUtility::GetSingleton();
|
|
||||||
|
|
||||||
//setup the utility objects
|
|
||||||
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
|
||||||
buttonImage.SetClipH(buttonImage.GetClipH()/3);
|
|
||||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
|
||||||
|
|
||||||
//pass the utility objects
|
|
||||||
disconnectButton.SetImage(&buttonImage);
|
|
||||||
disconnectButton.SetFont(&font);
|
|
||||||
shutDownButton.SetImage(&buttonImage);
|
|
||||||
shutDownButton.SetFont(&font);
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
disconnectButton.SetX(50);
|
|
||||||
disconnectButton.SetY(50 + buttonImage.GetClipH() * 0);
|
|
||||||
shutDownButton.SetX(50);
|
|
||||||
shutDownButton.SetY(50 + buttonImage.GetClipH() * 1);
|
|
||||||
|
|
||||||
//set the button texts
|
|
||||||
disconnectButton.SetText("Disconnect");
|
|
||||||
shutDownButton.SetText("Shut Down");
|
|
||||||
|
|
||||||
//load the tilesheet
|
|
||||||
//TODO: add the tilesheet to the map system?
|
|
||||||
//TODO: Tile size and tile sheet should be loaded elsewhere
|
|
||||||
tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 32, 32);
|
|
||||||
|
|
||||||
//request a sync
|
|
||||||
RequestSynchronize();
|
|
||||||
|
|
||||||
//debug
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
InWorld::~InWorld() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InWorld::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::Update(double delta) {
|
|
||||||
//suck in and process all waiting packets
|
|
||||||
SerialPacket* packetBuffer = static_cast<SerialPacket*>(malloc(MAX_PACKET_SIZE));
|
|
||||||
while(network.Receive(packetBuffer)) {
|
|
||||||
HandlePacket(packetBuffer);
|
|
||||||
}
|
|
||||||
free(static_cast<void*>(packetBuffer));
|
|
||||||
|
|
||||||
//update the characters
|
|
||||||
for (auto& it : characterMap) {
|
|
||||||
it.second.Update(delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
//check the map
|
|
||||||
UpdateMap();
|
|
||||||
|
|
||||||
//skip the rest
|
|
||||||
if (!localCharacter) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//check for collisions with the map
|
|
||||||
BoundingBox wallBounds = {0, 0, tileSheet.GetTileW(), tileSheet.GetTileH()};
|
|
||||||
const int xCount = localCharacter->GetBounds().w / wallBounds.w + 1;
|
|
||||||
const int yCount = localCharacter->GetBounds().h / wallBounds.h + 1;
|
|
||||||
|
|
||||||
for (int i = -1; i <= xCount; ++i) {
|
|
||||||
for (int j = -1; j <= yCount; ++j) {
|
|
||||||
//set the wall's position
|
|
||||||
wallBounds.x = wallBounds.w * i + snapToBase((double)wallBounds.w, localCharacter->GetOrigin().x);
|
|
||||||
wallBounds.y = wallBounds.h * j + snapToBase((double)wallBounds.h, localCharacter->GetOrigin().y);
|
|
||||||
|
|
||||||
if (!regionPager.GetSolid(wallBounds.x / wallBounds.w, wallBounds.y / wallBounds.h)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((localCharacter->GetOrigin() + localCharacter->GetBounds()).CheckOverlap(wallBounds)) {
|
|
||||||
localCharacter->SetOrigin(localCharacter->GetOrigin() - (localCharacter->GetMotion() * delta));
|
|
||||||
localCharacter->SetMotion({0,0});
|
|
||||||
localCharacter->CorrectSprite();
|
|
||||||
SendPlayerUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//update the camera
|
|
||||||
camera.x = localCharacter->GetOrigin().x - camera.marginX;
|
|
||||||
camera.y = localCharacter->GetOrigin().y - camera.marginY;
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::RenderFrame() {
|
|
||||||
// SDL_FillRect(GetScreen(), 0, 0);
|
|
||||||
Render(GetScreen());
|
|
||||||
SDL_Flip(GetScreen());
|
|
||||||
fps.Calculate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::Render(SDL_Surface* const screen) {
|
|
||||||
//draw the map
|
|
||||||
for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
|
|
||||||
tileSheet.DrawRegionTo(screen, &(*it), camera.x, camera.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
//draw characters
|
|
||||||
for (auto& it : characterMap) {
|
|
||||||
//BUG: #29 drawing order according to Y origin
|
|
||||||
it.second.DrawTo(screen, camera.x, camera.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
//draw UI
|
|
||||||
disconnectButton.DrawTo(screen);
|
|
||||||
shutDownButton.DrawTo(screen);
|
|
||||||
font.DrawStringTo(to_string_custom(fps.GetFrameRate()), screen, 0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InWorld::QuitEvent() {
|
|
||||||
//exit the game AND the server
|
|
||||||
RequestDisconnect();
|
|
||||||
SetNextScene(SceneList::QUIT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
|
||||||
disconnectButton.MouseMotion(motion);
|
|
||||||
shutDownButton.MouseMotion(motion);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
|
||||||
disconnectButton.MouseButtonDown(button);
|
|
||||||
shutDownButton.MouseButtonDown(button);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|
||||||
if (disconnectButton.MouseButtonUp(button) == Button::State::HOVER) {
|
|
||||||
RequestDisconnect();
|
|
||||||
}
|
|
||||||
if (shutDownButton.MouseButtonUp(button) == Button::State::HOVER) {
|
|
||||||
RequestShutDown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
|
||||||
if (!localCharacter) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//player movement
|
|
||||||
Vector2 motion = localCharacter->GetMotion();
|
|
||||||
switch(key.keysym.sym) {
|
|
||||||
case SDLK_LEFT:
|
|
||||||
motion.x -= CHARACTER_WALKING_SPEED;
|
|
||||||
break;
|
|
||||||
case SDLK_RIGHT:
|
|
||||||
motion.x += CHARACTER_WALKING_SPEED;
|
|
||||||
break;
|
|
||||||
case SDLK_UP:
|
|
||||||
motion.y -= CHARACTER_WALKING_SPEED;
|
|
||||||
break;
|
|
||||||
case SDLK_DOWN:
|
|
||||||
motion.y += CHARACTER_WALKING_SPEED;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
localCharacter->SetMotion(motion);
|
|
||||||
localCharacter->CorrectSprite();
|
|
||||||
SendPlayerUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
|
||||||
if (!localCharacter) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//player movement
|
|
||||||
Vector2 motion = localCharacter->GetMotion();
|
|
||||||
switch(key.keysym.sym) {
|
|
||||||
//NOTE: The use of min/max here are to prevent awkward movements
|
|
||||||
case SDLK_LEFT:
|
|
||||||
motion.x = std::min(motion.x + CHARACTER_WALKING_SPEED, 0.0);
|
|
||||||
break;
|
|
||||||
case SDLK_RIGHT:
|
|
||||||
motion.x = std::max(motion.x - CHARACTER_WALKING_SPEED, 0.0);
|
|
||||||
break;
|
|
||||||
case SDLK_UP:
|
|
||||||
motion.y = std::min(motion.y + CHARACTER_WALKING_SPEED, 0.0);
|
|
||||||
break;
|
|
||||||
case SDLK_DOWN:
|
|
||||||
motion.y = std::max(motion.y - CHARACTER_WALKING_SPEED, 0.0);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
localCharacter->SetMotion(motion);
|
|
||||||
localCharacter->CorrectSprite();
|
|
||||||
SendPlayerUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Network handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InWorld::HandlePacket(SerialPacket* const argPacket) {
|
|
||||||
switch(argPacket->type) {
|
|
||||||
case SerialPacketType::DISCONNECT:
|
|
||||||
HandleDisconnect(argPacket);
|
|
||||||
break;
|
|
||||||
case SerialPacketType::CHARACTER_NEW:
|
|
||||||
HandleCharacterNew(static_cast<CharacterPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
case SerialPacketType::CHARACTER_DELETE:
|
|
||||||
HandleCharacterDelete(static_cast<CharacterPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
case SerialPacketType::CHARACTER_UPDATE:
|
|
||||||
HandleCharacterUpdate(static_cast<CharacterPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
case SerialPacketType::REGION_CONTENT:
|
|
||||||
HandleRegionContent(static_cast<RegionPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
//handle errors
|
|
||||||
default:
|
|
||||||
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(static_cast<int>(argPacket->type)) ));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::HandleDisconnect(SerialPacket* const argPacket) {
|
|
||||||
SetNextScene(SceneList::CLEANUP);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::HandleCharacterNew(CharacterPacket* const argPacket) {
|
|
||||||
if (characterMap.find(argPacket->characterIndex) != characterMap.end()) {
|
|
||||||
throw(std::runtime_error("Cannot create duplicate characters"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//create the character object
|
|
||||||
Character& newCharacter = characterMap[argPacket->characterIndex];
|
|
||||||
|
|
||||||
//fill out the character's members
|
|
||||||
newCharacter.SetHandle(argPacket->handle);
|
|
||||||
newCharacter.SetAvatar(argPacket->avatar);
|
|
||||||
|
|
||||||
newCharacter.GetSprite()->LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + newCharacter.GetAvatar(), 4, 4);
|
|
||||||
|
|
||||||
newCharacter.SetOrigin(argPacket->origin);
|
|
||||||
newCharacter.SetMotion(argPacket->motion);
|
|
||||||
newCharacter.SetBounds({0, 16, 32, 32}); //TODO: magic numbers, fix this
|
|
||||||
|
|
||||||
(*newCharacter.GetStats()) = argPacket->stats;
|
|
||||||
|
|
||||||
//bookkeeping code
|
|
||||||
newCharacter.CorrectSprite();
|
|
||||||
|
|
||||||
//catch this client's player object
|
|
||||||
if (argPacket->accountIndex == accountIndex && !localCharacter) {
|
|
||||||
characterIndex = argPacket->characterIndex;
|
|
||||||
localCharacter = &newCharacter;
|
|
||||||
|
|
||||||
//setup the camera
|
|
||||||
//TODO: move this?
|
|
||||||
camera.width = GetScreen()->w;
|
|
||||||
camera.height = GetScreen()->h;
|
|
||||||
|
|
||||||
//center on the player's character
|
|
||||||
camera.marginX = (GetScreen()->w / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2);
|
|
||||||
camera.marginY = (GetScreen()->h / 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::HandleCharacterDelete(CharacterPacket* const argPacket) {
|
|
||||||
//TODO: authenticate when own character is being deleted (linked to a TODO in the server)
|
|
||||||
|
|
||||||
//catch this client's player object
|
|
||||||
if (argPacket->characterIndex == characterIndex) {
|
|
||||||
characterIndex = -1;
|
|
||||||
localCharacter = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
characterMap.erase(argPacket->characterIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::HandleCharacterUpdate(CharacterPacket* const argPacket) {
|
|
||||||
if (characterMap.find(argPacket->characterIndex) == characterMap.end()) {
|
|
||||||
std::cout << "Warning: HandleCharacterUpdate() is passing to HandleCharacterNew()" << std::endl;
|
|
||||||
HandleCharacterNew(argPacket);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Character& character = characterMap[argPacket->characterIndex];
|
|
||||||
|
|
||||||
//other characters moving
|
|
||||||
if (argPacket->characterIndex != characterIndex) {
|
|
||||||
character.SetOrigin(argPacket->origin);
|
|
||||||
character.SetMotion(argPacket->motion);
|
|
||||||
character.CorrectSprite();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::HandleRegionContent(RegionPacket* const argPacket) {
|
|
||||||
//replace existing regions
|
|
||||||
regionPager.UnloadRegion(argPacket->x, argPacket->y);
|
|
||||||
regionPager.PushRegion(argPacket->region);
|
|
||||||
|
|
||||||
//clean up after the serial code
|
|
||||||
delete argPacket->region;
|
|
||||||
argPacket->region = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Server control
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void InWorld::RequestSynchronize() {
|
|
||||||
ClientPacket newPacket;
|
|
||||||
|
|
||||||
//request a sync
|
|
||||||
newPacket.type = SerialPacketType::SYNCHRONIZE;
|
|
||||||
newPacket.clientIndex = clientIndex;
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
|
|
||||||
//TODO: location, range for sync request
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::SendPlayerUpdate() {
|
|
||||||
CharacterPacket newPacket;
|
|
||||||
|
|
||||||
//pack the packet
|
|
||||||
newPacket.type = SerialPacketType::CHARACTER_UPDATE;
|
|
||||||
|
|
||||||
newPacket.characterIndex = characterIndex;
|
|
||||||
//NOTE: omitting the handle and avatar here
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
newPacket.roomIndex = 0; //TODO: room index
|
|
||||||
newPacket.origin = localCharacter->GetOrigin();
|
|
||||||
newPacket.motion = localCharacter->GetMotion();
|
|
||||||
newPacket.stats = *localCharacter->GetStats();
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::RequestDisconnect() {
|
|
||||||
ClientPacket newPacket;
|
|
||||||
|
|
||||||
//send a disconnect request
|
|
||||||
newPacket.type = SerialPacketType::DISCONNECT;
|
|
||||||
newPacket.clientIndex = clientIndex;
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::RequestShutDown() {
|
|
||||||
ClientPacket newPacket;
|
|
||||||
|
|
||||||
//send a shutdown request
|
|
||||||
newPacket.type = SerialPacketType::SHUTDOWN;
|
|
||||||
newPacket.clientIndex = clientIndex;
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
void InWorld::RequestRegion(int roomIndex, int x, int y) {
|
|
||||||
RegionPacket packet;
|
|
||||||
|
|
||||||
//pack the region's data
|
|
||||||
packet.type = SerialPacketType::REGION_REQUEST;
|
|
||||||
packet.roomIndex = roomIndex;
|
|
||||||
packet.x = x;
|
|
||||||
packet.y = y;
|
|
||||||
|
|
||||||
network.SendTo(Channels::SERVER, &packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Utilities
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
//TODO: convert this into a more generic function?; using parameters for the bounds
|
|
||||||
void InWorld::UpdateMap() {
|
|
||||||
//these represent the zone of regions that the client needs loaded, including the mandatory buffers (+1/-1)
|
|
||||||
int xStart = snapToBase(REGION_WIDTH, camera.x/tileSheet.GetTileW()) - REGION_WIDTH;
|
|
||||||
int xEnd = snapToBase(REGION_WIDTH, (camera.x+camera.width)/tileSheet.GetTileW()) + REGION_WIDTH;
|
|
||||||
|
|
||||||
int yStart = snapToBase(REGION_HEIGHT, camera.y/tileSheet.GetTileH()) - REGION_HEIGHT;
|
|
||||||
int yEnd = snapToBase(REGION_HEIGHT, (camera.y+camera.height)/tileSheet.GetTileH()) + REGION_HEIGHT;
|
|
||||||
|
|
||||||
//prune distant regions
|
|
||||||
for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); /* EMPTY */) {
|
|
||||||
//check if the region is outside of this area
|
|
||||||
if (it->GetX() < xStart || it->GetX() > xEnd || it->GetY() < yStart || it->GetY() > yEnd) {
|
|
||||||
|
|
||||||
//clunky, but the alternative was time consuming
|
|
||||||
int tmpX = it->GetX();
|
|
||||||
int tmpY = it->GetY();
|
|
||||||
++it;
|
|
||||||
|
|
||||||
regionPager.UnloadRegion(tmpX, tmpY);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
|
|
||||||
//request empty regions within this zone
|
|
||||||
for (int i = xStart; i <= xEnd; i += REGION_WIDTH) {
|
|
||||||
for (int j = yStart; j <= yEnd; j += REGION_HEIGHT) {
|
|
||||||
if (!regionPager.FindRegion(i, j)) {
|
|
||||||
RequestRegion(0, i, j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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_
|
|
||||||
|
|
||||||
//maps
|
|
||||||
#include "region_pager_base.hpp"
|
|
||||||
|
|
||||||
//networking
|
|
||||||
#include "udp_network_utility.hpp"
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
#include "image.hpp"
|
|
||||||
#include "raster_font.hpp"
|
|
||||||
#include "button.hpp"
|
|
||||||
#include "tile_sheet.hpp"
|
|
||||||
|
|
||||||
//common
|
|
||||||
#include "frame_rate.hpp"
|
|
||||||
|
|
||||||
#include "character.hpp"
|
|
||||||
|
|
||||||
//client
|
|
||||||
#include "base_scene.hpp"
|
|
||||||
|
|
||||||
//STL
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
class InWorld : public BaseScene {
|
|
||||||
public:
|
|
||||||
//Public access members
|
|
||||||
InWorld(
|
|
||||||
UDPNetworkUtility* const argNetwork,
|
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex,
|
|
||||||
int* const argCharacterIndex,
|
|
||||||
CharacterMap* argCharacterMap
|
|
||||||
);
|
|
||||||
~InWorld();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
//Frame loop
|
|
||||||
void FrameStart();
|
|
||||||
void Update(double delta);
|
|
||||||
void FrameEnd();
|
|
||||||
void RenderFrame();
|
|
||||||
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&);
|
|
||||||
|
|
||||||
//Network handlers
|
|
||||||
void HandlePacket(SerialPacket* const);
|
|
||||||
void HandleDisconnect(SerialPacket* const);
|
|
||||||
void HandleCharacterNew(CharacterPacket* const);
|
|
||||||
void HandleCharacterDelete(CharacterPacket* const);
|
|
||||||
void HandleCharacterUpdate(CharacterPacket* const);
|
|
||||||
void HandleRegionContent(RegionPacket* const);
|
|
||||||
|
|
||||||
//Server control
|
|
||||||
void RequestSynchronize();
|
|
||||||
void SendPlayerUpdate();
|
|
||||||
void RequestDisconnect();
|
|
||||||
void RequestShutDown();
|
|
||||||
void RequestRegion(int roomIndex, int x, int y);
|
|
||||||
|
|
||||||
//utilities
|
|
||||||
void UpdateMap();
|
|
||||||
|
|
||||||
//shared parameters
|
|
||||||
UDPNetworkUtility& network;
|
|
||||||
int& clientIndex;
|
|
||||||
int& accountIndex;
|
|
||||||
int& characterIndex;
|
|
||||||
CharacterMap& characterMap;
|
|
||||||
|
|
||||||
//graphics
|
|
||||||
Image buttonImage;
|
|
||||||
RasterFont font;
|
|
||||||
TileSheet tileSheet;
|
|
||||||
|
|
||||||
//map
|
|
||||||
RegionPagerBase regionPager;
|
|
||||||
|
|
||||||
//UI
|
|
||||||
Button disconnectButton;
|
|
||||||
Button shutDownButton;
|
|
||||||
//TODO: Review the camera
|
|
||||||
struct {
|
|
||||||
int x = 0, y = 0;
|
|
||||||
int width = 0, height = 0;
|
|
||||||
int marginX = 0, marginY = 0;
|
|
||||||
} camera;
|
|
||||||
FrameRate fps;
|
|
||||||
|
|
||||||
//game
|
|
||||||
Character* localCharacter = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,245 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 "utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Public access members
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
LobbyMenu::LobbyMenu(
|
|
||||||
UDPNetworkUtility* const argNetwork,
|
|
||||||
int* const argClientIndex,
|
|
||||||
int* const argAccountIndex
|
|
||||||
):
|
|
||||||
network(*argNetwork),
|
|
||||||
clientIndex(*argClientIndex),
|
|
||||||
accountIndex(*argAccountIndex)
|
|
||||||
{
|
|
||||||
//setup the utility objects
|
|
||||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
|
||||||
image.SetClipH(image.GetClipH()/3);
|
|
||||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
|
||||||
|
|
||||||
//pass the utility objects
|
|
||||||
search.SetImage(&image);
|
|
||||||
search.SetFont(&font);
|
|
||||||
join.SetImage(&image);
|
|
||||||
join.SetFont(&font);
|
|
||||||
back.SetImage(&image);
|
|
||||||
back.SetFont(&font);
|
|
||||||
|
|
||||||
//set the button positions
|
|
||||||
search.SetX(50);
|
|
||||||
search.SetY(50 + image.GetClipH() * 0);
|
|
||||||
join.SetX(50);
|
|
||||||
join.SetY(50 + image.GetClipH() * 1);
|
|
||||||
back.SetX(50);
|
|
||||||
back.SetY(50 + image.GetClipH() * 2);
|
|
||||||
|
|
||||||
//set the button texts
|
|
||||||
search.SetText("Search");
|
|
||||||
join.SetText("Join");
|
|
||||||
back.SetText("Back");
|
|
||||||
|
|
||||||
//set the server list's position
|
|
||||||
listBox = {300, 50, 200, font.GetCharH()};
|
|
||||||
|
|
||||||
//BUGFIX: Eat incoming packets
|
|
||||||
while(network.Receive());
|
|
||||||
}
|
|
||||||
|
|
||||||
LobbyMenu::~LobbyMenu() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Frame loop
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void LobbyMenu::FrameStart() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::Update(double delta) {
|
|
||||||
//suck in and process all waiting packets
|
|
||||||
SerialPacket* packetBuffer = static_cast<SerialPacket*>(malloc(MAX_PACKET_SIZE));
|
|
||||||
while(network.Receive(packetBuffer)) {
|
|
||||||
HandlePacket(packetBuffer);
|
|
||||||
}
|
|
||||||
free(static_cast<void*>(packetBuffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::FrameEnd() {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::Render(SDL_Surface* const screen) {
|
|
||||||
//TODO: I need a proper UI system for the entire client and the editor
|
|
||||||
|
|
||||||
//UI
|
|
||||||
search.DrawTo(screen);
|
|
||||||
join.DrawTo(screen);
|
|
||||||
back.DrawTo(screen);
|
|
||||||
|
|
||||||
//TODO: draw headers for the server list
|
|
||||||
for (int i = 0; i < serverInfo.size(); i++) {
|
|
||||||
//draw the selected server's highlight
|
|
||||||
if (selection == &serverInfo[i]) {
|
|
||||||
SDL_Rect r = listBox;
|
|
||||||
r.y += i * listBox.h;
|
|
||||||
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 127, 39));
|
|
||||||
}
|
|
||||||
|
|
||||||
//draw the server name
|
|
||||||
font.DrawStringTo(serverInfo[i].name, screen, listBox.x, listBox.y + i*listBox.h);
|
|
||||||
|
|
||||||
//draw the player count
|
|
||||||
font.DrawStringTo(to_string_custom(serverInfo[i].playerCount), screen, listBox.x + listBox.w, listBox.y + i*listBox.h);
|
|
||||||
|
|
||||||
//compatible?
|
|
||||||
if (!serverInfo[i].compatible) {
|
|
||||||
font.DrawStringTo("?", screen, listBox.x - font.GetCharW(), listBox.y + i*listBox.h);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: ping/delay?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Event handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
|
||||||
search.MouseMotion(motion);
|
|
||||||
join.MouseMotion(motion);
|
|
||||||
back.MouseMotion(motion);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
|
||||||
search.MouseButtonDown(button);
|
|
||||||
join.MouseButtonDown(button);
|
|
||||||
back.MouseButtonDown(button);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|
||||||
if (search.MouseButtonUp(button) == Button::State::HOVER) {
|
|
||||||
//broadcast to the network, or a specific server
|
|
||||||
SerialPacket packet;
|
|
||||||
packet.type = SerialPacketType::BROADCAST_REQUEST;
|
|
||||||
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet);
|
|
||||||
|
|
||||||
//reset the server list
|
|
||||||
serverInfo.clear();
|
|
||||||
selection = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
|
|
||||||
//pack the packet
|
|
||||||
ClientPacket packet;
|
|
||||||
packet.type = SerialPacketType::JOIN_REQUEST;
|
|
||||||
strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//join the selected server
|
|
||||||
network.SendTo(&selection->address, &packet);
|
|
||||||
selection = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (back.MouseButtonUp(button) == Button::State::HOVER) {
|
|
||||||
SetNextScene(SceneList::MAINMENU);
|
|
||||||
}
|
|
||||||
|
|
||||||
else if (
|
|
||||||
//has the user selected a server on the list?
|
|
||||||
//TODO: replace with regular collision checker
|
|
||||||
button.x > listBox.x &&
|
|
||||||
button.x < listBox.x + listBox.w &&
|
|
||||||
button.y > listBox.y &&
|
|
||||||
button.y < listBox.y + listBox.h * serverInfo.size()
|
|
||||||
) {
|
|
||||||
selection = &serverInfo[(button.y - listBox.y)/listBox.h];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
selection = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Network handlers
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
|
|
||||||
switch(argPacket->type) {
|
|
||||||
case SerialPacketType::BROADCAST_RESPONSE:
|
|
||||||
HandleBroadcastResponse(static_cast<ServerPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
case SerialPacketType::JOIN_RESPONSE:
|
|
||||||
HandleJoinResponse(static_cast<ClientPacket*>(argPacket));
|
|
||||||
break;
|
|
||||||
//handle errors
|
|
||||||
default:
|
|
||||||
throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in LobbyMenu: " + to_string_custom(static_cast<int>(argPacket->type)) ));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
|
|
||||||
//extract the data
|
|
||||||
ServerInformation server;
|
|
||||||
server.address = argPacket->srcAddress;
|
|
||||||
server.name = argPacket->name;
|
|
||||||
server.playerCount = argPacket->playerCount;
|
|
||||||
server.version = argPacket->version;
|
|
||||||
|
|
||||||
//Checking compatibility
|
|
||||||
server.compatible = server.version == NETWORK_VERSION;
|
|
||||||
|
|
||||||
//push
|
|
||||||
serverInfo.push_back(server);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
|
|
||||||
clientIndex = argPacket->clientIndex;
|
|
||||||
accountIndex = argPacket->accountIndex;
|
|
||||||
network.Bind(&argPacket->srcAddress, Channels::SERVER);
|
|
||||||
SetNextScene(SceneList::INWORLD);
|
|
||||||
|
|
||||||
//send this player's character info
|
|
||||||
CharacterPacket newPacket;
|
|
||||||
newPacket.type = SerialPacketType::CHARACTER_NEW;
|
|
||||||
strncpy(newPacket.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE);
|
|
||||||
strncpy(newPacket.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE);
|
|
||||||
newPacket.accountIndex = accountIndex;
|
|
||||||
network.SendTo(Channels::SERVER, &newPacket);
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. .. ../../common/gameplay ../../common/graphics ../../common/map ../../common/network ../../common/network/packet ../../common/network/serial ../../common/ui ../../common/utilities
|
|
||||||
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 $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -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, 2014
|
/* 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
|
||||||
@@ -23,25 +23,31 @@
|
|||||||
#define 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();
|
SplashScreen();
|
||||||
~SplashScreen();
|
~SplashScreen();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//Frame loop
|
/* Frame loop */
|
||||||
void Update(double delta);
|
void RunFrame(double delta);
|
||||||
void Render(SDL_Surface* const);
|
void RenderFrame();
|
||||||
|
void LoadResources();
|
||||||
|
|
||||||
//members
|
bool loaded = false;
|
||||||
std::chrono::steady_clock::time_point startTick;
|
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||||
|
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||||
Image logo;
|
Image logo;
|
||||||
|
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -23,6 +23,13 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#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) {
|
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||||
return CalcState(motion.x, motion.y, motion.state & SDL_BUTTON_LMASK);
|
return CalcState(motion.x, motion.y, motion.state & SDL_BUTTON_LMASK);
|
||||||
}
|
}
|
||||||
@@ -42,40 +49,41 @@ Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Button::DrawTo(SDL_Surface* const dest) {
|
void Button::DrawTo(SDL_Surface* const dest) {
|
||||||
if (!image || !font) {
|
image.DrawTo(dest, x, y);
|
||||||
throw(std::runtime_error("Surface not set for Button"));
|
font.DrawStringTo(text, dest, textX + x, textY + y);
|
||||||
}
|
}
|
||||||
image->SetClipY(state * image->GetClipH());
|
|
||||||
image->DrawTo(dest, x, y);
|
void Button::SetSurfaces(SDL_Surface* bg, SDL_Surface* fg) {
|
||||||
font->DrawStringTo(text, dest, textX + x, textY + y);
|
//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) {
|
std::string Button::SetText(std::string t) {
|
||||||
if (!image || !font) {
|
|
||||||
throw(std::runtime_error("Surface not set for Button"));
|
|
||||||
}
|
|
||||||
//one line, cache the position
|
//one line, cache the position
|
||||||
text = t;
|
text = t;
|
||||||
textX = (image->GetClipW() / 2) - (font->GetCharW() * text.size() / 2);
|
textX = (image.GetClipW() / 2) - (font.GetCharW() * text.size() / 2);
|
||||||
textY = (image->GetClipH() / 2) - (font->GetCharH() / 2);
|
textY = (image.GetClipH() / 2) - (font.GetCharH() / 2);
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) {
|
Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) {
|
||||||
if (!image || !font) {
|
if (i < x || i > (x + image.GetClipW()) ||
|
||||||
throw(std::runtime_error("Surface not set for Button"));
|
j < y || j > (y + image.GetClipH())
|
||||||
}
|
|
||||||
//if out of bounds
|
|
||||||
if (i < x || i >= (x + image->GetClipW()) ||
|
|
||||||
j < y || j >= (y + image->GetClipH())
|
|
||||||
) {
|
) {
|
||||||
|
image.SetClipY(0);
|
||||||
return state = State::NORMAL;
|
return state = State::NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leftPressed) {
|
if (leftPressed) {
|
||||||
|
image.SetClipY(image.GetClipH()*2);
|
||||||
return state = State::PRESSED;
|
return state = State::PRESSED;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
image.SetClipY(image.GetClipH());
|
||||||
return state = State::HOVER;
|
return state = State::HOVER;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -27,66 +27,52 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/* 3-phases, no toggle, centred text
|
//3-phases, no toggle, centred text
|
||||||
* This class uses the size of the provided image as its bounds. Also,
|
|
||||||
* The provided image should be formatted correctly.
|
|
||||||
*
|
|
||||||
* The button's image should be divided into 3 sections virtucally,
|
|
||||||
* which act as the different button images. The clip width & height of the
|
|
||||||
* Image should be set manually, and the height should be 1/3 of the total
|
|
||||||
* graphical data.
|
|
||||||
*/
|
|
||||||
class Button {
|
class Button {
|
||||||
public:
|
public:
|
||||||
enum State {
|
enum class State {
|
||||||
NORMAL = 0, HOVER = 1, PRESSED = 2
|
NORMAL, HOVER, PRESSED
|
||||||
};
|
};
|
||||||
|
|
||||||
Button() = default;
|
Button() = default;
|
||||||
~Button() = default;
|
Button(Sint16 x, Sint16 y, SDL_Surface* bg, SDL_Surface* fg, std::string t = "") { Setup(x, y, bg, fg, t); }
|
||||||
|
|
||||||
//handle input
|
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 MouseMotion(SDL_MouseMotionEvent const&);
|
||||||
State MouseButtonDown(SDL_MouseButtonEvent const&);
|
State MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||||
State MouseButtonUp(SDL_MouseButtonEvent const&);
|
State MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||||
|
State GetState() const { return state; }
|
||||||
|
|
||||||
//yet another draw function
|
//yet another draw function
|
||||||
void DrawTo(SDL_Surface* const);
|
void DrawTo(SDL_Surface* const);
|
||||||
|
|
||||||
//accessors and mutators
|
//simple accessors and mutators
|
||||||
Image* SetImage(Image* const ptr) { return image = ptr; }
|
|
||||||
Image* GetImage() { return image; }
|
|
||||||
RasterFont* SetFont(RasterFont* const ptr) { return font = ptr; }
|
|
||||||
RasterFont* GetFont() { return font; }
|
|
||||||
|
|
||||||
Sint16 SetX(Sint16 i) { return x = i; }
|
Sint16 SetX(Sint16 i) { return x = i; }
|
||||||
Sint16 SetY(Sint16 i) { return y = i; }
|
Sint16 SetY(Sint16 i) { return y = i; }
|
||||||
Sint16 GetX() const { return x; }
|
Sint16 GetX() const { return x; }
|
||||||
Sint16 GetY() const { return y; }
|
Sint16 GetY() const { return y; }
|
||||||
|
|
||||||
Sint16 SetTextX(Sint16 i) { return textX = i; }
|
void SetSurfaces(SDL_Surface* bg, SDL_Surface* fg);
|
||||||
Sint16 SetTextY(Sint16 i) { return textY = i; }
|
|
||||||
Sint16 GetTextX() const { return textX; }
|
|
||||||
Sint16 GetTextY() const { return textY; }
|
|
||||||
|
|
||||||
State SetState(State s) { return state = s; }
|
std::string SetText(std::string t);
|
||||||
State GetState() const { return state; }
|
|
||||||
|
|
||||||
std::string SetText(std::string);
|
|
||||||
std::string GetText() const { return text; }
|
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:
|
private:
|
||||||
State CalcState(Sint16 x, Sint16 y, bool leftPressed);
|
State CalcState(Sint16 x, Sint16 y, bool leftPressed);
|
||||||
|
|
||||||
//point to the provided external objects
|
|
||||||
Image* image = nullptr;
|
|
||||||
RasterFont* font = nullptr;
|
|
||||||
|
|
||||||
//positions
|
|
||||||
Sint16 x = 0, y = 0;
|
Sint16 x = 0, y = 0;
|
||||||
Sint16 textX = 0, textY = 0;
|
Sint16 textX = 0, textY = 0; //prevent recalc every loop
|
||||||
|
Image image;
|
||||||
//
|
RasterFont font;
|
||||||
State state = State::NORMAL;
|
State state = State::NORMAL;
|
||||||
std::string text;
|
std::string text;
|
||||||
};
|
};
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,41 +21,28 @@
|
|||||||
*/
|
*/
|
||||||
#include "config_utility.hpp"
|
#include "config_utility.hpp"
|
||||||
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <fstream>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
void ConfigUtility::Load(std::string fname) {
|
using namespace std;
|
||||||
//clear the stored configuration
|
|
||||||
configMap.clear();
|
|
||||||
//pass to the recursive method
|
|
||||||
configMap = Read(fname);
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigUtility::table_t ConfigUtility::Read(std::string fname) {
|
void ConfigUtility::Load(string fname) {
|
||||||
//read in and return this file's data
|
ifstream is(fname);
|
||||||
table_t retTable;
|
|
||||||
std::ifstream is(fname);
|
|
||||||
|
|
||||||
if (!is.is_open()) {
|
if (!is.is_open()) {
|
||||||
std::string msg;
|
throw(runtime_error("Failed to open config file"));
|
||||||
msg += "Failed to open a config file: ";
|
|
||||||
msg += fname;
|
|
||||||
throw(std::runtime_error(msg));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string key, val;
|
string key, val;
|
||||||
|
|
||||||
while(true) { //forever
|
for (;;) { //forever
|
||||||
//eat whitespace
|
//eat whitespace
|
||||||
while(isspace(is.peek())) {
|
while(isspace(is.peek()))
|
||||||
is.ignore();
|
is.ignore();
|
||||||
}
|
|
||||||
|
|
||||||
//end of file
|
//end of file
|
||||||
if (is.eof()) {
|
if (is.eof())
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
//skip comment lines
|
//skip comment lines
|
||||||
if (is.peek() == '#') {
|
if (is.peek() == '#') {
|
||||||
@@ -76,55 +63,14 @@ ConfigUtility::table_t ConfigUtility::Read(std::string fname) {
|
|||||||
while(key.size() && isspace(*(key.end()-1))) key.erase(key.end() - 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);
|
while(val.size() && isspace(*(val.end()-1))) val.erase(val.end() - 1);
|
||||||
|
|
||||||
//disallow empty/wiped values
|
//allow empty/wiped values
|
||||||
if (key.size() == 0) {
|
if (key.size() == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//save the pair
|
//save the pair
|
||||||
retTable[key] = val;
|
table[key] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
is.close();
|
is.close();
|
||||||
|
|
||||||
//load in any subordinate config files
|
|
||||||
//TODO: Possibility of nesting config levels?
|
|
||||||
if (retTable.find("config.next") != retTable.end()) {
|
|
||||||
table_t subTable = Read(retTable["config.next"]);
|
|
||||||
retTable.insert(subTable.begin(), subTable.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
return retTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------
|
|
||||||
//Convert to a type
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
std::string& ConfigUtility::String(std::string s) {
|
|
||||||
return configMap[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
int ConfigUtility::Integer(std::string s) {
|
|
||||||
table_t::iterator it = configMap.find(s);
|
|
||||||
if (it == configMap.end()) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return atoi(it->second.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
double ConfigUtility::Double(std::string s) {
|
|
||||||
table_t::iterator it = configMap.find(s);
|
|
||||||
if (it == configMap.end()) {
|
|
||||||
return 0.0;
|
|
||||||
}
|
|
||||||
return atof(it->second.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ConfigUtility::Boolean(std::string s) {
|
|
||||||
table_t::iterator it = configMap.find(s);
|
|
||||||
if (it == configMap.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return it->second == "true";
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -22,37 +22,47 @@
|
|||||||
#ifndef CONFIGUTILITY_HPP_
|
#ifndef CONFIGUTILITY_HPP_
|
||||||
#define CONFIGUTILITY_HPP_
|
#define CONFIGUTILITY_HPP_
|
||||||
|
|
||||||
#include "singleton.hpp"
|
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
class ConfigUtility : public Singleton<ConfigUtility> {
|
class ConfigUtility {
|
||||||
public:
|
public:
|
||||||
|
ConfigUtility() {}
|
||||||
|
ConfigUtility(std::string s) { Load(s); }
|
||||||
|
|
||||||
void Load(std::string fname);
|
void Load(std::string fname);
|
||||||
|
|
||||||
//convert to a type
|
std::string String(std::string s) {
|
||||||
std::string& String(std::string);
|
return table[s];
|
||||||
int Integer(std::string);
|
}
|
||||||
double Double(std::string);
|
const char* CString(std::string s) {
|
||||||
bool Boolean(std::string);
|
return table[s].c_str();
|
||||||
|
}
|
||||||
//shorthand
|
int Integer(std::string s) {
|
||||||
inline std::string& operator[](std::string s) { return configMap[s]; }
|
return atoi(table[s].c_str());
|
||||||
inline int Int(std::string s) { return Integer(s); }
|
}
|
||||||
inline bool Bool(std::string s) { return Boolean(s); }
|
double Double(std::string s) {
|
||||||
|
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:
|
||||||
typedef std::map<std::string, std::string> table_t;
|
std::map<std::string, std::string> table;
|
||||||
|
|
||||||
friend Singleton<ConfigUtility>;
|
|
||||||
|
|
||||||
ConfigUtility() = default;
|
|
||||||
~ConfigUtility() = default;
|
|
||||||
|
|
||||||
table_t Read(std::string fname);
|
|
||||||
|
|
||||||
table_t configMap;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,37 +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))
|
|
||||||
|
|
||||||
#output
|
|
||||||
OUTDIR=../..
|
|
||||||
OUT=$(addprefix $(OUTDIR)/,libcommon.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 $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
/* 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,14 +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.
|
||||||
*/
|
*/
|
||||||
#ifndef CLIENTDATA_HPP_
|
#ifndef DEFINES_HPP_
|
||||||
#define CLIENTDATA_HPP_
|
#define DEFINES_HPP_
|
||||||
|
|
||||||
#include "SDL/SDL_net.h"
|
#define GAME_CHANNEL 0
|
||||||
|
#define CHAT_CHANNEL 1
|
||||||
|
|
||||||
struct ClientData {
|
#define WALKING_SPEED 140
|
||||||
IPaddress address = {0,0};
|
|
||||||
//TODO: ping system?
|
enum class CardinalDirection {
|
||||||
|
NORTH, SOUTH, EAST, WEST
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
/* 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,27 +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.
|
||||||
*/
|
*/
|
||||||
#include "timer.hpp"
|
#include "frame_rate.hpp"
|
||||||
|
|
||||||
Timer::Timer(): start(Timer::Clock::now()) {
|
#include <chrono>
|
||||||
//
|
|
||||||
|
typedef std::chrono::high_resolution_clock Clock;
|
||||||
|
|
||||||
|
static int frameCount = 0;
|
||||||
|
static int lastFrameRate = 0;
|
||||||
|
static Clock::time_point tick = Clock::now();
|
||||||
|
|
||||||
|
int clockFrameRate() {
|
||||||
|
frameCount++;
|
||||||
|
if (Clock::now() - tick >= std::chrono::duration<int>(1)) {
|
||||||
|
lastFrameRate = frameCount;
|
||||||
|
frameCount = 0;
|
||||||
|
tick = Clock::now();
|
||||||
|
}
|
||||||
|
return lastFrameRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer::Timer(std::string s): name(s), start(Timer::Clock::now()) {
|
int getFrameRate() {
|
||||||
//
|
return lastFrameRate;
|
||||||
}
|
|
||||||
|
|
||||||
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::milliseconds>(t.GetTime()).count();
|
|
||||||
os << "ms";
|
|
||||||
return os;
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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,11 +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.
|
||||||
*/
|
*/
|
||||||
#ifndef CHANNELS_HPP_
|
#ifndef FRAMERATE_HPP_
|
||||||
#define CHANNELS_HPP_
|
#define FRAMERATE_HPP_
|
||||||
|
|
||||||
enum Channels {
|
int clockFrameRate();
|
||||||
SERVER = 0
|
int getFrameRate();
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 CHARACTERDEFINES_HPP_
|
|
||||||
#define CHARACTERDEFINES_HPP_
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
//the speeds that the characters move
|
|
||||||
constexpr double CHARACTER_WALKING_SPEED = 140.0;
|
|
||||||
constexpr double CHARACTER_WALKING_MOD = 1.0/sqrt(2.0);
|
|
||||||
|
|
||||||
//the bounding boxes for the characters
|
|
||||||
constexpr double CHARACTER_BOUNDS_WIDTH = 32.0;
|
|
||||||
constexpr double CHARACTER_BOUNDS_HEIGHT = 32.0;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 STATISTICS_HPP_
|
|
||||||
#define STATISTICS_HPP_
|
|
||||||
|
|
||||||
struct Statistics {
|
|
||||||
int level = 0;
|
|
||||||
int exp = 0;
|
|
||||||
int maxHP = 0;
|
|
||||||
int health = 0;
|
|
||||||
int maxMP = 0;
|
|
||||||
int mana = 0;
|
|
||||||
int attack = 0;
|
|
||||||
int defence = 0;
|
|
||||||
int intelligence = 0;
|
|
||||||
int resistance = 0;
|
|
||||||
int speed = 0;
|
|
||||||
float accuracy = 0.0;
|
|
||||||
float evasion = 0.0;
|
|
||||||
float luck = 0.0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,37 +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))
|
|
||||||
|
|
||||||
#output
|
|
||||||
OUTDIR=../..
|
|
||||||
OUT=$(addprefix $(OUTDIR)/,libcommon.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 $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 <stdexcept>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
void SpriteSheet::Update(double delta) {
|
|
||||||
if (delay && (tick += delta) >= delay) {
|
|
||||||
if (++xIndex >= xCount) {
|
|
||||||
xIndex = 0;
|
|
||||||
}
|
|
||||||
tick = 0;
|
|
||||||
}
|
|
||||||
image.SetClipX(xIndex * image.GetClipW());
|
|
||||||
image.SetClipY(yIndex * image.GetClipH());
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Surface* SpriteSheet::LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount) {
|
|
||||||
image.LoadSurface(fname);
|
|
||||||
|
|
||||||
xCount = xCellCount;
|
|
||||||
yCount = yCellCount;
|
|
||||||
|
|
||||||
image.SetClipW(image.GetSurface()->w / xCount);
|
|
||||||
image.SetClipH(image.GetSurface()->h / yCount);
|
|
||||||
|
|
||||||
xIndex = yIndex = 0;
|
|
||||||
delay = tick = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) {
|
|
||||||
image.SetSurface(surface);
|
|
||||||
|
|
||||||
xCount = xCellCount;
|
|
||||||
yCount = yCellCount;
|
|
||||||
|
|
||||||
image.SetClipW(image.GetSurface()->w / xCount);
|
|
||||||
image.SetClipH(image.GetSurface()->h / yCount);
|
|
||||||
|
|
||||||
xIndex = yIndex = 0;
|
|
||||||
delay = tick = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SpriteSheet::FreeSurface() {
|
|
||||||
image.FreeSurface();
|
|
||||||
xCount = yCount = 0;
|
|
||||||
xIndex = yIndex = 0;
|
|
||||||
delay = tick = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetXCount(Uint16 i) {
|
|
||||||
xIndex = 0;
|
|
||||||
return xCount = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetYCount(Uint16 i) {
|
|
||||||
yIndex = 0;
|
|
||||||
return yCount = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetXIndex(Uint16 i) {
|
|
||||||
if (i > xCount) {
|
|
||||||
std::ostringstream os;
|
|
||||||
os << "Cannot set x index to " << i;
|
|
||||||
throw(std::invalid_argument(os.str()));
|
|
||||||
}
|
|
||||||
return xIndex = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint16 SpriteSheet::SetYIndex(Uint16 i) {
|
|
||||||
if (i > yCount) {
|
|
||||||
std::ostringstream os;
|
|
||||||
os << "Cannot set y index to " << i;
|
|
||||||
throw(std::invalid_argument(os.str()));
|
|
||||||
}
|
|
||||||
return yIndex = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
double SpriteSheet::SetDelay(double d) {
|
|
||||||
tick = 0;
|
|
||||||
return delay = d;
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -22,88 +22,16 @@
|
|||||||
#include "image.hpp"
|
#include "image.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
Image& Image::operator=(Image const& rhs) {
|
|
||||||
//don't screw yourself
|
|
||||||
if (this == &rhs) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
FreeSurface();
|
|
||||||
|
|
||||||
//Copy the other Image's stuff
|
|
||||||
surface = rhs.surface;
|
|
||||||
clip = rhs.clip;
|
|
||||||
local = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Image& Image::operator=(Image&& rhs) {
|
|
||||||
//don't screw yourself
|
|
||||||
if (this == &rhs) {
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
FreeSurface();
|
|
||||||
|
|
||||||
//Steal the other Image's stuff
|
|
||||||
surface = rhs.surface;
|
|
||||||
clip = rhs.clip;
|
|
||||||
local = rhs.local;
|
|
||||||
|
|
||||||
rhs.surface = nullptr;
|
|
||||||
rhs.clip = {0, 0, 0, 0};
|
|
||||||
rhs.local = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Surface* Image::LoadSurface(std::string fname) {
|
SDL_Surface* Image::LoadSurface(std::string fname) {
|
||||||
FreeSurface();
|
|
||||||
SDL_Surface* p = SDL_LoadBMP(fname.c_str());
|
SDL_Surface* p = SDL_LoadBMP(fname.c_str());
|
||||||
if (!p) {
|
if (!p) {
|
||||||
std::ostringstream os;
|
throw(std::runtime_error(std::string() + "Failed to load file: " + fname));
|
||||||
os << "Failed to load file: " << fname;
|
|
||||||
throw(std::runtime_error(os.str()));
|
|
||||||
}
|
}
|
||||||
surface = p;
|
surface = p;
|
||||||
|
SetTransparentColor(255, 0, 255); //default
|
||||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||||
local = true;
|
local = true;
|
||||||
SetTransparentColor(255, 0, 255); //default
|
|
||||||
return surface;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Surface* Image::CreateSurface(Uint16 w, Uint16 h) {
|
|
||||||
FreeSurface();
|
|
||||||
Uint32 rmask, gmask, bmask, amask;
|
|
||||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
|
||||||
rmask = 0xff000000;
|
|
||||||
gmask = 0x00ff0000;
|
|
||||||
bmask = 0x0000ff00;
|
|
||||||
amask = 0x000000ff;
|
|
||||||
#else
|
|
||||||
rmask = 0x000000ff;
|
|
||||||
gmask = 0x0000ff00;
|
|
||||||
bmask = 0x00ff0000;
|
|
||||||
amask = 0xff000000;
|
|
||||||
#endif
|
|
||||||
SDL_Surface* p = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, rmask, gmask, bmask, amask);
|
|
||||||
if (!p) {
|
|
||||||
throw(std::runtime_error("Failed to create Image surface"));
|
|
||||||
}
|
|
||||||
surface = p;
|
|
||||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
|
||||||
local = true;
|
|
||||||
SetTransparentColor(255, 0, 255); //default
|
|
||||||
return surface;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Surface* Image::SetSurface(SDL_Surface* p) {
|
|
||||||
FreeSurface();
|
|
||||||
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;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,6 +44,16 @@ void Image::FreeSurface() {
|
|||||||
clip = {0, 0, 0, 0};
|
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) {
|
void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) {
|
||||||
if (!surface) {
|
if (!surface) {
|
||||||
throw(std::logic_error("No image surface to draw"));
|
throw(std::logic_error("No image surface to draw"));
|
||||||
@@ -128,9 +66,6 @@ void Image::SetTransparentColor(Uint8 r, Uint8 g, Uint8 b) {
|
|||||||
if (!surface) {
|
if (!surface) {
|
||||||
throw(std::logic_error("Failed to set the transparent color"));
|
throw(std::logic_error("Failed to set the transparent color"));
|
||||||
}
|
}
|
||||||
if (!local) {
|
|
||||||
throw(std::logic_error("Cannot set the transparent color of a non-local surface"));
|
|
||||||
}
|
|
||||||
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b));
|
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,8 +73,5 @@ void Image::ClearTransparentColor() {
|
|||||||
if (!surface) {
|
if (!surface) {
|
||||||
throw(std::logic_error("Failed to clear the transparent color"));
|
throw(std::logic_error("Failed to clear the transparent color"));
|
||||||
}
|
}
|
||||||
if (!local) {
|
|
||||||
throw(std::logic_error("Cannot clear the transparent color of a non-local surface"));
|
|
||||||
}
|
|
||||||
SDL_SetColorKey(surface, 0, 0);
|
SDL_SetColorKey(surface, 0, 0);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -28,21 +28,14 @@
|
|||||||
class Image {
|
class Image {
|
||||||
public:
|
public:
|
||||||
Image() = default;
|
Image() = default;
|
||||||
Image(Image const& rhs) { *this = rhs; }
|
|
||||||
Image(Image&& rhs) { *this = std::move(rhs); }
|
|
||||||
Image(std::string fname) { LoadSurface(fname); }
|
|
||||||
Image(Uint16 w, Uint16 h) { CreateSurface(w, h); }
|
|
||||||
Image(SDL_Surface* p) { SetSurface(p); }
|
Image(SDL_Surface* p) { SetSurface(p); }
|
||||||
~Image() { FreeSurface(); }
|
~Image() { FreeSurface(); }
|
||||||
|
|
||||||
Image& operator=(Image const&);
|
|
||||||
Image& operator=(Image&&);
|
|
||||||
|
|
||||||
SDL_Surface* LoadSurface(std::string fname);
|
SDL_Surface* LoadSurface(std::string fname);
|
||||||
SDL_Surface* CreateSurface(Uint16 w, Uint16 h);
|
void FreeSurface();
|
||||||
|
|
||||||
SDL_Surface* SetSurface(SDL_Surface*);
|
SDL_Surface* SetSurface(SDL_Surface*);
|
||||||
SDL_Surface* GetSurface() const { return surface; }
|
SDL_Surface* GetSurface() const { return surface; }
|
||||||
void FreeSurface();
|
|
||||||
|
|
||||||
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
||||||
|
|
||||||
@@ -60,7 +53,7 @@ public:
|
|||||||
Uint16 GetClipW() const { return clip.w; }
|
Uint16 GetClipW() const { return clip.w; }
|
||||||
Uint16 GetClipH() const { return clip.h; }
|
Uint16 GetClipH() const { return clip.h; }
|
||||||
|
|
||||||
bool GetLocal() const { return local; }
|
bool GetLocal() { return local; }
|
||||||
|
|
||||||
void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b);
|
void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b);
|
||||||
void ClearTransparentColor();
|
void ClearTransparentColor();
|
||||||
+33
-8
@@ -1,11 +1,36 @@
|
|||||||
all:
|
#config
|
||||||
$(MAKE) -C debugging
|
LOCALLIBS=
|
||||||
$(MAKE) -C gameplay
|
LIB=
|
||||||
$(MAKE) -C graphics
|
INCLUDES=
|
||||||
$(MAKE) -C map
|
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||||
$(MAKE) -C network
|
|
||||||
$(MAKE) -C ui
|
#source
|
||||||
$(MAKE) -C utilities
|
SRC=$(wildcard *.cpp)
|
||||||
|
|
||||||
|
#objects
|
||||||
|
OBJDIR=obj
|
||||||
|
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||||
|
|
||||||
|
#output
|
||||||
|
OUTDIR=../lib
|
||||||
|
OUT=$(addprefix $(OUTDIR)/,libCommon.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 $@ $<
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) *.o *.a *.exe
|
$(RM) *.o *.a *.exe
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. ../graphics ../utilities
|
|
||||||
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)/,libcommon.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 $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
Region::Region(int argX, int argY): x(argX), y(argY) {
|
|
||||||
if (x != snapToBase(REGION_WIDTH, x) || y != snapToBase(REGION_HEIGHT, y)) {
|
|
||||||
throw(std::invalid_argument("Region location is off grid"));
|
|
||||||
}
|
|
||||||
memset(tiles, 0, REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH*sizeof(type_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::Region(Region const& rhs): x(rhs.x), y(rhs.y) {
|
|
||||||
memcpy(tiles, rhs.tiles, REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH*sizeof(type_t));
|
|
||||||
solid = rhs.solid;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
|
|
||||||
return tiles[x][y][z] = v;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::type_t Region::GetTile(int x, int y, int z) {
|
|
||||||
return tiles[x][y][z];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Region::SetSolid(int x, int y, bool b) {
|
|
||||||
return solid[x * REGION_WIDTH + y] = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Region::GetSolid(int x, int y) {
|
|
||||||
return solid[x * REGION_WIDTH + y];
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 REGION_HPP_
|
|
||||||
#define REGION_HPP_
|
|
||||||
|
|
||||||
#include <bitset>
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
//the region's storage format
|
|
||||||
constexpr int REGION_WIDTH = 20;
|
|
||||||
constexpr int REGION_HEIGHT = 20;
|
|
||||||
constexpr int REGION_DEPTH = 3;
|
|
||||||
|
|
||||||
//the size of the solid map
|
|
||||||
constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0);
|
|
||||||
|
|
||||||
class Region {
|
|
||||||
public:
|
|
||||||
typedef unsigned char type_t;
|
|
||||||
|
|
||||||
Region() = delete;
|
|
||||||
Region(int x, int y);
|
|
||||||
Region(Region const&);
|
|
||||||
~Region() = default;
|
|
||||||
|
|
||||||
type_t SetTile(int x, int y, int z, type_t v);
|
|
||||||
type_t GetTile(int x, int y, int z);
|
|
||||||
|
|
||||||
bool SetSolid(int x, int y, bool b);
|
|
||||||
bool GetSolid(int x, int y);
|
|
||||||
|
|
||||||
//accessors
|
|
||||||
int GetX() const { return x; }
|
|
||||||
int GetY() const { return y; }
|
|
||||||
|
|
||||||
std::bitset<REGION_WIDTH*REGION_HEIGHT>* GetSolidBitset() { return &solid; }
|
|
||||||
private:
|
|
||||||
const int x;
|
|
||||||
const int y;
|
|
||||||
|
|
||||||
type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH];
|
|
||||||
std::bitset<REGION_WIDTH*REGION_HEIGHT> solid;
|
|
||||||
};
|
|
||||||
|
|
||||||
//the memory footprint of the tile and solid data; not including any metadata
|
|
||||||
constexpr int REGION_FOOTPRINT = REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + REGION_SOLID_FOOTPRINT;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,122 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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_api.hpp"
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
static int setTile(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
int ret = region->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTile(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
int ret = region->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setSolid(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = region->SetSolid(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_toboolean(L, 4));
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getSolid(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = region->GetSolid(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1);
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getX(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, region->GetX());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getY(lua_State* L) {
|
|
||||||
Region* region = reinterpret_cast<Region*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, region->GetY());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getWidth(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_WIDTH);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getHeight(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_HEIGHT);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getDepth(lua_State* L) {
|
|
||||||
lua_pushinteger(L, REGION_DEPTH);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int load(lua_State* L) {
|
|
||||||
//EMPTY
|
|
||||||
lua_pushboolean(L, false);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int save(lua_State* L) {
|
|
||||||
//EMPTY
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int create(lua_State* L) {
|
|
||||||
//EMPTY
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int unload(lua_State* L) {
|
|
||||||
//EMPTY
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const luaL_Reg regionLib[] = {
|
|
||||||
{"SetTile",setTile},
|
|
||||||
{"GetTile",getTile},
|
|
||||||
{"SetSolid",setSolid},
|
|
||||||
{"GetSolid",getSolid},
|
|
||||||
{"GetX",getX},
|
|
||||||
{"GetY",getY},
|
|
||||||
{"GetWidth",getWidth},
|
|
||||||
{"GetHeight",getHeight},
|
|
||||||
{"GetDepth",getDepth},
|
|
||||||
{"Load",load},
|
|
||||||
{"Save",save},
|
|
||||||
{"Create",create},
|
|
||||||
{"Unload",unload},
|
|
||||||
{nullptr, nullptr}
|
|
||||||
};
|
|
||||||
|
|
||||||
LUAMOD_API int openRegionAPI(lua_State* L) {
|
|
||||||
luaL_newlib(L, regionLib);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 REGIONAPI_HPP_
|
|
||||||
#define REGIONAPI_HPP_
|
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
|
||||||
|
|
||||||
#define TORTUGA_REGION_NAME "Region"
|
|
||||||
LUAMOD_API int openRegionAPI(lua_State* L);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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_pager_api.hpp"
|
|
||||||
|
|
||||||
#include "region_pager_lua.hpp"
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
//DOCS: These glue functions simply wrap RegionPagerLua's methods
|
|
||||||
|
|
||||||
static int setTile(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
int ret = pager->SetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTile(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
int ret = pager->GetTile(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
|
|
||||||
lua_pushinteger(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setSolid(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = pager->SetSolid(lua_tointeger(L, 2), lua_tointeger(L, 3), lua_toboolean(L, 4));
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getSolid(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
bool ret = pager->GetSolid(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushboolean(L, ret);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->GetRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int loadRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->LoadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int saveRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->SaveRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int createRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
Region* region = pager->CreateRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
lua_pushlightuserdata(L, region);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int unloadRegion(lua_State* L) {
|
|
||||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
|
||||||
pager->UnloadRegion(lua_tointeger(L, 2), lua_tointeger(L, 3));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const luaL_Reg regionPagerLib[] = {
|
|
||||||
{"SetTile", setTile},
|
|
||||||
{"GetTile", getTile},
|
|
||||||
{"SetSolid", setSolid},
|
|
||||||
{"GetSolid", getSolid},
|
|
||||||
{"GetRegion", getRegion},
|
|
||||||
{"LoadRegion", loadRegion},
|
|
||||||
{"SaveRegion", saveRegion},
|
|
||||||
{"CreateRegion", createRegion},
|
|
||||||
{"UnloadRegion", unloadRegion},
|
|
||||||
{nullptr, nullptr}
|
|
||||||
};
|
|
||||||
|
|
||||||
LUAMOD_API int openRegionPagerAPI(lua_State* L) {
|
|
||||||
luaL_newlib(L, regionPagerLib);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 REGIONPAGERAPI_HPP_
|
|
||||||
#define REGIONPAGERAPI_HPP_
|
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
|
||||||
|
|
||||||
#define TORTUGA_REGION_PAGER_NAME "RegionPager"
|
|
||||||
LUAMOD_API int openRegionPagerAPI(lua_State* L);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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_pager_base.hpp"
|
|
||||||
|
|
||||||
#include "utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
Region::type_t RegionPagerBase::GetTile(int x, int y, int z) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RegionPagerBase::SetSolid(int x, int y, int b) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->SetSolid(x - ptr->GetX(), y - ptr->GetY(), b);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RegionPagerBase::GetSolid(int x, int y) {
|
|
||||||
Region* ptr = GetRegion(x, y);
|
|
||||||
return ptr->GetSolid(x - ptr->GetX(), y - ptr->GetY());
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::GetRegion(int x, int y) {
|
|
||||||
x = snapToBase(REGION_WIDTH, x);
|
|
||||||
y = snapToBase(REGION_HEIGHT, y);
|
|
||||||
|
|
||||||
//get the region by various means
|
|
||||||
Region* ptr = nullptr;
|
|
||||||
ptr = FindRegion(x, y);
|
|
||||||
if (ptr) return ptr;
|
|
||||||
ptr = LoadRegion(x, y);
|
|
||||||
if (ptr) return ptr;
|
|
||||||
return CreateRegion(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::FindRegion(int x, int y) {
|
|
||||||
//find the region
|
|
||||||
std::list<Region>::iterator it = find_if(regionList.begin(), regionList.end(), [x, y](Region& region) -> bool {
|
|
||||||
return region.GetX() == x && region.GetY() == y;
|
|
||||||
});
|
|
||||||
return it != regionList.end() ? &(*it) : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::PushRegion(Region* const ptr) {
|
|
||||||
regionList.push_front(*ptr);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::LoadRegion(int x, int y) {
|
|
||||||
//TODO: load the region if possible
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::SaveRegion(int x, int y) {
|
|
||||||
//TODO: find & save the region
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerBase::CreateRegion(int x, int y) {
|
|
||||||
if (FindRegion(x, y)) {
|
|
||||||
throw(std::logic_error("Cannot overwrite an existing region"));
|
|
||||||
}
|
|
||||||
regionList.emplace_front(x, y);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegionPagerBase::UnloadRegion(int x, int y) {
|
|
||||||
//custom loop, not FindRegion()
|
|
||||||
regionList.remove_if([x, y](Region& region) -> bool { return region.GetX() == x && region.GetY() == y; });
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegionPagerBase::UnloadAll() {
|
|
||||||
regionList.clear();
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 REGIONPAGERBASE_HPP_
|
|
||||||
#define REGIONPAGERBASE_HPP_
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
#include <list>
|
|
||||||
|
|
||||||
class RegionPagerBase {
|
|
||||||
public:
|
|
||||||
RegionPagerBase() = default;
|
|
||||||
virtual ~RegionPagerBase() { UnloadAll(); };
|
|
||||||
|
|
||||||
//tile manipulation
|
|
||||||
virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v);
|
|
||||||
virtual Region::type_t GetTile(int x, int y, int z);
|
|
||||||
|
|
||||||
//solid manipulation
|
|
||||||
virtual bool SetSolid(int x, int y, int b);
|
|
||||||
virtual bool GetSolid(int x, int y);
|
|
||||||
|
|
||||||
//region manipulation
|
|
||||||
virtual Region* GetRegion(int x, int y);
|
|
||||||
virtual Region* FindRegion(int x, int y);
|
|
||||||
virtual Region* PushRegion(Region* const);
|
|
||||||
|
|
||||||
virtual Region* LoadRegion(int x, int y);
|
|
||||||
virtual Region* SaveRegion(int x, int y);
|
|
||||||
virtual Region* CreateRegion(int x, int y);
|
|
||||||
virtual void UnloadRegion(int x, int y);
|
|
||||||
|
|
||||||
virtual void UnloadAll();
|
|
||||||
|
|
||||||
//accessors & mutators
|
|
||||||
std::list<Region>* GetContainer() { return ®ionList; }
|
|
||||||
protected:
|
|
||||||
std::list<Region> regionList;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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_pager_lua.hpp"
|
|
||||||
|
|
||||||
#include "utility.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
Region* RegionPagerLua::LoadRegion(int x, int y) {
|
|
||||||
//load the region if possible
|
|
||||||
|
|
||||||
//something to work on
|
|
||||||
Region tmpRegion(x, y);
|
|
||||||
|
|
||||||
//API hook
|
|
||||||
lua_getglobal(lua, "Region");
|
|
||||||
lua_getfield(lua, -1, "Load");
|
|
||||||
lua_pushlightuserdata(lua, &tmpRegion);
|
|
||||||
if (lua_pcall(lua, 1, 1, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
//success or failure
|
|
||||||
if (!lua_toboolean(lua, -1)) {
|
|
||||||
lua_pop(lua, 2);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
lua_pop(lua, 2);
|
|
||||||
regionList.push_front(tmpRegion);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerLua::SaveRegion(int x, int y) {
|
|
||||||
//find & save the region
|
|
||||||
Region* ptr = FindRegion(x, y);
|
|
||||||
if (ptr) {
|
|
||||||
//API hook
|
|
||||||
lua_getglobal(lua, "Region");
|
|
||||||
lua_getfield(lua, -1, "Save");
|
|
||||||
lua_pushlightuserdata(lua, ptr);
|
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
}
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
Region* RegionPagerLua::CreateRegion(int x, int y) {
|
|
||||||
if (FindRegion(x, y)) {
|
|
||||||
throw(std::logic_error("Cannot overwrite an existing region"));
|
|
||||||
}
|
|
||||||
|
|
||||||
//something to work on
|
|
||||||
Region tmpRegion(x, y);
|
|
||||||
|
|
||||||
//API hook
|
|
||||||
lua_getglobal(lua, "Region");
|
|
||||||
lua_getfield(lua, -1, "Create");
|
|
||||||
lua_pushlightuserdata(lua, &tmpRegion);
|
|
||||||
//TODO: parameters
|
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
regionList.push_front(tmpRegion);
|
|
||||||
return ®ionList.front();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegionPagerLua::UnloadRegion(int x, int y) {
|
|
||||||
lua_getglobal(lua, "Region");
|
|
||||||
|
|
||||||
regionList.remove_if([&](Region& region) -> bool {
|
|
||||||
if (region.GetX() == x && region.GetY() == y) {
|
|
||||||
|
|
||||||
//API hook
|
|
||||||
lua_getfield(lua, -1, "Unload");
|
|
||||||
lua_pushlightuserdata(lua, ®ion);
|
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RegionPagerLua::UnloadAll() {
|
|
||||||
lua_getglobal(lua, "Region");
|
|
||||||
|
|
||||||
for (auto& it : regionList) {
|
|
||||||
//API hook
|
|
||||||
lua_getfield(lua, -1, "Unload");
|
|
||||||
lua_pushlightuserdata(lua, &it);
|
|
||||||
if (lua_pcall(lua, 1, 0, 0) != LUA_OK) {
|
|
||||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(lua, -1) ));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pop(lua, 1);
|
|
||||||
regionList.clear();
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 REGIONPAGERLUA_HPP_
|
|
||||||
#define REGIONPAGERLUA_HPP_
|
|
||||||
|
|
||||||
#include "region_pager_base.hpp"
|
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class RegionPagerLua : public RegionPagerBase {
|
|
||||||
public:
|
|
||||||
RegionPagerLua() = default;
|
|
||||||
~RegionPagerLua() = default;
|
|
||||||
|
|
||||||
//region manipulation
|
|
||||||
Region* LoadRegion(int x, int y) override;
|
|
||||||
Region* SaveRegion(int x, int y) override;
|
|
||||||
Region* CreateRegion(int x, int y) override;
|
|
||||||
void UnloadRegion(int x, int y) override;
|
|
||||||
|
|
||||||
void UnloadAll() override;
|
|
||||||
|
|
||||||
//accessors & mutators
|
|
||||||
lua_State* SetLuaState(lua_State* L) { return lua = L; }
|
|
||||||
lua_State* GetLuaState() { return lua; }
|
|
||||||
protected:
|
|
||||||
lua_State* lua = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 "tile_sheet.hpp"
|
|
||||||
|
|
||||||
void TileSheet::Load(std::string fname, int tileWidth, int tileHeight) {
|
|
||||||
image.LoadSurface(fname);
|
|
||||||
image.SetClipW(tileWidth);
|
|
||||||
image.SetClipH(tileHeight);
|
|
||||||
xCount = image.GetSurface()->w / image.GetClipW();
|
|
||||||
yCount = image.GetSurface()->h / image.GetClipH();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::Unload() {
|
|
||||||
image.FreeSurface();
|
|
||||||
xCount = yCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) {
|
|
||||||
//0 is invisible
|
|
||||||
if (tile == 0) return;
|
|
||||||
image.SetClipX((tile-1) % xCount * image.GetClipW());
|
|
||||||
image.SetClipY((tile-1) / xCount * image.GetClipH());
|
|
||||||
image.DrawTo(dest, x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY) {
|
|
||||||
Region::type_t tile = 0;
|
|
||||||
for (register int i = 0; i < REGION_WIDTH; ++i) {
|
|
||||||
for (register int j = 0; j < REGION_HEIGHT; ++j) {
|
|
||||||
for (register int k = 0; k < REGION_DEPTH; ++k) {
|
|
||||||
tile = region->GetTile(i, j, k);
|
|
||||||
//0 is invisible
|
|
||||||
if (tile == 0) continue;
|
|
||||||
image.SetClipX((tile-1) % xCount * image.GetClipW());
|
|
||||||
image.SetClipY((tile-1) / xCount * image.GetClipH());
|
|
||||||
image.DrawTo(dest,
|
|
||||||
(region->GetX() + i) * image.GetClipW() - camX,
|
|
||||||
(region->GetY() + j) * image.GetClipH() - camY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 TILESHEET_HPP_
|
|
||||||
#define TILESHEET_HPP_
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
#include "image.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class TileSheet {
|
|
||||||
public:
|
|
||||||
TileSheet() = default;
|
|
||||||
TileSheet(std::string f, int w, int h) { Load(f, w, h); }
|
|
||||||
~TileSheet() = default;
|
|
||||||
|
|
||||||
void Load(std::string fname, int tileWidth, int tileHeight);
|
|
||||||
void Unload();
|
|
||||||
|
|
||||||
void DrawTileTo(SDL_Surface* const dest, int x, int y, Region::type_t tile);
|
|
||||||
void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY);
|
|
||||||
|
|
||||||
//accessors
|
|
||||||
Image* GetImage() { return ℑ }
|
|
||||||
int GetXCount() { return xCount; }
|
|
||||||
int GetYCount() { return yCount; }
|
|
||||||
int GetTileW() { return image.GetClipW(); }
|
|
||||||
int GetTileH() { return image.GetClipH(); }
|
|
||||||
private:
|
|
||||||
Image image;
|
|
||||||
int xCount = 0, yCount = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 "tile_sheet_api.hpp"
|
|
||||||
|
|
||||||
#include "tile_sheet.hpp"
|
|
||||||
|
|
||||||
static int load(lua_State* L) {
|
|
||||||
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
|
|
||||||
sheet->Load(lua_tostring(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int unload(lua_State* L) {
|
|
||||||
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
|
|
||||||
sheet->Unload();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getXCount(lua_State* L) {
|
|
||||||
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, sheet->GetXCount());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getYCount(lua_State* L) {
|
|
||||||
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, sheet->GetYCount());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTileW(lua_State* L) {
|
|
||||||
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, sheet->GetTileW());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getTileH(lua_State* L) {
|
|
||||||
TileSheet* sheet = reinterpret_cast<TileSheet*>(lua_touserdata(L, 1));
|
|
||||||
lua_pushinteger(L, sheet->GetTileH());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const luaL_Reg tileSheetLib[] = {
|
|
||||||
{"Load",load},
|
|
||||||
{"Unload",unload},
|
|
||||||
{"GetXCount",getXCount},
|
|
||||||
{"GetYCount",getYCount},
|
|
||||||
{"GetTileW",getTileW},
|
|
||||||
{"GetTileH",getTileH},
|
|
||||||
{nullptr, nullptr}
|
|
||||||
};
|
|
||||||
|
|
||||||
LUAMOD_API int openTileSheetAPI(lua_State* L) {
|
|
||||||
luaL_newlib(L, tileSheetLib);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. packet serial ../gameplay ../map ../utilities
|
|
||||||
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)/,libcommon.a)
|
|
||||||
|
|
||||||
#targets
|
|
||||||
all: $(OBJ) $(OUT)
|
|
||||||
ar -crs $(OUT) $(OBJ)
|
|
||||||
$(MAKE) -C packet
|
|
||||||
$(MAKE) -C serial
|
|
||||||
|
|
||||||
$(OBJ): | $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUT): | $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR):
|
|
||||||
mkdir $(OBJDIR)
|
|
||||||
|
|
||||||
$(OUTDIR):
|
|
||||||
mkdir $(OUTDIR)
|
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.cpp
|
|
||||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 CHARACTERPACKET_HPP_
|
|
||||||
#define CHARACTERPACKET_HPP_
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "vector2.hpp"
|
|
||||||
#include "statistics.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;
|
|
||||||
|
|
||||||
//gameplay
|
|
||||||
Statistics stats;
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 CLIENTPACKET_HPP_
|
|
||||||
#define CLIENTPACKET_HPP_
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
struct ClientPacket : SerialPacketBase {
|
|
||||||
int clientIndex;
|
|
||||||
int accountIndex;
|
|
||||||
char username[PACKET_STRING_SIZE];
|
|
||||||
// char password[PACKET_STRING_SIZE]; //hashed, not currently used
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 COMBATPACKET_HPP_
|
|
||||||
#define COMBATPACKET_HPP_
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "combat_defines.hpp"
|
|
||||||
|
|
||||||
struct CombatPacket : SerialPacketBase {
|
|
||||||
//identify the combat instance
|
|
||||||
int combatIndex;
|
|
||||||
int difficulty;
|
|
||||||
TerrainType terrainType;
|
|
||||||
|
|
||||||
//combatants
|
|
||||||
int characterArray[COMBAT_MAX_CHARACTERS];
|
|
||||||
int enemyArray[COMBAT_MAX_ENEMIES];
|
|
||||||
|
|
||||||
//location
|
|
||||||
int mapIndex;
|
|
||||||
Vector2 origin;
|
|
||||||
|
|
||||||
//TODO: gameplay components: rewards
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 ENEMYPACKET_HPP_
|
|
||||||
#define ENEMYPACKET_HPP_
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
struct EnemyPacket : SerialPacketBase {
|
|
||||||
//identify the enemy
|
|
||||||
int enemyIndex;
|
|
||||||
char handle[PACKET_STRING_SIZE];
|
|
||||||
char avatar[PACKET_STRING_SIZE];
|
|
||||||
|
|
||||||
//gameplay
|
|
||||||
Statistics stats;
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs, rewards
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. ../../gameplay ../../map ../../utilities
|
|
||||||
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)/,libcommon.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 $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 REGIONPACKET_HPP_
|
|
||||||
#define REGIONPACKET_HPP_
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
|
|
||||||
struct RegionPacket : SerialPacketBase {
|
|
||||||
//location/identify the region
|
|
||||||
int roomIndex;
|
|
||||||
int x, y;
|
|
||||||
|
|
||||||
//the data
|
|
||||||
Region* region;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
/* DOCS: Sanity check, read more
|
|
||||||
* Since most/all of the files in this directory are header files, I've created
|
|
||||||
* this source file as a "sanity check", to ensure that the above header files
|
|
||||||
* are written correctly via make.
|
|
||||||
*/
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 SERIALPACKET_HPP_
|
|
||||||
#define SERIALPACKET_HPP_
|
|
||||||
|
|
||||||
#include "character_packet.hpp"
|
|
||||||
#include "client_packet.hpp"
|
|
||||||
#include "combat_packet.hpp"
|
|
||||||
#include "enemy_packet.hpp"
|
|
||||||
#include "region_packet.hpp"
|
|
||||||
#include "server_packet.hpp"
|
|
||||||
|
|
||||||
//NOTE: SerialPacket is defined in serial_packet_base.hpp
|
|
||||||
|
|
||||||
union MaxPacket {
|
|
||||||
CharacterPacket a;
|
|
||||||
ClientPacket b;
|
|
||||||
CombatPacket c;
|
|
||||||
EnemyPacket d;
|
|
||||||
RegionPacket e;
|
|
||||||
ServerPacket f;
|
|
||||||
};
|
|
||||||
constexpr int MAX_PACKET_SIZE = sizeof(MaxPacket);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 SERIALPACKETBASE_HPP_
|
|
||||||
#define SERIALPACKETBASE_HPP_
|
|
||||||
|
|
||||||
#ifndef SERIALPACKET_HPP_
|
|
||||||
#error Cannot include this file without 'serial_packet.hpp'
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "serial_packet_type.hpp"
|
|
||||||
|
|
||||||
#include "SDL/SDL_net.h"
|
|
||||||
|
|
||||||
constexpr int NETWORK_VERSION = 20140701;
|
|
||||||
constexpr int PACKET_STRING_SIZE = 100;
|
|
||||||
|
|
||||||
struct SerialPacketBase {
|
|
||||||
//members
|
|
||||||
SerialPacketType type;
|
|
||||||
IPaddress srcAddress;
|
|
||||||
|
|
||||||
virtual ~SerialPacketBase() {};
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef SerialPacketBase SerialPacket;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 SERIALPACKETTYPE_HPP_
|
|
||||||
#define SERIALPACKETTYPE_HPP_
|
|
||||||
|
|
||||||
/* Key for the comments:
|
|
||||||
* request => response
|
|
||||||
*/
|
|
||||||
|
|
||||||
enum class SerialPacketType {
|
|
||||||
//default: there is something wrong
|
|
||||||
NONE = 0,
|
|
||||||
|
|
||||||
//keep alive
|
|
||||||
//ping => pong
|
|
||||||
PING = 1,
|
|
||||||
PONG = 2,
|
|
||||||
|
|
||||||
//search for the server list
|
|
||||||
//none => server name, player count, version info (and source address)
|
|
||||||
BROADCAST_REQUEST = 3,
|
|
||||||
BROADCAST_RESPONSE = 4,
|
|
||||||
|
|
||||||
//try to join the server
|
|
||||||
//username, and password => client index, account index
|
|
||||||
JOIN_REQUEST = 5,
|
|
||||||
JOIN_RESPONSE = 6,
|
|
||||||
JOIN_REJECTION = 7,
|
|
||||||
|
|
||||||
//mass update of all surrounding content
|
|
||||||
//character.x, character.y => packet barrage
|
|
||||||
SYNCHRONIZE = 8,
|
|
||||||
|
|
||||||
//disconnect from the server
|
|
||||||
//autentication, account index => disconnect that account
|
|
||||||
DISCONNECT = 9,
|
|
||||||
|
|
||||||
//shut down the server
|
|
||||||
//autentication => disconnect, shutdown
|
|
||||||
SHUTDOWN = 10,
|
|
||||||
|
|
||||||
//map data
|
|
||||||
//room index, region.x, region.y => room index, region.x, region.y, region content
|
|
||||||
REGION_REQUEST = 11,
|
|
||||||
REGION_CONTENT = 12,
|
|
||||||
|
|
||||||
//combat data
|
|
||||||
//TODO: system incomplete
|
|
||||||
COMBAT_NEW = 13,
|
|
||||||
COMBAT_DELETE = 14,
|
|
||||||
COMBAT_UPDATE = 15,
|
|
||||||
|
|
||||||
COMBAT_ENTER_REQUEST = 16,
|
|
||||||
COMBAT_ENTER_RESPONSE = 17,
|
|
||||||
|
|
||||||
COMBAT_EXIT_REQUEST = 18,
|
|
||||||
COMBAT_EXIT_RESPONSE = 19,
|
|
||||||
|
|
||||||
//TODO: COMBAT info
|
|
||||||
|
|
||||||
COMBAT_REJECTION = 20,
|
|
||||||
|
|
||||||
//character data
|
|
||||||
//character data => etc.
|
|
||||||
CHARACTER_NEW = 21,
|
|
||||||
CHARACTER_DELETE = 22,
|
|
||||||
CHARACTER_UPDATE = 23,
|
|
||||||
|
|
||||||
//authentication, character index => character stats
|
|
||||||
CHARACTER_STATS_REQUEST= 24,
|
|
||||||
CHARACTER_STATS_RESPONSE = 25,
|
|
||||||
|
|
||||||
//character new => character rejection, disconnect?
|
|
||||||
CHARACTER_REJECTION = 26,
|
|
||||||
|
|
||||||
//enemy data
|
|
||||||
//enemy data => etc.
|
|
||||||
ENEMY_NEW = 27,
|
|
||||||
ENEMY_DELETE = 28,
|
|
||||||
ENEMY_UPDATE = 29,
|
|
||||||
|
|
||||||
ENEMY_STATS_REQUEST = 30,
|
|
||||||
ENEMY_STATS_RESPONSE = 31,
|
|
||||||
|
|
||||||
//enemy index => enemy doens't exist
|
|
||||||
ENEMY_REJECTION= 32,
|
|
||||||
|
|
||||||
//NOTE: more packet types go here
|
|
||||||
|
|
||||||
//not used
|
|
||||||
LAST
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 SERVERPACKET_HPP_
|
|
||||||
#define SERVERPACKET_HPP_
|
|
||||||
|
|
||||||
#include "serial_packet_base.hpp"
|
|
||||||
|
|
||||||
struct ServerPacket : SerialPacketBase {
|
|
||||||
//identify the server
|
|
||||||
char name[PACKET_STRING_SIZE];
|
|
||||||
int playerCount;
|
|
||||||
int version;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. ../packet ../../gameplay ../../map ../../utilities
|
|
||||||
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)/,libcommon.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 $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -1,182 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
//simple type functions
|
|
||||||
void serializeType(SerialPacketBase* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeType(SerialPacketBase* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
}
|
|
||||||
|
|
||||||
//main switch functions
|
|
||||||
void serializePacket(SerialPacketBase* packet, void* buffer) {
|
|
||||||
switch(packet->type) {
|
|
||||||
//no extra data
|
|
||||||
case SerialPacketType::NONE:
|
|
||||||
case SerialPacketType::PING:
|
|
||||||
case SerialPacketType::PONG:
|
|
||||||
case SerialPacketType::BROADCAST_REQUEST:
|
|
||||||
|
|
||||||
//all rejections
|
|
||||||
case SerialPacketType::JOIN_REJECTION:
|
|
||||||
case SerialPacketType::CHARACTER_REJECTION:
|
|
||||||
case SerialPacketType::ENEMY_REJECTION:
|
|
||||||
case SerialPacketType::COMBAT_REJECTION:
|
|
||||||
|
|
||||||
serializeType(packet, buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//character info
|
|
||||||
case SerialPacketType::CHARACTER_NEW:
|
|
||||||
case SerialPacketType::CHARACTER_DELETE:
|
|
||||||
case SerialPacketType::CHARACTER_UPDATE:
|
|
||||||
case SerialPacketType::CHARACTER_STATS_REQUEST:
|
|
||||||
case SerialPacketType::CHARACTER_STATS_RESPONSE:
|
|
||||||
serializeCharacter(static_cast<CharacterPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//client info
|
|
||||||
case SerialPacketType::JOIN_REQUEST:
|
|
||||||
case SerialPacketType::JOIN_RESPONSE:
|
|
||||||
case SerialPacketType::SYNCHRONIZE:
|
|
||||||
case SerialPacketType::DISCONNECT:
|
|
||||||
case SerialPacketType::SHUTDOWN:
|
|
||||||
serializeClient(static_cast<ClientPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//combat info
|
|
||||||
case SerialPacketType::COMBAT_NEW:
|
|
||||||
case SerialPacketType::COMBAT_DELETE:
|
|
||||||
case SerialPacketType::COMBAT_UPDATE:
|
|
||||||
|
|
||||||
case SerialPacketType::COMBAT_ENTER_REQUEST:
|
|
||||||
case SerialPacketType::COMBAT_ENTER_RESPONSE:
|
|
||||||
case SerialPacketType::COMBAT_EXIT_REQUEST:
|
|
||||||
case SerialPacketType::COMBAT_EXIT_RESPONSE:
|
|
||||||
|
|
||||||
serializeCombat(static_cast<CombatPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//enemy info
|
|
||||||
case SerialPacketType::ENEMY_NEW:
|
|
||||||
case SerialPacketType::ENEMY_DELETE:
|
|
||||||
case SerialPacketType::ENEMY_UPDATE:
|
|
||||||
case SerialPacketType::ENEMY_STATS_REQUEST:
|
|
||||||
case SerialPacketType::ENEMY_STATS_RESPONSE:
|
|
||||||
serializeEnemy(static_cast<EnemyPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//region info
|
|
||||||
case SerialPacketType::REGION_REQUEST:
|
|
||||||
serializeRegionFormat(static_cast<RegionPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SerialPacketType::REGION_CONTENT:
|
|
||||||
serializeRegionContent(static_cast<RegionPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//server info
|
|
||||||
case SerialPacketType::BROADCAST_RESPONSE:
|
|
||||||
serializeServer(static_cast<ServerPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializePacket(SerialPacketBase* packet, void* buffer) {
|
|
||||||
//find the type, so that you can actually deserialize the packet!
|
|
||||||
deserializeType(packet, buffer);
|
|
||||||
switch(packet->type) {
|
|
||||||
//no extra data
|
|
||||||
case SerialPacketType::NONE:
|
|
||||||
case SerialPacketType::PING:
|
|
||||||
case SerialPacketType::PONG:
|
|
||||||
case SerialPacketType::BROADCAST_REQUEST:
|
|
||||||
|
|
||||||
//all rejections
|
|
||||||
case SerialPacketType::JOIN_REJECTION:
|
|
||||||
case SerialPacketType::CHARACTER_REJECTION:
|
|
||||||
case SerialPacketType::ENEMY_REJECTION:
|
|
||||||
case SerialPacketType::COMBAT_REJECTION:
|
|
||||||
|
|
||||||
//NOTHING
|
|
||||||
break;
|
|
||||||
|
|
||||||
//character info
|
|
||||||
case SerialPacketType::CHARACTER_NEW:
|
|
||||||
case SerialPacketType::CHARACTER_DELETE:
|
|
||||||
case SerialPacketType::CHARACTER_UPDATE:
|
|
||||||
case SerialPacketType::CHARACTER_STATS_REQUEST:
|
|
||||||
case SerialPacketType::CHARACTER_STATS_RESPONSE:
|
|
||||||
deserializeCharacter(static_cast<CharacterPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//client info
|
|
||||||
case SerialPacketType::JOIN_REQUEST:
|
|
||||||
case SerialPacketType::JOIN_RESPONSE:
|
|
||||||
case SerialPacketType::SYNCHRONIZE:
|
|
||||||
case SerialPacketType::DISCONNECT:
|
|
||||||
case SerialPacketType::SHUTDOWN:
|
|
||||||
deserializeClient(static_cast<ClientPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//combat info
|
|
||||||
case SerialPacketType::COMBAT_NEW:
|
|
||||||
case SerialPacketType::COMBAT_DELETE:
|
|
||||||
case SerialPacketType::COMBAT_UPDATE:
|
|
||||||
|
|
||||||
case SerialPacketType::COMBAT_ENTER_REQUEST:
|
|
||||||
case SerialPacketType::COMBAT_ENTER_RESPONSE:
|
|
||||||
case SerialPacketType::COMBAT_EXIT_REQUEST:
|
|
||||||
case SerialPacketType::COMBAT_EXIT_RESPONSE:
|
|
||||||
|
|
||||||
serializeCombat(static_cast<CombatPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//enemy info
|
|
||||||
case SerialPacketType::ENEMY_NEW:
|
|
||||||
case SerialPacketType::ENEMY_DELETE:
|
|
||||||
case SerialPacketType::ENEMY_UPDATE:
|
|
||||||
case SerialPacketType::ENEMY_STATS_REQUEST:
|
|
||||||
case SerialPacketType::ENEMY_STATS_RESPONSE:
|
|
||||||
serializeEnemy(static_cast<EnemyPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//region info
|
|
||||||
case SerialPacketType::REGION_REQUEST:
|
|
||||||
deserializeRegionFormat(static_cast<RegionPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SerialPacketType::REGION_CONTENT:
|
|
||||||
deserializeRegionContent(static_cast<RegionPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
|
|
||||||
//server info
|
|
||||||
case SerialPacketType::BROADCAST_RESPONSE:
|
|
||||||
deserializeServer(static_cast<ServerPacket*>(packet), buffer);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 SERIALIZE_HPP_
|
|
||||||
#define SERIALIZE_HPP_
|
|
||||||
|
|
||||||
#include "serial_packet.hpp"
|
|
||||||
|
|
||||||
#include "region.hpp"
|
|
||||||
#include "statistics.hpp"
|
|
||||||
|
|
||||||
//Primary interface functions
|
|
||||||
void serializePacket(SerialPacketBase*, void* dest);
|
|
||||||
void deserializePacket(SerialPacketBase*, void* src);
|
|
||||||
|
|
||||||
void serializeType(SerialPacketBase*, void*);
|
|
||||||
void deserializeType(SerialPacketBase*, void*);
|
|
||||||
|
|
||||||
//utility functions, exposed
|
|
||||||
void serializeCharacter(CharacterPacket*, void*);
|
|
||||||
void serializeClient(ClientPacket*, void*);
|
|
||||||
void serializeCombat(CombatPacket*, void*);
|
|
||||||
void serializeEnemy(EnemyPacket*, void*);
|
|
||||||
void serializeRegionFormat(RegionPacket*, void*);
|
|
||||||
void serializeRegionContent(RegionPacket*, void*);
|
|
||||||
void serializeServer(ServerPacket*, void*);
|
|
||||||
void serializeStatistics(Statistics*, void*);
|
|
||||||
|
|
||||||
void deserializeCharacter(CharacterPacket*, void*);
|
|
||||||
void deserializeClient(ClientPacket*, void*);
|
|
||||||
void deserializeCombat(CombatPacket*, void*);
|
|
||||||
void deserializeEnemy(EnemyPacket*, void*);
|
|
||||||
void deserializeRegionFormat(RegionPacket*, void*);
|
|
||||||
void deserializeRegionContent(RegionPacket*, void*);
|
|
||||||
void deserializeServer(ServerPacket*, void*);
|
|
||||||
void deserializeStatistics(Statistics*, void*);
|
|
||||||
|
|
||||||
/* DOCS: Keep PACKET_BUFFER_SIZE up to date
|
|
||||||
* DOCS: SerialPacketType::REGION_CONTENT is currently the largest type of packet, read more
|
|
||||||
* The metadata used are:
|
|
||||||
* SerialPacketType
|
|
||||||
* room index
|
|
||||||
* X & Y positon
|
|
||||||
* The rest is taken up by the Regions's content.
|
|
||||||
*/
|
|
||||||
|
|
||||||
constexpr int PACKET_BUFFER_SIZE = sizeof(SerialPacketType) + sizeof(int) * 3 + REGION_FOOTPRINT;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
void serializeCharacter(CharacterPacket* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the character
|
|
||||||
SERIALIZE(buffer, &packet->characterIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE);
|
|
||||||
SERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//the owner
|
|
||||||
SERIALIZE(buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
|
|
||||||
//location
|
|
||||||
SERIALIZE(buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->origin.x, sizeof(double));
|
|
||||||
SERIALIZE(buffer, &packet->origin.y, sizeof(double));
|
|
||||||
SERIALIZE(buffer, &packet->motion.x, sizeof(double));
|
|
||||||
SERIALIZE(buffer, &packet->motion.y, sizeof(double));
|
|
||||||
|
|
||||||
//stats structure
|
|
||||||
serializeStatistics(&packet->stats, buffer);
|
|
||||||
buffer = reinterpret_cast<char*>(buffer) + sizeof(Statistics);
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeCharacter(CharacterPacket* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the character
|
|
||||||
DESERIALIZE(buffer, &packet->characterIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE);
|
|
||||||
DESERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//the owner
|
|
||||||
DESERIALIZE(buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
|
|
||||||
//location
|
|
||||||
DESERIALIZE(buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->origin.x, sizeof(double));
|
|
||||||
DESERIALIZE(buffer, &packet->origin.y, sizeof(double));
|
|
||||||
DESERIALIZE(buffer, &packet->motion.x, sizeof(double));
|
|
||||||
DESERIALIZE(buffer, &packet->motion.y, sizeof(double));
|
|
||||||
|
|
||||||
//stats structure
|
|
||||||
deserializeStatistics(&packet->stats, buffer);
|
|
||||||
buffer = reinterpret_cast<char*>(buffer) + sizeof(Statistics);
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
void serializeClient(ClientPacket* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
SERIALIZE(buffer, &packet->clientIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->username, PACKET_STRING_SIZE);
|
|
||||||
// SERIALIZE(buffer, &packet->password, PACKET_STRING_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeClient(ClientPacket* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
DESERIALIZE(buffer, &packet->clientIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->accountIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->username, PACKET_STRING_SIZE);
|
|
||||||
// DESERIALIZE(buffer, &packet->password, PACKET_STRING_SIZE);
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
void serializeCombat(CombatPacket* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the combat instance
|
|
||||||
SERIALIZE(buffer, &packet->combatIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->difficulty, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->terrainType, sizeof(TerrainType));
|
|
||||||
|
|
||||||
//combatants
|
|
||||||
SERIALIZE(buffer, &packet->characterArray, sizeof(int) * COMBAT_MAX_CHARACTERS);
|
|
||||||
SERIALIZE(buffer, &packet->enemyArray, sizeof(int) * COMBAT_MAX_ENEMIES);
|
|
||||||
|
|
||||||
//location
|
|
||||||
SERIALIZE(buffer, &packet->mapIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->origin.x, sizeof(double));
|
|
||||||
SERIALIZE(buffer, &packet->origin.y, sizeof(double));
|
|
||||||
|
|
||||||
//TODO: gameplay components: rewards
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeCombat(CombatPacket* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the combat instance
|
|
||||||
DESERIALIZE(buffer, &packet->combatIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->difficulty, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->terrainType, sizeof(TerrainType));
|
|
||||||
|
|
||||||
//combatants
|
|
||||||
DESERIALIZE(buffer, &packet->characterArray, sizeof(int) * COMBAT_MAX_CHARACTERS);
|
|
||||||
DESERIALIZE(buffer, &packet->enemyArray, sizeof(int) * COMBAT_MAX_ENEMIES);
|
|
||||||
|
|
||||||
//location
|
|
||||||
DESERIALIZE(buffer, &packet->mapIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->origin.x, sizeof(double));
|
|
||||||
DESERIALIZE(buffer, &packet->origin.y, sizeof(double));
|
|
||||||
|
|
||||||
//TODO: gameplay components: rewards
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
void serializeEnemy(EnemyPacket* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the enemy
|
|
||||||
SERIALIZE(buffer, &packet->enemyIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE);
|
|
||||||
SERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//gameplay
|
|
||||||
|
|
||||||
//stats structure
|
|
||||||
serializeStatistics(&packet->stats, buffer);
|
|
||||||
buffer = reinterpret_cast<char*>(buffer) + sizeof(Statistics);
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs, rewards
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeEnemy(EnemyPacket* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the enemy
|
|
||||||
DESERIALIZE(buffer, &packet->enemyIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE);
|
|
||||||
DESERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE);
|
|
||||||
|
|
||||||
//stats structure
|
|
||||||
deserializeStatistics(&packet->stats, buffer);
|
|
||||||
buffer = reinterpret_cast<char*>(buffer) + sizeof(Statistics);
|
|
||||||
|
|
||||||
//TODO: gameplay components: equipment, items, buffs, debuffs, rewards
|
|
||||||
}
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
void serializeRegionFormat(RegionPacket* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//format
|
|
||||||
SERIALIZE(buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->x, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->y, sizeof(int));
|
|
||||||
}
|
|
||||||
|
|
||||||
void serializeRegionContent(RegionPacket* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//format
|
|
||||||
SERIALIZE(buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->x, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->y, sizeof(int));
|
|
||||||
|
|
||||||
//tiles
|
|
||||||
for (register int i = 0; i < REGION_WIDTH; i++) {
|
|
||||||
for (register int j = 0; j < REGION_HEIGHT; j++) {
|
|
||||||
for (register 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
|
|
||||||
SERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeRegionFormat(RegionPacket* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//format
|
|
||||||
DESERIALIZE(buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->x, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->y, sizeof(int));
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeRegionContent(RegionPacket* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//format
|
|
||||||
DESERIALIZE(buffer, &packet->roomIndex, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->x, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->y, sizeof(int));
|
|
||||||
|
|
||||||
//an object to work on
|
|
||||||
packet->region = new Region(packet->x, packet->y);
|
|
||||||
|
|
||||||
//tiles
|
|
||||||
for (register int i = 0; i < REGION_WIDTH; i++) {
|
|
||||||
for (register int j = 0; j < REGION_HEIGHT; j++) {
|
|
||||||
for (register 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
|
|
||||||
DESERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT);
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
void serializeServer(ServerPacket* packet, void* buffer) {
|
|
||||||
SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the server
|
|
||||||
SERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE);
|
|
||||||
SERIALIZE(buffer, &packet->playerCount, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &packet->version, sizeof(int));
|
|
||||||
}
|
|
||||||
|
|
||||||
void deserializeServer(ServerPacket* packet, void* buffer) {
|
|
||||||
DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType));
|
|
||||||
|
|
||||||
//identify the server
|
|
||||||
DESERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE);
|
|
||||||
DESERIALIZE(buffer, &packet->playerCount, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &packet->version, sizeof(int));
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include "serial_util.hpp"
|
|
||||||
|
|
||||||
void serializeStatistics(Statistics* stats, void* buffer) {
|
|
||||||
//integers
|
|
||||||
SERIALIZE(buffer, &stats->level, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->exp, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->maxHP, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->health, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->maxMP, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->mana, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->attack, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->defence, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->intelligence, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->resistance, sizeof(int));
|
|
||||||
SERIALIZE(buffer, &stats->speed, sizeof(int));
|
|
||||||
|
|
||||||
//floats
|
|
||||||
SERIALIZE(buffer, &stats->accuracy, sizeof(float));
|
|
||||||
SERIALIZE(buffer, &stats->evasion, sizeof(float));
|
|
||||||
SERIALIZE(buffer, &stats->luck, sizeof(float));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void deserializeStatistics(Statistics* stats, void* buffer) {
|
|
||||||
//integers
|
|
||||||
DESERIALIZE(buffer, &stats->level, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->exp, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->maxHP, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->health, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->maxMP, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->mana, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->attack, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->defence, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->intelligence, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->resistance, sizeof(int));
|
|
||||||
DESERIALIZE(buffer, &stats->speed, sizeof(int));
|
|
||||||
|
|
||||||
//floats
|
|
||||||
DESERIALIZE(buffer, &stats->accuracy, sizeof(float));
|
|
||||||
DESERIALIZE(buffer, &stats->evasion, sizeof(float));
|
|
||||||
DESERIALIZE(buffer, &stats->luck, sizeof(float));
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
|
||||||
*
|
|
||||||
* 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 SERIALIZEUTIL_HPP_
|
|
||||||
#define SERIALIZEUTIL_HPP_
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
//NOTE: The strange assignments here used in order to move the void* parameter
|
|
||||||
#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer = reinterpret_cast<char*>(buffer) + size;
|
|
||||||
#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer = reinterpret_cast<char*>(buffer) + size;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,224 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
//BUGFIX: 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 SerialPacket 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: put a void* and int* parameter list here
|
|
||||||
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 SerialPacket
|
|
||||||
//-------------------------
|
|
||||||
|
|
||||||
int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* 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, SerialPacket* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
serializePacket(serialPacket, packet->data);
|
|
||||||
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, SerialPacket* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
serializePacket(serialPacket, packet->data);
|
|
||||||
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(SerialPacket* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
serializePacket(serialPacket, packet->data);
|
|
||||||
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(SerialPacket* serialPacket) {
|
|
||||||
memset(packet->data, 0, packet->maxlen);
|
|
||||||
int ret = SDLNet_UDP_Recv(socket, packet);
|
|
||||||
deserializePacket(serialPacket, packet->data);
|
|
||||||
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 2014
|
/* 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,12 +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.
|
||||||
*/
|
*/
|
||||||
#ifndef TILESHEETAPI_HPP_
|
#ifndef NETWORKQUEUE_HPP_
|
||||||
#define TILESHEETAPI_HPP_
|
#define NETWORKQUEUE_HPP_
|
||||||
|
|
||||||
#include "lua/lua.hpp"
|
#include "packet.hpp"
|
||||||
|
|
||||||
#define TORTUGA_TILE_SHEET_NAME "TileSheet"
|
void beginQueueThread();
|
||||||
LUAMOD_API int openTileSheetAPI(lua_State* L);
|
void endQueueThread();
|
||||||
|
void killQueueThread();
|
||||||
|
Packet peekNetworkPacket();
|
||||||
|
Packet popNetworkPacket();
|
||||||
|
void flushNetworkQueue();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -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
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -23,8 +23,9 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
/* It might be more efficient to render to a different surface (like an Image)
|
/* Note: This class can only take a raster font with 16*16 characters, and the
|
||||||
* rather than calling this function with all of it's '%' and '/'.
|
* indevidual characters must have the same dimensions. Overall this class is too
|
||||||
|
* restrictive; I suggest using a 3rd party library.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) {
|
void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) {
|
||||||
@@ -40,21 +41,10 @@ void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Note: This class can only take a raster font with 16*16 characters, and the
|
|
||||||
* indevidual characters must have the same dimensions. Overall this class is too
|
|
||||||
* restrictive; I suggest using a 3rd party library.
|
|
||||||
*/
|
|
||||||
|
|
||||||
SDL_Surface* RasterFont::LoadSurface(std::string fname) {
|
|
||||||
image.LoadSurface(fname);
|
|
||||||
image.SetClipW(image.GetSurface()->w/16);
|
|
||||||
image.SetClipH(image.GetSurface()->h/16);
|
|
||||||
return image.GetSurface();
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) {
|
SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) {
|
||||||
image.SetSurface(p);
|
if (image.SetSurface(p)) {
|
||||||
image.SetClipW(image.GetSurface()->w/16);
|
image.SetClipW(image.GetSurface()->w/16);
|
||||||
image.SetClipH(image.GetSurface()->h/16);
|
image.SetClipH(image.GetSurface()->h/16);
|
||||||
|
}
|
||||||
return image.GetSurface();
|
return image.GetSurface();
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -24,29 +24,21 @@
|
|||||||
|
|
||||||
#include "image.hpp"
|
#include "image.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
class RasterFont {
|
class RasterFont {
|
||||||
public:
|
public:
|
||||||
RasterFont() = default;
|
RasterFont() = default;
|
||||||
RasterFont(RasterFont const& rhs) { *this = rhs; }
|
|
||||||
RasterFont(RasterFont&& rhs) { *this = std::move(rhs); }
|
|
||||||
RasterFont(std::string fname) { LoadSurface(fname); }
|
|
||||||
RasterFont(SDL_Surface* p) { SetSurface(p); }
|
RasterFont(SDL_Surface* p) { SetSurface(p); }
|
||||||
~RasterFont() = default;
|
~RasterFont() = default;
|
||||||
|
|
||||||
RasterFont& operator=(RasterFont const& rhs) { image = rhs.image; }
|
|
||||||
RasterFont& operator=(RasterFont&& rhs) { image = std::move(rhs.image); }
|
|
||||||
|
|
||||||
void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y);
|
void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y);
|
||||||
|
|
||||||
//Accessors and Mutators
|
//Accessors and Mutators
|
||||||
SDL_Surface* LoadSurface(std::string);
|
|
||||||
SDL_Surface* SetSurface(SDL_Surface*);
|
SDL_Surface* SetSurface(SDL_Surface*);
|
||||||
SDL_Surface* GetSurface() const { return image.GetSurface(); }
|
SDL_Surface* GetSurface() const { return image.GetSurface(); }
|
||||||
void FreeSurface() { image.FreeSurface(); }
|
Uint16 GetCharW() { return image.GetClipW(); }
|
||||||
|
Uint16 GetCharH() { return image.GetClipH(); }
|
||||||
Uint16 GetCharW() const { return image.GetClipW(); }
|
|
||||||
Uint16 GetCharH() const { return image.GetClipH(); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Image image;
|
Image image;
|
||||||
};
|
};
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
/* 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,16 +19,20 @@
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
#ifndef COMBATDEFINES_HPP_
|
#ifndef SINGLETON_HPP_
|
||||||
#define COMBATDEFINES_HPP_
|
#define SINGLETON_HPP_
|
||||||
|
|
||||||
#define COMBAT_MAX_CHARACTERS 16
|
template<typename T>
|
||||||
#define COMBAT_MAX_ENEMIES 16
|
class Singleton {
|
||||||
|
public:
|
||||||
enum class TerrainType {
|
static T* Get() {
|
||||||
NONE = 0,
|
return &instance;
|
||||||
GRASSLANDS,
|
}
|
||||||
//etc.
|
private:
|
||||||
|
static T instance;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T Singleton<T>::instance;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
/* 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,23 @@
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
#ifndef FRAMERATE_HPP_
|
#include "sprite_sheet.hpp"
|
||||||
#define FRAMERATE_HPP_
|
|
||||||
|
|
||||||
#include <chrono>
|
void SpriteSheet::Update(double delta) {
|
||||||
|
if (delay && (ticks += delta) >= delay) {
|
||||||
class FrameRate {
|
if (++currentFrame >= maxFrames) {
|
||||||
public:
|
currentFrame = 0;
|
||||||
typedef std::chrono::high_resolution_clock Clock;
|
|
||||||
|
|
||||||
FrameRate() = default;
|
|
||||||
int Calculate() {
|
|
||||||
frameCount++;
|
|
||||||
if (Clock::now() - tick >= std::chrono::duration<int>(1)) {
|
|
||||||
lastFrameRate = frameCount;
|
|
||||||
frameCount = 0;
|
|
||||||
tick = Clock::now();
|
|
||||||
}
|
}
|
||||||
return lastFrameRate;
|
ticks = 0;
|
||||||
|
}
|
||||||
|
image.SetClipX(currentFrame * image.GetClipW());
|
||||||
|
image.SetClipY(currentStrip * image.GetClipH());
|
||||||
}
|
}
|
||||||
int GetFrameRate() { return lastFrameRate; }
|
|
||||||
private:
|
|
||||||
int frameCount = 0;
|
|
||||||
int lastFrameRate = 0;
|
|
||||||
Clock::time_point tick = Clock::now();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* const s, Uint16 w, Uint16 h) {
|
||||||
|
image.SetSurface(s);
|
||||||
|
image.SetClip({0, 0, w, h});
|
||||||
|
currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW();
|
||||||
|
currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH();
|
||||||
|
delay = ticks = 0;
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -24,43 +24,39 @@
|
|||||||
|
|
||||||
#include "image.hpp"
|
#include "image.hpp"
|
||||||
|
|
||||||
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
class SpriteSheet {
|
class SpriteSheet {
|
||||||
public:
|
public:
|
||||||
SpriteSheet() = default;
|
SpriteSheet() = default;
|
||||||
SpriteSheet(std::string fname, Uint16 xCellCount, Uint16 yCellCount) { LoadSurface(fname, xCellCount, yCellCount); }
|
SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) { SetSurface(s, w, h); }
|
||||||
SpriteSheet(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) { SetSurface(surface, xCellCount, yCellCount); }
|
~SpriteSheet() = default;
|
||||||
~SpriteSheet() { FreeSurface(); };
|
|
||||||
|
|
||||||
void Update(double delta);
|
void Update(double delta);
|
||||||
|
|
||||||
SDL_Surface* LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount);
|
SDL_Surface* SetSurface(SDL_Surface* const, Uint16 w, Uint16 h);
|
||||||
SDL_Surface* SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount);
|
SDL_Surface* GetSurface() const { return image.GetSurface(); }
|
||||||
SDL_Surface* GetSurface() { return image.GetSurface(); }
|
|
||||||
void FreeSurface();
|
|
||||||
|
|
||||||
void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) { image.DrawTo(dest, x, y); }
|
void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) { image.DrawTo(dest, x, y); }
|
||||||
|
|
||||||
//accessors and mutators
|
//Accessors and Mutators
|
||||||
Image* GetImage() { return ℑ } //OO breaker
|
double SetDelay(double i) { return delay = i; }
|
||||||
|
|
||||||
Uint16 SetXCount(Uint16);
|
|
||||||
Uint16 SetYCount(Uint16);
|
|
||||||
Uint16 SetXIndex(Uint16);
|
|
||||||
Uint16 SetYIndex(Uint16);
|
|
||||||
|
|
||||||
Uint16 GetXCount() const { return xCount; }
|
|
||||||
Uint16 GetYCount() const { return yCount; }
|
|
||||||
Uint16 GetXIndex() const { return xIndex; }
|
|
||||||
Uint16 GetYIndex() const { return yIndex; }
|
|
||||||
|
|
||||||
double SetDelay(double d);
|
|
||||||
double GetDelay() const { return delay; }
|
double GetDelay() const { return delay; }
|
||||||
|
|
||||||
|
int SetCurrentFrame(int i) { return currentFrame = i; }
|
||||||
|
int SetCurrentStrip(int i) { return currentStrip = i; }
|
||||||
|
|
||||||
|
Uint16 GetFrameWidth() const { return image.GetClipW(); }
|
||||||
|
Uint16 GetFrameHeight() const { return image.GetClipH(); }
|
||||||
|
int GetCurrentFrame() const { return currentFrame; };
|
||||||
|
int GetCurrentStrip() const { return currentStrip; };
|
||||||
|
int GetMaxFrames() const { return maxFrames; }
|
||||||
|
int GetMaxStrips() const { return maxStrips; }
|
||||||
private:
|
private:
|
||||||
Image image;
|
Image image;
|
||||||
Uint16 xCount = 0, yCount = 0; //number of cells
|
int currentFrame = 0, maxFrames = 0;
|
||||||
Uint16 xIndex = 0, yIndex = 0; //current cell being drawn
|
int currentStrip = 0, maxStrips = 0;
|
||||||
double delay = 0.0, tick = 0.0;
|
double delay = 0, ticks = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
/* 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 "surface_manager.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) {
|
||||||
|
MapType::iterator it = surfaceMap.find(key);
|
||||||
|
if (it != surfaceMap.end()) {
|
||||||
|
throw(std::runtime_error(std::string("Surface already loaded: ") + key + std::string(", ") + fname));
|
||||||
|
}
|
||||||
|
return LoadSurface(key, fname);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Surface* SurfaceManager::Reload(std::string key, std::string fname) {
|
||||||
|
MapType::iterator it = surfaceMap.find(key);
|
||||||
|
if (it != surfaceMap.end()) {
|
||||||
|
SDL_FreeSurface(it->second);
|
||||||
|
surfaceMap.erase(it);
|
||||||
|
}
|
||||||
|
return LoadSurface(key, fname);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Surface* SurfaceManager::Get(std::string key) {
|
||||||
|
MapType::iterator it = surfaceMap.find(key);
|
||||||
|
if (it == surfaceMap.end()) {
|
||||||
|
throw(std::runtime_error(std::string("Could not find key: ") + key));
|
||||||
|
}
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Surface* SurfaceManager::Set(std::string key, SDL_Surface* ptr) {
|
||||||
|
MapType::iterator it = surfaceMap.find(key);
|
||||||
|
if (it != surfaceMap.end()) {
|
||||||
|
throw(std::runtime_error(std::string("Key already exists: ") + key));
|
||||||
|
}
|
||||||
|
return surfaceMap[key] = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SurfaceManager::Free(std::string key) {
|
||||||
|
MapType::iterator it = surfaceMap.find(key);
|
||||||
|
if (it != surfaceMap.end()) {
|
||||||
|
SDL_FreeSurface(it->second);
|
||||||
|
surfaceMap.erase(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SurfaceManager::FreeAll() {
|
||||||
|
for (auto it : surfaceMap) {
|
||||||
|
SDL_FreeSurface(it.second);
|
||||||
|
}
|
||||||
|
surfaceMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) {
|
||||||
|
SDL_Surface* ptr = SDL_LoadBMP(fname.c_str());
|
||||||
|
if (!ptr) {
|
||||||
|
throw(std::runtime_error(std::string("Failed to load file: ") + fname));
|
||||||
|
}
|
||||||
|
SDL_SetColorKey(ptr, SDL_SRCCOLORKEY, SDL_MapRGB(ptr->format, 255, 0, 255)); //default
|
||||||
|
return surfaceMap[key] = ptr;
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2014
|
/* 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,31 @@
|
|||||||
* 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.
|
||||||
*/
|
*/
|
||||||
#ifndef TIMER_HPP_
|
#ifndef SURFACEMANAGER_HPP_
|
||||||
#define TIMER_HPP_
|
#define SURFACEMANAGER_HPP_
|
||||||
|
|
||||||
#include <chrono>
|
#include "SDL/SDL.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <ostream>
|
|
||||||
|
|
||||||
class Timer {
|
class SurfaceManager {
|
||||||
public:
|
public:
|
||||||
typedef std::chrono::high_resolution_clock Clock;
|
SurfaceManager() = default;
|
||||||
|
~SurfaceManager() noexcept { FreeAll(); }
|
||||||
|
|
||||||
Timer();
|
SDL_Surface* Load(std::string key, std::string fname);
|
||||||
Timer(std::string s);
|
SDL_Surface* Reload(std::string key, std::string fname);
|
||||||
~Timer() = default;
|
SDL_Surface* Get(std::string key);
|
||||||
|
SDL_Surface* Set(std::string key, SDL_Surface* ptr);
|
||||||
inline void Start();
|
void Free(std::string key);
|
||||||
inline void Stop();
|
void FreeAll();
|
||||||
|
|
||||||
//accessors and mutators
|
|
||||||
Clock::duration GetTime() { return timeSpan; }
|
|
||||||
|
|
||||||
std::string SetName(std::string s) { return name = s; }
|
|
||||||
std::string GetName() { return name; }
|
|
||||||
|
|
||||||
|
SDL_Surface* operator[](std::string key) { return Get(key); };
|
||||||
private:
|
private:
|
||||||
std::string name;
|
SDL_Surface* LoadSurface(std::string key, std::string fname);
|
||||||
Clock::time_point start;
|
typedef std::map<std::string, SDL_Surface*> MapType;
|
||||||
Clock::duration timeSpan;
|
MapType surfaceMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, Timer& t);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
/* 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 "udp_network_utility.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
void UDPNetworkUtility::Open(int port, int packSize) {
|
||||||
|
if (!(socket = SDLNet_UDP_Open(port))) {
|
||||||
|
Close();
|
||||||
|
throw(std::runtime_error("Failed to open a UDP socket"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(packOut = SDLNet_AllocPacket(packSize))) {
|
||||||
|
Close();
|
||||||
|
throw(std::runtime_error("Failed to allocate the out packet"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(packIn = SDLNet_AllocPacket(packSize))) {
|
||||||
|
Close();
|
||||||
|
throw(std::runtime_error("Failed to allocate the in packet"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UDPNetworkUtility::Close() {
|
||||||
|
SDLNet_UDP_Close(socket);
|
||||||
|
SDLNet_FreePacket(packOut);
|
||||||
|
SDLNet_FreePacket(packIn);
|
||||||
|
socket = nullptr;
|
||||||
|
packOut = nullptr;
|
||||||
|
packIn = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 == -1) {
|
||||||
|
throw(std::runtime_error("Failed to bind to a channel"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UDPNetworkUtility::Unbind(int channel) {
|
||||||
|
SDLNet_UDP_Unbind(socket, channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
int UDPNetworkUtility::Send(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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Send(&add, data, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) {
|
||||||
|
if (len > packOut->maxlen) {
|
||||||
|
throw(std::runtime_error("Failed to copy the data into the packet"));
|
||||||
|
}
|
||||||
|
memset(packOut->data, 0, packOut->maxlen);
|
||||||
|
memcpy(packOut->data, data, len);
|
||||||
|
packOut->len = len;
|
||||||
|
packOut->address = *add;
|
||||||
|
|
||||||
|
int ret = SDLNet_UDP_Send(socket, -1, packOut);
|
||||||
|
|
||||||
|
if (ret <= 0) {
|
||||||
|
throw(std::runtime_error("Failed to send a packet"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int UDPNetworkUtility::Send(int channel, void* data, int len) {
|
||||||
|
if (len > packOut->maxlen) {
|
||||||
|
throw(std::runtime_error("Failed to copy the data into the packet"));
|
||||||
|
}
|
||||||
|
memset(packOut->data, 0, packOut->maxlen);
|
||||||
|
memcpy(packOut->data, data, len);
|
||||||
|
packOut->len = len;
|
||||||
|
|
||||||
|
int ret = SDLNet_UDP_Send(socket, channel, packOut);
|
||||||
|
|
||||||
|
if (ret <= 0) {
|
||||||
|
throw(std::runtime_error("Failed to send a packet"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int UDPNetworkUtility::SendAll(void* data, int len) {
|
||||||
|
if (len > packOut->maxlen) {
|
||||||
|
throw(std::runtime_error("Failed to copy the data into the packet"));
|
||||||
|
}
|
||||||
|
memset(packOut->data, 0, packOut->maxlen);
|
||||||
|
memcpy(packOut->data, data, len);
|
||||||
|
packOut->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, packOut);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
int UDPNetworkUtility::Receive() {
|
||||||
|
memset(packIn->data, 0, packIn->maxlen);
|
||||||
|
int ret = SDLNet_UDP_Recv(socket, packIn);
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
throw(std::runtime_error("Unknown network error occured"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
/* 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
|
||||||
@@ -24,49 +24,56 @@
|
|||||||
|
|
||||||
#include "SDL/SDL_net.h"
|
#include "SDL/SDL_net.h"
|
||||||
|
|
||||||
#include "serial_packet.hpp"
|
|
||||||
|
|
||||||
class UDPNetworkUtility {
|
class UDPNetworkUtility {
|
||||||
public:
|
public:
|
||||||
UDPNetworkUtility() = default;
|
UDPNetworkUtility() = default;
|
||||||
~UDPNetworkUtility() = default;
|
~UDPNetworkUtility() = default;
|
||||||
|
|
||||||
void Open(int port);
|
void Open(int port, int packSize);
|
||||||
void Close();
|
void Close();
|
||||||
|
|
||||||
//bind to a channel
|
//bind to an available channel
|
||||||
int Bind(const char* ip, int port, int channel = -1);
|
int Bind(const char* ip, int port) {
|
||||||
int Bind(IPaddress* add, int channel = -1);
|
Bind(ip, port, -1);
|
||||||
|
}
|
||||||
|
int Bind(IPaddress* add) {
|
||||||
|
Bind(add, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//bind to certain channel
|
||||||
|
int Bind(const char* ip, int port, int channel);
|
||||||
|
int Bind(IPaddress* add, int channel);
|
||||||
void Unbind(int channel);
|
void Unbind(int channel);
|
||||||
|
|
||||||
IPaddress* GetIPAddress(int channel) {
|
IPaddress* GetIPAddress(int channel) {
|
||||||
return SDLNet_UDP_GetPeerAddress(socket, channel);
|
return SDLNet_UDP_GetPeerAddress(socket, channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
//send a buffer
|
int Send(const char* ip, int port, void* data, int len);
|
||||||
int SendTo(const char* ip, int port, void* data, int len);
|
int Send(IPaddress* add, void* data, int len);
|
||||||
int SendTo(IPaddress* add, void* data, int len);
|
int Send(int channel, void* data, int len);
|
||||||
int SendTo(int channel, void* data, int len);
|
int SendAll(void* data, int len);
|
||||||
int SendToAllChannels(void* data, int len);
|
|
||||||
int Receive();
|
int Receive();
|
||||||
|
|
||||||
//send a SerialPacket
|
void* GetOutData() const {
|
||||||
int SendTo(const char* ip, int port, SerialPacket* serialPacket);
|
return reinterpret_cast<void*>(packOut->data);
|
||||||
int SendTo(IPaddress* add, SerialPacket* serialPacket);
|
};
|
||||||
int SendTo(int channel, SerialPacket* serialPacket);
|
void* GetInData() const {
|
||||||
int SendToAllChannels(SerialPacket* serialPacket);
|
return reinterpret_cast<void*>(packIn->data);
|
||||||
int Receive(SerialPacket* serialPacket);
|
};
|
||||||
|
UDPpacket* GetOutPacket() const {
|
||||||
//accessors
|
return packOut;
|
||||||
UDPpacket* GetPacket() const {
|
}
|
||||||
return packet;
|
UDPpacket* GetInPacket() const {
|
||||||
|
return packIn;
|
||||||
}
|
}
|
||||||
UDPsocket GetSocket() const {
|
UDPsocket GetSocket() const {
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
UDPsocket socket = nullptr;
|
UDPsocket socket = nullptr;
|
||||||
UDPpacket* packet = nullptr;
|
UDPpacket* packOut = nullptr;
|
||||||
|
UDPpacket* packIn = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
#config
|
|
||||||
INCLUDES+=. ../graphics
|
|
||||||
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)/,libcommon.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 $@ $<
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(RM) *.o *.a *.exe
|
|
||||||
|
|
||||||
rebuild: clean all
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
|
||||||
*
|
|
||||||
* 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 "menu_bar.hpp"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
void MenuBar::DrawTo(SDL_Surface* const dest) {
|
|
||||||
for (auto& i : entries) {
|
|
||||||
i.DrawTo(dest);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MenuBar::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
|
||||||
for (auto& i : entries) {
|
|
||||||
i.MouseMotion(motion);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MenuBar::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
|
||||||
for (auto& i : entries) {
|
|
||||||
i.MouseButtonDown(button);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MenuBar::MouseButtonUp(SDL_MouseButtonEvent const& button, int* entry, int* butt) {
|
|
||||||
*entry = *butt = -1;
|
|
||||||
int ret = -1;
|
|
||||||
for (auto& i : entries) {
|
|
||||||
ret = i.MouseButtonUp(button);
|
|
||||||
|
|
||||||
if (ret != -1) {
|
|
||||||
*entry = (&i - entries.data());
|
|
||||||
*butt = ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MenuBar::SetEntries(std::vector<std::vector<std::string>> info) {
|
|
||||||
if (!image || !font) {
|
|
||||||
throw(std::runtime_error("Surfaces not loaded into the menu bar"));
|
|
||||||
}
|
|
||||||
|
|
||||||
entries.clear();
|
|
||||||
for (int i = 0; i < info.size(); i++) {
|
|
||||||
//create the entry & the main button
|
|
||||||
entries.push_back(MenuBarEntry());
|
|
||||||
entries[i].mainButton.SetImage(image);
|
|
||||||
entries[i].mainButton.SetFont(font);
|
|
||||||
entries[i].mainButton.SetText(info[i][0]);
|
|
||||||
entries[i].mainButton.SetX(i * image->GetClipW());
|
|
||||||
entries[i].mainButton.SetY(0);
|
|
||||||
for (int j = 0; j < info[i].size()-1; j++) {
|
|
||||||
//create each drop button in this entry
|
|
||||||
entries[i].dropButtons.push_back(Button());
|
|
||||||
entries[i].dropButtons[j].SetImage(image);
|
|
||||||
entries[i].dropButtons[j].SetFont(font);
|
|
||||||
entries[i].dropButtons[j].SetText(info[i][j+1]);
|
|
||||||
entries[i].dropButtons[j].SetX(i * image->GetClipW());
|
|
||||||
entries[i].dropButtons[j].SetY((j+1) * image->GetClipH());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MenuBar::MenuBarEntry::DrawTo(SDL_Surface* const dest) {
|
|
||||||
//only draw the dropButtons in the user has this menu open
|
|
||||||
mainButton.DrawTo(dest);
|
|
||||||
|
|
||||||
if (!open) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& i : dropButtons) {
|
|
||||||
i.DrawTo(dest);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MenuBar::MenuBarEntry::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
|
||||||
//open the menu
|
|
||||||
bool o = mainButton.MouseMotion(motion) == Button::State::PRESSED;
|
|
||||||
|
|
||||||
if (!(open |= o)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& i : dropButtons) {
|
|
||||||
//dragging down the menu
|
|
||||||
o |= i.MouseMotion(motion) == Button::State::PRESSED;
|
|
||||||
}
|
|
||||||
|
|
||||||
open = o;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MenuBar::MenuBarEntry::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
|
||||||
//open the menu
|
|
||||||
if (!(open = mainButton.MouseButtonDown(button) == Button::State::PRESSED)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//update the others anyway
|
|
||||||
for (auto& i : dropButtons) {
|
|
||||||
i.MouseButtonDown(button);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int MenuBar::MenuBarEntry::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
|
||||||
int ret = -1;
|
|
||||||
mainButton.MouseButtonUp(button);
|
|
||||||
|
|
||||||
for (auto& i : dropButtons) {
|
|
||||||
//the user just released this button
|
|
||||||
if (i.GetState() != i.MouseButtonUp(button) && i.GetState() == Button::State::HOVER && open) {
|
|
||||||
//get this button's index
|
|
||||||
ret = (&i - dropButtons.data());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
open = false;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user