Compare commits
146 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 93c41bb19c | |||
| bf922ec598 | |||
| de902d2d3d | |||
| 7b3bf24e5d | |||
| de7da81102 | |||
| 6428b02d85 | |||
| 519b8a1e36 | |||
| 967f0653a1 | |||
| b86d393571 | |||
| 6b38501c27 | |||
| 5893342ad8 | |||
| ac4a264f12 | |||
| 43895a462a | |||
| 1bde0ed3f7 | |||
| d903c0df30 | |||
| 9620826d65 | |||
| 0a71f43ef3 | |||
| a47e76845f | |||
| c2eb08bd5e | |||
| 7b76e07231 | |||
| 1dd8042d3d | |||
| c575ee9ce1 | |||
| 1befc76b70 | |||
| 6a6e7f7125 | |||
| bb6e248583 | |||
| f7df4fba6c | |||
| e7403be508 | |||
| 873715b28c | |||
| b1d6e5a314 | |||
| f0453375c4 | |||
| e5a98efd7d | |||
| 14b330009b | |||
| 288b62d3f8 | |||
| c5e8f1b3af | |||
| 980717f9fd | |||
| 68475eee0f | |||
| eeb2400e79 | |||
| 01244005e9 | |||
| c5005b9b07 | |||
| 80a26341b1 | |||
| b7877962f1 | |||
| 688d064085 | |||
| 1c4d53e3ef | |||
| a53a134163 | |||
| 4d12788c53 | |||
| 1beb7cbd5d | |||
| 8358d72a98 | |||
| 4ebff4a25a | |||
| 0ff787abda | |||
| 9b5b48a8ab | |||
| 5dd0fb9e23 | |||
| 7c210e04a5 | |||
| 9236e02101 | |||
| 8f4ebf20da | |||
| 910e51f637 | |||
| 92fc9b4e25 | |||
| 2b8e7241c9 | |||
| 124cb3ad13 | |||
| 6d3400d948 | |||
| 5031352fe3 | |||
| 624369f147 | |||
| 9a7f7b8684 | |||
| fa9487c2f2 | |||
| 9ec1ddab99 | |||
| 6f1c2d0555 | |||
| 01b50d5590 | |||
| 9c673928e6 | |||
| d55dfb90e4 | |||
| 235a05d006 | |||
| dabb7b3b2e | |||
| e756289c2b | |||
| c534158c2e | |||
| b47191a0c1 | |||
| 6c6a025f2a | |||
| 60edc97ea5 | |||
| a5b209d9e0 | |||
| fd673a415f | |||
| 4270765146 | |||
| f56cb58dfb | |||
| f315f4bf35 | |||
| 69765de433 | |||
| 35d463d4ba | |||
| 894b46c5db | |||
| eb0b18af6f | |||
| fba183fa27 | |||
| c5a627004a | |||
| ac27fb0ca7 | |||
| be4a8311d5 | |||
| 6d32d44fa3 | |||
| 9bacfb1424 | |||
| c3464be589 | |||
| 7fe71c60d0 | |||
| 0c6537fb36 | |||
| 854dc0eb45 | |||
| e90afb7e4a | |||
| 0453f6becf | |||
| c8a58ab515 | |||
| 553f8dbfa5 | |||
| 2bacdcdab7 | |||
| 99aecbfdbb | |||
| 27bda5dc28 | |||
| 962f3f5dd0 | |||
| 41077b43b3 | |||
| 4410ab892f | |||
| 60000cb0cf | |||
| 66b00255d5 | |||
| a5b68cf1fd | |||
| 4cff57fe71 | |||
| 38b603fc8f | |||
| 47684380a9 | |||
| e4bfbfb906 | |||
| 9db86c19f6 | |||
| d5b551cec3 | |||
| 7e500027e3 | |||
| dd786ba579 | |||
| 5a57888305 | |||
| 706aa5e1d7 | |||
| 19c1b1197d | |||
| 5cf62f5517 | |||
| 56d02ad8d4 | |||
| 006a72174f | |||
| 0cbc9dd9db | |||
| 60c31ff56d | |||
| 6a204643f6 | |||
| 59285d1630 | |||
| a850d6b1af | |||
| f17fa0f345 | |||
| 756d4e770d | |||
| 7bb5e8ce0d | |||
| 975533afba | |||
| 19749f7c87 | |||
| 7703c4b0ad | |||
| dc8f594eec | |||
| 4629b7302b | |||
| 02d83d1f16 | |||
| 91c9cef56d | |||
| f7d4912942 | |||
| eb8674b84f | |||
| ff44e4d916 | |||
| e66540f114 | |||
| 639c0c70e3 | |||
| 37b02352e2 | |||
| 31c8bd7fd2 | |||
| 808fe570a3 | |||
| e706cc9d13 | |||
| eb02cc4b6a |
@@ -1,26 +1,28 @@
|
||||
You can find the [latest nightly build here](https://dl.dropboxusercontent.com/u/46669050/Tortuga.rar).
|
||||
The most recent stable build for Windows can be found [here](https://dl.dropboxusercontent.com/u/46669050/Tortuga.rar).
|
||||
|
||||
Tortuga is an open source 2D multiplayer role playing game featuring permadeath (deletion of a character upon death). The emphasis of this game is on multiplayer cooperation, competition, and customization. The game runs on customizable server software that can support up to 150 simultaneous players or more.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
## Documentation
|
||||
|
||||
Tortuga's full documentation can be found in a separate branch, see [Tortuga/docs](https://github.com/Ratstail91/Tortuga/tree/docs).
|
||||
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).
|
||||
|
||||
## External Dependencies
|
||||
|
||||
* [SDL 1.6](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
||||
* [SDL 1.2](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
||||
* [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
|
||||
|
||||
## Documentation
|
||||
|
||||
[Tortuga Game Design Document](https://docs.google.com/document/d/17SjOTdacKKWX0Z-UkaiFnVnjfEeMsc5LRmARtnjh5UI/edit?usp=sharing)
|
||||
[Tortuga Technical Document](https://docs.google.com/document/d/1ASTfM_1e0yE1cFP-IZey_rHEC6k2kmVY56X4K407sw0/edit?usp=sharing)
|
||||
|
||||
## 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).
|
||||
|
||||
Copyright (c) 2013 Kayne Ruse
|
||||
Copyright (c) 2013, 2014 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.
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,3 +1,24 @@
|
||||
/* 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 CHANNELS_HPP_
|
||||
#define CHANNELS_HPP_
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -21,15 +21,11 @@
|
||||
*/
|
||||
#include "client_application.hpp"
|
||||
|
||||
#include "serial.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
//-------------------------
|
||||
//Static declarations
|
||||
//-------------------------
|
||||
|
||||
ClientApplication ClientApplication::instance;
|
||||
|
||||
//-------------------------
|
||||
//Scene headers
|
||||
//-------------------------
|
||||
@@ -46,15 +42,7 @@ ClientApplication ClientApplication::instance;
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
ClientApplication::ClientApplication() {
|
||||
//
|
||||
}
|
||||
|
||||
ClientApplication::~ClientApplication() {
|
||||
//
|
||||
}
|
||||
|
||||
void ClientApplication::Init() {
|
||||
void ClientApplication::Init(int argc, char** argv) {
|
||||
//load the prerequisites
|
||||
config.Load("rsc\\config.cfg");
|
||||
|
||||
@@ -62,13 +50,13 @@ void ClientApplication::Init() {
|
||||
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||
throw(std::runtime_error("Failed to initialize SDL"));
|
||||
}
|
||||
BaseScene::SetScreen(config.Int("screen.w"), config.Int("screen.h"), 0, (config.Bool("screen.f")) ? SDL_HWSURFACE|SDL_DOUBLEBUF : SDL_HWSURFACE);
|
||||
BaseScene::SetScreen(config.Int("screen.w"), config.Int("screen.h"), 0, (config.Bool("screen.f")) ? SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN : SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||
|
||||
//initialize SDL_net
|
||||
if (SDLNet_Init()) {
|
||||
throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||
}
|
||||
network.Open(0, sizeof(NetworkPacket));
|
||||
network.Open(0);
|
||||
}
|
||||
|
||||
void ClientApplication::Proc() {
|
||||
@@ -77,7 +65,7 @@ void ClientApplication::Proc() {
|
||||
//prepare the time system
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
|
||||
Clock::duration delta(16 * Clock::duration::period::den / std::milli::den);
|
||||
std::chrono::duration<int, std::milli> delta(16);
|
||||
Clock::time_point simTime = Clock::now();
|
||||
Clock::time_point realTime;
|
||||
|
||||
@@ -95,15 +83,12 @@ void ClientApplication::Proc() {
|
||||
//simulate game time
|
||||
while (simTime < realTime) {
|
||||
//call each user defined function
|
||||
activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den);
|
||||
activeScene->RunFrame(double(delta.count()) / std::chrono::duration<int, std::milli>::period::den);
|
||||
simTime += delta;
|
||||
}
|
||||
|
||||
//draw the game to the screen
|
||||
activeScene->RenderFrame();
|
||||
|
||||
//give the computer a break
|
||||
SDL_Delay(10);
|
||||
}
|
||||
|
||||
UnloadScene();
|
||||
@@ -121,7 +106,6 @@ void ClientApplication::Quit() {
|
||||
|
||||
void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||
UnloadScene();
|
||||
|
||||
switch(sceneIndex) {
|
||||
//add scene creation calls here
|
||||
case SceneList::FIRST:
|
||||
@@ -135,15 +119,14 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||
activeScene = new OptionsMenu(&config);
|
||||
break;
|
||||
case SceneList::LOBBYMENU:
|
||||
activeScene = new LobbyMenu(&config, &network, &clientIndex);
|
||||
activeScene = new LobbyMenu(&config, &network, &clientIndex, &accountIndex, &characterIndex);
|
||||
break;
|
||||
case SceneList::INWORLD:
|
||||
activeScene = new InWorld(&config, &network, &clientIndex);
|
||||
activeScene = new InWorld(&config, &network, &clientIndex, &accountIndex, &characterIndex, &combatMap, &characterMap);
|
||||
break;
|
||||
case SceneList::INCOMBAT:
|
||||
activeScene = new InCombat();
|
||||
activeScene = new InCombat(&config, &network, &clientIndex, &accountIndex, &characterIndex, &combatMap, &characterMap, &enemyMap);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw(std::logic_error("Failed to recognize the scene index"));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -26,19 +26,19 @@
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "network_packet.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "character_data.hpp"
|
||||
#include "combat_data.hpp"
|
||||
#include "enemy_data.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
class ClientApplication {
|
||||
private:
|
||||
ClientApplication();
|
||||
~ClientApplication();
|
||||
static ClientApplication instance;
|
||||
|
||||
public:
|
||||
static ClientApplication* GetInstance() { return &instance; }
|
||||
ClientApplication() = default;
|
||||
~ClientApplication() = default;
|
||||
|
||||
void Init();
|
||||
void Init(int argc, char** argv);
|
||||
void Proc();
|
||||
void Quit();
|
||||
|
||||
@@ -49,9 +49,16 @@ private:
|
||||
|
||||
BaseScene* activeScene = nullptr;
|
||||
|
||||
//shared parameters
|
||||
ConfigUtility config;
|
||||
UDPNetworkUtility network;
|
||||
int clientIndex = -1; //replace with a struct?
|
||||
int clientIndex = -1;
|
||||
int accountIndex = -1;
|
||||
int characterIndex = -1;
|
||||
|
||||
std::map<int, CombatData> combatMap;
|
||||
std::map<int, CharacterData> characterMap;
|
||||
std::map<int, EnemyData> enemyMap;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -25,7 +25,25 @@
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InCombat::InCombat() {
|
||||
InCombat::InCombat(
|
||||
ConfigUtility* const argConfig,
|
||||
UDPNetworkUtility* const argNetwork,
|
||||
int* const argClientIndex,
|
||||
int* const argAccountIndex,
|
||||
int* const argCharacterIndex,
|
||||
std::map<int, CombatData>* argCombatMap,
|
||||
std::map<int, CharacterData>* argCharacterMap,
|
||||
std::map<int, EnemyData>* argEnemyMap
|
||||
):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex),
|
||||
accountIndex(*argAccountIndex),
|
||||
characterIndex(*argCharacterIndex),
|
||||
combatMap(*argCombatMap),
|
||||
characterMap(*argCharacterMap),
|
||||
enemyMap(*argEnemyMap)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
@@ -49,6 +67,13 @@ 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) {
|
||||
//
|
||||
}
|
||||
@@ -57,6 +82,12 @@ void InCombat::Render(SDL_Surface* const screen) {
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void InCombat::QuitEvent() {
|
||||
//exit the game AND the server
|
||||
// RequestDisconnect();
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
|
||||
void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
@@ -80,3 +111,15 @@ void InCombat::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
void InCombat::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Network handlers
|
||||
//-------------------------
|
||||
|
||||
//TODO: network handlers
|
||||
|
||||
//-------------------------
|
||||
//Server control
|
||||
//-------------------------
|
||||
|
||||
//TODO: server control
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,32 +19,49 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef INWORLD_HPP_
|
||||
#define INWORLD_HPP_
|
||||
#ifndef INCOMBAT_HPP_
|
||||
#define INCOMBAT_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
//network
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "network_packet.hpp"
|
||||
|
||||
//graphics
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
#include "player_character.hpp"
|
||||
|
||||
#include <map>
|
||||
//common
|
||||
#include "config_utility.hpp"
|
||||
#include "frame_rate.hpp"
|
||||
|
||||
class InWorld : public BaseScene {
|
||||
#include "combat_data.hpp"
|
||||
#include "character_data.hpp"
|
||||
#include "enemy_data.hpp"
|
||||
|
||||
//client
|
||||
#include "base_scene.hpp"
|
||||
|
||||
class InCombat : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InWorld(ConfigUtility* const, UDPNetworkUtility* const, int* const);
|
||||
~InWorld();
|
||||
InCombat(
|
||||
ConfigUtility* const argConfig,
|
||||
UDPNetworkUtility* const argNetwork,
|
||||
int* const argClientIndex,
|
||||
int* const argAccountIndex,
|
||||
int* const argCharacterIndex,
|
||||
std::map<int, CombatData>* argCombatMap,
|
||||
std::map<int, CharacterData>* argCharacterMap,
|
||||
std::map<int, EnemyData>* argEnemyMap
|
||||
);
|
||||
~InCombat();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void RenderFrame();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
@@ -55,29 +72,33 @@ protected:
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
void HandlePacket(NetworkPacket);
|
||||
void HandleDisconnect(NetworkPacket);
|
||||
void HandlePlayerNew(NetworkPacket);
|
||||
void HandlePlayerDelete(NetworkPacket);
|
||||
void HandlePlayerUpdate(NetworkPacket);
|
||||
//Network handlers
|
||||
void HandlePacket(SerialPacket);
|
||||
void HandleDisconnect(SerialPacket);
|
||||
//TODO: more
|
||||
|
||||
void SendState();
|
||||
//Server control
|
||||
void SendPlayerUpdate();
|
||||
void RequestDisconnect();
|
||||
void RequestShutDown();
|
||||
void RequestShutdown();
|
||||
//TODO: more
|
||||
|
||||
//global
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
int& accountIndex;
|
||||
int& characterIndex;
|
||||
std::map<int, CombatData>& combatMap;
|
||||
std::map<int, CharacterData>& characterMap;
|
||||
std::map<int, EnemyData>& enemyMap;
|
||||
|
||||
//members
|
||||
Image image;
|
||||
RasterFont font;
|
||||
Button disconnectButton;
|
||||
Button shutDownButton;
|
||||
std::map<int, PlayerCharacter> playerCharacters;
|
||||
PlayerCharacter* localCharacter = nullptr;
|
||||
int playerIndex = -1;
|
||||
//graphics
|
||||
//TODO: graphics
|
||||
|
||||
//UI
|
||||
//TODO: UI
|
||||
FrameRate fps;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,452 @@
|
||||
/* 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 <algorithm>
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InWorld::InWorld(
|
||||
ConfigUtility* const argConfig,
|
||||
UDPNetworkUtility* const argNetwork,
|
||||
int* const argClientIndex,
|
||||
int* const argAccountIndex,
|
||||
int* const argCharacterIndex,
|
||||
std::map<int, CombatData>* argCombatMap,
|
||||
std::map<int, CharacterData>* argCharacterMap
|
||||
):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex),
|
||||
accountIndex(*argAccountIndex),
|
||||
characterIndex(*argCharacterIndex),
|
||||
combatMap(*argCombatMap),
|
||||
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
|
||||
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?
|
||||
tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15);
|
||||
|
||||
//request a sync
|
||||
RequestSynchronize();
|
||||
|
||||
//debug
|
||||
// RequestRegion(0, 0);
|
||||
}
|
||||
|
||||
InWorld::~InWorld() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void InWorld::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Update(double delta) {
|
||||
SerialPacket packet;
|
||||
|
||||
//suck in all waiting packets
|
||||
while(network.Receive(&packet)) {
|
||||
HandlePacket(packet);
|
||||
}
|
||||
|
||||
//update the characters
|
||||
for (auto& it : characterMap) {
|
||||
it.second.Update(delta);
|
||||
}
|
||||
|
||||
//update the camera
|
||||
if(localCharacter) {
|
||||
camera.x = localCharacter->position.x - camera.marginX;
|
||||
camera.y = localCharacter->position.y - camera.marginY;
|
||||
}
|
||||
|
||||
//check the map
|
||||
UpdateMap();
|
||||
}
|
||||
|
||||
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 (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
|
||||
tileSheet.DrawRegionTo(screen, *it, camera.x, camera.y);
|
||||
}
|
||||
|
||||
//draw characters
|
||||
for (auto& it : characterMap) {
|
||||
//TODO: drawing order according to Y position
|
||||
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::MAINMENU);
|
||||
}
|
||||
|
||||
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) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE: {
|
||||
QuitEvent();
|
||||
}
|
||||
break;
|
||||
|
||||
//player movement
|
||||
case SDLK_LEFT:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.x -= CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.x += CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_UP:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.y -= CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.y += CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
//player movement
|
||||
case SDLK_LEFT:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.x += CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.x -= CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_UP:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.y += CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
if (localCharacter) {
|
||||
localCharacter->motion.y -= CHARACTER_WALKING_SPEED;
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Network handlers
|
||||
//-------------------------
|
||||
|
||||
void InWorld::HandlePacket(SerialPacket packet) {
|
||||
switch(packet.meta.type) {
|
||||
case SerialPacket::Type::DISCONNECT:
|
||||
HandleDisconnect(packet);
|
||||
break;
|
||||
case SerialPacket::Type::REGION_CONTENT:
|
||||
HandleRegionContent(packet);
|
||||
break;
|
||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
||||
HandleCharacterUpdate(packet);
|
||||
break;
|
||||
case SerialPacket::Type::CHARACTER_NEW:
|
||||
HandleCharacterNew(packet);
|
||||
break;
|
||||
case SerialPacket::Type::CHARACTER_DELETE:
|
||||
HandleCharacterDelete(packet);
|
||||
break;
|
||||
//handle errors
|
||||
default:
|
||||
throw(std::runtime_error(std::string() + "Unknown SerialPacket::Type encountered in InWorld: " + to_string_custom(int(packet.meta.type))));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandleDisconnect(SerialPacket packet) {
|
||||
network.Unbind(Channels::SERVER);
|
||||
clientIndex = -1;
|
||||
accountIndex = -1;
|
||||
characterIndex = -1;
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
|
||||
void InWorld::HandleRegionContent(SerialPacket packet) {
|
||||
//replace existing regions
|
||||
regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
regionPager.PushRegion(packet.regionInfo.region);
|
||||
packet.regionInfo.region = nullptr;
|
||||
}
|
||||
|
||||
void InWorld::HandleCharacterUpdate(SerialPacket packet) {
|
||||
if (characterMap.find(packet.characterInfo.characterIndex) == characterMap.end()) {
|
||||
HandleCharacterNew(packet);
|
||||
return;
|
||||
}
|
||||
|
||||
//update only if the message didn't originate from here
|
||||
if (packet.characterInfo.clientIndex != clientIndex) {
|
||||
characterMap[packet.characterInfo.characterIndex].position = packet.characterInfo.position;
|
||||
characterMap[packet.characterInfo.characterIndex].motion = packet.characterInfo.motion;
|
||||
}
|
||||
characterMap[packet.characterInfo.characterIndex].CorrectSprite();
|
||||
}
|
||||
|
||||
void InWorld::HandleCharacterNew(SerialPacket packet) {
|
||||
if (characterMap.find(packet.characterInfo.characterIndex) != characterMap.end()) {
|
||||
throw(std::runtime_error("Cannot create duplicate characters"));
|
||||
}
|
||||
|
||||
//create the character object
|
||||
CharacterData& character = characterMap[packet.characterInfo.characterIndex];
|
||||
|
||||
//set the members
|
||||
character.handle = packet.characterInfo.handle;
|
||||
character.avatar = packet.characterInfo.avatar;
|
||||
character.sprite.LoadSurface(config["dir.sprites"] + character.avatar, 4, 4);
|
||||
character.mapIndex = packet.characterInfo.mapIndex;
|
||||
character.position = packet.characterInfo.position;
|
||||
character.motion = packet.characterInfo.motion;
|
||||
character.stats = packet.characterInfo.stats;
|
||||
|
||||
character.CorrectSprite();
|
||||
|
||||
//catch this client's player object
|
||||
if (packet.characterInfo.characterIndex == characterIndex && !localCharacter) {
|
||||
localCharacter = &character;
|
||||
|
||||
//setup the camera
|
||||
//TODO: can't change the screen size?
|
||||
camera.width = GetScreen()->w;
|
||||
camera.height = GetScreen()->h;
|
||||
|
||||
//center on the player's character
|
||||
camera.marginX = (GetScreen()->w / 2 - localCharacter->sprite.GetImage()->GetClipW() / 2);
|
||||
camera.marginY = (GetScreen()->h / 2 - localCharacter->sprite.GetImage()->GetClipH() / 2);
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandleCharacterDelete(SerialPacket packet) {
|
||||
//TODO: authenticate when own character is being deleted (linked to a TODO in the server)
|
||||
//catch this client's player object
|
||||
if (packet.characterInfo.characterIndex == characterIndex) {
|
||||
characterIndex = -1;
|
||||
localCharacter = nullptr;
|
||||
}
|
||||
|
||||
characterMap.erase(packet.characterInfo.characterIndex);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Server control
|
||||
//-------------------------
|
||||
|
||||
void InWorld::RequestSynchronize() {
|
||||
SerialPacket packet;
|
||||
|
||||
//request a sync
|
||||
packet.meta.type = SerialPacket::Type::SYNCHRONIZE;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
packet.clientInfo.accountIndex = accountIndex;
|
||||
packet.clientInfo.characterIndex = characterIndex;
|
||||
|
||||
network.SendTo(Channels::SERVER, &packet);
|
||||
}
|
||||
|
||||
void InWorld::SendPlayerUpdate() {
|
||||
SerialPacket packet;
|
||||
|
||||
//pack the packet
|
||||
packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
|
||||
packet.characterInfo.clientIndex = clientIndex;
|
||||
packet.characterInfo.accountIndex = accountIndex;
|
||||
packet.characterInfo.characterIndex = characterIndex;
|
||||
packet.characterInfo.position = localCharacter->position;
|
||||
packet.characterInfo.motion = localCharacter->motion;
|
||||
|
||||
network.SendTo(Channels::SERVER, &packet);
|
||||
}
|
||||
|
||||
void InWorld::RequestDisconnect() {
|
||||
SerialPacket packet;
|
||||
|
||||
//send a disconnect request
|
||||
packet.meta.type = SerialPacket::Type::DISCONNECT;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
packet.clientInfo.accountIndex = accountIndex;
|
||||
packet.clientInfo.characterIndex = characterIndex;
|
||||
|
||||
network.SendTo(Channels::SERVER, &packet);
|
||||
}
|
||||
|
||||
void InWorld::RequestShutDown() {
|
||||
SerialPacket packet;
|
||||
|
||||
//send a shutdown request
|
||||
packet.meta.type = SerialPacket::Type::SHUTDOWN;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
packet.clientInfo.accountIndex = accountIndex;
|
||||
packet.clientInfo.characterIndex = characterIndex;
|
||||
|
||||
network.SendTo(Channels::SERVER, &packet);
|
||||
}
|
||||
|
||||
void InWorld::RequestRegion(int mapIndex, int x, int y) {
|
||||
SerialPacket packet;
|
||||
|
||||
//pack the region's data
|
||||
packet.meta.type = SerialPacket::Type::REGION_REQUEST;
|
||||
packet.regionInfo.mapIndex = mapIndex;
|
||||
packet.regionInfo.x = x;
|
||||
packet.regionInfo.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 (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); /* EMPTY */) {
|
||||
//check if the region is outside off 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/* 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 "map_allocator.hpp"
|
||||
#include "map_file_format.hpp"
|
||||
#include "region_pager.hpp"
|
||||
|
||||
//networking
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
//graphics
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
#include "tile_sheet.hpp"
|
||||
|
||||
//common
|
||||
#include "config_utility.hpp"
|
||||
#include "frame_rate.hpp"
|
||||
|
||||
#include "combat_data.hpp"
|
||||
#include "character_data.hpp"
|
||||
|
||||
//client
|
||||
#include "base_scene.hpp"
|
||||
|
||||
//STL
|
||||
#include <map>
|
||||
|
||||
class InWorld : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InWorld(
|
||||
ConfigUtility* const argConfig,
|
||||
UDPNetworkUtility* const argNetwork,
|
||||
int* const argClientIndex,
|
||||
int* const argAccountIndex,
|
||||
int* const argCharacterIndex,
|
||||
std::map<int, CombatData>* argCombatMap,
|
||||
std::map<int, CharacterData>* 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);
|
||||
void HandleDisconnect(SerialPacket);
|
||||
void HandleCharacterNew(SerialPacket);
|
||||
void HandleCharacterDelete(SerialPacket);
|
||||
void HandleCharacterUpdate(SerialPacket);
|
||||
void HandleRegionContent(SerialPacket);
|
||||
|
||||
//Server control
|
||||
void RequestSynchronize();
|
||||
void SendPlayerUpdate();
|
||||
void RequestDisconnect();
|
||||
void RequestShutDown();
|
||||
void RequestRegion(int mapIndex, int x, int y);
|
||||
|
||||
//utilities
|
||||
void UpdateMap();
|
||||
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
int& accountIndex;
|
||||
int& characterIndex;
|
||||
std::map<int, CombatData>& combatMap;
|
||||
std::map<int, CharacterData>& characterMap;
|
||||
|
||||
//graphics
|
||||
Image buttonImage;
|
||||
RasterFont font;
|
||||
TileSheet tileSheet;
|
||||
|
||||
//map
|
||||
RegionPager<BlankAllocator, DummyFormat> 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
|
||||
CharacterData* localCharacter = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "lobby_menu.hpp"
|
||||
|
||||
#include "channels.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
@@ -29,10 +30,18 @@
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
LobbyMenu::LobbyMenu(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex):
|
||||
LobbyMenu::LobbyMenu(
|
||||
ConfigUtility* const argConfig,
|
||||
UDPNetworkUtility* const argNetwork,
|
||||
int* const argClientIndex,
|
||||
int* const argAccountIndex,
|
||||
int* const argCharacterIndex
|
||||
):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex)
|
||||
clientIndex(*argClientIndex),
|
||||
accountIndex(*argAccountIndex),
|
||||
characterIndex(*argCharacterIndex)
|
||||
{
|
||||
//setup the utility objects
|
||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
@@ -77,11 +86,9 @@ void LobbyMenu::FrameStart() {
|
||||
}
|
||||
|
||||
void LobbyMenu::Update(double delta) {
|
||||
//suck in all waiting packets
|
||||
NetworkPacket packet;
|
||||
while(network.Receive()) {
|
||||
memcpy(&packet, network.GetInData(), sizeof(NetworkPacket));
|
||||
packet.meta.srcAddress = network.GetInPacket()->address;
|
||||
//suck in and process all waiting packets
|
||||
SerialPacket packet;
|
||||
while(network.Receive(&packet)) {
|
||||
HandlePacket(packet);
|
||||
}
|
||||
}
|
||||
@@ -91,9 +98,14 @@ 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]) {
|
||||
@@ -104,6 +116,16 @@ void LobbyMenu::Render(SDL_Surface* const screen) {
|
||||
|
||||
//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?
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,20 +148,25 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (search.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
//broadcast to the network, or a specific server
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST;
|
||||
network.Send(config["server.host"].c_str(), config.Int("server.port"), reinterpret_cast<void*>(&packet), sizeof(NetworkPacket));
|
||||
SerialPacket packet;
|
||||
packet.meta.type = SerialPacket::Type::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) {
|
||||
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
|
||||
//pack the packet
|
||||
SerialPacket packet;
|
||||
packet.meta.type = SerialPacket::Type::JOIN_REQUEST;
|
||||
strncpy(packet.clientInfo.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
|
||||
strncpy(packet.clientInfo.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE);
|
||||
strncpy(packet.clientInfo.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE);
|
||||
|
||||
//join the selected server
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::JOIN_REQUEST;
|
||||
network.Send(&selection->address, &packet, sizeof(NetworkPacket));
|
||||
network.SendTo(&selection->address, &packet);
|
||||
selection = nullptr;
|
||||
}
|
||||
|
||||
@@ -173,24 +200,38 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
void LobbyMenu::HandlePacket(NetworkPacket packet) {
|
||||
//-------------------------
|
||||
//Network handlers
|
||||
//-------------------------
|
||||
|
||||
void LobbyMenu::HandlePacket(SerialPacket packet) {
|
||||
switch(packet.meta.type) {
|
||||
case NetworkPacket::Type::BROADCAST_RESPONSE: {
|
||||
case SerialPacket::Type::BROADCAST_RESPONSE: {
|
||||
//extract the data
|
||||
ServerInformation server;
|
||||
server.name = packet.serverInfo.name;
|
||||
server.address = packet.meta.srcAddress;
|
||||
server.networkVersion = packet.serverInfo.networkVersion;
|
||||
server.name = packet.serverInfo.name;
|
||||
server.playerCount = packet.serverInfo.playerCount;
|
||||
|
||||
//NOTE: Check compatibility here
|
||||
server.compatible = server.networkVersion == NETWORK_VERSION;
|
||||
|
||||
//push
|
||||
serverInfo.push_back(server);
|
||||
}
|
||||
break;
|
||||
case NetworkPacket::Type::JOIN_RESPONSE:
|
||||
clientIndex = packet.clientInfo.index;
|
||||
case SerialPacket::Type::JOIN_RESPONSE:
|
||||
clientIndex = packet.clientInfo.clientIndex;
|
||||
accountIndex = packet.clientInfo.accountIndex;
|
||||
characterIndex = packet.clientInfo.characterIndex;
|
||||
network.Bind(&packet.meta.srcAddress, Channels::SERVER);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
break;
|
||||
|
||||
//handle errors
|
||||
default:
|
||||
throw(std::runtime_error("Unknown NetworkPacket::Type encountered"));
|
||||
throw(std::runtime_error(std::string() + "Unknown SerialPacket::Type encountered in LobbyMenu: " + to_string_custom(int(packet.meta.type))));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -22,22 +22,31 @@
|
||||
#ifndef LOBBYMENU_HPP_
|
||||
#define LOBBYMENU_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
//graphics & utilities
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "network_packet.hpp"
|
||||
|
||||
//network
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
//client
|
||||
#include "base_scene.hpp"
|
||||
|
||||
//STL
|
||||
#include <vector>
|
||||
|
||||
class LobbyMenu : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, int* const);
|
||||
LobbyMenu(
|
||||
ConfigUtility* const argConfig,
|
||||
UDPNetworkUtility* const argNetwork,
|
||||
int* const argClientIndex,
|
||||
int* const argAccountIndex,
|
||||
int* const argCharacterIndex
|
||||
);
|
||||
~LobbyMenu();
|
||||
|
||||
protected:
|
||||
@@ -54,12 +63,15 @@ protected:
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
void HandlePacket(NetworkPacket);
|
||||
//Network handlers
|
||||
void HandlePacket(SerialPacket);
|
||||
|
||||
//global
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
int& accountIndex;
|
||||
int& characterIndex;
|
||||
|
||||
//members
|
||||
Image image;
|
||||
@@ -70,8 +82,11 @@ protected:
|
||||
|
||||
//server list
|
||||
struct ServerInformation {
|
||||
std::string name;
|
||||
IPaddress address;
|
||||
int networkVersion;
|
||||
std::string name;
|
||||
int playerCount;
|
||||
bool compatible;
|
||||
};
|
||||
|
||||
std::vector<ServerInformation> serverInfo;
|
||||
+6
-5
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -26,12 +26,13 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int, char**) {
|
||||
int main(int argc, char** argv) {
|
||||
cout << "Beginning client" << endl;
|
||||
try {
|
||||
ClientApplication::GetInstance()->Init();
|
||||
ClientApplication::GetInstance()->Proc();
|
||||
ClientApplication::GetInstance()->Quit();
|
||||
ClientApplication app;
|
||||
app.Init(argc, argv);
|
||||
app.Proc();
|
||||
app.Quit();
|
||||
}
|
||||
catch(exception& e) {
|
||||
cerr << "Fatal exception thrown: " << e.what() << endl;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -49,7 +49,7 @@ protected:
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//globals
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
|
||||
//members
|
||||
+3
-10
@@ -1,17 +1,14 @@
|
||||
#config
|
||||
INCLUDES+=. scenes ../common ../common/graphics ../common/map ../common/network ../common/ui
|
||||
LIBS+=libclient.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
INCLUDES+=. ../common/gameplay ../common/graphics ../common/map ../common/network ../common/ui ../common/utilities
|
||||
LIBS+=../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) -DGRAPHICS
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
@@ -19,7 +16,6 @@ OUT=$(addprefix $(OUTDIR)/,client)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
$(MAKE) -C scenes
|
||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
@@ -35,9 +31,6 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -49,7 +49,7 @@ protected:
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//globals
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
|
||||
//members
|
||||
@@ -1,117 +0,0 @@
|
||||
/* 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"
|
||||
|
||||
#define WALKING_SPEED 140
|
||||
|
||||
void PlayerCharacter::Update(double delta) {
|
||||
if (diagonal) {
|
||||
constexpr double d = 1.0/sqrt(2);
|
||||
position += motion * delta * d;
|
||||
}
|
||||
else {
|
||||
position += motion * delta;
|
||||
}
|
||||
sprite.Update(delta);
|
||||
}
|
||||
|
||||
void PlayerCharacter::AdjustDirection(Direction direction) {
|
||||
//shift the movement in this direction
|
||||
switch(direction) {
|
||||
case Direction::NORTH:
|
||||
if (motion.y >= 0) {
|
||||
motion.y -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case Direction::SOUTH:
|
||||
if (motion.y <= 0) {
|
||||
motion.y += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case Direction::WEST:
|
||||
if (motion.x >= 0) {
|
||||
motion.x -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case Direction::EAST:
|
||||
if (motion.x <= 0) {
|
||||
motion.x += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
//face the correct direction
|
||||
ResetDirection();
|
||||
}
|
||||
|
||||
void PlayerCharacter::FaceDirection(Direction direction) {
|
||||
//this function depends on the format of the sprite sheets
|
||||
switch(direction) {
|
||||
case Direction::NORTH:
|
||||
sprite.SetYIndex(1);
|
||||
break;
|
||||
case Direction::SOUTH:
|
||||
sprite.SetYIndex(0);
|
||||
break;
|
||||
case Direction::WEST:
|
||||
sprite.SetYIndex(2);
|
||||
break;
|
||||
case Direction::EAST:
|
||||
sprite.SetYIndex(3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerCharacter::ResetDirection() {
|
||||
//base the direction on the character's movement
|
||||
if (motion.y > 0) {
|
||||
FaceDirection(Direction::SOUTH);
|
||||
}
|
||||
else if (motion.y < 0) {
|
||||
FaceDirection(Direction::NORTH);
|
||||
}
|
||||
else if (motion.x > 0) {
|
||||
FaceDirection(Direction::EAST);
|
||||
}
|
||||
else if (motion.x < 0) {
|
||||
FaceDirection(Direction::WEST);
|
||||
}
|
||||
ResetSpeed();
|
||||
}
|
||||
|
||||
void PlayerCharacter::ResetSpeed() {
|
||||
//diagonal
|
||||
if (motion.x != 0 && motion.y != 0) {
|
||||
sprite.SetDelay(0.1);
|
||||
diagonal = true;
|
||||
}
|
||||
//cardinal
|
||||
else if (motion != 0) {
|
||||
sprite.SetDelay(0.1);
|
||||
diagonal = false;
|
||||
}
|
||||
//not moving
|
||||
else {
|
||||
sprite.SetDelay(0);
|
||||
sprite.SetXIndex(0);
|
||||
diagonal = false;
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/* 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"
|
||||
|
||||
class PlayerCharacter {
|
||||
public:
|
||||
enum class Direction {
|
||||
NORTH, SOUTH, EAST, WEST
|
||||
};
|
||||
|
||||
PlayerCharacter() = default;
|
||||
~PlayerCharacter() = default;
|
||||
|
||||
void Update(double delta);
|
||||
|
||||
void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); }
|
||||
|
||||
//clunky code results in smooth movement and controls
|
||||
void AdjustDirection(Direction);
|
||||
void FaceDirection(Direction);
|
||||
void ResetDirection();
|
||||
void ResetSpeed();
|
||||
|
||||
//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:
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
SpriteSheet sprite;
|
||||
|
||||
//for moving diagonally
|
||||
bool diagonal = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
/* 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 "base_scene.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Static declarations
|
||||
//-------------------------
|
||||
|
||||
SDL_Surface* BaseScene::screen = nullptr;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
BaseScene::BaseScene() {
|
||||
//
|
||||
}
|
||||
|
||||
BaseScene::~BaseScene() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Program control
|
||||
//-------------------------
|
||||
|
||||
SDL_Surface* BaseScene::SetScreen(int w, int h, int bpp, Uint32 flags) {
|
||||
if (!bpp) {
|
||||
bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
|
||||
}
|
||||
|
||||
screen = SDL_SetVideoMode(w, h, bpp, flags);
|
||||
|
||||
if (!screen) {
|
||||
throw(std::runtime_error("Failed to create the screen surface"));
|
||||
}
|
||||
|
||||
return screen;
|
||||
}
|
||||
|
||||
SDL_Surface* BaseScene::GetScreen() {
|
||||
return screen;
|
||||
}
|
||||
|
||||
SceneList BaseScene::SetNextScene(SceneList sceneIndex) {
|
||||
return nextScene = sceneIndex;
|
||||
}
|
||||
|
||||
SceneList BaseScene::GetNextScene() const {
|
||||
return nextScene;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void BaseScene::RunFrame(double delta) {
|
||||
FrameStart();
|
||||
HandleEvents();
|
||||
Update(delta);
|
||||
FrameEnd();
|
||||
}
|
||||
|
||||
void BaseScene::RenderFrame() {
|
||||
SDL_FillRect(screen, 0, 0);
|
||||
Render(screen);
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void BaseScene::HandleEvents() {
|
||||
SDL_Event event;
|
||||
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_QUIT:
|
||||
QuitEvent();
|
||||
break;
|
||||
|
||||
case SDL_VIDEORESIZE:
|
||||
SetScreen(event.resize.w, event.resize.h, 0, screen->flags);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEMOTION:
|
||||
MouseMotion(event.motion);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
MouseButtonDown(event.button);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
MouseButtonUp(event.button);
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
KeyDown(event.key);
|
||||
break;
|
||||
|
||||
case SDL_KEYUP:
|
||||
KeyUp(event.key);
|
||||
break;
|
||||
|
||||
#ifdef USE_EVENT_JOYSTICK
|
||||
//TODO: joystick/gamepad support
|
||||
#endif
|
||||
|
||||
#ifdef USE_EVENT_UNKNOWN
|
||||
default:
|
||||
UnknownEvent(event);
|
||||
break;
|
||||
#endif
|
||||
}//switch
|
||||
}//while
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef BASESCENE_HPP_
|
||||
#define BASESCENE_HPP_
|
||||
|
||||
#include "scene_list.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
BaseScene();
|
||||
virtual ~BaseScene();
|
||||
|
||||
//Program control
|
||||
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||
static SDL_Surface* GetScreen();
|
||||
|
||||
SceneList SetNextScene(SceneList sceneIndex);
|
||||
SceneList GetNextScene() const;
|
||||
|
||||
//Frame loop
|
||||
virtual void RunFrame(double delta);
|
||||
virtual void RenderFrame();
|
||||
|
||||
protected:
|
||||
virtual void FrameStart() {}
|
||||
virtual void HandleEvents();
|
||||
virtual void Update(double delta) {}
|
||||
virtual void FrameEnd() {}
|
||||
virtual void Render(SDL_Surface* const screen) {}
|
||||
|
||||
//Event handlers
|
||||
virtual void QuitEvent() { SetNextScene(SceneList::QUIT); }
|
||||
virtual void MouseMotion(SDL_MouseMotionEvent const&) {}
|
||||
virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {}
|
||||
virtual void MouseButtonUp(SDL_MouseButtonEvent const&) {}
|
||||
virtual void KeyDown(SDL_KeyboardEvent const&) {}
|
||||
virtual void KeyUp(SDL_KeyboardEvent const&) {}
|
||||
|
||||
#ifdef USE_EVENT_JOYSTICK
|
||||
//TODO: joystick/gamepad support
|
||||
#endif
|
||||
|
||||
#ifdef USE_EVENT_UNKNOWN
|
||||
virtual void UnknownEvent(SDL_Event const&) {}
|
||||
#endif
|
||||
|
||||
private:
|
||||
static SDL_Surface* screen;
|
||||
SceneList nextScene = SceneList::CONTINUE;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,315 +0,0 @@
|
||||
/* 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 "channels.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex)
|
||||
{
|
||||
//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
|
||||
disconnectButton.SetImage(&image);
|
||||
disconnectButton.SetFont(&font);
|
||||
shutDownButton.SetImage(&image);
|
||||
shutDownButton.SetFont(&font);
|
||||
|
||||
//set the button positions
|
||||
disconnectButton.SetX(50);
|
||||
disconnectButton.SetY(50 + image.GetClipH() * 0);
|
||||
shutDownButton.SetX(50);
|
||||
shutDownButton.SetY(50 + image.GetClipH() * 1);
|
||||
|
||||
//set the button texts
|
||||
disconnectButton.SetText("Disconnect");
|
||||
shutDownButton.SetText("Shut Down");
|
||||
|
||||
//create the server-side player object
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::PLAYER_NEW;
|
||||
packet.playerInfo.clientIndex = clientIndex;
|
||||
snprintf(packet.playerInfo.handle, PACKET_STRING_SIZE, "%s", config["player.handle"].c_str());
|
||||
snprintf(packet.playerInfo.avatar, PACKET_STRING_SIZE, "%s", config["player.avatar"].c_str());
|
||||
packet.playerInfo.position = {0,0};
|
||||
packet.playerInfo.motion = {0,0};
|
||||
|
||||
//send it
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
|
||||
//request a sync
|
||||
packet.meta.type = NetworkPacket::Type::SYNCHRONIZE;
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
}
|
||||
|
||||
InWorld::~InWorld() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void InWorld::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Update(double delta) {
|
||||
//suck in all waiting packets
|
||||
NetworkPacket packet;
|
||||
while(network.Receive()) {
|
||||
memcpy(&packet, network.GetInData(), sizeof(NetworkPacket));
|
||||
packet.meta.srcAddress = network.GetInPacket()->address;
|
||||
HandlePacket(packet);
|
||||
}
|
||||
|
||||
for (auto& it : playerCharacters) {
|
||||
it.second.Update(delta);
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Render(SDL_Surface* const screen) {
|
||||
for (auto& it : playerCharacters) {
|
||||
it.second.DrawTo(screen);
|
||||
}
|
||||
disconnectButton.DrawTo(screen);
|
||||
shutDownButton.DrawTo(screen);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void InWorld::QuitEvent() {
|
||||
//exit the game AND the server
|
||||
RequestDisconnect();
|
||||
}
|
||||
|
||||
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) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE: {
|
||||
QuitEvent();
|
||||
}
|
||||
break;
|
||||
|
||||
//player movement
|
||||
case SDLK_LEFT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_UP:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
//player movement
|
||||
case SDLK_LEFT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_UP:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH);
|
||||
SendState();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandlePacket(NetworkPacket packet) {
|
||||
switch(packet.meta.type) {
|
||||
case NetworkPacket::Type::DISCONNECT:
|
||||
HandleDisconnect(packet);
|
||||
break;
|
||||
|
||||
case NetworkPacket::Type::PLAYER_NEW:
|
||||
HandlePlayerNew(packet);
|
||||
break;
|
||||
|
||||
case NetworkPacket::Type::PLAYER_DELETE:
|
||||
HandlePlayerDelete(packet);
|
||||
break;
|
||||
|
||||
case NetworkPacket::Type::PLAYER_UPDATE:
|
||||
HandlePlayerUpdate(packet);
|
||||
break;
|
||||
|
||||
//handle errors
|
||||
default:
|
||||
throw(std::runtime_error("Unknown NetworkPacket::Type encountered"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandleDisconnect(NetworkPacket packet) {
|
||||
network.Unbind(Channels::SERVER);
|
||||
clientIndex = -1;
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
|
||||
void InWorld::HandlePlayerNew(NetworkPacket packet) {
|
||||
if (playerCharacters.find(packet.playerInfo.playerIndex) != playerCharacters.end()) {
|
||||
throw(std::runtime_error("Cannot create duplicate players"));
|
||||
}
|
||||
|
||||
playerCharacters[packet.playerInfo.playerIndex].GetSprite()->LoadSurface(config["dir.sprites"] + packet.playerInfo.avatar, 4, 4);
|
||||
playerCharacters[packet.playerInfo.playerIndex].SetPosition(packet.playerInfo.position);
|
||||
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion);
|
||||
playerCharacters[packet.playerInfo.playerIndex].ResetDirection();
|
||||
|
||||
//catch this client's player object
|
||||
if (packet.playerInfo.clientIndex == clientIndex && !localCharacter) {
|
||||
playerIndex = packet.playerInfo.playerIndex;
|
||||
localCharacter = &playerCharacters[playerIndex];
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandlePlayerDelete(NetworkPacket packet) {
|
||||
if (playerCharacters.find(packet.playerInfo.playerIndex) == playerCharacters.end()) {
|
||||
throw(std::runtime_error("Cannot delete non-existant players"));
|
||||
}
|
||||
|
||||
playerCharacters.erase(packet.playerInfo.playerIndex);
|
||||
|
||||
//catch this client's player object
|
||||
if (packet.playerInfo.clientIndex == clientIndex) {
|
||||
playerIndex = -1;
|
||||
localCharacter = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
|
||||
if (playerCharacters.find(packet.playerInfo.playerIndex) == playerCharacters.end()) {
|
||||
HandlePlayerNew(packet);
|
||||
return;
|
||||
}
|
||||
|
||||
//update only if the message didn't originate from here
|
||||
if (packet.playerInfo.clientIndex != clientIndex) {
|
||||
playerCharacters[packet.playerInfo.playerIndex].SetPosition(packet.playerInfo.position);
|
||||
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion);
|
||||
}
|
||||
playerCharacters[packet.playerInfo.playerIndex].ResetDirection();
|
||||
}
|
||||
|
||||
void InWorld::SendState() {
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::PLAYER_UPDATE;
|
||||
packet.playerInfo.clientIndex = clientIndex;
|
||||
packet.playerInfo.playerIndex = playerIndex;
|
||||
// snprintf(packet.playerInfo.handle, PACKET_STRING_SIZE, "%s", config["player.handle"].c_str());
|
||||
// snprintf(packet.playerInfo.avatar, PACKET_STRING_SIZE, "%s", config["player.avatar"].c_str());
|
||||
packet.playerInfo.position = localCharacter->GetPosition();
|
||||
packet.playerInfo.motion = localCharacter->GetMotion();
|
||||
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
}
|
||||
|
||||
void InWorld::RequestDisconnect() {
|
||||
//send a disconnect request
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::DISCONNECT;
|
||||
packet.clientInfo.index = clientIndex;
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
}
|
||||
|
||||
void InWorld::RequestShutDown() {
|
||||
//send a shutdown request
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::SHUTDOWN;
|
||||
packet.clientInfo.index = clientIndex;
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -40,7 +40,7 @@ protected:
|
||||
void Update(double delta);
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//globals
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
|
||||
//members
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,25 +19,18 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef PLAYER_HPP_
|
||||
#define PLAYER_HPP_
|
||||
|
||||
#include "vector2.hpp"
|
||||
#ifndef ACCOUNTDATA_HPP_
|
||||
#define ACCOUNTDATA_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
/* Hold the player info.
|
||||
*/
|
||||
struct AccountData {
|
||||
std::string username;
|
||||
//TODO: password
|
||||
bool blackListed = false;
|
||||
bool whiteListed = true;
|
||||
|
||||
struct Player {
|
||||
int clientIndex;
|
||||
std::string handle;
|
||||
std::string avatar;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
};
|
||||
|
||||
typedef std::map<int, Player> PlayerMap;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,67 @@
|
||||
/* 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_data.hpp"
|
||||
|
||||
void CharacterData::Update(double delta) {
|
||||
if (motion.x && motion.y) {
|
||||
position += motion * delta * CHARACTER_WALKING_MOD;
|
||||
}
|
||||
else if (motion != 0) {
|
||||
position += motion * delta;
|
||||
}
|
||||
#ifdef GRAPHICS
|
||||
sprite.Update(delta);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef GRAPHICS
|
||||
|
||||
void CharacterData::DrawTo(SDL_Surface* const dest, int camX, int camY) {
|
||||
sprite.DrawTo(dest, position.x - camX, position.y - camY);
|
||||
}
|
||||
|
||||
void CharacterData::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);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,80 @@
|
||||
/* 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 CHARACTERDATA_HPP_
|
||||
#define CHARACTERDATA_HPP_
|
||||
|
||||
//POD members
|
||||
#include "bbox.hpp"
|
||||
#include "vector2.hpp"
|
||||
#include "statistics.hpp"
|
||||
|
||||
//graphics
|
||||
#ifdef GRAPHICS
|
||||
#include "sprite_sheet.hpp"
|
||||
#endif
|
||||
|
||||
//std namespace
|
||||
#include <string>
|
||||
#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);
|
||||
|
||||
struct CharacterData {
|
||||
//metadata
|
||||
int owner;
|
||||
std::string handle;
|
||||
std::string avatar;
|
||||
|
||||
//world position
|
||||
int mapIndex = 0;
|
||||
Vector2 position = {0.0,0.0};
|
||||
Vector2 motion = {0.0,0.0};
|
||||
|
||||
//base statistics
|
||||
Statistics stats;
|
||||
|
||||
//TODO: equipment
|
||||
//TODO: items
|
||||
//TODO: buffs
|
||||
//TODO: debuffs
|
||||
|
||||
//methods
|
||||
void Update(double delta);
|
||||
#ifdef GRAPHICS
|
||||
void DrawTo(SDL_Surface* const, int camX, int camY);
|
||||
void CorrectSprite();
|
||||
#endif
|
||||
|
||||
//active gameplay members
|
||||
//NOTE: these are lost when unloaded
|
||||
#ifdef GRAPHICS
|
||||
SpriteSheet sprite;
|
||||
#endif
|
||||
BBox bbox = {0,0,0,0};
|
||||
bool inCombat = false;
|
||||
int atbGauge = 0;
|
||||
//TODO: stored command
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,20 +19,13 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef CLIENT_HPP_
|
||||
#define CLIENT_HPP_
|
||||
#ifndef CLIENTDATA_HPP_
|
||||
#define CLIENTDATA_HPP_
|
||||
|
||||
#include "SDL/SDL_net.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
/* Hold the client info.
|
||||
*/
|
||||
|
||||
struct Client {
|
||||
IPaddress address;
|
||||
struct ClientData {
|
||||
IPaddress address = {0,0};
|
||||
};
|
||||
|
||||
typedef std::map<int, Client> ClientMap;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
/* 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 COMBATDATA_HPP_
|
||||
#define COMBATDATA_HPP_
|
||||
|
||||
//POD members
|
||||
#include "vector2.hpp"
|
||||
#include "bbox.hpp"
|
||||
|
||||
//gameplay members
|
||||
#include "character_data.hpp"
|
||||
#include "enemy_data.hpp"
|
||||
|
||||
//graphics
|
||||
#ifdef GRAPHICS
|
||||
#include "sprite_sheet.hpp"
|
||||
#endif
|
||||
|
||||
//std namespace
|
||||
#include <chrono>
|
||||
#include <list>
|
||||
#include <utility>
|
||||
|
||||
struct CombatData {
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
|
||||
//combatants, point to the std::map's internal pairs
|
||||
std::list<std::pair<const int, CharacterData>*> characterList;
|
||||
std::list<std::pair<const int, EnemyData>*> enemyList;
|
||||
|
||||
//world interaction
|
||||
int mapIndex = 0;
|
||||
Vector2 position = {0.0,0.0};
|
||||
BBox bbox = {0,0,0,0};
|
||||
|
||||
//time interval
|
||||
Clock::time_point lastTick = Clock::now();
|
||||
|
||||
//graphics
|
||||
#ifdef GRAPHICS
|
||||
SpriteSheet sprite;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
/* 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 ENEMYDATA_HPP_
|
||||
#define ENEMYDATA_HPP_
|
||||
|
||||
//gameplay
|
||||
#include "statistics.hpp"
|
||||
|
||||
//graphics
|
||||
#ifdef GRAPHICS
|
||||
#include "sprite_sheet.hpp"
|
||||
#endif
|
||||
|
||||
//std namespace
|
||||
#include <string>
|
||||
|
||||
struct EnemyData {
|
||||
//metadata
|
||||
std::string handle;
|
||||
std::string avatar;
|
||||
|
||||
//gameplay
|
||||
Statistics stats;
|
||||
|
||||
//TODO: equipment
|
||||
//TODO: items
|
||||
//TODO: buffs
|
||||
//TODO: debuffs
|
||||
|
||||
//active gameplay members
|
||||
//NOTE: these are lost when unloaded
|
||||
#ifdef GRAPHICS
|
||||
SpriteSheet sprite;
|
||||
#endif
|
||||
int tableIndex;
|
||||
int atbGauge = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,21 +1,18 @@
|
||||
#config
|
||||
INCLUDES+=. .. ../../common ../../common/graphics ../../common/map ../../common/network ../../common/ui
|
||||
INCLUDES+=. ../utilities ../graphics
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) -DGRAPHICS
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=..
|
||||
OUT=$(addprefix $(OUTDIR)/,libclient.a)
|
||||
OUTDIR=../..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
@@ -34,9 +31,6 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/* 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 "account_data.hpp"
|
||||
#include "character_data.hpp"
|
||||
#include "client_data.hpp"
|
||||
#include "combat_data.hpp"
|
||||
#include "enemy_data.hpp"
|
||||
#include "statistics.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.
|
||||
*
|
||||
* Oddly enough, I'm pretty sure this is the first directory compiled in a
|
||||
* clean build.
|
||||
*/
|
||||
@@ -0,0 +1,42 @@
|
||||
/* 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,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
#config
|
||||
INCLUDES+=. ..
|
||||
INCLUDES+=. ../map
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../..
|
||||
@@ -34,9 +31,6 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/* 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 xc, int yc) {
|
||||
XCount = xc;
|
||||
YCount = yc;
|
||||
image.LoadSurface(fname);
|
||||
image.SetClipW(image.GetClipW()/XCount);
|
||||
image.SetClipH(image.GetClipH()/YCount);
|
||||
}
|
||||
|
||||
void TileSheet::Unload() {
|
||||
image.FreeSurface();
|
||||
XCount = YCount = 0;
|
||||
}
|
||||
|
||||
void TileSheet::DrawTo(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,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,33 +19,36 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef TILESHEETMANAGER_HPP_
|
||||
#define TILESHEETMANAGER_HPP_
|
||||
#ifndef TILESHEET_HPP_
|
||||
#define TILESHEET_HPP_
|
||||
|
||||
#include "tile_sheet.hpp"
|
||||
#include "region.hpp"
|
||||
|
||||
#include "image.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class TileSheetManager {
|
||||
class TileSheet {
|
||||
public:
|
||||
TileSheetManager() = default;
|
||||
~TileSheetManager() = default;
|
||||
TileSheet() = default;
|
||||
TileSheet(std::string f, int x, int y) { Load(f, x, y); }
|
||||
~TileSheet() = default;
|
||||
|
||||
TileSheet* LoadSheet(std::string fname, Uint16 w, Uint16 h);
|
||||
TileSheet* GetSheet(std::string name);
|
||||
TileSheet* GetSheetByIndex(int tileIndex);
|
||||
void UnloadSheet(std::string name);
|
||||
void Load(std::string fname, int XCount, int YCount);
|
||||
void Unload();
|
||||
|
||||
void DrawTo(SDL_Surface* const, int x, int y, int tileIndex);
|
||||
void DrawTo(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);
|
||||
|
||||
int SetRangeEnd(int i) { return rangeEnd += i; }
|
||||
int GetRangeEnd() const { return rangeEnd; }
|
||||
|
||||
std::map<std::string, TileSheet>* GetSheetMap() { return &sheetMap; }
|
||||
//accessors
|
||||
Image* GetImage() { return ℑ }
|
||||
int GetXCount() { return XCount; }
|
||||
int GetYCount() { return YCount; }
|
||||
int GetTileW() { return image.GetClipW(); }
|
||||
int GetTileH() { return image.GetClipH(); }
|
||||
private:
|
||||
std::map<std::string, TileSheet> sheetMap;
|
||||
int rangeEnd = 0;
|
||||
Image image;
|
||||
int XCount = 0, YCount = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
+4
-38
@@ -1,45 +1,11 @@
|
||||
#config
|
||||
INCLUDES+=.
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
all:
|
||||
$(MAKE) -C gameplay
|
||||
$(MAKE) -C graphics
|
||||
$(MAKE) -C map
|
||||
$(MAKE) -C network
|
||||
$(MAKE) -C script
|
||||
$(MAKE) -C ui
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
$(MAKE) -C utilities
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
+2
-8
@@ -1,17 +1,14 @@
|
||||
#config
|
||||
INCLUDES+=. .. ../graphics
|
||||
INCLUDES+=. ../utilities
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../..
|
||||
@@ -34,9 +31,6 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/* 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 "map_allocator.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void BlankAllocator::Create(Region** const ptr, int x, int y) {
|
||||
(*ptr) = new Region(x, y);
|
||||
}
|
||||
|
||||
void BlankAllocator::Unload(Region* const ptr) {
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
void LuaAllocator::Create(Region** const ptr, int x, int y) {
|
||||
//something to work on
|
||||
(*ptr) = new Region(x, y);
|
||||
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "create");
|
||||
lua_pushlightuserdata(state, *ptr);
|
||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
|
||||
void LuaAllocator::Unload(Region* const ptr) {
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "unload");
|
||||
lua_pushlightuserdata(state, ptr);
|
||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
|
||||
//clean up the memory
|
||||
delete ptr;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/* 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 MAPALLOCATOR_HPP_
|
||||
#define MAPALLOCATOR_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
class BlankAllocator {
|
||||
public:
|
||||
void Create(Region** const, int x, int y);
|
||||
void Unload(Region* const);
|
||||
private:
|
||||
//
|
||||
};
|
||||
|
||||
class LuaAllocator {
|
||||
public:
|
||||
void Create(Region** const, int x, int y);
|
||||
void Unload(Region* const);
|
||||
|
||||
lua_State* SetLuaState(lua_State* L) { return state = L; }
|
||||
lua_State* GetLuaState() { return state; }
|
||||
private:
|
||||
lua_State* state = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
/* 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 "map_file_format.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void DummyFormat::Load(Region** const ptr, int x, int y) {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void DummyFormat::Save(Region* const ptr) {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void LuaFormat::Load(Region** const ptr, int x, int y) {
|
||||
//something to load into
|
||||
|
||||
if (!(*ptr)) {
|
||||
(*ptr) = new Region(x, y);
|
||||
}
|
||||
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "load");
|
||||
lua_pushlightuserdata(state, *ptr);
|
||||
lua_pushstring(state, saveDir.c_str());
|
||||
if (lua_pcall(state, 2, 1, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
if (lua_toboolean(state, -1) == false) {
|
||||
delete (*ptr);
|
||||
(*ptr) = nullptr;
|
||||
}
|
||||
lua_pop(state, 2);
|
||||
}
|
||||
|
||||
void LuaFormat::Save(Region* const ptr) {
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "save");
|
||||
lua_pushlightuserdata(state, ptr);
|
||||
lua_pushstring(state, saveDir.c_str());
|
||||
if (lua_pcall(state, 2, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,41 +19,42 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef TESTIFICATESCENE_HPP_
|
||||
#define TESTIFICATESCENE_HPP_
|
||||
#ifndef MAPFILEFORMAT_HPP_
|
||||
#define MAPFILEFORMAT_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "region.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "tile_Sheet_manager.hpp"
|
||||
#include "region_pager.hpp"
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
class TestificateScene : public BaseScene {
|
||||
#include <string>
|
||||
|
||||
class DummyFormat {
|
||||
public:
|
||||
//Public access members
|
||||
TestificateScene(ConfigUtility* const);
|
||||
~TestificateScene();
|
||||
void Load(Region** const, int x, int y);
|
||||
void Save(Region* const);
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||
std::string GetSaveDir() { return saveDir; }
|
||||
private:
|
||||
std::string saveDir;
|
||||
};
|
||||
|
||||
//Event handlers
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
//TODO: verbose save file format
|
||||
//TODO: compact save file format
|
||||
|
||||
//globals
|
||||
ConfigUtility& config;
|
||||
class LuaFormat {
|
||||
public:
|
||||
void Load(Region** const, int x, int y);
|
||||
void Save(Region* const);
|
||||
|
||||
//members
|
||||
TileSheetManager sheetMgr;
|
||||
RegionPager pager;
|
||||
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||
std::string GetSaveDir() { return saveDir; }
|
||||
|
||||
lua_State* SetLuaState(lua_State* L) { return state = L; }
|
||||
lua_State* GetLuaState() { return state; }
|
||||
private:
|
||||
std::string saveDir;
|
||||
lua_State* state = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
+10
-94
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -21,103 +21,19 @@
|
||||
*/
|
||||
#include "region.hpp"
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
Region::Region(int _x, int _y, int _w, int _h):
|
||||
x(_x),
|
||||
y(_y),
|
||||
width(_w),
|
||||
height(_h)
|
||||
Region::Region(int argX, int argY):
|
||||
x(argX),
|
||||
y(argY)
|
||||
{
|
||||
//make sure that the region's position lines up
|
||||
if (x != snapToBase(width, x) || y != snapToBase(height, y)) {
|
||||
std::ostringstream os;
|
||||
os << "Region is unaligned; x: " << x << ", y: " << y << ", width: " << width << ", height: " << height;
|
||||
throw(std::runtime_error(os.str()));
|
||||
for (register int i = 0; i < REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH; ++i) {
|
||||
*(reinterpret_cast<type_t*>(tiles) + i) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int Region::NewTileR(Tile const& tile) {
|
||||
//return 1 for overwrite, 0 for insert
|
||||
if (!InBoundsR(tile.x, tile.y)) {
|
||||
std::ostringstream os;
|
||||
os << "New tile location out of bounds: " <<
|
||||
"(" << x << "," << y << ")->" <<
|
||||
"(" << tile.x << "," << tile.y << ")"
|
||||
;
|
||||
throw(std::runtime_error(os.str()));
|
||||
}
|
||||
|
||||
int ret = tiles.erase(tile);
|
||||
tiles.insert(tile);
|
||||
return ret;
|
||||
Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
|
||||
return tiles[x][y][z] = v;
|
||||
}
|
||||
|
||||
Tile Region::GetTileR(int tx, int ty, int minDepth) {
|
||||
std::set<Tile>::iterator ptr = tiles.begin();
|
||||
|
||||
//skip the tiles that are too deep
|
||||
while(ptr != tiles.end()) {
|
||||
if (ptr->depth >= minDepth) {
|
||||
break;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
|
||||
//find the first tile here
|
||||
while(ptr != tiles.end()) {
|
||||
//bounds
|
||||
if ((ptr->x <= tx) && (ptr->y <= ty) && (ptr->x + ptr->width > tx) && (ptr->y + ptr->height > ty)) {
|
||||
break;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
|
||||
//found it
|
||||
if (ptr != tiles.end()) {
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
//a tileIndex of -1 is an error code, the rest is for show
|
||||
return {0,0,0,-1,-1,-1};
|
||||
}
|
||||
|
||||
int Region::DeleteTileR(Tile const& tile) {
|
||||
if (!InBoundsR(tile.x, tile.y)) {
|
||||
throw(std::runtime_error("Deleted tile location out of bounds"));
|
||||
|
||||
std::ostringstream os;
|
||||
os << "Deleted tile location out of bounds: " <<
|
||||
"(" << x << "," << y << ")->" <<
|
||||
"(" << tile.x << "," << tile.y << ")"
|
||||
;
|
||||
throw(std::runtime_error(os.str()));
|
||||
}
|
||||
//sentinel/error code
|
||||
if (tile.tileIndex == -1) {
|
||||
return 0;
|
||||
}
|
||||
return tiles.erase(tile);
|
||||
}
|
||||
|
||||
bool operator<(Region const& lhs, Region const& rhs) {
|
||||
//sort by y -> x
|
||||
if (lhs.y == rhs.y) {
|
||||
return lhs.x < rhs.x;
|
||||
}
|
||||
return lhs.y < rhs.y;
|
||||
}
|
||||
|
||||
inline bool operator>(Region const& lhs, Region const& rhs) {
|
||||
//wrap the other operator
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
inline bool operator==(Region const& lhs, Region const& rhs) {
|
||||
//comparisons work on the location ONLY
|
||||
//this function is redundant as far as the std::set object is concerned
|
||||
return (lhs.x == rhs.x) && (lhs.y == rhs.y);
|
||||
Region::type_t Region::GetTile(int x, int y, int z) {
|
||||
return tiles[x][y][z];
|
||||
}
|
||||
|
||||
+14
-76
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -22,91 +22,29 @@
|
||||
#ifndef REGION_HPP_
|
||||
#define REGION_HPP_
|
||||
|
||||
#include "tile.hpp"
|
||||
#define REGION_WIDTH 20
|
||||
#define REGION_HEIGHT 20
|
||||
#define REGION_DEPTH 3
|
||||
|
||||
#include <set>
|
||||
|
||||
/* A single section of the map.
|
||||
* This class stores the tiles relative to it's own position, but
|
||||
* there are functions for referencing the tiles' absolute position.
|
||||
* These functions simply wrap the normal functions.
|
||||
*/
|
||||
class Region {
|
||||
public:
|
||||
typedef unsigned short type_t;
|
||||
|
||||
Region() = delete;
|
||||
Region(int x, int y, int width, int height);
|
||||
Region(int x, int y);
|
||||
~Region() = default;
|
||||
|
||||
//create and insert a new tile, overwriting an existing tile at that location
|
||||
int NewTileR(Tile const& tile);
|
||||
int NewTileA(Tile const& tile) {
|
||||
//these can change, if the Tile class is changed
|
||||
return NewTileR({
|
||||
tile.x - x,
|
||||
tile.y - y,
|
||||
tile.depth,
|
||||
tile.width,
|
||||
tile.height,
|
||||
tile.tileIndex
|
||||
});
|
||||
}
|
||||
type_t SetTile(int x, int y, int z, type_t v);
|
||||
type_t GetTile(int x, int y, int z);
|
||||
|
||||
//find the first tile at this location, with the specified minimum depth
|
||||
Tile GetTileR(int tx, int ty, int minDepth);
|
||||
Tile GetTileA(int tx, int ty, int minDepth) {
|
||||
return GetTileR(tx - x, ty - y, minDepth);
|
||||
}
|
||||
|
||||
//wrap the other delete functions
|
||||
int DeleteTileR(int tx, int ty, int minDepth) {
|
||||
return DeleteTileR(GetTileR(tx, ty, minDepth));
|
||||
}
|
||||
int DeleteTileA(int tx, int ty, int minDepth) {
|
||||
//explicitly skip one function call by adjusting from A to R
|
||||
return DeleteTileR(GetTileR(tx - x, ty - y, minDepth));
|
||||
}
|
||||
|
||||
//delete the specified tile
|
||||
int DeleteTileR(Tile const& tile);
|
||||
int DeleteTileA(Tile const& tile) {
|
||||
//these can change, if the Tile class is changed
|
||||
return DeleteTileR({
|
||||
tile.x - x,
|
||||
tile.y - y,
|
||||
tile.depth,
|
||||
tile.width,
|
||||
tile.height,
|
||||
tile.tileIndex
|
||||
});
|
||||
}
|
||||
|
||||
//find if the specified location exists within the region's bounds
|
||||
bool InBoundsR(int i, int j) {
|
||||
return (i >= 0) && (j >= 0) && (i < width) && (j < height);
|
||||
}
|
||||
bool InBoundsA(int i, int j) {
|
||||
return InBoundsR(i - x, j - y);
|
||||
}
|
||||
|
||||
//Raw accessors & mutators
|
||||
//accessors
|
||||
int GetX() const { return x; }
|
||||
int GetY() const { return y; }
|
||||
int GetWidth() const { return width; }
|
||||
int GetHeight() const { return height; }
|
||||
|
||||
std::set<Tile>* GetTiles() { return &tiles; }
|
||||
|
||||
//sorting the regions by the locations
|
||||
friend bool operator<(Region const& lhs, Region const& rhs);
|
||||
friend bool operator>(Region const& lhs, Region const& rhs);
|
||||
friend bool operator==(Region const& lhs, Region const& rhs);
|
||||
|
||||
private:
|
||||
int const x;
|
||||
int const y;
|
||||
int const width;
|
||||
int const height;
|
||||
std::set<Tile> tiles;
|
||||
const int x;
|
||||
const int y;
|
||||
|
||||
type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+37
-91
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -23,99 +23,45 @@
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
RegionPager::RegionPager() {
|
||||
//
|
||||
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);
|
||||
}
|
||||
|
||||
RegionPager::~RegionPager() {
|
||||
if (onDelete) {
|
||||
for (auto& i : regionList) {
|
||||
onDelete(&i);
|
||||
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);
|
||||
}
|
||||
|
||||
Region* RegionPagerBase::GetRegion(int x, int y) {
|
||||
//snap the coords
|
||||
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) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//find the region
|
||||
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
|
||||
if ((*it)->GetX() == x && (*it)->GetY() == y) {
|
||||
return *it;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Region* RegionPager::NewRegion(int x, int y) {
|
||||
for (auto& i : regionList) {
|
||||
if (i.GetX() == x && i.GetY() == y) {
|
||||
throw(std::runtime_error("Duplicate Regions detected"));
|
||||
}
|
||||
}
|
||||
|
||||
regionList.push_front({x, y, regionWidth, regionHeight});
|
||||
if (onNew) {
|
||||
onNew(®ionList.front());
|
||||
}
|
||||
return ®ionList.front();
|
||||
}
|
||||
|
||||
Region* RegionPager::GetRegion(int x, int y) {
|
||||
for (auto& i : regionList) {
|
||||
if (i.GetX() == x && i.GetY() == y) {
|
||||
return &i;
|
||||
}
|
||||
}
|
||||
//create, insert and return
|
||||
regionList.push_front({x, y, regionWidth, regionHeight});
|
||||
if (onNew) {
|
||||
onNew(®ionList.front());
|
||||
}
|
||||
return ®ionList.front();
|
||||
}
|
||||
|
||||
void RegionPager::DeleteRegion(int x, int y) {
|
||||
for (std::list<Region>::iterator i = regionList.begin(); i != regionList.end(); i++) {
|
||||
if (i->GetX() == x && i->GetY() == y) {
|
||||
if (onDelete) {
|
||||
onDelete(&(*i));
|
||||
}
|
||||
regionList.erase(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RegionPager::DrawTo(SDL_Surface* const dest, TileSheetManager* const sheetMgr, int camX, int camY) {
|
||||
for (auto& regionIter : regionList) {
|
||||
|
||||
#ifdef DEBUG
|
||||
//draw the region's location
|
||||
SDL_Rect box = {
|
||||
Sint16(regionIter.GetX() - camX),
|
||||
Sint16(regionIter.GetY() - camY),
|
||||
Uint16(regionIter.GetWidth()),
|
||||
Uint16(regionIter.GetHeight())
|
||||
};
|
||||
SDL_FillRect(dest, &box, SDL_MapRGB(dest->format, 10, 10, 20));
|
||||
#endif
|
||||
|
||||
//draw each tile
|
||||
for (auto& tileIter : *regionIter.GetTiles()) {
|
||||
sheetMgr->DrawTo(
|
||||
dest,
|
||||
tileIter.x + regionIter.GetX() - camX,
|
||||
tileIter.y + regionIter.GetY() - camY,
|
||||
tileIter.tileIndex
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RegionPager::Prune(int left, int top, int right, int bottom) {
|
||||
std::list<Region>::iterator it = regionList.begin();
|
||||
|
||||
while(it != regionList.end()) {
|
||||
if (it->GetX() >= right || it->GetY() >= bottom || it->GetX() + it->GetWidth() < left || it->GetY() + it->GetHeight() < top) {
|
||||
if (onDelete) {
|
||||
onDelete(&(*it));
|
||||
}
|
||||
regionList.erase(it);
|
||||
it = regionList.begin();
|
||||
continue;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
}
|
||||
Region* RegionPagerBase::PushRegion(Region* ptr) {
|
||||
regionList.push_front(ptr);
|
||||
return regionList.front();
|
||||
}
|
||||
+92
-28
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -23,47 +23,111 @@
|
||||
#define REGIONPAGER_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
#include "tile_sheet_manager.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
class RegionPager {
|
||||
class RegionPagerBase {
|
||||
public:
|
||||
//for simplicity and consistency
|
||||
typedef void (*regionCallback_t)(Region* const);
|
||||
RegionPagerBase() {};
|
||||
virtual ~RegionPagerBase() {};
|
||||
|
||||
RegionPager();
|
||||
~RegionPager();
|
||||
//tile manipulation
|
||||
Region::type_t SetTile(int x, int y, int z, Region::type_t v);
|
||||
Region::type_t GetTile(int x, int y, int z);
|
||||
|
||||
//these parameters MUST be multiples of the width & height
|
||||
Region* NewRegion(int x, int y);
|
||||
//region manipulation
|
||||
Region* GetRegion(int x, int y);
|
||||
void DeleteRegion(int x, int y);
|
||||
Region* FindRegion(int x, int y);
|
||||
Region* PushRegion(Region*);
|
||||
|
||||
//call this to draw to the screen
|
||||
void DrawTo(SDL_Surface* const, TileSheetManager* const, int camX, int camY);
|
||||
//interface
|
||||
virtual Region* LoadRegion(int x, int y) = 0;
|
||||
virtual Region* SaveRegion(int x, int y) = 0;
|
||||
virtual Region* CreateRegion(int x, int y) = 0;
|
||||
virtual void UnloadRegion(int x, int y) = 0;
|
||||
//TODO: delete existing regions
|
||||
|
||||
//callback hooks
|
||||
void SetOnNew(regionCallback_t f) { onNew = f; }
|
||||
void SetOnDelete(regionCallback_t f) { onDelete = f; }
|
||||
//accessors & mutators
|
||||
std::list<Region*>* GetContainer() { return ®ionList; }
|
||||
protected:
|
||||
std::list<Region*> regionList;
|
||||
};
|
||||
|
||||
//params: Absolute values
|
||||
void Prune(int left, int top, int right, int bottom);
|
||||
template<typename Allocator, typename FileFormat>
|
||||
class RegionPager : public RegionPagerBase {
|
||||
public:
|
||||
RegionPager() {};
|
||||
~RegionPager() {
|
||||
UnloadAll();
|
||||
}
|
||||
|
||||
//accessors and mutators
|
||||
int SetWidth(int i) { return regionWidth = i; }
|
||||
int SetHeight(int i) { return regionHeight = i; }
|
||||
Region* LoadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
int GetWidth() const { return regionWidth; }
|
||||
int GetHeight() const { return regionHeight; }
|
||||
//load the region if possible
|
||||
Region* ptr = nullptr;
|
||||
format.Load(&ptr, x, y);
|
||||
if (ptr) {
|
||||
return PushRegion(ptr);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::list<Region>* GetRegions() { return ®ionList; }
|
||||
private:
|
||||
std::list<Region> regionList;
|
||||
int regionWidth = 0, regionHeight = 0;
|
||||
Region* SaveRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
regionCallback_t onNew = nullptr;
|
||||
regionCallback_t onDelete = nullptr;
|
||||
//find & save the region
|
||||
Region* ptr = FindRegion(x, y);
|
||||
if (ptr) {
|
||||
format.Save(ptr);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Region* CreateRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//create and push the object
|
||||
Region* ptr = nullptr;
|
||||
allocator.Create(&ptr, x, y);
|
||||
return PushRegion(ptr);
|
||||
}
|
||||
|
||||
void UnloadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//custom loop
|
||||
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
|
||||
if ((*it)->GetX() == x && (*it)->GetY() == y) {
|
||||
allocator.Unload(*it);
|
||||
it = regionList.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
void UnloadAll() {
|
||||
for (auto& it : regionList) {
|
||||
allocator.Unload(it);
|
||||
}
|
||||
regionList.clear();
|
||||
}
|
||||
|
||||
//accessors
|
||||
Allocator* GetAllocator() { return &allocator; }
|
||||
FileFormat* GetFormat() { return &format; }
|
||||
protected:
|
||||
Allocator allocator;
|
||||
FileFormat format;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
/* 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 "tile_sheet.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void TileSheet::Load(std::string fname, Uint16 w, Uint16 h) {
|
||||
//setup the image
|
||||
image.LoadSurface(fname);
|
||||
image.SetClipW(w);
|
||||
image.SetClipH(h);
|
||||
|
||||
//get the tile counts
|
||||
xCount = image.GetSurface()->w / w;
|
||||
yCount = image.GetSurface()->h / h;
|
||||
totalCount = xCount * yCount;
|
||||
|
||||
//set begin & end (usually temporary)
|
||||
begin = 0;
|
||||
end = totalCount;
|
||||
}
|
||||
|
||||
void TileSheet::Unload() {
|
||||
image.FreeSurface();
|
||||
totalCount = xCount = yCount = 0;
|
||||
begin = end = -1;
|
||||
}
|
||||
|
||||
void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) {
|
||||
if (!InRange(tileIndex)) {
|
||||
throw(std::invalid_argument("Tile index out of range"));
|
||||
}
|
||||
Sint16 clipX = (tileIndex-begin) % xCount * image.GetClipW();
|
||||
Sint16 clipY = (tileIndex-begin) / xCount * image.GetClipH();
|
||||
|
||||
image.SetClipX(clipX);
|
||||
image.SetClipY(clipY);
|
||||
|
||||
image.DrawTo(dest, x, y);
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
/* 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 TILESHEET_HPP_
|
||||
#define TILESHEET_HPP_
|
||||
|
||||
#include "image.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
/* The TileSheet class is used for drawing tiles of the map to the screen.
|
||||
* This class also tracks the range of the tile images.
|
||||
*/
|
||||
class TileSheet {
|
||||
public:
|
||||
TileSheet() = default;
|
||||
~TileSheet() = default;
|
||||
|
||||
//these load/set functions need to be followed by bookkeeping code
|
||||
//w & h are the width & height of individual tiles
|
||||
//TODO: rename these
|
||||
void Load(std::string fname, Uint16 w, Uint16 h);
|
||||
void Unload();
|
||||
|
||||
void DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex);
|
||||
|
||||
bool InRange(int i) { return i >= begin && i < end; }
|
||||
|
||||
//accessors and mutators
|
||||
Image* GetImage() { return ℑ }
|
||||
|
||||
int GetTileW() const { return image.GetClipW(); }
|
||||
int GetTileH() const { return image.GetClipH(); }
|
||||
|
||||
int GetTotalCount() const { return totalCount; }
|
||||
int GetXCount() const { return xCount; }
|
||||
int GetYCount() const { return yCount; }
|
||||
|
||||
int SetBegin(int i) { return begin = i; }
|
||||
int SetEnd(int i) { return end = i; }
|
||||
|
||||
int GetBegin() const { return begin; }
|
||||
int GetEnd() const { return end; }
|
||||
private:
|
||||
Image image;
|
||||
|
||||
//these are generated and used by internal processes
|
||||
int totalCount = 0, xCount = 0, yCount = 0;
|
||||
int begin = -1, end = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,73 +0,0 @@
|
||||
/* 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 "tile_sheet_manager.hpp"
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
TileSheet* TileSheetManager::LoadSheet(std::string fname, Uint16 w, Uint16 h) {
|
||||
//get the key
|
||||
std::string key = truncatePath(fname);
|
||||
|
||||
//don't override what's already here
|
||||
if (sheetMap.find(key) != sheetMap.end()) {
|
||||
throw(std::runtime_error("Cannot load duplicate tile sheets"));
|
||||
}
|
||||
|
||||
//load & setup the sheet object
|
||||
sheetMap[key].Load(fname, w, h);
|
||||
sheetMap[key].SetBegin(rangeEnd);
|
||||
rangeEnd += sheetMap[key].GetTotalCount();
|
||||
sheetMap[key].SetEnd(rangeEnd);
|
||||
|
||||
//you can modify the object, say, during the save & load routines...
|
||||
return &sheetMap[key];
|
||||
}
|
||||
|
||||
TileSheet* TileSheetManager::GetSheet(std::string name) {
|
||||
return &sheetMap.at(name);
|
||||
}
|
||||
|
||||
TileSheet* TileSheetManager::GetSheetByIndex(int tileIndex) {
|
||||
for (auto& it : sheetMap) {
|
||||
if (it.second.InRange(tileIndex)) {
|
||||
return &it.second;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void TileSheetManager::UnloadSheet(std::string name) {
|
||||
sheetMap.erase(name);
|
||||
}
|
||||
|
||||
void TileSheetManager::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) {
|
||||
for (auto& it : sheetMap) {
|
||||
if (it.second.InRange(tileIndex)) {
|
||||
it.second.DrawTo(dest, x, y, tileIndex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
//No matching tile index
|
||||
throw(std::invalid_argument("Tile index is out of range of all tile sheets"));
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
#config
|
||||
INCLUDES+=. ..
|
||||
INCLUDES+=. ../gameplay ../map ../utilities
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../..
|
||||
@@ -34,9 +31,6 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
/* 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 NETWORKPACKET_HPP_
|
||||
#define NETWORKPACKET_HPP_
|
||||
|
||||
#include "SDL/SDL_net.h"
|
||||
|
||||
#include "vector2.hpp"
|
||||
|
||||
#define PACKET_STRING_SIZE 100
|
||||
|
||||
#pragma pack(push, 0)
|
||||
|
||||
union NetworkPacket {
|
||||
//types of packets
|
||||
enum class Type {
|
||||
//default: there is something wrong
|
||||
NONE = 0,
|
||||
|
||||
//not used
|
||||
PING = 1,
|
||||
PONG = 2,
|
||||
|
||||
//Searching for a server to join
|
||||
BROADCAST_REQUEST = 3,
|
||||
BROADCAST_RESPONSE = 4,
|
||||
|
||||
//try to join the server
|
||||
JOIN_REQUEST = 5,
|
||||
JOIN_RESPONSE = 6,
|
||||
|
||||
//disconnect from the server
|
||||
DISCONNECT = 7,
|
||||
|
||||
//mass update
|
||||
SYNCHRONIZE = 8,
|
||||
|
||||
//shut down the server
|
||||
SHUTDOWN = 9,
|
||||
|
||||
//Player movement, etc.
|
||||
PLAYER_NEW = 10,
|
||||
PLAYER_DELETE = 11,
|
||||
PLAYER_UPDATE = 12,
|
||||
};
|
||||
|
||||
//metadata on the packet itself
|
||||
struct Metadata {
|
||||
Type type;
|
||||
IPaddress srcAddress;
|
||||
}meta;
|
||||
|
||||
//information about the server
|
||||
struct ServerInformation {
|
||||
Metadata meta;
|
||||
//TODO: version info
|
||||
char name[PACKET_STRING_SIZE];
|
||||
//TODO: player count
|
||||
}serverInfo;
|
||||
|
||||
//information about the client
|
||||
struct ClientInformation {
|
||||
Metadata meta;
|
||||
int index;
|
||||
}clientInfo;
|
||||
|
||||
//information about a player
|
||||
struct PlayerInformation {
|
||||
Metadata meta;
|
||||
int clientIndex;
|
||||
int playerIndex;
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
}playerInfo;
|
||||
|
||||
//defaults
|
||||
NetworkPacket() {
|
||||
meta.type = Type::NONE;
|
||||
meta.srcAddress = {0,0};
|
||||
}
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,433 @@
|
||||
/* 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 "map_allocator.hpp"
|
||||
#include "statistics.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
//-------------------------
|
||||
//Convenience Macros
|
||||
//-------------------------
|
||||
|
||||
#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer += size;
|
||||
#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer += size;
|
||||
|
||||
//-------------------------
|
||||
//internal serialization functions
|
||||
//-------------------------
|
||||
|
||||
void serializeType(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
}
|
||||
|
||||
void serializeServer(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//server info
|
||||
SERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int));
|
||||
SERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int));
|
||||
}
|
||||
|
||||
void serializeClient(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
SERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->clientInfo.accountIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
SERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE);
|
||||
//TODO: password
|
||||
SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
}
|
||||
|
||||
void serializeRegionFormat(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
}
|
||||
|
||||
void serializeRegionContent(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
|
||||
//content
|
||||
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->regionInfo.region->GetTile(i, j, k);
|
||||
buffer += sizeof(Region::type_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void serializeCombat(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//integers
|
||||
SERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int));
|
||||
//TODO: more comabat info
|
||||
}
|
||||
|
||||
void serializeStatistics(Statistics* stats, char* 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 serializeCharacter(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
SERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->characterInfo.accountIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
|
||||
//vectors
|
||||
SERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double));
|
||||
SERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double));
|
||||
SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
|
||||
SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
|
||||
|
||||
//stats structure
|
||||
serializeStatistics(&packet->characterInfo.stats, buffer);
|
||||
buffer += sizeof(Statistics);
|
||||
}
|
||||
|
||||
void serializeEnemy(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//texts
|
||||
SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
|
||||
//stats structure
|
||||
serializeStatistics(&packet->characterInfo.stats, buffer);
|
||||
buffer += sizeof(Statistics);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//internal deserialization functions
|
||||
//-------------------------
|
||||
|
||||
void deserializeType(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
}
|
||||
|
||||
void deserializeServer(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//server info
|
||||
DESERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int));
|
||||
DESERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int));
|
||||
}
|
||||
|
||||
void deserializeClient(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
DESERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->clientInfo.accountIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
DESERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE);
|
||||
//TODO: password
|
||||
DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
}
|
||||
|
||||
void deserializeRegionFormat(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
}
|
||||
|
||||
void deserializeRegionContent(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
|
||||
//an object to work on
|
||||
BlankAllocator().Create(
|
||||
&packet->regionInfo.region,
|
||||
packet->regionInfo.x,
|
||||
packet->regionInfo.y
|
||||
);
|
||||
|
||||
//content
|
||||
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->regionInfo.region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer));
|
||||
buffer += sizeof(Region::type_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void deserializeCombat(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//integers
|
||||
DESERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int));
|
||||
//TODO: more comabat info
|
||||
}
|
||||
|
||||
|
||||
void deserializeStatistics(Statistics* stats, char* 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));
|
||||
}
|
||||
|
||||
void deserializeCharacter(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
DESERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.accountIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
|
||||
//vectors
|
||||
DESERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
|
||||
|
||||
//stats structure
|
||||
deserializeStatistics(&packet->characterInfo.stats, buffer);
|
||||
buffer += sizeof(Statistics);
|
||||
}
|
||||
|
||||
void deserializeEnemy(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//texts
|
||||
DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
|
||||
//stats structure
|
||||
deserializeStatistics(&packet->characterInfo.stats, buffer);
|
||||
buffer += sizeof(Statistics);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//the interface functions
|
||||
//-------------------------
|
||||
|
||||
void serialize(SerialPacket* packet, void* buffer) {
|
||||
switch(packet->meta.type) {
|
||||
//no extra data
|
||||
case SerialPacket::Type::NONE:
|
||||
case SerialPacket::Type::PING:
|
||||
case SerialPacket::Type::PONG:
|
||||
case SerialPacket::Type::BROADCAST_REQUEST:
|
||||
|
||||
//all rejections
|
||||
case SerialPacket::Type::BROADCAST_REJECTION:
|
||||
case SerialPacket::Type::JOIN_REJECTION:
|
||||
case SerialPacket::Type::REGION_REJECTION:
|
||||
case SerialPacket::Type::CHARACTER_REJECTION:
|
||||
case SerialPacket::Type::ENEMY_REJECTION:
|
||||
case SerialPacket::Type::COMBAT_REJECTION:
|
||||
|
||||
serializeType(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//server info
|
||||
case SerialPacket::Type::BROADCAST_RESPONSE:
|
||||
serializeServer(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//client info
|
||||
case SerialPacket::Type::JOIN_REQUEST:
|
||||
case SerialPacket::Type::JOIN_RESPONSE:
|
||||
case SerialPacket::Type::SYNCHRONIZE:
|
||||
case SerialPacket::Type::DISCONNECT:
|
||||
case SerialPacket::Type::SHUTDOWN:
|
||||
serializeClient(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//region info
|
||||
case SerialPacket::Type::REGION_REQUEST:
|
||||
serializeRegionFormat(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
case SerialPacket::Type::REGION_CONTENT:
|
||||
serializeRegionContent(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//combat info
|
||||
case SerialPacket::Type::COMBAT_ENTER:
|
||||
case SerialPacket::Type::COMBAT_EXIT:
|
||||
serializeCombat(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//character info
|
||||
case SerialPacket::Type::CHARACTER_NEW:
|
||||
case SerialPacket::Type::CHARACTER_DELETE:
|
||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
||||
case SerialPacket::Type::CHARACTER_STATS_REQUEST:
|
||||
case SerialPacket::Type::CHARACTER_STATS_RESPONSE:
|
||||
serializeCharacter(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//enemy info
|
||||
case SerialPacket::Type::ENEMY_NEW:
|
||||
case SerialPacket::Type::ENEMY_DELETE:
|
||||
case SerialPacket::Type::ENEMY_UPDATE:
|
||||
case SerialPacket::Type::ENEMY_STATS_REQUEST:
|
||||
case SerialPacket::Type::ENEMY_STATS_RESPONSE:
|
||||
serializeEnemy(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void deserialize(SerialPacket* packet, void* buffer) {
|
||||
//find the type, so that you can actually deserialize the packet!
|
||||
deserializeType(packet, reinterpret_cast<char*>(buffer));
|
||||
switch(packet->meta.type) {
|
||||
//no extra data
|
||||
case SerialPacket::Type::NONE:
|
||||
case SerialPacket::Type::PING:
|
||||
case SerialPacket::Type::PONG:
|
||||
case SerialPacket::Type::BROADCAST_REQUEST:
|
||||
|
||||
//all rejections
|
||||
case SerialPacket::Type::BROADCAST_REJECTION:
|
||||
case SerialPacket::Type::JOIN_REJECTION:
|
||||
case SerialPacket::Type::REGION_REJECTION:
|
||||
case SerialPacket::Type::CHARACTER_REJECTION:
|
||||
case SerialPacket::Type::ENEMY_REJECTION:
|
||||
case SerialPacket::Type::COMBAT_REJECTION:
|
||||
|
||||
//NOTHING
|
||||
break;
|
||||
|
||||
//server info
|
||||
case SerialPacket::Type::BROADCAST_RESPONSE:
|
||||
deserializeServer(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//client info
|
||||
case SerialPacket::Type::JOIN_REQUEST:
|
||||
case SerialPacket::Type::JOIN_RESPONSE:
|
||||
case SerialPacket::Type::SYNCHRONIZE:
|
||||
case SerialPacket::Type::DISCONNECT:
|
||||
case SerialPacket::Type::SHUTDOWN:
|
||||
deserializeClient(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//region info
|
||||
case SerialPacket::Type::REGION_REQUEST:
|
||||
deserializeRegionFormat(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
case SerialPacket::Type::REGION_CONTENT:
|
||||
deserializeRegionContent(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//combat info
|
||||
case SerialPacket::Type::COMBAT_ENTER:
|
||||
case SerialPacket::Type::COMBAT_EXIT:
|
||||
serializeCombat(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//character info
|
||||
case SerialPacket::Type::CHARACTER_NEW:
|
||||
case SerialPacket::Type::CHARACTER_DELETE:
|
||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
||||
case SerialPacket::Type::CHARACTER_STATS_REQUEST:
|
||||
case SerialPacket::Type::CHARACTER_STATS_RESPONSE:
|
||||
deserializeCharacter(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//enemy info
|
||||
case SerialPacket::Type::ENEMY_NEW:
|
||||
case SerialPacket::Type::ENEMY_DELETE:
|
||||
case SerialPacket::Type::ENEMY_UPDATE:
|
||||
case SerialPacket::Type::ENEMY_STATS_REQUEST:
|
||||
case SerialPacket::Type::ENEMY_STATS_RESPONSE:
|
||||
serializeEnemy(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,32 +19,20 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef TILE_HPP_
|
||||
#define TILE_HPP_
|
||||
#ifndef SERIAL_HPP_
|
||||
#define SERIAL_HPP_
|
||||
|
||||
//explicitly a POD
|
||||
struct Tile {
|
||||
//position relative to the Region
|
||||
int x, y, depth;
|
||||
#include "serial_packet.hpp"
|
||||
|
||||
//graphics
|
||||
int width, height;
|
||||
int tileIndex;
|
||||
/* NOTE: Keep the PACKET_BUFFER_SIZE up to date
|
||||
* NOTE: REGION_CONTENT is currently the largest type of packet
|
||||
* map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t)
|
||||
* map format: sizeof(int) * 3
|
||||
* metadata: sizeof(SerialPacket::Type)
|
||||
*/
|
||||
#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Type)
|
||||
|
||||
Tile() = default;
|
||||
Tile(int _x, int _y, int _depth, int _width, int _height, int _tileIndex) {
|
||||
//The order of the arguments should be explicit
|
||||
x = _x;
|
||||
y = _y;
|
||||
depth = _depth;
|
||||
width = _width;
|
||||
height = _height;
|
||||
tileIndex = _tileIndex;
|
||||
}
|
||||
};
|
||||
|
||||
bool operator<(Tile const& lhs, Tile const& rhs);
|
||||
bool operator>(Tile const& lhs, Tile const& rhs);
|
||||
bool operator==(Tile const& lhs, Tile const& rhs);
|
||||
void serialize(SerialPacket* const, void* dest);
|
||||
void deserialize(SerialPacket* const, void* src);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,175 @@
|
||||
/* 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 "vector2.hpp"
|
||||
#include "region.hpp"
|
||||
#include "statistics.hpp"
|
||||
|
||||
#include "SDL/SDL_net.h"
|
||||
|
||||
#define NETWORK_VERSION 20140528
|
||||
#define PACKET_STRING_SIZE 100
|
||||
|
||||
union SerialPacket {
|
||||
//types of packets
|
||||
enum class Type {
|
||||
//default: there is something wrong
|
||||
NONE = 0,
|
||||
|
||||
//keep alive
|
||||
PING = 1,
|
||||
PONG = 2,
|
||||
|
||||
//searching for a server to join
|
||||
BROADCAST_REQUEST = 3,
|
||||
BROADCAST_RESPONSE = 4,
|
||||
BROADCAST_REJECTION = 5,
|
||||
|
||||
//try to join the server
|
||||
JOIN_REQUEST = 6,
|
||||
JOIN_RESPONSE = 7,
|
||||
JOIN_REJECTION = 8,
|
||||
|
||||
//mass update
|
||||
SYNCHRONIZE = 9,
|
||||
|
||||
//disconnect from the server
|
||||
DISCONNECT = 10,
|
||||
|
||||
//shut down the server
|
||||
SHUTDOWN = 11,
|
||||
|
||||
//map data
|
||||
REGION_REQUEST = 12,
|
||||
REGION_CONTENT = 13,
|
||||
REGION_REJECTION = 14,
|
||||
|
||||
//combat data
|
||||
COMBAT_ENTER = 15,
|
||||
COMBAT_EXIT = 16,
|
||||
|
||||
COMBAT_UPDATE = 17,
|
||||
|
||||
COMBAT_REJECTION = 18,
|
||||
|
||||
//character data
|
||||
CHARACTER_NEW = 19,
|
||||
CHARACTER_DELETE = 20,
|
||||
CHARACTER_UPDATE = 21,
|
||||
|
||||
CHARACTER_STATS_REQUEST = 22,
|
||||
CHARACTER_STATS_RESPONSE = 23,
|
||||
|
||||
CHARACTER_REJECTION = 24,
|
||||
|
||||
//enemy data
|
||||
ENEMY_NEW = 25,
|
||||
ENEMY_DELETE = 26,
|
||||
ENEMY_UPDATE = 27,
|
||||
|
||||
ENEMY_STATS_REQUEST = 28,
|
||||
ENEMY_STATS_RESPONSE = 29,
|
||||
|
||||
ENEMY_REJECTION = 30,
|
||||
|
||||
//more packet types go here
|
||||
|
||||
//not used
|
||||
LAST,
|
||||
};
|
||||
|
||||
//metadata on the packet itself
|
||||
struct Metadata {
|
||||
Type type;
|
||||
IPaddress srcAddress;
|
||||
}meta;
|
||||
|
||||
//info about the server
|
||||
struct ServerInformation {
|
||||
Metadata meta;
|
||||
int networkVersion;
|
||||
char name[PACKET_STRING_SIZE];
|
||||
int playerCount;
|
||||
}serverInfo;
|
||||
|
||||
//info about the client
|
||||
struct ClientInformation {
|
||||
Metadata meta;
|
||||
int clientIndex;
|
||||
int accountIndex;
|
||||
int characterIndex;
|
||||
char username[PACKET_STRING_SIZE];
|
||||
//TODO: password
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
}clientInfo;
|
||||
|
||||
//info about a region
|
||||
struct RegionInformation {
|
||||
Metadata meta;
|
||||
int mapIndex;
|
||||
int x, y;
|
||||
Region* region;
|
||||
}regionInfo;
|
||||
|
||||
//info about a combat scenario
|
||||
struct CombatInformation {
|
||||
Metadata meta;
|
||||
int combatIndex;
|
||||
int difficulty;
|
||||
//TODO: background image, based on terrain type
|
||||
//TODO: array of combatants
|
||||
//TODO: rewards
|
||||
}combatInfo;
|
||||
|
||||
//info about a character
|
||||
struct CharacterInformation {
|
||||
Metadata meta;
|
||||
int clientIndex;
|
||||
int accountIndex;
|
||||
int characterIndex;
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
int mapIndex;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
Statistics stats;
|
||||
}characterInfo;
|
||||
|
||||
//info about an enemy
|
||||
struct EnemyInformation {
|
||||
Metadata meta;
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
Statistics stats;
|
||||
}enemyInfo;
|
||||
|
||||
//defaults
|
||||
SerialPacket() {
|
||||
meta.type = Type::NONE;
|
||||
meta.srcAddress = {0,0};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -21,34 +21,33 @@
|
||||
*/
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
#include "serial.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"));
|
||||
}
|
||||
//DOCS: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network
|
||||
//NOTE: don't confuse SerialPacket with UDPpacket
|
||||
|
||||
if (!(packOut = SDLNet_AllocPacket(packSize))) {
|
||||
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 allocate the out packet"));
|
||||
}
|
||||
|
||||
if (!(packIn = SDLNet_AllocPacket(packSize))) {
|
||||
Close();
|
||||
throw(std::runtime_error("Failed to allocate the in packet"));
|
||||
throw(std::runtime_error("Failed to open UDPNetworkUtility"));
|
||||
}
|
||||
}
|
||||
|
||||
void UDPNetworkUtility::Close() {
|
||||
SDLNet_UDP_Close(socket);
|
||||
SDLNet_FreePacket(packOut);
|
||||
SDLNet_FreePacket(packIn);
|
||||
SDLNet_FreePacket(packet);
|
||||
socket = nullptr;
|
||||
packOut = nullptr;
|
||||
packIn = 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) {
|
||||
@@ -61,7 +60,7 @@ int UDPNetworkUtility::Bind(const char* ip, int port, int channel) {
|
||||
int UDPNetworkUtility::Bind(IPaddress* add, int channel) {
|
||||
int ret = SDLNet_UDP_Bind(socket, channel, add);
|
||||
|
||||
if (ret == -1) {
|
||||
if (ret < 0) {
|
||||
throw(std::runtime_error("Failed to bind to a channel"));
|
||||
}
|
||||
|
||||
@@ -72,25 +71,29 @@ void UDPNetworkUtility::Unbind(int channel) {
|
||||
SDLNet_UDP_Unbind(socket, channel);
|
||||
}
|
||||
|
||||
int UDPNetworkUtility::Send(const char* ip, int port, void* data, int len) {
|
||||
//-------------------------
|
||||
//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"));
|
||||
}
|
||||
|
||||
Send(&add, data, len);
|
||||
SendTo(&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"));
|
||||
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(packOut->data, 0, packOut->maxlen);
|
||||
memcpy(packOut->data, data, len);
|
||||
packOut->len = len;
|
||||
packOut->address = *add;
|
||||
memset(packet->data, 0, packet->maxlen);
|
||||
memcpy(packet->data, data, len);
|
||||
packet->len = len;
|
||||
packet->address = *add;
|
||||
|
||||
int ret = SDLNet_UDP_Send(socket, -1, packOut);
|
||||
int ret = SDLNet_UDP_Send(socket, -1, packet);
|
||||
|
||||
if (ret <= 0) {
|
||||
throw(std::runtime_error("Failed to send a packet"));
|
||||
@@ -99,15 +102,15 @@ int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) {
|
||||
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"));
|
||||
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(packOut->data, 0, packOut->maxlen);
|
||||
memcpy(packOut->data, data, len);
|
||||
packOut->len = len;
|
||||
memset(packet->data, 0, packet->maxlen);
|
||||
memcpy(packet->data, data, len);
|
||||
packet->len = len;
|
||||
|
||||
int ret = SDLNet_UDP_Send(socket, channel, packOut);
|
||||
int ret = SDLNet_UDP_Send(socket, channel, packet);
|
||||
|
||||
if (ret <= 0) {
|
||||
throw(std::runtime_error("Failed to send a packet"));
|
||||
@@ -116,29 +119,30 @@ int UDPNetworkUtility::Send(int channel, void* data, int len) {
|
||||
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"));
|
||||
int UDPNetworkUtility::SendToAllChannels(void* data, int len) {
|
||||
if (len > packet->maxlen) {
|
||||
throw(std::runtime_error("The buffer is to large for the UDPpacket"));
|
||||
}
|
||||
memset(packOut->data, 0, packOut->maxlen);
|
||||
memcpy(packOut->data, data, len);
|
||||
packOut->len = len;
|
||||
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, packOut);
|
||||
sent += SDLNet_UDP_Send(socket, i, packet);
|
||||
}
|
||||
}
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
//TODO: put a void* and int* parameter list here
|
||||
int UDPNetworkUtility::Receive() {
|
||||
memset(packIn->data, 0, packIn->maxlen);
|
||||
int ret = SDLNet_UDP_Recv(socket, packIn);
|
||||
memset(packet->data, 0, packet->maxlen);
|
||||
int ret = SDLNet_UDP_Recv(socket, packet);
|
||||
|
||||
if (ret < 0) {
|
||||
throw(std::runtime_error("Unknown network error occured"));
|
||||
@@ -146,3 +150,75 @@ int UDPNetworkUtility::Receive() {
|
||||
|
||||
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);
|
||||
serialize(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);
|
||||
serialize(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);
|
||||
serialize(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);
|
||||
deserialize(serialPacket, packet->data);
|
||||
serialPacket->meta.srcAddress = packet->address;
|
||||
|
||||
if (ret < 0) {
|
||||
throw(std::runtime_error("Unknown network error occured"));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -24,12 +24,14 @@
|
||||
|
||||
#include "SDL/SDL_net.h"
|
||||
|
||||
#include "serial_packet.hpp"
|
||||
|
||||
class UDPNetworkUtility {
|
||||
public:
|
||||
UDPNetworkUtility() = default;
|
||||
~UDPNetworkUtility() = default;
|
||||
|
||||
void Open(int port, int packSize);
|
||||
void Open(int port);
|
||||
void Close();
|
||||
|
||||
//bind to a channel
|
||||
@@ -41,31 +43,30 @@ public:
|
||||
return SDLNet_UDP_GetPeerAddress(socket, channel);
|
||||
}
|
||||
|
||||
int Send(const char* ip, int port, void* data, int len);
|
||||
int Send(IPaddress* add, void* data, int len);
|
||||
int Send(int channel, void* data, int len);
|
||||
int SendAll(void* data, int len);
|
||||
//send a buffer
|
||||
int SendTo(const char* ip, int port, void* data, int len);
|
||||
int SendTo(IPaddress* add, void* data, int len);
|
||||
int SendTo(int channel, void* data, int len);
|
||||
int SendToAllChannels(void* data, int len);
|
||||
int Receive();
|
||||
|
||||
void* GetOutData() const {
|
||||
return reinterpret_cast<void*>(packOut->data);
|
||||
};
|
||||
void* GetInData() const {
|
||||
return reinterpret_cast<void*>(packIn->data);
|
||||
};
|
||||
UDPpacket* GetOutPacket() const {
|
||||
return packOut;
|
||||
}
|
||||
UDPpacket* GetInPacket() const {
|
||||
return packIn;
|
||||
//send a SerialPacket
|
||||
int SendTo(const char* ip, int port, SerialPacket* serialPacket);
|
||||
int SendTo(IPaddress* add, SerialPacket* serialPacket);
|
||||
int SendTo(int channel, SerialPacket* serialPacket);
|
||||
int SendToAllChannels(SerialPacket* serialPacket);
|
||||
int Receive(SerialPacket* serialPacket);
|
||||
|
||||
//accessors
|
||||
UDPpacket* GetPacket() const {
|
||||
return packet;
|
||||
}
|
||||
UDPsocket GetSocket() const {
|
||||
return socket;
|
||||
}
|
||||
private:
|
||||
UDPsocket socket = nullptr;
|
||||
UDPpacket* packOut = nullptr;
|
||||
UDPpacket* packIn = nullptr;
|
||||
UDPpacket* packet = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $
|
||||
** Initialization of libraries for lua.c and other clients
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
/* Modified for use in Tortuga, renamed to linit.cpp
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** If you embed Lua in your program and need to open the standard
|
||||
** libraries, call luaL_openlibs in your program. If you need a
|
||||
** different set of libraries, copy this file to your project and edit
|
||||
** it to suit your needs.
|
||||
*/
|
||||
|
||||
|
||||
#define linit_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
#include "map_api.hpp"
|
||||
|
||||
|
||||
/*
|
||||
** these libs are loaded by lua.c and are readily available to any Lua
|
||||
** program
|
||||
*/
|
||||
static const luaL_Reg loadedlibs[] = {
|
||||
/* Standard libs */
|
||||
{"_G", luaopen_base},
|
||||
{LUA_LOADLIBNAME, luaopen_package},
|
||||
{LUA_COLIBNAME, luaopen_coroutine},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
{LUA_IOLIBNAME, luaopen_io},
|
||||
{LUA_OSLIBNAME, luaopen_os},
|
||||
{LUA_STRLIBNAME, luaopen_string},
|
||||
{LUA_BITLIBNAME, luaopen_bit32},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
|
||||
/* custom libs */
|
||||
{LUA_MAPLIBNAME, luaopen_mapapi},
|
||||
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** these libs are preloaded and must be required before used
|
||||
*/
|
||||
static const luaL_Reg preloadedlibs[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||
const luaL_Reg *lib;
|
||||
/* call open functions from 'loadedlibs' and set results to global table */
|
||||
for (lib = loadedlibs; lib->func; lib++) {
|
||||
luaL_requiref(L, lib->name, lib->func, 1);
|
||||
lua_pop(L, 1); /* remove lib */
|
||||
}
|
||||
/* add open functions from 'preloadedlibs' into 'package.preload' table */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
for (lib = preloadedlibs; lib->func; lib++) {
|
||||
lua_pushcfunction(L, lib->func);
|
||||
lua_setfield(L, -2, lib->name);
|
||||
}
|
||||
lua_pop(L, 1); /* remove _PRELOAD table */
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#config
|
||||
INCLUDES+=. ../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
|
||||
@@ -0,0 +1,126 @@
|
||||
/* 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 "map_api.hpp"
|
||||
|
||||
//map headers
|
||||
#include "map_allocator.hpp"
|
||||
#include "map_file_format.hpp"
|
||||
#include "region_pager.hpp"
|
||||
|
||||
//NOTE: When operating on a region, setTile() & getTile() *are not* zero indexed, but when operating on the entire map they *are* zero indexed.
|
||||
|
||||
static int setTile(lua_State* L) {
|
||||
if (lua_gettop(L) == 5) {
|
||||
//operating on a region
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
|
||||
}
|
||||
else {
|
||||
//operating on the whole map
|
||||
lua_pushstring(L, "pager");
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
//assume the pager is using lua
|
||||
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||
|
||||
//balance the stack
|
||||
lua_pop(L, 1);
|
||||
|
||||
pager->SetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getTile(lua_State* L) {
|
||||
if (lua_gettop(L) == 4) {
|
||||
//operating on a region
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
|
||||
lua_pushnumber(L, ret);
|
||||
}
|
||||
else {
|
||||
//operating on the whole map
|
||||
lua_pushstring(L, "pager");
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
//assume the pager is using lua
|
||||
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||
|
||||
//balance the stack
|
||||
lua_pop(L, 1);
|
||||
|
||||
int ret = pager->GetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3));
|
||||
lua_pushnumber(L, ret);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getX(lua_State* L) {
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
lua_pushinteger(L, ptr->GetX());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getY(lua_State* L) {
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
lua_pushinteger(L, ptr->GetY());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getRegionWidth(lua_State* L) {
|
||||
lua_pushinteger(L, REGION_WIDTH);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getRegionHeight(lua_State* L) {
|
||||
lua_pushinteger(L, REGION_HEIGHT);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getRegionDepth(lua_State* L) {
|
||||
lua_pushinteger(L, REGION_DEPTH);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dummy(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const luaL_Reg regionlib[] = {
|
||||
{"create", dummy},
|
||||
{"unload", dummy},
|
||||
{"load", dummy},
|
||||
{"save", dummy},
|
||||
{"settile",setTile},
|
||||
{"gettile",getTile},
|
||||
{"getx",getX},
|
||||
{"gety",getY},
|
||||
{"getregionwidth",getRegionWidth},
|
||||
{"getregionheight",getRegionHeight},
|
||||
{"getregiondepth",getRegionDepth},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
LUAMOD_API int luaopen_mapapi(lua_State* L) {
|
||||
luaL_newlib(L, regionlib);
|
||||
return 1;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,18 +19,12 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef SCENELIST_HPP_
|
||||
#define SCENELIST_HPP_
|
||||
#ifndef MAPAPI_HPP_
|
||||
#define MAPAPI_HPP_
|
||||
|
||||
enum class SceneList {
|
||||
//these are reserved
|
||||
QUIT,
|
||||
CONTINUE,
|
||||
FIRST,
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
//custom indexes
|
||||
TESTIFICATESCENE,
|
||||
EDITORSCENE,
|
||||
};
|
||||
#define LUA_MAPLIBNAME "map"
|
||||
LUAMOD_API int luaopen_mapapi(lua_State* L);
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
+2
-8
@@ -1,17 +1,14 @@
|
||||
#config
|
||||
INCLUDES+=. .. ../graphics
|
||||
INCLUDES+=. ../graphics
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../..
|
||||
@@ -34,9 +31,6 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/* 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 BBOX_HPP_
|
||||
#define BBOX_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
|
||||
//TODO: This is supposed to interact with the vector
|
||||
class BBox {
|
||||
public:
|
||||
double x, y;
|
||||
double w, h;
|
||||
|
||||
BBox() = default;
|
||||
BBox(double i, double j, double k, double l): x(i), y(j), w(k), h(l) {};
|
||||
~BBox() = default;
|
||||
BBox& operator=(BBox const&) = default;
|
||||
|
||||
double Size() {
|
||||
return std::max(w*h,0.0);
|
||||
}
|
||||
|
||||
bool IsCollision(BBox rhs) {
|
||||
return not (
|
||||
x >= rhs.x + rhs.w ||
|
||||
y >= rhs.y + rhs.h ||
|
||||
rhs.x >= x + w ||
|
||||
rhs.y >= y + h
|
||||
);
|
||||
}
|
||||
|
||||
BBox Intersection(BBox rhs) {
|
||||
if (!IsCollision(rhs)) {
|
||||
return {0, 0, 0, 0};
|
||||
}
|
||||
BBox ret;
|
||||
ret.x = std::max(x, rhs.x);
|
||||
ret.y = std::max(y, rhs.y);
|
||||
ret.w = std::min(x+w, rhs.x+rhs.w) - ret.x;
|
||||
ret.h = std::min(y+h, rhs.y+rhs.h) - ret.y;
|
||||
return ret;
|
||||
}
|
||||
|
||||
double operator[](size_t i) {
|
||||
if (i >= 4)
|
||||
throw(std::domain_error("Out of range"));
|
||||
return *(&x+i);
|
||||
}
|
||||
};
|
||||
|
||||
//This is explicitly a POD
|
||||
static_assert(std::is_pod<BBox>::value, "BBox is not a POD");
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,35 +19,30 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef EDITORAPPLICATION_HPP_
|
||||
#define EDITORAPPLICATION_HPP_
|
||||
#ifndef FRAMERATE_HPP_
|
||||
#define FRAMERATE_HPP_
|
||||
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
#include "config_utility.hpp"
|
||||
|
||||
class EditorApplication {
|
||||
private:
|
||||
EditorApplication();
|
||||
~EditorApplication();
|
||||
static EditorApplication instance;
|
||||
#include <chrono>
|
||||
|
||||
class FrameRate {
|
||||
public:
|
||||
static EditorApplication* GetInstance() { return &instance; }
|
||||
|
||||
void Init();
|
||||
void Proc();
|
||||
void Quit();
|
||||
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;
|
||||
}
|
||||
int GetFrameRate() { return lastFrameRate; }
|
||||
private:
|
||||
//Private access members
|
||||
void LoadScene(SceneList sceneIndex);
|
||||
void UnloadScene();
|
||||
|
||||
//globals
|
||||
ConfigUtility config;
|
||||
|
||||
BaseScene* activeScene = nullptr;
|
||||
int frameCount = 0;
|
||||
int lastFrameRate = 0;
|
||||
Clock::time_point tick = Clock::now();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
#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 2013
|
||||
/* 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
|
||||
@@ -19,26 +19,32 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "tile.hpp"
|
||||
#include "sql_utility.hpp"
|
||||
|
||||
bool operator<(Tile const& lhs, Tile const& rhs) {
|
||||
//sort by depth -> y -> x
|
||||
if (lhs.depth == rhs.depth) {
|
||||
if (lhs.y == rhs.y) {
|
||||
return lhs.x < rhs.x;
|
||||
}
|
||||
return lhs.y < rhs.y;
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
|
||||
int runSQLScript(sqlite3* db, std::string fname, int (*callback)(void*,int,char**,char**), void* argPtr) {
|
||||
//load the file into a string
|
||||
std::ifstream is(fname);
|
||||
if (!is.is_open()) {
|
||||
return -1;
|
||||
}
|
||||
return lhs.depth < rhs.depth;
|
||||
}
|
||||
std::string script;
|
||||
getline(is, script, '\0');
|
||||
is.close();
|
||||
|
||||
inline bool operator>(Tile const& lhs, Tile const& rhs) {
|
||||
//wrap the other operator
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
inline bool operator==(Tile const& lhs, Tile const& rhs) {
|
||||
//comparisons work on the location ONLY
|
||||
//this function is redundant as far as the std::set object is concerned
|
||||
return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.depth == rhs.depth);
|
||||
}
|
||||
//run the SQL loaded from the file
|
||||
char* errmsg = nullptr;
|
||||
int ret = sqlite3_exec(db, script.c_str(), callback, argPtr, &errmsg);
|
||||
if (ret != SQLITE_OK) {
|
||||
//handle any errors received from the SQL
|
||||
std::runtime_error e(std::string() + "SQL Script Error " + to_string_custom(ret) + ": " + errmsg);
|
||||
free(errmsg);
|
||||
throw(e);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/* 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 SERVERUTILITY_HPP_
|
||||
#define SERVERUTILITY_HPP_
|
||||
|
||||
#include "sqlite3/sqlite3.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
int runSQLScript(sqlite3* db, std::string fname, int (*callback)(void*,int,char**,char**) = nullptr, void* argPtr = nullptr);
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -27,9 +27,9 @@ int snapToBase(int base, int x) {
|
||||
//snap to a grid
|
||||
if (x < 0) {
|
||||
x++;
|
||||
return x - x % base - base;
|
||||
return x / base * base - base;
|
||||
}
|
||||
return x - x % base;
|
||||
return x / base * base;
|
||||
}
|
||||
|
||||
std::string truncatePath(std::string pathname) {
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -22,6 +22,7 @@
|
||||
#ifndef VECTOR2_HPP_
|
||||
#define VECTOR2_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
|
||||
@@ -29,7 +30,6 @@ class Vector2 {
|
||||
public:
|
||||
double x, y;
|
||||
|
||||
//This is explicitly a POD
|
||||
Vector2() = default;
|
||||
Vector2(double i, double j): x(i), y(j) {};
|
||||
~Vector2() = default;
|
||||
@@ -112,4 +112,7 @@ template<typename T> Vector2 operator/(T t, Vector2 v) { return v / t; }
|
||||
template<typename T> bool operator==(T t, Vector2 v) { return v == t; }
|
||||
template<typename T> bool operator!=(T t, Vector2 v) { return v != t; }
|
||||
|
||||
//This is explicitly a POD
|
||||
static_assert(std::is_pod<Vector2>::value, "Vector2 is not a POD");
|
||||
|
||||
#endif
|
||||
@@ -1,130 +0,0 @@
|
||||
/* 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 EditorApplications, 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 "editor_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
//-------------------------
|
||||
//Static declarations
|
||||
//-------------------------
|
||||
|
||||
EditorApplication EditorApplication::instance;
|
||||
|
||||
//-------------------------
|
||||
//Scene headers
|
||||
//-------------------------
|
||||
|
||||
//Add the custom scene headers here
|
||||
#include "editor_scene.hpp"
|
||||
#include "testificate_scene.hpp"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
EditorApplication::EditorApplication() {
|
||||
//
|
||||
}
|
||||
|
||||
EditorApplication::~EditorApplication() {
|
||||
//
|
||||
}
|
||||
|
||||
void EditorApplication::Init() {
|
||||
config.Load("rsc\\config.cfg");
|
||||
if (SDL_Init(SDL_INIT_VIDEO))
|
||||
throw(std::runtime_error("Failed to initialize SDL"));
|
||||
|
||||
BaseScene::SetScreen(config.Int("screen.w"), config.Int("screen.h"));
|
||||
}
|
||||
|
||||
void EditorApplication::Proc() {
|
||||
LoadScene(SceneList::FIRST);
|
||||
|
||||
//prepare the time system
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
|
||||
Clock::duration delta(16 * Clock::duration::period::den / std::milli::den);
|
||||
Clock::time_point simTime = Clock::now();
|
||||
Clock::time_point realTime;
|
||||
|
||||
//The main loop
|
||||
while(activeScene->GetNextScene() != SceneList::QUIT) {
|
||||
//switch scenes when necessary
|
||||
if (activeScene->GetNextScene() != SceneList::CONTINUE) {
|
||||
LoadScene(activeScene->GetNextScene());
|
||||
continue;
|
||||
}
|
||||
|
||||
//update the current time
|
||||
realTime = Clock::now();
|
||||
|
||||
//simulate game time
|
||||
while (simTime < realTime) {
|
||||
//call each user defined function
|
||||
activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den);
|
||||
simTime += delta;
|
||||
}
|
||||
|
||||
//draw the game to the screen
|
||||
activeScene->RenderFrame();
|
||||
|
||||
//give the computer a break
|
||||
SDL_Delay(10);
|
||||
}
|
||||
|
||||
UnloadScene();
|
||||
}
|
||||
|
||||
void EditorApplication::Quit() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Private access members
|
||||
//-------------------------
|
||||
|
||||
void EditorApplication::LoadScene(SceneList sceneIndex) {
|
||||
UnloadScene();
|
||||
|
||||
switch(sceneIndex) {
|
||||
//add scene creation calls here
|
||||
case SceneList::FIRST:
|
||||
case SceneList::EDITORSCENE:
|
||||
activeScene = new EditorScene(&config);
|
||||
break;
|
||||
|
||||
case SceneList::TESTIFICATESCENE:
|
||||
activeScene = new TestificateScene(&config);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
throw(std::logic_error("Failed to recognize the scene index"));
|
||||
}
|
||||
}
|
||||
|
||||
void EditorApplication::UnloadScene() {
|
||||
delete activeScene;
|
||||
activeScene = nullptr;
|
||||
}
|
||||
@@ -1,282 +0,0 @@
|
||||
/* 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 "editor_scene.hpp"
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
EditorScene::EditorScene(ConfigUtility* const arg1):
|
||||
config(*arg1)
|
||||
{
|
||||
//create the debugging "window"
|
||||
debugInfo.CreateSurface(256, 256);
|
||||
|
||||
//setup the utility objects
|
||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
||||
|
||||
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
buttonImage.SetClipH(buttonImage.GetClipH()/3);
|
||||
|
||||
//setup the menu bar
|
||||
menuBar.SetFont(&font);
|
||||
menuBar.SetImage(&buttonImage);
|
||||
|
||||
menuBar.SetEntries({
|
||||
{"File", "-New", "-Open", "-Save", "-Save As...", "-Close", "Exit"},
|
||||
{"Edit", "-Set Tile", "-Load Sheet", "-Delete Sheet", "-Metadata", "-Run Script"},
|
||||
{"Debugging", "Debug On", "Debug Off", "Toggle Debug", "Testificate"}
|
||||
});
|
||||
|
||||
//setup the pager
|
||||
pager.SetOnNew([](Region* const ptr){
|
||||
printf("New Region: %d, %d\n", ptr->GetX(), ptr->GetY());
|
||||
});
|
||||
|
||||
pager.SetOnDelete([](Region* const ptr){
|
||||
printf("Delete Region: %d, %d\n", ptr->GetX(), ptr->GetY());
|
||||
});
|
||||
|
||||
//Set a resonable size for the regions
|
||||
pager.SetWidth(32*4);
|
||||
pager.SetHeight(32*4);
|
||||
|
||||
sheetMgr.LoadSheet(config["dir.tilesets"] + "terrain.bmp", 32, 32);
|
||||
}
|
||||
|
||||
EditorScene::~EditorScene() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void EditorScene::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void EditorScene::Update(double delta) {
|
||||
pager.Prune(camera.x, camera.y, camera.x + GetScreen()->w, camera.y + GetScreen()->h);
|
||||
}
|
||||
|
||||
void EditorScene::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void EditorScene::Render(SDL_Surface* const screen) {
|
||||
//draw the map
|
||||
pager.DrawTo(screen, &sheetMgr, camera.x, camera.y);
|
||||
|
||||
//draw a big bar across the top
|
||||
buttonImage.SetClipY(0);
|
||||
for (int i = 0; i < screen->w; i += buttonImage.GetClipW()) {
|
||||
buttonImage.DrawTo(screen, i, 0);
|
||||
}
|
||||
|
||||
//draw the menu bar
|
||||
menuBar.DrawTo(screen);
|
||||
|
||||
//draw some debugging info
|
||||
if (debugOpen) {
|
||||
SDL_FillRect(debugInfo.GetSurface(), 0, 0);
|
||||
DrawToDebugInfo(string("camera.x: ") + to_string_custom(camera.x), 0);
|
||||
DrawToDebugInfo(string("camera.y: ") + to_string_custom(camera.y), 1);
|
||||
debugInfo.DrawTo(screen, screen->w - debugInfo.GetClipW(), buttonImage.GetClipH());
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::DrawToDebugInfo(std::string str, int line) {
|
||||
//draw the debug info on the right
|
||||
font.DrawStringTo(
|
||||
str,
|
||||
debugInfo.GetSurface(),
|
||||
debugInfo.GetClipW() - str.size() * font.GetCharW(),
|
||||
font.GetCharH() * line
|
||||
);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void EditorScene::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
menuBar.MouseMotion(motion);
|
||||
|
||||
if (motion.state & SDL_BUTTON_LMASK && motion.y >= buttonImage.GetClipH()) {
|
||||
Region* regionPtr = pager.GetRegion(
|
||||
snapToBase(pager.GetWidth(), motion.x + camera.x),
|
||||
snapToBase(pager.GetHeight(), motion.y + camera.y)
|
||||
);
|
||||
|
||||
TileSheet* sheetPtr = sheetMgr.GetSheetByIndex(tileCounter);
|
||||
|
||||
regionPtr->NewTileA({
|
||||
snapToBase(sheetPtr->GetTileW(), motion.x + camera.x), //x
|
||||
snapToBase(sheetPtr->GetTileH(), motion.y + camera.y), //y
|
||||
0, //depth
|
||||
sheetPtr->GetTileW(), //width
|
||||
sheetPtr->GetTileH(), //height
|
||||
tileCounter++ //value
|
||||
});
|
||||
}
|
||||
|
||||
if (motion.state & SDL_BUTTON_RMASK) {
|
||||
camera.x -= motion.xrel;
|
||||
camera.y -= motion.yrel;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
menuBar.MouseButtonDown(button);
|
||||
|
||||
if (button.button == SDL_BUTTON_LEFT && button.y >= buttonImage.GetClipH()) {
|
||||
Region* regionPtr = pager.GetRegion(
|
||||
snapToBase(pager.GetWidth(), button.x + camera.x),
|
||||
snapToBase(pager.GetHeight(), button.y + camera.y)
|
||||
);
|
||||
|
||||
TileSheet* sheetPtr = sheetMgr.GetSheetByIndex(tileCounter);
|
||||
|
||||
regionPtr->NewTileA({
|
||||
snapToBase(sheetPtr->GetTileW(), button.x + camera.x), //x
|
||||
snapToBase(sheetPtr->GetTileH(), button.y + camera.y), //y
|
||||
0, //depth
|
||||
sheetPtr->GetTileW(), //width
|
||||
sheetPtr->GetTileH(), //height
|
||||
tileCounter++ //value
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
int entry, drop;
|
||||
menuBar.MouseButtonUp(button, &entry, &drop);
|
||||
|
||||
//manage input from the menu bar
|
||||
switch(entry) {
|
||||
case 0: //File
|
||||
switch(drop) {
|
||||
case 0:
|
||||
//TODO: NEW
|
||||
break;
|
||||
|
||||
case 1:
|
||||
//TODO: OPEN
|
||||
break;
|
||||
|
||||
case 2:
|
||||
//TODO: SAVE
|
||||
break;
|
||||
|
||||
case 3:
|
||||
//TODO: SAVE AS
|
||||
break;
|
||||
|
||||
case 4:
|
||||
//TODO: CLOSE
|
||||
break;
|
||||
|
||||
case 5: {
|
||||
//Quit
|
||||
SDL_Event e;
|
||||
e.type = SDL_QUIT;
|
||||
SDL_PushEvent(&e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 1: //Edit
|
||||
switch(drop) {
|
||||
case 0:
|
||||
//TODO: SET TILE
|
||||
break;
|
||||
|
||||
case 1:
|
||||
//TODO: LOAD SHEET
|
||||
break;
|
||||
|
||||
case 2:
|
||||
//TODO: DELETE SHEET
|
||||
break;
|
||||
|
||||
case 3:
|
||||
//TODO: METADATA
|
||||
break;
|
||||
|
||||
case 4:
|
||||
//TODO: RUN SCRIPT
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 2: //Debug
|
||||
switch(drop) {
|
||||
case 0:
|
||||
debugOpen = true;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
debugOpen = false;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
debugOpen = !debugOpen;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
SetNextScene(SceneList::TESTIFICATESCENE);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
break;
|
||||
|
||||
case SDLK_SPACE:
|
||||
camera.x = 0;
|
||||
camera.y = 0;
|
||||
break;
|
||||
|
||||
case SDLK_TAB:
|
||||
debugOpen = !debugOpen;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
/* 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 EDITORSCENE_HPP_
|
||||
#define EDITORSCENE_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "region_pager.hpp"
|
||||
#include "tile_sheet_manager.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "menu_bar.hpp"
|
||||
|
||||
//#include "map_loader.hpp"
|
||||
|
||||
class EditorScene : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
EditorScene(ConfigUtility* const);
|
||||
~EditorScene();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//globals
|
||||
ConfigUtility& config;
|
||||
|
||||
//debugging tools
|
||||
void DrawToDebugInfo(std::string, int line);
|
||||
Image debugInfo;
|
||||
bool debugOpen = true;
|
||||
|
||||
RegionPager pager;
|
||||
TileSheetManager sheetMgr;
|
||||
|
||||
RasterFont font;
|
||||
Image buttonImage;
|
||||
|
||||
MenuBar menuBar;
|
||||
|
||||
struct {
|
||||
int x = 0, y = 0;
|
||||
} camera;
|
||||
|
||||
int tileCounter = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,43 +0,0 @@
|
||||
#config
|
||||
INCLUDES+=../common ../common/graphics ../common/map ../common/ui
|
||||
LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,editor)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -1,107 +0,0 @@
|
||||
/* 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 "testificate_scene.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
TestificateScene::TestificateScene(ConfigUtility* const arg1):
|
||||
config(*arg1)
|
||||
{
|
||||
sheetMgr.LoadSheet(config["dir.tilesets"] + "grass.bmp", 32, 32);
|
||||
sheetMgr.LoadSheet(config["dir.tilesets"] + "longgrass.bmp", 32, 32);
|
||||
sheetMgr.LoadSheet(config["dir.tilesets"] + "sand.bmp", 32, 32);
|
||||
sheetMgr.LoadSheet(config["dir.tilesets"] + "dirt.bmp", 32, 32);
|
||||
sheetMgr.LoadSheet(config["dir.tilesets"] + "water.bmp", 32, 32);
|
||||
|
||||
cout << "Range End: " << sheetMgr.GetRangeEnd() << endl;
|
||||
|
||||
pager.SetWidth(128);
|
||||
pager.SetHeight(128);
|
||||
|
||||
pager.GetRegion(0, 0)->NewTileR({0, 0, 0, 32, 32, 0});
|
||||
}
|
||||
|
||||
TestificateScene::~TestificateScene() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void TestificateScene::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::Update(double delta) {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::Render(SDL_Surface* const screen) {
|
||||
//dump all tile graphics to the screen
|
||||
for (int i = 0; i < sheetMgr.GetRangeEnd(); i++) {
|
||||
sheetMgr.DrawTo(screen, i * 32 % screen->w, i * 32 / screen->w * 32, i);
|
||||
}
|
||||
|
||||
// pager.DrawTo(screen, &sheetMgr, 0, 0);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void TestificateScene::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
|
||||
|
||||
void TestificateScene::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
// QuitEvent();
|
||||
SetNextScene(SceneList::EDITORSCENE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TestificateScene::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
@@ -14,7 +14,6 @@ all: $(OUTDIR)
|
||||
$(MAKE) -C common
|
||||
$(MAKE) -C server
|
||||
$(MAKE) -C client
|
||||
$(MAKE) -C editor
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
+8
-2
@@ -17,8 +17,14 @@ dir.tilesets = rsc/graphics/tilesets/
|
||||
dir.interface = rsc/graphics/interface/
|
||||
dir.scripts = rsc/scripts/
|
||||
|
||||
#map system
|
||||
map.pager.width = 20
|
||||
map.pager.height = 20
|
||||
map.pager.depth = 3
|
||||
|
||||
#player options
|
||||
player.handle = username
|
||||
player.avatar = elliot2.bmp
|
||||
client.username = Kayne Ruse
|
||||
client.handle = Ratstail91
|
||||
client.avatar = elliot2.bmp
|
||||
|
||||
#debugging
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 18 KiB |
@@ -0,0 +1,36 @@
|
||||
print("Lua script check (./rsc)")
|
||||
|
||||
-------------------------
|
||||
--Map API overrides
|
||||
-------------------------
|
||||
|
||||
function map.create(region)
|
||||
for i = 1, map.getregionwidth() do
|
||||
for j = 1, map.getregionheight() do
|
||||
if math.abs(map.getx(region) + i -1) == math.abs(map.gety(region) + j -1) then
|
||||
map.settile(region, i, j, 1, 50)
|
||||
else
|
||||
map.settile(region, i, j, 1, 14)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function map.unload(region)
|
||||
--
|
||||
end
|
||||
|
||||
function map.load(region, dir)
|
||||
--return true if file loaded, otherwise return false
|
||||
return false
|
||||
end
|
||||
|
||||
function map.save(region, dir)
|
||||
--
|
||||
end
|
||||
|
||||
-------------------------
|
||||
--Enemy API
|
||||
-------------------------
|
||||
|
||||
--TODO
|
||||
@@ -1,73 +1,63 @@
|
||||
-------------------------
|
||||
--Server
|
||||
-------------------------
|
||||
|
||||
CREATE TABLE IF NOT EXISTS UserAccounts (
|
||||
userAccountID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username varchar(30) UNIQUE,
|
||||
password varchar(30),
|
||||
CREATE TABLE IF NOT EXISTS Accounts (
|
||||
uid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username varchar(100) UNIQUE,
|
||||
--TODO: server-client security
|
||||
-- password varchar(100),
|
||||
blacklisted BIT DEFAULT 0,
|
||||
whitelisted BIT DEFAULT 1
|
||||
whitelisted BIT DEFAULT 1,
|
||||
administrator BIT DEFAULT 0
|
||||
);
|
||||
|
||||
-------------------------
|
||||
--Items
|
||||
-------------------------
|
||||
CREATE TABLE IF NOT EXISTS Characters (
|
||||
uid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
CREATE TABLE IF NOT EXISTS GlobalItemList (
|
||||
globalItemListID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
itemName varchar(30) UNIQUE,
|
||||
itemImage varchar(30),
|
||||
type varchar(15), --{'mundane', 'consumable', 'equipment'}
|
||||
maxStackSize INTEGER, --{1-max; 0 for non-stackable}
|
||||
maxUniqueCopies INTEGER --{1-max; 0 for unlimited}
|
||||
);
|
||||
--metadata
|
||||
owner INTEGER REFERENCES Accounts(uid),
|
||||
handle varchar(100) UNIQUE,
|
||||
avatar varchar(100),
|
||||
birth timestamp NOT NULL DEFAULT (datetime()),
|
||||
|
||||
CREATE TABLE IF NOT EXISTS MundaneItems (
|
||||
mundaneItemID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID)
|
||||
--holds whatever
|
||||
);
|
||||
--position
|
||||
mapIndex INTEGER DEFAULT 0,
|
||||
positionX INTEGER DEFAULT 0,
|
||||
positionY INTEGER DEFAULT 0,
|
||||
|
||||
CREATE TABLE IF NOT EXISTS Consumables (
|
||||
consumableID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID)
|
||||
--holds all consumable items info (food, potions, etc.)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS Equipment (
|
||||
equipmentID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID)
|
||||
--hold all equipment info
|
||||
);
|
||||
|
||||
-------------------------
|
||||
--Players
|
||||
-------------------------
|
||||
|
||||
CREATE TABLE IF NOT EXISTS PlayerCharacters (
|
||||
playerCharacterID INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name varchar(30) UNIQUE,
|
||||
|
||||
--stats
|
||||
currentLevel INTEGER DEFAULT 0,
|
||||
currentExperience INTEGER DEFAULT 0,
|
||||
maxHealth INTEGER DEFAULT 0,
|
||||
maxMana INTEGER DEFAULT 0,
|
||||
currentHealth INTEGER DEFAULT 0,
|
||||
currentMana INTEGER DEFAULT 0,
|
||||
attack INTEGER DEFAULT 0,
|
||||
defence INTEGER DEFAULT 0,
|
||||
--etc.
|
||||
--statistics
|
||||
level INTEGER DEFAULT 0,
|
||||
exp INTEGER DEFAULT 0,
|
||||
maxHP INTEGER DEFAULT 0,
|
||||
health INTEGER DEFAULT 0,
|
||||
maxMP INTEGER DEFAULT 0,
|
||||
mana INTEGER DEFAULT 0,
|
||||
attack INTEGER DEFAULT 0,
|
||||
defence INTEGER DEFAULT 0,
|
||||
intelligence INTEGER DEFAULT 0,
|
||||
resistance INTEGER DEFAULT 0,
|
||||
speed INTEGER DEFAULT 0,
|
||||
accuracy REAL DEFAULT 0.0,
|
||||
evasion REAL DEFAULT 0.0,
|
||||
luck REAL DEFAULT 0.0,
|
||||
|
||||
--equipment
|
||||
weapon INTEGER REFERENCES Equipment(equipmentID),
|
||||
helmet INTEGER REFERENCES Equipment(equipmentID),
|
||||
armour INTEGER REFERENCES Equipment(equipmentID)
|
||||
weapon INTEGER REFERENCES WornEquipment(uid),
|
||||
helmet INTEGER REFERENCES WornEquipment(uid),
|
||||
armour INTEGER REFERENCES WornEquipment(uid)
|
||||
--etc.
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS PlayerInventoryItems (
|
||||
characterID INTEGER REFERENCES PlayerCharacters(characterID),
|
||||
globalItemListID INTEGER REFERENCES GlobalItemList(globalItemListID)
|
||||
CREATE TABLE IF NOT EXISTS InventoryItems (
|
||||
--metadata
|
||||
uid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
itemID INTEGER, --type
|
||||
stackSize INTEGER DEFAULT 0,
|
||||
owner INTEGER REFERENCES Characters(uid)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS WornEquipment (
|
||||
--metadata
|
||||
uid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
itemID INTEGER, --type
|
||||
owner INTEGER REFERENCES Characters(uid)
|
||||
--hold all equipment info
|
||||
--stat mods, special effects, etc.
|
||||
);
|
||||
@@ -0,0 +1,191 @@
|
||||
/* 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 "server_application.hpp"
|
||||
|
||||
#include "sqlite3/sqlite3.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Define the queries
|
||||
//-------------------------
|
||||
|
||||
static const char* CREATE_USER_ACCOUNT = "INSERT INTO Accounts (username) VALUES (?);";
|
||||
static const char* LOAD_USER_ACCOUNT = "SELECT * FROM Accounts WHERE username = ?;";
|
||||
static const char* SAVE_USER_ACCOUNT = "UPDATE OR FAIL Accounts SET blacklisted = ?2, whitelisted = ?3 WHERE uid = ?1;";
|
||||
static const char* DELETE_USER_ACCOUNT = "DELETE FROM Accounts WHERE uid = ?;";
|
||||
|
||||
//-------------------------
|
||||
//Define the methods
|
||||
//-------------------------
|
||||
|
||||
int ServerApplication::CreateUserAccount(std::string username, int clientIndex) {
|
||||
//create this user account, failing if it exists, leave this account in memory
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, CREATE_USER_ACCOUNT, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameter
|
||||
if (sqlite3_bind_text(statement, 1, username.c_str(), username.size() + 1, SQLITE_STATIC) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
//if this fails, than this account exists
|
||||
sqlite3_finalize(statement);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sqlite3_finalize(statement);
|
||||
|
||||
//load this account into memory
|
||||
return LoadUserAccount(username, clientIndex);
|
||||
}
|
||||
|
||||
int ServerApplication::LoadUserAccount(std::string username, int clientIndex) {
|
||||
//load this user account, failing if it is in memory, creating it if it doesn't exist
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, LOAD_USER_ACCOUNT, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameter
|
||||
if (sqlite3_bind_text(statement, 1, username.c_str(), username.size() + 1, SQLITE_STATIC) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
int ret = sqlite3_step(statement);
|
||||
|
||||
//process the result
|
||||
if (ret == SQLITE_ROW) {
|
||||
//get the index
|
||||
int uid = sqlite3_column_int(statement, 0);
|
||||
|
||||
//check to see if this account is already loaded
|
||||
if (accountMap.find(uid) != accountMap.end()) {
|
||||
sqlite3_finalize(statement);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//extract the data into memory
|
||||
AccountData& newAccount = accountMap[uid];
|
||||
newAccount.username = reinterpret_cast<const char*>(sqlite3_column_text(statement, 1));
|
||||
newAccount.blackListed = sqlite3_column_int(statement, 2);
|
||||
newAccount.whiteListed = sqlite3_column_int(statement, 3);
|
||||
newAccount.clientIndex = clientIndex;
|
||||
|
||||
//finish the routine
|
||||
sqlite3_finalize(statement);
|
||||
return uid;
|
||||
}
|
||||
|
||||
sqlite3_finalize(statement);
|
||||
|
||||
if (ret == SQLITE_DONE) {
|
||||
//create the non-existant account instead
|
||||
return CreateUserAccount(username, clientIndex);
|
||||
}
|
||||
|
||||
throw(std::runtime_error(std::string() + "Unknown SQL error in LoadUserAccount: " + sqlite3_errmsg(database) ));
|
||||
}
|
||||
|
||||
int ServerApplication::SaveUserAccount(int uid) {
|
||||
//save this user account from memory, replacing it if it exists in the database
|
||||
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
|
||||
|
||||
//this method fails if this account is not loaded
|
||||
if (accountMap.find(uid) == accountMap.end()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
AccountData& account = accountMap[uid];
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, SAVE_USER_ACCOUNT, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameters
|
||||
bool ret = false;
|
||||
ret |= sqlite3_bind_int(statement, 1, uid) != SQLITE_OK;
|
||||
ret |= sqlite3_bind_int(statement, 2, account.blackListed) != SQLITE_OK;
|
||||
ret |= sqlite3_bind_int(statement, 3, account.whiteListed) != SQLITE_OK;
|
||||
|
||||
//check for binding errors
|
||||
if (ret) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
//if this fails, than something went horribly wrong
|
||||
sqlite3_finalize(statement);
|
||||
throw( std::runtime_error(std::string() + "Unknown SQL error when saving an account: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
sqlite3_finalize(statement);
|
||||
|
||||
//successful execution
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ServerApplication::UnloadUserAccount(int uid) {
|
||||
//save this user account, and then unload it
|
||||
//NOTE: the associated characters are unloaded externally
|
||||
SaveUserAccount(uid);
|
||||
accountMap.erase(uid);
|
||||
}
|
||||
|
||||
void ServerApplication::DeleteUserAccount(int uid) {
|
||||
//delete a user account from the database, and remove it from memory
|
||||
//NOTE: the associated characters are unloaded externally
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, DELETE_USER_ACCOUNT, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameter
|
||||
if (sqlite3_bind_int(statement, 1, uid) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
//if this fails, than something went horribly wrong
|
||||
sqlite3_finalize(statement);
|
||||
throw( std::runtime_error(std::string() + "Unknown SQL error when deleting an account: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//finish the routine
|
||||
sqlite3_finalize(statement);
|
||||
accountMap.erase(uid);
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
/* 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 "server_application.hpp"
|
||||
|
||||
#include "sqlite3/sqlite3.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Define the queries
|
||||
//-------------------------
|
||||
|
||||
//TODO: save and load the statistics
|
||||
static const char* CREATE_CHARACTER = "INSERT INTO Characters (owner, handle, avatar) VALUES (?, ?, ?);";
|
||||
static const char* LOAD_CHARACTER = "SELECT * FROM Characters WHERE handle = ?;";
|
||||
static const char* SAVE_CHARACTER = "UPDATE OR FAIL Characters SET mapIndex = ?2, positionX = ?3, positionY = ?4 WHERE uid = ?1;";
|
||||
static const char* DELETE_CHARACTER = "DELETE FROM Characters WHERE uid = ?;";
|
||||
|
||||
//-------------------------
|
||||
//Define the methods
|
||||
//-------------------------
|
||||
|
||||
//TODO: default stats as a parameter
|
||||
int ServerApplication::CreateCharacter(int owner, std::string handle, std::string avatar) {
|
||||
//Create the character, failing if it exists
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, CREATE_CHARACTER, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameters
|
||||
bool ret = false;
|
||||
ret |= sqlite3_bind_int(statement, 1, owner);
|
||||
ret |= sqlite3_bind_text(statement, 2, handle.c_str(), handle.size() + 1, SQLITE_STATIC);
|
||||
ret |= sqlite3_bind_text(statement, 3, avatar.c_str(), avatar.size() + 1, SQLITE_STATIC);
|
||||
|
||||
//check for binding errors
|
||||
if (ret) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
//if this fails, than this character exists
|
||||
sqlite3_finalize(statement);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sqlite3_finalize(statement);
|
||||
|
||||
//load this character into memory
|
||||
return LoadCharacter(owner, handle, avatar);
|
||||
}
|
||||
|
||||
int ServerApplication::LoadCharacter(int owner, std::string handle, std::string avatar) {
|
||||
//load the specified character, creating it if it doesn't exist
|
||||
//fail if it is already loaded, or does not belong to this account
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, LOAD_CHARACTER, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameter
|
||||
if (sqlite3_bind_text(statement, 1, handle.c_str(), handle.size() + 1, SQLITE_STATIC) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
int ret = sqlite3_step(statement);
|
||||
|
||||
//process the result
|
||||
if (ret == SQLITE_ROW) {
|
||||
//get the index
|
||||
int uid = sqlite3_column_int(statement, 0);
|
||||
|
||||
//check to see if this character is already loaded
|
||||
if (characterMap.find(uid) != characterMap.end()) {
|
||||
sqlite3_finalize(statement);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//check the owner
|
||||
if (owner != sqlite3_column_int(statement, 1)) {
|
||||
sqlite3_finalize(statement);
|
||||
return -2;
|
||||
}
|
||||
|
||||
//extract the data into memory
|
||||
CharacterData& newChar = characterMap[uid];
|
||||
|
||||
//metadata
|
||||
newChar.owner = owner;
|
||||
newChar.handle = reinterpret_cast<const char*>(sqlite3_column_text(statement, 2));
|
||||
newChar.avatar = reinterpret_cast<const char*>(sqlite3_column_text(statement, 3));
|
||||
//Don't cache the birth
|
||||
|
||||
//world position
|
||||
newChar.mapIndex = sqlite3_column_int(statement, 5);
|
||||
newChar.position.x = (double)sqlite3_column_int(statement, 6);
|
||||
newChar.position.y = (double)sqlite3_column_int(statement, 7);
|
||||
|
||||
//statistics
|
||||
newChar.stats.level = sqlite3_column_int(statement, 8);
|
||||
newChar.stats.exp = sqlite3_column_int(statement, 9);
|
||||
newChar.stats.maxHP = sqlite3_column_int(statement, 10);
|
||||
newChar.stats.health = sqlite3_column_int(statement, 11);
|
||||
newChar.stats.maxMP = sqlite3_column_int(statement, 12);
|
||||
newChar.stats.mana = sqlite3_column_int(statement, 13);
|
||||
newChar.stats.attack = sqlite3_column_int(statement, 14);
|
||||
newChar.stats.defence = sqlite3_column_int(statement, 15);
|
||||
newChar.stats.intelligence = sqlite3_column_int(statement, 16);
|
||||
newChar.stats.resistance = sqlite3_column_int(statement, 17);
|
||||
newChar.stats.speed = sqlite3_column_int(statement, 18);
|
||||
newChar.stats.accuracy = sqlite3_column_double(statement, 19);
|
||||
newChar.stats.evasion = sqlite3_column_double(statement, 20);
|
||||
newChar.stats.luck = sqlite3_column_double(statement, 21);
|
||||
|
||||
//TODO: equipment
|
||||
|
||||
//finish the routine
|
||||
sqlite3_finalize(statement);
|
||||
return uid;
|
||||
}
|
||||
|
||||
sqlite3_finalize(statement);
|
||||
|
||||
if (ret == SQLITE_DONE) {
|
||||
//create the non-existant character instead
|
||||
return CreateCharacter(owner, handle, avatar);
|
||||
}
|
||||
|
||||
throw(std::runtime_error(std::string() + "Unknown SQL error in LoadCharacter: " + sqlite3_errmsg(database) ));
|
||||
}
|
||||
|
||||
int ServerApplication::SaveCharacter(int uid) {
|
||||
//save this character from memory, replacing it if it exists in the database
|
||||
//DOCS: To use this method, change the in-memory copy, and then call this function using that object's UID.
|
||||
|
||||
//this method fails if this character is not loaded
|
||||
if (characterMap.find(uid) == characterMap.end()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
CharacterData& character = characterMap[uid];
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, SAVE_CHARACTER, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameters
|
||||
bool ret = false;
|
||||
ret |= sqlite3_bind_int(statement, 1, uid) != SQLITE_OK;
|
||||
ret |= sqlite3_bind_int(statement, 2, character.mapIndex) != SQLITE_OK;
|
||||
ret |= sqlite3_bind_int(statement, 3, (int)character.position.x) != SQLITE_OK;
|
||||
ret |= sqlite3_bind_int(statement, 4, (int)character.position.y) != SQLITE_OK;
|
||||
//TODO: stats, etc.
|
||||
|
||||
//check for binding errors
|
||||
if (ret) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
//if this fails, than something went horribly wrong
|
||||
sqlite3_finalize(statement);
|
||||
throw( std::runtime_error(std::string() + "Unknown SQL error when saving an account: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
sqlite3_finalize(statement);
|
||||
|
||||
//successful execution
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ServerApplication::UnloadCharacter(int uid) {
|
||||
//save this character, then unload it
|
||||
SaveCharacter(uid);
|
||||
characterMap.erase(uid);
|
||||
}
|
||||
|
||||
void ServerApplication::DeleteCharacter(int uid) {
|
||||
//delete this character from the database, then remove it from memory
|
||||
sqlite3_stmt* statement = nullptr;
|
||||
|
||||
//prep
|
||||
if (sqlite3_prepare_v2(database, DELETE_CHARACTER, -1, &statement, nullptr) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to prepare an SQL statement: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//parameter
|
||||
if (sqlite3_bind_int(statement, 1, uid) != SQLITE_OK) {
|
||||
throw( std::runtime_error(std::string() + "Failed to replace a prepared statement's parameter: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//execute
|
||||
if (sqlite3_step(statement) != SQLITE_DONE) {
|
||||
//if this fails, than something went horribly wrong
|
||||
sqlite3_finalize(statement);
|
||||
throw( std::runtime_error(std::string() + "Unknown SQL error when deleting an account: " + sqlite3_errmsg(database)) );
|
||||
}
|
||||
|
||||
//finish the routine
|
||||
sqlite3_finalize(statement);
|
||||
characterMap.erase(uid);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,24 +19,16 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "editor_application.hpp"
|
||||
#include "enemy_factory_generic.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int, char**) {
|
||||
cout << "Beginning editor" << endl;
|
||||
try {
|
||||
EditorApplication::GetInstance()->Init();
|
||||
EditorApplication::GetInstance()->Proc();
|
||||
EditorApplication::GetInstance()->Quit();
|
||||
}
|
||||
catch(exception& e) {
|
||||
cerr << "Fatal exception thrown: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
cout << "Clean exit" << endl;
|
||||
return 0;
|
||||
EnemyFactoryGeneric::EnemyFactoryGeneric() : EnemyFactoryInterface() {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
EnemyFactoryGeneric::~EnemyFactoryGeneric() noexcept {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void EnemyFactoryGeneric::Generate(std::list<EnemyData>* container) {
|
||||
//TODO: fill this out
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/* 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 ENEMYFACTORYGENERIC_HPP_
|
||||
#define ENEMYFACTORYGENERIC_HPP_
|
||||
|
||||
#include "enemy_factory_interface.hpp"
|
||||
|
||||
#include "enemy_data.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
//DOCS: Not really intended for use, but rather for copying and tweaking
|
||||
class EnemyFactoryGeneric : public EnemyFactoryInterface {
|
||||
public:
|
||||
EnemyFactoryGeneric();
|
||||
~EnemyFactoryGeneric() noexcept override;
|
||||
|
||||
void Generate(std::list<EnemyData>* container) override;
|
||||
private:
|
||||
//TODO: hold the parameters specified by the room
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -19,30 +19,38 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef INCOMBAT_HPP_
|
||||
#define INCOMBAT_HPP_
|
||||
#ifndef ENEMYFACTORYINTERFACE_HPP_
|
||||
#define ENEMYFACTORYINTERFACE_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "enemy_data.hpp"
|
||||
|
||||
class InCombat : public BaseScene {
|
||||
#include <list>
|
||||
|
||||
//TODO: move this elsewhere
|
||||
enum RoomType {
|
||||
OVERWORLD,
|
||||
RUINS,
|
||||
TOWERS,
|
||||
FORESTS,
|
||||
CAVES,
|
||||
};
|
||||
|
||||
//NOTE: Based on biome, world difficulty, etc.
|
||||
class EnemyFactoryInterface {
|
||||
public:
|
||||
//Public access members
|
||||
InCombat();
|
||||
~InCombat();
|
||||
EnemyFactoryInterface() = default;
|
||||
virtual ~EnemyFactoryInterface() = default;
|
||||
|
||||
virtual void Generate(std::list<EnemyData>* container) = 0;
|
||||
|
||||
//control the difficulty of the room
|
||||
RoomType SetType(RoomType t) { return type = t; }
|
||||
int SetDifficulty(int d) { return difficulty = d; }
|
||||
RoomType GetType() { return type; }
|
||||
int GetDifficulty() { return difficulty; }
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
RoomType type;
|
||||
int difficulty;
|
||||
};
|
||||
|
||||
#endif
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* 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
|
||||
@@ -33,7 +33,7 @@ int main(int argc, char** argv) {
|
||||
try {
|
||||
ServerApplication app;
|
||||
app.Init(argc, argv);
|
||||
app.Loop();
|
||||
app.Proc();
|
||||
app.Quit();
|
||||
}
|
||||
catch(exception& e) {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user