-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5facdf7
commit fd3dfa3
Showing
12 changed files
with
426 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#ifndef MESSAGE_STRUCTS_H | ||
#define MESSAGE_STRUCTS_H | ||
|
||
#include <string> | ||
#include <string.h> | ||
#include <iostream> | ||
|
||
struct Message | ||
{ | ||
int type; | ||
virtual ~Message() {} | ||
}; | ||
|
||
struct NewClientMessage : Message | ||
{ | ||
NewClientMessage() | ||
{ | ||
type = 0; | ||
} | ||
std::string name; | ||
}; | ||
|
||
int serialize(Message *msg, char *data) | ||
{ | ||
int bytesUsed = 0; | ||
int *q = (int *)data; | ||
*q = msg->type; | ||
q++; | ||
bytesUsed += sizeof(int); | ||
switch (msg->type) | ||
{ | ||
case 0: | ||
NewClientMessage *msgNewClient = dynamic_cast<NewClientMessage *>(msg); | ||
char *p = (char *)q; | ||
for (int i = 0; i < msgNewClient->name.length(); i++) | ||
{ | ||
*p = msgNewClient->name[i]; | ||
p++; | ||
bytesUsed++; | ||
} | ||
*p = '\0'; | ||
p++; | ||
bytesUsed++; | ||
break; | ||
} | ||
return bytesUsed; | ||
} | ||
|
||
void deserialize(char *data, Message *msg) | ||
{ | ||
int *q = (int *)data; | ||
msg->type = *q; | ||
q++; | ||
switch (msg->type) | ||
{ | ||
case 0: | ||
NewClientMessage *msgNewClient = dynamic_cast<NewClientMessage *>(msg); | ||
char *p = (char *)q; | ||
// int j = 0; | ||
// while (p[j] != 0) | ||
// { | ||
// std::cout << p[j]; | ||
// j++; | ||
// } | ||
int nameLen = strlen(p); | ||
for (int i = 0; i < nameLen; i++) | ||
{ | ||
msgNewClient->name += *p; | ||
p++; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "SearchOpponent.h" | ||
#include "MyWindow.h" | ||
#include "stdio.h" | ||
#include "MessageStructs.h" | ||
#include <string> | ||
#include <SDL2/SDL.h> | ||
#include <SDL2/SDL_net.h> | ||
#include <iostream> | ||
#include "Game.h" | ||
|
||
SearchOpponent::SearchOpponent(LWindow &window, std::string playerName) : LScreen(window), playerName(playerName) | ||
{ | ||
if (!(sd = SDLNet_UDP_Open(2000))) | ||
{ | ||
fprintf(stderr, "SDLNet_UDP_Open: %s\n", SDLNet_GetError()); | ||
} | ||
|
||
if (!(p = SDLNet_AllocPacket(512))) | ||
{ | ||
fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError()); | ||
} | ||
|
||
if (SDLNet_ResolveHost(&broadcastAdd, "255.255.255.255", 2000)) | ||
{ | ||
fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError()); | ||
} | ||
|
||
NewClientMessage newClientMsg; | ||
newClientMsg.name = playerName; | ||
int bytesUsed = serialize(&newClientMsg, (char *)p->data); | ||
p->address.host = broadcastAdd.host; | ||
p->address.port = broadcastAdd.port; | ||
p->len = bytesUsed; | ||
// int j = 4; | ||
// while (p->data[j] != 0) | ||
// { | ||
// std::cout << p->data[j]; | ||
// j++; | ||
// } | ||
SDLNet_UDP_Send(sd, -1, p); | ||
} | ||
|
||
void SearchOpponent::handleEvent(SDL_Event &e) | ||
{ | ||
switch (e.type) | ||
{ | ||
case SDL_KEYUP: | ||
if (e.key.keysym.sym == SDLK_RETURN) | ||
{ | ||
LGame *myGame = new LGame(window); | ||
window.setCurrScreen(myGame); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void SearchOpponent::update() | ||
{ | ||
if (SDLNet_UDP_Recv(sd, p)) | ||
{ | ||
// int j = 4; | ||
// while (p->data[j] != 0) | ||
// { | ||
// std::cout << p->data[j]; | ||
// j++; | ||
// } | ||
NewClientMessage newClientMsg; | ||
deserialize((char *)p->data, &newClientMsg); | ||
std::cout << newClientMsg.name << std::endl; | ||
} | ||
} | ||
|
||
void SearchOpponent::render(SDL_Renderer *renderer) | ||
{ | ||
SDL_SetRenderDrawColor(renderer, 3, 211, 252, 255); | ||
SDL_RenderClear(renderer); | ||
} | ||
|
||
void SearchOpponent::cleanUp() | ||
{ | ||
SDLNet_FreePacket(p); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef SEARCH_OPPONENT | ||
#define SEARCH_OPPONENT | ||
|
||
#include "Screen.h" | ||
#include <string> | ||
#include <SDL2/SDL_net.h> | ||
|
||
class SearchOpponent : public LScreen | ||
{ | ||
private: | ||
std::string playerName; | ||
UDPsocket sd; | ||
UDPpacket *p; | ||
IPaddress broadcastAdd; | ||
|
||
public: | ||
SearchOpponent(LWindow &window, std::string playerName); | ||
void handleEvent(SDL_Event &e); | ||
void update(); | ||
void render(SDL_Renderer *renderer); | ||
void cleanUp(); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.