Drafted a basic networking protocol, and hooked a button up to the network broadcast
This commit is contained in:
+14
-7
@@ -33,10 +33,7 @@ Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontS
|
||||
y = j;
|
||||
state = State::NORMAL;
|
||||
|
||||
//graphical stuff
|
||||
image.SetSurface(imageSurface);
|
||||
image.SetClipH(image.GetClipH() / 3); //3 phases
|
||||
font.SetSurface(fontSurface);
|
||||
SetSurfaces(imageSurface, fontSurface);
|
||||
|
||||
SetText(s);
|
||||
}
|
||||
@@ -67,14 +64,24 @@ 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, textY);
|
||||
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 + x) - (font.GetClipW() * text.size() / 2);
|
||||
textY = (image.GetClipH() / 2 + y) - (font.GetClipH() / 2);
|
||||
textX = (image.GetClipW() / 2) - (font.GetClipW() * text.size() / 2);
|
||||
textY = (image.GetClipH() / 2) - (font.GetClipH() / 2);
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,8 @@ public:
|
||||
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; }
|
||||
|
||||
|
||||
+30
-25
@@ -5,34 +5,35 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#define PACKET_STRING_SIZE 256
|
||||
#define PACKET_STRING_SIZE 100
|
||||
|
||||
enum class PacketList {
|
||||
//networking systems
|
||||
PING, PONG,
|
||||
JOINREQUEST,
|
||||
JOINCONFIRM,
|
||||
NONE,
|
||||
|
||||
//connections
|
||||
PING,
|
||||
PONG,
|
||||
JOINREQUEST,
|
||||
JOINCONFIRM,
|
||||
DISCONNECT,
|
||||
|
||||
//player controls
|
||||
NEWPLAYER,
|
||||
DELETEPLAYER,
|
||||
|
||||
//play updates
|
||||
MOTIONUPDATE,
|
||||
MOVEMENT,
|
||||
};
|
||||
|
||||
//-------------------------
|
||||
//networking systems
|
||||
//connections
|
||||
//-------------------------
|
||||
|
||||
struct Ping {
|
||||
PacketList type = PacketList::PING;
|
||||
char buffer[PACKET_STRING_SIZE];
|
||||
};
|
||||
|
||||
struct Pong {
|
||||
PacketList type = PacketList::PONG;
|
||||
char buffer[PACKET_STRING_SIZE];
|
||||
char serverName[PACKET_STRING_SIZE];
|
||||
};
|
||||
|
||||
struct JoinRequest {
|
||||
@@ -46,8 +47,12 @@ struct JoinConfirm {
|
||||
int playerID;
|
||||
};
|
||||
|
||||
struct Disconnect {
|
||||
PacketList type = PacketList::DISCONNECT;
|
||||
};
|
||||
|
||||
//-------------------------
|
||||
//connections
|
||||
//player controls
|
||||
//-------------------------
|
||||
|
||||
struct NewPlayer {
|
||||
@@ -55,6 +60,7 @@ struct NewPlayer {
|
||||
int playerID;
|
||||
char handle[PACKET_STRING_SIZE];
|
||||
char avatar[PACKET_STRING_SIZE];
|
||||
Vector2 position;
|
||||
};
|
||||
|
||||
struct DeletePlayer {
|
||||
@@ -62,12 +68,8 @@ struct DeletePlayer {
|
||||
int playerID;
|
||||
};
|
||||
|
||||
//-------------------------
|
||||
//play updates
|
||||
//-------------------------
|
||||
|
||||
struct MotionUpdate {
|
||||
PacketList type = PacketList::MOTIONUPDATE;
|
||||
struct Movement {
|
||||
PacketList type = PacketList::MOVEMENT;
|
||||
int playerID;
|
||||
Vector2 position;
|
||||
Vector2 motion;
|
||||
@@ -78,19 +80,22 @@ struct MotionUpdate {
|
||||
//-------------------------
|
||||
|
||||
union Packet {
|
||||
Packet() {};
|
||||
~Packet() {};
|
||||
PacketList type;
|
||||
//networking systems
|
||||
Packet () {}
|
||||
~Packet() {}
|
||||
|
||||
PacketList type = PacketList::NONE;
|
||||
|
||||
//connections
|
||||
Ping ping;
|
||||
Pong pong;
|
||||
JoinRequest joinRequest;
|
||||
JoinConfirm joinConfirm;
|
||||
//connections
|
||||
Disconnect disconnect;
|
||||
|
||||
//player controls
|
||||
NewPlayer newPlayer;
|
||||
DeletePlayer deletePlayer;
|
||||
//play updates
|
||||
MotionUpdate motionUpdate;
|
||||
Movement movement;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user