iphone_doom.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. Copyright (C) 2009-2011 id Software LLC, a ZeniMax Media company.
  3. Copyright (C) 2009 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. // this is the version number displayed on the menu screen
  17. #define DOOM_IPHONE_VERSION 0.9
  18. // if defined, the game runs in a separate thread from the app event loop
  19. #define USE_GAME_THREAD
  20. typedef enum menuState {
  21. IPM_GAME,
  22. IPM_MAIN,
  23. IPM_MAPS,
  24. IPM_MULTIPLAYER,
  25. IPM_CONTROLS,
  26. IPM_OPTIONS,
  27. IPM_HUDEDIT,
  28. IPM_PACKET_TEST
  29. } menuState_t;
  30. extern menuState_t menuState;
  31. extern menuState_t lastState;
  32. void iphoneDrawMenus();
  33. #define VID_WIDTH 480
  34. #define VID_HEIGHT 320
  35. #define MAX_SKILLS 5
  36. #define MAX_MAPS 200
  37. #define MF_TRIED 1
  38. #define MF_COMPLETED 2
  39. #define MF_KILLS 4
  40. #define MF_SECRETS 8
  41. #define MF_TREASURE 16
  42. #define MF_TIME 32
  43. // we want to track mapStats for downloaded content, so we
  44. // won't have a known number of these
  45. typedef struct {
  46. int dataset;
  47. int episode;
  48. int map;
  49. int completionFlags[MAX_SKILLS];
  50. } mapStats_t;
  51. // the level select screen returns this
  52. typedef struct {
  53. int dataset;
  54. int episode;
  55. int map;
  56. int skill;
  57. } mapStart_t;
  58. // this structure is saved out at the head of the binary save file,
  59. // and allows all the menus to work without having to load a game save
  60. typedef struct {
  61. mapStart_t map; // this is the map currently being run
  62. int saveGameIsValid; // when 0, resume game will just be a new game
  63. // if someone downloads more than MAX_MAPS, they won't get stat tracking on them.
  64. int numMapStats;
  65. mapStats_t mapStats[MAX_MAPS];
  66. } playState_t;
  67. extern playState_t playState;
  68. extern boolean levelHasBeenLoaded; // determines if "resume game" does a loadGame and exiting does a saveGame
  69. extern pkTexture_t *arialFontTexture;
  70. // set to 1 when app is exiting to cause game thread to do a save game,
  71. // which would not be safe to do from the event thread
  72. extern volatile int saveOnExitState;
  73. extern int asyncTicNum; // 30hz
  74. extern int iphoneFrameNum; // frame rate dependent, max of 30hz
  75. extern int levelLoadFrameNum;
  76. extern int consoleActive;
  77. extern boolean iphoneTimeDemo;
  78. extern int timeDemoStart;
  79. extern char timeDemoResultString[80];
  80. extern cvar_t *skill;
  81. extern cvar_t *episode;
  82. extern cvar_t *controlScheme;
  83. extern cvar_t *stickMove;
  84. extern cvar_t *stickTurn;
  85. extern cvar_t *rotorTurn;
  86. extern cvar_t *stickDeadBand;
  87. extern cvar_t *tiltTurn;
  88. extern cvar_t *tiltMove;
  89. extern cvar_t *tiltDeadBand;
  90. extern cvar_t *tiltAverages;
  91. extern cvar_t *music;
  92. extern cvar_t *miniNet;
  93. extern cvar_t *showTilt;
  94. extern cvar_t *showTime;
  95. extern cvar_t *showNet;
  96. extern cvar_t *showSound;
  97. extern cvar_t *cropSprites;
  98. extern cvar_t *revLand;
  99. extern cvar_t *mapScale;
  100. extern cvar_t *drawControls;
  101. extern cvar_t *autoUse;
  102. extern cvar_t *statusBar;
  103. extern cvar_t *touchClick;
  104. extern cvar_t *messages;
  105. extern cvar_t *timeLimit;
  106. extern cvar_t *fragLimit;
  107. extern cvar_t *mpDeathmatch;
  108. extern cvar_t *mpDataset;
  109. extern cvar_t *mpSkill;
  110. extern cvar_t *mpEpisode;
  111. extern cvar_t *mpMap;
  112. extern cvar_t *glfinish;
  113. extern cvar_t *mapSelectY;
  114. extern cvar_t *throttle;
  115. extern cvar_t *centerSticks;
  116. extern cvar_t *rampTurn;
  117. extern cvar_t *netBuffer;
  118. extern int numTouches;
  119. extern int touches[5][2]; // [0] = x, [1] = y in landscape mode, raster order with y = 0 at top
  120. // so we can detect button releases
  121. extern int numPrevTouches;
  122. extern int prevTouches[5][2];
  123. extern float tilt; // -1.0 to 1.0
  124. extern float tiltPitch;
  125. extern boolean drawWeaponSelect; // true when the weapon select overlay is up
  126. extern int weaponSelected; // -1 for no change
  127. typedef unsigned char color4_t[4];
  128. typedef unsigned char color3_t[3];
  129. // networking
  130. enum {
  131. PACKET_VERSION_BASE = 0x24350010,
  132. PACKET_VERSION_SETUP,
  133. PACKET_VERSION_JOIN,
  134. PACKET_VERSION_CLIENT,
  135. PACKET_VERSION_SERVER
  136. } packetType_t;
  137. #define DOOM_PORT 14666 // setup packets will go to DOOM_PORT+1
  138. // the server sends out a setup packet by broadcast, and also directly addressed
  139. // to each client that has joined the game because broadcast packets have truly
  140. // crappy delivery characteristics over WiFi
  141. typedef struct {
  142. int packetType;
  143. int gameID; // every change to anything in packetSetup_t must change gameID
  144. int startGame; // when this is set, start running the game
  145. int sendCount; // just for packet drop tests
  146. mapStart_t map;
  147. int deathmatch;
  148. int fraglimit;
  149. int timelimit;
  150. int playerID[MAXPLAYERS]; // 0 = not in game
  151. } packetSetup_t;
  152. // If we have received a recent setup packet before hitting the multiplayer
  153. // button, we will send a join packet to that server. Otherwise, we will
  154. // start acting as a new server.
  155. typedef struct {
  156. int packetType;
  157. // this should match the packetSetup.gameID
  158. int gameID;
  159. int playerID;
  160. } packetJoin_t;
  161. typedef struct {
  162. int packetType;
  163. // gameID is determined randomly during setup, any packet that doesn't
  164. // match is discarded
  165. int gameID;
  166. // this could be used to tell when we are dropping client packets
  167. // but it isn't critical
  168. int packetSequence;
  169. // used to show current round trip latency
  170. int packetAcknowledge;
  171. // the client's clock at the time the packet was sent, used
  172. // to track one-way latency
  173. int milliseconds;
  174. // the server could match this up based on ip alone, but it is nice to have
  175. int consoleplayer;
  176. // the last tic that the client has run
  177. int gametic;
  178. // some commands will get missed over the network
  179. ticcmd_t cmd;
  180. } packetClient_t;
  181. typedef struct {
  182. int packetType;
  183. // gameID is determined randomly during setup, any packet that doesn't
  184. // match is discarded
  185. int gameID;
  186. // used to detect packet drops
  187. int packetSequence;
  188. // used to show current round trip latency
  189. int packetAcknowledge;
  190. // the server's clock at the time the packet was sent, used
  191. // to track one-way latency
  192. int milliseconds;
  193. // consistancyTic will be the last acknowledged gametic for this
  194. // particular client
  195. int consistancyTic;
  196. // constancy is used to see if somehow the game running on the
  197. // client has diverged from the one running on the server,
  198. // which is an unrecoverable error
  199. short consistancy[MAXPLAYERS];
  200. // this will be the last pc.gametic from the player
  201. int starttic;
  202. // netcmds[][(maketic-1)&BACKUPTICMASK] is the most recent
  203. int maketic;
  204. // only the [playersInGame*(maketic-starttic)] will be transmitted
  205. ticcmd_t netcmds[MAXPLAYERS*BACKUPTICS];
  206. } packetServer_t;
  207. extern int gameSocket;
  208. extern struct sockaddr_in gameSocketAddress;
  209. extern int playerID;
  210. extern int gameID;
  211. extern int localGameID;
  212. extern int packetSequence;
  213. // Only one game can be set up at a time on a given wireless segment, although
  214. // several independent games can be played.
  215. // If a valid setupPacket has arrived in the last second, that will be the
  216. // displayed game, otherwise the local system starts sending out setupPackets.
  217. extern packetSetup_t setupPacket;
  218. extern int setupPacketFrameNum;
  219. extern int localGameID; // change every time we take over as the sender of setupPackets
  220. // set after each game tic if a usable line is in front of the player
  221. extern boolean autoUseActive;
  222. extern boolean respawnActive;
  223. typedef enum {
  224. NF_NONE,
  225. NF_CONSISTANCY,
  226. NF_INTERRUPTED
  227. } netFail_t;
  228. extern netFail_t netGameFailure; // set by asyncThread
  229. typedef struct {
  230. int interfaceIndex; // we must use the right socket to send packets
  231. struct sockaddr address;
  232. int oneWayLatency; // will always have 30+ msec of jitter
  233. int lastPacketAsyncTic; // to easily tell if it just arrived
  234. int lastPacketTime; // local milliseconds of last receive
  235. int lastTimeDelta; // packet milliseconds - local milliseconds
  236. int lowestTimeDelta; // min'd with lastTimeDelta each arrival
  237. int currentPingTics; // packetSequence - last packetAcknowledge
  238. } netPeer_t;
  239. typedef struct {
  240. netPeer_t peer;
  241. packetClient_t pc; // most recent packet received
  242. // TODO: There would be some benefit to ensuring that no edge transitions on
  243. // buttons are missed due to clock/net jitter.
  244. } netPlayer_t;
  245. // all received packets, whether bluetooth or WiFi, go through here
  246. void iphoneProcessPacket( const struct sockaddr *from, const void *data, int len );
  247. extern netPeer_t netServer;
  248. extern netPlayer_t netPlayers[MAXPLAYERS];
  249. extern sem_t * ticSemaphore;
  250. typedef struct {
  251. int numGameTics;
  252. int numPingTics;
  253. int enterFrame;
  254. int afterSleep;
  255. int beforeSwap;
  256. int afterSwap;
  257. } logTime_t;
  258. #define MAX_LOGGED_TIMES 512
  259. extern logTime_t loggedTimes[MAX_LOGGED_TIMES]; // indexed by iphoneFrameNum
  260. void LoadWallTexture( int wallPicNum );
  261. float StringFontWidth( const char *str );
  262. int TouchDown( int x, int y, int w, int h );
  263. int TouchReleased( int x, int y, int w, int h );
  264. int TouchPressed( int x, int y, int w, int h );
  265. // y is the baseline for font drawing
  266. float iphoneDrawText( float x, float y, float scale, const char *str );
  267. float iphoneCenterText( float x, float y, float scale, const char *str );
  268. void StartGame();
  269. void iphoneOpenAutomap();
  270. void iphoneDrawNotifyText();
  271. void iphoneSet2D( void );
  272. void R_Draw_Fill( int x, int y, int w, int h, color3_t c );
  273. void R_Draw_Blend( int x, int y, int w, int h, color4_t c );
  274. void InitImmediateModeGL();
  275. int iphoneRotateForLandscape();
  276. void iphoneCheckForLandscapeReverse();
  277. void iphonePacifierUpdate();
  278. void iphoneDrawScreen();
  279. extern int damageflash;
  280. extern int bonusFrameNum;
  281. extern int attackDirTime[2];
  282. #define BF_IGNORE 1 // don't draw or process touches
  283. #define BF_INACTIVE 2 // draw, but no touch processing at all
  284. #define BF_GLOW 4 // animated overbright glow
  285. #define BF_DIMMED 8 // draw darker, but still selectable
  286. #define BF_CENTERTEXT 16 // text in middle of button, not underneath
  287. #define BF_TRANSPARENT 32 // blend translucent
  288. #define BF_HUDBUTTON 64 // don't process in UpdateHudTouch
  289. #define BF_DRAW_ACTIVE 128 // for fire button
  290. #define BF_SMALL_CLICK 256 // for fire button
  291. typedef struct {
  292. int x, y;
  293. int drawWidth, drawHeight;
  294. pkTexture_t *texture;
  295. const char *title;
  296. struct touch_s *touch;
  297. float scale; // ramps up and down after touches
  298. int frameNum; // reset scale if not checked on previous frame
  299. int buttonFlags;
  300. boolean twoFingerPress; // if a second finger came down before a release for timedemo / etc
  301. boolean pressed; // true when a touch goes down in it
  302. // stuff for hud controls
  303. boolean drawAsLimit; // color tint when further movement won't do anything
  304. float touchState; // rotor angle
  305. float drawState; // offsets for rotors
  306. int downX, downY; // initial touch went down here
  307. } ibutton_t;
  308. typedef struct {
  309. ibutton_t forwardStick;
  310. ibutton_t sideStick;
  311. ibutton_t turnStick;
  312. ibutton_t turnRotor;
  313. ibutton_t fire;
  314. ibutton_t menu;
  315. ibutton_t map;
  316. ibutton_t weaponSelect;
  317. } hud_t;
  318. extern hud_t huds;
  319. void HudSetForScheme( int schemeNum );
  320. void HudSetTexnums();
  321. void HudEditFrame();
  322. boolean StartNetGame();
  323. int BackButton();
  324. void ResumeGame();
  325. //---------------------------------------
  326. // Touch and button
  327. //---------------------------------------
  328. typedef struct touch_s {
  329. boolean down;
  330. int x, y;
  331. // int prevX, prevY; // will be set to x, y on first touch, copied after each game frame
  332. int stateCount; // set to 1 on first event that state changes, incremented each game frame (-1 is a special tapped-and-released code)
  333. void *controlOwner;
  334. } touch_t;
  335. #define MAX_TOUCHES 5
  336. extern touch_t sysTouches[MAX_TOUCHES];
  337. extern touch_t gameTouches[MAX_TOUCHES];
  338. extern pthread_mutex_t eventMutex; // used to sync between game and event threads
  339. touch_t *TouchInBounds( int x, int y, int w, int h );
  340. touch_t *AnyTouchInBounds( int x, int y, int w, int h );
  341. touch_t *UpdateHudTouch( ibutton_t *hud );
  342. bool NewTextButton( ibutton_t *b, const char *title, int x, int y, int w, int h );
  343. void SetButtonPics( ibutton_t *button, const char *picName, const char *title, int x, int y );
  344. void SetButtonPicsAndSizes( ibutton_t *button, const char *picBase, const char *title, int x, int y, int w, int h );
  345. boolean HandleButton( ibutton_t *button );
  346. //---------------------------------------
  347. // Doom stuff we use directly
  348. //---------------------------------------
  349. void G_DoSaveGame (boolean menu);
  350. extern short consistancy[MAXPLAYERS][BACKUPTICS];
  351. extern boolean levelTimer;
  352. extern int levelTimeCount;
  353. extern boolean levelFragLimit;
  354. extern int levelFragLimitCount;
  355. //---------------------------------------
  356. // iphone_sound.c
  357. //---------------------------------------
  358. void Sound_Init( void );
  359. void Sound_StartLocalSound( const char *sound );
  360. void Sound_StartLocalSoundAtVolume( const char *sound, float volume );
  361. void ShowSound();
  362. //---------------------------------------
  363. // iphone_net.c
  364. //---------------------------------------
  365. // dump all the interfaces and ip addresses for debugging
  366. void ReportNetworkInterfaces();
  367. // open a UDP socket, pass "en0" for wifi
  368. int UDPSocket( const char *interfaceName, int portnum );
  369. // return false if the multiplayer button should be disabled
  370. boolean NetworkAvailable();
  371. // this can be called every frame in the menu to highlight
  372. // the multiplayer icon when a server is already up
  373. boolean NetworkServerAvailable();
  374. // returns "WiFi", "BlueTooth", or "" for display on the
  375. // main menu multiplayer icon
  376. const char *NetworkServerTransport();
  377. // this queries DNS for the actual address
  378. boolean ResolveNetworkServer( struct sockaddr *addr );
  379. // If we are starting a server instead of joining one, make
  380. // us available as a bonjour service until we start the game
  381. // or back out of the multiplayer menu. Returns false if
  382. // someone else grabbed it just before we could.
  383. boolean RegisterGameService();
  384. void TerminateGameService();
  385. // called by AsyncTic() to check for server state changes,
  386. // registers for service browsing on first call.
  387. void ProcessDNSMessages();
  388. // draw a graph of packets sent and received
  389. void ShowNet();
  390. void ShowMiniNet();
  391. //---------------------------------------
  392. // iphone_mapSelect.c
  393. //---------------------------------------
  394. // returns false if nothing was selected
  395. // if map->map is -1, the back button was hit instead of choosing a level
  396. boolean iphoneMapSelectMenu( mapStart_t *map );
  397. mapStats_t *FindMapStats( int dataset, int episode, int map, boolean create );
  398. const char *FindMapName( int dataset, int episode, int map );
  399. //---------------------------------------
  400. // iphone_start.c
  401. //
  402. // game harness routines
  403. //---------------------------------------
  404. void ResumeGame();
  405. boolean StartNetGame();
  406. void StartSaveGame();
  407. void StartSinglePlayerGame( mapStart_t map );
  408. void StartDemoGame( boolean timeDemoMode );
  409. //---------------------------------------
  410. // interfaces from the original game code
  411. //---------------------------------------
  412. void iphoneSetNotifyText( const char *str, ... );
  413. void iphoneIntermission( wbstartstruct_t* wbstartstruct );
  414. void iphoneStartLevel();
  415. void iphoneStartMusic();
  416. void iphoneStopMusic();
  417. void iphonePlayMusic( const char *name );
  418. void iphonePauseMusic();
  419. void iphoneResumeMusic();
  420. //---------------------------------------
  421. // interfaces to Objective-C land
  422. //---------------------------------------
  423. // The event thread will fill this after hitting enter
  424. // on the console. The game thread should check it,
  425. // execute it, and clear it under mutex.
  426. extern char consoleCommand[1024];
  427. void SysIPhoneSwapBuffers();
  428. void SysIPhoneVibrate();
  429. void SysIPhoneOpenURL( const char *url );
  430. void SysIPhoneSetUIKitOrientation( int isLandscapeRight );
  431. const char * SysIPhoneGetConsoleTextField();
  432. void SysIPhoneSetConsoleTextField(const char *);
  433. void SysIPhoneInitAudioSession();
  434. int SysIPhoneOtherAudioIsPlaying();
  435. int SysIphoneMilliseconds();
  436. int SysIphoneMicroseconds();
  437. const char * SysIphoneGetAppDir();
  438. const char * SysIphoneGetDocDir();
  439. //---------------------------------------
  440. // interfaces from Objective-C land
  441. //---------------------------------------
  442. void iphoneStartup();
  443. void iphoneShutdown();
  444. void iphoneFrame();
  445. void iphoneAsyncTic();
  446. void iphoneTiltEvent( float *tilts );