Implemented the log on and log off systems
This is a pretty straight forward port of the old version, including the incredibly hacky server list. But I just need to remember that this is a prototype.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
#ifndef CHANNELS_HPP_
|
||||
#define CHANNELS_HPP_
|
||||
|
||||
enum Channels {
|
||||
SERVER = 0
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -135,10 +135,10 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
|
||||
activeScene = new OptionsMenu(&config);
|
||||
break;
|
||||
case SceneList::LOBBYMENU:
|
||||
activeScene = new LobbyMenu(&config, &network);
|
||||
activeScene = new LobbyMenu(&config, &network, &clientIndex);
|
||||
break;
|
||||
case SceneList::INWORLD:
|
||||
activeScene = new InWorld();
|
||||
activeScene = new InWorld(&config, &network, &clientIndex);
|
||||
break;
|
||||
case SceneList::INCOMBAT:
|
||||
activeScene = new InCombat();
|
||||
|
||||
@@ -51,6 +51,7 @@ private:
|
||||
|
||||
ConfigUtility config;
|
||||
UDPNetworkUtility network;
|
||||
int clientIndex = -1; //replace with a struct?
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+78
-7
@@ -21,12 +21,39 @@
|
||||
*/
|
||||
#include "in_world.hpp"
|
||||
|
||||
#include "channels.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
InWorld::InWorld() {
|
||||
//
|
||||
InWorld::InWorld(ConfigUtility* const arg1, UDPNetworkUtility* const arg2, int* const arg3):
|
||||
config(*arg1),
|
||||
network(*arg2),
|
||||
clientIndex(*arg3)
|
||||
{
|
||||
//setup the utility objects
|
||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
image.SetClipH(image.GetClipH()/3);
|
||||
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
|
||||
|
||||
//pass the utility objects
|
||||
disconnectButton.SetImage(&image);
|
||||
disconnectButton.SetFont(&font);
|
||||
shutDownButton.SetImage(&image);
|
||||
shutDownButton.SetFont(&font);
|
||||
|
||||
//set the button positions
|
||||
disconnectButton.SetX(50);
|
||||
disconnectButton.SetY(50 + image.GetClipH() * 0);
|
||||
shutDownButton.SetX(50);
|
||||
shutDownButton.SetY(50 + image.GetClipH() * 1);
|
||||
|
||||
//set the button texts
|
||||
disconnectButton.SetText("Disconnect");
|
||||
shutDownButton.SetText("Shut Down");
|
||||
}
|
||||
|
||||
InWorld::~InWorld() {
|
||||
@@ -42,7 +69,13 @@ void InWorld::FrameStart() {
|
||||
}
|
||||
|
||||
void InWorld::Update(double delta) {
|
||||
//
|
||||
//suck in all waiting packets
|
||||
NetworkPacket packet;
|
||||
while(network.Receive()) {
|
||||
memcpy(&packet, network.GetInData(), sizeof(NetworkPacket));
|
||||
packet.meta.srcAddress = network.GetInPacket()->address;
|
||||
HandlePacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::FrameEnd() {
|
||||
@@ -50,7 +83,8 @@ void InWorld::FrameEnd() {
|
||||
}
|
||||
|
||||
void InWorld::Render(SDL_Surface* const screen) {
|
||||
//
|
||||
disconnectButton.DrawTo(screen);
|
||||
shutDownButton.DrawTo(screen);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
@@ -58,20 +92,39 @@ void InWorld::Render(SDL_Surface* const screen) {
|
||||
//-------------------------
|
||||
|
||||
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) {
|
||||
//send a disconnect request
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::DISCONNECT;
|
||||
packet.clientInfo.index = clientIndex;
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
}
|
||||
if (shutDownButton.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
//send a shutdown request
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::SHUTDOWN;
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
}
|
||||
}
|
||||
|
||||
void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
switch(key.keysym.sym) {
|
||||
case SDLK_ESCAPE:
|
||||
//send a disconnect request
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::DISCONNECT;
|
||||
packet.clientInfo.index = clientIndex;
|
||||
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
|
||||
QuitEvent();
|
||||
break;
|
||||
}
|
||||
@@ -80,3 +133,21 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
|
||||
void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
//
|
||||
}
|
||||
|
||||
void InWorld::HandlePacket(NetworkPacket packet) {
|
||||
switch(packet.meta.type) {
|
||||
case NetworkPacket::Type::DISCONNECT:
|
||||
network.Unbind(Channels::SERVER);
|
||||
clientIndex = -1;
|
||||
SetNextScene(SceneList::MAINMENU);
|
||||
break;
|
||||
case NetworkPacket::Type::SYNCHRONIZE:
|
||||
//TODO
|
||||
break;
|
||||
|
||||
//handle errors
|
||||
default:
|
||||
throw(std::runtime_error("Unknown NetworkPacket::Type encountered"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
+21
-1
@@ -24,10 +24,17 @@
|
||||
|
||||
#include "base_scene.hpp"
|
||||
|
||||
#include "config_utility.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "network_packet.hpp"
|
||||
#include "image.hpp"
|
||||
#include "raster_font.hpp"
|
||||
#include "button.hpp"
|
||||
|
||||
class InWorld : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
InWorld();
|
||||
InWorld(ConfigUtility* const, UDPNetworkUtility* const, int* const);
|
||||
~InWorld();
|
||||
|
||||
protected:
|
||||
@@ -43,6 +50,19 @@ protected:
|
||||
void MouseButtonUp(SDL_MouseButtonEvent const&);
|
||||
void KeyDown(SDL_KeyboardEvent const&);
|
||||
void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
void HandlePacket(NetworkPacket);
|
||||
|
||||
//global
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
|
||||
//members
|
||||
Image image;
|
||||
RasterFont font;
|
||||
Button disconnectButton;
|
||||
Button shutDownButton;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+42
-8
@@ -21,15 +21,18 @@
|
||||
*/
|
||||
#include "lobby_menu.hpp"
|
||||
|
||||
#include "channels.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
//-------------------------
|
||||
//Public access members
|
||||
//-------------------------
|
||||
|
||||
LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2):
|
||||
LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2, int* const arg3):
|
||||
config(*arg1),
|
||||
network(*arg2)
|
||||
network(*arg2),
|
||||
clientIndex(*arg3)
|
||||
{
|
||||
//setup the utility objects
|
||||
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
|
||||
@@ -56,6 +59,9 @@ LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2):
|
||||
search.SetText("Search");
|
||||
join.SetText("Join");
|
||||
back.SetText("Back");
|
||||
|
||||
//set the server list's position
|
||||
listBox = {300, 50, 200, font.GetCharH()};
|
||||
}
|
||||
|
||||
LobbyMenu::~LobbyMenu() {
|
||||
@@ -89,7 +95,15 @@ void LobbyMenu::Render(SDL_Surface* const screen) {
|
||||
join.DrawTo(screen);
|
||||
back.DrawTo(screen);
|
||||
for (int i = 0; i < serverInfo.size(); i++) {
|
||||
font.DrawStringTo(serverInfo[i].name, screen, 300, 50 + i*font.GetCharH());
|
||||
//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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,15 +132,33 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
|
||||
|
||||
//reset the server list
|
||||
serverInfo.clear();
|
||||
selection = nullptr;
|
||||
}
|
||||
|
||||
if (join.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
//TODO: join the selected server
|
||||
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr) {
|
||||
//join the selected server
|
||||
NetworkPacket packet;
|
||||
packet.meta.type = NetworkPacket::Type::JOIN_REQUEST;
|
||||
network.Send(&selection->address, &packet, sizeof(NetworkPacket));
|
||||
selection = nullptr;
|
||||
}
|
||||
|
||||
if (back.MouseButtonUp(button) == Button::State::HOVER) {
|
||||
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) {
|
||||
@@ -144,14 +176,16 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) {
|
||||
void LobbyMenu::HandlePacket(NetworkPacket packet) {
|
||||
switch(packet.meta.type) {
|
||||
case NetworkPacket::Type::BROADCAST_RESPONSE: {
|
||||
ServerInfo server;
|
||||
ServerInformation server;
|
||||
server.name = packet.serverInfo.name;
|
||||
server.address = packet.meta.srcAddress;
|
||||
serverInfo.push_back(server);
|
||||
}
|
||||
break;
|
||||
case NetworkPacket::Type::JOIN_RESPONSE:
|
||||
//
|
||||
clientIndex = packet.clientInfo.index;
|
||||
network.Bind(&packet.meta.srcAddress, Channels::SERVER);
|
||||
SetNextScene(SceneList::INWORLD);
|
||||
break;
|
||||
|
||||
//handle errors
|
||||
|
||||
+10
-3
@@ -37,7 +37,7 @@
|
||||
class LobbyMenu : public BaseScene {
|
||||
public:
|
||||
//Public access members
|
||||
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const);
|
||||
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const, int* const);
|
||||
~LobbyMenu();
|
||||
|
||||
protected:
|
||||
@@ -59,6 +59,7 @@ protected:
|
||||
//global
|
||||
ConfigUtility& config;
|
||||
UDPNetworkUtility& network;
|
||||
int& clientIndex;
|
||||
|
||||
//members
|
||||
Image image;
|
||||
@@ -67,12 +68,18 @@ protected:
|
||||
Button join;
|
||||
Button back;
|
||||
|
||||
struct ServerInfo {
|
||||
//server list
|
||||
struct ServerInformation {
|
||||
std::string name;
|
||||
IPaddress address;
|
||||
};
|
||||
|
||||
std::vector<ServerInfo> serverInfo;
|
||||
std::vector<ServerInformation> serverInfo;
|
||||
|
||||
//a terrible hack, forgive me
|
||||
//I'd love a proper gui system for this
|
||||
SDL_Rect listBox;
|
||||
ServerInformation* selection = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user