lobby_server.cpp compiles

This commit is contained in:
Kayne Ruse
2014-07-31 15:58:56 +10:00
parent 35363c05c3
commit 5868ec6a79
2 changed files with 63 additions and 34 deletions
+57 -23
View File
@@ -30,21 +30,25 @@
//Public access members //Public access members
//------------------------- //-------------------------
LobbyMenu::LobbyMenu( LobbyMenu::LobbyMenu(lua_State* L, UDPNetworkUtility& aNetwork):
ConfigUtility* const argConfig, lua(L),
UDPNetworkUtility* const argNetwork, network(aNetwork)
int* const argClientIndex,
int* const argAccountIndex
):
config(*argConfig),
network(*argNetwork),
clientIndex(*argClientIndex),
accountIndex(*argAccountIndex)
{ {
//get the config info
lua_getglobal(lua, "config");
lua_getfield(lua, -1, "dir");
lua_getfield(lua, -1, "interface");
lua_getfield(lua, -2, "fonts");
std::string interfaceDir = lua_tostring(lua, -2);
std::string fontsDir = lua_tostring(lua, -1);
lua_pop(lua, 4);
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); image.LoadSurface(interfaceDir + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3); image.SetClipH(image.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); font.LoadSurface(fontsDir + "pk_white_8.bmp");
//pass the utility objects //pass the utility objects
search.SetImage(&image); search.SetImage(&image);
@@ -88,11 +92,11 @@ void LobbyMenu::FrameStart() {
void LobbyMenu::Update(double delta) { void LobbyMenu::Update(double delta) {
//suck in and process all waiting packets //suck in and process all waiting packets
SerialPacket* packetBuffer = static_cast<SerialPacket*>(malloc(MAX_PACKET_SIZE)); SerialPacket* packetBuffer = new SerialPacket();
while(network.Receive(packetBuffer)) { while(network.Receive(packetBuffer)) {
HandlePacket(packetBuffer); HandlePacket(packetBuffer);
} }
free(static_cast<void*>(packetBuffer)); delete packetBuffer;
} }
void LobbyMenu::FrameEnd() { void LobbyMenu::FrameEnd() {
@@ -148,26 +152,44 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
} }
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
//prep
lua_getglobal(lua, "config");
if (search.MouseButtonUp(button) == Button::State::HOVER) { if (search.MouseButtonUp(button) == Button::State::HOVER) {
//get the set parameters
lua_getfield(lua, -1, "server");
lua_getfield(lua, -1, "host");
lua_getfield(lua, -2, "port");
//broadcast to the network, or a specific server //broadcast to the network, or a specific server
SerialPacket packet; SerialPacket packet;
packet.type = SerialPacketType::BROADCAST_REQUEST; packet.type = SerialPacketType::BROADCAST_REQUEST;
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); network.SendTo(lua_tostring(lua, -2), lua_tointeger(lua, -1), &packet);
//reset the server list //reset the server list
serverInfo.clear(); serverInfo.clear();
selection = nullptr; selection = nullptr;
//clear the parameters
lua_pop(lua, 3);
} }
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) { else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
//get the parameters
lua_getfield(lua, -1, "client");
lua_getfield(lua, -1, "username");
//pack the packet //pack the packet
ClientPacket packet; ClientPacket packet;
packet.type = SerialPacketType::JOIN_REQUEST; packet.type = SerialPacketType::JOIN_REQUEST;
strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE); strncpy(packet.username, lua_tostring(lua, -1), PACKET_STRING_SIZE);
//join the selected server //join the selected server
network.SendTo(&selection->address, &packet); network.SendTo(&selection->address, &packet);
selection = nullptr; selection = nullptr;
//clear the parameters
lua_pop(lua, 2);
} }
else if (back.MouseButtonUp(button) == Button::State::HOVER) { else if (back.MouseButtonUp(button) == Button::State::HOVER) {
@@ -187,6 +209,9 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
else { else {
selection = nullptr; selection = nullptr;
} }
//clear the parameters
lua_pop(lua, 1);
} }
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) { void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) {
@@ -204,10 +229,10 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
void LobbyMenu::HandlePacket(SerialPacket* const argPacket) { void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
switch(argPacket->type) { switch(argPacket->type) {
case SerialPacketType::BROADCAST_RESPONSE: case SerialPacketType::BROADCAST_RESPONSE:
HandleBroadcastResponse(static_cast<ServerPacket*>(argPacket)); HandleBroadcastResponse(dynamic_cast<ServerPacket*>(argPacket));
break; break;
case SerialPacketType::JOIN_RESPONSE: case SerialPacketType::JOIN_RESPONSE:
HandleJoinResponse(static_cast<ClientPacket*>(argPacket)); HandleJoinResponse(dynamic_cast<ClientPacket*>(argPacket));
break; break;
//handle errors //handle errors
default: default:
@@ -232,16 +257,25 @@ void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
} }
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
clientIndex = argPacket->clientIndex; lua_getglobal(lua, "config");
accountIndex = argPacket->accountIndex; lua_getfield(lua, -1, "client");
lua_getfield(lua, -1, "handle");
lua_getfield(lua, -2, "avatar");
lua_pushinteger(lua, argPacket->clientIndex);
lua_pushinteger(lua, argPacket->accountIndex);
lua_setfield(lua, -5, "accountIndex");
lua_setfield(lua, -4, "clientIndex");
network.Bind(&argPacket->srcAddress, Channels::SERVER); network.Bind(&argPacket->srcAddress, Channels::SERVER);
SetNextScene(SceneList::INWORLD); SetNextScene(SceneList::INWORLD);
//send this player's character info //send this player's character info
CharacterPacket newPacket; CharacterPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_NEW; newPacket.type = SerialPacketType::CHARACTER_NEW;
strncpy(newPacket.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE); strncpy(newPacket.handle, lua_tostring(lua, -2), PACKET_STRING_SIZE);
strncpy(newPacket.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE); strncpy(newPacket.avatar, lua_tostring(lua, -1), PACKET_STRING_SIZE);
newPacket.accountIndex = accountIndex; newPacket.accountIndex = argPacket->accountIndex;
network.SendTo(Channels::SERVER, &newPacket); network.SendTo(Channels::SERVER, &newPacket);
lua_pop(lua, 4);
} }
+6 -11
View File
@@ -28,24 +28,21 @@
#include "button.hpp" #include "button.hpp"
//utilities //utilities
#include "config_utility.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
//client //client
#include "base_scene.hpp" #include "base_scene.hpp"
//APIs
#include "lua/lua.hpp"
//STL //STL
#include <vector> #include <vector>
class LobbyMenu : public BaseScene { class LobbyMenu : public BaseScene {
public: public:
//Public access members //Public access members
LobbyMenu( LobbyMenu(lua_State*, UDPNetworkUtility&);
ConfigUtility* const argConfig,
UDPNetworkUtility* const argNetwork,
int* const argClientIndex,
int* const argAccountIndex
);
~LobbyMenu(); ~LobbyMenu();
protected: protected:
@@ -68,10 +65,8 @@ protected:
void HandleJoinResponse(ClientPacket* const); void HandleJoinResponse(ClientPacket* const);
//shared parameters //shared parameters
ConfigUtility& config; lua_State* lua = nullptr;
UDPNetworkUtility& network; UDPNetworkUtility& network;
int& clientIndex;
int& accountIndex;
//members //members
Image image; Image image;
@@ -92,7 +87,7 @@ protected:
std::vector<ServerInformation> serverInfo; std::vector<ServerInformation> serverInfo;
//a terrible hack, forgive me //a terrible hack, forgive me
//I'd love a proper gui system for this //TODO: I'd love a proper gui system for this
SDL_Rect listBox; SDL_Rect listBox;
ServerInformation* selection = nullptr; ServerInformation* selection = nullptr;
}; };