Skip to content

Commit

Permalink
Testing network
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishTuteja committed Apr 14, 2022
1 parent 5facdf7 commit fd3dfa3
Show file tree
Hide file tree
Showing 12 changed files with 426 additions and 19 deletions.
32 changes: 30 additions & 2 deletions MainMenu.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Screen.h"
#include "MainMenu.h"
#include "Game.h"
#include "TextInput.h"
#include "SearchOpponent.h"
#include <functional>
#include <iostream>

Expand All @@ -21,9 +23,17 @@ MainMenu::MainMenu(LWindow &window) : LScreen(window)
playButton->setHighlightTexture(buttonBgHighlighted);
playButton->setClickTexture(buttonBgClicked);
playButton->setOnClickListener([&](LWindow &window)
{LGame* myGame = new LGame(window);
window.setCurrScreen(myGame); });
{
LScreen* currScreen = window.getCurrScreen();
MainMenu* menuScreen = dynamic_cast<MainMenu*>(currScreen);
SearchOpponent *searchOpponent = new SearchOpponent(window, menuScreen->getName());
window.setCurrScreen(searchOpponent); });
buttons.push_back(playButton);

txtColor = {0, 0, 0, 255};
TextInput *nameInput = new TextInput(x, y - 40 - 20, 240, 40, {255, 255, 255, 255}, window, "", font, txtColor);
nameInput->setCenterX(true);
textInputs.push_back(nameInput);
}

void MainMenu::handleEvent(SDL_Event &e)
Expand All @@ -32,6 +42,10 @@ void MainMenu::handleEvent(SDL_Event &e)
{
buttons[i]->handle(e);
}
for (int i = 0; i < textInputs.size(); i++)
{
textInputs[i]->handleEvent(e);
}
}

void MainMenu::update()
Expand All @@ -46,6 +60,10 @@ void MainMenu::render(SDL_Renderer *renderer)
{
buttons[i]->render(renderer);
}
for (int i = 0; i < textInputs.size(); i++)
{
textInputs[i]->render(renderer);
}
}

void MainMenu::cleanUp()
Expand All @@ -55,4 +73,14 @@ void MainMenu::cleanUp()
buttons[i]->cleanUp();
delete buttons[i];
}
for (int i = 0; i < textInputs.size(); i++)
{
textInputs[i]->cleanUp();
delete textInputs[i];
}
}

std::string MainMenu::getName()
{
return textInputs[0]->getText();
}
3 changes: 3 additions & 0 deletions MainMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#include "Screen.h"
#include "MyWindow.h"
#include "Button.h"
#include "TextInput.h"

class MainMenu : public LScreen
{
private:
std::vector<Button *> buttons;
std::vector<TextInput *> textInputs;
LTexture buttonBg;
LTexture buttonBgHighlighted;
LTexture buttonBgClicked;
Expand All @@ -19,6 +21,7 @@ class MainMenu : public LScreen
void update();
void render(SDL_Renderer *renderer);
void cleanUp();
std::string getName();
};

#endif
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
CXX = g++

#OBJS specifies which files to compile as part of the project
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp Text.cpp Tile.cpp TileAtlas.cpp Timer.cpp SoundEffect.cpp Entity.cpp
OBJS = Button.cpp collision.cpp Game.cpp main.cpp MainMenu.cpp MyTexture.cpp MyWindow.cpp Player.cpp Screen.cpp SearchOpponent.cpp Text.cpp TextInput.cpp Tile.cpp TileAtlas.cpp Timer.cpp SoundEffect.cpp Entity.cpp

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -g

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lSDL2_net

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = experiment.out
Expand Down
75 changes: 75 additions & 0 deletions MessageStructs.h
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
24 changes: 17 additions & 7 deletions MyWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_net.h>
#include <vector>
#include <sstream>
#include <string>
Expand All @@ -15,7 +16,6 @@

#include "Timer.h"


LWindow::LWindow(int width, int height)
{
initLibs();
Expand Down Expand Up @@ -51,7 +51,7 @@ void LWindow::begin()

LTimer updateTimer;
int MILLIS_PER_FRAME = 1000 / 60;
int loopTimes = 0 ;
int loopTimes = 0;

// While application is running
while (!quit)
Expand Down Expand Up @@ -97,10 +97,11 @@ void LWindow::begin()
// Wait remaining time
SDL_Delay(MILLIS_PER_FRAME - frameTicks);
}
FPS = 1000 / updateTimer.getTicks();
FPS = 1000 / updateTimer.getTicks();
std::stringstream caption;
loopTimes = (loopTimes + 1 )%FPSupdatespeed ;
if(loopTimes == 0 ){
loopTimes = (loopTimes + 1) % FPSupdatespeed;
if (loopTimes == 0)
{
caption << "MAZEGAME FPS:" << std::to_string(FPS);
SDL_SetWindowTitle(mWindow, caption.str().c_str());
}
Expand Down Expand Up @@ -174,8 +175,6 @@ void LWindow::handleEvent(SDL_Event &e)
mMinimized = false;
break;
}


}
// Enter exit full screen on return key
else if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_RETURN)
Expand Down Expand Up @@ -233,6 +232,12 @@ bool LWindow::initLibs()
}
Mix_AllocateChannels(32);

if (SDLNet_Init() < 0)
{
fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
return false;
}

return true;
}

Expand Down Expand Up @@ -295,4 +300,9 @@ void LWindow::cleanUp()
mKeyboardFocus = false;
mWidth = 0;
mHeight = 0;
}

LScreen *LWindow::getCurrScreen()
{
return mCurrScreen;
}
3 changes: 2 additions & 1 deletion MyWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class LWindow
bool loadText(LTexture &texture, std::string txt, TTF_Font *font, SDL_Color textColor);

void setCurrScreen(LScreen *screen);
LScreen *getCurrScreen();

private:
bool initLibs();
Expand All @@ -50,7 +51,7 @@ class LWindow
// Window dimensions
int mWidth;
int mHeight;
int FPS ;
int FPS;

// Window focus
bool mMouseFocus;
Expand Down
84 changes: 84 additions & 0 deletions SearchOpponent.cpp
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);
}
24 changes: 24 additions & 0 deletions SearchOpponent.h
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
Loading

0 comments on commit fd3dfa3

Please sign in to comment.