Compare commits
98 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e2f6a2835 | |||
| 8f4cc1c28f | |||
| 1425e0facd | |||
| 8f419b4beb | |||
| d87d90f2c1 | |||
| cdc2c88623 | |||
| 0ea37b1032 | |||
| 34d1b614e9 | |||
| 8e205330bb | |||
| bed7cdb1c2 | |||
| fb7a5f1a2b | |||
| 7365b65206 | |||
| 65abfb7414 | |||
| 8279d83d53 | |||
| 9a655eff45 | |||
| 223a71ceff | |||
| d32e8e4242 | |||
| dfa0a4e810 | |||
| 5110ab3c8c | |||
| af5b975bb4 | |||
| febd93d3ae | |||
| bd38d39736 | |||
| 2b4de1eaee | |||
| f19c0136b8 | |||
| b97148d8a3 | |||
| c56f308f7b | |||
| d8af39148f | |||
| 56e2207638 | |||
| c540da95ca | |||
| c146266dc8 | |||
| 99bbc7a024 | |||
| 501a67b014 | |||
| 7623bc2c4f | |||
| 1034ebe46d | |||
| 0d4e1163f8 | |||
| 5d9351455c | |||
| b9d5513f8b | |||
| 36420b76d9 | |||
| 6f9052c4ea | |||
| 6f8d798082 | |||
| 036c8359d7 | |||
| 8c8cfa4b5a | |||
| a85240d68f | |||
| 2590323319 | |||
| f39157c5c4 | |||
| 06816312ff | |||
| 1a9dcb63c4 | |||
| e2e86967e7 | |||
| 75b32b2e6d | |||
| 54b9825c09 | |||
| f8ba393f3b | |||
| 403de9df43 | |||
| fdf8f69d3c | |||
| bdf3cd6c54 | |||
| db63e378f7 | |||
| 4fa2413a0b | |||
| e011baae1a | |||
| 32063f657d | |||
| abc0a89237 | |||
| 0596f89b9b | |||
| 814faa4262 | |||
| 14d36ae471 | |||
| abf8e4e803 | |||
| 4bbc0f131d | |||
| 91f7cda041 | |||
| 692e6d03aa | |||
| f649ff1148 | |||
| 68acd0812a | |||
| 84b87f91b6 | |||
| cf1d0e878e | |||
| bc075c20ad | |||
| f14ffc7780 | |||
| 18bf188d58 | |||
| 40c23bbc3d | |||
| 200e54ddad | |||
| 1396941337 | |||
| e868a922bc | |||
| 94b1a64175 | |||
| a5619b4eec | |||
| 0fa95818e0 | |||
| d6037b1a0c | |||
| 918455d0cf | |||
| 580b1693bd | |||
| 1e4587d25a | |||
| e52a7f4e0b | |||
| d0fc789576 | |||
| 1e7c42be93 | |||
| 3c9843af73 | |||
| c016fd97ed | |||
| d62e3d7149 | |||
| 6f657e5d1a | |||
| 067f1a253f | |||
| da0d326861 | |||
| ca5cdd9c36 | |||
| e7165a5a65 | |||
| 8fa2e747e1 | |||
| 920f8b18c0 | |||
| a78f00d577 |
@@ -1,21 +0,0 @@
|
||||
#Editor generated files
|
||||
#*.sln
|
||||
#*.vcproj
|
||||
*.suo
|
||||
*.ncb
|
||||
*.user
|
||||
|
||||
#Directories
|
||||
Release/
|
||||
Debug/
|
||||
Out/
|
||||
|
||||
#Project generated files
|
||||
*.db
|
||||
*.o
|
||||
*.a
|
||||
*.exe
|
||||
|
||||
#Shell files
|
||||
*.bat
|
||||
*.sh
|
||||
@@ -1,31 +0,0 @@
|
||||
_This project is currently in it's early prototype stage. You can see other versions in various branches or tagged as prototype-X._
|
||||
|
||||
Tortuga is a 2D multiplayer role playing game set in a large archipelago occupied by warring pirate clans. The emphasis of this game is on multiplayer cooperation, competition, and server customization. The game runs on highly customizable server software that can support up to 150 simultaneous players or more. The player characters are tied to the server where they are created and are susceptible to permadeath: deletion of a character upon death.
|
||||
|
||||
## Libraries
|
||||
|
||||
* [SDL](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
||||
* [SDL_net](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension. This must be installed first.
|
||||
* [Codebase](https://github.com/Ratstail91/Codebase) - generic reusable code modules
|
||||
|
||||
## Instructions
|
||||
|
||||
* This project uses C++11, which is available via GNU (or MinGW) 4.7, or Visual Studio 2012. Personally, I'm using MinGW 4.7.2.
|
||||
* I'm trying to keep this as IDE agnostic as possible, so if you use an IDE, please add it's files to .gitignore.
|
||||
* You can read more details on the Tortuga wiki [here](https://github.com/Ratstail91/Tortuga/wiki).
|
||||
|
||||
## Copyright
|
||||
|
||||
The current version of Tortuga is released under the [zlib license](http://en.wikipedia.org/wiki/Zlib_License).
|
||||
|
||||
Copyright (c) 2013 Kayne Ruse
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
||||
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.
|
||||
@@ -0,0 +1,57 @@
|
||||
--TODO: each function needs to check it's parameter count
|
||||
--TODO: Set metatables
|
||||
|
||||
--RoomManager interface
|
||||
RoomMgr.GetRoom(index)
|
||||
RoomMgr.CreateRoom([{params}])
|
||||
RoomMgr.UnloadRoom(index)
|
||||
|
||||
|
||||
--RoomData interface
|
||||
Room.GetPager()
|
||||
Room.GetGenerator()
|
||||
Room.OnCreate([{params}])
|
||||
Room.OnUnload()
|
||||
|
||||
Room.Get{params}()
|
||||
|
||||
|
||||
--RegionPager interface
|
||||
RegionPager.SetTile(x, y, z, v)
|
||||
RegionPager.GetTile(x, y, z)
|
||||
RegionPager.SetSolid(x, y, b)
|
||||
RegionPager.GetSolid(x, y)
|
||||
RegionPager.GetRegion(x, y)
|
||||
RegionPager.SetDirectory(directory)
|
||||
RegionPager.GetDirectory()
|
||||
RegionPager.LoadRegion(x, y)
|
||||
RegionPager.SaveRegion(x, y)
|
||||
RegionPager.CreateRegion(x, y[, {params}])
|
||||
RegionPager.UnloadRegion(x, y)
|
||||
|
||||
|
||||
--Region interface
|
||||
Region.SetTile(x, y, z, v)
|
||||
Region.GetTile(x, y, z)
|
||||
Region.SetSolid(x, y, b)
|
||||
Region.GetSolid(x, y)
|
||||
Region.GetX()
|
||||
Region.GetY()
|
||||
Region.GetWidth()
|
||||
Region.GetHeight()
|
||||
Region.GetDepth()
|
||||
Region.OnLoad(directory)
|
||||
Region.OnSave(directory)
|
||||
Region.OnCreate([{params}])
|
||||
Region.OnUnload([directory])
|
||||
|
||||
|
||||
--*Generator interface
|
||||
Generator.GetMapType()
|
||||
Generator.GetChunk(x, y)
|
||||
Generator.GetMapWidth()
|
||||
Generator.GetMapHeight()
|
||||
|
||||
|
||||
--Chunk interface
|
||||
??
|
||||
@@ -0,0 +1,19 @@
|
||||
My game Tortuga will have permadeath; it's actually one of the first design decisions I made. How to implement it, however, is another decision all together.
|
||||
|
||||
A while ago, before the implementation of hardcore mode, I was playing on a Minecraft server that boasted something close to permadeath: your account would be banned for 15 days, so long that it's almost unavoidable that you'd lose all of your hidden items. This was a brutal server; the further you went from spawn, the more dangerous it became, not less. If the natural hazards didn't kill you, another player would.
|
||||
|
||||
I loved it.
|
||||
|
||||
Although I haven't been back to that server since I died for the last time, the experience will stay with me forever. I'd played and died on that server a few times, each time I had to wait for my ban to clear. I never really lost much, since I never survived for very long. Even today, in a single player world, I'm likely to die on the first night.
|
||||
|
||||
After playing for a while, I began to understand the mentality needed to survive there. Always fear other people, never take risks, and never hoard valuable items if they could be better spent keeping you alive. One day, the last day I played, I found a hole in the ground that someone was obviously using as a "hidden" base. I tried to get in, always careful not to trigger any traps. However, while I was trying to get in, the owner came home.
|
||||
|
||||
I was suddenly attacked from behind, I barely had enough sense to dig down, since they were wearing enchanted diamond armour. I had no chance against them. I thought I was safe, digging 10, 20, 30 meters down, but no, they poured lava down the hole and plugged it up. I was a gonner, and I knew it. I was futilely digging and thrashing around in what was now my tomb, about to lose my life. I'd survived for so long, only to lose it all by not keeping a lookout.
|
||||
|
||||
When the game over screen flashed up, I screamed. I screamed, and screamed, and screamed. For 5 minutes, my mind was blank, nothing but hatred and pain and loss. I'm sure you've seen the video of the angry German kid who died in WoW, but have you ever actually experienced that pain? Have you ever worked so hard, and lost it all?
|
||||
|
||||
For days afterwards, that loss was all I could think about. Even now, that experience stays with me. What about that game, that server, invoked so much pain that I almost lost consciousness from screaming? Me of all people, who thought he was invincible?
|
||||
|
||||
I've played games that have moved me, terrified me, made me fall in love and fight for the people that I care about. But never have I played a game that has made me feel pain and loss like that. I died that day. Me.
|
||||
|
||||
Pain and loss are part of life, but not games. Permadeath seems like the obvious choice for creating a sense of loss for the player, but there are so few games with permadeath, and even fewer multiplayer games. Pain and loss can be conveyed in other ways, I'm sure anybody who's played Final Fantasy 7 knows that, and some games are especially well suited to delivering that message. However, for a player to feel like they are the one who's died, that is a challenge.
|
||||
@@ -0,0 +1,125 @@
|
||||
Abstract:
|
||||
|
||||
The goal of this pseudocode is to create a collision system that brings several
|
||||
colliding bodies to a near-contact state (no space between the bodies, but not
|
||||
overlapping), while preserving the motion of said bodies. For simplicity, I've
|
||||
decided to use a square box (BoundingBox, or BBox for short) for the collidable
|
||||
bodies.
|
||||
|
||||
Example:
|
||||
|
||||
My current game has a tiled-map, where the tiles are arranged on a 2D grid. The
|
||||
tiles each have a flag indicating if they are solid (i.e. collidable) or not. I
|
||||
also have characters walking around on these tile maps, that must not intersect
|
||||
with solid tiles. For stylistic reasons, I want characters moving at an angle
|
||||
to "slide" along these walls, and continue along with their original velocity
|
||||
when they've circumvented the obstacles.
|
||||
|
||||
I'd also like to keep the possibility of non-grid collisions using this logic
|
||||
open (for objects like trees, etc.) if the code allows it.
|
||||
|
||||
Obviously, there are many in depth issues that I will need to take into
|
||||
account when writing this logic, that have been glossed over or omitted in this
|
||||
article.
|
||||
|
||||
-------------------------
|
||||
|
||||
```
|
||||
velocity = motion + speed
|
||||
if (collision(position + velocity)) then
|
||||
if (collision(position + {velocity.x, 0})) then
|
||||
velocity.x = 0
|
||||
end
|
||||
if (collision(position + {0, velocity.y})) then
|
||||
velocity.y = 0
|
||||
end
|
||||
end
|
||||
position = position + velocity
|
||||
```
|
||||
|
||||
This code is a basic outline for a collision system that preserves the object's
|
||||
motion, but it still leaves several pixels of space between the bounding boxes.
|
||||
Notably, it also treats "collision" as an abstract concept, rather than as an
|
||||
event that could happen multiple times per frame.
|
||||
|
||||
-------------------------
|
||||
|
||||
```
|
||||
velocity = motion + speed
|
||||
if (collisionSimple(BOXSET, position + velocity)) then
|
||||
velocity.x = collisionX(BOXSET, velocity.x)
|
||||
velocity.y = collisionY(BOXSET, velocity.y)
|
||||
end
|
||||
position = position + velocity
|
||||
```
|
||||
|
||||
Here, collisions are still abstract, but "BOXSET" is defined externally
|
||||
(probably as a set of solid boxes, and their positions). This does require more
|
||||
in depth calculations, as well as three specialized utility functions, but the
|
||||
results might be what I'm looking for.
|
||||
|
||||
If there are any collisions between the player object and the given box set,
|
||||
then collisionX() and collisionY() are called to calculate the new distance
|
||||
that the character will move.
|
||||
|
||||
-------------------------
|
||||
|
||||
```
|
||||
bool collisionSimple(BOXSET, newPos):
|
||||
for_each box in BOXSET do
|
||||
if (box.overlap(PLAYER.box + newPos)) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
```
|
||||
|
||||
collisionSimple() first runs through the BOXSET, checking if any of the given
|
||||
bounding boxes would collide with the player object's new position (just
|
||||
pretend PLAYER is accessible). In this case, non-tile bounding boxes can be
|
||||
included as part of BOXSET; they're treated just the same. The optimal outcome
|
||||
is that there are no collisions.
|
||||
|
||||
A possible, but flawed, optimization that could be preformed here is to remove
|
||||
any elements from BOXSET that do not collide with newPos, and let the other
|
||||
utility functions operate only on what remains. However, if there are any
|
||||
collisions, than newPos is not the algorithm's final result, therefore any
|
||||
final result that the algorithm would calculate based on the remaining elements
|
||||
would not have been checked against the removed elements.
|
||||
|
||||
Just something to note.
|
||||
|
||||
-------------------------
|
||||
|
||||
```
|
||||
var collisionX(BOXSET, velocityX):
|
||||
var ret = velocityX
|
||||
|
||||
for_each box in BOXSET do
|
||||
if (box.overlap(PLAYER.box + PLAYER.position + {velocityX,0})) then
|
||||
if (velocityX > 0) then
|
||||
ret = min(ret, box.west - PLAYER.position.x)
|
||||
else
|
||||
ret = max(ret, box.east - PLAYER.position.x)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return ret
|
||||
end
|
||||
```
|
||||
|
||||
Two things: 1. collisionX() and collisionY() should be identical except for the
|
||||
axis of operation and 2. if a player object is "sliding" along a wall (or
|
||||
stuck), then these functions will be called every frame.
|
||||
|
||||
collisionX() and collisionY() check the sides of the elements in BOXSET, and if
|
||||
there's a box that the player would collide with, given the current distance to
|
||||
move, than the distance is reduced, based on if the character is moving left or
|
||||
right (or up or down).
|
||||
|
||||
An unfortunate bug I can already see is that this logic doesn't check corners;
|
||||
it might be possible to get stuck on a corner of a wall, but if this becomes an
|
||||
issue in my implementation I will update this article with that information,
|
||||
and you can promptly ignore it.
|
||||
@@ -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() {
|
||||
nextScene = SceneList::CONTINUE;
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,183 +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 "client_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
//-------------------------
|
||||
//Scene headers
|
||||
//-------------------------
|
||||
|
||||
//Add the custom scene headers here
|
||||
#include "in_combat.hpp"
|
||||
#include "in_world.hpp"
|
||||
#include "lobby.hpp"
|
||||
#include "main_menu.hpp"
|
||||
#include "option_screen.hpp"
|
||||
#include "splash_screen.hpp"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
ClientApplication::ClientApplication() {
|
||||
//
|
||||
}
|
||||
|
||||
ClientApplication::~ClientApplication() {
|
||||
UnloadScene();
|
||||
}
|
||||
|
||||
/* ClientApplication::Init()
|
||||
* This function initializes the entire program. There are a number of things
|
||||
* that could go wrong here, which is why there is such an unusual order of
|
||||
* operations.
|
||||
* Important things to note:
|
||||
* The APIs are initiated here.
|
||||
* The global objects are initialized here.
|
||||
* The game's screen is created here, based on information loaded from the config file.
|
||||
* The ConfigUtility's call to Load() also ensures that the "rsc\" folder is in the directory. It's easy to forget it.
|
||||
*/
|
||||
|
||||
void ClientApplication::Init() {
|
||||
//load the config file
|
||||
try {
|
||||
configUtil->Load("rsc/config.cfg");
|
||||
}
|
||||
catch(std::runtime_error& e) {
|
||||
std::string s = e.what();
|
||||
s += "; Ensure that the \"rsc\" directory is present";
|
||||
throw(std::runtime_error(s));
|
||||
}
|
||||
|
||||
//initialize the APIs
|
||||
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||
throw(std::runtime_error("Failed to initialize SDL"));
|
||||
}
|
||||
if (SDLNet_Init()) {
|
||||
throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||
}
|
||||
|
||||
//create the screen
|
||||
Uint32 flags = SDL_HWSURFACE | SDL_DOUBLEBUF;
|
||||
flags |= configUtil->Bool("screen.f") ? SDL_FULLSCREEN : 0;
|
||||
|
||||
BaseScene::SetScreen(
|
||||
configUtil->Int("screen.w"),
|
||||
configUtil->Int("screen.h"),
|
||||
SDL_GetVideoInfo()->vfmt->BitsPerPixel,
|
||||
flags);
|
||||
|
||||
//initiate the remaining singletons
|
||||
netUtil->Open(0, sizeof(Packet));
|
||||
}
|
||||
|
||||
void ClientApplication::Proc() {
|
||||
LoadScene(SceneList::FIRST);
|
||||
|
||||
//prepare the time system
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
Clock::duration delta(16 * Clock::duration::period::den / std::milli::den);
|
||||
Clock::time_point simTime = Clock::now();
|
||||
Clock::time_point realTime;
|
||||
|
||||
//The main loop
|
||||
while(activeScene->GetNextScene() != SceneList::QUIT) {
|
||||
//switch scenes when necessary
|
||||
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 ClientApplication::Quit() {
|
||||
//clean up the singletons
|
||||
netUtil->Close();
|
||||
surfaceMgr->FreeAll();
|
||||
|
||||
//clean up the scene
|
||||
UnloadScene();
|
||||
|
||||
//deinitialize the APIs
|
||||
SDLNet_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Private access members
|
||||
//-------------------------
|
||||
|
||||
void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||
UnloadScene();
|
||||
|
||||
switch(sceneIndex) {
|
||||
//add scene creation calls here
|
||||
case SceneList::INCOMBAT:
|
||||
activeScene = new InCombat();
|
||||
break;
|
||||
case SceneList::INWORLD:
|
||||
activeScene = new InWorld();
|
||||
break;
|
||||
case SceneList::LOBBY:
|
||||
activeScene = new Lobby();
|
||||
break;
|
||||
case SceneList::MAINMENU:
|
||||
activeScene = new MainMenu();
|
||||
break;
|
||||
case SceneList::OPTIONSCREEN:
|
||||
activeScene = new OptionScreen();
|
||||
break;
|
||||
case SceneList::FIRST:
|
||||
case SceneList::SPLASHSCREEN:
|
||||
activeScene = new SplashScreen();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw(std::logic_error("Failed to recognize the scene index"));
|
||||
}
|
||||
}
|
||||
|
||||
void ClientApplication::UnloadScene() {
|
||||
delete activeScene;
|
||||
activeScene = nullptr;
|
||||
}
|
||||
@@ -1,62 +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 CLIENTAPPLICATION_HPP_
|
||||
#define CLIENTAPPLICATION_HPP_
|
||||
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class ClientApplication {
|
||||
public:
|
||||
/* Public access members */
|
||||
ClientApplication();
|
||||
~ClientApplication();
|
||||
|
||||
void Init();
|
||||
void Proc();
|
||||
void Quit();
|
||||
|
||||
ClientApplication(ClientApplication const&) = delete;
|
||||
ClientApplication(ClientApplication const&&) = delete;
|
||||
private:
|
||||
/* Private access members */
|
||||
void LoadScene(SceneList sceneIndex);
|
||||
void UnloadScene();
|
||||
|
||||
BaseScene* activeScene = nullptr;
|
||||
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,90 +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_combat.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InCombat::InCombat() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering InCombat" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
InCombat::~InCombat() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving InCombat" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void InCombat::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::Update(double delta) {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::Render(SDL_Surface* const screen) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void InCombat::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InCombat::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
@@ -1,48 +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 INCOMBAT_HPP_
|
||||
#define INCOMBAT_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
class InCombat : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
InCombat();
|
||||
~InCombat();
|
||||
|
||||
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&);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,314 +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 <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InWorld::InWorld() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering InWorld" << endl;
|
||||
#endif
|
||||
cout << "Client Index: " << infoMgr->GetClientIndex() << endl;
|
||||
font.SetSurface(surfaceMgr->Get("font"));
|
||||
|
||||
//debugging
|
||||
Packet p;
|
||||
p.meta.type = Packet::Type::PLAYER_NEW;
|
||||
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||
|
||||
snprintf(p.playerInfo.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
|
||||
snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
|
||||
p.playerInfo.position = {50, 50};
|
||||
p.playerInfo.motion = {0, 0};
|
||||
|
||||
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||
|
||||
//request a sync
|
||||
p.meta.type = Packet::Type::SYNCHRONIZE;
|
||||
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||
}
|
||||
|
||||
InWorld::~InWorld() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving InWorld" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void InWorld::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Update(double delta) {
|
||||
while(HandlePacket(popNetworkPacket()));
|
||||
|
||||
for (auto& it : playerCharacters) {
|
||||
it.second.Update(delta);
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Render(SDL_Surface* const screen) {
|
||||
ClockFrameRate();
|
||||
|
||||
for (auto& it : playerCharacters) {
|
||||
it.second.DrawTo(screen);
|
||||
}
|
||||
|
||||
//since we're using this twice, make a tmp var
|
||||
string fps = itos(GetFrameRate());
|
||||
font.DrawStringTo(fps, screen, screen->w - fps.size() * font.GetCharW(), 0);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void InWorld::QuitEvent() {
|
||||
//ensure that the client is disconnected properly
|
||||
ExitGame();
|
||||
SetNextScene(SceneList::QUIT);
|
||||
}
|
||||
|
||||
void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
//general
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
ExitGame();
|
||||
break;
|
||||
}
|
||||
|
||||
//player movement
|
||||
if (infoMgr->GetPlayerIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_w:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::NORTH);
|
||||
SendState();
|
||||
break;
|
||||
case SDLK_s:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::SOUTH);
|
||||
SendState();
|
||||
break;
|
||||
case SDLK_a:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::WEST);
|
||||
SendState();
|
||||
break;
|
||||
case SDLK_d:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::EAST);
|
||||
SendState();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//player movement reversed
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_w:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::SOUTH);
|
||||
SendState();
|
||||
break;
|
||||
case SDLK_s:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::NORTH);
|
||||
SendState();
|
||||
break;
|
||||
case SDLK_a:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::EAST);
|
||||
SendState();
|
||||
break;
|
||||
case SDLK_d:
|
||||
playerCharacters[infoMgr->GetPlayerIndex()].MoveDirection(CardinalDirection::WEST);
|
||||
SendState();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Utilities
|
||||
//-------------------------
|
||||
|
||||
int InWorld::HandlePacket(Packet p) {
|
||||
switch(p.meta.type) {
|
||||
case Packet::Type::NONE:
|
||||
//DO NOTHING
|
||||
return 0;
|
||||
break;
|
||||
case Packet::Type::PING:
|
||||
//quick pong
|
||||
p.meta.type = Packet::Type::PONG;
|
||||
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||
break;
|
||||
case Packet::Type::PONG:
|
||||
//
|
||||
break;
|
||||
// case Packet::Type::BROADCAST_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
// case Packet::Type::BROADCAST_RESPONSE:
|
||||
// //
|
||||
// break;
|
||||
// case Packet::Type::JOIN_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
// case Packet::Type::JOIN_RESPONSE:
|
||||
// //
|
||||
// break;
|
||||
case Packet::Type::DISCONNECT:
|
||||
HandleDisconnection(p);
|
||||
break;
|
||||
// case Packet::Type::SYNCHRONIZE:
|
||||
// //
|
||||
// break;
|
||||
case Packet::Type::PLAYER_NEW:
|
||||
AddPlayer(p);
|
||||
break;
|
||||
case Packet::Type::PLAYER_DELETE:
|
||||
RemovePlayer(p);
|
||||
break;
|
||||
case Packet::Type::PLAYER_UPDATE:
|
||||
UpdatePlayer(p);
|
||||
break;
|
||||
default:
|
||||
throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type))));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void InWorld::Disconnect() {
|
||||
Packet p;
|
||||
|
||||
//delete the player
|
||||
p.meta.type = Packet::Type::PLAYER_DELETE;
|
||||
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||
p.playerInfo.index = infoMgr->GetPlayerIndex();
|
||||
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||
|
||||
//disconnect
|
||||
p.meta.type = Packet::Type::DISCONNECT;
|
||||
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||
|
||||
netUtil->Unbind(GAME_CHANNEL);
|
||||
|
||||
//reset the client
|
||||
infoMgr->ResetClientIndex();
|
||||
infoMgr->ResetPlayerIndex();
|
||||
}
|
||||
|
||||
void InWorld::ExitGame() {
|
||||
Disconnect();
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
endQueueThread();
|
||||
cout << "The game session has ended" << endl;
|
||||
}
|
||||
|
||||
void InWorld::HandleDisconnection(Packet& disconnect) {
|
||||
Disconnect();
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
endQueueThread();
|
||||
cout << "You have been disconnected" << endl;
|
||||
}
|
||||
|
||||
void InWorld::AddPlayer(Packet& p) {
|
||||
if (playerCharacters.find(p.playerInfo.index) != playerCharacters.end()) {
|
||||
throw(runtime_error("Duplicate players detected"));
|
||||
}
|
||||
|
||||
//position
|
||||
playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position);
|
||||
playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion);
|
||||
|
||||
//sprite
|
||||
playerCharacters[p.playerInfo.index].GetSprite()->SetSurface(surfaceMgr->Get(p.playerInfo.avatar), 32, 48);
|
||||
playerCharacters[p.playerInfo.index].FaceDirection();
|
||||
|
||||
//is it this player?
|
||||
if (p.meta.clientIndex == infoMgr->GetClientIndex()) {
|
||||
infoMgr->SetPlayerIndex(p.playerInfo.index);
|
||||
}
|
||||
|
||||
//debugging
|
||||
cout << "New player, index " << p.playerInfo.index << endl;
|
||||
}
|
||||
|
||||
void InWorld::RemovePlayer(Packet& p) {
|
||||
if (playerCharacters.find(p.playerInfo.index) == playerCharacters.end()) {
|
||||
throw(runtime_error("Player to delete not found"));
|
||||
}
|
||||
|
||||
playerCharacters.erase(p.playerInfo.index);
|
||||
}
|
||||
|
||||
void InWorld::UpdatePlayer(Packet& p) {
|
||||
if (playerCharacters.find(p.playerInfo.index) == playerCharacters.end()) {
|
||||
AddPlayer(p);
|
||||
return;
|
||||
}
|
||||
|
||||
playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position);
|
||||
playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion);
|
||||
playerCharacters[p.playerInfo.index].FaceDirection();
|
||||
}
|
||||
|
||||
void InWorld::SendState() {
|
||||
//send the state of this player's character
|
||||
if (infoMgr->GetPlayerIndex() == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
Packet p;
|
||||
p.meta.type = Packet::Type::PLAYER_UPDATE;
|
||||
p.meta.clientIndex = infoMgr->GetClientIndex();
|
||||
p.playerInfo.index = infoMgr->GetPlayerIndex();
|
||||
p.playerInfo.position = playerCharacters[infoMgr->GetPlayerIndex()].GetPosition();
|
||||
p.playerInfo.motion = playerCharacters[infoMgr->GetPlayerIndex()].GetMotion();
|
||||
|
||||
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
|
||||
}
|
||||
@@ -1,89 +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 INWORLD_HPP_
|
||||
#define INWORLD_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "utilities.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "singleton.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "network_queue.hpp"
|
||||
#include "information_manager.hpp"
|
||||
#include "player_character.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "button.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "frame_rate.hpp"
|
||||
|
||||
#include <map>
|
||||
|
||||
class InWorld : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InWorld();
|
||||
~InWorld();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
void QuitEvent();
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//Utilities
|
||||
int HandlePacket(Packet);
|
||||
void Disconnect();
|
||||
void ExitGame();
|
||||
|
||||
void HandleDisconnection(Packet&);
|
||||
|
||||
void AddPlayer(Packet&);
|
||||
void RemovePlayer(Packet&);
|
||||
void UpdatePlayer(Packet&);
|
||||
|
||||
void SendState();
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||
|
||||
//members
|
||||
RasterFont font;
|
||||
std::map<int, PlayerCharacter> playerCharacters;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,40 +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 INFORMATIONMANAGER_HPP_
|
||||
#define INFORMATIONMANAGER_HPP_
|
||||
|
||||
class InformationManager {
|
||||
public:
|
||||
int SetClientIndex(int i) { return clientIndex = i; }
|
||||
int GetClientIndex() { return clientIndex; }
|
||||
void ResetClientIndex() { clientIndex = -1; }
|
||||
|
||||
//one player at a time
|
||||
int SetPlayerIndex(int i) { return playerIndex = i; }
|
||||
int GetPlayerIndex() { return playerIndex; }
|
||||
void ResetPlayerIndex() { playerIndex = -1; }
|
||||
private:
|
||||
int clientIndex = -1;
|
||||
int playerIndex = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,234 +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 "lobby.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
Lobby::Lobby() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering Lobby" << endl;
|
||||
#endif
|
||||
refreshButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Refresh");
|
||||
joinButton.Setup(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Join");
|
||||
backButton.Setup(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back");
|
||||
|
||||
font.SetSurface(surfaceMgr->Get("font"));
|
||||
listBox.x = 280;
|
||||
listBox.y = 50;
|
||||
listBox.w = GetScreen()->w - listBox.x - 50;
|
||||
listBox.h = font.GetCharH();
|
||||
|
||||
serverList.push_back({"foo",{0,0}});
|
||||
serverList.push_back({"bar",{0,0}});
|
||||
serverList.push_back({"foobar",{0,0}});
|
||||
|
||||
flushNetworkQueue();
|
||||
beginQueueThread();
|
||||
BroadcastNetwork();
|
||||
}
|
||||
|
||||
Lobby::~Lobby() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving Lobby" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void Lobby::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void Lobby::Update(double delta) {
|
||||
while(HandlePacket(popNetworkPacket()));
|
||||
}
|
||||
|
||||
void Lobby::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void Lobby::Render(SDL_Surface* const screen) {
|
||||
refreshButton.DrawTo(screen);
|
||||
joinButton.DrawTo(screen);
|
||||
backButton.DrawTo(screen);
|
||||
|
||||
for (int i = 0; i < serverList.size(); i++) {
|
||||
if (selectedServer == &serverList[i]) {
|
||||
//draw the highlight box
|
||||
SDL_Rect r = listBox;
|
||||
r.y += i * font.GetCharH();
|
||||
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 127, 39));
|
||||
}
|
||||
font.DrawStringTo(serverList[i].name, screen, listBox.x, listBox.y + i * font.GetCharH());
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
refreshButton.MouseMotion(motion);
|
||||
joinButton.MouseMotion(motion);
|
||||
backButton.MouseMotion(motion);
|
||||
}
|
||||
|
||||
void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
refreshButton.MouseButtonDown(button);
|
||||
joinButton.MouseButtonDown(button);
|
||||
backButton.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (refreshButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
BroadcastNetwork();
|
||||
selectedServer = nullptr;
|
||||
}
|
||||
else if (joinButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
if (selectedServer) {
|
||||
ConnectToServer(selectedServer);
|
||||
selectedServer = nullptr;
|
||||
}
|
||||
}
|
||||
else if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
endQueueThread();
|
||||
selectedServer = nullptr;
|
||||
}
|
||||
else if (
|
||||
//clicked within bounds TODO: make the damn collision system
|
||||
button.x > listBox.x &&
|
||||
button.y > listBox.y &&
|
||||
button.x < listBox.x + listBox.w &&
|
||||
button.y < listBox.y + (listBox.h * serverList.size())
|
||||
)
|
||||
{
|
||||
//selecting a server
|
||||
selectedServer = &serverList[(button.y - listBox.y) / listBox.h];
|
||||
}
|
||||
else {
|
||||
//lose focus on a server
|
||||
selectedServer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Lobby::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Utilities
|
||||
//-------------------------
|
||||
|
||||
int Lobby::HandlePacket(Packet p) {
|
||||
switch(p.meta.type) {
|
||||
case Packet::Type::NONE:
|
||||
//DO NOTHING
|
||||
return 0;
|
||||
break;
|
||||
case Packet::Type::PING:
|
||||
//quick pong
|
||||
p.meta.type = Packet::Type::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||
break;
|
||||
case Packet::Type::PONG:
|
||||
//
|
||||
break;
|
||||
// case Packet::Type::BROADCAST_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
case Packet::Type::BROADCAST_RESPONSE:
|
||||
PushServer(p);
|
||||
break;
|
||||
// case Packet::Type::JOIN_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
case Packet::Type::JOIN_RESPONSE:
|
||||
BeginGame(p);
|
||||
break;
|
||||
// case Packet::Type::DISCONNECT:
|
||||
// //
|
||||
// break;
|
||||
// case Packet::Type::SYNCHRONIZE:
|
||||
// //
|
||||
// break;
|
||||
// case Packet::Type::PLAYER_NEW:
|
||||
// //
|
||||
// break;
|
||||
// case Packet::Type::PLAYER_DELETE:
|
||||
// //
|
||||
// break;
|
||||
// case Packet::Type::PLAYER_UPDATE:
|
||||
// //
|
||||
// break;
|
||||
default:
|
||||
throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type))));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Lobby::BroadcastNetwork() {
|
||||
Packet p;
|
||||
p.meta.type = Packet::Type::BROADCAST_REQUEST;
|
||||
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet));
|
||||
serverList.clear();
|
||||
}
|
||||
|
||||
void Lobby::PushServer(Packet& bcast) {
|
||||
ServerEntry entry;
|
||||
entry.name = bcast.serverInfo.name;
|
||||
entry.address = bcast.meta.address;
|
||||
serverList.push_back(entry);
|
||||
}
|
||||
|
||||
void Lobby::ConnectToServer(ServerEntry* server) {
|
||||
//_attempt_ to connect to a server
|
||||
if (!server) {
|
||||
throw(runtime_error("No server received"));
|
||||
}
|
||||
Packet p;
|
||||
p.meta.type = Packet::Type::JOIN_REQUEST;
|
||||
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||
}
|
||||
|
||||
void Lobby::BeginGame(Packet& response) {
|
||||
//should be downloading the resources here as well
|
||||
infoMgr->SetClientIndex(response.meta.clientIndex);
|
||||
netUtil->Bind(&response.meta.address, GAME_CHANNEL);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
}
|
||||
@@ -1,89 +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 LOBBY_HPP_
|
||||
#define LOBBY_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "utilities.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "server_entry.hpp"
|
||||
#include "packet.hpp"
|
||||
#include "network_queue.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "button.hpp"
|
||||
#include "raster_font.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
class Lobby : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
Lobby();
|
||||
~Lobby();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//utilities
|
||||
int HandlePacket(Packet);
|
||||
void BroadcastNetwork();
|
||||
void PushServer(Packet&);
|
||||
void ConnectToServer(ServerEntry*);
|
||||
void BeginGame(Packet&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = Singleton<InformationManager>::Get();
|
||||
|
||||
//members
|
||||
Button refreshButton;
|
||||
Button joinButton;
|
||||
Button backButton;
|
||||
|
||||
RasterFont font;
|
||||
SDL_Rect listBox;
|
||||
std::vector<ServerEntry> serverList;
|
||||
ServerEntry* selectedServer = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,47 +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 "client_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int, char**) {
|
||||
#ifdef DEBUG
|
||||
cout << "Beginning program" << endl;
|
||||
#endif
|
||||
try {
|
||||
ClientApplication app;
|
||||
app.Init();
|
||||
app.Proc();
|
||||
app.Quit();
|
||||
}
|
||||
catch(exception& e) {
|
||||
cerr << "Fatal error: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
cout << "Clean exit" << endl;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
@@ -1,103 +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 "main_menu.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
MainMenu::MainMenu() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering MainMenu" << endl;
|
||||
#endif
|
||||
startButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start");
|
||||
optionsButton.Setup(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options");
|
||||
quitButton.Setup(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit");
|
||||
}
|
||||
|
||||
MainMenu::~MainMenu() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving MainMenu" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void MainMenu::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void MainMenu::Update(double delta) {
|
||||
//
|
||||
}
|
||||
|
||||
void MainMenu::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void MainMenu::Render(SDL_Surface* const screen) {
|
||||
startButton.DrawTo(screen);
|
||||
optionsButton.DrawTo(screen);
|
||||
quitButton.DrawTo(screen);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void MainMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
startButton.MouseMotion(motion);
|
||||
optionsButton.MouseMotion(motion);
|
||||
quitButton.MouseMotion(motion);
|
||||
}
|
||||
|
||||
void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
startButton.MouseButtonDown(button);
|
||||
optionsButton.MouseButtonDown(button);
|
||||
quitButton.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (startButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::LOBBY);
|
||||
}
|
||||
if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::OPTIONSCREEN);
|
||||
}
|
||||
if (quitButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
QuitEvent();
|
||||
}
|
||||
}
|
||||
|
||||
void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +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 MAINMENU_HPP_
|
||||
#define MAINMENU_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "surface_manager.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
class MainMenu : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
MainMenu();
|
||||
~MainMenu();
|
||||
|
||||
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&);
|
||||
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
|
||||
Button startButton;
|
||||
Button optionsButton;
|
||||
Button quitButton;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,39 +0,0 @@
|
||||
#config
|
||||
LIBDIR=../libs
|
||||
LOCALLIBS=$(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libCommon.a
|
||||
LIB=$(LOCALLIBS) -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL
|
||||
INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/common
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
SRC=$(wildcard *.cpp)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,client)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIB)
|
||||
|
||||
$(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,77 +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 "option_screen.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
OptionScreen::OptionScreen() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering OptionScreen" << endl;
|
||||
#endif
|
||||
backButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back");
|
||||
}
|
||||
|
||||
OptionScreen::~OptionScreen() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving OptionScreen" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void OptionScreen::Render(SDL_Surface* const screen) {
|
||||
backButton.DrawTo(screen);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void OptionScreen::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
backButton.MouseMotion(motion);
|
||||
}
|
||||
|
||||
void OptionScreen::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
backButton.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void OptionScreen::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
}
|
||||
|
||||
void OptionScreen::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +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 OPTIONSCREEN_HPP_
|
||||
#define OPTIONSCREEN_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "surface_manager.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
class OptionScreen : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
OptionScreen();
|
||||
~OptionScreen();
|
||||
|
||||
protected:
|
||||
/* Frame loop */
|
||||
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&);
|
||||
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
Button backButton;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,114 +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"
|
||||
|
||||
void PlayerCharacter::Update(double delta) {
|
||||
if (limitSpeed) {
|
||||
constexpr double d = 1.0/sqrt(2);
|
||||
position += motion * delta * d;
|
||||
}
|
||||
else {
|
||||
position += motion * delta;
|
||||
}
|
||||
sprite.Update(delta);
|
||||
}
|
||||
|
||||
void PlayerCharacter::MoveDirection(CardinalDirection cd) {
|
||||
//shift the movement in this direction
|
||||
switch(cd) {
|
||||
case CardinalDirection::NORTH:
|
||||
if (motion.y >= 0) {
|
||||
motion.y -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case CardinalDirection::SOUTH:
|
||||
if (motion.y <= 0) {
|
||||
motion.y += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case CardinalDirection::WEST:
|
||||
if (motion.x >= 0) {
|
||||
motion.x -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case CardinalDirection::EAST:
|
||||
if (motion.x <= 0) {
|
||||
motion.x += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
//face the correct direction
|
||||
FaceDirection();
|
||||
}
|
||||
|
||||
void PlayerCharacter::FaceDirection(CardinalDirection cd) {
|
||||
switch(cd) {
|
||||
case CardinalDirection::NORTH:
|
||||
sprite.SetCurrentStrip(1);
|
||||
break;
|
||||
case CardinalDirection::SOUTH:
|
||||
sprite.SetCurrentStrip(0);
|
||||
break;
|
||||
case CardinalDirection::WEST:
|
||||
sprite.SetCurrentStrip(2);
|
||||
break;
|
||||
case CardinalDirection::EAST:
|
||||
sprite.SetCurrentStrip(3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerCharacter::FaceDirection() {
|
||||
//base the direction on the character's movement
|
||||
if (motion.y < 0) {
|
||||
FaceDirection(CardinalDirection::NORTH);
|
||||
}
|
||||
if (motion.y > 0) {
|
||||
FaceDirection(CardinalDirection::SOUTH);
|
||||
}
|
||||
if (motion.x < 0) {
|
||||
FaceDirection(CardinalDirection::WEST);
|
||||
}
|
||||
if (motion.x > 0) {
|
||||
FaceDirection(CardinalDirection::EAST);
|
||||
}
|
||||
CheckSpeed();
|
||||
}
|
||||
|
||||
void PlayerCharacter::CheckSpeed() {
|
||||
//diagonal
|
||||
if (motion.x != 0 && motion.y != 0) {
|
||||
sprite.SetInterval(0.1);
|
||||
limitSpeed = true;
|
||||
}
|
||||
//cardinal
|
||||
else if (motion != 0) {
|
||||
sprite.SetInterval(0.1);
|
||||
limitSpeed = false;
|
||||
}
|
||||
//not moving
|
||||
else {
|
||||
sprite.SetInterval(0);
|
||||
sprite.SetCurrentFrame(0);
|
||||
limitSpeed = false;
|
||||
}
|
||||
}
|
||||
@@ -1,63 +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"
|
||||
#include "defines.hpp"
|
||||
|
||||
class PlayerCharacter {
|
||||
public:
|
||||
PlayerCharacter() = default;
|
||||
~PlayerCharacter() = default;
|
||||
|
||||
void Update(double delta);
|
||||
|
||||
void MoveDirection(CardinalDirection);
|
||||
void FaceDirection(CardinalDirection);
|
||||
void FaceDirection();
|
||||
|
||||
void DrawTo(SDL_Surface* const dest) { sprite.DrawTo(dest, position.x, position.y); }
|
||||
|
||||
//accessors and mutators
|
||||
Vector2 SetPosition(Vector2 v) { return position = v; }
|
||||
Vector2 ShiftPosition(Vector2 v) { return position += v; }
|
||||
Vector2 GetPosition() { return position; }
|
||||
|
||||
Vector2 SetMotion(Vector2 v) { return motion = v; }
|
||||
Vector2 ShiftMotion(Vector2 v) { return motion += v; }
|
||||
Vector2 GetMotion() { return motion; }
|
||||
|
||||
SpriteSheet* GetSprite() { return &sprite; }
|
||||
private:
|
||||
void CheckSpeed();
|
||||
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
SpriteSheet sprite;
|
||||
|
||||
//for moving diagonal
|
||||
bool limitSpeed = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,40 +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 SCENELIST_HPP_
|
||||
#define SCENELIST_HPP_
|
||||
|
||||
enum class SceneList {
|
||||
//these are reserved
|
||||
QUIT,
|
||||
CONTINUE,
|
||||
FIRST,
|
||||
|
||||
//custom indexes
|
||||
INCOMBAT,
|
||||
INWORLD,
|
||||
LOBBY,
|
||||
MAINMENU,
|
||||
OPTIONSCREEN,
|
||||
SPLASHSCREEN,
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,86 +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 "splash_screen.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
SplashScreen::SplashScreen() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering SplashScreen" << endl;
|
||||
#endif
|
||||
logo.SetSurface(surfaceMgr->Load("splash-logo", configUtil->String("logos") + "/krstudios.bmp"));
|
||||
}
|
||||
|
||||
SplashScreen::~SplashScreen() {
|
||||
surfaceMgr->Free("splash-logo");
|
||||
#ifdef DEBUG
|
||||
cout << "leaving SplashScreen" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void SplashScreen::RunFrame(double delta) {
|
||||
HandleEvents();
|
||||
if (!loaded) {
|
||||
//never repeat this
|
||||
loaded = true;
|
||||
|
||||
//quick draw
|
||||
RenderFrame();
|
||||
|
||||
LoadResources();
|
||||
}
|
||||
|
||||
if (std::chrono::steady_clock::now() - start > std::chrono::duration<int>(1)) {
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
}
|
||||
|
||||
void SplashScreen::RenderFrame() {
|
||||
SDL_FillRect(GetScreen(), 0, 0);
|
||||
int x = (GetScreen()->w - logo.GetClipW()) / 2;
|
||||
int y = (GetScreen()->h - logo.GetClipH()) / 2;
|
||||
logo.DrawTo(GetScreen(), x, y);
|
||||
SDL_Flip(GetScreen());
|
||||
}
|
||||
|
||||
void SplashScreen::LoadResources() {
|
||||
//standard
|
||||
surfaceMgr->Load("font", configUtil->String("fonts") + "/pokemon_dark_font.bmp");
|
||||
surfaceMgr->Load("button", configUtil->String("interface") + "/button.bmp");
|
||||
|
||||
//debugging
|
||||
surfaceMgr->Load("elliot", configUtil->String("sprites") + "/elliot2.bmp");
|
||||
surfaceMgr->Load("coa", configUtil->String("sprites") + "/coa2.bmp");
|
||||
surfaceMgr->Load("flower", configUtil->String("sprites") + "/aniflower.bmp");
|
||||
surfaceMgr->Load("terrain", configUtil->String("tilesets") + "/terrain.bmp");
|
||||
//TODO
|
||||
}
|
||||
@@ -1,53 +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 SPLASHSCREEN_HPP_
|
||||
#define SPLASHSCREEN_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "singleton.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
#include "image.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
class SplashScreen : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
SplashScreen();
|
||||
~SplashScreen();
|
||||
|
||||
protected:
|
||||
/* Frame loop */
|
||||
void RunFrame(double delta);
|
||||
void RenderFrame();
|
||||
void LoadResources();
|
||||
|
||||
bool loaded = false;
|
||||
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = Singleton<SurfaceManager>::Get();
|
||||
Image logo;
|
||||
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
The Game Map
|
||||
|
||||
This section outlines the game’s map system. This system utilizes pagination to create a theoretically infinite game map, as well as supporting multiple tilesets in the same map. The goal of this design is to create a system with as much flexibility as possible, and simply enforcing a more rigid approach higher in the tool chain.
|
||||
|
||||
Tile
|
||||
|
||||
The Tile class is the basic unit of the map system, and is explicitly a POD (plain old data) structure. A tile has these members:
|
||||
|
||||
X Position
|
||||
Y Position
|
||||
Depth
|
||||
Width
|
||||
Height
|
||||
Tile Index
|
||||
|
||||
The tile’s X and Y positions are relative to their container region’s location. A tile’s depth allows multiple tiles to be drawn at the same location, and in the correct order; tiles with lower depths (including below zero) are drawn first. If a new tile has the same X position, Y position and depth as an existing tile, the old tile is overwritten.
|
||||
|
||||
The width and height members indicate the graphical size of the tile (not actually used when drawing), while the tile index is the specific tile for the sheet manager to draw. A negative value here is considered an error message.
|
||||
|
||||
Region
|
||||
|
||||
The region class has these members:
|
||||
|
||||
X Position
|
||||
Y Position
|
||||
Width
|
||||
Height
|
||||
Tile Container
|
||||
|
||||
Each region in a certain map must have the same width and height, and it’s X and Y positions must be multiples of those width and height values, respectfully. The outcome of this restriction is a theoretically infinite grid of region objects.
|
||||
|
||||
Each region holds a set of tiles corresponding to the region’s bounds. The tiles’ X and Y positions are relative to the regions’, so moving the region will move the tiles as well. A region object is created or loaded when a tile is place in it’s bounds; similarly, if a region has no tiles it should be deleted or removed from memory.
|
||||
|
||||
The exact width and height of a region has no significant impact, other than loading or transmission costs. The width and height of a map can be adjusted as needed.
|
||||
|
||||
Region Pager
|
||||
|
||||
The region pager class has these members:
|
||||
|
||||
Region Width
|
||||
Region Height
|
||||
On New Callback
|
||||
On Delete Callback
|
||||
Region Container
|
||||
|
||||
The Region Pager class holds a series of region objects, as well as creating and deleting them as needed. Every region theoretically exists at any time, so if a non-existent region object is requested, it is created and then returned. This class also has the Prune() method, which removes all regions out of bounds from memory, and the DrawTo method, which takes (among other things) the sheet manager for the map.
|
||||
|
||||
The width and height members must be set before the pager is used, and must not be changed while it still has regions loaded. These are used to create region objects as needed.
|
||||
|
||||
Each pager can also have two different callbacks set: “on new” and “on delete”. If either of these are set (that is, not null) then each region object’s address is passed to these after it is created or before it is destroyed, respectfully. The callbacks are intended to be used for domain specific processes, such as loading or saving data, or even requesting data from a remote server.
|
||||
|
||||
Tile Sheet
|
||||
|
||||
A tileset is a series of tile graphics stored in a single file. The tile sheet class loads a tile set into memory, and provides utilities for drawing them to the screen. The tile sheet class has these members:
|
||||
|
||||
Image
|
||||
X Count
|
||||
Y Count
|
||||
Total Count
|
||||
Begin
|
||||
End
|
||||
|
||||
The Image class is utilized heavily here by storing the graphical data and the tile size. Any file loaded into a sheet object must have all tile images arranged in a grid pattern, and they must all have the same width and height. The width and height must be provided when the file is loaded.
|
||||
|
||||
The X and Y counts are the number of tiles along the X and Y axis of the sheet’s image, and the total count is the number of tiles in the whole sheet (which is equal to the product of the X and Y counts).
|
||||
|
||||
Begin is the index of the first tile on the sheet (default is zero), and end is the index after the last tile (defaults to the value of total count). These indicate the range of the tiles, and are mostly used by the sheet manager. They are also used by the InRange() method, which checks to see if a certain tile is in that sheet.
|
||||
|
||||
Tile Sheet Manager
|
||||
|
||||
This class has these members:
|
||||
|
||||
Tile Sheet Container
|
||||
Range End
|
||||
|
||||
This class is a wrapper around a key-value container, using strings as the keys. Given a specific tile index, this class will draw the correct tile from the loaded sheets, or it throws an error.
|
||||
|
||||
Also, this class keeps track of the end of the sheet’s ranges.
|
||||
|
||||
TODO
|
||||
|
||||
Map File Format
|
||||
|
||||
TODO
|
||||
@@ -0,0 +1,3 @@
|
||||
This particular refactoring stage was absolute hell, mostly because I was wrestling with a severe bout of depression too. So, I've added the diffs, and a scary screenshot of the git console.
|
||||
|
||||
There are three diff files because server/server_application.cpp was split into two files: server/server_internals.cpp and server/server_connections.cpp, each with it's own diff.
|
||||
|
After Width: | Height: | Size: 27 KiB |
@@ -0,0 +1,394 @@
|
||||
diff --git a/server/server_connections.cpp b/server/server_connections.cpp
|
||||
index 1a96d5b..2f35566 100644
|
||||
--- a/server/server_connections.cpp
|
||||
+++ b/server/server_connections.cpp
|
||||
@@ -21,212 +21,106 @@
|
||||
*/
|
||||
#include "server_application.hpp"
|
||||
|
||||
-#include "utility.hpp"
|
||||
-
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
-#include <string>
|
||||
-
|
||||
-//-------------------------
|
||||
-//Define the public members
|
||||
-//-------------------------
|
||||
-
|
||||
-void ServerApplication::Init(int argc, char** argv) {
|
||||
- //NOTE: I might need to rearrange the init process so that lua & SQL can interact with the map system as needed.
|
||||
- std::cout << "Beginning startup" << std::endl;
|
||||
-
|
||||
- //initial setup
|
||||
- ClientEntry::uidCounter = 0;
|
||||
- PlayerEntry::uidCounter = 0;
|
||||
- config.Load("rsc\\config.cfg");
|
||||
-
|
||||
- //Init SDL
|
||||
- if (SDL_Init(0)) {
|
||||
- throw(std::runtime_error("Failed to initialize SDL"));
|
||||
- }
|
||||
- std::cout << "Initialized SDL" << std::endl;
|
||||
-
|
||||
- //Init SDL_net
|
||||
- if (SDLNet_Init()) {
|
||||
- throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||
- }
|
||||
- network.Open(config.Int("server.port"), PACKET_BUFFER_SIZE);
|
||||
- std::cout << "Initialized SDL_net" << std::endl;
|
||||
-
|
||||
- //Init SQL
|
||||
- int ret = sqlite3_open_v2(config["server.dbname"].c_str(), &database, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, nullptr);
|
||||
- if (ret != SQLITE_OK || !database) {
|
||||
- throw(std::runtime_error(std::string() + "Failed to initialize SQL: " + sqlite3_errmsg(database) ));
|
||||
- }
|
||||
- std::cout << "Initialized SQL" << std::endl;
|
||||
-
|
||||
- //setup the database
|
||||
- if (runSQLScript(database, config["dir.scripts"] + "setup_server.sql")) {
|
||||
- throw(std::runtime_error("Failed to initialize SQL's setup script"));
|
||||
- }
|
||||
- std::cout << "Initialized SQL's setup script" << std::endl;
|
||||
-
|
||||
- //lua
|
||||
- luaState = luaL_newstate();
|
||||
- if (!luaState) {
|
||||
- throw(std::runtime_error("Failed to initialize lua"));
|
||||
- }
|
||||
- luaL_openlibs(luaState);
|
||||
- std::cout << "Initialized lua" << std::endl;
|
||||
-
|
||||
- //run the startup script
|
||||
- if (luaL_dofile(luaState, (config["dir.scripts"] + "setup_server.lua").c_str())) {
|
||||
- throw(std::runtime_error(std::string() + "Failed to initialize lua's setup script: " + lua_tostring(luaState, -1) ));
|
||||
- }
|
||||
- std::cout << "Initialized lua's setup script" << std::endl;
|
||||
-
|
||||
- //setup the map object
|
||||
- regionPager.GetAllocator()->SetLuaState(luaState);
|
||||
- regionPager.GetFormat()->SetLuaState(luaState);
|
||||
- //TODO: config parameter
|
||||
- regionPager.GetFormat()->SetSaveDir("save/mapname/");
|
||||
-
|
||||
- std::cout << "Initialized the map system" << std::endl;
|
||||
- std::cout << "\tsizeof(SerialPacket): " << sizeof(SerialPacket) << std::endl;
|
||||
- std::cout << "\tPACKET_BUFFER_SIZE: " << PACKET_BUFFER_SIZE << std::endl;
|
||||
-
|
||||
- //finalize the startup
|
||||
- std::cout << "Startup completed successfully" << std::endl;
|
||||
-
|
||||
- //debugging
|
||||
- //
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::Proc() {
|
||||
- SerialPacket packet;
|
||||
- while(running) {
|
||||
- //suck in the waiting packets & process them
|
||||
- while(network.Receive()) {
|
||||
- //get the packet
|
||||
- deserialize(&packet, network.GetInData());
|
||||
- //cache the source address
|
||||
- packet.meta.srcAddress = network.GetInPacket()->address;
|
||||
- //we need to go deeper
|
||||
- HandlePacket(packet);
|
||||
- }
|
||||
- //give the computer a break
|
||||
- SDL_Delay(10);
|
||||
- }
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::Quit() {
|
||||
- std::cout << "Shutting down" << std::endl;
|
||||
- //empty the members
|
||||
- regionPager.UnloadAll();
|
||||
-
|
||||
- //APIs
|
||||
- lua_close(luaState);
|
||||
- sqlite3_close_v2(database);
|
||||
- network.Close();
|
||||
- SDLNet_Quit();
|
||||
- SDL_Quit();
|
||||
- std::cout << "Shutdown finished" << std::endl;
|
||||
-}
|
||||
-
|
||||
-//-------------------------
|
||||
-//Define the uber switch
|
||||
-//-------------------------
|
||||
-
|
||||
-void ServerApplication::HandlePacket(SerialPacket packet) {
|
||||
- switch(packet.meta.type) {
|
||||
- case SerialPacket::Type::BROADCAST_REQUEST:
|
||||
- HandleBroadcastRequest(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::JOIN_REQUEST:
|
||||
- HandleJoinRequest(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::DISCONNECT:
|
||||
- HandleDisconnect(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::SYNCHRONIZE:
|
||||
- HandleSynchronize(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::SHUTDOWN:
|
||||
- HandleShutdown(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_NEW:
|
||||
- HandlePlayerNew(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_DELETE:
|
||||
- HandlePlayerDelete(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_UPDATE:
|
||||
- HandlePlayerUpdate(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::REGION_REQUEST:
|
||||
- HandleRegionRequest(packet);
|
||||
- break;
|
||||
- //handle errors
|
||||
- default:
|
||||
- throw(std::runtime_error("Unknown SerialPacket::Type encountered"));
|
||||
- break;
|
||||
- }
|
||||
-}
|
||||
|
||||
//-------------------------
|
||||
//Handle various network input
|
||||
//-------------------------
|
||||
|
||||
void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
|
||||
- //send back the server's metadata
|
||||
+ //pack the server's data
|
||||
packet.meta.type = SerialPacket::Type::BROADCAST_RESPONSE;
|
||||
-
|
||||
- //pack the data
|
||||
+ packet.serverInfo.networkVersion = NETWORK_VERSION;
|
||||
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
|
||||
packet.serverInfo.playerCount = playerMap.size();
|
||||
- packet.serverInfo.regionWidth = REGION_WIDTH;
|
||||
- packet.serverInfo.regionHeight = REGION_HEIGHT;
|
||||
- packet.serverInfo.regionDepth = REGION_DEPTH;
|
||||
|
||||
- //send the data
|
||||
+ //bounce this packet
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
serialize(&packet, buffer);
|
||||
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void ServerApplication::HandleJoinRequest(SerialPacket packet) {
|
||||
- //register the new client
|
||||
+ //create the new client
|
||||
ClientEntry newClient;
|
||||
newClient.address = packet.meta.srcAddress;
|
||||
- clientMap[ClientEntry::uidCounter] = newClient;
|
||||
|
||||
- //send the client their index
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
+ //TODO: move this into the player management code
|
||||
+ //create the new player
|
||||
+ PlayerEntry newPlayer;
|
||||
+ newPlayer.clientIndex = ClientEntry::uidCounter;
|
||||
+ newPlayer.player = packet.clientInfo.player;
|
||||
+ newPlayer.handle = packet.clientInfo.handle;
|
||||
+ newPlayer.avatar = packet.clientInfo.avatar;
|
||||
+
|
||||
+ //send the client their info
|
||||
packet.meta.type = SerialPacket::Type::JOIN_RESPONSE;
|
||||
- packet.clientInfo.index = ClientEntry::uidCounter;
|
||||
- serialize(&packet, buffer);
|
||||
+ packet.clientInfo.clientIndex = ClientEntry::uidCounter;
|
||||
+ packet.clientInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
|
||||
//bounce this packet
|
||||
+ char buffer[PACKET_BUFFER_SIZE];
|
||||
+ serialize(&packet, buffer);
|
||||
network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE);
|
||||
|
||||
+ //send the new player to all clients
|
||||
+ packet.meta.type = SerialPacket::Type::PLAYER_NEW;
|
||||
+ packet.playerInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
+ strncpy(packet.playerInfo.handle, newPlayer.handle.c_str(), PACKET_STRING_SIZE);
|
||||
+ strncpy(packet.playerInfo.avatar, newPlayer.avatar.c_str(), PACKET_STRING_SIZE);
|
||||
+ packet.playerInfo.position = newPlayer.position;
|
||||
+ packet.playerInfo.motion = newPlayer.motion;
|
||||
+ PumpPacket(packet);
|
||||
+
|
||||
//finished this routine
|
||||
+ clientMap[ClientEntry::uidCounter] = newClient;
|
||||
+ playerMap[PlayerEntry::uidCounter] = newPlayer;
|
||||
ClientEntry::uidCounter++;
|
||||
+ PlayerEntry::uidCounter++;
|
||||
std::cout << "Connect, total: " << clientMap.size() << std::endl;
|
||||
}
|
||||
|
||||
+void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
||||
+ //TODO: compensate for large distances
|
||||
+
|
||||
+ //send all the server's data to this client
|
||||
+ SerialPacket newPacket;
|
||||
+ char buffer[PACKET_BUFFER_SIZE];
|
||||
+
|
||||
+ //players
|
||||
+ newPacket.meta.type = SerialPacket::Type::PLAYER_UPDATE;
|
||||
+ for (auto& it : playerMap) {
|
||||
+ //TODO: update this for the expanded PlayerEntry structure
|
||||
+ newPacket.playerInfo.playerIndex = it.first;
|
||||
+ snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
||||
+ snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
||||
+ newPacket.playerInfo.mapIndex = it.second.mapIndex;
|
||||
+ newPacket.playerInfo.position = it.second.position;
|
||||
+ newPacket.playerInfo.motion = it.second.motion;
|
||||
+ serialize(&newPacket, buffer);
|
||||
+ network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE);
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
void ServerApplication::HandleDisconnect(SerialPacket packet) {
|
||||
//TODO: authenticate who is disconnecting/kicking
|
||||
+ //TODO: define the difference between unloading and deletng a player
|
||||
|
||||
//disconnect the specified client
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
serialize(&packet, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- clientMap.erase(packet.clientInfo.index);
|
||||
+ network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE);
|
||||
+ clientMap.erase(packet.clientInfo.clientIndex);
|
||||
|
||||
//prep the delete packet
|
||||
SerialPacket delPacket;
|
||||
delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
|
||||
- //TODO: can this use DeletePlayer() instead?
|
||||
//delete server and client side players
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
+ erase_if(playerMap, [&](std::pair<int, PlayerEntry> it) -> bool {
|
||||
//find the internal players to delete
|
||||
- if (it.second.clientIndex == packet.clientInfo.index) {
|
||||
+ if (it.second.clientIndex == packet.clientInfo.clientIndex) {
|
||||
//send the delete player command to all clients
|
||||
delPacket.playerInfo.playerIndex = it.first;
|
||||
PumpPacket(delPacket);
|
||||
@@ -243,102 +137,23 @@ void ServerApplication::HandleDisconnect(SerialPacket packet) {
|
||||
std::cout << "Disconnect, total: " << clientMap.size() << std::endl;
|
||||
}
|
||||
|
||||
-void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
||||
- //TODO: compensate for large distances
|
||||
-
|
||||
- //send all the server's data to this client
|
||||
- SerialPacket newPacket;
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
-
|
||||
- //players
|
||||
- newPacket.meta.type = SerialPacket::Type::PLAYER_UPDATE;
|
||||
- for (auto& it : playerMap) {
|
||||
- //TODO: update this for the expanded PlayerEntry structure
|
||||
- newPacket.playerInfo.playerIndex = it.first;
|
||||
- snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
||||
- snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
||||
- newPacket.playerInfo.position = it.second.position;
|
||||
- newPacket.playerInfo.motion = it.second.motion;
|
||||
- serialize(&newPacket, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- }
|
||||
-}
|
||||
-
|
||||
void ServerApplication::HandleShutdown(SerialPacket packet) {
|
||||
+ //TODO: authenticate who is shutting the server down
|
||||
+
|
||||
//end the server
|
||||
running = false;
|
||||
|
||||
//disconnect all clients
|
||||
packet.meta.type = SerialPacket::Type::DISCONNECT;
|
||||
+ packet.clientInfo.clientIndex = -1;
|
||||
PumpPacket(packet);
|
||||
|
||||
//finished this routine
|
||||
std::cout << "Shutdown signal accepted" << std::endl;
|
||||
}
|
||||
|
||||
-void ServerApplication::HandlePlayerNew(SerialPacket packet) {
|
||||
- //register the new PlayerEntry
|
||||
- //NOTE: assigning each field one-by-one so adding or moving a field doesn't break this code
|
||||
- PlayerEntry newPlayer;
|
||||
-
|
||||
- //metadata
|
||||
- newPlayer.clientIndex = packet.playerInfo.clientIndex;
|
||||
- newPlayer.handle = packet.playerInfo.handle;
|
||||
- newPlayer.avatar = packet.playerInfo.avatar;
|
||||
-
|
||||
- //position
|
||||
- newPlayer.mapIndex = 0;
|
||||
- newPlayer.position = {0,0};
|
||||
- newPlayer.motion = {0,0};
|
||||
- newPlayer.bbox = {0, 0, 0, 0};
|
||||
-
|
||||
- //TODO: Add the statistic creation code here
|
||||
-
|
||||
- //push this player
|
||||
- playerMap[PlayerEntry::uidCounter] = newPlayer;
|
||||
-
|
||||
- //send the client their info
|
||||
- packet.playerInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
- packet.playerInfo.position = newPlayer.position;
|
||||
- packet.playerInfo.motion = newPlayer.motion;
|
||||
-
|
||||
- //actually send to everyone
|
||||
- PumpPacket(packet);
|
||||
-
|
||||
- //finish this routine
|
||||
- PlayerEntry::uidCounter++;
|
||||
-}
|
||||
-
|
||||
-//TODO: differentiate between delete and unload
|
||||
-void ServerApplication::HandlePlayerDelete(SerialPacket packet) {
|
||||
- //TODO: authenticate who is deleting this player
|
||||
- if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
- throw(std::runtime_error("Cannot delete a non-existant player"));
|
||||
- }
|
||||
-
|
||||
- //TODO: remove the deleted player from the database?
|
||||
-
|
||||
- //prep the delete packet
|
||||
- SerialPacket delPacket;
|
||||
- delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
-
|
||||
- //delete the specified playerEntry
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
- //find the specified PlayerEntry
|
||||
- if (it.first == packet.playerInfo.playerIndex) {
|
||||
- //send to all
|
||||
- delPacket.playerInfo.playerIndex = it.first;
|
||||
- PumpPacket(delPacket);
|
||||
-
|
||||
- //delete this player
|
||||
- return true;
|
||||
- }
|
||||
- //skip this player
|
||||
- return false;
|
||||
- });
|
||||
-}
|
||||
-
|
||||
void ServerApplication::HandlePlayerUpdate(SerialPacket packet) {
|
||||
+ //TODO: this should be moved elsewhere
|
||||
if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
throw(std::runtime_error("Cannot update a non-existant player"));
|
||||
}
|
||||
@@ -351,9 +166,12 @@ void ServerApplication::HandlePlayerUpdate(SerialPacket packet) {
|
||||
}
|
||||
|
||||
void ServerApplication::HandleRegionRequest(SerialPacket packet) {
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
+ //TODO: this should be moved elsewhere
|
||||
packet.meta.type = SerialPacket::Type::REGION_CONTENT;
|
||||
packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
+
|
||||
+ //send the content
|
||||
+ char buffer[PACKET_BUFFER_SIZE];
|
||||
serialize(&packet, buffer);
|
||||
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
diff --git a/server/server_internals.cpp b/server/server_internals.cpp
|
||||
index 1a96d5b..e1a9cb9 100644
|
||||
--- a/server/server_internals.cpp
|
||||
+++ b/server/server_internals.cpp
|
||||
@@ -1,4 +1,4 @@
|
||||
-/* Copyright: (c) Kayne Ruse 2013, 2014
|
||||
+/* 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,7 +21,7 @@
|
||||
*/
|
||||
#include "server_application.hpp"
|
||||
|
||||
-#include "utility.hpp"
|
||||
+#include "server_utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
@@ -36,8 +36,6 @@ void ServerApplication::Init(int argc, char** argv) {
|
||||
std::cout << "Beginning startup" << std::endl;
|
||||
|
||||
//initial setup
|
||||
- ClientEntry::uidCounter = 0;
|
||||
- PlayerEntry::uidCounter = 0;
|
||||
config.Load("rsc\\config.cfg");
|
||||
|
||||
//Init SDL
|
||||
@@ -109,6 +107,8 @@ void ServerApplication::Proc() {
|
||||
//we need to go deeper
|
||||
HandlePacket(packet);
|
||||
}
|
||||
+ //update the internals
|
||||
+ //TODO: update the internals i.e. player positions
|
||||
//give the computer a break
|
||||
SDL_Delay(10);
|
||||
}
|
||||
@@ -116,6 +116,10 @@ void ServerApplication::Proc() {
|
||||
|
||||
void ServerApplication::Quit() {
|
||||
std::cout << "Shutting down" << std::endl;
|
||||
+
|
||||
+ //save the server state
|
||||
+ //TODO: save the existing players
|
||||
+
|
||||
//empty the members
|
||||
regionPager.UnloadAll();
|
||||
|
||||
@@ -125,6 +129,7 @@ void ServerApplication::Quit() {
|
||||
network.Close();
|
||||
SDLNet_Quit();
|
||||
SDL_Quit();
|
||||
+
|
||||
std::cout << "Shutdown finished" << std::endl;
|
||||
}
|
||||
|
||||
@@ -140,21 +145,15 @@ void ServerApplication::HandlePacket(SerialPacket packet) {
|
||||
case SerialPacket::Type::JOIN_REQUEST:
|
||||
HandleJoinRequest(packet);
|
||||
break;
|
||||
- case SerialPacket::Type::DISCONNECT:
|
||||
- HandleDisconnect(packet);
|
||||
- break;
|
||||
case SerialPacket::Type::SYNCHRONIZE:
|
||||
HandleSynchronize(packet);
|
||||
break;
|
||||
+ case SerialPacket::Type::DISCONNECT:
|
||||
+ HandleDisconnect(packet);
|
||||
+ break;
|
||||
case SerialPacket::Type::SHUTDOWN:
|
||||
HandleShutdown(packet);
|
||||
break;
|
||||
- case SerialPacket::Type::PLAYER_NEW:
|
||||
- HandlePlayerNew(packet);
|
||||
- break;
|
||||
- case SerialPacket::Type::PLAYER_DELETE:
|
||||
- HandlePlayerDelete(packet);
|
||||
- break;
|
||||
case SerialPacket::Type::PLAYER_UPDATE:
|
||||
HandlePlayerUpdate(packet);
|
||||
break;
|
||||
@@ -167,202 +166,3 @@ void ServerApplication::HandlePacket(SerialPacket packet) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
-
|
||||
-//-------------------------
|
||||
-//Handle various network input
|
||||
-//-------------------------
|
||||
-
|
||||
-void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
|
||||
- //send back the server's metadata
|
||||
- packet.meta.type = SerialPacket::Type::BROADCAST_RESPONSE;
|
||||
-
|
||||
- //pack the data
|
||||
- snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
|
||||
- packet.serverInfo.playerCount = playerMap.size();
|
||||
- packet.serverInfo.regionWidth = REGION_WIDTH;
|
||||
- packet.serverInfo.regionHeight = REGION_HEIGHT;
|
||||
- packet.serverInfo.regionDepth = REGION_DEPTH;
|
||||
-
|
||||
- //send the data
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- serialize(&packet, buffer);
|
||||
- network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleJoinRequest(SerialPacket packet) {
|
||||
- //register the new client
|
||||
- ClientEntry newClient;
|
||||
- newClient.address = packet.meta.srcAddress;
|
||||
- clientMap[ClientEntry::uidCounter] = newClient;
|
||||
-
|
||||
- //send the client their index
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- packet.meta.type = SerialPacket::Type::JOIN_RESPONSE;
|
||||
- packet.clientInfo.index = ClientEntry::uidCounter;
|
||||
- serialize(&packet, buffer);
|
||||
-
|
||||
- //bounce this packet
|
||||
- network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE);
|
||||
-
|
||||
- //finished this routine
|
||||
- ClientEntry::uidCounter++;
|
||||
- std::cout << "Connect, total: " << clientMap.size() << std::endl;
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleDisconnect(SerialPacket packet) {
|
||||
- //TODO: authenticate who is disconnecting/kicking
|
||||
-
|
||||
- //disconnect the specified client
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- serialize(&packet, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- clientMap.erase(packet.clientInfo.index);
|
||||
-
|
||||
- //prep the delete packet
|
||||
- SerialPacket delPacket;
|
||||
- delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
-
|
||||
- //TODO: can this use DeletePlayer() instead?
|
||||
- //delete server and client side players
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
- //find the internal players to delete
|
||||
- if (it.second.clientIndex == packet.clientInfo.index) {
|
||||
- //send the delete player command to all clients
|
||||
- delPacket.playerInfo.playerIndex = it.first;
|
||||
- PumpPacket(delPacket);
|
||||
-
|
||||
- //delete this player object
|
||||
- return true;
|
||||
- }
|
||||
-
|
||||
- //don't delete this player object
|
||||
- return false;
|
||||
- });
|
||||
-
|
||||
- //finished this routine
|
||||
- std::cout << "Disconnect, total: " << clientMap.size() << std::endl;
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleSynchronize(SerialPacket packet) {
|
||||
- //TODO: compensate for large distances
|
||||
-
|
||||
- //send all the server's data to this client
|
||||
- SerialPacket newPacket;
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
-
|
||||
- //players
|
||||
- newPacket.meta.type = SerialPacket::Type::PLAYER_UPDATE;
|
||||
- for (auto& it : playerMap) {
|
||||
- //TODO: update this for the expanded PlayerEntry structure
|
||||
- newPacket.playerInfo.playerIndex = it.first;
|
||||
- snprintf(newPacket.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
|
||||
- snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
|
||||
- newPacket.playerInfo.position = it.second.position;
|
||||
- newPacket.playerInfo.motion = it.second.motion;
|
||||
- serialize(&newPacket, buffer);
|
||||
- network.Send(&clientMap[packet.clientInfo.index].address, buffer, PACKET_BUFFER_SIZE);
|
||||
- }
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleShutdown(SerialPacket packet) {
|
||||
- //end the server
|
||||
- running = false;
|
||||
-
|
||||
- //disconnect all clients
|
||||
- packet.meta.type = SerialPacket::Type::DISCONNECT;
|
||||
- PumpPacket(packet);
|
||||
-
|
||||
- //finished this routine
|
||||
- std::cout << "Shutdown signal accepted" << std::endl;
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandlePlayerNew(SerialPacket packet) {
|
||||
- //register the new PlayerEntry
|
||||
- //NOTE: assigning each field one-by-one so adding or moving a field doesn't break this code
|
||||
- PlayerEntry newPlayer;
|
||||
-
|
||||
- //metadata
|
||||
- newPlayer.clientIndex = packet.playerInfo.clientIndex;
|
||||
- newPlayer.handle = packet.playerInfo.handle;
|
||||
- newPlayer.avatar = packet.playerInfo.avatar;
|
||||
-
|
||||
- //position
|
||||
- newPlayer.mapIndex = 0;
|
||||
- newPlayer.position = {0,0};
|
||||
- newPlayer.motion = {0,0};
|
||||
- newPlayer.bbox = {0, 0, 0, 0};
|
||||
-
|
||||
- //TODO: Add the statistic creation code here
|
||||
-
|
||||
- //push this player
|
||||
- playerMap[PlayerEntry::uidCounter] = newPlayer;
|
||||
-
|
||||
- //send the client their info
|
||||
- packet.playerInfo.playerIndex = PlayerEntry::uidCounter;
|
||||
- packet.playerInfo.position = newPlayer.position;
|
||||
- packet.playerInfo.motion = newPlayer.motion;
|
||||
-
|
||||
- //actually send to everyone
|
||||
- PumpPacket(packet);
|
||||
-
|
||||
- //finish this routine
|
||||
- PlayerEntry::uidCounter++;
|
||||
-}
|
||||
-
|
||||
-//TODO: differentiate between delete and unload
|
||||
-void ServerApplication::HandlePlayerDelete(SerialPacket packet) {
|
||||
- //TODO: authenticate who is deleting this player
|
||||
- if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
- throw(std::runtime_error("Cannot delete a non-existant player"));
|
||||
- }
|
||||
-
|
||||
- //TODO: remove the deleted player from the database?
|
||||
-
|
||||
- //prep the delete packet
|
||||
- SerialPacket delPacket;
|
||||
- delPacket.meta.type = SerialPacket::Type::PLAYER_DELETE;
|
||||
-
|
||||
- //delete the specified playerEntry
|
||||
- erase_if(playerMap, [&](std::pair<unsigned int, PlayerEntry> it) -> bool {
|
||||
- //find the specified PlayerEntry
|
||||
- if (it.first == packet.playerInfo.playerIndex) {
|
||||
- //send to all
|
||||
- delPacket.playerInfo.playerIndex = it.first;
|
||||
- PumpPacket(delPacket);
|
||||
-
|
||||
- //delete this player
|
||||
- return true;
|
||||
- }
|
||||
- //skip this player
|
||||
- return false;
|
||||
- });
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandlePlayerUpdate(SerialPacket packet) {
|
||||
- if (playerMap.find(packet.playerInfo.playerIndex) == playerMap.end()) {
|
||||
- throw(std::runtime_error("Cannot update a non-existant player"));
|
||||
- }
|
||||
-
|
||||
- //TODO: the server needs it's own movement system too
|
||||
- playerMap[packet.playerInfo.playerIndex].position = packet.playerInfo.position;
|
||||
- playerMap[packet.playerInfo.playerIndex].motion = packet.playerInfo.motion;
|
||||
-
|
||||
- PumpPacket(packet);
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::HandleRegionRequest(SerialPacket packet) {
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- packet.meta.type = SerialPacket::Type::REGION_CONTENT;
|
||||
- packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
- serialize(&packet, buffer);
|
||||
- network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
|
||||
-}
|
||||
-
|
||||
-void ServerApplication::PumpPacket(SerialPacket packet) {
|
||||
- //NOTE: I don't really like this, but it'll do for now
|
||||
- char buffer[PACKET_BUFFER_SIZE];
|
||||
- serialize(&packet, buffer);
|
||||
- for (auto& it : clientMap) {
|
||||
- network.Send(&it.second.address, buffer, PACKET_BUFFER_SIZE);
|
||||
- }
|
||||
-}
|
||||
@@ -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() {
|
||||
nextScene = SceneList::CONTINUE;
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,118 +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_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
//-------------------------
|
||||
//Scene headers
|
||||
//-------------------------
|
||||
|
||||
//Add the custom scene headers here
|
||||
#include "map_editor.hpp"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
EditorApplication::EditorApplication() {
|
||||
//
|
||||
}
|
||||
|
||||
EditorApplication::~EditorApplication() {
|
||||
UnloadScene();
|
||||
}
|
||||
|
||||
void EditorApplication::Init() {
|
||||
if (SDL_Init(SDL_INIT_VIDEO))
|
||||
throw(std::runtime_error("Failed to initialize SDL"));
|
||||
|
||||
BaseScene::SetScreen(800, 600);
|
||||
}
|
||||
|
||||
void EditorApplication::Proc() {
|
||||
LoadScene(SceneList::FIRST);
|
||||
|
||||
//prepare the time system
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
Clock::duration delta(16 * Clock::duration::period::den / std::milli::den);
|
||||
Clock::time_point simTime = Clock::now();
|
||||
Clock::time_point realTime;
|
||||
|
||||
//The main loop
|
||||
while(activeScene->GetNextScene() != SceneList::QUIT) {
|
||||
//switch scenes when necessary
|
||||
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() {
|
||||
UnloadScene();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Private access members
|
||||
//-------------------------
|
||||
|
||||
void EditorApplication::LoadScene(SceneList sceneIndex) {
|
||||
UnloadScene();
|
||||
|
||||
switch(sceneIndex) {
|
||||
//add scene creation calls here
|
||||
case SceneList::FIRST:
|
||||
case SceneList::MAPEDITOR:
|
||||
activeScene = new MapEditor();
|
||||
break;
|
||||
|
||||
default:
|
||||
throw(std::logic_error("Failed to recognize the scene index"));
|
||||
}
|
||||
}
|
||||
|
||||
void EditorApplication::UnloadScene() {
|
||||
delete activeScene;
|
||||
activeScene = nullptr;
|
||||
}
|
||||
@@ -1,50 +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 EDITORAPPLICATION_HPP_
|
||||
#define EDITORAPPLICATION_HPP_
|
||||
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class EditorApplication {
|
||||
public:
|
||||
/* Public access members */
|
||||
EditorApplication();
|
||||
~EditorApplication();
|
||||
|
||||
void Init();
|
||||
void Proc();
|
||||
void Quit();
|
||||
|
||||
EditorApplication(EditorApplication const&) = delete;
|
||||
EditorApplication(EditorApplication const&&) = delete;
|
||||
private:
|
||||
/* Private access members */
|
||||
void LoadScene(SceneList sceneIndex);
|
||||
void UnloadScene();
|
||||
|
||||
BaseScene* activeScene = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,47 +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_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int, char**) {
|
||||
#ifdef DEBUG
|
||||
cout << "Beginning program" << endl;
|
||||
#endif
|
||||
try {
|
||||
EditorApplication app;
|
||||
app.Init();
|
||||
app.Proc();
|
||||
app.Quit();
|
||||
}
|
||||
catch(exception& e) {
|
||||
cerr << "Fatal error: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
cout << "Clean exit" << endl;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
#config
|
||||
LIBDIR=../libs
|
||||
LOCALLIBS=$(LIBDIR)/out/libCodebase.a $(LIBDIR)/out/libCommon.a
|
||||
LIB=$(LOCALLIBS) -lmingw32 -lSDLmain -lSDL
|
||||
INCLUDES=$(LIBDIR)/Codebase $(LIBDIR)/common
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
SRC=$(wildcard *.cpp)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,editor)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIB)
|
||||
|
||||
$(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,82 +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 "map_editor.hpp"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
MapEditor::MapEditor() {
|
||||
//
|
||||
}
|
||||
|
||||
MapEditor::~MapEditor() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void MapEditor::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void MapEditor::Update(double delta) {
|
||||
//
|
||||
}
|
||||
|
||||
void MapEditor::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void MapEditor::Render(SDL_Surface* const screen) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void MapEditor::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
|
||||
void MapEditor::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void MapEditor::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void MapEditor::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void MapEditor::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
@@ -1,48 +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 MAPEDITOR_HPP_
|
||||
#define MAPEDITOR_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
class MapEditor : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
MapEditor();
|
||||
~MapEditor();
|
||||
|
||||
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&);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,35 +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 SCENELIST_HPP_
|
||||
#define SCENELIST_HPP_
|
||||
|
||||
enum class SceneList {
|
||||
//these are reserved
|
||||
QUIT,
|
||||
CONTINUE,
|
||||
FIRST,
|
||||
|
||||
//custom indexes
|
||||
MAPEDITOR,
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
/* 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 "server_application.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
cout << "Beginning server" << endl;
|
||||
try {
|
||||
ServerApplication app;
|
||||
app.Init(argc, argv);
|
||||
app.Loop();
|
||||
app.Quit();
|
||||
}
|
||||
catch(exception& e) {
|
||||
cerr << "Fatal error: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
cout << "Clean exit" << endl;
|
||||
return 0;
|
||||
}*/
|
||||
|
||||
#include "network_packet.hpp"
|
||||
#include "serial.hpp"
|
||||
#include "region.hpp"
|
||||
#include "map_generator.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int, char**) {
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
Region region(20, 20, 3, 0, 0);
|
||||
NetworkPacket packet;
|
||||
|
||||
packet.meta.type = NetworkPacket::Type::REGION_CONTENT;
|
||||
packet.regionInfo.region = ®ion;
|
||||
|
||||
for (int i = 0; i < packet.regionInfo.region->GetWidth(); i++) {
|
||||
for (int j = 0; j < packet.regionInfo.region->GetHeight(); j++) {
|
||||
for (int k = 0; k < packet.regionInfo.region->GetDepth(); k++) {
|
||||
packet.regionInfo.region->SetTile(i, j, k, i*20*3 + j*3 +k);
|
||||
}
|
||||
}
|
||||
}
|
||||
serialize(&packet, buffer);
|
||||
|
||||
for (int i = 0; i < PACKET_BUFFER_SIZE; i++) {
|
||||
cout << ((Region::type_t*)(buffer))[i] << ",";
|
||||
}
|
||||
cout << endl << endl;
|
||||
|
||||
deserialize(&packet, buffer);
|
||||
|
||||
for (int i = 0; i < packet.regionInfo.region->GetWidth(); i++) {
|
||||
for (int j = 0; j < packet.regionInfo.region->GetHeight(); j++) {
|
||||
for (int k = 0; k < packet.regionInfo.region->GetDepth(); k++) {
|
||||
cout << packet.regionInfo.region->GetTile(i, j, k) << ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
BlankGenerator().Unload(packet.regionInfo.region);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
using namespace std;
|
||||
|
||||
union Foo {
|
||||
enum class Type {
|
||||
INT,
|
||||
FLOAT
|
||||
}type;
|
||||
struct {
|
||||
Type type;
|
||||
int i;
|
||||
}i;
|
||||
struct {
|
||||
Type type;
|
||||
float f;
|
||||
}f;
|
||||
};
|
||||
|
||||
void serialize(Foo* f, void* buffer) {
|
||||
int len = 0;
|
||||
memcpy((void*)((int)buffer + len), &f->type, sizeof(Foo::Type));
|
||||
len += sizeof(Foo::Type);
|
||||
switch(f->type) {
|
||||
case Foo::Type::INT:
|
||||
memcpy((void*)((int)buffer + len), &f->i.i, sizeof(int));
|
||||
len += sizeof(int);
|
||||
break;
|
||||
case Foo::Type::FLOAT:
|
||||
memcpy((void*)((int)buffer + len), &f->f.f, sizeof(float));
|
||||
len += sizeof(float);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void deserialize(Foo* f, void* buffer) {
|
||||
int len = 0;
|
||||
memcpy(&f->type, (void*)((int)buffer + len), sizeof(Foo::Type));
|
||||
len += sizeof(Foo::Type);
|
||||
switch(f->type) {
|
||||
case Foo::Type::INT:
|
||||
memcpy(&f->i.i, (void*)((int)buffer + len), sizeof(int));
|
||||
len += sizeof(int);
|
||||
break;
|
||||
case Foo::Type::FLOAT:
|
||||
memcpy(&f->f.f, (void*)((int)buffer + len), sizeof(float));
|
||||
len += sizeof(float);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Foo f;
|
||||
f.type = Foo::Type::FLOAT;
|
||||
f.f.f = 3.14;
|
||||
Foo i;
|
||||
i.type = Foo::Type::INT;
|
||||
i.i.i = 42;
|
||||
|
||||
char buffer[sizeof(Foo)];
|
||||
serialize(&f, buffer);
|
||||
deserialize(&i, buffer);
|
||||
|
||||
switch(i.type) {
|
||||
case Foo::Type::INT:
|
||||
cout << i.i.i << endl;
|
||||
break;
|
||||
case Foo::Type::FLOAT:
|
||||
cout << i.f.f << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
The palette is extracted from the frog images. It's only meant as areference, and can change later. Hot pink (255, 0, 255) is used to fill the gaps.
|
||||
|
||||
[blank], mid highlight, high highlight
|
||||
low base, mid base, high base
|
||||
[blank], mid shadow, high shadow
|
||||
inner cheek
|
||||
tongue outline, tongue shadow, tongue base
|
||||
leather shadow, leather base, leather hightlight
|
||||
wood shadow, wood base, wood highlight
|
||||
sharp shadow, sharp base, [blank]
|
||||
dull shadow, dull base, [blank]
|
||||
frog outline, metal outline, wood outline
|
||||
|
After Width: | Height: | Size: 997 B |
|
After Width: | Height: | Size: 687 KiB |
|
After Width: | Height: | Size: 320 KiB |
|
After Width: | Height: | Size: 838 KiB |
|
After Width: | Height: | Size: 577 KiB |
|
After Width: | Height: | Size: 494 KiB |
|
After Width: | Height: | Size: 363 KiB |
|
After Width: | Height: | Size: 180 KiB |
|
After Width: | Height: | Size: 141 KiB |
|
After Width: | Height: | Size: 1.1 MiB |
@@ -0,0 +1,7 @@
|
||||
pay attention to each frog's eyes
|
||||
|
||||
frogs are green
|
||||
bog toad is yellow/orange with spots (gradient?)
|
||||
princess dress is an ugly pink
|
||||
frog king wears a gold crown and red cape with gold trimmings
|
||||
the knight's helmet has a purple plume, and it's wearing boxer shorts with loveheart patterns
|
||||
|
After Width: | Height: | Size: 974 KiB |
|
After Width: | Height: | Size: 984 KiB |
|
After Width: | Height: | Size: 1.1 MiB |
|
After Width: | Height: | Size: 1.1 MiB |
|
After Width: | Height: | Size: 1.1 MiB |
|
After Width: | Height: | Size: 1004 KiB |
|
After Width: | Height: | Size: 506 KiB |
|
After Width: | Height: | Size: 572 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 5.8 KiB |
|
After Width: | Height: | Size: 4.9 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 524 KiB |
|
After Width: | Height: | Size: 336 KiB |
|
After Width: | Height: | Size: 433 KiB |
|
After Width: | Height: | Size: 231 KiB |
|
After Width: | Height: | Size: 133 KiB |
|
After Width: | Height: | Size: 387 KiB |
|
After Width: | Height: | Size: 80 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 473 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
|
After Width: | Height: | Size: 3.7 KiB |
|
After Width: | Height: | Size: 7.4 KiB |
|
After Width: | Height: | Size: 45 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 869 KiB |
|
After Width: | Height: | Size: 864 KiB |
|
After Width: | Height: | Size: 970 KiB |
|
After Width: | Height: | Size: 1.0 MiB |
|
After Width: | Height: | Size: 981 KiB |
|
After Width: | Height: | Size: 849 KiB |
|
After Width: | Height: | Size: 865 KiB |
|
After Width: | Height: | Size: 856 KiB |
|
After Width: | Height: | Size: 944 KiB |
|
After Width: | Height: | Size: 884 KiB |
|
After Width: | Height: | Size: 968 KiB |
@@ -1,101 +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 "button.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string s) {
|
||||
Setup(i, j, imageSurface, fontSurface, s);
|
||||
}
|
||||
|
||||
void Button::Setup(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string s) {
|
||||
x = i;
|
||||
y = j;
|
||||
|
||||
SetSurfaces(imageSurface, fontSurface);
|
||||
|
||||
SetText(s);
|
||||
}
|
||||
|
||||
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
if (motion.state & SDL_BUTTON_LMASK) {
|
||||
return CalcState(motion.x, motion.y, true);
|
||||
}
|
||||
else {
|
||||
return CalcState(motion.x, motion.y, false);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
if (button.button == SDL_BUTTON_LEFT) {
|
||||
return CalcState(button.x, button.y, true);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (button.button == SDL_BUTTON_LEFT) {
|
||||
return CalcState(button.x, button.y, false);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
void Button::DrawTo(SDL_Surface* const dest) {
|
||||
image.DrawTo(dest, x, y);
|
||||
font.DrawStringTo(text, dest, textX + x, textY + y);
|
||||
}
|
||||
|
||||
void Button::SetSurfaces(SDL_Surface* imageSurface, SDL_Surface* fontSurface) {
|
||||
//graphical stuff
|
||||
image.SetSurface(imageSurface);
|
||||
image.SetClipH(image.GetClipH() / 3); //3 phases
|
||||
font.SetSurface(fontSurface);
|
||||
|
||||
//reset textX & textY
|
||||
SetText(text);
|
||||
}
|
||||
|
||||
std::string Button::SetText(std::string s) {
|
||||
//one line
|
||||
text = s;
|
||||
textX = (image.GetClipW() / 2) - (font.GetCharW() * text.size() / 2);
|
||||
textY = (image.GetClipH() / 2) - (font.GetCharH() / 2);
|
||||
return text;
|
||||
}
|
||||
|
||||
Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) {
|
||||
if (i < x || i > (x + image.GetClipW()) ||
|
||||
j < y || j > (y + image.GetClipH())
|
||||
) {
|
||||
image.SetClipY(0);
|
||||
return state = State::NORMAL;
|
||||
}
|
||||
if (leftPressed) {
|
||||
image.SetClipY(image.GetClipH()*2);
|
||||
return state = State::PRESSED;
|
||||
}
|
||||
else {
|
||||
image.SetClipY(image.GetClipH());
|
||||
return state = State::HOVER;
|
||||
}
|
||||
}
|
||||
@@ -1,82 +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 BUTTON_HPP_
|
||||
#define BUTTON_HPP_
|
||||
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
//3-phases, no toggle, centred text
|
||||
class Button {
|
||||
public:
|
||||
enum class State {
|
||||
NORMAL, HOVER, PRESSED
|
||||
};
|
||||
|
||||
Button() = default;
|
||||
Button(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = "");
|
||||
|
||||
void Setup(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = "");
|
||||
|
||||
//return the current state
|
||||
State MouseMotion(SDL_MouseMotionEvent const&);
|
||||
State MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
State MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
State GetState() const {
|
||||
return state;
|
||||
}
|
||||
|
||||
//yet another draw function
|
||||
void DrawTo(SDL_Surface* const);
|
||||
|
||||
//simple accessors and mutators
|
||||
Sint16 SetX(Sint16 i) { return x = i; }
|
||||
Sint16 SetY(Sint16 i) { return y = i; }
|
||||
Sint16 GetX() const { return x; }
|
||||
Sint16 GetY() const { return y; }
|
||||
|
||||
void SetSurfaces(SDL_Surface* image, SDL_Surface* font);
|
||||
|
||||
std::string SetText(std::string s);
|
||||
std::string GetText() const { return text; }
|
||||
|
||||
//raw access, be careful
|
||||
Image* GetImage() { return ℑ }
|
||||
RasterFont* GetFont() { return &font; }
|
||||
|
||||
//debug
|
||||
Sint16 GetTextX() const { return textX; }
|
||||
Sint16 GetTextY() const { return textY; }
|
||||
private:
|
||||
State CalcState(Sint16 x, Sint16 y, bool leftPressed);
|
||||
|
||||
Sint16 x = 0, y = 0;
|
||||
Sint16 textX = 0, textY = 0; //prevent recalc every loop
|
||||
Image image;
|
||||
RasterFont font;
|
||||
State state = State::NORMAL;
|
||||
std::string text;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,76 +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 "config_utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void ConfigUtility::Load(string fname) {
|
||||
ifstream is(fname);
|
||||
|
||||
if (!is.is_open()) {
|
||||
throw(runtime_error("Failed to open config file"));
|
||||
}
|
||||
|
||||
string key, val;
|
||||
|
||||
for (;;) { //forever
|
||||
//eat whitespace
|
||||
while(isspace(is.peek()))
|
||||
is.ignore();
|
||||
|
||||
//end of file
|
||||
if (is.eof())
|
||||
break;
|
||||
|
||||
//skip comment lines
|
||||
if (is.peek() == '#') {
|
||||
while(is.peek() != '\n' && !is.eof()) {
|
||||
is.ignore();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
//read in the pair
|
||||
getline(is, key,'=');
|
||||
getline(is, val);
|
||||
|
||||
//trim the strings at the start & end
|
||||
while(key.size() && isspace(*key.begin())) key.erase(0, 1);
|
||||
while(val.size() && isspace(*val.begin())) val.erase(0, 1);
|
||||
|
||||
while(key.size() && isspace(*(key.end()-1))) key.erase(key.end() - 1);
|
||||
while(val.size() && isspace(*(val.end()-1))) val.erase(val.end() - 1);
|
||||
|
||||
//allow empty/wiped values
|
||||
if (key.size() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//save the pair
|
||||
table[key] = val;
|
||||
}
|
||||
|
||||
is.close();
|
||||
}
|
||||