Skip to content

Commit

Permalink
First network test
Browse files Browse the repository at this point in the history
Warning: old, unfinished code
  • Loading branch information
andre111 committed Apr 26, 2017
1 parent c107f82 commit 3b2a0f4
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 48 deletions.
Binary file modified data/icons2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion source/Menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ void renderMenu(int menu,int xscr,int yscr){
char * text = malloc((50+8+1) * sizeof(char));
memset(text, 0, (50+8+1) * sizeof(char));
networkGetScanName(text, i);
strcpy(text, "'s World");
strcat(text, "'s World");

if(i != currentSelection) color &= 0xFF7F7F7F; // Darken color.

Expand Down
19 changes: 14 additions & 5 deletions source/Network.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,28 @@ bool networkConnected() {
return isConnected;
}

bool networkIsServer() {
return isServer;
}



void networkSend(networkPacket *packet, size_t size) {
if(udsRunning && isConnected) {
//Server broadcasts to all clients but clients only send info to server
u16 dest;
if(isServer) {
dest = UDS_BROADCAST_NETWORKNODEID;
networkSendTo(packet, size, UDS_BROADCAST_NETWORKNODEID);
} else {
dest = UDS_HOST_NETWORKNODEID;
networkSendTo(packet, size, UDS_HOST_NETWORKNODEID);
}
}
}

//TODO: I somehow need to implement a relieable protocol on top of uds
void networkSendTo(networkPacket *packet, size_t size, u16 reciever) {
if(udsRunning && isConnected) {

Result ret = udsSendTo(dest, NETWORK_CHANNEL, UDS_SENDFLAG_Default, packet, size);
Result ret = udsSendTo(reciever, NETWORK_CHANNEL, UDS_SENDFLAG_Default, packet, size);
if(UDS_CHECK_SENDTO_FATALERROR(ret)) {
//TODO: what do?
}
Expand All @@ -187,7 +196,7 @@ void networkRecieve() {
if(actualSize) {
networkPacket *packet = (networkPacket*) networkBuffer;

processPacket(packet, packet->analyze.type);
processPacket(packet, packet->analyze.type, sourceNetworkNodeID);
}
}
}
2 changes: 2 additions & 0 deletions source/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ bool networkConnect(int pos);
void networkDisconnect();

bool networkConnected();
bool networkIsServer();

void networkSend(networkPacket *packet, size_t size); //TODO: Should this be a pointer? Calling function needs to cleanup itself
void networkSendTo(networkPacket *packet, size_t size, u16 reciever); //TODO: Should this be a pointer? Calling function needs to cleanup itself
void networkRecieve(); //TODO: Should recieve actually handle all the packets or just return them?
50 changes: 44 additions & 6 deletions source/PacketHandler.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
#include "PacketHandler.h"

void processPacket(networkPacket *packet, u8 type) {
#include "Globals.h"

void processPacket(networkPacket *packet, u8 type, u16 sender) {
//TODO: Differenciate the packets and process them
if(type==PACKET_REQUEST_MAPDATA) {

} else if(type==PACKET_MAPDATA) {

}
if(networkIsServer()) {
if(type==PACKET_REQUEST_MAPDATA) {
u8 level = packet->requestMapData.level;

if(level>=0 && level<=5) {
//send back tile data
for(int y=0; y<128; y++) {
networkPacket packet = {
.mapData = {
.type = PACKET_MAPDATA,
.level = level,
.offset = y
}
};
for(int x=0; x<128; x++) {
packet.mapData.map[x] = map[level][x+y*128];
packet.mapData.data[x] = data[level][x+y*128];
}
networkSendTo(&packet, sizeof(packetMapData), sender);
}
}
} else {
//TODO: Unknown packet - how to handle?
}
} else {
if(type==PACKET_MAPDATA) {
u8 level = packet->mapData.level;

if(level>=0 && level<=5) {
//recieve tile data
//TODO: This should really check whether the values are in valid range
int y = packet->mapData.offset;
for(int x=0; x<128; x++) {
map[level][x+y*128] = packet->mapData.map[x];
data[level][x+y*128] = packet->mapData.data[x];
}
}
} else {
//TODO: Unknown packet - how to handle?
}
}
}
2 changes: 1 addition & 1 deletion source/PacketHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#include "Network.h"

void processPacket(networkPacket *packet, u8 type);
void processPacket(networkPacket *packet, u8 type, u16 sender);
125 changes: 90 additions & 35 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
// -> Skeleton arrows are slower, do a little less damage
// -> Or instead of less damage, implement a simple armor system

//TODO: Multiplayer should use normal drawing code -> so remove this first test again
float tmxscr = 400;
float tmyscr = 400;
float tmenuxa = 0.25;
float tmenuya = 0.25;


void initMiniMapData() {
int i;
for(i = 0; i < 128 * 128; ++i) {
Expand Down Expand Up @@ -75,6 +82,12 @@ void setupGame(bool loadUpWorld, bool remote) {

initMiniMap(loadUpWorld);
} else {
//reset level data so no old data can somehow remain
memset(map, 0, 128*128*5 * sizeof(u8));
memset(data, 0, 128*128*5 * sizeof(u8));

currentLevel = 1;

//TODO: Can Packets get dropped - if yes, should resending be handled by network functions (so I dont need to do it everywhere)
networkPacket packet = {
.requestMapData = {
Expand Down Expand Up @@ -286,41 +299,83 @@ int main() {

if (currentMenu == 0) {
tick();
sf2d_start_frame(GFX_TOP, GFX_LEFT);

offsetX = xscr;
offsetY = yscr;
sf2d_draw_rectangle(0, 0, 400, 240, 0xFF0C0C0C); //RGBA8(12, 12, 12, 255)); //You might think "real" black would be better, but it actually looks better that way

renderLightsToStencil(false, false, true);

renderBackground(xscr, yscr);
renderEntities(player.x, player.y, &eManager);
renderPlayer();
renderWeather(xscr, yscr);

resetStencilStuff();

renderDayNight();

offsetX = 0;
offsetY = 0;

if(shouldRenderDebug){
sprintf(fpsstr, " FPS: %.0f, X:%d, Y:%d, E:%d", sf2d_get_fps(), player.x, player.y, eManager.lastSlot[currentLevel]);
drawText(fpsstr, 2, 225);
}

sf2d_end_frame();

sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
if(!shouldRenderMap){
sf2d_draw_texture(bottombg, 0, 0);
renderGui();
} else {
renderZoomedMap();
}
sf2d_end_frame();
//TODO: Multiplayer should use normal drawing code -> so remove this first test again
if(!isRemote) {
sf2d_start_frame(GFX_TOP, GFX_LEFT);

offsetX = xscr;
offsetY = yscr;
sf2d_draw_rectangle(0, 0, 400, 240, 0xFF0C0C0C); //RGBA8(12, 12, 12, 255)); //You might think "real" black would be better, but it actually looks better that way

renderLightsToStencil(false, false, true);

renderBackground(xscr, yscr);
renderEntities(player.x, player.y, &eManager);
renderPlayer();
renderWeather(xscr, yscr);

resetStencilStuff();

renderDayNight();

offsetX = 0;
offsetY = 0;

if(shouldRenderDebug){
sprintf(fpsstr, " FPS: %.0f, X:%d, Y:%d, E:%d", sf2d_get_fps(), player.x, player.y, eManager.lastSlot[currentLevel]);
drawText(fpsstr, 2, 225);
}

sf2d_end_frame();

sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
if(!shouldRenderMap){
sf2d_draw_texture(bottombg, 0, 0);
renderGui();
} else {
renderZoomedMap();
}
sf2d_end_frame();
//TODO: Multiplayer should use normal drawing code -> so remove this first test again
} else {
//TODO: Temporary way of getting back to the menu
if (k_pause.clicked){
sf2d_set_clear_color(0xFF);
currentSelection = 0;
currentMenu = MENU_TITLE;

networkDisconnect();

playMusic(music_menu);
}

tmxscr += tmenuxa;
tmyscr += tmenuya;

if (tmxscr < 16) {
tmxscr = 16;
tmenuxa = -tmenuxa;
} else if (tmxscr > 1832) {
tmxscr = 1832;
tmenuxa = -tmenuxa;
}
if (tmyscr < 16) {
tmyscr = 16;
tmenuya = -tmenuya;
} else if (tmyscr > 1792) {
tmyscr = 1792;
tmenuya = -tmenuya;
}

sf2d_start_frame(GFX_TOP, GFX_LEFT);
offsetX = (int) tmxscr; offsetY = (int) tmyscr;
renderBackground((int) tmxscr, (int) tmyscr);
offsetX = 0; offsetY = 0;
sf2d_end_frame();

sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
sf2d_end_frame();
}
} else {
tickMenu(currentMenu);
renderMenu(currentMenu, xscr, yscr);
Expand Down

0 comments on commit 3b2a0f4

Please sign in to comment.