11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. |
12 * GNU General Public License for more details. |
13 * |
13 * |
14 * You should have received a copy of the GNU General Public License |
14 * You should have received a copy of the GNU General Public License |
15 * along with this program; if not, write to the Free Software |
15 * along with this program; if not, write to the Free Software |
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 *) |
17 *) |
18 |
18 |
19 {$INCLUDE "options.inc"} |
19 {$INCLUDE "options.inc"} |
20 |
20 |
21 {$IFDEF WIN32} |
21 {$IFDEF WIN32} |
22 {$R hwengine.rc} |
22 {$R res/hwengine.rc} |
23 {$ENDIF} |
23 {$ENDIF} |
24 |
24 |
25 {$IFDEF HWLIBRARY} |
25 {$IFDEF HWLIBRARY} |
26 unit hwengine; |
26 unit hwengine; |
27 interface |
27 interface |
28 {$ELSE} |
28 {$ELSE} |
29 program hwengine; |
29 program hwengine; |
30 {$ENDIF} |
30 {$ENDIF} |
31 |
31 |
32 uses SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler |
32 uses {$IFDEF IPHONEOS}cmem, {$ENDIF} SDLh, uMisc, uConsole, uGame, uConsts, uLand, uAmmos, uVisualGears, uGears, uStore, uWorld, uInputHandler |
33 , uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions |
33 , uSound, uScript, uTeams, uStats, uIO, uLocale, uChat, uAI, uAIMisc, uAILandMarks, uLandTexture, uCollisions |
34 , SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted |
34 , SysUtils, uTypes, uVariables, uCommands, uUtils, uCaptions, uDebug, uCommandHandlers, uLandPainted |
35 , uPhysFSLayer, uCursor, uRandom, ArgParsers, uVisualGearsHandlers, uTextures |
35 , uPhysFSLayer, uCursor, uRandom, ArgParsers, uVisualGearsHandlers, uTextures, uRender |
36 {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF} |
36 {$IFDEF USE_VIDEO_RECORDING}, uVideoRec {$ENDIF} |
37 {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF} |
37 {$IFDEF USE_TOUCH_INTERFACE}, uTouch {$ENDIF} |
38 {$IFDEF ANDROID}, GLUnit{$ENDIF} |
38 {$IFDEF ANDROID}, GLUnit{$ENDIF} |
39 ; |
39 ; |
40 |
40 |
41 {$IFDEF HWLIBRARY} |
41 {$IFDEF HWLIBRARY} |
|
42 procedure RunEngine(argc: LongInt; argv: PPChar); cdecl; export; |
|
43 |
42 procedure preInitEverything(); |
44 procedure preInitEverything(); |
43 procedure initEverything(complete:boolean); |
45 procedure initEverything(complete:boolean); |
44 procedure freeEverything(complete:boolean); |
46 procedure freeEverything(complete:boolean); |
45 procedure Game(argc: LongInt; argv: PPChar); cdecl; export; |
|
46 procedure GenLandPreview(port: Longint); cdecl; export; |
|
47 |
47 |
48 implementation |
48 implementation |
49 {$ELSE} |
49 {$ELSE} |
50 procedure preInitEverything(); forward; |
50 procedure preInitEverything(); forward; |
51 procedure initEverything(complete:boolean); forward; |
51 procedure initEverything(complete:boolean); forward; |
112 {$ENDIF} |
121 {$ENDIF} |
113 |
122 |
114 if flagMakeCapture then |
123 if flagMakeCapture then |
115 begin |
124 begin |
116 flagMakeCapture:= false; |
125 flagMakeCapture:= false; |
117 s:= '/Screenshots/hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks); |
126 if flagDumpLand then |
|
127 s:= '/Screenshots/mapdump_' |
|
128 else s:= '/Screenshots/hw_'; |
|
129 {$IFDEF PAS2C} |
|
130 s:= s + inttostr(GameTicks); |
|
131 {$ELSE} |
|
132 s:= s + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks); |
|
133 {$ENDIF} |
118 |
134 |
119 // flash |
135 // flash |
120 playSound(sndShutter); |
136 playSound(sndShutter); |
121 ScreenFade:= sfFromWhite; |
137 ScreenFade:= sfFromWhite; |
122 ScreenFadeValue:= sfMax; |
138 ScreenFadeValue:= sfMax; |
123 ScreenFadeSpeed:= 5; |
139 ScreenFadeSpeed:= 5; |
124 |
140 |
125 if MakeScreenshot(s, 1) then |
141 if (not flagDumpLand and MakeScreenshot(s, 1, 0)) or |
|
142 (flagDumpLand and MakeScreenshot(s, 1, 1) and ((cReducedQuality and rqBlurryLand <> 0) or MakeScreenshot(s, 1, 2))) then |
126 WriteLnToConsole('Screenshot saved: ' + s) |
143 WriteLnToConsole('Screenshot saved: ' + s) |
127 else |
144 else |
128 begin |
145 begin |
129 WriteLnToConsole('Screenshot failed.'); |
146 WriteLnToConsole('Screenshot failed.'); |
130 AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)'); |
147 AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)'); |
147 |
164 |
148 while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do |
165 while SDL_PeepEvents(@event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0 do |
149 begin |
166 begin |
150 case event.type_ of |
167 case event.type_ of |
151 SDL_KEYDOWN: |
168 SDL_KEYDOWN: |
152 if (GameState = gsChat) then |
169 if GameState = gsChat then |
153 KeyPressChat(event.key.keysym.sym) |
170 begin |
154 else if (GameState >= gsGame) then |
171 // sdl on iphone supports only ashii keyboards and the unicode field is deprecated in sdl 1.3 |
155 ProcessKey(event.key); |
172 KeyPressChat(SDL_GetKeyFromScancode(event.key.keysym.sym), event.key.keysym.sym, event.key.keysym.modifier); |
|
173 end |
|
174 else |
|
175 if GameState >= gsGame then ProcessKey(event.key); |
156 SDL_KEYUP: |
176 SDL_KEYUP: |
157 if (GameState <> gsChat) and (GameState >= gsGame) then |
177 if (GameState <> gsChat) and (GameState >= gsGame) then |
158 ProcessKey(event.key); |
178 ProcessKey(event.key); |
159 |
179 |
160 SDL_MOUSEBUTTONDOWN: |
180 SDL_MOUSEBUTTONDOWN: |
234 ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true); |
254 ParseCommand('fullscr '+intToStr(LongInt(cFullScreen)), true); |
235 WriteLnToConsole('window resize: ' + IntToStr(cScreenWidth) + ' x ' + IntToStr(cScreenHeight)); |
255 WriteLnToConsole('window resize: ' + IntToStr(cScreenWidth) + ' x ' + IntToStr(cScreenHeight)); |
236 ScriptOnScreenResize(); |
256 ScriptOnScreenResize(); |
237 InitCameraBorders(); |
257 InitCameraBorders(); |
238 InitTouchInterface(); |
258 InitTouchInterface(); |
|
259 InitZoom(zoomValue); |
239 SendIPC('W' + IntToStr(cScreenWidth) + 'x' + IntToStr(cScreenHeight)); |
260 SendIPC('W' + IntToStr(cScreenWidth) + 'x' + IntToStr(cScreenHeight)); |
240 end; |
261 end; |
241 |
262 |
242 CurrTime:= SDL_GetTicks(); |
263 CurrTime:= SDL_GetTicks(); |
243 if PrevTime + longword(cTimerInterval) <= CurrTime then |
264 if PrevTime + longword(cTimerInterval) <= CurrTime then |
244 begin |
265 begin |
245 isTerminated := isTerminated or DoTimer(CurrTime - PrevTime); |
266 isTerminated:= isTerminated or DoTimer(CurrTime - PrevTime); |
246 PrevTime:= CurrTime |
267 PrevTime:= CurrTime; |
247 end |
268 end |
248 else SDL_Delay(1); |
269 else SDL_Delay(1); |
249 IPCCheckSock(); |
270 IPCCheckSock(); |
|
271 |
250 end; |
272 end; |
251 end; |
273 end; |
252 |
274 |
253 {$IFDEF USE_VIDEO_RECORDING} |
275 {$IFDEF USE_VIDEO_RECORDING} |
254 procedure RecorderMainLoop; |
276 procedure RecorderMainLoop; |
282 StopVideoRecording(); |
304 StopVideoRecording(); |
283 end; |
305 end; |
284 {$ENDIF} |
306 {$ENDIF} |
285 |
307 |
286 /////////////////////////////////////////////////////////////////////////////// |
308 /////////////////////////////////////////////////////////////////////////////// |
287 procedure Game{$IFDEF HWLIBRARY}(argc: LongInt; argv: PPChar); cdecl; export{$ENDIF}; |
309 procedure Game; |
288 //var p: TPathType; |
310 //var p: TPathType; |
289 var s: shortstring; |
311 var s: shortstring; |
290 i: LongInt; |
312 i: LongInt; |
291 begin |
313 begin |
292 {$IFDEF HWLIBRARY} |
|
293 preInitEverything(); |
|
294 parseCommandLine(argc, argv); |
|
295 {$ENDIF} |
|
296 initEverything(true); |
314 initEverything(true); |
297 WriteLnToConsole('Hedgewars engine ' + cVersionString + '-r' + cRevisionString + |
315 WriteLnToConsole('Hedgewars engine ' + cVersionString + '-r' + cRevisionString + |
298 ' (' + cHashString + ') with protocol #' + inttostr(cNetProtoVersion)); |
316 ' (' + cHashString + ') with protocol #' + inttostr(cNetProtoVersion)); |
299 AddFileLog('Prefix: "' + PathPrefix +'"'); |
317 AddFileLog('Prefix: "' + shortstring(PathPrefix) +'"'); |
300 AddFileLog('UserPrefix: "' + UserPathPrefix +'"'); |
318 AddFileLog('UserPrefix: "' + shortstring(UserPathPrefix) +'"'); |
301 |
319 |
302 for i:= 0 to ParamCount do |
320 for i:= 0 to ParamCount do |
303 AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
321 AddFileLog(inttostr(i) + ': ' + ParamStr(i)); |
304 |
322 |
305 WriteToConsole('Init SDL... '); |
323 WriteToConsole('Init SDL... '); |
307 WriteLnToConsole(msgOK); |
325 WriteLnToConsole(msgOK); |
308 |
326 |
309 //SDL_StartTextInput(); |
327 //SDL_StartTextInput(); |
310 SDL_ShowCursor(0); |
328 SDL_ShowCursor(0); |
311 |
329 |
312 WriteToConsole('Init SDL_ttf... '); |
330 if not cOnlyStats then |
313 SDLTry(TTF_Init() <> -1, 'TTF_Init', true); |
331 begin |
314 WriteLnToConsole(msgOK); |
332 WriteToConsole('Init SDL_ttf... '); |
|
333 SDLTry(TTF_Init() <> -1, 'TTF_Init', true); |
|
334 WriteLnToConsole(msgOK); |
|
335 end; |
315 |
336 |
316 {$IFDEF USE_VIDEO_RECORDING} |
337 {$IFDEF USE_VIDEO_RECORDING} |
317 if GameType = gmtRecord then |
338 if GameType = gmtRecord then |
318 InitOffscreenOpenGL() |
339 InitOffscreenOpenGL() |
319 else |
340 else |
368 TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true); |
396 TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true); |
369 //ParseCommand('rotmask', true); |
397 //ParseCommand('rotmask', true); |
370 |
398 |
371 {$IFDEF USE_VIDEO_RECORDING} |
399 {$IFDEF USE_VIDEO_RECORDING} |
372 if GameType = gmtRecord then |
400 if GameType = gmtRecord then |
373 RecorderMainLoop() |
401 begin |
374 else |
402 RecorderMainLoop(); |
375 {$ENDIF} |
403 freeEverything(true); |
376 MainLoop(); |
404 exit; |
377 |
405 end; |
|
406 {$ENDIF} |
|
407 |
|
408 MainLoop; |
378 // clean up all the memory allocated |
409 // clean up all the memory allocated |
379 freeEverything(true); |
410 freeEverything(true); |
380 end; |
411 end; |
381 |
412 |
382 /////////////////////////////////////////////////////////////////////////////// |
413 /////////////////////////////////////////////////////////////////////////////// |
399 uCommandHandlers.initModule; // registers all messages from frontend |
430 uCommandHandlers.initModule; // registers all messages from frontend |
400 |
431 |
401 uLand.initModule; // computes land |
432 uLand.initModule; // computes land |
402 uLandPainted.initModule; // computes drawn land |
433 uLandPainted.initModule; // computes drawn land |
403 uIO.initModule; // sets up sockets |
434 uIO.initModule; // sets up sockets |
|
435 uPhysFSLayer.initModule; |
|
436 uScript.initModule; |
404 |
437 |
405 if complete then |
438 if complete then |
406 begin |
439 begin |
407 uPhysFSLayer.initModule; |
|
408 uTextures.initModule; |
440 uTextures.initModule; |
409 {$IFDEF ANDROID}GLUnit.initModule;{$ENDIF} |
441 {$IFDEF ANDROID}GLUnit.initModule;{$ENDIF} |
410 {$IFDEF USE_TOUCH_INTERFACE}uTouch.initModule;{$ENDIF} |
442 {$IFDEF USE_TOUCH_INTERFACE}uTouch.initModule;{$ENDIF} |
411 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.initModule;{$ENDIF} |
443 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.initModule;{$ENDIF} |
412 uAI.initModule; |
444 uAI.initModule; |
419 uCollisions.initModule; |
451 uCollisions.initModule; |
420 uGears.initModule; |
452 uGears.initModule; |
421 uInputHandler.initModule; |
453 uInputHandler.initModule; |
422 uMisc.initModule; |
454 uMisc.initModule; |
423 uLandTexture.initModule; //stub |
455 uLandTexture.initModule; //stub |
424 uScript.initModule; |
|
425 uSound.initModule; |
456 uSound.initModule; |
426 uStats.initModule; |
457 uStats.initModule; |
427 uStore.initModule; |
458 uStore.initModule; |
|
459 uRender.initModule; |
428 uTeams.initModule; |
460 uTeams.initModule; |
429 uVisualGears.initModule; |
461 uVisualGears.initModule; |
430 uVisualGearsHandlers.initModule; |
462 uVisualGearsHandlers.initModule; |
431 uWorld.initModule; |
463 uWorld.initModule; |
432 end; |
464 end; |
433 end; |
465 end; |
434 |
466 |
435 procedure freeEverything (complete:boolean); |
467 procedure freeEverything (complete:boolean); |
436 begin |
468 begin |
437 if complete then |
469 if complete then |
438 begin |
470 begin |
439 WriteLnToConsole('Freeing resources...'); |
471 WriteLnToConsole('Freeing resources...'); |
440 uAI.freeModule; // AI things need to be freed first |
472 uAI.freeModule; // AI things need to be freed first |
441 uAIMisc.freeModule; //stub |
473 uAIMisc.freeModule; //stub |
442 uAILandMarks.freeModule; |
474 uAILandMarks.freeModule; |
443 uCaptions.freeModule; |
475 uCaptions.freeModule; |
445 uVisualGears.freeModule; |
477 uVisualGears.freeModule; |
446 uTeams.freeModule; |
478 uTeams.freeModule; |
447 uInputHandler.freeModule; |
479 uInputHandler.freeModule; |
448 uStats.freeModule; //stub |
480 uStats.freeModule; //stub |
449 uSound.freeModule; |
481 uSound.freeModule; |
450 uScript.freeModule; |
|
451 uMisc.freeModule; |
482 uMisc.freeModule; |
452 uLandTexture.freeModule; |
483 uLandTexture.freeModule; |
453 uGears.freeModule; |
484 uGears.freeModule; |
454 uCollisions.freeModule; //stub |
485 uCollisions.freeModule; //stub |
455 uChat.freeModule; |
486 uChat.freeModule; |
456 uAmmos.freeModule; |
487 uAmmos.freeModule; |
|
488 uRender.freeModule; |
457 uStore.freeModule; // closes SDL |
489 uStore.freeModule; // closes SDL |
458 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF} |
490 {$IFDEF USE_VIDEO_RECORDING}uVideoRec.freeModule;{$ENDIF} |
459 {$IFDEF USE_TOUCH_INTERFACE}uTouch.freeModule;{$ENDIF} //stub |
491 {$IFDEF USE_TOUCH_INTERFACE}uTouch.freeModule;{$ENDIF} //stub |
460 {$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF} |
492 {$IFDEF ANDROID}GLUnit.freeModule;{$ENDIF} |
461 uTextures.freeModule; |
493 uTextures.freeModule; |
462 uPhysFSLayer.freeModule; |
494 end; |
463 end; |
|
464 |
495 |
465 uIO.freeModule; |
496 uIO.freeModule; |
466 uLand.freeModule; |
497 uLand.freeModule; |
467 uLandPainted.freeModule; |
498 uLandPainted.freeModule; |
468 |
499 |
469 uCommandHandlers.freeModule; |
500 uCommandHandlers.freeModule; |
470 uCommands.freeModule; |
501 uCommands.freeModule; |
471 uVariables.freeModule; |
502 uVariables.freeModule; |
472 uUtils.freeModule; // closes debug file |
503 uUtils.freeModule; // closes debug file |
473 end; |
504 uPhysFSLayer.freeModule; |
474 |
505 uScript.freeModule; |
475 /////////////////////////////////////////////////////////////////////////////// |
506 end; |
476 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF}; |
507 |
|
508 /////////////////////////////////////////////////////////////////////////////// |
|
509 procedure GenLandPreview; |
|
510 {$IFDEF MOBILE} |
477 var Preview: TPreview; |
511 var Preview: TPreview; |
|
512 {$ELSE} |
|
513 var Preview: TPreviewAlpha; |
|
514 {$ENDIF} |
478 begin |
515 begin |
479 initEverything(false); |
516 initEverything(false); |
480 {$IFDEF HWLIBRARY} |
517 |
481 WriteLnToConsole('Preview connecting on port ' + inttostr(port)); |
|
482 ipcPort:= port; |
|
483 InitStepsFlags:= cifRandomize; |
|
484 {$ENDIF} |
|
485 InitIPC; |
518 InitIPC; |
486 IPCWaitPongEvent; |
519 IPCWaitPongEvent; |
487 TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true); |
520 TryDo(InitStepsFlags = cifRandomize, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true); |
488 |
521 |
|
522 ScriptOnPreviewInit; |
|
523 {$IFDEF MOBILE} |
489 GenPreview(Preview); |
524 GenPreview(Preview); |
|
525 {$ELSE} |
|
526 GenPreviewAlpha(Preview); |
|
527 {$ENDIF} |
490 WriteLnToConsole('Sending preview...'); |
528 WriteLnToConsole('Sending preview...'); |
491 SendIPCRaw(@Preview, sizeof(Preview)); |
529 SendIPCRaw(@Preview, sizeof(Preview)); |
492 SendIPCRaw(@MaxHedgehogs, sizeof(byte)); |
530 SendIPCRaw(@MaxHedgehogs, sizeof(byte)); |
493 WriteLnToConsole('Preview sent, disconnect'); |
531 WriteLnToConsole('Preview sent, disconnect'); |
494 freeEverything(false); |
532 freeEverything(false); |
495 end; |
533 end; |
496 |
534 |
497 {$IFNDEF HWLIBRARY} |
535 {$IFDEF HWLIBRARY} |
|
536 procedure RunEngine(argc: LongInt; argv: PPChar); cdecl; export; |
|
537 begin |
|
538 operatingsystem_parameter_argc:= argc; |
|
539 operatingsystem_parameter_argv:= argv; |
|
540 {$ELSE} |
|
541 begin |
|
542 {$ENDIF} |
498 |
543 |
499 /////////////////////////////////////////////////////////////////////////////// |
544 /////////////////////////////////////////////////////////////////////////////// |
500 /////////////////////////////////// m a i n /////////////////////////////////// |
545 /////////////////////////////////// m a i n /////////////////////////////////// |
501 /////////////////////////////////////////////////////////////////////////////// |
546 /////////////////////////////////////////////////////////////////////////////// |
502 begin |
547 {$IFDEF PAS2C} |
|
548 // workaround for pascal's ParamStr and ParamCount |
|
549 init(argc, argv); |
|
550 {$ENDIF} |
503 preInitEverything(); |
551 preInitEverything(); |
504 cTagsMask:= htTeamName or htName or htHealth; // this one doesn't fit nicely w/ reset of other variables. suggestions welcome |
552 |
505 GetParams(); |
553 GetParams(); |
506 |
554 |
507 if GameType = gmtLandPreview then |
555 if GameType = gmtLandPreview then |
508 GenLandPreview() |
556 GenLandPreview() |
509 else if GameType <> gmtSyntax then |
557 else if GameType <> gmtSyntax then |
510 Game(); |
558 Game(); |
511 |
559 |
512 // return 1 when engine is not called correctly |
560 // return 1 when engine is not called correctly |
513 halt(LongInt(GameType = gmtSyntax)); |
561 if GameType = gmtSyntax then |
514 {$ENDIF} |
562 {$IFDEF PAS2C} |
|
563 exit(HaltUsageError); |
|
564 {$ELSE} |
|
565 halt(HaltUsageError); |
|
566 {$ENDIF} |
|
567 |
|
568 if cTestLua then |
|
569 begin |
|
570 WriteLnToConsole(errmsgLuaTestTerm); |
|
571 {$IFDEF PAS2C} |
|
572 exit(HaltTestUnexpected); |
|
573 {$ELSE} |
|
574 halt(HaltTestUnexpected); |
|
575 {$ENDIF} |
|
576 end; |
|
577 |
|
578 {$IFDEF PAS2C} |
|
579 exit(HaltNoError); |
|
580 {$ELSE} |
|
581 {$IFDEF IPHONEOS} |
|
582 exit; |
|
583 {$ELSE} |
|
584 halt(HaltNoError); |
|
585 {$ENDIF} |
|
586 {$ENDIF} |
|
587 {$IFDEF HWLIBRARY} |
|
588 end; |
|
589 {$ENDIF} |
|
590 |
515 end. |
591 end. |