Compare commits
169 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 92fc9b4e25 | |||
| 2b8e7241c9 | |||
| 124cb3ad13 | |||
| 6d3400d948 | |||
| 5031352fe3 | |||
| 624369f147 | |||
| 9a7f7b8684 | |||
| fa9487c2f2 | |||
| 9ec1ddab99 | |||
| 6f1c2d0555 | |||
| 01b50d5590 | |||
| 9c673928e6 | |||
| d55dfb90e4 | |||
| 235a05d006 | |||
| dabb7b3b2e | |||
| e756289c2b | |||
| c534158c2e | |||
| b47191a0c1 | |||
| 6c6a025f2a | |||
| 60edc97ea5 | |||
| a5b209d9e0 | |||
| fd673a415f | |||
| 4270765146 | |||
| f56cb58dfb | |||
| f315f4bf35 | |||
| 69765de433 | |||
| 35d463d4ba | |||
| 894b46c5db | |||
| eb0b18af6f | |||
| fba183fa27 | |||
| c5a627004a | |||
| ac27fb0ca7 | |||
| be4a8311d5 | |||
| 6d32d44fa3 | |||
| 9bacfb1424 | |||
| c3464be589 | |||
| 7fe71c60d0 | |||
| 0c6537fb36 | |||
| 854dc0eb45 | |||
| e90afb7e4a | |||
| 0453f6becf | |||
| c8a58ab515 | |||
| 553f8dbfa5 | |||
| 2bacdcdab7 | |||
| 99aecbfdbb | |||
| 27bda5dc28 | |||
| 962f3f5dd0 | |||
| 41077b43b3 | |||
| 4410ab892f | |||
| 60000cb0cf | |||
| 66b00255d5 | |||
| a5b68cf1fd | |||
| 4cff57fe71 | |||
| 38b603fc8f | |||
| 47684380a9 | |||
| e4bfbfb906 | |||
| 9db86c19f6 | |||
| d5b551cec3 | |||
| 7e500027e3 | |||
| dd786ba579 | |||
| 5a57888305 | |||
| 706aa5e1d7 | |||
| 19c1b1197d | |||
| 5cf62f5517 | |||
| 56d02ad8d4 | |||
| 006a72174f | |||
| 0cbc9dd9db | |||
| 60c31ff56d | |||
| 6a204643f6 | |||
| 59285d1630 | |||
| a850d6b1af | |||
| f17fa0f345 | |||
| 756d4e770d | |||
| 7bb5e8ce0d | |||
| 975533afba | |||
| 19749f7c87 | |||
| 7703c4b0ad | |||
| dc8f594eec | |||
| 4629b7302b | |||
| 02d83d1f16 | |||
| 91c9cef56d | |||
| f7d4912942 | |||
| eb8674b84f | |||
| ff44e4d916 | |||
| e66540f114 | |||
| 639c0c70e3 | |||
| 37b02352e2 | |||
| 31c8bd7fd2 | |||
| 808fe570a3 | |||
| e706cc9d13 | |||
| eb02cc4b6a | |||
| 31cca61d1c | |||
| 071e0d9021 | |||
| a494bfbb38 | |||
| baadf554cd | |||
| 9df16fede0 | |||
| dd0ad31ac6 | |||
| be26c9d103 | |||
| a448c8fb68 | |||
| de1a6a050f | |||
| 49c9abe91b | |||
| 3b05476689 | |||
| 1c032bfc47 | |||
| 1a7457f650 | |||
| e640eda771 | |||
| fc2b67608f | |||
| a3a990cc01 | |||
| f6a4674a2f | |||
| 865620b4a8 | |||
| 1e0ed350fc | |||
| 6ccc874583 | |||
| ca86dc5fb8 | |||
| e3605e4dc5 | |||
| cb9aef95ec | |||
| 09f97de0e6 | |||
| f5466b9982 | |||
| e176a60d28 | |||
| dc24d1b059 | |||
| fa3fc18ddf | |||
| c2941cd3e8 | |||
| bf15a5d957 | |||
| f01463bab3 | |||
| 54cd26b76f | |||
| 2bd8adaf69 | |||
| 26eba2def6 | |||
| 43852ce755 | |||
| 81fab06e3b | |||
| 77a90f9c0c | |||
| bc32f2a2f0 | |||
| 76bfecd466 | |||
| d4eb1e7b8d | |||
| 2ec307510d | |||
| 0b4e6003d6 | |||
| 7e603ffa89 | |||
| 6fc8570cf4 | |||
| 0bfd916df4 | |||
| 88aee0f4f5 | |||
| 0d3c3243a0 | |||
| 871b1136c2 | |||
| f64c935ffe | |||
| 600ec6789b | |||
| 65a3937592 | |||
| 719c8f49a6 | |||
| 29928c0b92 | |||
| a88a1f7cf7 | |||
| 46efbfbe9a | |||
| 26ff8dcc8f | |||
| 9c91e9d5fd | |||
| 3628d3c1fd | |||
| 6c9406147a | |||
| aae244de39 | |||
| d5b14c2679 | |||
| a54fbfb9e9 | |||
| 9cbbfe77b7 | |||
| 9b64c67068 | |||
| de0227a1cf | |||
| 1dfeabf195 | |||
| 6c5197f3f2 | |||
| 09453cc24e | |||
| 4b62310158 | |||
| 43f2190c3e | |||
| 3225da6b13 | |||
| 0c233b8764 | |||
| 027d0125ef | |||
| e4884c3e18 | |||
| b675f516e7 | |||
| d5f7363c33 | |||
| 7458962ad4 | |||
| 0a0b61287e |
+2
-2
@@ -1,6 +1,6 @@
|
||||
#Editor generated files
|
||||
#*.sln
|
||||
#*.vcproj
|
||||
*.sln
|
||||
*.vcproj
|
||||
*.suo
|
||||
*.ncb
|
||||
*.user
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
_This project is currently in it's early prototype stage. You can see other versions in various branches, tagged as prototype-X._
|
||||
The most recent stable windows build can be found [here](https://dl.dropboxusercontent.com/u/46669050/Tortuga.rar).
|
||||
|
||||
Tortuga is a 3/4 top down multiplayer RPG set in a large archipelago occupied by warring pirate clans. The emphasis of this game is on multiplayer competition, exploration in an open world, and solo adventuring. The game runs on a Minecraft like server system with a goal of about 50-100 players on a single server. The player characters are tied to the server where they are created, and are susceptible to permadeath: deletion of a character upon death. The servers are designed to allow a large amount of modification by the hosts.
|
||||
Tortuga is an open source 2D multiplayer role playing game featuring permadeath (deletion of a character upon death). The emphasis of this game is on multiplayer cooperation, competition, exploration and customization. The game runs on customizable server software that can support up to 150 simultaneous players or more.
|
||||
|
||||
## Libraries
|
||||
This game is inspired by classic 2D RPGs, as well as more modern sandbox MMOs. This project is currently independently created and funded, with the goal of creating a game that will engage user's imagination and inspire a large modding community.
|
||||
|
||||
* [SDL](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
||||
* [SDL_net](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension, slightly modified. This must be installed first.
|
||||
* [Codebase](https://github.com/Ratstail91/Codebase) - generic reusable code modules
|
||||
## External Dependencies
|
||||
|
||||
## Instructions
|
||||
* [SDL 1.6](http://www.libsdl.org/) - Simple DirectMedia Layer API
|
||||
* [SDL_net 1.2](http://www.libsdl.org/projects/SDL_net/) - SDL's networking extension
|
||||
* [lua 5.2](http://www.lua.org/) - The lua programming language
|
||||
* [SQLite3](http://www.sqlite.org/) - A lightweight SQL database engine
|
||||
|
||||
* 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 GitHub wiki [here](https://github.com/Ratstail91/Tortuga/wiki).
|
||||
## Documentation
|
||||
|
||||
[Tortuga Game Design Document](https://github.com/Ratstail91/Tortuga/blob/docs/design%20doc.docx?raw=true)
|
||||
[Tortuga Technical Document](https://github.com/Ratstail91/Tortuga/blob/docs/technical%20doc.docx?raw=true)
|
||||
|
||||
## Copyright
|
||||
|
||||
Tortuga is released under the [zlib license](http://en.wikipedia.org/wiki/Zlib_License).
|
||||
The current version of Tortuga is released under the [zlib license](http://en.wikipedia.org/wiki/Zlib_License).
|
||||
|
||||
Copyright (c) 2013 Kayne Ruse
|
||||
Copyright (c) 2013, 2014 Kayne Ruse
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
|
||||
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.
|
||||
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,8 @@
|
||||
#ifndef CHANNELS_HPP_
|
||||
#define CHANNELS_HPP_
|
||||
|
||||
enum Channels {
|
||||
SERVER = 0
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,138 @@
|
||||
/* 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 ClientApplications, 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 "serial.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
//-------------------------
|
||||
//Scene headers
|
||||
//-------------------------
|
||||
|
||||
//Add the custom scene headers here
|
||||
#include "splash_screen.hpp"
|
||||
#include "main_menu.hpp"
|
||||
#include "options_menu.hpp"
|
||||
#include "lobby_menu.hpp"
|
||||
#include "in_world.hpp"
|
||||
#include "in_combat.hpp"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
void ClientApplication::Init(int argc, char** argv) {
|
||||
//load the prerequisites
|
||||
config.Load("rsc\\config.cfg");
|
||||
|
||||
//initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO)) {
|
||||
throw(std::runtime_error("Failed to initialize SDL"));
|
||||
}
|
||||
BaseScene::SetScreen(config.Int("screen.w"), config.Int("screen.h"), 0, (config.Bool("screen.f")) ? SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN : SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||
|
||||
//initialize SDL_net
|
||||
if (SDLNet_Init()) {
|
||||
throw(std::runtime_error("Failed to initialize SDL_net"));
|
||||
}
|
||||
network.Open(0, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void ClientApplication::Proc() {
|
||||
LoadScene(SceneList::FIRST);
|
||||
|
||||
//prepare the time system
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
|
||||
std::chrono::duration<int, std::milli> delta(16);
|
||||
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()) / std::chrono::duration<int, std::milli>::period::den);
|
||||
simTime += delta;
|
||||
}
|
||||
|
||||
//draw the game to the screen
|
||||
activeScene->RenderFrame();
|
||||
}
|
||||
|
||||
UnloadScene();
|
||||
}
|
||||
|
||||
void ClientApplication::Quit() {
|
||||
network.Close();
|
||||
SDLNet_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Private access members
|
||||
//-------------------------
|
||||
|
||||
void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||
UnloadScene();
|
||||
switch(sceneIndex) {
|
||||
//add scene creation calls here
|
||||
case SceneList::FIRST:
|
||||
case SceneList::SPLASHSCREEN:
|
||||
activeScene = new SplashScreen(&config);
|
||||
break;
|
||||
case SceneList::MAINMENU:
|
||||
activeScene = new MainMenu(&config);
|
||||
break;
|
||||
case SceneList::OPTIONSMENU:
|
||||
activeScene = new OptionsMenu(&config);
|
||||
break;
|
||||
case SceneList::LOBBYMENU:
|
||||
activeScene = new LobbyMenu(&config, &network, &clientIndex, &characterIndex);
|
||||
break;
|
||||
case SceneList::INWORLD:
|
||||
activeScene = new InWorld(&config, &network, &clientIndex, &characterIndex);
|
||||
break;
|
||||
case SceneList::INCOMBAT:
|
||||
activeScene = new InCombat();
|
||||
break;
|
||||
default:
|
||||
throw(std::logic_error("Failed to recognize the scene index"));
|
||||
}
|
||||
}
|
||||
|
||||
void ClientApplication::UnloadScene() {
|
||||
delete activeScene;
|
||||
activeScene = nullptr;
|
||||
}
|
||||
@@ -19,42 +19,36 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef SCENEMANAGER_HPP_
|
||||
#define SCENEMANAGER_HPP_
|
||||
#ifndef CLIENTAPPLICATION_HPP_
|
||||
#define CLIENTAPPLICATION_HPP_
|
||||
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "packet_type.hpp"
|
||||
#include "information_manager.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class SceneManager {
|
||||
class ClientApplication {
|
||||
public:
|
||||
/* Public access members */
|
||||
SceneManager();
|
||||
~SceneManager();
|
||||
ClientApplication() = default;
|
||||
~ClientApplication() = default;
|
||||
|
||||
void Init();
|
||||
void Init(int argc, char** argv);
|
||||
void Proc();
|
||||
void Quit();
|
||||
|
||||
private:
|
||||
/* Private access members */
|
||||
//Private access members
|
||||
void LoadScene(SceneList sceneIndex);
|
||||
void UnloadScene();
|
||||
|
||||
BaseScene* activeScene = nullptr;
|
||||
|
||||
ConfigUtility* configUtil = nullptr;
|
||||
SurfaceManager* surfaceMgr = nullptr;
|
||||
UDPNetworkUtility* netUtil = nullptr;
|
||||
InformationManager* infoMgr = nullptr;
|
||||
//shared parameters
|
||||
ConfigUtility config;
|
||||
UDPNetworkUtility network;
|
||||
int clientIndex = -1;
|
||||
int characterIndex = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,173 +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;
|
||||
}
|
||||
|
||||
InWorld::~InWorld() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving InWorld" << endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void InWorld::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Update(double delta) {
|
||||
while(HandlePacket(popNetworkPacket()));
|
||||
}
|
||||
|
||||
void InWorld::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Render(SDL_Surface* const screen) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//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) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
ExitGame();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Utilities
|
||||
//-------------------------
|
||||
|
||||
int InWorld::HandlePacket(Packet p) {
|
||||
switch(p.meta.type) {
|
||||
case PacketType::NONE:
|
||||
//DO NOTHING
|
||||
return 0;
|
||||
break;
|
||||
case PacketType::PING:
|
||||
//quick pong
|
||||
p.meta.type = PacketType::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||
break;
|
||||
case PacketType::PONG:
|
||||
//
|
||||
break;
|
||||
// case PacketType::BROADCAST_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::BROADCAST_RESPONSE:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::JOIN_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::JOIN_RESPONSE:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::DISCONNECT:
|
||||
HandleDisconnection(p.disconnect);
|
||||
break;
|
||||
// case PacketType::SYNCHRONIZE:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::PLAYER_NEW:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::PLAYER_DELETE:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::PLAYER_MOVE:
|
||||
// //
|
||||
// break;
|
||||
default:
|
||||
throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type))));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void InWorld::Disconnect() {
|
||||
//disconnect
|
||||
Packet p;
|
||||
p.meta.type = PacketType::DISCONNECT;
|
||||
p.disconnect.clientIndex = infoMgr->GetClientIndex();
|
||||
netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||
netUtil->Unbind(GAME_CHANNEL);
|
||||
endQueueThread();
|
||||
|
||||
//reset the client
|
||||
infoMgr->ResetClientIndex();
|
||||
}
|
||||
|
||||
void InWorld::ExitGame() {
|
||||
Disconnect();
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
cout << "The game session has ended" << endl;
|
||||
}
|
||||
|
||||
void InWorld::HandleDisconnection(::Disconnect& disconnect) {
|
||||
Disconnect();
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
cout << "You have been disconnected" << endl;
|
||||
}
|
||||
@@ -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 PacketType::NONE:
|
||||
//DO NOTHING
|
||||
return 0;
|
||||
break;
|
||||
case PacketType::PING:
|
||||
//quick pong
|
||||
p.meta.type = PacketType::PONG;
|
||||
netUtil->Send(&p.meta.address, &p, sizeof(Packet));
|
||||
break;
|
||||
case PacketType::PONG:
|
||||
//
|
||||
break;
|
||||
// case PacketType::BROADCAST_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::BROADCAST_RESPONSE:
|
||||
PushServer(p.broadcastResponse);
|
||||
break;
|
||||
// case PacketType::JOIN_REQUEST:
|
||||
// //
|
||||
// break;
|
||||
case PacketType::JOIN_RESPONSE:
|
||||
BeginGame(p.joinResponse);
|
||||
break;
|
||||
// case PacketType::DISCONNECT:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::SYNCHRONIZE:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::PLAYER_NEW:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::PLAYER_DELETE:
|
||||
// //
|
||||
// break;
|
||||
// case PacketType::PLAYER_MOVE:
|
||||
// //
|
||||
// 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 = PacketType::BROADCAST_REQUEST;
|
||||
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet));
|
||||
serverList.clear();
|
||||
}
|
||||
|
||||
void Lobby::PushServer(BroadcastResponse& bcast) {
|
||||
ServerEntry entry;
|
||||
entry.name = bcast.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 = PacketType::JOIN_REQUEST;
|
||||
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet));
|
||||
}
|
||||
|
||||
void Lobby::BeginGame(JoinResponse& response) {
|
||||
//should be downloading the resources here as well
|
||||
infoMgr->SetClientIndex(response.clientIndex);
|
||||
netUtil->Bind(&response.meta.address, GAME_CHANNEL);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
}
|
||||
+6
-10
@@ -19,29 +19,25 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "scene_manager.hpp"
|
||||
#include "client_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int, char**) {
|
||||
#ifdef DEBUG
|
||||
cout << "Beginning program" << endl;
|
||||
#endif
|
||||
int main(int argc, char** argv) {
|
||||
cout << "Beginning client" << endl;
|
||||
try {
|
||||
SceneManager app;
|
||||
app.Init();
|
||||
ClientApplication app;
|
||||
app.Init(argc, argv);
|
||||
app.Proc();
|
||||
app.Quit();
|
||||
}
|
||||
catch(exception& e) {
|
||||
cerr << "Fatal error: " << e.what() << endl;
|
||||
cerr << "Fatal exception thrown: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
cout << "Clean exit" << endl;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
+12
-7
@@ -1,16 +1,17 @@
|
||||
#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
|
||||
INCLUDES+=. scenes ../common ../common/graphics ../common/map ../common/network ../common/ui
|
||||
LIBS+=libclient.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
SRC=$(wildcard *.cpp)
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
@@ -18,7 +19,8 @@ OUT=$(addprefix $(OUTDIR)/,client)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIB)
|
||||
$(MAKE) -C scenes
|
||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
@@ -33,6 +35,9 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "player_character.hpp"
|
||||
|
||||
#define WALKING_SPEED 140
|
||||
|
||||
void PlayerCharacter::Update(double delta) {
|
||||
if (diagonal) {
|
||||
constexpr double d = 1.0/sqrt(2);
|
||||
position += motion * delta * d;
|
||||
}
|
||||
else {
|
||||
position += motion * delta;
|
||||
}
|
||||
sprite.Update(delta);
|
||||
}
|
||||
|
||||
void PlayerCharacter::AdjustDirection(Direction direction) {
|
||||
//shift the movement in this direction
|
||||
switch(direction) {
|
||||
case Direction::NORTH:
|
||||
if (motion.y >= 0) {
|
||||
motion.y -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case Direction::SOUTH:
|
||||
if (motion.y <= 0) {
|
||||
motion.y += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case Direction::WEST:
|
||||
if (motion.x >= 0) {
|
||||
motion.x -= WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
case Direction::EAST:
|
||||
if (motion.x <= 0) {
|
||||
motion.x += WALKING_SPEED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
//face the correct direction
|
||||
ResetDirection();
|
||||
}
|
||||
|
||||
void PlayerCharacter::FaceDirection(Direction direction) {
|
||||
//this function depends on the format of the sprite sheets
|
||||
switch(direction) {
|
||||
case Direction::NORTH:
|
||||
sprite.SetYIndex(1);
|
||||
break;
|
||||
case Direction::SOUTH:
|
||||
sprite.SetYIndex(0);
|
||||
break;
|
||||
case Direction::WEST:
|
||||
sprite.SetYIndex(2);
|
||||
break;
|
||||
case Direction::EAST:
|
||||
sprite.SetYIndex(3);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerCharacter::ResetDirection() {
|
||||
//base the direction on the character's movement
|
||||
if (motion.y > 0) {
|
||||
FaceDirection(Direction::SOUTH);
|
||||
}
|
||||
else if (motion.y < 0) {
|
||||
FaceDirection(Direction::NORTH);
|
||||
}
|
||||
else if (motion.x > 0) {
|
||||
FaceDirection(Direction::EAST);
|
||||
}
|
||||
else if (motion.x < 0) {
|
||||
FaceDirection(Direction::WEST);
|
||||
}
|
||||
ResetSpeed();
|
||||
}
|
||||
|
||||
void PlayerCharacter::ResetSpeed() {
|
||||
//diagonal
|
||||
if (motion.x != 0 && motion.y != 0) {
|
||||
sprite.SetDelay(0.1);
|
||||
diagonal = true;
|
||||
}
|
||||
//cardinal
|
||||
else if (motion != 0) {
|
||||
sprite.SetDelay(0.1);
|
||||
diagonal = false;
|
||||
}
|
||||
//not moving
|
||||
else {
|
||||
sprite.SetDelay(0);
|
||||
sprite.SetXIndex(0);
|
||||
diagonal = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/* 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"
|
||||
|
||||
//TODO: correct the PlayerCharacter class and it's movement system
|
||||
class PlayerCharacter {
|
||||
public:
|
||||
enum class Direction {
|
||||
NORTH, SOUTH, EAST, WEST
|
||||
};
|
||||
|
||||
PlayerCharacter() = default;
|
||||
~PlayerCharacter() = default;
|
||||
|
||||
void Update(double delta);
|
||||
|
||||
void DrawTo(SDL_Surface* const dest, int camX, int camY) { sprite.DrawTo(dest, position.x - camX, position.y - camY); }
|
||||
|
||||
//clunky code results in smooth movement and controls
|
||||
void AdjustDirection(Direction);
|
||||
void FaceDirection(Direction);
|
||||
void ResetDirection();
|
||||
void ResetSpeed();
|
||||
|
||||
//accessors and mutators
|
||||
Vector2 SetPosition(Vector2 v) { return position = v; }
|
||||
Vector2 ShiftPosition(Vector2 v) { return position += v; }
|
||||
Vector2 GetPosition() { return position; }
|
||||
|
||||
Vector2 SetMotion(Vector2 v) { return motion = v; }
|
||||
Vector2 ShiftMotion(Vector2 v) { return motion += v; }
|
||||
Vector2 GetMotion() { return motion; }
|
||||
|
||||
SpriteSheet* GetSprite() { return &sprite; }
|
||||
private:
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
SpriteSheet sprite;
|
||||
|
||||
//for moving diagonally
|
||||
bool diagonal = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -29,12 +29,12 @@ enum class SceneList {
|
||||
FIRST,
|
||||
|
||||
//custom indexes
|
||||
INCOMBAT,
|
||||
INWORLD,
|
||||
LOBBY,
|
||||
MAINMENU,
|
||||
OPTIONSCREEN,
|
||||
SPLASHSCREEN,
|
||||
MAINMENU,
|
||||
OPTIONSMENU,
|
||||
LOBBYMENU,
|
||||
INWORLD,
|
||||
INCOMBAT,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,195 +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 "scene_manager.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
|
||||
//-------------------------
|
||||
|
||||
SceneManager::SceneManager() {
|
||||
//
|
||||
}
|
||||
|
||||
SceneManager::~SceneManager() {
|
||||
UnloadScene();
|
||||
}
|
||||
|
||||
/* SceneManager::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 created 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 SceneManager::Init() {
|
||||
//load the config file
|
||||
try {
|
||||
configUtil = ServiceLocator<ConfigUtility>::Set(new ConfigUtility());
|
||||
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);
|
||||
|
||||
//instanciate the remaining services
|
||||
surfaceMgr = ServiceLocator<SurfaceManager>::Set(new SurfaceManager());
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(new UDPNetworkUtility());
|
||||
infoMgr = ServiceLocator<InformationManager>::Set(new InformationManager());
|
||||
|
||||
//initiate the remaining services
|
||||
netUtil->Open(0, sizeof(Packet));
|
||||
}
|
||||
|
||||
void SceneManager::Proc() {
|
||||
LoadScene(SceneList::FIRST);
|
||||
|
||||
//prepare the time system
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
Clock::duration delta(16 * Clock::duration::period::den / std::milli::den);
|
||||
Clock::time_point simTime = Clock::now();
|
||||
Clock::time_point realTime;
|
||||
|
||||
//The main loop
|
||||
while(activeScene->GetNextScene() != SceneList::QUIT) {
|
||||
//switch scenes when necessary
|
||||
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 SceneManager::Quit() {
|
||||
//clean up the services
|
||||
netUtil->Close();
|
||||
surfaceMgr->FreeAll();
|
||||
|
||||
//delete the services
|
||||
configUtil = ServiceLocator<ConfigUtility>::Set(nullptr);
|
||||
surfaceMgr = ServiceLocator<SurfaceManager>::Set(nullptr);
|
||||
netUtil = ServiceLocator<UDPNetworkUtility>::Set(nullptr);
|
||||
infoMgr = ServiceLocator<InformationManager>::Set(nullptr);
|
||||
|
||||
//clean up the scene
|
||||
UnloadScene();
|
||||
|
||||
//deinitialize the APIs
|
||||
SDLNet_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Private access members
|
||||
//-------------------------
|
||||
|
||||
void SceneManager::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 SceneManager::UnloadScene() {
|
||||
delete activeScene;
|
||||
activeScene = nullptr;
|
||||
}
|
||||
@@ -34,7 +34,7 @@ SDL_Surface* BaseScene::screen = nullptr;
|
||||
//-------------------------
|
||||
|
||||
BaseScene::BaseScene() {
|
||||
nextScene = SceneList::CONTINUE;
|
||||
//
|
||||
}
|
||||
|
||||
BaseScene::~BaseScene() {
|
||||
@@ -28,18 +28,18 @@
|
||||
|
||||
class BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
//Public access members
|
||||
BaseScene();
|
||||
virtual ~BaseScene();
|
||||
|
||||
/* Program control */
|
||||
//Program control
|
||||
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||
static SDL_Surface* GetScreen();
|
||||
|
||||
SceneList SetNextScene(SceneList sceneIndex);
|
||||
SceneList GetNextScene() const;
|
||||
|
||||
/* Frame loop */
|
||||
//Frame loop
|
||||
virtual void RunFrame(double delta);
|
||||
virtual void RenderFrame();
|
||||
|
||||
@@ -50,7 +50,7 @@ protected:
|
||||
virtual void FrameEnd() {}
|
||||
virtual void Render(SDL_Surface* const screen) {}
|
||||
|
||||
/* Event handlers */
|
||||
//Event handlers
|
||||
virtual void QuitEvent() { SetNextScene(SceneList::QUIT); }
|
||||
virtual void MouseMotion(SDL_MouseMotionEvent const&) {}
|
||||
virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {}
|
||||
@@ -68,7 +68,7 @@ protected:
|
||||
|
||||
private:
|
||||
static SDL_Surface* screen;
|
||||
SceneList nextScene;
|
||||
SceneList nextScene = SceneList::CONTINUE;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -21,24 +21,16 @@
|
||||
*/
|
||||
#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
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
@@ -26,18 +26,18 @@
|
||||
|
||||
class InCombat : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
//Public access members
|
||||
InCombat();
|
||||
~InCombat();
|
||||
|
||||
protected:
|
||||
/* Frame loop */
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
/* Event handlers */
|
||||
//Event handlers
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
@@ -0,0 +1,431 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "in_world.hpp"
|
||||
|
||||
#include "channels.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argCharacterIndex):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex),
|
||||
characterIndex(*argCharacterIndex)
|
||||
{
|
||||
//setup the utility objects
|
||||
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
buttonImage.SetClipH(buttonImage.GetClipH()/3);
|
||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
||||
|
||||
//pass the utility objects
|
||||
disconnectButton.SetImage(&buttonImage);
|
||||
disconnectButton.SetFont(&font);
|
||||
shutDownButton.SetImage(&buttonImage);
|
||||
shutDownButton.SetFont(&font);
|
||||
|
||||
//set the button positions
|
||||
disconnectButton.SetX(50);
|
||||
disconnectButton.SetY(50 + buttonImage.GetClipH() * 0);
|
||||
shutDownButton.SetX(50);
|
||||
shutDownButton.SetY(50 + buttonImage.GetClipH() * 1);
|
||||
|
||||
//set the button texts
|
||||
disconnectButton.SetText("Disconnect");
|
||||
shutDownButton.SetText("Shut Down");
|
||||
|
||||
//load the tilesheet
|
||||
//TODO: add the tilesheet to the map system?
|
||||
tileSheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15);
|
||||
|
||||
//request a sync
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_STRING_SIZE];
|
||||
packet.meta.type = SerialPacket::Type::SYNCHRONIZE;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
packet.clientInfo.characterIndex = characterIndex;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
|
||||
//debug
|
||||
// RequestRegion(0, 0);
|
||||
}
|
||||
|
||||
InWorld::~InWorld() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void InWorld::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::Update(double delta) {
|
||||
SerialPacket packet;
|
||||
|
||||
//suck in all waiting packets
|
||||
while(network.Receive()) {
|
||||
deserialize(&packet, network.GetInData());
|
||||
packet.meta.srcAddress = network.GetInPacket()->address;
|
||||
HandlePacket(packet);
|
||||
}
|
||||
|
||||
//update the characters
|
||||
for (auto& it : playerCharacters) {
|
||||
it.second.Update(delta);
|
||||
}
|
||||
//TODO: sort the players and entities by Y position
|
||||
|
||||
//update the camera
|
||||
if(localCharacter) {
|
||||
camera.x = localCharacter->GetPosition().x - camera.marginX;
|
||||
camera.y = localCharacter->GetPosition().y - camera.marginY;
|
||||
}
|
||||
|
||||
//check the map
|
||||
UpdateMap();
|
||||
}
|
||||
|
||||
void InWorld::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::RenderFrame() {
|
||||
// SDL_FillRect(GetScreen(), 0, 0);
|
||||
Render(GetScreen());
|
||||
SDL_Flip(GetScreen());
|
||||
fps.Calculate();
|
||||
}
|
||||
|
||||
void InWorld::Render(SDL_Surface* const screen) {
|
||||
//draw the map
|
||||
for (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
|
||||
tileSheet.DrawRegionTo(screen, *it, camera.x, camera.y);
|
||||
}
|
||||
|
||||
//draw characters
|
||||
for (auto& it : playerCharacters) {
|
||||
it.second.DrawTo(screen, camera.x, camera.y);
|
||||
}
|
||||
|
||||
//draw UI
|
||||
disconnectButton.DrawTo(screen);
|
||||
shutDownButton.DrawTo(screen);
|
||||
font.DrawStringTo(to_string_custom(fps.GetFrameRate()), screen, 0, 0);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void InWorld::QuitEvent() {
|
||||
//exit the game AND the server
|
||||
RequestDisconnect();
|
||||
}
|
||||
|
||||
void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
disconnectButton.MouseMotion(motion);
|
||||
shutDownButton.MouseMotion(motion);
|
||||
}
|
||||
|
||||
void InWorld::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
disconnectButton.MouseButtonDown(button);
|
||||
shutDownButton.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (disconnectButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
RequestDisconnect();
|
||||
}
|
||||
if (shutDownButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
RequestShutDown();
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE: {
|
||||
QuitEvent();
|
||||
}
|
||||
break;
|
||||
|
||||
//player movement
|
||||
case SDLK_LEFT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_UP:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
//player movement
|
||||
case SDLK_LEFT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::EAST);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::WEST);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_UP:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::SOUTH);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
if (localCharacter) {
|
||||
localCharacter->AdjustDirection(PlayerCharacter::Direction::NORTH);
|
||||
SendPlayerUpdate();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Network handlers
|
||||
//-------------------------
|
||||
|
||||
void InWorld::HandlePacket(SerialPacket packet) {
|
||||
switch(packet.meta.type) {
|
||||
case SerialPacket::Type::DISCONNECT:
|
||||
HandleDisconnect(packet);
|
||||
break;
|
||||
case SerialPacket::Type::REGION_CONTENT:
|
||||
HandleRegionContent(packet);
|
||||
break;
|
||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
||||
HandleCharacterUpdate(packet);
|
||||
break;
|
||||
case SerialPacket::Type::CHARACTER_NEW:
|
||||
HandleCharacterNew(packet);
|
||||
break;
|
||||
case SerialPacket::Type::CHARACTER_DELETE:
|
||||
HandleCharacterDelete(packet);
|
||||
break;
|
||||
//handle errors
|
||||
default:
|
||||
throw(std::runtime_error("Unknown SerialPacket::Type encountered"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandleDisconnect(SerialPacket packet) {
|
||||
network.Unbind(Channels::SERVER);
|
||||
clientIndex = -1;
|
||||
characterIndex = -1;
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
|
||||
void InWorld::HandleRegionContent(SerialPacket packet) {
|
||||
//replace existing regions
|
||||
//TODO: account for map index
|
||||
if (regionPager.FindRegion(packet.regionInfo.x, packet.regionInfo.y)) {
|
||||
regionPager.UnloadRegion(packet.regionInfo.x, packet.regionInfo.y);
|
||||
}
|
||||
regionPager.PushRegion(packet.regionInfo.region);
|
||||
packet.regionInfo.region = nullptr;
|
||||
}
|
||||
|
||||
void InWorld::HandleCharacterUpdate(SerialPacket packet) {
|
||||
if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) {
|
||||
HandleCharacterNew(packet);
|
||||
return;
|
||||
}
|
||||
|
||||
//update only if the message didn't originate from here
|
||||
if (packet.characterInfo.clientIndex != clientIndex) {
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetPosition(packet.characterInfo.position);
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetMotion(packet.characterInfo.motion);
|
||||
}
|
||||
playerCharacters[packet.characterInfo.characterIndex].ResetDirection();
|
||||
}
|
||||
|
||||
void InWorld::HandleCharacterNew(SerialPacket packet) {
|
||||
if (playerCharacters.find(packet.characterInfo.characterIndex) != playerCharacters.end()) {
|
||||
throw(std::runtime_error("Cannot create duplicate characters"));
|
||||
}
|
||||
|
||||
//TODO: set the player's handle
|
||||
playerCharacters[packet.characterInfo.characterIndex].GetSprite()->LoadSurface(config["dir.sprites"] + packet.characterInfo.avatar, 4, 4);
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetPosition(packet.characterInfo.position);
|
||||
playerCharacters[packet.characterInfo.characterIndex].SetMotion(packet.characterInfo.motion);
|
||||
playerCharacters[packet.characterInfo.characterIndex].ResetDirection();
|
||||
|
||||
//catch this client's player object
|
||||
if (packet.characterInfo.characterIndex == characterIndex && !localCharacter) {
|
||||
localCharacter = &playerCharacters[characterIndex];
|
||||
|
||||
//setup the camera
|
||||
camera.width = GetScreen()->w;
|
||||
camera.height = GetScreen()->h;
|
||||
//center on the player's character
|
||||
camera.marginX = (GetScreen()->w / 2 - localCharacter->GetSprite()->GetImage()->GetClipW() / 2);
|
||||
camera.marginY = (GetScreen()->h / 2 - localCharacter->GetSprite()->GetImage()->GetClipH() / 2);
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::HandleCharacterDelete(SerialPacket packet) {
|
||||
if (playerCharacters.find(packet.characterInfo.characterIndex) == playerCharacters.end()) {
|
||||
throw(std::runtime_error("Cannot delete non-existant characters"));
|
||||
}
|
||||
|
||||
playerCharacters.erase(packet.characterInfo.characterIndex);
|
||||
|
||||
//catch this client's player object
|
||||
if (packet.characterInfo.characterIndex == characterIndex) {
|
||||
characterIndex = -1;
|
||||
localCharacter = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Server control
|
||||
//-------------------------
|
||||
|
||||
void InWorld::SendPlayerUpdate() {
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
|
||||
//pack the packet
|
||||
packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
|
||||
packet.characterInfo.clientIndex = clientIndex;
|
||||
packet.characterInfo.characterIndex = characterIndex;
|
||||
packet.characterInfo.position = localCharacter->GetPosition();
|
||||
packet.characterInfo.motion = localCharacter->GetMotion();
|
||||
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void InWorld::RequestDisconnect() {
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
|
||||
//send a disconnect request
|
||||
packet.meta.type = SerialPacket::Type::DISCONNECT;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void InWorld::RequestShutDown() {
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
|
||||
//send a shutdown request
|
||||
packet.meta.type = SerialPacket::Type::SHUTDOWN;
|
||||
packet.clientInfo.clientIndex = clientIndex;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
void InWorld::RequestRegion(int mapIndex, int x, int y) {
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
|
||||
//pack the region's data
|
||||
packet.meta.type = SerialPacket::Type::REGION_REQUEST;
|
||||
packet.regionInfo.mapIndex = mapIndex;
|
||||
packet.regionInfo.x = x;
|
||||
packet.regionInfo.y = y;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Utilities
|
||||
//-------------------------
|
||||
|
||||
//TODO: convert this into a more generic function?; using parameters for the bounds
|
||||
void InWorld::UpdateMap() {
|
||||
//these represent the zone of regions that the client needs loaded, including the mandatory buffers (+1/-1)
|
||||
int xStart = snapToBase(REGION_WIDTH, camera.x/tileSheet.GetTileW()) - REGION_WIDTH;
|
||||
int xEnd = snapToBase(REGION_WIDTH, (camera.x+camera.width)/tileSheet.GetTileW()) + REGION_WIDTH;
|
||||
|
||||
int yStart = snapToBase(REGION_HEIGHT, camera.y/tileSheet.GetTileH()) - REGION_HEIGHT;
|
||||
int yEnd = snapToBase(REGION_HEIGHT, (camera.y+camera.height)/tileSheet.GetTileH()) + REGION_HEIGHT;
|
||||
|
||||
//prune distant regions
|
||||
for (auto it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); /* EMPTY */) {
|
||||
//check if the region is outside off this area
|
||||
if ((*it)->GetX() < xStart || (*it)->GetX() > xEnd || (*it)->GetY() < yStart || (*it)->GetY() > yEnd) {
|
||||
|
||||
//clunky, but the alternative was time consuming
|
||||
int tmpX = (*it)->GetX();
|
||||
int tmpY = (*it)->GetY();
|
||||
++it;
|
||||
|
||||
regionPager.UnloadRegion(tmpX, tmpY);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
|
||||
//request empty regions within this zone
|
||||
for (int i = xStart; i <= xEnd; i += REGION_WIDTH) {
|
||||
for (int j = yStart; j <= yEnd; j += REGION_HEIGHT) {
|
||||
if (!regionPager.FindRegion(i, j)) {
|
||||
RequestRegion(0, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/* 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_
|
||||
|
||||
//maps
|
||||
#include "map_allocator.hpp"
|
||||
#include "map_file_format.hpp"
|
||||
#include "region_pager.hpp"
|
||||
|
||||
//networking
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "serial_packet.hpp"
|
||||
#include "serial.hpp"
|
||||
|
||||
//graphics
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
#include "tile_sheet.hpp"
|
||||
|
||||
//common
|
||||
#include "config_utility.hpp"
|
||||
#include "frame_rate.hpp"
|
||||
|
||||
//client
|
||||
#include "base_scene.hpp"
|
||||
#include "player_character.hpp"
|
||||
|
||||
//STL
|
||||
#include <map>
|
||||
|
||||
class InWorld : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InWorld(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const);
|
||||
~InWorld();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void RenderFrame();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
void QuitEvent();
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//Network handlers
|
||||
void HandlePacket(SerialPacket);
|
||||
void HandleDisconnect(SerialPacket);
|
||||
void HandleCharacterNew(SerialPacket);
|
||||
void HandleCharacterDelete(SerialPacket);
|
||||
void HandleCharacterUpdate(SerialPacket);
|
||||
void HandleRegionContent(SerialPacket);
|
||||
|
||||
//Server control
|
||||
void SendPlayerUpdate();
|
||||
void RequestDisconnect();
|
||||
void RequestShutDown();
|
||||
void RequestRegion(int mapIndex, int x, int y);
|
||||
|
||||
//utilities
|
||||
void UpdateMap();
|
||||
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
int& characterIndex;
|
||||
|
||||
//graphics
|
||||
Image buttonImage;
|
||||
RasterFont font;
|
||||
TileSheet tileSheet;
|
||||
|
||||
//map
|
||||
RegionPager<BlankAllocator, DummyFormat> regionPager;
|
||||
|
||||
//UI
|
||||
Button disconnectButton;
|
||||
Button shutDownButton;
|
||||
//TODO: Fix the camera
|
||||
struct {
|
||||
int x = 0, y = 0;
|
||||
int width = 0, height = 0;
|
||||
int marginX = 0, marginY = 0;
|
||||
} camera;
|
||||
FrameRate fps;
|
||||
|
||||
//game
|
||||
std::map<int, PlayerCharacter> playerCharacters;
|
||||
PlayerCharacter* localCharacter = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,234 @@
|
||||
/* 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_menu.hpp"
|
||||
|
||||
#include "channels.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
LobbyMenu::LobbyMenu(ConfigUtility* const argConfig, UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argCharacterIndex):
|
||||
config(*argConfig),
|
||||
network(*argNetwork),
|
||||
clientIndex(*argClientIndex),
|
||||
characterIndex(*argCharacterIndex)
|
||||
{
|
||||
//setup the utility objects
|
||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
image.SetClipH(image.GetClipH()/3);
|
||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
||||
|
||||
//pass the utility objects
|
||||
search.SetImage(&image);
|
||||
search.SetFont(&font);
|
||||
join.SetImage(&image);
|
||||
join.SetFont(&font);
|
||||
back.SetImage(&image);
|
||||
back.SetFont(&font);
|
||||
|
||||
//set the button positions
|
||||
search.SetX(50);
|
||||
search.SetY(50 + image.GetClipH() * 0);
|
||||
join.SetX(50);
|
||||
join.SetY(50 + image.GetClipH() * 1);
|
||||
back.SetX(50);
|
||||
back.SetY(50 + image.GetClipH() * 2);
|
||||
|
||||
//set the button texts
|
||||
search.SetText("Search");
|
||||
join.SetText("Join");
|
||||
back.SetText("Back");
|
||||
|
||||
//set the server list's position
|
||||
listBox = {300, 50, 200, font.GetCharH()};
|
||||
}
|
||||
|
||||
LobbyMenu::~LobbyMenu() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void LobbyMenu::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void LobbyMenu::Update(double delta) {
|
||||
//suck in all waiting packets
|
||||
SerialPacket packet;
|
||||
while(network.Receive()) {
|
||||
deserialize(&packet, network.GetInData());
|
||||
packet.meta.srcAddress = network.GetInPacket()->address;
|
||||
HandlePacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
void LobbyMenu::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void LobbyMenu::Render(SDL_Surface* const screen) {
|
||||
//TODO: I need a proper UI system for the entire client and the editor
|
||||
//UI
|
||||
search.DrawTo(screen);
|
||||
join.DrawTo(screen);
|
||||
back.DrawTo(screen);
|
||||
|
||||
//TODO: draw headers for the server list
|
||||
for (int i = 0; i < serverInfo.size(); i++) {
|
||||
//draw the selected server's highlight
|
||||
if (selection == &serverInfo[i]) {
|
||||
SDL_Rect r = listBox;
|
||||
r.y += i * listBox.h;
|
||||
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 127, 39));
|
||||
}
|
||||
|
||||
//draw the server name
|
||||
font.DrawStringTo(serverInfo[i].name, screen, listBox.x, listBox.y + i*listBox.h);
|
||||
|
||||
//draw the player count
|
||||
font.DrawStringTo(to_string_custom(serverInfo[i].playerCount), screen, listBox.x + listBox.w, listBox.y + i*listBox.h);
|
||||
|
||||
//compatible?
|
||||
if (!serverInfo[i].compatible) {
|
||||
font.DrawStringTo("?", screen, listBox.x - font.GetCharW(), listBox.y + i*listBox.h);
|
||||
}
|
||||
|
||||
//ping?
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
search.MouseMotion(motion);
|
||||
join.MouseMotion(motion);
|
||||
back.MouseMotion(motion);
|
||||
}
|
||||
|
||||
void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
search.MouseButtonDown(button);
|
||||
join.MouseButtonDown(button);
|
||||
back.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (search.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
//the vars
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
|
||||
//broadcast to the network, or a specific server
|
||||
packet.meta.type = SerialPacket::Type::BROADCAST_REQUEST;
|
||||
serialize(&packet, buffer);
|
||||
network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, PACKET_BUFFER_SIZE);
|
||||
|
||||
//reset the server list
|
||||
serverInfo.clear();
|
||||
selection = nullptr;
|
||||
}
|
||||
|
||||
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
|
||||
//the vars
|
||||
SerialPacket packet;
|
||||
char buffer[PACKET_BUFFER_SIZE];
|
||||
|
||||
//pack the packet
|
||||
packet.meta.type = SerialPacket::Type::JOIN_REQUEST;
|
||||
strncpy(packet.clientInfo.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
|
||||
strncpy(packet.clientInfo.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE);
|
||||
strncpy(packet.clientInfo.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE);
|
||||
|
||||
//join the selected server
|
||||
serialize(&packet, buffer);
|
||||
network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE);
|
||||
selection = nullptr;
|
||||
}
|
||||
|
||||
else if (back.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
|
||||
else if (
|
||||
//has the user selected a server on the list?
|
||||
button.x > listBox.x &&
|
||||
button.x < listBox.x + listBox.w &&
|
||||
button.y > listBox.y &&
|
||||
button.y < listBox.y + listBox.h * serverInfo.size()
|
||||
) {
|
||||
selection = &serverInfo[(button.y - listBox.y)/listBox.h];
|
||||
}
|
||||
else {
|
||||
selection = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
void LobbyMenu::HandlePacket(SerialPacket packet) {
|
||||
switch(packet.meta.type) {
|
||||
case SerialPacket::Type::BROADCAST_RESPONSE: {
|
||||
//extract the data
|
||||
ServerInformation server;
|
||||
server.address = packet.meta.srcAddress;
|
||||
server.networkVersion = packet.serverInfo.networkVersion;
|
||||
server.name = packet.serverInfo.name;
|
||||
server.playerCount = packet.serverInfo.playerCount;
|
||||
|
||||
//NOTE: Check compatibility here
|
||||
server.compatible = server.networkVersion == NETWORK_VERSION;
|
||||
|
||||
//push
|
||||
serverInfo.push_back(server);
|
||||
}
|
||||
break;
|
||||
case SerialPacket::Type::JOIN_RESPONSE:
|
||||
clientIndex = packet.clientInfo.clientIndex;
|
||||
characterIndex = packet.clientInfo.characterIndex;
|
||||
network.Bind(&packet.meta.srcAddress, Channels::SERVER);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
break;
|
||||
|
||||
//handle errors
|
||||
default:
|
||||
throw(std::runtime_error("Unknown SerialPacket::Type encountered"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -19,35 +19,31 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef LOBBY_HPP_
|
||||
#define LOBBY_HPP_
|
||||
#ifndef LOBBYMENU_HPP_
|
||||
#define LOBBYMENU_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "packet_type.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"
|
||||
//graphics & utilities
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
#include "config_utility.hpp"
|
||||
|
||||
//network
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "serial_packet.hpp"
|
||||
#include "serial.hpp"
|
||||
|
||||
//client
|
||||
#include "base_scene.hpp"
|
||||
|
||||
//STL
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
struct ServerEntry {
|
||||
std::string name;
|
||||
IPaddress address;
|
||||
};
|
||||
|
||||
class Lobby : public BaseScene {
|
||||
class LobbyMenu : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
Lobby();
|
||||
~Lobby();
|
||||
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, int* const, int* const);
|
||||
~LobbyMenu();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
@@ -63,28 +59,36 @@ protected:
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//utilities
|
||||
int HandlePacket(Packet p);
|
||||
void BroadcastNetwork();
|
||||
void PushServer(BroadcastResponse&);
|
||||
void ConnectToServer(ServerEntry*);
|
||||
void BeginGame(JoinResponse&);
|
||||
void HandlePacket(SerialPacket);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = ServiceLocator<InformationManager>::Get();
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
int& characterIndex;
|
||||
|
||||
//members
|
||||
Button refreshButton;
|
||||
Button joinButton;
|
||||
Button backButton;
|
||||
|
||||
Image image;
|
||||
RasterFont font;
|
||||
Button search;
|
||||
Button join;
|
||||
Button back;
|
||||
|
||||
//server list
|
||||
struct ServerInformation {
|
||||
IPaddress address;
|
||||
int networkVersion;
|
||||
std::string name;
|
||||
int playerCount;
|
||||
bool compatible;
|
||||
};
|
||||
|
||||
std::vector<ServerInformation> serverInfo;
|
||||
|
||||
//a terrible hack, forgive me
|
||||
//I'd love a proper gui system for this
|
||||
SDL_Rect listBox;
|
||||
std::vector<ServerEntry> serverList;
|
||||
ServerEntry* selectedServer = nullptr;
|
||||
ServerInformation* selection = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -21,27 +21,42 @@
|
||||
*/
|
||||
#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(ConfigUtility* const argConfig):
|
||||
config(*argConfig)
|
||||
{
|
||||
//setup the utility objects
|
||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
image.SetClipH(image.GetClipH()/3);
|
||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
||||
|
||||
//pass the utility objects
|
||||
startButton.SetImage(&image);
|
||||
startButton.SetFont(&font);
|
||||
optionsButton.SetImage(&image);
|
||||
optionsButton.SetFont(&font);
|
||||
quitButton.SetImage(&image);
|
||||
quitButton.SetFont(&font);
|
||||
|
||||
//set the button positions
|
||||
startButton.SetX(50);
|
||||
startButton.SetY(50 + image.GetClipH() * 0);
|
||||
optionsButton.SetX(50);
|
||||
optionsButton.SetY(50 + image.GetClipH() * 1);
|
||||
quitButton.SetX(50);
|
||||
quitButton.SetY(50 + image.GetClipH() * 2);
|
||||
|
||||
//set the button texts
|
||||
startButton.SetText("Start");
|
||||
optionsButton.SetText("Options");
|
||||
quitButton.SetText("Quit");
|
||||
}
|
||||
|
||||
MainMenu::~MainMenu() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving MainMenu" << endl;
|
||||
#endif
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
@@ -84,10 +99,10 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
|
||||
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (startButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::LOBBY);
|
||||
SetNextScene(SceneList::LOBBYMENU);
|
||||
}
|
||||
if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::OPTIONSCREEN);
|
||||
SetNextScene(SceneList::OPTIONSMENU);
|
||||
}
|
||||
if (quitButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
QuitEvent();
|
||||
@@ -98,6 +113,10 @@ void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
break;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void MainMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
@@ -23,32 +23,38 @@
|
||||
#define MAINMENU_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
|
||||
#include "surface_manager.hpp"
|
||||
#include "config_utility.hpp"
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
class MainMenu : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
MainMenu();
|
||||
//Public access members
|
||||
MainMenu(ConfigUtility* const);
|
||||
~MainMenu();
|
||||
|
||||
protected:
|
||||
/* Frame loop */
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
/* Event handlers */
|
||||
//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&);
|
||||
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
|
||||
//members
|
||||
Image image;
|
||||
RasterFont font;
|
||||
Button startButton;
|
||||
Button optionsButton;
|
||||
Button quitButton;
|
||||
@@ -0,0 +1,43 @@
|
||||
#config
|
||||
INCLUDES+=. .. ../../common ../../common/graphics ../../common/map ../../common/network ../../common/ui
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=..
|
||||
OUT=$(addprefix $(OUTDIR)/,libclient.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -0,0 +1,102 @@
|
||||
/* 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 "options_menu.hpp"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
OptionsMenu::OptionsMenu(ConfigUtility* const argConfig):
|
||||
config(*argConfig)
|
||||
{
|
||||
//setup the utility objects
|
||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
image.SetClipH(image.GetClipH()/3);
|
||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
||||
|
||||
//pass the utility objects
|
||||
backButton.SetImage(&image);
|
||||
backButton.SetFont(&font);
|
||||
|
||||
//set the button positions
|
||||
backButton.SetX(50);
|
||||
backButton.SetY(50 + image.GetClipH() * 0);
|
||||
|
||||
//set the button texts
|
||||
backButton.SetText("Back");
|
||||
}
|
||||
|
||||
OptionsMenu::~OptionsMenu() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void OptionsMenu::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void OptionsMenu::Update(double delta) {
|
||||
//
|
||||
}
|
||||
|
||||
void OptionsMenu::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void OptionsMenu::Render(SDL_Surface* const screen) {
|
||||
backButton.DrawTo(screen);
|
||||
|
||||
font.DrawStringTo("Oh, were you looking for the options screen?", screen, 50, 30);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void OptionsMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
backButton.MouseMotion(motion);
|
||||
}
|
||||
|
||||
void OptionsMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
backButton.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void OptionsMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
}
|
||||
|
||||
void OptionsMenu::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void OptionsMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
@@ -19,27 +19,21 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef INWORLD_HPP_
|
||||
#define INWORLD_HPP_
|
||||
#ifndef OPTIONSMENU_HPP_
|
||||
#define OPTIONSMENU_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "service_locator.hpp"
|
||||
#include "packet_type.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 "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
class InWorld : public BaseScene {
|
||||
class OptionsMenu : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InWorld();
|
||||
~InWorld();
|
||||
OptionsMenu(ConfigUtility* const);
|
||||
~OptionsMenu();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
@@ -49,27 +43,19 @@ protected:
|
||||
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 p);
|
||||
void Disconnect();
|
||||
void ExitGame();
|
||||
void HandleDisconnection(::Disconnect&);
|
||||
|
||||
//services
|
||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
InformationManager* infoMgr = ServiceLocator<InformationManager>::Get();
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
|
||||
//members
|
||||
//...
|
||||
Image image;
|
||||
RasterFont font;
|
||||
Button backButton;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -19,26 +19,33 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "sprite_sheet.hpp"
|
||||
#include "splash_screen.hpp"
|
||||
|
||||
SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) {
|
||||
SetSurface(s, w, h);
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
SplashScreen::SplashScreen(ConfigUtility* const argConfig):
|
||||
config(*argConfig)
|
||||
{
|
||||
logo.LoadSurface(config["dir.logos"] + "krstudios.bmp");
|
||||
startTick = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void SpriteSheet::Update(double delta) {
|
||||
if (interval && (ticks += delta) >= interval) {
|
||||
if (++currentFrame >= maxFrames) {
|
||||
currentFrame = 0;
|
||||
}
|
||||
ticks = 0;
|
||||
SplashScreen::~SplashScreen() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void SplashScreen::Update(double delta) {
|
||||
if (std::chrono::steady_clock::now() - startTick > std::chrono::duration<int>(1)) {
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
image.SetClipX(currentFrame * image.GetClipW());
|
||||
image.SetClipY(currentStrip * image.GetClipH());
|
||||
}
|
||||
|
||||
SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* const s, Uint16 w, Uint16 h) {
|
||||
image.SetSurface(s, {0, 0, w, h});
|
||||
currentFrame = 0; maxFrames = image.GetSurface()->w / image.GetClipW();
|
||||
currentStrip = 0; maxStrips = image.GetSurface()->h / image.GetClipH();
|
||||
interval = ticks = 0;
|
||||
void SplashScreen::Render(SDL_Surface* const screen) {
|
||||
logo.DrawTo(screen, (screen->w - logo.GetClipW()) / 2, (screen->h - logo.GetClipH()) / 2);
|
||||
}
|
||||
@@ -23,32 +23,29 @@
|
||||
#define SPLASHSCREEN_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "defines.hpp"
|
||||
#include "service_locator.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "surface_manager.hpp"
|
||||
#include "image.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
class SplashScreen : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
SplashScreen();
|
||||
//Public access members
|
||||
SplashScreen(ConfigUtility* const);
|
||||
~SplashScreen();
|
||||
|
||||
protected:
|
||||
/* Frame loop */
|
||||
void RunFrame(double delta);
|
||||
void RenderFrame();
|
||||
void LoadResources();
|
||||
//Frame loop
|
||||
void Update(double delta);
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
bool loaded = false;
|
||||
ConfigUtility* configUtil = ServiceLocator<ConfigUtility>::Get();
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
//shared parameters
|
||||
ConfigUtility& config;
|
||||
|
||||
//members
|
||||
std::chrono::steady_clock::time_point startTick;
|
||||
Image logo;
|
||||
Clock::time_point start = Clock::now();
|
||||
};
|
||||
|
||||
#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 (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
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef BBOX_HPP_
|
||||
#define BBOX_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
|
||||
//TODO: This is supposed to interact with the vector
|
||||
class BBox {
|
||||
public:
|
||||
double x, y;
|
||||
double w, h;
|
||||
|
||||
BBox() = default;
|
||||
BBox(double i, double j, double k, double l): x(i), y(j), w(k), h(l) {};
|
||||
~BBox() = default;
|
||||
BBox& operator=(BBox const&) = default;
|
||||
|
||||
double Size() {
|
||||
return std::max(w*h,0.0);
|
||||
}
|
||||
|
||||
bool IsCollision(BBox rhs) {
|
||||
return not (
|
||||
x >= rhs.x + rhs.w ||
|
||||
y >= rhs.y + rhs.h ||
|
||||
rhs.x >= x + w ||
|
||||
rhs.y >= y + h
|
||||
);
|
||||
}
|
||||
|
||||
BBox Intersection(BBox rhs) {
|
||||
if (!IsCollision(rhs)) {
|
||||
return {0, 0, 0, 0};
|
||||
}
|
||||
BBox ret;
|
||||
ret.x = std::max(x, rhs.x);
|
||||
ret.y = std::max(y, rhs.y);
|
||||
ret.w = std::min(x+w, rhs.x+rhs.w) - ret.x;
|
||||
ret.h = std::min(y+h, rhs.y+rhs.h) - ret.y;
|
||||
return ret;
|
||||
}
|
||||
|
||||
double operator[](size_t i) {
|
||||
if (i >= 4)
|
||||
throw(std::domain_error("Out of range"));
|
||||
return *(&x+i);
|
||||
}
|
||||
};
|
||||
|
||||
//This is explicitly a POD
|
||||
static_assert(std::is_pod<BBox>::value, "BBox is not a POD");
|
||||
|
||||
#endif
|
||||
@@ -21,8 +21,9 @@
|
||||
*/
|
||||
#include "config_utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -74,3 +75,31 @@ void ConfigUtility::Load(string fname) {
|
||||
|
||||
is.close();
|
||||
}
|
||||
|
||||
std::string& ConfigUtility::String(std::string s) {
|
||||
return table[s];
|
||||
}
|
||||
|
||||
int ConfigUtility::Integer(std::string s) {
|
||||
std::map<std::string, std::string>::iterator it = table.find(s);
|
||||
if (it == table.end()) {
|
||||
return 0;
|
||||
}
|
||||
return atoi(it->second.c_str());
|
||||
}
|
||||
|
||||
double ConfigUtility::Double(std::string s) {
|
||||
std::map<std::string, std::string>::iterator it = table.find(s);
|
||||
if (it == table.end()) {
|
||||
return 0.0;
|
||||
}
|
||||
return atof(it->second.c_str());
|
||||
}
|
||||
|
||||
bool ConfigUtility::Boolean(std::string s) {
|
||||
std::map<std::string, std::string>::iterator it = table.find(s);
|
||||
if (it == table.end()) {
|
||||
return false;
|
||||
}
|
||||
return it->second == "true";
|
||||
}
|
||||
@@ -24,40 +24,32 @@
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
class ConfigUtility {
|
||||
public:
|
||||
ConfigUtility() {}
|
||||
ConfigUtility() = default;
|
||||
ConfigUtility(std::string s) { Load(s); }
|
||||
|
||||
void Load(std::string fname);
|
||||
|
||||
std::string String(std::string s) {
|
||||
return table[s];
|
||||
}
|
||||
const char* CString(std::string s) {
|
||||
return table[s].c_str();
|
||||
}
|
||||
int Integer(std::string s) {
|
||||
return atoi(table[s].c_str());
|
||||
}
|
||||
double Double(std::string s) {
|
||||
return atof(table[s].c_str());
|
||||
}
|
||||
bool Boolean(std::string s) {
|
||||
return table[s] == "true";
|
||||
}
|
||||
//convert to a type
|
||||
std::string& String(std::string);
|
||||
int Integer(std::string);
|
||||
double Double(std::string);
|
||||
bool Boolean(std::string);
|
||||
|
||||
//shorthand
|
||||
std::string& operator[](std::string s) {
|
||||
return table[s];
|
||||
return String(s);
|
||||
}
|
||||
int Int(std::string s) {
|
||||
return Integer(s);
|
||||
}
|
||||
int Bool(std::string s) {
|
||||
bool Bool(std::string s) {
|
||||
return Boolean(s);
|
||||
}
|
||||
|
||||
//OO breaker
|
||||
std::map<std::string, std::string>* GetMap() {
|
||||
return &table;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
@@ -28,8 +28,8 @@ class FrameRate {
|
||||
public:
|
||||
typedef std::chrono::high_resolution_clock Clock;
|
||||
|
||||
FrameRate() = delete;
|
||||
static int Calculate() {
|
||||
FrameRate() = default;
|
||||
int Calculate() {
|
||||
frameCount++;
|
||||
if (Clock::now() - tick >= std::chrono::duration<int>(1)) {
|
||||
lastFrameRate = frameCount;
|
||||
@@ -38,11 +38,11 @@ public:
|
||||
}
|
||||
return lastFrameRate;
|
||||
}
|
||||
static int GetFrameRate() { return lastFrameRate; }
|
||||
int GetFrameRate() { return lastFrameRate; }
|
||||
private:
|
||||
static int frameCount;
|
||||
static int lastFrameRate;
|
||||
static Clock::time_point tick;
|
||||
int frameCount = 0;
|
||||
int lastFrameRate = 0;
|
||||
Clock::time_point tick = Clock::now();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,145 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "image.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
Image& Image::operator=(Image const& rhs) {
|
||||
//don't screw yourself
|
||||
if (this == &rhs) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
FreeSurface();
|
||||
|
||||
//Copy the other Image's stuff
|
||||
surface = rhs.surface;
|
||||
clip = rhs.clip;
|
||||
local = false;
|
||||
}
|
||||
|
||||
Image& Image::operator=(Image&& rhs) {
|
||||
//don't screw yourself
|
||||
if (this == &rhs) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
FreeSurface();
|
||||
|
||||
//Steal the other Image's stuff
|
||||
surface = rhs.surface;
|
||||
clip = rhs.clip;
|
||||
local = rhs.local;
|
||||
|
||||
rhs.surface = nullptr;
|
||||
rhs.clip = {0, 0, 0, 0};
|
||||
rhs.local = false;
|
||||
}
|
||||
|
||||
SDL_Surface* Image::LoadSurface(std::string fname) {
|
||||
FreeSurface();
|
||||
SDL_Surface* p = SDL_LoadBMP(fname.c_str());
|
||||
if (!p) {
|
||||
std::ostringstream os;
|
||||
os << "Failed to load file: " << fname;
|
||||
throw(std::runtime_error(os.str()));
|
||||
}
|
||||
surface = p;
|
||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||
local = true;
|
||||
SetTransparentColor(255, 0, 255); //default
|
||||
return surface;
|
||||
}
|
||||
|
||||
SDL_Surface* Image::CreateSurface(Uint16 w, Uint16 h) {
|
||||
FreeSurface();
|
||||
Uint32 rmask, gmask, bmask, amask;
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
rmask = 0xff000000;
|
||||
gmask = 0x00ff0000;
|
||||
bmask = 0x0000ff00;
|
||||
amask = 0x000000ff;
|
||||
#else
|
||||
rmask = 0x000000ff;
|
||||
gmask = 0x0000ff00;
|
||||
bmask = 0x00ff0000;
|
||||
amask = 0xff000000;
|
||||
#endif
|
||||
SDL_Surface* p = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, rmask, gmask, bmask, amask);
|
||||
if (!p) {
|
||||
throw(std::runtime_error("Failed to create Image surface"));
|
||||
}
|
||||
surface = p;
|
||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||
local = true;
|
||||
SetTransparentColor(255, 0, 255); //default
|
||||
return surface;
|
||||
}
|
||||
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* p) {
|
||||
FreeSurface();
|
||||
if (!p) {
|
||||
throw(std::invalid_argument("No surface pointer provided"));
|
||||
}
|
||||
surface = p;
|
||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||
local = false;
|
||||
return surface;
|
||||
}
|
||||
|
||||
void Image::FreeSurface() {
|
||||
if (local) {
|
||||
SDL_FreeSurface(surface);
|
||||
local = false;
|
||||
}
|
||||
surface = nullptr;
|
||||
clip = {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) {
|
||||
if (!surface) {
|
||||
throw(std::logic_error("No image surface to draw"));
|
||||
}
|
||||
SDL_Rect sclip = clip, dclip = {x,y};
|
||||
SDL_BlitSurface(surface, &sclip, dest, &dclip);
|
||||
}
|
||||
|
||||
void Image::SetTransparentColor(Uint8 r, Uint8 g, Uint8 b) {
|
||||
if (!surface) {
|
||||
throw(std::logic_error("Failed to set the transparent color"));
|
||||
}
|
||||
if (!local) {
|
||||
throw(std::logic_error("Cannot set the transparent color of a non-local surface"));
|
||||
}
|
||||
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b));
|
||||
}
|
||||
|
||||
void Image::ClearTransparentColor() {
|
||||
if (!surface) {
|
||||
throw(std::logic_error("Failed to clear the transparent color"));
|
||||
}
|
||||
if (!local) {
|
||||
throw(std::logic_error("Cannot clear the transparent color of a non-local surface"));
|
||||
}
|
||||
SDL_SetColorKey(surface, 0, 0);
|
||||
}
|
||||
@@ -23,17 +23,26 @@
|
||||
#define IMAGE_HPP_
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
#include <string>
|
||||
|
||||
class Image {
|
||||
public:
|
||||
Image() = default;
|
||||
Image(SDL_Surface*);
|
||||
Image(SDL_Surface*, SDL_Rect);
|
||||
~Image() = default;
|
||||
Image(Image const& rhs) { *this = rhs; }
|
||||
Image(Image&& rhs) { *this = std::move(rhs); }
|
||||
Image(std::string fname) { LoadSurface(fname); }
|
||||
Image(Uint16 w, Uint16 h) { CreateSurface(w, h); }
|
||||
Image(SDL_Surface* p) { SetSurface(p); }
|
||||
~Image() { FreeSurface(); }
|
||||
|
||||
Image& operator=(Image const&);
|
||||
Image& operator=(Image&&);
|
||||
|
||||
SDL_Surface* LoadSurface(std::string fname);
|
||||
SDL_Surface* CreateSurface(Uint16 w, Uint16 h);
|
||||
SDL_Surface* SetSurface(SDL_Surface*);
|
||||
SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect);
|
||||
SDL_Surface* GetSurface() const { return surface; }
|
||||
void FreeSurface();
|
||||
|
||||
void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
|
||||
|
||||
@@ -50,9 +59,15 @@ public:
|
||||
Sint16 GetClipY() const { return clip.y; }
|
||||
Uint16 GetClipW() const { return clip.w; }
|
||||
Uint16 GetClipH() const { return clip.h; }
|
||||
|
||||
bool GetLocal() const { return local; }
|
||||
|
||||
void SetTransparentColor(Uint8 r, Uint8 g, Uint8 b);
|
||||
void ClearTransparentColor();
|
||||
protected:
|
||||
SDL_Surface* surface = nullptr;
|
||||
SDL_Rect clip = {0, 0, 0, 0};
|
||||
bool local = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,20 +1,21 @@
|
||||
#config
|
||||
LIBDIR=..
|
||||
LOCALLIBS=$(LIBDIR)/out/libCodebase.a
|
||||
LIB=
|
||||
INCLUDES=$(LIBDIR)/Codebase
|
||||
INCLUDES+=. .. ../map
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
SRC=$(wildcard *.cpp)
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,libCommon.a)
|
||||
OUTDIR=../..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
@@ -33,6 +34,9 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/* 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 "sprite_sheet.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
void SpriteSheet::Update(double delta) {
|
||||
if (delay && (tick += delta) >= delay) {
|
||||
if (++xIndex >= xCount) {
|
||||
xIndex = 0;
|
||||
}
|
||||
tick = 0;
|
||||
}
|
||||
image.SetClipX(xIndex * image.GetClipW());
|
||||
image.SetClipY(yIndex * image.GetClipH());
|
||||
}
|
||||
|
||||
SDL_Surface* SpriteSheet::LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount) {
|
||||
image.LoadSurface(fname);
|
||||
|
||||
xCount = xCellCount;
|
||||
yCount = yCellCount;
|
||||
|
||||
image.SetClipW(image.GetSurface()->w / xCount);
|
||||
image.SetClipH(image.GetSurface()->h / yCount);
|
||||
|
||||
xIndex = yIndex = 0;
|
||||
delay = tick = 0.0;
|
||||
}
|
||||
|
||||
SDL_Surface* SpriteSheet::SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) {
|
||||
image.SetSurface(surface);
|
||||
|
||||
xCount = xCellCount;
|
||||
yCount = yCellCount;
|
||||
|
||||
image.SetClipW(image.GetSurface()->w / xCount);
|
||||
image.SetClipH(image.GetSurface()->h / yCount);
|
||||
|
||||
xIndex = yIndex = 0;
|
||||
delay = tick = 0.0;
|
||||
}
|
||||
|
||||
void SpriteSheet::FreeSurface() {
|
||||
image.FreeSurface();
|
||||
xCount = yCount = 0;
|
||||
xIndex = yIndex = 0;
|
||||
delay = tick = 0.0;
|
||||
}
|
||||
|
||||
Uint16 SpriteSheet::SetXCount(Uint16 i) {
|
||||
xIndex = 0;
|
||||
return xCount = i;
|
||||
}
|
||||
|
||||
Uint16 SpriteSheet::SetYCount(Uint16 i) {
|
||||
yIndex = 0;
|
||||
return yCount = i;
|
||||
}
|
||||
|
||||
Uint16 SpriteSheet::SetXIndex(Uint16 i) {
|
||||
if (i > xCount) {
|
||||
std::ostringstream os;
|
||||
os << "Cannot set x index to " << i;
|
||||
throw(std::invalid_argument(os.str()));
|
||||
}
|
||||
return xIndex = i;
|
||||
}
|
||||
|
||||
Uint16 SpriteSheet::SetYIndex(Uint16 i) {
|
||||
if (i > yCount) {
|
||||
std::ostringstream os;
|
||||
os << "Cannot set y index to " << i;
|
||||
throw(std::invalid_argument(os.str()));
|
||||
}
|
||||
return yIndex = i;
|
||||
}
|
||||
|
||||
double SpriteSheet::SetDelay(double d) {
|
||||
tick = 0;
|
||||
return delay = d;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef SPRITESHEET_HPP_
|
||||
#define SPRITESHEET_HPP_
|
||||
|
||||
#include "image.hpp"
|
||||
|
||||
class SpriteSheet {
|
||||
public:
|
||||
SpriteSheet() = default;
|
||||
SpriteSheet(std::string fname, Uint16 xCellCount, Uint16 yCellCount) { LoadSurface(fname, xCellCount, yCellCount); }
|
||||
SpriteSheet(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount) { SetSurface(surface, xCellCount, yCellCount); }
|
||||
~SpriteSheet() { FreeSurface(); };
|
||||
|
||||
void Update(double delta);
|
||||
|
||||
SDL_Surface* LoadSurface(std::string fname, Uint16 xCellCount, Uint16 yCellCount);
|
||||
SDL_Surface* SetSurface(SDL_Surface* surface, Uint16 xCellCount, Uint16 yCellCount);
|
||||
SDL_Surface* GetSurface() { return image.GetSurface(); }
|
||||
void FreeSurface();
|
||||
|
||||
void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) { image.DrawTo(dest, x, y); }
|
||||
|
||||
//accessors and mutators
|
||||
Image* GetImage() { return ℑ } //OO breaker
|
||||
|
||||
Uint16 SetXCount(Uint16);
|
||||
Uint16 SetYCount(Uint16);
|
||||
Uint16 SetXIndex(Uint16);
|
||||
Uint16 SetYIndex(Uint16);
|
||||
|
||||
Uint16 GetXCount() const { return xCount; }
|
||||
Uint16 GetYCount() const { return yCount; }
|
||||
Uint16 GetXIndex() const { return xIndex; }
|
||||
Uint16 GetYIndex() const { return yIndex; }
|
||||
|
||||
double SetDelay(double d);
|
||||
double GetDelay() const { return delay; }
|
||||
|
||||
private:
|
||||
Image image;
|
||||
Uint16 xCount = 0, yCount = 0; //number of cells
|
||||
Uint16 xIndex = 0, yIndex = 0; //current cell being drawn
|
||||
double delay = 0.0, tick = 0.0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "tile_sheet.hpp"
|
||||
|
||||
void TileSheet::Load(std::string fname, int xc, int yc) {
|
||||
XCount = xc;
|
||||
YCount = yc;
|
||||
image.LoadSurface(fname);
|
||||
image.SetClipW(image.GetClipW()/XCount);
|
||||
image.SetClipH(image.GetClipH()/YCount);
|
||||
}
|
||||
|
||||
void TileSheet::Unload() {
|
||||
image.FreeSurface();
|
||||
XCount = YCount = 0;
|
||||
}
|
||||
|
||||
void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile) {
|
||||
//0 is invisible
|
||||
if (tile == 0) return;
|
||||
image.SetClipX((tile-1) % XCount * image.GetClipW());
|
||||
image.SetClipY((tile-1) / XCount * image.GetClipH());
|
||||
image.DrawTo(dest, x, y);
|
||||
}
|
||||
|
||||
void TileSheet::DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY) {
|
||||
Region::type_t tile = 0;
|
||||
for (register int i = 0; i < REGION_WIDTH; ++i) {
|
||||
for (register int j = 0; j < REGION_HEIGHT; ++j) {
|
||||
for (register int k = 0; k < REGION_DEPTH; ++k) {
|
||||
tile = region->GetTile(i, j, k);
|
||||
//0 is invisible
|
||||
if (tile == 0) continue;
|
||||
image.SetClipX((tile-1) % XCount * image.GetClipW());
|
||||
image.SetClipY((tile-1) / XCount * image.GetClipH());
|
||||
image.DrawTo(dest,
|
||||
(region->GetX() + i) * image.GetClipW() - camX,
|
||||
(region->GetY() + j) * image.GetClipH() - camY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef TILESHEET_HPP_
|
||||
#define TILESHEET_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
|
||||
#include "image.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
class TileSheet {
|
||||
public:
|
||||
TileSheet() = default;
|
||||
TileSheet(std::string f, int x, int y) { Load(f, x, y); }
|
||||
~TileSheet() = default;
|
||||
|
||||
void Load(std::string fname, int XCount, int YCount);
|
||||
void Unload();
|
||||
|
||||
void DrawTo(SDL_Surface* const dest, int x, int y, Region::type_t tile);
|
||||
void DrawRegionTo(SDL_Surface* const dest, Region* const region, int camX, int camY);
|
||||
|
||||
//accessors
|
||||
Image* GetImage() { return ℑ }
|
||||
int GetXCount() { return XCount; }
|
||||
int GetYCount() { return YCount; }
|
||||
int GetTileW() { return image.GetClipW(); }
|
||||
int GetTileH() { return image.GetClipH(); }
|
||||
private:
|
||||
Image image;
|
||||
int XCount = 0, YCount = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
#config
|
||||
INCLUDES+=.
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
$(MAKE) -C graphics
|
||||
$(MAKE) -C map
|
||||
$(MAKE) -C script
|
||||
$(MAKE) -C network
|
||||
$(MAKE) -C ui
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -0,0 +1,43 @@
|
||||
#config
|
||||
INCLUDES+=. .. ../graphics
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -0,0 +1,60 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "map_allocator.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void BlankAllocator::Create(Region** const ptr, int x, int y) {
|
||||
(*ptr) = new Region(x, y);
|
||||
}
|
||||
|
||||
void BlankAllocator::Unload(Region* const ptr) {
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
void LuaAllocator::Create(Region** const ptr, int x, int y) {
|
||||
//something to work on
|
||||
(*ptr) = new Region(x, y);
|
||||
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "create");
|
||||
lua_pushlightuserdata(state, *ptr);
|
||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
|
||||
void LuaAllocator::Unload(Region* const ptr) {
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "unload");
|
||||
lua_pushlightuserdata(state, ptr);
|
||||
if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
|
||||
//clean up the memory
|
||||
delete ptr;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef MAPALLOCATOR_HPP_
|
||||
#define MAPALLOCATOR_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
class BlankAllocator {
|
||||
public:
|
||||
void Create(Region** const, int x, int y);
|
||||
void Unload(Region* const);
|
||||
private:
|
||||
//
|
||||
};
|
||||
|
||||
class LuaAllocator {
|
||||
public:
|
||||
void Create(Region** const, int x, int y);
|
||||
void Unload(Region* const);
|
||||
|
||||
lua_State* SetLuaState(lua_State* L) { return state = L; }
|
||||
lua_State* GetLuaState() { return state; }
|
||||
private:
|
||||
lua_State* state = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "map_file_format.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
void DummyFormat::Load(Region** const ptr, int x, int y) {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void DummyFormat::Save(Region* const ptr) {
|
||||
//EMPTY
|
||||
}
|
||||
|
||||
void LuaFormat::Load(Region** const ptr, int x, int y) {
|
||||
//something to load into
|
||||
|
||||
if (!(*ptr)) {
|
||||
(*ptr) = new Region(x, y);
|
||||
}
|
||||
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "load");
|
||||
lua_pushlightuserdata(state, *ptr);
|
||||
lua_pushstring(state, saveDir.c_str());
|
||||
if (lua_pcall(state, 2, 1, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
if (lua_toboolean(state, -1) == false) {
|
||||
delete (*ptr);
|
||||
(*ptr) = nullptr;
|
||||
}
|
||||
lua_pop(state, 2);
|
||||
}
|
||||
|
||||
void LuaFormat::Save(Region* const ptr) {
|
||||
//API hook
|
||||
lua_getglobal(state, "map");
|
||||
lua_getfield(state, -1, "save");
|
||||
lua_pushlightuserdata(state, ptr);
|
||||
lua_pushstring(state, saveDir.c_str());
|
||||
if (lua_pcall(state, 2, 0, 0) != LUA_OK) {
|
||||
throw(std::runtime_error(std::string() + "Lua error: " + lua_tostring(state, -1) ));
|
||||
}
|
||||
lua_pop(state, 1);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef MAPFILEFORMAT_HPP_
|
||||
#define MAPFILEFORMAT_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
class DummyFormat {
|
||||
public:
|
||||
void Load(Region** const, int x, int y);
|
||||
void Save(Region* const);
|
||||
|
||||
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||
std::string GetSaveDir() { return saveDir; }
|
||||
private:
|
||||
std::string saveDir;
|
||||
};
|
||||
|
||||
//TODO: verbose save file format
|
||||
//TODO: compact save file format
|
||||
|
||||
class LuaFormat {
|
||||
public:
|
||||
void Load(Region** const, int x, int y);
|
||||
void Save(Region* const);
|
||||
|
||||
std::string SetSaveDir(std::string s) { return saveDir = s; }
|
||||
std::string GetSaveDir() { return saveDir; }
|
||||
|
||||
lua_State* SetLuaState(lua_State* L) { return state = L; }
|
||||
lua_State* GetLuaState() { return state; }
|
||||
private:
|
||||
std::string saveDir;
|
||||
lua_State* state = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "region.hpp"
|
||||
|
||||
Region::Region(int argX, int argY):
|
||||
x(argX),
|
||||
y(argY)
|
||||
{
|
||||
for (register int i = 0; i < REGION_WIDTH*REGION_HEIGHT*REGION_DEPTH; ++i) {
|
||||
*(reinterpret_cast<type_t*>(tiles) + i) = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Region::type_t Region::SetTile(int x, int y, int z, type_t v) {
|
||||
return tiles[x][y][z] = v;
|
||||
}
|
||||
|
||||
Region::type_t Region::GetTile(int x, int y, int z) {
|
||||
return tiles[x][y][z];
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef REGION_HPP_
|
||||
#define REGION_HPP_
|
||||
|
||||
#define REGION_WIDTH 20
|
||||
#define REGION_HEIGHT 20
|
||||
#define REGION_DEPTH 3
|
||||
|
||||
class Region {
|
||||
public:
|
||||
typedef unsigned short type_t;
|
||||
|
||||
Region() = delete;
|
||||
Region(int x, int y);
|
||||
~Region() = default;
|
||||
|
||||
type_t SetTile(int x, int y, int z, type_t v);
|
||||
type_t GetTile(int x, int y, int z);
|
||||
|
||||
//accessors
|
||||
int GetX() const { return x; }
|
||||
int GetY() const { return y; }
|
||||
private:
|
||||
const int x;
|
||||
const int y;
|
||||
|
||||
type_t tiles[REGION_WIDTH][REGION_HEIGHT][REGION_DEPTH];
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,67 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "region_pager.hpp"
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
|
||||
Region* ptr = GetRegion(x, y);
|
||||
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
|
||||
}
|
||||
|
||||
Region::type_t RegionPagerBase::GetTile(int x, int y, int z) {
|
||||
Region* ptr = GetRegion(x, y);
|
||||
return ptr->GetTile(x - ptr->GetX(), y - ptr->GetY(), z);
|
||||
}
|
||||
|
||||
Region* RegionPagerBase::GetRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//get the region by various means
|
||||
Region* ptr = nullptr;
|
||||
ptr = FindRegion(x, y);
|
||||
if (ptr) return ptr;
|
||||
ptr = LoadRegion(x, y);
|
||||
if (ptr) return ptr;
|
||||
return CreateRegion(x, y);
|
||||
}
|
||||
|
||||
Region* RegionPagerBase::FindRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//find the region
|
||||
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); it++) {
|
||||
if ((*it)->GetX() == x && (*it)->GetY() == y) {
|
||||
return *it;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Region* RegionPagerBase::PushRegion(Region* ptr) {
|
||||
regionList.push_front(ptr);
|
||||
return regionList.front();
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef REGIONPAGER_HPP_
|
||||
#define REGIONPAGER_HPP_
|
||||
|
||||
#include "region.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
class RegionPagerBase {
|
||||
public:
|
||||
RegionPagerBase() {};
|
||||
virtual ~RegionPagerBase() {};
|
||||
|
||||
//tile manipulation
|
||||
Region::type_t SetTile(int x, int y, int z, Region::type_t v);
|
||||
Region::type_t GetTile(int x, int y, int z);
|
||||
|
||||
//region manipulation
|
||||
Region* GetRegion(int x, int y);
|
||||
Region* FindRegion(int x, int y);
|
||||
Region* PushRegion(Region*);
|
||||
|
||||
//interface
|
||||
virtual Region* LoadRegion(int x, int y) = 0;
|
||||
virtual Region* SaveRegion(int x, int y) = 0;
|
||||
virtual Region* CreateRegion(int x, int y) = 0;
|
||||
virtual void UnloadRegion(int x, int y) = 0;
|
||||
//TODO: delete existing regions
|
||||
|
||||
//accessors & mutators
|
||||
std::list<Region*>* GetContainer() { return ®ionList; }
|
||||
protected:
|
||||
std::list<Region*> regionList;
|
||||
};
|
||||
|
||||
template<typename Allocator, typename FileFormat>
|
||||
class RegionPager : public RegionPagerBase {
|
||||
public:
|
||||
RegionPager() {};
|
||||
~RegionPager() {
|
||||
UnloadAll();
|
||||
}
|
||||
|
||||
Region* LoadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//load the region if possible
|
||||
Region* ptr = nullptr;
|
||||
format.Load(&ptr, x, y);
|
||||
if (ptr) {
|
||||
return PushRegion(ptr);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Region* SaveRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//find & save the region
|
||||
Region* ptr = FindRegion(x, y);
|
||||
if (ptr) {
|
||||
format.Save(ptr);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Region* CreateRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//create and push the object
|
||||
Region* ptr = nullptr;
|
||||
allocator.Create(&ptr, x, y);
|
||||
return PushRegion(ptr);
|
||||
}
|
||||
|
||||
void UnloadRegion(int x, int y) {
|
||||
//snap the coords
|
||||
x = snapToBase(REGION_WIDTH, x);
|
||||
y = snapToBase(REGION_HEIGHT, y);
|
||||
|
||||
//custom loop
|
||||
for (std::list<Region*>::iterator it = regionList.begin(); it != regionList.end(); /* EMPTY */) {
|
||||
if ((*it)->GetX() == x && (*it)->GetY() == y) {
|
||||
allocator.Unload(*it);
|
||||
it = regionList.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
void UnloadAll() {
|
||||
for (auto& it : regionList) {
|
||||
allocator.Unload(it);
|
||||
}
|
||||
regionList.clear();
|
||||
}
|
||||
|
||||
//accessors
|
||||
Allocator* GetAllocator() { return &allocator; }
|
||||
FileFormat* GetFormat() { return &format; }
|
||||
protected:
|
||||
Allocator allocator;
|
||||
FileFormat format;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,20 +1,21 @@
|
||||
#config
|
||||
LIBDIR=..
|
||||
LOCALLIBS=
|
||||
LIB=
|
||||
INCLUDES=
|
||||
INCLUDES+=. .. ../map
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
SRC=$(wildcard *.cpp)
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,libCodebase.a)
|
||||
OUTDIR=../..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
@@ -33,6 +34,9 @@ $(OUTDIR):
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "serial.hpp"
|
||||
|
||||
#include "map_allocator.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
//-------------------------
|
||||
//Convenience Macros
|
||||
//-------------------------
|
||||
|
||||
#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer += size;
|
||||
#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer += size;
|
||||
|
||||
//-------------------------
|
||||
//internal serialization functions
|
||||
//-------------------------
|
||||
|
||||
void serializeType(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
}
|
||||
|
||||
void serializeServer(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//server info
|
||||
SERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int));
|
||||
SERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int));
|
||||
}
|
||||
|
||||
void serializeClient(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
SERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
SERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
}
|
||||
|
||||
void serializeRegionFormat(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
}
|
||||
|
||||
void serializeRegionContent(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
|
||||
//content
|
||||
for (register int i = 0; i < REGION_WIDTH; i++) {
|
||||
for (register int j = 0; j < REGION_HEIGHT; j++) {
|
||||
for (register int k = 0; k < REGION_DEPTH; k++) {
|
||||
*reinterpret_cast<Region::type_t*>(buffer) = packet->regionInfo.region->GetTile(i, j, k);
|
||||
buffer += sizeof(Region::type_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void serializeCharacter(SerialPacket* packet, char* buffer) {
|
||||
SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
SERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int));
|
||||
SERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
|
||||
//vectors
|
||||
SERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double));
|
||||
SERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double));
|
||||
SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
|
||||
SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//internal deserialization functions
|
||||
//-------------------------
|
||||
|
||||
void deserializeType(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
}
|
||||
|
||||
void deserializeServer(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//server info
|
||||
DESERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int));
|
||||
DESERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int));
|
||||
}
|
||||
|
||||
void deserializeClient(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
DESERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
DESERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
}
|
||||
|
||||
void deserializeRegionFormat(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
}
|
||||
|
||||
void deserializeRegionContent(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//format
|
||||
DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int));
|
||||
|
||||
//an object to work on
|
||||
BlankAllocator().Create(
|
||||
&packet->regionInfo.region,
|
||||
packet->regionInfo.x,
|
||||
packet->regionInfo.y
|
||||
);
|
||||
|
||||
//content
|
||||
for (register int i = 0; i < REGION_WIDTH; i++) {
|
||||
for (register int j = 0; j < REGION_HEIGHT; j++) {
|
||||
for (register int k = 0; k < REGION_DEPTH; k++) {
|
||||
packet->regionInfo.region->SetTile(i, j, k, *reinterpret_cast<Region::type_t*>(buffer));
|
||||
buffer += sizeof(Region::type_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void deserializeCharacter(SerialPacket* packet, char* buffer) {
|
||||
DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type));
|
||||
|
||||
//indexes
|
||||
DESERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int));
|
||||
|
||||
//texts
|
||||
DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE);
|
||||
DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE);
|
||||
|
||||
//vectors
|
||||
DESERIALIZE(buffer, &packet->characterInfo.position.x, sizeof(double));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.position.y, sizeof(double));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double));
|
||||
DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double));
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//the interface functions
|
||||
//-------------------------
|
||||
|
||||
void serialize(SerialPacket* packet, void* buffer) {
|
||||
switch(packet->meta.type) {
|
||||
//No extra data
|
||||
case SerialPacket::Type::NONE:
|
||||
case SerialPacket::Type::PING:
|
||||
case SerialPacket::Type::PONG:
|
||||
case SerialPacket::Type::BROADCAST_REQUEST:
|
||||
serializeType(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//Server info
|
||||
case SerialPacket::Type::BROADCAST_RESPONSE:
|
||||
serializeServer(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//Client info
|
||||
case SerialPacket::Type::JOIN_REQUEST:
|
||||
case SerialPacket::Type::JOIN_RESPONSE:
|
||||
case SerialPacket::Type::SYNCHRONIZE:
|
||||
case SerialPacket::Type::DISCONNECT:
|
||||
case SerialPacket::Type::SHUTDOWN:
|
||||
serializeClient(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//region info
|
||||
case SerialPacket::Type::REGION_REQUEST:
|
||||
serializeRegionFormat(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
case SerialPacket::Type::REGION_CONTENT:
|
||||
serializeRegionContent(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//Character info
|
||||
case SerialPacket::Type::CHARACTER_NEW:
|
||||
case SerialPacket::Type::CHARACTER_DELETE:
|
||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
||||
serializeCharacter(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void deserialize(SerialPacket* packet, void* buffer) {
|
||||
//find the type, so that you can actually deserialize the packet!
|
||||
deserializeType(packet, reinterpret_cast<char*>(buffer));
|
||||
switch(packet->meta.type) {
|
||||
//No extra data
|
||||
case SerialPacket::Type::NONE:
|
||||
case SerialPacket::Type::PING:
|
||||
case SerialPacket::Type::PONG:
|
||||
case SerialPacket::Type::BROADCAST_REQUEST:
|
||||
//NOTHING
|
||||
break;
|
||||
|
||||
//Server info
|
||||
case SerialPacket::Type::BROADCAST_RESPONSE:
|
||||
deserializeServer(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//Client info
|
||||
case SerialPacket::Type::JOIN_REQUEST:
|
||||
case SerialPacket::Type::JOIN_RESPONSE:
|
||||
case SerialPacket::Type::SYNCHRONIZE:
|
||||
case SerialPacket::Type::DISCONNECT:
|
||||
case SerialPacket::Type::SHUTDOWN:
|
||||
deserializeClient(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//region info
|
||||
case SerialPacket::Type::REGION_REQUEST:
|
||||
deserializeRegionFormat(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
case SerialPacket::Type::REGION_CONTENT:
|
||||
deserializeRegionContent(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
|
||||
//Character info
|
||||
case SerialPacket::Type::CHARACTER_NEW:
|
||||
case SerialPacket::Type::CHARACTER_DELETE:
|
||||
case SerialPacket::Type::CHARACTER_UPDATE:
|
||||
deserializeCharacter(packet, reinterpret_cast<char*>(buffer));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef SERIAL_HPP_
|
||||
#define SERIAL_HPP_
|
||||
|
||||
#include "serial_packet.hpp"
|
||||
|
||||
/* NOTE: Keep the PACKET_BUFFER_SIZE up to date
|
||||
* NOTE: REGION_CONTENT is currently the largest type of packet
|
||||
* map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t)
|
||||
* map format: sizeof(int) * 3
|
||||
* metadata: sizeof(metadata)
|
||||
*/
|
||||
#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Metadata)
|
||||
|
||||
void serialize(SerialPacket* const, void*);
|
||||
void deserialize(SerialPacket* const, void*);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,127 @@
|
||||
/* 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 SERIALPACKET_HPP_
|
||||
#define SERIALPACKET_HPP_
|
||||
|
||||
#include "vector2.hpp"
|
||||
#include "region.hpp"
|
||||
|
||||
#include "SDL/SDL_net.h"
|
||||
|
||||
#define NETWORK_VERSION 20140428
|
||||
#define PACKET_STRING_SIZE 100
|
||||
|
||||
#pragma pack(push, 0)
|
||||
|
||||
union SerialPacket {
|
||||
//types of packets
|
||||
enum class Type {
|
||||
//default: there is something wrong
|
||||
NONE = 0,
|
||||
|
||||
//not used
|
||||
PING = 1,
|
||||
PONG = 2,
|
||||
|
||||
//Searching for a server to join
|
||||
BROADCAST_REQUEST = 3,
|
||||
BROADCAST_RESPONSE = 4,
|
||||
|
||||
//try to join the server
|
||||
JOIN_REQUEST = 5,
|
||||
JOIN_RESPONSE = 6,
|
||||
|
||||
//mass update
|
||||
SYNCHRONIZE = 7,
|
||||
|
||||
//disconnect from the server
|
||||
DISCONNECT = 8,
|
||||
|
||||
//shut down the server
|
||||
SHUTDOWN = 9,
|
||||
|
||||
//map data
|
||||
REGION_REQUEST = 10,
|
||||
REGION_CONTENT = 11,
|
||||
|
||||
//Character movement, etc.
|
||||
CHARACTER_NEW = 12,
|
||||
CHARACTER_DELETE = 13,
|
||||
CHARACTER_UPDATE = 14,
|
||||
|
||||
//TODO: combat packets
|
||||
};
|
||||
|
||||
//metadata on the packet itself
|
||||
struct Metadata {
|
||||
Type type;
|
||||
IPaddress srcAddress;
|
||||
}meta;
|
||||
|
||||
//information about the server
|
||||
struct ServerInformation {
|
||||
Metadata meta;
|
||||
int networkVersion;
|
||||
char name[PACKET_STRING_SIZE];
|
||||
int playerCount;
|
||||
}serverInfo;
|
||||
|
||||
//information about the client
|
||||
struct ClientInformation {
|
||||
Metadata meta;
|
||||
int clientIndex;
|
||||
int characterIndex;
|
||||
char username[PACKET_STRING_SIZE];
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
}clientInfo;
|
||||
|
||||
//map data
|
||||
struct RegionInformation {
|
||||
Metadata meta;
|
||||
int mapIndex;
|
||||
int x, y;
|
||||
Region* region;
|
||||
}regionInfo;
|
||||
|
||||
//information about a character
|
||||
struct CharacterInformation {
|
||||
Metadata meta;
|
||||
int clientIndex;
|
||||
int characterIndex;
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
int mapIndex;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
}characterInfo;
|
||||
|
||||
//defaults
|
||||
SerialPacket() {
|
||||
meta.type = Type::NONE;
|
||||
meta.srcAddress = {0,0};
|
||||
}
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#endif
|
||||
@@ -22,7 +22,7 @@
|
||||
#ifndef UDPNETWORKUTILITY_HPP_
|
||||
#define UDPNETWORKUTILITY_HPP_
|
||||
|
||||
#include "SDL_net/SDL_net.h"
|
||||
#include "SDL/SDL_net.h"
|
||||
|
||||
class UDPNetworkUtility {
|
||||
public:
|
||||
@@ -32,17 +32,9 @@ public:
|
||||
void Open(int port, int packSize);
|
||||
void Close();
|
||||
|
||||
//bind to an available channel
|
||||
int Bind(const char* ip, int port) {
|
||||
Bind(ip, port, -1);
|
||||
}
|
||||
int Bind(IPaddress* add) {
|
||||
Bind(add, -1);
|
||||
}
|
||||
|
||||
//bind to certain channel
|
||||
int Bind(const char* ip, int port, int channel);
|
||||
int Bind(IPaddress* add, int channel);
|
||||
//bind to a channel
|
||||
int Bind(const char* ip, int port, int channel = -1);
|
||||
int Bind(IPaddress* add, int channel = -1);
|
||||
void Unbind(int channel);
|
||||
|
||||
IPaddress* GetIPAddress(int channel) {
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $
|
||||
** Initialization of libraries for lua.c and other clients
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
||||
/* Modified for use in Tortuga, renamed to linit.cpp
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
** If you embed Lua in your program and need to open the standard
|
||||
** libraries, call luaL_openlibs in your program. If you need a
|
||||
** different set of libraries, copy this file to your project and edit
|
||||
** it to suit your needs.
|
||||
*/
|
||||
|
||||
|
||||
#define linit_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua/lua.hpp"
|
||||
#include "map_api.hpp"
|
||||
|
||||
|
||||
/*
|
||||
** these libs are loaded by lua.c and are readily available to any Lua
|
||||
** program
|
||||
*/
|
||||
static const luaL_Reg loadedlibs[] = {
|
||||
/* Standard libs */
|
||||
{"_G", luaopen_base},
|
||||
{LUA_LOADLIBNAME, luaopen_package},
|
||||
{LUA_COLIBNAME, luaopen_coroutine},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
{LUA_IOLIBNAME, luaopen_io},
|
||||
{LUA_OSLIBNAME, luaopen_os},
|
||||
{LUA_STRLIBNAME, luaopen_string},
|
||||
{LUA_BITLIBNAME, luaopen_bit32},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
|
||||
/* custom libs */
|
||||
{LUA_MAPLIBNAME, luaopen_mapapi},
|
||||
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
** these libs are preloaded and must be required before used
|
||||
*/
|
||||
static const luaL_Reg preloadedlibs[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||
const luaL_Reg *lib;
|
||||
/* call open functions from 'loadedlibs' and set results to global table */
|
||||
for (lib = loadedlibs; lib->func; lib++) {
|
||||
luaL_requiref(L, lib->name, lib->func, 1);
|
||||
lua_pop(L, 1); /* remove lib */
|
||||
}
|
||||
/* add open functions from 'preloadedlibs' into 'package.preload' table */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
for (lib = preloadedlibs; lib->func; lib++) {
|
||||
lua_pushcfunction(L, lib->func);
|
||||
lua_setfield(L, -2, lib->name);
|
||||
}
|
||||
lua_pop(L, 1); /* remove _PRELOAD table */
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#config
|
||||
INCLUDES+=. .. ../map
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -0,0 +1,126 @@
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "map_api.hpp"
|
||||
|
||||
//map headers
|
||||
#include "map_allocator.hpp"
|
||||
#include "map_file_format.hpp"
|
||||
#include "region_pager.hpp"
|
||||
|
||||
//NOTE: When operating on a region, setTile() & getTile() *are not* zero indexed, but when operating on the entire map they *are* zero indexed.
|
||||
|
||||
static int setTile(lua_State* L) {
|
||||
if (lua_gettop(L) == 5) {
|
||||
//operating on a region
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
ptr->SetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1, lua_tointeger(L, 5));
|
||||
}
|
||||
else {
|
||||
//operating on the whole map
|
||||
lua_pushstring(L, "pager");
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
//assume the pager is using lua
|
||||
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||
|
||||
//balance the stack
|
||||
lua_pop(L, 1);
|
||||
|
||||
pager->SetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getTile(lua_State* L) {
|
||||
if (lua_gettop(L) == 4) {
|
||||
//operating on a region
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
int ret = ptr->GetTile(lua_tointeger(L, 2)-1, lua_tointeger(L, 3)-1, lua_tointeger(L, 4)-1);
|
||||
lua_pushnumber(L, ret);
|
||||
}
|
||||
else {
|
||||
//operating on the whole map
|
||||
lua_pushstring(L, "pager");
|
||||
lua_gettable(L, LUA_REGISTRYINDEX);
|
||||
|
||||
//assume the pager is using lua
|
||||
RegionPager<LuaAllocator, LuaFormat>* pager = reinterpret_cast<RegionPager<LuaAllocator, LuaFormat>*>(lua_touserdata(L, -1));
|
||||
|
||||
//balance the stack
|
||||
lua_pop(L, 1);
|
||||
|
||||
int ret = pager->GetTile(lua_tointeger(L, 1), lua_tointeger(L, 2), lua_tointeger(L, 3));
|
||||
lua_pushnumber(L, ret);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getX(lua_State* L) {
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
lua_pushinteger(L, ptr->GetX());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getY(lua_State* L) {
|
||||
Region* ptr = (Region*)lua_touserdata(L, 1);
|
||||
lua_pushinteger(L, ptr->GetY());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getRegionWidth(lua_State* L) {
|
||||
lua_pushinteger(L, REGION_WIDTH);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getRegionHeight(lua_State* L) {
|
||||
lua_pushinteger(L, REGION_HEIGHT);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int getRegionDepth(lua_State* L) {
|
||||
lua_pushinteger(L, REGION_DEPTH);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int dummy(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const luaL_Reg regionlib[] = {
|
||||
{"create", dummy},
|
||||
{"unload", dummy},
|
||||
{"load", dummy},
|
||||
{"save", dummy},
|
||||
{"settile",setTile},
|
||||
{"gettile",getTile},
|
||||
{"getx",getX},
|
||||
{"gety",getY},
|
||||
{"getregionwidth",getRegionWidth},
|
||||
{"getregionheight",getRegionHeight},
|
||||
{"getregiondepth",getRegionDepth},
|
||||
{nullptr, nullptr}
|
||||
};
|
||||
|
||||
LUAMOD_API int luaopen_mapapi(lua_State* L) {
|
||||
luaL_newlib(L, regionlib);
|
||||
return 1;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
/* Copyright: (c) Kayne Ruse 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
@@ -19,8 +19,12 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "frame_rate.hpp"
|
||||
#ifndef MAPAPI_HPP_
|
||||
#define MAPAPI_HPP_
|
||||
|
||||
int FrameRate::frameCount = 0;
|
||||
int FrameRate::lastFrameRate = 0;
|
||||
FrameRate::Clock::time_point FrameRate::tick = FrameRate::Clock::now();
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
#define LUA_MAPLIBNAME "map"
|
||||
LUAMOD_API int luaopen_mapapi(lua_State* L);
|
||||
|
||||
#endif
|
||||
@@ -23,27 +23,8 @@
|
||||
|
||||
#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;
|
||||
return CalcState(motion.x, motion.y, motion.state & SDL_BUTTON_LMASK);
|
||||
}
|
||||
|
||||
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
@@ -61,41 +42,40 @@ Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
}
|
||||
|
||||
void Button::DrawTo(SDL_Surface* const dest) {
|
||||
image.DrawTo(dest, x, y);
|
||||
font.DrawStringTo(text, dest, textX + x, textY + y);
|
||||
if (!image || !font) {
|
||||
throw(std::runtime_error("Surface not set for Button"));
|
||||
}
|
||||
image->SetClipY(state * image->GetClipH());
|
||||
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);
|
||||
std::string Button::SetText(std::string t) {
|
||||
if (!image || !font) {
|
||||
throw(std::runtime_error("Surface not set for Button"));
|
||||
}
|
||||
//one line, cache the position
|
||||
text = t;
|
||||
textX = (image->GetClipW() / 2) - (font->GetCharW() * text.size() / 2);
|
||||
textY = (image->GetClipH() / 2) - (font->GetCharH() / 2);
|
||||
return text;
|
||||
}
|
||||
|
||||
Button::State Button::CalcState(Sint16 i, Sint16 j, bool leftPressed) {
|
||||
if (i < x || i > (x + image.GetClipW()) ||
|
||||
j < y || j > (y + image.GetClipH())
|
||||
if (!image || !font) {
|
||||
throw(std::runtime_error("Surface not set for Button"));
|
||||
}
|
||||
//if out of bounds
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -27,54 +27,66 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
//3-phases, no toggle, centred text
|
||||
/* 3-phases, no toggle, centred text
|
||||
* This class uses the size of the provided image as its bounds. Also,
|
||||
* The provided image should be formatted correctly.
|
||||
*
|
||||
* The button's image should be divided into 3 sections virtucally,
|
||||
* which act as the different button images. The clip width & height of the
|
||||
* Image should be set manually, and the height should be 1/3 of the total
|
||||
* graphical data.
|
||||
*/
|
||||
class Button {
|
||||
public:
|
||||
enum class State {
|
||||
NORMAL, HOVER, PRESSED
|
||||
enum State {
|
||||
NORMAL = 0, HOVER = 1, PRESSED = 2
|
||||
};
|
||||
|
||||
Button() = default;
|
||||
Button(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = "");
|
||||
~Button() = default;
|
||||
|
||||
void Setup(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = "");
|
||||
|
||||
//return the current state
|
||||
//handle input
|
||||
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
|
||||
//accessors and mutators
|
||||
Image* SetImage(Image* const ptr) { return image = ptr; }
|
||||
Image* GetImage() { return image; }
|
||||
RasterFont* SetFont(RasterFont* const ptr) { return font = ptr; }
|
||||
RasterFont* GetFont() { return font; }
|
||||
|
||||
Sint16 SetX(Sint16 i) { return x = i; }
|
||||
Sint16 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 SetTextX(Sint16 i) { return textX = i; }
|
||||
Sint16 SetTextY(Sint16 i) { return textY = i; }
|
||||
Sint16 GetTextX() const { return textX; }
|
||||
Sint16 GetTextY() const { return textY; }
|
||||
|
||||
State SetState(State s) { return state = s; }
|
||||
State GetState() const { return state; }
|
||||
|
||||
std::string SetText(std::string);
|
||||
std::string GetText() const { return text; }
|
||||
|
||||
private:
|
||||
State CalcState(Sint16 x, Sint16 y, bool leftPressed);
|
||||
|
||||
//point to the provided external objects
|
||||
Image* image = nullptr;
|
||||
RasterFont* font = nullptr;
|
||||
|
||||
//positions
|
||||
Sint16 x = 0, y = 0;
|
||||
Sint16 textX = 0, textY = 0; //prevent recalc every loop
|
||||
Image image;
|
||||
RasterFont font;
|
||||
Sint16 textX = 0, textY = 0;
|
||||
|
||||
//
|
||||
State state = State::NORMAL;
|
||||
std::string text;
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
#config
|
||||
INCLUDES+=. .. ../graphics
|
||||
LIBS+=
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../..
|
||||
OUT=$(addprefix $(OUTDIR)/,libcommon.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -0,0 +1,139 @@
|
||||
/* 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 "menu_bar.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
void MenuBar::DrawTo(SDL_Surface* const dest) {
|
||||
for (auto& i : entries) {
|
||||
i.DrawTo(dest);
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
for (auto& i : entries) {
|
||||
i.MouseMotion(motion);
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
for (auto& i : entries) {
|
||||
i.MouseButtonDown(button);
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::MouseButtonUp(SDL_MouseButtonEvent const& button, int* entry, int* butt) {
|
||||
*entry = *butt = -1;
|
||||
int ret = -1;
|
||||
for (auto& i : entries) {
|
||||
ret = i.MouseButtonUp(button);
|
||||
|
||||
if (ret != -1) {
|
||||
*entry = (&i - entries.data());
|
||||
*butt = ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::SetEntries(std::vector<std::vector<std::string>> info) {
|
||||
if (!image || !font) {
|
||||
throw(std::runtime_error("Surfaces not loaded into the menu bar"));
|
||||
}
|
||||
|
||||
entries.clear();
|
||||
for (int i = 0; i < info.size(); i++) {
|
||||
//create the entry & the main button
|
||||
entries.push_back(MenuBarEntry());
|
||||
entries[i].mainButton.SetImage(image);
|
||||
entries[i].mainButton.SetFont(font);
|
||||
entries[i].mainButton.SetText(info[i][0]);
|
||||
entries[i].mainButton.SetX(i * image->GetClipW());
|
||||
entries[i].mainButton.SetY(0);
|
||||
for (int j = 0; j < info[i].size()-1; j++) {
|
||||
//create each drop button in this entry
|
||||
entries[i].dropButtons.push_back(Button());
|
||||
entries[i].dropButtons[j].SetImage(image);
|
||||
entries[i].dropButtons[j].SetFont(font);
|
||||
entries[i].dropButtons[j].SetText(info[i][j+1]);
|
||||
entries[i].dropButtons[j].SetX(i * image->GetClipW());
|
||||
entries[i].dropButtons[j].SetY((j+1) * image->GetClipH());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::MenuBarEntry::DrawTo(SDL_Surface* const dest) {
|
||||
//only draw the dropButtons in the user has this menu open
|
||||
mainButton.DrawTo(dest);
|
||||
|
||||
if (!open) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& i : dropButtons) {
|
||||
i.DrawTo(dest);
|
||||
}
|
||||
}
|
||||
|
||||
void MenuBar::MenuBarEntry::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//open the menu
|
||||
bool o = mainButton.MouseMotion(motion) == Button::State::PRESSED;
|
||||
|
||||
if (!(open |= o)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& i : dropButtons) {
|
||||
//dragging down the menu
|
||||
o |= i.MouseMotion(motion) == Button::State::PRESSED;
|
||||
}
|
||||
|
||||
open = o;
|
||||
}
|
||||
|
||||
void MenuBar::MenuBarEntry::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//open the menu
|
||||
if (!(open = mainButton.MouseButtonDown(button) == Button::State::PRESSED)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//update the others anyway
|
||||
for (auto& i : dropButtons) {
|
||||
i.MouseButtonDown(button);
|
||||
}
|
||||
}
|
||||
|
||||
int MenuBar::MenuBarEntry::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
int ret = -1;
|
||||
mainButton.MouseButtonUp(button);
|
||||
|
||||
for (auto& i : dropButtons) {
|
||||
//the user just released this button
|
||||
if (i.GetState() != i.MouseButtonUp(button) && i.GetState() == Button::State::HOVER && open) {
|
||||
//get this button's index
|
||||
ret = (&i - dropButtons.data());
|
||||
}
|
||||
}
|
||||
|
||||
open = false;
|
||||
return ret;
|
||||
}
|
||||
@@ -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.
|
||||
*/
|
||||
#ifndef MENUBAR_HPP_
|
||||
#define MENUBAR_HPP_
|
||||
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/* I've redesigned this so that the contents of the menu bar can't change during run time.
|
||||
* This is more restrictive but I'm focusing on getting this working first.
|
||||
* The Image and Font pointers must be set before the text data is entered.
|
||||
*
|
||||
* This class needs a rewrite.
|
||||
*/
|
||||
|
||||
class MenuBar {
|
||||
public:
|
||||
MenuBar() = default;
|
||||
~MenuBar() = default;
|
||||
|
||||
//yet another draw function
|
||||
void DrawTo(SDL_Surface* const dest);
|
||||
|
||||
//user inputs
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&, int* entry, int* button);
|
||||
|
||||
//manage the entries & buttons
|
||||
void SetEntries(std::vector<std::vector<std::string>> info);
|
||||
void ClearEntries() { entries.clear(); }
|
||||
|
||||
//Accessors and mutators
|
||||
Image* SetImage(Image* const ptr) { return image = ptr; }
|
||||
Image* GetImage() { return image; }
|
||||
RasterFont* SetFont(RasterFont* const ptr) { return font = ptr; }
|
||||
RasterFont* GetFont() { return font; }
|
||||
|
||||
private:
|
||||
class MenuBarEntry;
|
||||
|
||||
std::vector<MenuBarEntry> entries;
|
||||
|
||||
Image* image = nullptr;
|
||||
RasterFont* font = nullptr;
|
||||
};
|
||||
|
||||
class MenuBar::MenuBarEntry {
|
||||
public:
|
||||
MenuBarEntry() = default;
|
||||
~MenuBarEntry() = default;
|
||||
|
||||
void DrawTo(SDL_Surface* const dest);
|
||||
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
int MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
|
||||
private:
|
||||
Button mainButton;
|
||||
|
||||
std::vector<Button> dropButtons;
|
||||
bool open = false;
|
||||
|
||||
friend class MenuBar;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -23,9 +23,9 @@
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
RasterFont::RasterFont(SDL_Surface* p) {
|
||||
SetSurface(p);
|
||||
}
|
||||
/* It might be more efficient to render to a different surface (like an Image)
|
||||
* rather than calling this function with all of it's '%' and '/'.
|
||||
*/
|
||||
|
||||
void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x, Sint16 y) {
|
||||
if (!image.GetSurface()) {
|
||||
@@ -34,16 +34,27 @@ void RasterFont::DrawStringTo(std::string s, SDL_Surface* const dest, Sint16 x,
|
||||
const Uint16 w = image.GetClipW();
|
||||
const Uint16 h = image.GetClipH();
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
image.SetClipX(s[i] % w * w);
|
||||
image.SetClipY(s[i] / h * h);
|
||||
image.SetClipX(s[i] % 16 * w);
|
||||
image.SetClipY(s[i] / 16 * h);
|
||||
image.DrawTo(dest, x + i * w, y);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) {
|
||||
if (image.SetSurface(p)) {
|
||||
image.SetClipW(image.GetSurface()->w/16);
|
||||
image.SetClipH(image.GetSurface()->h/16);
|
||||
}
|
||||
/* Note: This class can only take a raster font with 16*16 characters, and the
|
||||
* indevidual characters must have the same dimensions. Overall this class is too
|
||||
* restrictive; I suggest using a 3rd party library.
|
||||
*/
|
||||
|
||||
SDL_Surface* RasterFont::LoadSurface(std::string fname) {
|
||||
image.LoadSurface(fname);
|
||||
image.SetClipW(image.GetSurface()->w/16);
|
||||
image.SetClipH(image.GetSurface()->h/16);
|
||||
return image.GetSurface();
|
||||
}
|
||||
|
||||
SDL_Surface* RasterFont::SetSurface(SDL_Surface* p) {
|
||||
image.SetSurface(p);
|
||||
image.SetClipW(image.GetSurface()->w/16);
|
||||
image.SetClipH(image.GetSurface()->h/16);
|
||||
return image.GetSurface();
|
||||
}
|
||||
@@ -24,21 +24,29 @@
|
||||
|
||||
#include "image.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
class RasterFont {
|
||||
public:
|
||||
RasterFont() = default;
|
||||
RasterFont(SDL_Surface* p);
|
||||
RasterFont(RasterFont const& rhs) { *this = rhs; }
|
||||
RasterFont(RasterFont&& rhs) { *this = std::move(rhs); }
|
||||
RasterFont(std::string fname) { LoadSurface(fname); }
|
||||
RasterFont(SDL_Surface* p) { SetSurface(p); }
|
||||
~RasterFont() = default;
|
||||
|
||||
RasterFont& operator=(RasterFont const& rhs) { image = rhs.image; }
|
||||
RasterFont& operator=(RasterFont&& rhs) { image = std::move(rhs.image); }
|
||||
|
||||
void DrawStringTo(std::string, SDL_Surface* const, Sint16 x, Sint16 y);
|
||||
|
||||
//Accessors and Mutators
|
||||
SDL_Surface* LoadSurface(std::string);
|
||||
SDL_Surface* SetSurface(SDL_Surface*);
|
||||
SDL_Surface* GetSurface() const { return image.GetSurface(); }
|
||||
Uint16 GetCharW() { return image.GetClipW(); }
|
||||
Uint16 GetCharH() { return image.GetClipH(); }
|
||||
void FreeSurface() { image.FreeSurface(); }
|
||||
|
||||
Uint16 GetCharW() const { return image.GetClipW(); }
|
||||
Uint16 GetCharH() const { return image.GetClipH(); }
|
||||
|
||||
private:
|
||||
Image image;
|
||||
};
|
||||
@@ -19,36 +19,41 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "image.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
Image::Image(SDL_Surface* p) {
|
||||
SetSurface(p);
|
||||
}
|
||||
#include <algorithm>
|
||||
|
||||
Image::Image(SDL_Surface* p, SDL_Rect r) {
|
||||
SetSurface(p, r);
|
||||
}
|
||||
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* p) {
|
||||
if (!(surface = p)) {
|
||||
clip = {0, 0, 0, 0};
|
||||
int snapToBase(int base, int x) {
|
||||
//snap to a grid
|
||||
if (x < 0) {
|
||||
x++;
|
||||
return x / base * base - base;
|
||||
}
|
||||
else {
|
||||
clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h};
|
||||
}
|
||||
return surface;
|
||||
return x / base * base;
|
||||
}
|
||||
|
||||
SDL_Surface* Image::SetSurface(SDL_Surface* const p, SDL_Rect r) {
|
||||
surface = p;
|
||||
clip = r;
|
||||
return surface;
|
||||
std::string truncatePath(std::string pathname) {
|
||||
return std::string(
|
||||
std::find_if(
|
||||
pathname.rbegin(),
|
||||
pathname.rend(),
|
||||
[](char ch) -> bool {
|
||||
//windows only
|
||||
return ch == '/' || ch == '\\';
|
||||
// //unix only
|
||||
// return ch == '/';
|
||||
}).base(),
|
||||
pathname.end());
|
||||
}
|
||||
|
||||
void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) {
|
||||
if (!surface) {
|
||||
return;
|
||||
}
|
||||
SDL_Rect sclip = clip, dclip = {x,y};
|
||||
SDL_BlitSurface(surface, &sclip, dest, &dclip);
|
||||
std::string to_string_custom(int i) {
|
||||
char buffer[20];
|
||||
snprintf(buffer, 20, "%d", i);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
int to_integer_custom(std::string s) {
|
||||
int ret = 0;
|
||||
sscanf(s.c_str(), "%d", &ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/* 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 UTILITY_HPP_
|
||||
#define UTILITY_HPP_
|
||||
|
||||
#include <string>
|
||||
|
||||
int snapToBase(int base, int x);
|
||||
std::string truncatePath(std::string pathname);
|
||||
|
||||
//fixing known bugs in g++
|
||||
std::string to_string_custom(int i);
|
||||
|
||||
int to_integer_custom(std::string);
|
||||
|
||||
//wow
|
||||
template<typename ContainerT, typename PredicateT>
|
||||
void erase_if(ContainerT& items, const PredicateT& predicate) {
|
||||
for(auto it = items.begin(); it != items.end(); /* empty */) {
|
||||
if(predicate(*it)) {
|
||||
it = items.erase(it);
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -22,16 +22,19 @@
|
||||
#ifndef VECTOR2_HPP_
|
||||
#define VECTOR2_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
|
||||
class Vector2 {
|
||||
public:
|
||||
double x = 0, y = 0;
|
||||
double x, y;
|
||||
|
||||
Vector2() = default;
|
||||
Vector2(double i, double j) {
|
||||
x = i; y = j;
|
||||
}
|
||||
Vector2(double i, double j): x(i), y(j) {};
|
||||
~Vector2() = default;
|
||||
Vector2& operator=(Vector2 const&) = default;
|
||||
|
||||
double Length() const {
|
||||
return sqrt(x*x+y*y);
|
||||
}
|
||||
@@ -41,31 +44,57 @@ public:
|
||||
|
||||
double operator[](size_t i) {
|
||||
if (i >= 2)
|
||||
throw(std::runtime_error("Out of range"));
|
||||
throw(std::domain_error("Out of range"));
|
||||
return *(&x+i);
|
||||
}
|
||||
|
||||
//Arithmetic operators
|
||||
Vector2 operator+(Vector2 v) const { return Vector2(x + v.x, y + v.y); }
|
||||
Vector2 operator-(Vector2 v) const { return Vector2(x - v.x, y - v.y); }
|
||||
Vector2 operator*(Vector2 v) const { return Vector2(x * v.x, y * v.y); }
|
||||
Vector2 operator*(double d) const { return Vector2(x * d, y * d); }
|
||||
Vector2 operator+(Vector2 v) const {
|
||||
Vector2 ret;
|
||||
ret.x = x + v.x;
|
||||
ret.y = y + v.y;
|
||||
return ret;
|
||||
}
|
||||
Vector2 operator-(Vector2 v) const {
|
||||
Vector2 ret;
|
||||
ret.x = x - v.x;
|
||||
ret.y = y - v.y;
|
||||
return ret;
|
||||
}
|
||||
Vector2 operator*(Vector2 v) const {
|
||||
Vector2 ret;
|
||||
ret.x = x * v.x;
|
||||
ret.y = y * v.y;
|
||||
return ret;
|
||||
}
|
||||
Vector2 operator*(double d) const {
|
||||
Vector2 ret;
|
||||
ret.x = x * d;
|
||||
ret.y = y * d;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vector2 operator/(Vector2 v) {
|
||||
if (!v.x || !v.y)
|
||||
throw(std::runtime_error("Divide by zero"));
|
||||
return Vector2(x / v.x, y / v.y);
|
||||
throw(std::domain_error("Divide by zero"));
|
||||
Vector2 ret;
|
||||
ret.x = x / v.x;
|
||||
ret.y = y / v.y;
|
||||
return ret;
|
||||
}
|
||||
Vector2 operator/(double d) {
|
||||
if (!d)
|
||||
throw(std::runtime_error("Divide by zero"));
|
||||
return Vector2(x / d, y / d);
|
||||
throw(std::domain_error("Divide by zero"));
|
||||
Vector2 ret;
|
||||
ret.x = x / d;
|
||||
ret.y = y / d;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool operator==(Vector2 v) { return (x == v.x && y == v.y); }
|
||||
bool operator!=(Vector2 v) { return (x != v.x || y != v.y); }
|
||||
|
||||
//templates
|
||||
//member templates (curry the above operators)
|
||||
template<typename T> Vector2 operator+=(T t) { return *this = *this + t; }
|
||||
template<typename T> Vector2 operator-=(T t) { return *this = *this - t; }
|
||||
template<typename T> Vector2 operator*=(T t) { return *this = *this * t; }
|
||||
@@ -74,4 +103,16 @@ public:
|
||||
template<typename T> bool operator!=(T t) { return (x != t || y != t); }
|
||||
};
|
||||
|
||||
//non-member templates (flip the order)
|
||||
template<typename T> Vector2 operator+(T t, Vector2 v) { return v + t; }
|
||||
template<typename T> Vector2 operator-(T t, Vector2 v) { return v - t; }
|
||||
template<typename T> Vector2 operator*(T t, Vector2 v) { return v * t; }
|
||||
template<typename T> Vector2 operator/(T t, Vector2 v) { return v / t; }
|
||||
|
||||
template<typename T> bool operator==(T t, Vector2 v) { return v == t; }
|
||||
template<typename T> bool operator!=(T t, Vector2 v) { return v != t; }
|
||||
|
||||
//This is explicitly a POD
|
||||
static_assert(std::is_pod<Vector2>::value, "Vector2 is not a POD");
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,139 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Static declarations
|
||||
//-------------------------
|
||||
|
||||
SDL_Surface* BaseScene::screen = nullptr;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
BaseScene::BaseScene() {
|
||||
//
|
||||
}
|
||||
|
||||
BaseScene::~BaseScene() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Program control
|
||||
//-------------------------
|
||||
|
||||
SDL_Surface* BaseScene::SetScreen(int w, int h, int bpp, Uint32 flags) {
|
||||
if (!bpp) {
|
||||
bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
|
||||
}
|
||||
|
||||
screen = SDL_SetVideoMode(w, h, bpp, flags);
|
||||
|
||||
if (!screen) {
|
||||
throw(std::runtime_error("Failed to create the screen surface"));
|
||||
}
|
||||
|
||||
return screen;
|
||||
}
|
||||
|
||||
SDL_Surface* BaseScene::GetScreen() {
|
||||
return screen;
|
||||
}
|
||||
|
||||
SceneList BaseScene::SetNextScene(SceneList sceneIndex) {
|
||||
return nextScene = sceneIndex;
|
||||
}
|
||||
|
||||
SceneList BaseScene::GetNextScene() const {
|
||||
return nextScene;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void BaseScene::RunFrame(double delta) {
|
||||
FrameStart();
|
||||
HandleEvents();
|
||||
Update(delta);
|
||||
FrameEnd();
|
||||
}
|
||||
|
||||
void BaseScene::RenderFrame() {
|
||||
SDL_FillRect(screen, 0, 0);
|
||||
Render(screen);
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void BaseScene::HandleEvents() {
|
||||
SDL_Event event;
|
||||
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_QUIT:
|
||||
QuitEvent();
|
||||
break;
|
||||
|
||||
case SDL_VIDEORESIZE:
|
||||
SetScreen(event.resize.w, event.resize.h, 0, screen->flags);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEMOTION:
|
||||
MouseMotion(event.motion);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
MouseButtonDown(event.button);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
MouseButtonUp(event.button);
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
KeyDown(event.key);
|
||||
break;
|
||||
|
||||
case SDL_KEYUP:
|
||||
KeyUp(event.key);
|
||||
break;
|
||||
|
||||
#ifdef USE_EVENT_JOYSTICK
|
||||
//TODO: joystick/gamepad support
|
||||
#endif
|
||||
|
||||
#ifdef USE_EVENT_UNKNOWN
|
||||
default:
|
||||
UnknownEvent(event);
|
||||
break;
|
||||
#endif
|
||||
}//switch
|
||||
}//while
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef BASESCENE_HPP_
|
||||
#define BASESCENE_HPP_
|
||||
|
||||
#include "scene_list.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
BaseScene();
|
||||
virtual ~BaseScene();
|
||||
|
||||
//Program control
|
||||
static SDL_Surface* SetScreen(int w, int h, int bpp = 0, Uint32 flags = SDL_HWSURFACE|SDL_DOUBLEBUF);
|
||||
static SDL_Surface* GetScreen();
|
||||
|
||||
SceneList SetNextScene(SceneList sceneIndex);
|
||||
SceneList GetNextScene() const;
|
||||
|
||||
//Frame loop
|
||||
virtual void RunFrame(double delta);
|
||||
virtual void RenderFrame();
|
||||
|
||||
protected:
|
||||
virtual void FrameStart() {}
|
||||
virtual void HandleEvents();
|
||||
virtual void Update(double delta) {}
|
||||
virtual void FrameEnd() {}
|
||||
virtual void Render(SDL_Surface* const screen) {}
|
||||
|
||||
//Event handlers
|
||||
virtual void QuitEvent() { SetNextScene(SceneList::QUIT); }
|
||||
virtual void MouseMotion(SDL_MouseMotionEvent const&) {}
|
||||
virtual void MouseButtonDown(SDL_MouseButtonEvent const&) {}
|
||||
virtual void MouseButtonUp(SDL_MouseButtonEvent const&) {}
|
||||
virtual void KeyDown(SDL_KeyboardEvent const&) {}
|
||||
virtual void KeyUp(SDL_KeyboardEvent const&) {}
|
||||
|
||||
#ifdef USE_EVENT_JOYSTICK
|
||||
//TODO: joystick/gamepad support
|
||||
#endif
|
||||
|
||||
#ifdef USE_EVENT_UNKNOWN
|
||||
virtual void UnknownEvent(SDL_Event const&) {}
|
||||
#endif
|
||||
|
||||
private:
|
||||
static SDL_Surface* screen;
|
||||
SceneList nextScene = SceneList::CONTINUE;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial EditorApplications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "editor_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <chrono>
|
||||
|
||||
//-------------------------
|
||||
//Scene headers
|
||||
//-------------------------
|
||||
|
||||
//Add the custom scene headers here
|
||||
#include "editor_scene.hpp"
|
||||
#include "testificate_scene.hpp"
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
void EditorApplication::Init(int argc, char** argv) {
|
||||
config.Load("rsc\\config.cfg");
|
||||
if (SDL_Init(SDL_INIT_VIDEO))
|
||||
throw(std::runtime_error("Failed to initialize SDL"));
|
||||
|
||||
BaseScene::SetScreen(config.Int("screen.w"), config.Int("screen.h"));
|
||||
}
|
||||
|
||||
void EditorApplication::Proc() {
|
||||
LoadScene(SceneList::FIRST);
|
||||
|
||||
//prepare the time system
|
||||
typedef std::chrono::steady_clock Clock;
|
||||
|
||||
Clock::duration delta(16 * Clock::duration::period::den / std::milli::den);
|
||||
Clock::time_point simTime = Clock::now();
|
||||
Clock::time_point realTime;
|
||||
|
||||
//The main loop
|
||||
while(activeScene->GetNextScene() != SceneList::QUIT) {
|
||||
//switch scenes when necessary
|
||||
if (activeScene->GetNextScene() != SceneList::CONTINUE) {
|
||||
LoadScene(activeScene->GetNextScene());
|
||||
continue;
|
||||
}
|
||||
|
||||
//update the current time
|
||||
realTime = Clock::now();
|
||||
|
||||
//simulate game time
|
||||
while (simTime < realTime) {
|
||||
//call each user defined function
|
||||
activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den);
|
||||
simTime += delta;
|
||||
}
|
||||
|
||||
//draw the game to the screen
|
||||
activeScene->RenderFrame();
|
||||
|
||||
//give the computer a break
|
||||
SDL_Delay(10);
|
||||
}
|
||||
|
||||
UnloadScene();
|
||||
}
|
||||
|
||||
void EditorApplication::Quit() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Private access members
|
||||
//-------------------------
|
||||
|
||||
void EditorApplication::LoadScene(SceneList sceneIndex) {
|
||||
UnloadScene();
|
||||
|
||||
switch(sceneIndex) {
|
||||
//add scene creation calls here
|
||||
case SceneList::FIRST:
|
||||
case SceneList::EDITORSCENE:
|
||||
activeScene = new EditorScene(&config);
|
||||
break;
|
||||
|
||||
case SceneList::TESTIFICATESCENE:
|
||||
activeScene = new TestificateScene(&config);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
throw(std::logic_error("Failed to recognize the scene index"));
|
||||
}
|
||||
}
|
||||
|
||||
void EditorApplication::UnloadScene() {
|
||||
delete activeScene;
|
||||
activeScene = nullptr;
|
||||
}
|
||||
@@ -19,16 +19,31 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef INFORMATIONMANAGER_HPP_
|
||||
#define INFORMATIONMANAGER_HPP_
|
||||
#ifndef EDITORAPPLICATION_HPP_
|
||||
#define EDITORAPPLICATION_HPP_
|
||||
|
||||
class InformationManager {
|
||||
#include "scene_list.hpp"
|
||||
#include "base_scene.hpp"
|
||||
#include "config_utility.hpp"
|
||||
|
||||
class EditorApplication {
|
||||
public:
|
||||
int SetClientIndex(int i) { return clientIndex = i; }
|
||||
int GetClientIndex() { return clientIndex; }
|
||||
void ResetClientIndex() { clientIndex = -1; }
|
||||
EditorApplication() = default;
|
||||
~EditorApplication() = default;
|
||||
|
||||
void Init(int argc, char** argv);
|
||||
void Proc();
|
||||
void Quit();
|
||||
|
||||
private:
|
||||
int clientIndex = -1;
|
||||
//Private access members
|
||||
void LoadScene(SceneList sceneIndex);
|
||||
void UnloadScene();
|
||||
|
||||
//globals
|
||||
ConfigUtility config;
|
||||
|
||||
BaseScene* activeScene = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,216 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "editor_scene.hpp"
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
EditorScene::EditorScene(ConfigUtility* const arg1):
|
||||
config(*arg1)
|
||||
{
|
||||
//create the debugging "window"
|
||||
debugInfo.CreateSurface(256, 256);
|
||||
|
||||
//setup the utility objects
|
||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
||||
|
||||
buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
buttonImage.SetClipH(buttonImage.GetClipH()/3);
|
||||
|
||||
//setup the menu bar
|
||||
menuBar.SetFont(&font);
|
||||
menuBar.SetImage(&buttonImage);
|
||||
|
||||
menuBar.SetEntries({
|
||||
{"File", "New", "Open", "Save", "Close"},
|
||||
{"Edit", "Set Tile", "Set Brush", "Script"},
|
||||
{"Debug", "Debug On", "Debug Off", "Toggle", "Testificate"}
|
||||
});
|
||||
|
||||
//debug
|
||||
tsheet.Load(config["dir.tilesets"] + "terrain.bmp", 12, 15);
|
||||
for (int i = 0; i < REGION_WIDTH; i++) {
|
||||
for (int j = 0; j < REGION_HEIGHT; j++) {
|
||||
pager.SetTile(i, j, 0, 14);
|
||||
}
|
||||
}
|
||||
pager.SetTile(5, 10, 1, 48);
|
||||
}
|
||||
|
||||
EditorScene::~EditorScene() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void EditorScene::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void EditorScene::Update(double delta) {
|
||||
//
|
||||
}
|
||||
|
||||
void EditorScene::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void EditorScene::Render(SDL_Surface* const screen) {
|
||||
tsheet.DrawRegionTo(screen, pager.GetRegion(0, 0), camera.x, camera.y);
|
||||
|
||||
//draw a big bar across the top (hackish)
|
||||
buttonImage.SetClipY(0);
|
||||
for (int i = 0; i < screen->w; i += buttonImage.GetClipW()) {
|
||||
buttonImage.DrawTo(screen, i, 0);
|
||||
}
|
||||
|
||||
//draw the menu bar
|
||||
menuBar.DrawTo(screen);
|
||||
|
||||
//draw some debugging info
|
||||
if (debugOpen) {
|
||||
SDL_FillRect(debugInfo.GetSurface(), 0, 0);
|
||||
DrawToDebugInfo(string("camera.x: ") + to_string_custom(camera.x), 0);
|
||||
DrawToDebugInfo(string("camera.y: ") + to_string_custom(camera.y), 1);
|
||||
debugInfo.DrawTo(screen, screen->w - debugInfo.GetClipW(), buttonImage.GetClipH());
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::DrawToDebugInfo(std::string str, int line) {
|
||||
//draw the debug info on the right, with a grey background
|
||||
SDL_Rect clip = {
|
||||
Sint16(debugInfo.GetClipW() - str.size() * font.GetCharW()),
|
||||
Sint16(font.GetCharH() * line),
|
||||
Uint16(str.size() * font.GetCharW()),
|
||||
Uint16(font.GetCharH())
|
||||
};
|
||||
SDL_FillRect(debugInfo.GetSurface(), &clip, SDL_MapRGB(debugInfo.GetSurface()->format, 64, 64, 64));
|
||||
font.DrawStringTo(str, debugInfo.GetSurface(), clip.x, clip.y);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void EditorScene::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
menuBar.MouseMotion(motion);
|
||||
|
||||
if (motion.state & SDL_BUTTON_RMASK) {
|
||||
camera.x -= motion.xrel;
|
||||
camera.y -= motion.yrel;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
menuBar.MouseButtonDown(button);
|
||||
}
|
||||
|
||||
void EditorScene::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
int entry, drop;
|
||||
menuBar.MouseButtonUp(button, &entry, &drop);
|
||||
HandleMenuOption(entry, drop);
|
||||
}
|
||||
|
||||
void EditorScene::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
QuitEvent();
|
||||
break;
|
||||
case SDLK_SPACE:
|
||||
camera.x = 0;
|
||||
camera.y = 0;
|
||||
break;
|
||||
case SDLK_TAB:
|
||||
debugOpen = !debugOpen;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorScene::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Members
|
||||
//-------------------------
|
||||
|
||||
void EditorScene::HandleMenuOption(int entry, int drop) {
|
||||
//manage input from the menu bar
|
||||
switch(entry) {
|
||||
case 0: //File
|
||||
switch(drop) {
|
||||
case 0:
|
||||
//NEW
|
||||
break;
|
||||
case 1:
|
||||
//OPEN
|
||||
break;
|
||||
case 2:
|
||||
//SAVE
|
||||
break;
|
||||
case 3:
|
||||
//CLOSE
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 1: //Edit
|
||||
switch(drop) {
|
||||
case 0:
|
||||
//SET TILE
|
||||
break;
|
||||
case 1:
|
||||
//SET BRUSH
|
||||
break;
|
||||
case 2:
|
||||
//SCRIPT
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 2: //Debug
|
||||
switch(drop) {
|
||||
case 0:
|
||||
debugOpen = true;
|
||||
break;
|
||||
case 1:
|
||||
debugOpen = false;
|
||||
break;
|
||||
case 2:
|
||||
debugOpen = !debugOpen;
|
||||
break;
|
||||
case 3:
|
||||
SetNextScene(SceneList::TESTIFICATESCENE);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013, 2014
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef EDITORSCENE_HPP_
|
||||
#define EDITORSCENE_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "menu_bar.hpp"
|
||||
|
||||
#include "region_pager.hpp"
|
||||
#include "map_allocator.hpp"
|
||||
#include "map_file_format.hpp"
|
||||
#include "tile_sheet.hpp"
|
||||
|
||||
class EditorScene : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
EditorScene(ConfigUtility* const);
|
||||
~EditorScene();
|
||||
|
||||
protected:
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
//Event handlers
|
||||
void MouseMotion(SDL_MouseMotionEvent const&);
|
||||
void MouseButtonDown(SDL_MouseButtonEvent const&);
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//members
|
||||
void HandleMenuOption(int entry, int drop);
|
||||
|
||||
//globals
|
||||
ConfigUtility& config;
|
||||
|
||||
//debugging tools
|
||||
void DrawToDebugInfo(std::string, int line);
|
||||
Image debugInfo;
|
||||
bool debugOpen = true;
|
||||
|
||||
RasterFont font;
|
||||
Image buttonImage;
|
||||
MenuBar menuBar;
|
||||
|
||||
struct {
|
||||
int x = 0, y = 0;
|
||||
} camera;
|
||||
|
||||
RegionPager<BlankAllocator, DummyFormat> pager;
|
||||
TileSheet tsheet;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -19,24 +19,25 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef SERVICELOCATOR_HPP_
|
||||
#define SERVICELOCATOR_HPP_
|
||||
#include "editor_application.hpp"
|
||||
|
||||
template<typename T>
|
||||
class ServiceLocator {
|
||||
public:
|
||||
static T* Set(T* t) {
|
||||
delete service;
|
||||
return service = t;
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
cout << "Beginning editor" << endl;
|
||||
try {
|
||||
EditorApplication app;
|
||||
app.Init(argc, argv);
|
||||
app.Proc();
|
||||
app.Quit();
|
||||
}
|
||||
static T* Get() {
|
||||
return service;
|
||||
catch(exception& e) {
|
||||
cerr << "Fatal exception thrown: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
private:
|
||||
static T* service;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T* ServiceLocator<T>::service = nullptr;
|
||||
|
||||
#endif
|
||||
cout << "Clean exit" << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#config
|
||||
INCLUDES+=../common ../common/graphics ../common/map ../common/ui
|
||||
LIBS+=../libcommon.a -lmingw32 -lSDLmain -lSDL -llua
|
||||
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
CFLAGS+=-DDEBUG $(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
CXXSRC=$(wildcard *.cpp)
|
||||
CSRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
|
||||
OBJ+=$(addprefix $(OBJDIR)/,$(CSRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,editor)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
$(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -19,12 +19,18 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "defines.hpp"
|
||||
#ifndef SCENELIST_HPP_
|
||||
#define SCENELIST_HPP_
|
||||
|
||||
#include <cstdio>
|
||||
enum class SceneList {
|
||||
//these are reserved
|
||||
QUIT,
|
||||
CONTINUE,
|
||||
FIRST,
|
||||
|
||||
std::string itos(int i) {
|
||||
char buffer[20];
|
||||
snprintf(buffer, 20, "%d", i);
|
||||
return std::string(buffer);
|
||||
}
|
||||
//custom indexes
|
||||
TESTIFICATESCENE,
|
||||
EDITORSCENE,
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -19,59 +19,73 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#include "option_screen.hpp"
|
||||
#include "testificate_scene.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
OptionScreen::OptionScreen() {
|
||||
#ifdef DEBUG
|
||||
cout << "entering OptionScreen" << endl;
|
||||
#endif
|
||||
backButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back");
|
||||
TestificateScene::TestificateScene(ConfigUtility* const arg1):
|
||||
config(*arg1)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
OptionScreen::~OptionScreen() {
|
||||
#ifdef DEBUG
|
||||
cout << "leaving OptionScreen" << endl;
|
||||
#endif
|
||||
TestificateScene::~TestificateScene() {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Frame loop
|
||||
//-------------------------
|
||||
|
||||
void OptionScreen::Render(SDL_Surface* const screen) {
|
||||
backButton.DrawTo(screen);
|
||||
void TestificateScene::FrameStart() {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::Update(double delta) {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::FrameEnd() {
|
||||
//
|
||||
}
|
||||
|
||||
void TestificateScene::Render(SDL_Surface* const screen) {
|
||||
//
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
//Event handlers
|
||||
//-------------------------
|
||||
|
||||
void OptionScreen::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
backButton.MouseMotion(motion);
|
||||
void TestificateScene::MouseMotion(SDL_MouseMotionEvent const& motion) {
|
||||
//
|
||||
}
|
||||
|
||||
void OptionScreen::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
backButton.MouseButtonDown(button);
|
||||
|
||||
void TestificateScene::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void OptionScreen::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
}
|
||||
void TestificateScene::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
//
|
||||
}
|
||||
|
||||
void OptionScreen::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
void TestificateScene::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
break;
|
||||
// QuitEvent();
|
||||
SetNextScene(SceneList::EDITORSCENE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void TestificateScene::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
@@ -19,33 +19,35 @@
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef OPTIONSCREEN_HPP_
|
||||
#define OPTIONSCREEN_HPP_
|
||||
#ifndef TESTIFICATESCENE_HPP_
|
||||
#define TESTIFICATESCENE_HPP_
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "service_locator.hpp"
|
||||
|
||||
#include "surface_manager.hpp"
|
||||
#include "button.hpp"
|
||||
#include "config_utility.hpp"
|
||||
|
||||
class OptionScreen : public BaseScene {
|
||||
class TestificateScene : public BaseScene {
|
||||
public:
|
||||
/* Public access members */
|
||||
OptionScreen();
|
||||
~OptionScreen();
|
||||
//Public access members
|
||||
TestificateScene(ConfigUtility* const);
|
||||
~TestificateScene();
|
||||
|
||||
protected:
|
||||
/* Frame loop */
|
||||
//Frame loop
|
||||
void FrameStart();
|
||||
void Update(double delta);
|
||||
void FrameEnd();
|
||||
void Render(SDL_Surface* const);
|
||||
|
||||
/* Event handlers */
|
||||
//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&);
|
||||
|
||||
SurfaceManager* surfaceMgr = ServiceLocator<SurfaceManager>::Get();
|
||||
Button backButton;
|
||||
//globals
|
||||
ConfigUtility& config;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,66 +0,0 @@
|
||||
/* Copyright: (c) Kayne Ruse 2013
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
#ifndef SPRITESHEET_HPP_
|
||||
#define SPRITESHEET_HPP_
|
||||
|
||||
#include "image.hpp"
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
class SpriteSheet {
|
||||
public:
|
||||
SpriteSheet() = default;
|
||||
SpriteSheet(SDL_Surface*, Uint16 w, Uint16 h);
|
||||
~SpriteSheet() = default;
|
||||
|
||||
void Update(double delta);
|
||||
|
||||
SDL_Surface* SetSurface(SDL_Surface* const, Uint16 w, Uint16 h);
|
||||
SDL_Surface* GetSurface() const {
|
||||
return image.GetSurface();
|
||||
}
|
||||
|
||||
void DrawTo(SDL_Surface* const dest, Sint16 x, Sint16 y) {
|
||||
image.DrawTo(dest, x, y);
|
||||
}
|
||||
|
||||
//Accessors and Mutators
|
||||
double SetInterval(double i) { return interval = i; }
|
||||
double GetInterval() const { return interval; }
|
||||
|
||||
int SetCurrentFrame(int i) { return currentFrame = i; }
|
||||
int SetCurrentStrip(int i) { return currentStrip = i; }
|
||||
|
||||
Uint16 GetFrameWidth() const { return image.GetClipW(); }
|
||||
Uint16 GetFrameHeight() const { return image.GetClipH(); }
|
||||
int GetCurrentFrame() const { return currentFrame; };
|
||||
int GetCurrentStrip() const { return currentStrip; };
|
||||
int GetMaxFrames() const { return maxFrames; }
|
||||
int GetMaxStrips() const { return maxStrips; }
|
||||
private:
|
||||
Image image;
|
||||
int currentFrame = 0, maxFrames = 0;
|
||||
int currentStrip = 0, maxStrips = 0;
|
||||
double interval = 0, ticks = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,85 +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 "surface_manager.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
SurfaceManager::~SurfaceManager() noexcept {
|
||||
FreeAll();
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
throw(std::runtime_error(std::string("Surface already loaded: ") + key + std::string(", ") + fname));
|
||||
}
|
||||
return LoadSurface(key, fname);
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Reload(std::string key, std::string fname) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
SDL_FreeSurface(it->second);
|
||||
surfaceMap.erase(it);
|
||||
}
|
||||
return LoadSurface(key, fname);
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Get(std::string key) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it == surfaceMap.end()) {
|
||||
throw(std::runtime_error(std::string("Could not find key: ") + key));
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::Set(std::string key, SDL_Surface* ptr) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
throw(std::runtime_error(std::string("Key already exists: ") + key));
|
||||
}
|
||||
return surfaceMap[key] = ptr;
|
||||
}
|
||||
|
||||
void SurfaceManager::Free(std::string key) {
|
||||
MapType::iterator it = surfaceMap.find(key);
|
||||
if (it != surfaceMap.end()) {
|
||||
SDL_FreeSurface(it->second);
|
||||
surfaceMap.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void SurfaceManager::FreeAll() {
|
||||
for (auto it : surfaceMap) {
|
||||
SDL_FreeSurface(it.second);
|
||||
}
|
||||
surfaceMap.clear();
|
||||
}
|
||||
|
||||
SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) {
|
||||
SDL_Surface* ptr = SDL_LoadBMP(fname.c_str());
|
||||
if (!ptr) {
|
||||
throw(std::runtime_error(std::string("Failed to load file: ") + fname));
|
||||
}
|
||||
SDL_SetColorKey(ptr, SDL_SRCCOLORKEY, SDL_MapRGB(ptr->format, 255, 0, 255)); //default
|
||||
return surfaceMap[key] = ptr;
|
||||
}
|
||||
@@ -1,452 +0,0 @@
|
||||
/*
|
||||
SDL_net: An example cross-platform network library for use with SDL
|
||||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef _SDL_NET_H
|
||||
#define _SDL_NET_H
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
#include "SDL/SDL_endian.h"
|
||||
#include "SDL/SDL_version.h"
|
||||
#include "SDL/begin_code.h"
|
||||
|
||||
|
||||
|
||||
/* Set up for C function definitions, even when using C++ */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
|
||||
*/
|
||||
#define SDL_NET_MAJOR_VERSION 1
|
||||
#define SDL_NET_MINOR_VERSION 2
|
||||
#define SDL_NET_PATCHLEVEL 8
|
||||
|
||||
/* This macro can be used to fill a version structure with the compile-time
|
||||
* version of the SDL_net library.
|
||||
*/
|
||||
#define SDL_NET_VERSION(X) \
|
||||
{ \
|
||||
(X)->major = SDL_NET_MAJOR_VERSION; \
|
||||
(X)->minor = SDL_NET_MINOR_VERSION; \
|
||||
(X)->patch = SDL_NET_PATCHLEVEL; \
|
||||
}
|
||||
|
||||
/* This function gets the version of the dynamically linked SDL_net library.
|
||||
it should NOT be used to fill a version structure, instead you should
|
||||
use the SDL_NET_VERSION() macro.
|
||||
*/
|
||||
extern DECLSPEC const SDL_version * SDLCALL SDLNet_Linked_Version(void);
|
||||
|
||||
/* Initialize/Cleanup the network API
|
||||
SDL must be initialized before calls to functions in this library,
|
||||
because this library uses utility functions from the SDL library.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_Init(void);
|
||||
extern DECLSPEC void SDLCALL SDLNet_Quit(void);
|
||||
|
||||
/***********************************************************************/
|
||||
/* IPv4 hostname resolution API */
|
||||
/***********************************************************************/
|
||||
|
||||
typedef struct {
|
||||
Uint32 host; /* 32-bit IPv4 host address */
|
||||
Uint16 port; /* 16-bit protocol port */
|
||||
} IPaddress;
|
||||
|
||||
/* Resolve a host name and port to an IP address in network form.
|
||||
If the function succeeds, it will return 0.
|
||||
If the host couldn't be resolved, the host portion of the returned
|
||||
address will be INADDR_NONE, and the function will return -1.
|
||||
If 'host' is NULL, the resolved host will be set to INADDR_ANY.
|
||||
*/
|
||||
#ifndef INADDR_ANY
|
||||
#define INADDR_ANY 0x00000000
|
||||
#endif
|
||||
#ifndef INADDR_NONE
|
||||
#define INADDR_NONE 0xFFFFFFFF
|
||||
#endif
|
||||
#ifndef INADDR_LOOPBACK
|
||||
#define INADDR_LOOPBACK 0x7f000001
|
||||
#endif
|
||||
#ifndef INADDR_BROADCAST
|
||||
#define INADDR_BROADCAST 0xFFFFFFFF
|
||||
#endif
|
||||
extern DECLSPEC int SDLCALL SDLNet_ResolveHost(IPaddress *address, const char *host, Uint16 port);
|
||||
|
||||
/* Resolve an ip address to a host name in canonical form.
|
||||
If the ip couldn't be resolved, this function returns NULL,
|
||||
otherwise a pointer to a static buffer containing the hostname
|
||||
is returned. Note that this function is not thread-safe.
|
||||
*/
|
||||
extern DECLSPEC const char * SDLCALL SDLNet_ResolveIP(const IPaddress *ip);
|
||||
|
||||
/* Get the addresses of network interfaces on this system.
|
||||
This returns the number of addresses saved in 'addresses'
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_GetLocalAddresses(IPaddress *addresses, int maxcount);
|
||||
|
||||
/***********************************************************************/
|
||||
/* TCP network API */
|
||||
/***********************************************************************/
|
||||
|
||||
typedef struct _TCPsocket *TCPsocket;
|
||||
|
||||
/* Open a TCP network socket
|
||||
If ip.host is INADDR_NONE or INADDR_ANY, this creates a local server
|
||||
socket on the given port, otherwise a TCP connection to the remote
|
||||
host and port is attempted. The address passed in should already be
|
||||
swapped to network byte order (addresses returned from
|
||||
SDLNet_ResolveHost() are already in the correct form).
|
||||
The newly created socket is returned, or NULL if there was an error.
|
||||
*/
|
||||
extern DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Open(IPaddress *ip);
|
||||
|
||||
/* Accept an incoming connection on the given server socket.
|
||||
The newly created socket is returned, or NULL if there was an error.
|
||||
*/
|
||||
extern DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Accept(TCPsocket server);
|
||||
|
||||
/* Get the IP address of the remote system associated with the socket.
|
||||
If the socket is a server socket, this function returns NULL.
|
||||
*/
|
||||
extern DECLSPEC IPaddress * SDLCALL SDLNet_TCP_GetPeerAddress(TCPsocket sock);
|
||||
|
||||
/* Send 'len' bytes of 'data' over the non-server socket 'sock'
|
||||
This function returns the actual amount of data sent. If the return value
|
||||
is less than the amount of data sent, then either the remote connection was
|
||||
closed, or an unknown socket error occurred.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_TCP_Send(TCPsocket sock, const void *data,
|
||||
int len);
|
||||
|
||||
/* Receive up to 'maxlen' bytes of data over the non-server socket 'sock',
|
||||
and store them in the buffer pointed to by 'data'.
|
||||
This function returns the actual amount of data received. If the return
|
||||
value is less than or equal to zero, then either the remote connection was
|
||||
closed, or an unknown socket error occurred.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen);
|
||||
|
||||
/* Close a TCP network socket */
|
||||
extern DECLSPEC void SDLCALL SDLNet_TCP_Close(TCPsocket sock);
|
||||
|
||||
|
||||
/***********************************************************************/
|
||||
/* UDP network API */
|
||||
/***********************************************************************/
|
||||
|
||||
/* The maximum channels on a a UDP socket */
|
||||
#define SDLNET_MAX_UDPCHANNELS 150
|
||||
/* The maximum addresses bound to a single UDP socket channel */
|
||||
#define SDLNET_MAX_UDPADDRESSES 1
|
||||
|
||||
typedef struct _UDPsocket *UDPsocket;
|
||||
typedef struct {
|
||||
int channel; /* The src/dst channel of the packet */
|
||||
Uint8 *data; /* The packet data */
|
||||
int len; /* The length of the packet data */
|
||||
int maxlen; /* The size of the data buffer */
|
||||
int status; /* packet status after sending */
|
||||
IPaddress address; /* The source/dest address of an incoming/outgoing packet */
|
||||
} UDPpacket;
|
||||
|
||||
/* Allocate/resize/free a single UDP packet 'size' bytes long.
|
||||
The new packet is returned, or NULL if the function ran out of memory.
|
||||
*/
|
||||
extern DECLSPEC UDPpacket * SDLCALL SDLNet_AllocPacket(int size);
|
||||
extern DECLSPEC int SDLCALL SDLNet_ResizePacket(UDPpacket *packet, int newsize);
|
||||
extern DECLSPEC void SDLCALL SDLNet_FreePacket(UDPpacket *packet);
|
||||
|
||||
/* Allocate/Free a UDP packet vector (array of packets) of 'howmany' packets,
|
||||
each 'size' bytes long.
|
||||
A pointer to the first packet in the array is returned, or NULL if the
|
||||
function ran out of memory.
|
||||
*/
|
||||
extern DECLSPEC UDPpacket ** SDLCALL SDLNet_AllocPacketV(int howmany, int size);
|
||||
extern DECLSPEC void SDLCALL SDLNet_FreePacketV(UDPpacket **packetV);
|
||||
|
||||
|
||||
/* Open a UDP network socket
|
||||
If 'port' is non-zero, the UDP socket is bound to a local port.
|
||||
The 'port' should be given in native byte order, but is used
|
||||
internally in network (big endian) byte order, in addresses, etc.
|
||||
This allows other systems to send to this socket via a known port.
|
||||
*/
|
||||
extern DECLSPEC UDPsocket SDLCALL SDLNet_UDP_Open(Uint16 port);
|
||||
|
||||
/* Set the percentage of simulated packet loss for packets sent on the socket.
|
||||
*/
|
||||
extern DECLSPEC void SDLCALL SDLNet_UDP_SetPacketLoss(UDPsocket sock, int percent);
|
||||
|
||||
/* Bind the address 'address' to the requested channel on the UDP socket.
|
||||
If the channel is -1, then the first unbound channel will be bound with
|
||||
the given address as it's primary address.
|
||||
If the channel is already bound, this new address will be added to the
|
||||
list of valid source addresses for packets arriving on the channel.
|
||||
If the channel is not already bound, then the address becomes the primary
|
||||
address, to which all outbound packets on the channel are sent.
|
||||
This function returns the channel which was bound, or -1 on error.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_UDP_Bind(UDPsocket sock, int channel, const IPaddress *address);
|
||||
|
||||
/* Unbind all addresses from the given channel */
|
||||
extern DECLSPEC void SDLCALL SDLNet_UDP_Unbind(UDPsocket sock, int channel);
|
||||
|
||||
/* Get the primary IP address of the remote system associated with the
|
||||
socket and channel. If the channel is -1, then the primary IP port
|
||||
of the UDP socket is returned -- this is only meaningful for sockets
|
||||
opened with a specific port.
|
||||
If the channel is not bound and not -1, this function returns NULL.
|
||||
*/
|
||||
extern DECLSPEC IPaddress * SDLCALL SDLNet_UDP_GetPeerAddress(UDPsocket sock, int channel);
|
||||
|
||||
/* Send a vector of packets to the the channels specified within the packet.
|
||||
If the channel specified in the packet is -1, the packet will be sent to
|
||||
the address in the 'src' member of the packet.
|
||||
Each packet will be updated with the status of the packet after it has
|
||||
been sent, -1 if the packet send failed.
|
||||
This function returns the number of packets sent.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_UDP_SendV(UDPsocket sock, UDPpacket **packets, int npackets);
|
||||
|
||||
/* Send a single packet to the specified channel.
|
||||
If the channel specified in the packet is -1, the packet will be sent to
|
||||
the address in the 'src' member of the packet.
|
||||
The packet will be updated with the status of the packet after it has
|
||||
been sent.
|
||||
This function returns 1 if the packet was sent, or 0 on error.
|
||||
|
||||
NOTE:
|
||||
The maximum size of the packet is limited by the MTU (Maximum Transfer Unit)
|
||||
of the transport medium. It can be as low as 250 bytes for some PPP links,
|
||||
and as high as 1500 bytes for ethernet.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet);
|
||||
|
||||
/* Receive a vector of pending packets from the UDP socket.
|
||||
The returned packets contain the source address and the channel they arrived
|
||||
on. If they did not arrive on a bound channel, the the channel will be set
|
||||
to -1.
|
||||
The channels are checked in highest to lowest order, so if an address is
|
||||
bound to multiple channels, the highest channel with the source address
|
||||
bound will be returned.
|
||||
This function returns the number of packets read from the network, or -1
|
||||
on error. This function does not block, so can return 0 packets pending.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_UDP_RecvV(UDPsocket sock, UDPpacket **packets);
|
||||
|
||||
/* Receive a single packet from the UDP socket.
|
||||
The returned packet contains the source address and the channel it arrived
|
||||
on. If it did not arrive on a bound channel, the the channel will be set
|
||||
to -1.
|
||||
The channels are checked in highest to lowest order, so if an address is
|
||||
bound to multiple channels, the highest channel with the source address
|
||||
bound will be returned.
|
||||
This function returns the number of packets read from the network, or -1
|
||||
on error. This function does not block, so can return 0 packets pending.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_UDP_Recv(UDPsocket sock, UDPpacket *packet);
|
||||
|
||||
/* Close a UDP network socket */
|
||||
extern DECLSPEC void SDLCALL SDLNet_UDP_Close(UDPsocket sock);
|
||||
|
||||
|
||||
/***********************************************************************/
|
||||
/* Hooks for checking sockets for available data */
|
||||
/***********************************************************************/
|
||||
|
||||
typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;
|
||||
|
||||
/* Any network socket can be safely cast to this socket type */
|
||||
typedef struct _SDLNet_GenericSocket {
|
||||
int ready;
|
||||
} *SDLNet_GenericSocket;
|
||||
|
||||
/* Allocate a socket set for use with SDLNet_CheckSockets()
|
||||
This returns a socket set for up to 'maxsockets' sockets, or NULL if
|
||||
the function ran out of memory.
|
||||
*/
|
||||
extern DECLSPEC SDLNet_SocketSet SDLCALL SDLNet_AllocSocketSet(int maxsockets);
|
||||
|
||||
/* Add a socket to a set of sockets to be checked for available data */
|
||||
#define SDLNet_TCP_AddSocket(set, sock) \
|
||||
SDLNet_AddSocket(set, SDL_reinterpret_cast(SDLNet_GenericSocket, sock))
|
||||
#define SDLNet_UDP_AddSocket(set, sock) \
|
||||
SDLNet_AddSocket(set, SDL_reinterpret_cast(SDLNet_GenericSocket, sock))
|
||||
extern DECLSPEC int SDLCALL SDLNet_AddSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock);
|
||||
|
||||
/* Remove a socket from a set of sockets to be checked for available data */
|
||||
#define SDLNet_TCP_DelSocket(set, sock) \
|
||||
SDLNet_DelSocket(set, SDL_reinterpret_cast(SDLNet_GenericSocket, sock))
|
||||
#define SDLNet_UDP_DelSocket(set, sock) \
|
||||
SDLNet_DelSocket(set, SDL_reinterpret_cast(SDLNet_GenericSocket, sock))
|
||||
extern DECLSPEC int SDLCALL SDLNet_DelSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock);
|
||||
|
||||
/* This function checks to see if data is available for reading on the
|
||||
given set of sockets. If 'timeout' is 0, it performs a quick poll,
|
||||
otherwise the function returns when either data is available for
|
||||
reading, or the timeout in milliseconds has elapsed, which ever occurs
|
||||
first. This function returns the number of sockets ready for reading,
|
||||
or -1 if there was an error with the select() system call.
|
||||
*/
|
||||
extern DECLSPEC int SDLCALL SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout);
|
||||
|
||||
/* After calling SDLNet_CheckSockets(), you can use this function on a
|
||||
socket that was in the socket set, to find out if data is available
|
||||
for reading.
|
||||
*/
|
||||
#define SDLNet_SocketReady(sock) \
|
||||
((sock != NULL) && SDL_reinterpret_cast(SDLNet_GenericSocket, sock)->ready)
|
||||
|
||||
/* Free a set of sockets allocated by SDL_NetAllocSocketSet() */
|
||||
extern DECLSPEC void SDLCALL SDLNet_FreeSocketSet(SDLNet_SocketSet set);
|
||||
|
||||
|
||||
/***********************************************************************/
|
||||
/* Platform-independent data conversion functions */
|
||||
/***********************************************************************/
|
||||
|
||||
/* Write a 16/32 bit value to network packet buffer */
|
||||
extern DECLSPEC void SDLCALL SDLNet_Write16(Uint16 value, void *area);
|
||||
extern DECLSPEC void SDLCALL SDLNet_Write32(Uint32 value, void *area);
|
||||
|
||||
/* Read a 16/32 bit value from network packet buffer */
|
||||
extern DECLSPEC Uint16 SDLCALL SDLNet_Read16(void *area);
|
||||
extern DECLSPEC Uint32 SDLCALL SDLNet_Read32(void *area);
|
||||
|
||||
/***********************************************************************/
|
||||
/* Error reporting functions */
|
||||
/***********************************************************************/
|
||||
|
||||
/* We'll use SDL's functions for error reporting */
|
||||
#define SDLNet_SetError SDL_SetError
|
||||
#define SDLNet_GetError SDL_GetError
|
||||
|
||||
/* I'm eventually going to try to disentangle SDL_net from SDL, thus making
|
||||
SDL_net an independent X-platform networking toolkit. Not today though....
|
||||
|
||||
extern no_parse_DECLSPEC void SDLCALL SDLNet_SetError(const char *fmt, ...);
|
||||
extern no_parse_DECLSPEC char * SDLCALL SDLNet_GetError(void);
|
||||
*/
|
||||
|
||||
|
||||
/* Inline macro functions to read/write network data */
|
||||
|
||||
/* Warning, some systems have data access alignment restrictions */
|
||||
#if defined(sparc) || defined(mips)
|
||||
#define SDL_DATA_ALIGNED 1
|
||||
#endif
|
||||
#ifndef SDL_DATA_ALIGNED
|
||||
#define SDL_DATA_ALIGNED 0
|
||||
#endif
|
||||
|
||||
/* Write a 16 bit value to network packet buffer */
|
||||
#if !SDL_DATA_ALIGNED
|
||||
#define SDLNet_Write16(value, areap) \
|
||||
(*SDL_reinterpret_cast(Uint16 *, areap) = SDL_SwapBE16(value))
|
||||
#else
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
#define SDLNet_Write16(value, areap) \
|
||||
do \
|
||||
{ \
|
||||
Uint8 *area = SDL_reinterpret_cast(Uint8 *, areap); \
|
||||
area[0] = (value >> 8) & 0xFF; \
|
||||
area[1] = value & 0xFF; \
|
||||
} while ( 0 )
|
||||
#else
|
||||
#define SDLNet_Write16(value, areap) \
|
||||
do \
|
||||
{ \
|
||||
Uint8 *area = SDL_reinterpret_cast(Uint8 *, areap); \
|
||||
area[1] = (value >> 8) & 0xFF; \
|
||||
area[0] = value & 0xFF; \
|
||||
} while ( 0 )
|
||||
#endif
|
||||
#endif /* !SDL_DATA_ALIGNED */
|
||||
|
||||
/* Write a 32 bit value to network packet buffer */
|
||||
#if !SDL_DATA_ALIGNED
|
||||
#define SDLNet_Write32(value, areap) \
|
||||
*SDL_reinterpret_cast(Uint32 *, areap) = SDL_SwapBE32(value);
|
||||
#else
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
#define SDLNet_Write32(value, areap) \
|
||||
do \
|
||||
{ \
|
||||
Uint8 *area = SDL_reinterpret_cast(Uint8 *, areap); \
|
||||
area[0] = (value >> 24) & 0xFF; \
|
||||
area[1] = (value >> 16) & 0xFF; \
|
||||
area[2] = (value >> 8) & 0xFF; \
|
||||
area[3] = value & 0xFF; \
|
||||
} while ( 0 )
|
||||
#else
|
||||
#define SDLNet_Write32(value, areap) \
|
||||
do \
|
||||
{ \
|
||||
Uint8 *area = SDL_reinterpret_cast(Uint8 *, areap); \
|
||||
area[3] = (value >> 24) & 0xFF; \
|
||||
area[2] = (value >> 16) & 0xFF; \
|
||||
area[1] = (value >> 8) & 0xFF; \
|
||||
area[0] = value & 0xFF; \
|
||||
} while ( 0 )
|
||||
#endif
|
||||
#endif /* !SDL_DATA_ALIGNED */
|
||||
|
||||
/* Read a 16 bit value from network packet buffer */
|
||||
#if !SDL_DATA_ALIGNED
|
||||
#define SDLNet_Read16(areap) \
|
||||
(SDL_SwapBE16(*SDL_reinterpret_cast(Uint16 *, areap)))
|
||||
#else
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
#define SDLNet_Read16(areap) \
|
||||
(((SDL_reinterpret_cast(Uint8 *, areap))[0] << 8) | (SDL_reinterpret_cast(Uint8 *, areap))[1] << 0)
|
||||
#else
|
||||
#define SDLNet_Read16(areap) \
|
||||
(((SDL_reinterpret_cast(Uint8 *, areap))[1] << 8) | (SDL_reinterpret_cast(Uint8 *, areap))[0] << 0)
|
||||
#endif
|
||||
#endif /* !SDL_DATA_ALIGNED */
|
||||
|
||||
/* Read a 32 bit value from network packet buffer */
|
||||
#if !SDL_DATA_ALIGNED
|
||||
#define SDLNet_Read32(areap) \
|
||||
(SDL_SwapBE32(*SDL_reinterpret_cast(Uint32 *, areap)))
|
||||
#else
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
#define SDLNet_Read32(areap) \
|
||||
(((SDL_reinterpret_cast(Uint8 *, areap))[0] << 24) | ((SDL_reinterpret_cast(Uint8 *, areap))[1] << 16) | \
|
||||
((SDL_reinterpret_cast(Uint8 *, areap))[2] << 8) | (SDL_reinterpret_cast(Uint8 *, areap))[3] << 0)
|
||||
#else
|
||||
#define SDLNet_Read32(areap) \
|
||||
(((SDL_reinterpret_cast(Uint8 *, areap))[3] << 24) | ((SDL_reinterpret_cast(Uint8 *, areap))[2] << 16) | \
|
||||
((SDL_reinterpret_cast(Uint8 *, areap))[1] << 8) | (SDL_reinterpret_cast(Uint8 *, areap))[0] << 0)
|
||||
#endif
|
||||
#endif /* !SDL_DATA_ALIGNED */
|
||||
|
||||
/* Ends C function definitions when using C++ */
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#include "SDL/close_code.h"
|
||||
|
||||
#endif /* _SDL_NET_H */
|
||||
@@ -1,269 +0,0 @@
|
||||
/*
|
||||
SDL_net: An example cross-platform network library for use with SDL
|
||||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "SDL/SDL_endian.h"
|
||||
|
||||
#include "SDLnetsys.h"
|
||||
#include "SDL_net.h"
|
||||
|
||||
|
||||
const SDL_version *SDLNet_Linked_Version(void)
|
||||
{
|
||||
static SDL_version linked_version;
|
||||
SDL_NET_VERSION(&linked_version);
|
||||
return(&linked_version);
|
||||
}
|
||||
|
||||
/* Since the UNIX/Win32/BeOS code is so different from MacOS,
|
||||
we'll just have two completely different sections here.
|
||||
*/
|
||||
static int SDLNet_started = 0;
|
||||
|
||||
#ifndef __USE_W32_SOCKETS
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#ifndef __USE_W32_SOCKETS
|
||||
|
||||
int SDLNet_GetLastError(void)
|
||||
{
|
||||
return errno;
|
||||
}
|
||||
|
||||
void SDLNet_SetLastError(int err)
|
||||
{
|
||||
errno = err;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* Initialize/Cleanup the network API */
|
||||
int SDLNet_Init(void)
|
||||
{
|
||||
if ( !SDLNet_started ) {
|
||||
#ifdef __USE_W32_SOCKETS
|
||||
/* Start up the windows networking */
|
||||
WORD version_wanted = MAKEWORD(1,1);
|
||||
WSADATA wsaData;
|
||||
|
||||
if ( WSAStartup(version_wanted, &wsaData) != 0 ) {
|
||||
SDLNet_SetError("Couldn't initialize Winsock 1.1\n");
|
||||
return(-1);
|
||||
}
|
||||
#else
|
||||
/* SIGPIPE is generated when a remote socket is closed */
|
||||
void (*handler)(int);
|
||||
handler = signal(SIGPIPE, SIG_IGN);
|
||||
if ( handler != SIG_DFL ) {
|
||||
signal(SIGPIPE, handler);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
++SDLNet_started;
|
||||
return(0);
|
||||
}
|
||||
void SDLNet_Quit(void)
|
||||
{
|
||||
if ( SDLNet_started == 0 ) {
|
||||
return;
|
||||
}
|
||||
if ( --SDLNet_started == 0 ) {
|
||||
#ifdef __USE_W32_SOCKETS
|
||||
/* Clean up windows networking */
|
||||
if ( WSACleanup() == SOCKET_ERROR ) {
|
||||
if ( WSAGetLastError() == WSAEINPROGRESS ) {
|
||||
#ifndef _WIN32_WCE
|
||||
WSACancelBlockingCall();
|
||||
#endif
|
||||
WSACleanup();
|
||||
}
|
||||
}
|
||||
#else
|
||||
/* Restore the SIGPIPE handler */
|
||||
void (*handler)(int);
|
||||
handler = signal(SIGPIPE, SIG_DFL);
|
||||
if ( handler != SIG_IGN ) {
|
||||
signal(SIGPIPE, handler);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/* Resolve a host name and port to an IP address in network form */
|
||||
int SDLNet_ResolveHost(IPaddress *address, const char *host, Uint16 port)
|
||||
{
|
||||
int retval = 0;
|
||||
|
||||
/* Perform the actual host resolution */
|
||||
if ( host == NULL ) {
|
||||
address->host = INADDR_ANY;
|
||||
} else {
|
||||
address->host = inet_addr(host);
|
||||
if ( address->host == INADDR_NONE ) {
|
||||
struct hostent *hp;
|
||||
|
||||
hp = gethostbyname(host);
|
||||
if ( hp ) {
|
||||
memcpy(&address->host,hp->h_addr,hp->h_length);
|
||||
} else {
|
||||
retval = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
address->port = SDL_SwapBE16(port);
|
||||
|
||||
/* Return the status */
|
||||
return(retval);
|
||||
}
|
||||
|
||||
/* Resolve an ip address to a host name in canonical form.
|
||||
If the ip couldn't be resolved, this function returns NULL,
|
||||
otherwise a pointer to a static buffer containing the hostname
|
||||
is returned. Note that this function is not thread-safe.
|
||||
*/
|
||||
/* Written by Miguel Angel Blanch.
|
||||
* Main Programmer of Arianne RPG.
|
||||
* http://come.to/arianne_rpg
|
||||
*/
|
||||
const char *SDLNet_ResolveIP(const IPaddress *ip)
|
||||
{
|
||||
struct hostent *hp;
|
||||
struct in_addr in;
|
||||
|
||||
hp = gethostbyaddr((const char *)&ip->host, sizeof(ip->host), AF_INET);
|
||||
if ( hp != NULL ) {
|
||||
return hp->h_name;
|
||||
}
|
||||
|
||||
in.s_addr = ip->host;
|
||||
return inet_ntoa(in);
|
||||
}
|
||||
|
||||
int SDLNet_GetLocalAddresses(IPaddress *addresses, int maxcount)
|
||||
{
|
||||
int count = 0;
|
||||
#ifdef SIOCGIFCONF
|
||||
/* Defined on Mac OS X */
|
||||
#ifndef _SIZEOF_ADDR_IFREQ
|
||||
#define _SIZEOF_ADDR_IFREQ sizeof
|
||||
#endif
|
||||
SOCKET sock;
|
||||
struct ifconf conf;
|
||||
char data[4096];
|
||||
struct ifreq *ifr;
|
||||
struct sockaddr_in *sock_addr;
|
||||
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if ( sock == INVALID_SOCKET ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
conf.ifc_len = sizeof(data);
|
||||
conf.ifc_buf = (caddr_t) data;
|
||||
if ( ioctl(sock, SIOCGIFCONF, &conf) < 0 ) {
|
||||
closesocket(sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ifr = (struct ifreq*)data;
|
||||
while ((char*)ifr < data+conf.ifc_len) {
|
||||
if (ifr->ifr_addr.sa_family == AF_INET) {
|
||||
if (count < maxcount) {
|
||||
sock_addr = (struct sockaddr_in*)&ifr->ifr_addr;
|
||||
addresses[count].host = sock_addr->sin_addr.s_addr;
|
||||
addresses[count].port = sock_addr->sin_port;
|
||||
}
|
||||
++count;
|
||||
}
|
||||
ifr = (struct ifreq*)((char*)ifr + _SIZEOF_ADDR_IFREQ(*ifr));
|
||||
}
|
||||
closesocket(sock);
|
||||
#elif defined(__WIN32__)
|
||||
PIP_ADAPTER_INFO pAdapterInfo;
|
||||
PIP_ADAPTER_INFO pAdapter;
|
||||
PIP_ADDR_STRING pAddress;
|
||||
DWORD dwRetVal = 0;
|
||||
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
|
||||
|
||||
pAdapterInfo = (IP_ADAPTER_INFO *) SDL_malloc(sizeof (IP_ADAPTER_INFO));
|
||||
if (pAdapterInfo == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == ERROR_BUFFER_OVERFLOW) {
|
||||
pAdapterInfo = (IP_ADAPTER_INFO *) SDL_realloc(pAdapterInfo, ulOutBufLen);
|
||||
if (pAdapterInfo == NULL) {
|
||||
return 0;
|
||||
}
|
||||
dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
||||
}
|
||||
|
||||
if (dwRetVal == NO_ERROR) {
|
||||
for (pAdapter = pAdapterInfo; pAdapter; pAdapter = pAdapter->Next) {
|
||||
for (pAddress = &pAdapterInfo->IpAddressList; pAddress; pAddress = pAddress->Next) {
|
||||
if (count < maxcount) {
|
||||
addresses[count].host = inet_addr(pAddress->IpAddress.String);
|
||||
addresses[count].port = 0;
|
||||
}
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_free(pAdapterInfo);
|
||||
#endif
|
||||
return count;
|
||||
}
|
||||
|
||||
#if !SDL_DATA_ALIGNED /* function versions for binary compatibility */
|
||||
|
||||
/* Write a 16 bit value to network packet buffer */
|
||||
#undef SDLNet_Write16
|
||||
void SDLNet_Write16(Uint16 value, void *areap)
|
||||
{
|
||||
(*(Uint16 *)(areap) = SDL_SwapBE16(value));
|
||||
}
|
||||
|
||||
/* Write a 32 bit value to network packet buffer */
|
||||
#undef SDLNet_Write32
|
||||
void SDLNet_Write32(Uint32 value, void *areap)
|
||||
{
|
||||
*(Uint32 *)(areap) = SDL_SwapBE32(value);
|
||||
}
|
||||
|
||||
/* Read a 16 bit value from network packet buffer */
|
||||
#undef SDLNet_Read16
|
||||
Uint16 SDLNet_Read16(void *areap)
|
||||
{
|
||||
return (SDL_SwapBE16(*(Uint16 *)(areap)));
|
||||
}
|
||||
|
||||
/* Read a 32 bit value from network packet buffer */
|
||||
#undef SDLNet_Read32
|
||||
Uint32 SDLNet_Read32(void *areap)
|
||||
{
|
||||
return (SDL_SwapBE32(*(Uint32 *)(areap)));
|
||||
}
|
||||
|
||||
#endif /* !SDL_DATA_ALIGNED */
|
||||
@@ -1,295 +0,0 @@
|
||||
/*
|
||||
SDL_net: An example cross-platform network library for use with SDL
|
||||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#include "SDLnetsys.h"
|
||||
#include "SDL_net.h"
|
||||
|
||||
/* The network API for TCP sockets */
|
||||
|
||||
/* Since the UNIX/Win32/BeOS code is so different from MacOS,
|
||||
we'll just have two completely different sections here.
|
||||
*/
|
||||
|
||||
struct _TCPsocket {
|
||||
int ready;
|
||||
SOCKET channel;
|
||||
IPaddress remoteAddress;
|
||||
IPaddress localAddress;
|
||||
int sflag;
|
||||
};
|
||||
|
||||
/* Open a TCP network socket
|
||||
If 'remote' is NULL, this creates a local server socket on the given port,
|
||||
otherwise a TCP connection to the remote host and port is attempted.
|
||||
The newly created socket is returned, or NULL if there was an error.
|
||||
*/
|
||||
TCPsocket SDLNet_TCP_Open(IPaddress *ip)
|
||||
{
|
||||
TCPsocket sock;
|
||||
struct sockaddr_in sock_addr;
|
||||
|
||||
/* Allocate a TCP socket structure */
|
||||
sock = (TCPsocket)malloc(sizeof(*sock));
|
||||
if ( sock == NULL ) {
|
||||
SDLNet_SetError("Out of memory");
|
||||
goto error_return;
|
||||
}
|
||||
|
||||
/* Open the socket */
|
||||
sock->channel = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if ( sock->channel == INVALID_SOCKET ) {
|
||||
SDLNet_SetError("Couldn't create socket");
|
||||
goto error_return;
|
||||
}
|
||||
|
||||
/* Connect to remote, or bind locally, as appropriate */
|
||||
if ( (ip->host != INADDR_NONE) && (ip->host != INADDR_ANY) ) {
|
||||
|
||||
// ######### Connecting to remote
|
||||
|
||||
memset(&sock_addr, 0, sizeof(sock_addr));
|
||||
sock_addr.sin_family = AF_INET;
|
||||
sock_addr.sin_addr.s_addr = ip->host;
|
||||
sock_addr.sin_port = ip->port;
|
||||
|
||||
/* Connect to the remote host */
|
||||
if ( connect(sock->channel, (struct sockaddr *)&sock_addr,
|
||||
sizeof(sock_addr)) == SOCKET_ERROR ) {
|
||||
SDLNet_SetError("Couldn't connect to remote host");
|
||||
goto error_return;
|
||||
}
|
||||
sock->sflag = 0;
|
||||
} else {
|
||||
|
||||
// ########## Binding locally
|
||||
|
||||
memset(&sock_addr, 0, sizeof(sock_addr));
|
||||
sock_addr.sin_family = AF_INET;
|
||||
sock_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
sock_addr.sin_port = ip->port;
|
||||
|
||||
/*
|
||||
* Windows gets bad mojo with SO_REUSEADDR:
|
||||
* http://www.devolution.com/pipermail/sdl/2005-September/070491.html
|
||||
* --ryan.
|
||||
*/
|
||||
#ifndef WIN32
|
||||
/* allow local address reuse */
|
||||
{ int yes = 1;
|
||||
setsockopt(sock->channel, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Bind the socket for listening */
|
||||
if ( bind(sock->channel, (struct sockaddr *)&sock_addr,
|
||||
sizeof(sock_addr)) == SOCKET_ERROR ) {
|
||||
SDLNet_SetError("Couldn't bind to local port");
|
||||
goto error_return;
|
||||
}
|
||||
if ( listen(sock->channel, 5) == SOCKET_ERROR ) {
|
||||
SDLNet_SetError("Couldn't listen to local port");
|
||||
goto error_return;
|
||||
}
|
||||
|
||||
/* Set the socket to non-blocking mode for accept() */
|
||||
#if defined(__BEOS__) && defined(SO_NONBLOCK)
|
||||
/* On BeOS r5 there is O_NONBLOCK but it's for files only */
|
||||
{
|
||||
long b = 1;
|
||||
setsockopt(sock->channel, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
|
||||
}
|
||||
#elif defined(O_NONBLOCK)
|
||||
{
|
||||
fcntl(sock->channel, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
#elif defined(WIN32)
|
||||
{
|
||||
unsigned long mode = 1;
|
||||
ioctlsocket (sock->channel, FIONBIO, &mode);
|
||||
}
|
||||
#elif defined(__OS2__)
|
||||
{
|
||||
int dontblock = 1;
|
||||
ioctl(sock->channel, FIONBIO, &dontblock);
|
||||
}
|
||||
#else
|
||||
#warning How do we set non-blocking mode on other operating systems?
|
||||
#endif
|
||||
sock->sflag = 1;
|
||||
}
|
||||
sock->ready = 0;
|
||||
|
||||
#ifdef TCP_NODELAY
|
||||
/* Set the nodelay TCP option for real-time games */
|
||||
{ int yes = 1;
|
||||
setsockopt(sock->channel, IPPROTO_TCP, TCP_NODELAY, (char*)&yes, sizeof(yes));
|
||||
}
|
||||
#endif /* TCP_NODELAY */
|
||||
|
||||
/* Fill in the channel host address */
|
||||
sock->remoteAddress.host = sock_addr.sin_addr.s_addr;
|
||||
sock->remoteAddress.port = sock_addr.sin_port;
|
||||
|
||||
/* The socket is ready */
|
||||
return(sock);
|
||||
|
||||
error_return:
|
||||
SDLNet_TCP_Close(sock);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
/* Accept an incoming connection on the given server socket.
|
||||
The newly created socket is returned, or NULL if there was an error.
|
||||
*/
|
||||
TCPsocket SDLNet_TCP_Accept(TCPsocket server)
|
||||
{
|
||||
TCPsocket sock;
|
||||
struct sockaddr_in sock_addr;
|
||||
socklen_t sock_alen;
|
||||
|
||||
/* Only server sockets can accept */
|
||||
if ( ! server->sflag ) {
|
||||
SDLNet_SetError("Only server sockets can accept()");
|
||||
return(NULL);
|
||||
}
|
||||
server->ready = 0;
|
||||
|
||||
/* Allocate a TCP socket structure */
|
||||
sock = (TCPsocket)malloc(sizeof(*sock));
|
||||
if ( sock == NULL ) {
|
||||
SDLNet_SetError("Out of memory");
|
||||
goto error_return;
|
||||
}
|
||||
|
||||
/* Accept a new TCP connection on a server socket */
|
||||
sock_alen = sizeof(sock_addr);
|
||||
sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr,
|
||||
&sock_alen);
|
||||
if ( sock->channel == INVALID_SOCKET ) {
|
||||
SDLNet_SetError("accept() failed");
|
||||
goto error_return;
|
||||
}
|
||||
#ifdef WIN32
|
||||
{
|
||||
/* passing a zero value, socket mode set to block on */
|
||||
unsigned long mode = 0;
|
||||
ioctlsocket (sock->channel, FIONBIO, &mode);
|
||||
}
|
||||
#elif defined(O_NONBLOCK)
|
||||
{
|
||||
int flags = fcntl(sock->channel, F_GETFL, 0);
|
||||
fcntl(sock->channel, F_SETFL, flags & ~O_NONBLOCK);
|
||||
}
|
||||
#endif /* WIN32 */
|
||||
sock->remoteAddress.host = sock_addr.sin_addr.s_addr;
|
||||
sock->remoteAddress.port = sock_addr.sin_port;
|
||||
|
||||
sock->sflag = 0;
|
||||
sock->ready = 0;
|
||||
|
||||
/* The socket is ready */
|
||||
return(sock);
|
||||
|
||||
error_return:
|
||||
SDLNet_TCP_Close(sock);
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
/* Get the IP address of the remote system associated with the socket.
|
||||
If the socket is a server socket, this function returns NULL.
|
||||
*/
|
||||
IPaddress *SDLNet_TCP_GetPeerAddress(TCPsocket sock)
|
||||
{
|
||||
if ( sock->sflag ) {
|
||||
return(NULL);
|
||||
}
|
||||
return(&sock->remoteAddress);
|
||||
}
|
||||
|
||||
/* Send 'len' bytes of 'data' over the non-server socket 'sock'
|
||||
This function returns the actual amount of data sent. If the return value
|
||||
is less than the amount of data sent, then either the remote connection was
|
||||
closed, or an unknown socket error occurred.
|
||||
*/
|
||||
int SDLNet_TCP_Send(TCPsocket sock, const void *datap, int len)
|
||||
{
|
||||
const Uint8 *data = (const Uint8 *)datap; /* For pointer arithmetic */
|
||||
int sent, left;
|
||||
|
||||
/* Server sockets are for accepting connections only */
|
||||
if ( sock->sflag ) {
|
||||
SDLNet_SetError("Server sockets cannot send");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
/* Keep sending data until it's sent or an error occurs */
|
||||
left = len;
|
||||
sent = 0;
|
||||
SDLNet_SetLastError(0);
|
||||
do {
|
||||
len = send(sock->channel, (const char *) data, left, 0);
|
||||
if ( len > 0 ) {
|
||||
sent += len;
|
||||
left -= len;
|
||||
data += len;
|
||||
}
|
||||
} while ( (left > 0) && ((len > 0) || (SDLNet_GetLastError() == EINTR)) );
|
||||
|
||||
return(sent);
|
||||
}
|
||||
|
||||
/* Receive up to 'maxlen' bytes of data over the non-server socket 'sock',
|
||||
and store them in the buffer pointed to by 'data'.
|
||||
This function returns the actual amount of data received. If the return
|
||||
value is less than or equal to zero, then either the remote connection was
|
||||
closed, or an unknown socket error occurred.
|
||||
*/
|
||||
int SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen)
|
||||
{
|
||||
int len;
|
||||
|
||||
/* Server sockets are for accepting connections only */
|
||||
if ( sock->sflag ) {
|
||||
SDLNet_SetError("Server sockets cannot receive");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
SDLNet_SetLastError(0);
|
||||
do {
|
||||
len = recv(sock->channel, (char *) data, maxlen, 0);
|
||||
} while ( SDLNet_GetLastError() == EINTR );
|
||||
|
||||
sock->ready = 0;
|
||||
return(len);
|
||||
}
|
||||
|
||||
/* Close a TCP network socket */
|
||||
void SDLNet_TCP_Close(TCPsocket sock)
|
||||
{
|
||||
if ( sock != NULL ) {
|
||||
if ( sock->channel != INVALID_SOCKET ) {
|
||||
closesocket(sock->channel);
|
||||
}
|
||||
free(sock);
|
||||
}
|
||||
}
|
||||
@@ -1,528 +0,0 @@
|
||||
/*
|
||||
SDL_net: An example cross-platform network library for use with SDL
|
||||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#include "SDLnetsys.h"
|
||||
#include "SDL_net.h"
|
||||
|
||||
#ifdef __WIN32__
|
||||
#define srandom srand
|
||||
#define random rand
|
||||
#endif
|
||||
|
||||
struct UDP_channel {
|
||||
int numbound;
|
||||
IPaddress address[SDLNET_MAX_UDPADDRESSES];
|
||||
};
|
||||
|
||||
struct _UDPsocket {
|
||||
int ready;
|
||||
SOCKET channel;
|
||||
IPaddress address;
|
||||
|
||||
struct UDP_channel binding[SDLNET_MAX_UDPCHANNELS];
|
||||
|
||||
/* For debugging purposes */
|
||||
int packetloss;
|
||||
};
|
||||
|
||||
/* Allocate/free a single UDP packet 'size' bytes long.
|
||||
The new packet is returned, or NULL if the function ran out of memory.
|
||||
*/
|
||||
extern UDPpacket *SDLNet_AllocPacket(int size)
|
||||
{
|
||||
UDPpacket *packet;
|
||||
int error;
|
||||
|
||||
|
||||
error = 1;
|
||||
packet = (UDPpacket *)malloc(sizeof(*packet));
|
||||
if ( packet != NULL ) {
|
||||
packet->maxlen = size;
|
||||
packet->data = (Uint8 *)malloc(size);
|
||||
if ( packet->data != NULL ) {
|
||||
error = 0;
|
||||
}
|
||||
}
|
||||
if ( error ) {
|
||||
SDLNet_SetError("Out of memory");
|
||||
SDLNet_FreePacket(packet);
|
||||
packet = NULL;
|
||||
}
|
||||
return(packet);
|
||||
}
|
||||
int SDLNet_ResizePacket(UDPpacket *packet, int newsize)
|
||||
{
|
||||
Uint8 *newdata;
|
||||
|
||||
newdata = (Uint8 *)malloc(newsize);
|
||||
if ( newdata != NULL ) {
|
||||
free(packet->data);
|
||||
packet->data = newdata;
|
||||
packet->maxlen = newsize;
|
||||
}
|
||||
return(packet->maxlen);
|
||||
}
|
||||
extern void SDLNet_FreePacket(UDPpacket *packet)
|
||||
{
|
||||
if ( packet ) {
|
||||
if ( packet->data )
|
||||
free(packet->data);
|
||||
free(packet);
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate/Free a UDP packet vector (array of packets) of 'howmany' packets,
|
||||
each 'size' bytes long.
|
||||
A pointer to the packet array is returned, or NULL if the function ran out
|
||||
of memory.
|
||||
*/
|
||||
UDPpacket **SDLNet_AllocPacketV(int howmany, int size)
|
||||
{
|
||||
UDPpacket **packetV;
|
||||
|
||||
packetV = (UDPpacket **)malloc((howmany+1)*sizeof(*packetV));
|
||||
if ( packetV != NULL ) {
|
||||
int i;
|
||||
for ( i=0; i<howmany; ++i ) {
|
||||
packetV[i] = SDLNet_AllocPacket(size);
|
||||
if ( packetV[i] == NULL ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
packetV[i] = NULL;
|
||||
|
||||
if ( i != howmany ) {
|
||||
SDLNet_SetError("Out of memory");
|
||||
SDLNet_FreePacketV(packetV);
|
||||
packetV = NULL;
|
||||
}
|
||||
}
|
||||
return(packetV);
|
||||
}
|
||||
void SDLNet_FreePacketV(UDPpacket **packetV)
|
||||
{
|
||||
if ( packetV ) {
|
||||
int i;
|
||||
for ( i=0; packetV[i]; ++i ) {
|
||||
SDLNet_FreePacket(packetV[i]);
|
||||
}
|
||||
free(packetV);
|
||||
}
|
||||
}
|
||||
|
||||
/* Since the UNIX/Win32/BeOS code is so different from MacOS,
|
||||
we'll just have two completely different sections here.
|
||||
*/
|
||||
|
||||
/* Open a UDP network socket
|
||||
If 'port' is non-zero, the UDP socket is bound to a fixed local port.
|
||||
*/
|
||||
UDPsocket SDLNet_UDP_Open(Uint16 port)
|
||||
{
|
||||
UDPsocket sock;
|
||||
struct sockaddr_in sock_addr;
|
||||
socklen_t sock_len;
|
||||
|
||||
/* Allocate a UDP socket structure */
|
||||
sock = (UDPsocket)malloc(sizeof(*sock));
|
||||
if ( sock == NULL ) {
|
||||
SDLNet_SetError("Out of memory");
|
||||
goto error_return;
|
||||
}
|
||||
memset(sock, 0, sizeof(*sock));
|
||||
memset(&sock_addr, 0, sizeof(sock_addr));
|
||||
|
||||
/* Open the socket */
|
||||
sock->channel = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if ( sock->channel == INVALID_SOCKET )
|
||||
{
|
||||
SDLNet_SetError("Couldn't create socket");
|
||||
goto error_return;
|
||||
}
|
||||
|
||||
/* Bind locally, if appropriate */
|
||||
sock_addr.sin_family = AF_INET;
|
||||
sock_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
sock_addr.sin_port = SDL_SwapBE16(port);
|
||||
|
||||
/* Bind the socket for listening */
|
||||
if ( bind(sock->channel, (struct sockaddr *)&sock_addr,
|
||||
sizeof(sock_addr)) == SOCKET_ERROR ) {
|
||||
SDLNet_SetError("Couldn't bind to local port");
|
||||
goto error_return;
|
||||
}
|
||||
|
||||
/* Get the bound address and port */
|
||||
sock_len = sizeof(sock_addr);
|
||||
if ( getsockname(sock->channel, (struct sockaddr *)&sock_addr, &sock_len) < 0 ) {
|
||||
perror("getsockname");
|
||||
SDLNet_SetError("Couldn't get socket address");
|
||||
goto error_return;
|
||||
}
|
||||
|
||||
/* Fill in the channel host address */
|
||||
sock->address.host = sock_addr.sin_addr.s_addr;
|
||||
sock->address.port = sock_addr.sin_port;
|
||||
|
||||
#ifdef SO_BROADCAST
|
||||
/* Allow LAN broadcasts with the socket */
|
||||
{ int yes = 1;
|
||||
setsockopt(sock->channel, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes));
|
||||
}
|
||||
#endif
|
||||
#ifdef IP_ADD_MEMBERSHIP
|
||||
/* Receive LAN multicast packets on 224.0.0.1
|
||||
This automatically works on Mac OS X, Linux and BSD, but needs
|
||||
this code on Windows.
|
||||
*/
|
||||
/* A good description of multicast can be found here:
|
||||
http://www.docs.hp.com/en/B2355-90136/ch05s05.html
|
||||
*/
|
||||
/* FIXME: Add support for joining arbitrary groups to the API */
|
||||
{
|
||||
struct ip_mreq g;
|
||||
|
||||
g.imr_multiaddr.s_addr = inet_addr("224.0.0.1");
|
||||
g.imr_interface.s_addr = INADDR_ANY;
|
||||
setsockopt(sock->channel, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
||||
(char*)&g, sizeof(g));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* The socket is ready */
|
||||
|
||||
return(sock);
|
||||
|
||||
error_return:
|
||||
SDLNet_UDP_Close(sock);
|
||||
|
||||
return(NULL);
|
||||
}
|
||||
|
||||
void SDLNet_UDP_SetPacketLoss(UDPsocket sock, int percent)
|
||||
{
|
||||
/* FIXME: We may want this behavior to be reproducible
|
||||
but there isn't a portable reentrant random
|
||||
number generator with good randomness.
|
||||
*/
|
||||
srandom(SDL_GetTicks());
|
||||
|
||||
if (percent < 0) {
|
||||
percent = 0;
|
||||
} else if (percent > 100) {
|
||||
percent = 100;
|
||||
}
|
||||
sock->packetloss = percent;
|
||||
}
|
||||
|
||||
/* Verify that the channel is in the valid range */
|
||||
static int ValidChannel(int channel)
|
||||
{
|
||||
if ( (channel < 0) || (channel >= SDLNET_MAX_UDPCHANNELS) ) {
|
||||
SDLNet_SetError("Invalid channel");
|
||||
return(0);
|
||||
}
|
||||
return(1);
|
||||
}
|
||||
|
||||
/* Bind the address 'address' to the requested channel on the UDP socket.
|
||||
If the channel is -1, then the first unbound channel will be bound with
|
||||
the given address as it's primary address.
|
||||
If the channel is already bound, this new address will be added to the
|
||||
list of valid source addresses for packets arriving on the channel.
|
||||
If the channel is not already bound, then the address becomes the primary
|
||||
address, to which all outbound packets on the channel are sent.
|
||||
This function returns the channel which was bound, or -1 on error.
|
||||
*/
|
||||
int SDLNet_UDP_Bind(UDPsocket sock, int channel, const IPaddress *address)
|
||||
{
|
||||
struct UDP_channel *binding;
|
||||
|
||||
if ( sock == NULL ) {
|
||||
SDLNet_SetError("Passed a NULL socket");
|
||||
return(-1);
|
||||
}
|
||||
|
||||
if ( channel == -1 ) {
|
||||
for ( channel=0; channel < SDLNET_MAX_UDPCHANNELS; ++channel ) {
|
||||
binding = &sock->binding[channel];
|
||||
if ( binding->numbound < SDLNET_MAX_UDPADDRESSES ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( ! ValidChannel(channel) ) {
|
||||
return(-1);
|
||||
}
|
||||
binding = &sock->binding[channel];
|
||||
}
|
||||
if ( binding->numbound == SDLNET_MAX_UDPADDRESSES ) {
|
||||
SDLNet_SetError("No room for new addresses");
|
||||
return(-1);
|
||||
}
|
||||
binding->address[binding->numbound++] = *address;
|
||||
return(channel);
|
||||
}
|
||||
|
||||
/* Unbind all addresses from the given channel */
|
||||
void SDLNet_UDP_Unbind(UDPsocket sock, int channel)
|
||||
{
|
||||
if ( (channel >= 0) && (channel < SDLNET_MAX_UDPCHANNELS) ) {
|
||||
sock->binding[channel].numbound = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the primary IP address of the remote system associated with the
|
||||
socket and channel.
|
||||
If the channel is not bound, this function returns NULL.
|
||||
*/
|
||||
IPaddress *SDLNet_UDP_GetPeerAddress(UDPsocket sock, int channel)
|
||||
{
|
||||
IPaddress *address;
|
||||
|
||||
address = NULL;
|
||||
switch (channel) {
|
||||
case -1:
|
||||
/* Return the actual address of the socket */
|
||||
address = &sock->address;
|
||||
break;
|
||||
default:
|
||||
/* Return the address of the bound channel */
|
||||
if ( ValidChannel(channel) &&
|
||||
(sock->binding[channel].numbound > 0) ) {
|
||||
address = &sock->binding[channel].address[0];
|
||||
}
|
||||
break;
|
||||
}
|
||||
return(address);
|
||||
}
|
||||
|
||||
/* Send a vector of packets to the the channels specified within the packet.
|
||||
If the channel specified in the packet is -1, the packet will be sent to
|
||||
the address in the 'src' member of the packet.
|
||||
Each packet will be updated with the status of the packet after it has
|
||||
been sent, -1 if the packet send failed.
|
||||
This function returns the number of packets sent.
|
||||
*/
|
||||
int SDLNet_UDP_SendV(UDPsocket sock, UDPpacket **packets, int npackets)
|
||||
{
|
||||
int numsent, i, j;
|
||||
struct UDP_channel *binding;
|
||||
int status;
|
||||
int sock_len;
|
||||
struct sockaddr_in sock_addr;
|
||||
|
||||
if ( sock == NULL ) {
|
||||
SDLNet_SetError("Passed a NULL socket");
|
||||
return(0);
|
||||
}
|
||||
|
||||
/* Set up the variables to send packets */
|
||||
sock_len = sizeof(sock_addr);
|
||||
|
||||
numsent = 0;
|
||||
for ( i=0; i<npackets; ++i )
|
||||
{
|
||||
/* Simulate packet loss, if desired */
|
||||
if (sock->packetloss) {
|
||||
if ((random()%100) <= sock->packetloss) {
|
||||
packets[i]->status = packets[i]->len;
|
||||
++numsent;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* if channel is < 0, then use channel specified in sock */
|
||||
|
||||
if ( packets[i]->channel < 0 )
|
||||
{
|
||||
sock_addr.sin_addr.s_addr = packets[i]->address.host;
|
||||
sock_addr.sin_port = packets[i]->address.port;
|
||||
sock_addr.sin_family = AF_INET;
|
||||
status = sendto(sock->channel,
|
||||
packets[i]->data, packets[i]->len, 0,
|
||||
(struct sockaddr *)&sock_addr,sock_len);
|
||||
if ( status >= 0 )
|
||||
{
|
||||
packets[i]->status = status;
|
||||
++numsent;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Send to each of the bound addresses on the channel */
|
||||
#ifdef DEBUG_NET
|
||||
printf("SDLNet_UDP_SendV sending packet to channel = %d\n", packets[i]->channel );
|
||||
#endif
|
||||
|
||||
binding = &sock->binding[packets[i]->channel];
|
||||
|
||||
for ( j=binding->numbound-1; j>=0; --j )
|
||||
{
|
||||
sock_addr.sin_addr.s_addr = binding->address[j].host;
|
||||
sock_addr.sin_port = binding->address[j].port;
|
||||
sock_addr.sin_family = AF_INET;
|
||||
status = sendto(sock->channel,
|
||||
packets[i]->data, packets[i]->len, 0,
|
||||
(struct sockaddr *)&sock_addr,sock_len);
|
||||
if ( status >= 0 )
|
||||
{
|
||||
packets[i]->status = status;
|
||||
++numsent;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return(numsent);
|
||||
}
|
||||
|
||||
int SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet)
|
||||
{
|
||||
/* This is silly, but... */
|
||||
packet->channel = channel;
|
||||
return(SDLNet_UDP_SendV(sock, &packet, 1));
|
||||
}
|
||||
|
||||
/* Returns true if a socket is has data available for reading right now */
|
||||
static int SocketReady(SOCKET sock)
|
||||
{
|
||||
int retval = 0;
|
||||
struct timeval tv;
|
||||
fd_set mask;
|
||||
|
||||
/* Check the file descriptors for available data */
|
||||
do {
|
||||
SDLNet_SetLastError(0);
|
||||
|
||||
/* Set up the mask of file descriptors */
|
||||
FD_ZERO(&mask);
|
||||
FD_SET(sock, &mask);
|
||||
|
||||
/* Set up the timeout */
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
/* Look! */
|
||||
retval = select(sock+1, &mask, NULL, NULL, &tv);
|
||||
} while ( SDLNet_GetLastError() == EINTR );
|
||||
|
||||
return(retval == 1);
|
||||
}
|
||||
|
||||
/* Receive a vector of pending packets from the UDP socket.
|
||||
The returned packets contain the source address and the channel they arrived
|
||||
on. If they did not arrive on a bound channel, the the channel will be set
|
||||
to -1.
|
||||
This function returns the number of packets read from the network, or -1
|
||||
on error. This function does not block, so can return 0 packets pending.
|
||||
*/
|
||||
extern int SDLNet_UDP_RecvV(UDPsocket sock, UDPpacket **packets)
|
||||
{
|
||||
int numrecv, i, j;
|
||||
struct UDP_channel *binding;
|
||||
socklen_t sock_len;
|
||||
struct sockaddr_in sock_addr;
|
||||
|
||||
if ( sock == NULL ) {
|
||||
return(0);
|
||||
}
|
||||
|
||||
numrecv = 0;
|
||||
while ( packets[numrecv] && SocketReady(sock->channel) )
|
||||
{
|
||||
UDPpacket *packet;
|
||||
|
||||
packet = packets[numrecv];
|
||||
|
||||
sock_len = sizeof(sock_addr);
|
||||
packet->status = recvfrom(sock->channel,
|
||||
packet->data, packet->maxlen, 0,
|
||||
(struct sockaddr *)&sock_addr,
|
||||
&sock_len);
|
||||
if ( packet->status >= 0 ) {
|
||||
packet->len = packet->status;
|
||||
packet->address.host = sock_addr.sin_addr.s_addr;
|
||||
packet->address.port = sock_addr.sin_port;
|
||||
packet->channel = -1;
|
||||
|
||||
for (i=(SDLNET_MAX_UDPCHANNELS-1); i>=0; --i )
|
||||
{
|
||||
binding = &sock->binding[i];
|
||||
|
||||
for ( j=binding->numbound-1; j>=0; --j )
|
||||
{
|
||||
if ( (packet->address.host == binding->address[j].host) &&
|
||||
(packet->address.port == binding->address[j].port) )
|
||||
{
|
||||
packet->channel = i;
|
||||
goto foundit; /* break twice */
|
||||
}
|
||||
}
|
||||
}
|
||||
foundit:
|
||||
++numrecv;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
packet->len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
sock->ready = 0;
|
||||
|
||||
return(numrecv);
|
||||
}
|
||||
|
||||
/* Receive a single packet from the UDP socket.
|
||||
The returned packet contains the source address and the channel it arrived
|
||||
on. If it did not arrive on a bound channel, the the channel will be set
|
||||
to -1.
|
||||
This function returns the number of packets read from the network, or -1
|
||||
on error. This function does not block, so can return 0 packets pending.
|
||||
*/
|
||||
int SDLNet_UDP_Recv(UDPsocket sock, UDPpacket *packet)
|
||||
{
|
||||
UDPpacket *packets[2];
|
||||
|
||||
/* Receive a packet array of 1 */
|
||||
packets[0] = packet;
|
||||
packets[1] = NULL;
|
||||
return(SDLNet_UDP_RecvV(sock, packets));
|
||||
}
|
||||
|
||||
/* Close a UDP network socket */
|
||||
extern void SDLNet_UDP_Close(UDPsocket sock)
|
||||
{
|
||||
if ( sock != NULL )
|
||||
{
|
||||
if ( sock->channel != INVALID_SOCKET )
|
||||
{
|
||||
closesocket(sock->channel);
|
||||
}
|
||||
|
||||
free(sock);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
/*
|
||||
SDL_net: An example cross-platform network library for use with SDL
|
||||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
#include "SDLnetsys.h"
|
||||
#include "SDL_net.h"
|
||||
|
||||
/* The select() API for network sockets */
|
||||
|
||||
struct SDLNet_Socket {
|
||||
int ready;
|
||||
SOCKET channel;
|
||||
};
|
||||
|
||||
struct _SDLNet_SocketSet {
|
||||
int numsockets;
|
||||
int maxsockets;
|
||||
struct SDLNet_Socket **sockets;
|
||||
};
|
||||
|
||||
/* Allocate a socket set for use with SDLNet_CheckSockets()
|
||||
This returns a socket set for up to 'maxsockets' sockets, or NULL if
|
||||
the function ran out of memory.
|
||||
*/
|
||||
SDLNet_SocketSet SDLNet_AllocSocketSet(int maxsockets)
|
||||
{
|
||||
struct _SDLNet_SocketSet *set;
|
||||
int i;
|
||||
|
||||
set = (struct _SDLNet_SocketSet *)malloc(sizeof(*set));
|
||||
if ( set != NULL ) {
|
||||
set->numsockets = 0;
|
||||
set->maxsockets = maxsockets;
|
||||
set->sockets = (struct SDLNet_Socket **)malloc
|
||||
(maxsockets*sizeof(*set->sockets));
|
||||
if ( set->sockets != NULL ) {
|
||||
for ( i=0; i<maxsockets; ++i ) {
|
||||
set->sockets[i] = NULL;
|
||||
}
|
||||
} else {
|
||||
free(set);
|
||||
set = NULL;
|
||||
}
|
||||
}
|
||||
return(set);
|
||||
}
|
||||
|
||||
/* Add a socket to a set of sockets to be checked for available data */
|
||||
int SDLNet_AddSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock)
|
||||
{
|
||||
if ( sock != NULL ) {
|
||||
if ( set->numsockets == set->maxsockets ) {
|
||||
SDLNet_SetError("socketset is full");
|
||||
return(-1);
|
||||
}
|
||||
set->sockets[set->numsockets++] = (struct SDLNet_Socket *)sock;
|
||||
}
|
||||
return(set->numsockets);
|
||||
}
|
||||
|
||||
/* Remove a socket from a set of sockets to be checked for available data */
|
||||
int SDLNet_DelSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock)
|
||||
{
|
||||
int i;
|
||||
|
||||
if ( sock != NULL ) {
|
||||
for ( i=0; i<set->numsockets; ++i ) {
|
||||
if ( set->sockets[i] == (struct SDLNet_Socket *)sock ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( i == set->numsockets ) {
|
||||
SDLNet_SetError("socket not found in socketset");
|
||||
return(-1);
|
||||
}
|
||||
--set->numsockets;
|
||||
for ( ; i<set->numsockets; ++i ) {
|
||||
set->sockets[i] = set->sockets[i+1];
|
||||
}
|
||||
}
|
||||
return(set->numsockets);
|
||||
}
|
||||
|
||||
/* This function checks to see if data is available for reading on the
|
||||
given set of sockets. If 'timeout' is 0, it performs a quick poll,
|
||||
otherwise the function returns when either data is available for
|
||||
reading, or the timeout in milliseconds has elapsed, which ever occurs
|
||||
first. This function returns the number of sockets ready for reading,
|
||||
or -1 if there was an error with the select() system call.
|
||||
*/
|
||||
int SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout)
|
||||
{
|
||||
int i;
|
||||
SOCKET maxfd;
|
||||
int retval;
|
||||
struct timeval tv;
|
||||
fd_set mask;
|
||||
|
||||
/* Find the largest file descriptor */
|
||||
maxfd = 0;
|
||||
for ( i=set->numsockets-1; i>=0; --i ) {
|
||||
if ( set->sockets[i]->channel > maxfd ) {
|
||||
maxfd = set->sockets[i]->channel;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check the file descriptors for available data */
|
||||
do {
|
||||
SDLNet_SetLastError(0);
|
||||
|
||||
/* Set up the mask of file descriptors */
|
||||
FD_ZERO(&mask);
|
||||
for ( i=set->numsockets-1; i>=0; --i ) {
|
||||
FD_SET(set->sockets[i]->channel, &mask);
|
||||
}
|
||||
|
||||
/* Set up the timeout */
|
||||
tv.tv_sec = timeout/1000;
|
||||
tv.tv_usec = (timeout%1000)*1000;
|
||||
|
||||
/* Look! */
|
||||
retval = select(maxfd+1, &mask, NULL, NULL, &tv);
|
||||
} while ( SDLNet_GetLastError() == EINTR );
|
||||
|
||||
/* Mark all file descriptors ready that have data available */
|
||||
if ( retval > 0 ) {
|
||||
for ( i=set->numsockets-1; i>=0; --i ) {
|
||||
if ( FD_ISSET(set->sockets[i]->channel, &mask) ) {
|
||||
set->sockets[i]->ready = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return(retval);
|
||||
}
|
||||
|
||||
/* Free a set of sockets allocated by SDL_NetAllocSocketSet() */
|
||||
extern void SDLNet_FreeSocketSet(SDLNet_SocketSet set)
|
||||
{
|
||||
if ( set ) {
|
||||
free(set->sockets);
|
||||
free(set);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
SDL_net: An example cross-platform network library for use with SDL
|
||||
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
/* $Id$ */
|
||||
|
||||
/* Include normal system headers */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef _WIN32_WCE
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
/* Include system network headers */
|
||||
#if defined(__WIN32__) || defined(WIN32)
|
||||
#define __USE_W32_SOCKETS
|
||||
#ifdef _WIN64
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <winsock.h>
|
||||
/* NOTE: windows socklen_t is signed
|
||||
* and is defined only for winsock2. */
|
||||
typedef int socklen_t;
|
||||
#endif /* W64 */
|
||||
#include <iphlpapi.h>
|
||||
#else /* UNIX */
|
||||
#include <sys/types.h>
|
||||
#ifdef __FreeBSD__
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#ifndef __BEOS__
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
#ifdef linux /* FIXME: what other platforms have this? */
|
||||
#include <netinet/tcp.h>
|
||||
#endif
|
||||
#include <sys/socket.h>
|
||||
#include <net/if.h>
|
||||
#include <netdb.h>
|
||||
#endif /* WIN32 */
|
||||
|
||||
/* FIXME: What platforms need this? */
|
||||
#if 0
|
||||
typedef Uint32 socklen_t;
|
||||
#endif
|
||||
|
||||
/* System-dependent definitions */
|
||||
#ifndef __USE_W32_SOCKETS
|
||||
#ifdef __OS2__
|
||||
#define closesocket soclose
|
||||
#else /* !__OS2__ */
|
||||
#define closesocket close
|
||||
#endif /* __OS2__ */
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET -1
|
||||
#define SOCKET_ERROR -1
|
||||
#endif /* __USE_W32_SOCKETS */
|
||||
|
||||
#ifdef __USE_W32_SOCKETS
|
||||
#define SDLNet_GetLastError WSAGetLastError
|
||||
#define SDLNet_SetLastError WSASetLastError
|
||||
#ifndef EINTR
|
||||
#define EINTR WSAEINTR
|
||||
#endif
|
||||
#else
|
||||
int SDLNet_GetLastError(void);
|
||||
void SDLNet_SetLastError(int err);
|
||||
#endif
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
#config
|
||||
LIBDIR=..
|
||||
LOCALLIBS=
|
||||
LIB=
|
||||
INCLUDES=
|
||||
CFLAGS+=$(addprefix -I,$(INCLUDES))
|
||||
|
||||
#source
|
||||
SRC=$(wildcard *.c)
|
||||
|
||||
#objects
|
||||
OBJDIR=obj
|
||||
OBJ=$(addprefix $(OBJDIR)/,$(SRC:.c=.o))
|
||||
|
||||
#output
|
||||
OUTDIR=../out
|
||||
OUT=$(addprefix $(OUTDIR)/,libSDL_net.a)
|
||||
|
||||
#targets
|
||||
all: $(OBJ) $(OUT)
|
||||
ar -crs $(OUT) $(OBJ)
|
||||
|
||||
$(OBJ): | $(OBJDIR)
|
||||
|
||||
$(OUT): | $(OUTDIR)
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -1,110 +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 "network_queue.hpp"
|
||||
#include "service_locator.hpp"
|
||||
|
||||
#include "udp_network_utility.hpp"
|
||||
|
||||
#include "SDL/SDL_thread.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <deque>
|
||||
|
||||
static SDL_sem* lock = SDL_CreateSemaphore(1);
|
||||
static SDL_Thread* queueThread = nullptr;
|
||||
|
||||
static std::deque<Packet> queue;
|
||||
|
||||
static bool running = false;
|
||||
|
||||
static int networkQueue(void*) {
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
while(running) {
|
||||
SDL_SemWait(lock);
|
||||
while(netUtil->Receive()) {
|
||||
Packet p;
|
||||
memcpy(&p, netUtil->GetInData(), sizeof(Packet));
|
||||
p.meta.address = netUtil->GetInPacket()->address;
|
||||
queue.push_back(p);
|
||||
}
|
||||
SDL_SemPost(lock);
|
||||
SDL_Delay(10);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void beginQueueThread() {
|
||||
if (running) {
|
||||
return;
|
||||
}
|
||||
running = true;
|
||||
if (!(queueThread = SDL_CreateThread(networkQueue, nullptr))) {
|
||||
throw(std::runtime_error("Failed to create the network thread"));
|
||||
}
|
||||
}
|
||||
|
||||
void endQueueThread() {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
SDL_WaitThread(queueThread, nullptr);
|
||||
queueThread = nullptr;
|
||||
}
|
||||
|
||||
void killQueueThread() {
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
running = false;
|
||||
SDL_KillThread(queueThread);
|
||||
queueThread = nullptr;
|
||||
}
|
||||
|
||||
Packet peekNetworkPacket() {
|
||||
SDL_SemWait(lock);
|
||||
Packet p;
|
||||
if (queue.size() > 0) {
|
||||
Packet p = queue[0];
|
||||
}
|
||||
SDL_SemPost(lock);
|
||||
return p;
|
||||
}
|
||||
|
||||
Packet popNetworkPacket() {
|
||||
SDL_SemWait(lock);
|
||||
Packet p;
|
||||
if (queue.size() > 0) {
|
||||
p = queue[0];
|
||||
queue.pop_front();
|
||||
}
|
||||
SDL_SemPost(lock);
|
||||
return p;
|
||||
}
|
||||
|
||||
void flushNetworkQueue() {
|
||||
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
|
||||
SDL_SemWait(lock);
|
||||
while(netUtil->Receive());
|
||||
queue.clear();
|
||||
SDL_SemPost(lock);
|
||||
}
|
||||
@@ -1,138 +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 PACKETTYPE_HPP_
|
||||
#define PACKETTYPE_HPP_
|
||||
|
||||
#include "vector2.hpp"
|
||||
|
||||
#include "SDL_net/SDL_net.h"
|
||||
|
||||
#define PACKET_STRING_SIZE 100
|
||||
|
||||
#pragma pack(push, 0)
|
||||
|
||||
enum class PacketType {
|
||||
NONE = 0,
|
||||
|
||||
PING = 1,
|
||||
PONG = 2,
|
||||
BROADCAST_REQUEST = 3,
|
||||
BROADCAST_RESPONSE = 4,
|
||||
JOIN_REQUEST = 5,
|
||||
JOIN_RESPONSE = 6,
|
||||
DISCONNECT = 7,
|
||||
|
||||
SYNCHRONIZE = 8,
|
||||
|
||||
PLAYER_NEW = 9,
|
||||
PLAYER_DELETE = 10,
|
||||
PLAYER_MOVE = 11,
|
||||
};
|
||||
|
||||
struct Metadata {
|
||||
PacketType type;
|
||||
IPaddress address;
|
||||
};
|
||||
|
||||
struct Ping {
|
||||
Metadata meta;
|
||||
};
|
||||
|
||||
struct Pong {
|
||||
Metadata meta;
|
||||
};
|
||||
|
||||
struct BroadcastRequest {
|
||||
Metadata meta;
|
||||
};
|
||||
|
||||
struct BroadcastResponse {
|
||||
Metadata meta;
|
||||
char name[PACKET_STRING_SIZE];
|
||||
//TODO: version
|
||||
};
|
||||
|
||||
struct JoinRequest {
|
||||
Metadata meta;
|
||||
};
|
||||
|
||||
struct JoinResponse {
|
||||
Metadata meta;
|
||||
int clientIndex;
|
||||
//resource list
|
||||
};
|
||||
|
||||
struct Disconnect {
|
||||
Metadata meta;
|
||||
int clientIndex;
|
||||
};
|
||||
|
||||
struct Synchronize {
|
||||
Metadata meta;
|
||||
};
|
||||
|
||||
struct PlayerNew {
|
||||
Metadata meta;
|
||||
int playerIndex;
|
||||
//TODO Playerdata
|
||||
};
|
||||
|
||||
struct PlayerDelete {
|
||||
Metadata meta;
|
||||
int playerIndex;
|
||||
};
|
||||
|
||||
struct PlayerMove {
|
||||
Metadata meta;
|
||||
int playerIndex;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
};
|
||||
|
||||
union Packet {
|
||||
Packet() {
|
||||
meta.type = PacketType::NONE;
|
||||
meta.address.host = 0;
|
||||
meta.address.port = 0;
|
||||
};
|
||||
Metadata meta;
|
||||
|
||||
Ping ping;
|
||||
Pong pong;
|
||||
BroadcastRequest broadcastRequest;
|
||||
BroadcastResponse broadcastResponse;
|
||||
JoinRequest joinRequest;
|
||||
JoinResponse joinResponse;
|
||||
Disconnect disconnect;
|
||||
|
||||
PlayerNew playerNew;
|
||||
PlayerDelete playerDelete;
|
||||
PlayerMove playerMove;
|
||||
|
||||
#ifdef DEBUG
|
||||
char buffer[1024];
|
||||
#endif
|
||||
};
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#endif
|
||||
@@ -1,14 +0,0 @@
|
||||
OUTDIR=out
|
||||
|
||||
all: $(OUTDIR)
|
||||
$(MAKE) -C SDL_net
|
||||
$(MAKE) -C codebase
|
||||
$(MAKE) -C common
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
clean:
|
||||
$(RM) *.o *.a *.exe
|
||||
|
||||
rebuild: clean all
|
||||
@@ -1,10 +1,21 @@
|
||||
#TODO: The build process needs revising
|
||||
#for use on Windows:
|
||||
|
||||
#MKDIR=mkdir
|
||||
#RM=del /y
|
||||
|
||||
CXXFLAGS+=-static-libgcc -static-libstdc++
|
||||
CFLAGS+=-static-libgcc
|
||||
|
||||
export
|
||||
|
||||
OUTDIR=out
|
||||
|
||||
all: $(OUTDIR)
|
||||
$(MAKE) -C libs
|
||||
$(MAKE) -C common
|
||||
$(MAKE) -C server
|
||||
$(MAKE) -C client
|
||||
$(MAKE) -C test
|
||||
$(MAKE) -C editor
|
||||
|
||||
$(OUTDIR):
|
||||
mkdir $(OUTDIR)
|
||||
|
||||
+21
-11
@@ -1,20 +1,30 @@
|
||||
#configuration of the programs
|
||||
server.host = 127.0.0.1
|
||||
server.port = 1991
|
||||
server.name = foobar
|
||||
server.host = 255.255.255.255
|
||||
server.port = 21795
|
||||
server.name = local
|
||||
|
||||
server.dbname = database.db
|
||||
|
||||
screen.w = 800
|
||||
screen.h = 600
|
||||
screen.f = false
|
||||
|
||||
#directories
|
||||
fonts = rsc/graphics/fonts
|
||||
logos = rsc/graphics/logos
|
||||
sprites = rsc/graphics/sprites
|
||||
tilesets = rsc/graphics/tilesets
|
||||
interface = rsc/graphics/interface
|
||||
dir.fonts = rsc/graphics/fonts/
|
||||
dir.logos = rsc/graphics/logos/
|
||||
dir.sprites = rsc/graphics/sprites/
|
||||
dir.tilesets = rsc/graphics/tilesets/
|
||||
dir.interface = rsc/graphics/interface/
|
||||
dir.scripts = rsc/scripts/
|
||||
|
||||
#map system
|
||||
map.pager.width = 20
|
||||
map.pager.height = 20
|
||||
map.pager.depth = 3
|
||||
|
||||
#player options
|
||||
client.username = Kayne Ruse
|
||||
client.handle = Ratstail91
|
||||
client.avatar = elliot2.bmp
|
||||
|
||||
#debugging
|
||||
debug = true
|
||||
avatar = elliot2.bmp
|
||||
handle = UserName
|
||||
|
||||
|
Before Width: | Height: | Size: 65 KiB After Width: | Height: | Size: 65 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 88 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user