| 1 | /* FCE Ultra - NES/Famicom Emulator\r |
| 2 | *\r |
| 3 | * Copyright notice for this file:\r |
| 4 | * Copyright (C) 2002 Ben Parnell\r |
| 5 | *\r |
| 6 | * This program is free software; you can redistribute it and/or modify\r |
| 7 | * it under the terms of the GNU General Public License as published by\r |
| 8 | * the Free Software Foundation; either version 2 of the License, or\r |
| 9 | * (at your option) any later version.\r |
| 10 | *\r |
| 11 | * This program is distributed in the hope that it will be useful,\r |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r |
| 14 | * GNU General Public License for more details.\r |
| 15 | *\r |
| 16 | * You should have received a copy of the GNU General Public License\r |
| 17 | * along with this program; if not, write to the Free Software\r |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r |
| 19 | */\r |
| 20 | \r |
| 21 | #ifdef NETWORK\r |
| 22 | \r |
| 23 | #include <stdio.h>\r |
| 24 | #include <stdlib.h>\r |
| 25 | #include <string.h>\r |
| 26 | \r |
| 27 | #include "types.h"\r |
| 28 | #include "svga.h"\r |
| 29 | #include "netplay.h"\r |
| 30 | \r |
| 31 | int netplay=0; /* 1 if we are a server */\r |
| 32 | /* 2 if we need a host */\r |
| 33 | static uint8 netjoy[4]; // controller stuff.\r |
| 34 | \r |
| 35 | static void NetError(void)\r |
| 36 | {\r |
| 37 | FCEU_DispMessage("Network error/connection lost!");\r |
| 38 | // FCEUD_PrintError("Network error/connection lost!");\r |
| 39 | netplay=0;\r |
| 40 | FCEUD_NetworkClose();\r |
| 41 | }\r |
| 42 | \r |
| 43 | void KillNetplay(void)\r |
| 44 | {\r |
| 45 | if(netplay)\r |
| 46 | {\r |
| 47 | FCEUD_NetworkClose();\r |
| 48 | netplay=0;\r |
| 49 | }\r |
| 50 | }\r |
| 51 | \r |
| 52 | int InitNetplay(void)\r |
| 53 | {\r |
| 54 | if(!FCEUD_NetworkConnect())\r |
| 55 | {NetError();return 0;}\r |
| 56 | netplay=FSettings.NetworkPlay;\r |
| 57 | memset(netjoy,0,sizeof(netjoy));\r |
| 58 | return 1;\r |
| 59 | }\r |
| 60 | \r |
| 61 | void NetplayUpdate(uint16 *joyp1, uint16 *joyp2)\r |
| 62 | {\r |
| 63 | uint8 buf[5];\r |
| 64 | if(netplay==1) // We're the server\r |
| 65 | {\r |
| 66 | int t;\r |
| 67 | \r |
| 68 | loopo:\r |
| 69 | \r |
| 70 | t=FCEUD_NetworkRecvData(&netjoy[2],1,0);\r |
| 71 | if(!t) {NetError();return;} \r |
| 72 | if(t!=-1) goto loopo;\r |
| 73 | \r |
| 74 | netjoy[0]=*joyp1;\r |
| 75 | memcpy(buf,netjoy,4);\r |
| 76 | buf[4]=CommandQueue;\r |
| 77 | \r |
| 78 | if(!FCEUD_NetworkSendData(buf,5)) {NetError();return;}\r |
| 79 | if(CommandQueue)\r |
| 80 | {\r |
| 81 | DoCommand(CommandQueue);\r |
| 82 | CommandQueue=0;\r |
| 83 | }\r |
| 84 | }\r |
| 85 | else if(netplay==2) // We're connected to a host (we're second player)\r |
| 86 | {\r |
| 87 | uint8 ja=(*joyp1)|(*joyp1>>8)|(*joyp2)|(*joyp2>>8);\r |
| 88 | if(!FCEUD_NetworkSendData(&ja,1)) {NetError();return;}\r |
| 89 | if(!FCEUD_NetworkRecvData(buf,5,1)) {NetError();return;}\r |
| 90 | \r |
| 91 | memcpy(netjoy,buf,4);\r |
| 92 | if(buf[4]) DoCommand(buf[4]);\r |
| 93 | }\r |
| 94 | *joyp1=netjoy[0]|(netjoy[1]<<8);\r |
| 95 | *joyp2=netjoy[2]|(netjoy[3]<<8);\r |
| 96 | \r |
| 97 | }\r |
| 98 | #endif\r |