Skip to content

Commit

Permalink
added SDL_p2p windows module
Browse files Browse the repository at this point in the history
added SDL_p2p for windows
  • Loading branch information
lufinkey committed Nov 13, 2014
1 parent 35b17e9 commit 8932267
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 58 deletions.
Binary file added External/SDL/lib/SDL2_p2p.lib
Binary file not shown.
Binary file added External/SDL/lib/SDL2_p2p_debug.lib
Binary file not shown.
4 changes: 2 additions & 2 deletions SmashBros.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>External\PlistCpp\Windows\lib;External\SDL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;SDL2_p2p_debug.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -83,7 +83,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>SDL2.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_ttf.lib;SDL2_mixer.lib;SDL2_p2p.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>External\PlistCpp\Windows\lib;External\SDL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
Expand Down
16 changes: 8 additions & 8 deletions Source/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace SmashBros
Game::Game()
{
//Constructor
View::setSize(900,600);
firstUpdate = true;
drawnOnce = false;
}
Expand All @@ -23,15 +24,12 @@ namespace SmashBros
{
//Initialize things
scaleToWindow(true,900,600);

Global::init();
Controls::loadControls();
Preferences::init();
Preferences::load();


AssetManager::loadImage("Images/icon.png");
setLoadScreen("Images/loading.png");

Global::init();

showRealFPS(true);
if(Preferences::highFPS())
{
Expand All @@ -48,9 +46,11 @@ namespace SmashBros

void Game::LoadContent()
{
bool thingy = true;
Console::WriteLine((byte)thingy);
//Load Things
Controls::loadControls();
Preferences::init();
Preferences::load();

Menus::loadAssets();
Menus::loadMenus();
ScreenManager::Add(new GameScreen("Game"));
Expand Down
15 changes: 9 additions & 6 deletions Source/GameEngine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,23 @@ namespace GameEngine
{
if(TTF_Init() < 0)
{
Console::WriteLine("failed to initalize SDL_ttf library");
Console::WriteLine((String)"failed to initalize SDL_ttf library with error: " + TTF_GetError());
gameRunning = false;
return false;
}
}

{
int flags = MIX_INIT_FLAC | MIX_INIT_MP3 | MIX_INIT_MOD | MIX_INIT_OGG;
Mix_Init(flags);
int flags = MIX_INIT_FLAC | MIX_INIT_MP3 | /*MIX_INIT_MOD | */MIX_INIT_OGG;
if(Mix_Init(flags) != flags)
{
Console::WriteLine((String)"failed to initialize SDL_mixer with error: " + Mix_GetError());
}
}

if(Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096)<0)
{
Console::WriteLine("failed to open SDL_mixer Audio");
Console::WriteLine((String)"failed to open SDL_mixer audio with error: " + Mix_GetError());
}

Sound::init();
Expand Down Expand Up @@ -249,8 +252,8 @@ namespace GameEngine
int windowX = 0;
int windowY = 0;
#else
int windowX = 100;
int windowY = 100;
int windowX = SDL_WINDOWPOS_UNDEFINED;
int windowY = SDL_WINDOWPOS_UNDEFINED;
#endif

if(borderless)
Expand Down
120 changes: 80 additions & 40 deletions Source/GameEngine/Util/P2PManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,6 @@ namespace GameEngine

int P2PManager_SendDataHandler(void*data)
{
#if defined(SMASHBROS_P2P_DISABLE)
return -1;
#else
GameEngine_DataPacket*packet = (GameEngine_DataPacket*)data;

if(packet->peers.size()>0) //specific peers
Expand Down Expand Up @@ -589,12 +586,10 @@ namespace GameEngine
}
delete packet;
return 0;
#endif
}

void P2PManager_EventHandler(P2P_Event*event)
{
#ifndef SMASHBROS_P2P_DISABLE
if(P2PManager::eventListener != NULL)
{
switch(event->type)
Expand Down Expand Up @@ -642,8 +637,8 @@ namespace GameEngine

case P2P_PICKERDIDCANCEL:
{
P2PManager::eventListener->pickerDidCancel();
P2PManager::pickerIsOpen = false;
P2PManager::eventListener->pickerDidCancel();
}
break;

Expand All @@ -654,7 +649,6 @@ namespace GameEngine
break;
}
}
#endif
}

P2PRequest::P2PRequest(const String&peerID)
Expand All @@ -676,9 +670,6 @@ namespace GameEngine

bool P2PRequest::accept()
{
#if defined(SMASHBROS_P2P_DISABLE)
return false;
#else
if(!handled)
{
SDL_bool success = P2P_acceptConnectionRequest(P2PManager::session, peerID);
Expand All @@ -692,19 +683,16 @@ namespace GameEngine
return false;
}
return connected;
#endif
}

void P2PRequest::deny()
{
#ifndef SMASHBROS_P2P_DISABLE
if(!handled)
{
P2P_denyConnectionRequest(P2PManager::session, peerID);
handled = true;
connected = false;
}
#endif
}

P2PEventListener::P2PEventListener()
Expand Down Expand Up @@ -754,12 +742,10 @@ namespace GameEngine

void P2PManager::Update(long gameTime)
{
#ifndef SMASHBROS_P2P_DISABLE
if(session!=NULL && P2P_isConnected(session))
{
updateAppEvents();
}
#endif
}

void P2PManager::Draw(Graphics2D&g, long gameTime)
Expand All @@ -779,13 +765,22 @@ namespace GameEngine

bool P2PManager::searchForPeers()
{
#if defined(SMASHBROS_P2P_DISABLE)
return false;
#else
if(session==NULL)
{
session = P2P_createSession();
P2P_setEventHandler(session, P2PManager_EventHandler);
if(session == NULL)
{
P2P_Event event;
event.type = P2P_PICKERDIDCANCEL;
event.peer.peerID = "";
event.peer.peerDisplayName = "";
event.data.data = NULL;
event.data.size = 0;
P2PManager_EventHandler(&event);
return false;
}

P2P_setEventHandler(session, &P2PManager_EventHandler);
pickerIsOpen = true;
P2P_searchForPeers(session, sessionID);
while(pickerIsOpen)
Expand All @@ -799,55 +794,108 @@ namespace GameEngine
}
}
return false;
#endif
}

bool P2PManager::searchForClients()
{
if(session==NULL)
{
session = P2P_createSession();
if(session == NULL)
{
P2P_Event event;
event.type = P2P_PICKERDIDCANCEL;
event.peer.peerID = "";
event.peer.peerDisplayName = "";
event.data.data = NULL;
event.data.size = 0;
P2PManager_EventHandler(&event);
return false;
}

P2P_setEventHandler(session, &P2PManager_EventHandler);
pickerIsOpen = true;
P2P_searchForClients(session, sessionID);
while(pickerIsOpen)
{
SDL_Delay(30);
updateAppEvents();
}
if(P2P_isConnected(session))
{
return true;
}
}
return false;
}

bool P2PManager::searchForServer()
{
if(session==NULL)
{
session = P2P_createSession();
if(session == NULL)
{
P2P_Event event;
event.type = P2P_PICKERDIDCANCEL;
event.peer.peerID = "";
event.peer.peerDisplayName = "";
event.data.data = NULL;
event.data.size = 0;
P2PManager_EventHandler(&event);
return false;
}

P2P_setEventHandler(session, &P2PManager_EventHandler);
pickerIsOpen = true;
P2P_searchForServer(session, sessionID);
while(pickerIsOpen)
{
SDL_Delay(30);
updateAppEvents();
}
if(P2P_isConnected(session))
{
return true;
}
}
return false;
}

bool P2PManager::isConnected()
{
#if defined(SMASHBROS_P2P_DISABLE)
return false;
#else
if(session!=NULL && P2P_isConnected(session))
{
return true;
}
return false;
#endif
}

bool P2PManager::isConnectedToPeer(const String&peerID)
{
#if defined(SMASHBROS_P2P_DISABLE)
return false;
#else
if(session!=NULL && P2P_isConnectedToPeer(session, peerID))
{
return true;
}
return false;
#endif
}

void P2PManager::disconnectPeer(const String&peerID)
{
#ifndef SMASHBROS_P2P_DISABLE
if(session!=NULL && P2P_isConnectedToPeer(session, peerID))
{
P2P_disconnectPeer(session, peerID);
}
#endif
}

void P2PManager::endSession()
{
#ifndef SMASHBROS_P2P_DISABLE
if(session!=NULL)
{
P2P_endSession(session);
P2P_destroySession(session);
session = NULL;
}
#endif
}

ArrayList<String> P2PManager::getPeers()
Expand All @@ -874,9 +922,6 @@ namespace GameEngine

String P2PManager::getPeerDisplayName(const String&peerID)
{
#if defined(SMASHBROS_P2P_DISABLE)
return "Unknown";
#else
peers_mutex.lock();
for(int i=0; i<peers.size(); i++)
{
Expand All @@ -893,7 +938,6 @@ namespace GameEngine
P2P_getPeerDisplayName(session, (char*)peerID, buffer);
String displayName = buffer;
return displayName;
#endif
}

void P2PManager::setEventListener(P2PEventListener*listener)
Expand All @@ -903,7 +947,6 @@ namespace GameEngine

void P2PManager::sendData(void*data, unsigned int size, Byte sendDataMode)
{
#ifndef SMASHBROS_P2P_DISABLE
/*if(sendDataMode == SENDDATA_RELIABLE)
{
SDL_P2P_sendData(Application::getWindow(), data, size, SDL_P2P_SENDDATA_RELIABLE);
Expand All @@ -925,12 +968,10 @@ namespace GameEngine

SDL_CreateThread(&P2PManager_SendDataHandler, "SendData", (void*)packet);
}
#endif
}

void P2PManager::sendDataToPeers(const ArrayList<String>&peers, void*data, unsigned int size, Byte sendDataMode)
{
#ifndef SMASHBROS_P2P_DISABLE
/*char** peerList = new char*[peers.size()];
for(unsigned int i=0; i<peers.size(); i++)
{
Expand Down Expand Up @@ -964,6 +1005,5 @@ namespace GameEngine

SDL_CreateThread(&P2PManager_SendDataHandler, "SendData", (void*)(packet));
}
#endif
}
}
4 changes: 2 additions & 2 deletions Source/GameEngine/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace GameEngine

void View::setSize(int w, int h)
{
windowWidth = 480;
windowHeight = 320;
windowWidth = w;
windowHeight = h;
if(Application::window != NULL)
{
SDL_SetWindowSize(Application::window, w, h);
Expand Down

0 comments on commit 8932267

Please sign in to comment.