hedgewars/hwengine.pas
changeset 2678 334016e8d895
parent 2677 83ad68ceef72
child 2682 d4c395f25db2
equal deleted inserted replaced
2677:83ad68ceef72 2678:334016e8d895
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 program hwengine;
    21 program hwengine;
    22 uses	SDLh in 'SDLh.pas',
    22 uses
       
    23 	SDLh in 'SDLh.pas',
    23 {$IFDEF GLES11}
    24 {$IFDEF GLES11}
    24 	gles11,
    25 	gles11,
    25 {$ELSE}
    26 {$ELSE}
    26 	GL,
    27 	GL,
    27 {$ENDIF}
    28 {$ENDIF}
   114 			end;
   115 			end;
   115 	end;
   116 	end;
   116 
   117 
   117 SDL_GL_SwapBuffers();
   118 SDL_GL_SwapBuffers();
   118 {$IFNDEF IPHONEOS}
   119 {$IFNDEF IPHONEOS}
   119 // not going to make captures on the iPhone
   120 // not going to make captures on the iPhone (nor resizing)
   120 if flagMakeCapture then
   121 if flagMakeCapture then
   121 	begin
   122 	begin
   122 	flagMakeCapture:= false;
   123 	flagMakeCapture:= false;
   123 	s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.tga';
   124 	s:= 'hw_' + cSeed + '_' + inttostr(GameTicks) + '.tga';
   124 	WriteLnToConsole('Saving ' + s);
   125 	WriteLnToConsole('Saving ' + s);
   125 	MakeScreenshot(s);
   126 	MakeScreenshot(s);
   126 //	SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1)
   127 //	SDL_SaveBMP_RW(SDLPrimSurface, SDL_RWFromFile(Str2PChar(s), 'wb'), 1)
   127 	end;
   128 	end;
       
   129 end;
       
   130 
       
   131 ////////////////////////////////
       
   132 procedure Resize(w, h: LongInt);
       
   133 begin
       
   134 cScreenWidth:= w;
       
   135 cScreenHeight:= h;
       
   136 if cFullScreen then
       
   137 	ParseCommand('/fullscr 1', true)
       
   138 else
       
   139 	ParseCommand('/fullscr 0', true);
   128 {$ENDIF}
   140 {$ENDIF}
   129 end;
   141 end;
   130 
   142 
   131 ////////////////////
   143 ////////////////////
   132 procedure OnDestroy;
   144 procedure OnDestroy;
   140 TTF_Quit;
   152 TTF_Quit;
   141 SDL_Quit;
   153 SDL_Quit;
   142 halt
   154 halt
   143 end;
   155 end;
   144 
   156 
   145 ////////////////////////////////
       
   146 procedure Resize(w, h: LongInt);
       
   147 begin
       
   148 cScreenWidth:= w;
       
   149 cScreenHeight:= h;
       
   150 if cFullScreen then
       
   151 	ParseCommand('/fullscr 1', true)
       
   152 else
       
   153 	ParseCommand('/fullscr 0', true);
       
   154 end;
       
   155 
       
   156 ///////////////////
   157 ///////////////////
   157 procedure MainLoop;
   158 procedure MainLoop;
   158 var PrevTime,
   159 var PrevTime,
   159     CurrTime: Longword;
   160     CurrTime: Longword;
   160     event: TSDL_Event;
   161     event: TSDL_Event;
   161 {$IFDEF TOUCHINPUT}
   162 {$IFDEF TOUCHINPUT}
   162 type TDirection = (nodir, left, right);
   163 type TDirection = (nodir, left, right);
   163 var x, y, dx, dy, mouseState: LongInt;
   164 var tiltValue: LongInt;
   164 //    tiltValue: LongInt;
       
   165     direction: TDirection = nodir;
   165     direction: TDirection = nodir;
   166     movedbybuttons: boolean = false;
   166     movedbybuttons: boolean = false;
   167 {$ENDIF}
   167 {$ENDIF}
   168 begin
   168 begin
   169 PrevTime:= SDL_GetTicks;
   169 PrevTime:= SDL_GetTicks;
   179 end
   179 end
   180 else uKeys.isWalking:= false;
   180 else uKeys.isWalking:= false;
   181 {$ENDIF}
   181 {$ENDIF}
   182 	case event.type_ of
   182 	case event.type_ of
   183 {$IFDEF SDL13}
   183 {$IFDEF SDL13}
   184                 SDL_WINDOWEVENT:
   184 		SDL_WINDOWEVENT:
   185 {$ELSE}
   185 {$ELSE}
   186 		SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode);
   186 		SDL_KEYDOWN: if GameState = gsChat then KeyPressChat(event.key.keysym.unicode);
   187                 SDL_ACTIVEEVENT:
   187 		SDL_ACTIVEEVENT:
   188 {$ENDIF}
   188 {$ENDIF}
   189                         if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   189 			if (event.active.state and SDL_APPINPUTFOCUS) <> 0 then
   190 				cHasFocus:= event.active.gain = 1;
   190 				cHasFocus:= event.active.gain = 1;
   191 		//SDL_VIDEORESIZE: Resize(max(event.resize.w, 600), max(event.resize.h, 450));
   191 		//SDL_VIDEORESIZE: Resize(max(event.resize.w, 600), max(event.resize.h, 450));
   192 {$IFDEF TOUCHINPUT}
       
   193                 SDL_MOUSEMOTION: begin
       
   194 {$IFDEF DEBUGFILE}
       
   195                         AddFileLog('*********************************************       motion');
       
   196 {$ENDIF}
       
   197 						SDL_SelectMouse(event.motion.which);
       
   198                         mouseState:= SDL_GetMouseState(@x, @y);
       
   199                         SDL_GetRelativeMouseState(event.motion.which, @dx, @dy);
       
   200                         
       
   201                         direction:= nodir;
       
   202 
       
   203                         if boolean(mouseState) then
       
   204                         begin
       
   205 {$IFDEF DEBUGFILE}		
       
   206                                 AddFileLog('x: ' + inttostr(x) + ' y: ' + inttostr(y) + ' dx: ' + inttostr(dx) + ' dy: ' + inttostr(dy));
       
   207 {$ENDIF}
       
   208 
       
   209                                 {* zoom slider *}
       
   210                                 if (x <= 50) and (y <= 430) and (y > 50) then 
       
   211                                 begin
       
   212                                         if (dy > 0) then uKeys.wheelDown:= true
       
   213                                         else if (dy < 0) then uKeys.wheelUp:= true;
       
   214                                 end;
       
   215 
       
   216                                 {* aim slider *}
       
   217                                 if (x > 270) and (y > 50) and (y <= 430) then
       
   218                                 begin
       
   219                                         if (dy > 0) then uKeys.downKey:= true
       
   220                                         else if (dy < 0) then uKeys.upKey:= true;
       
   221                                 end;
       
   222 
       
   223                                 {* switch *}
       
   224                                 if (x > 50) and (x <= 270) and (y > 400) then
       
   225                                 begin
       
   226                                         if (dy <> 0) then uKeys.tabKey:= true
       
   227                                 end;
       
   228                         end;
       
   229                 end;
       
   230         {*MoveCamera is in uWord.pas -- conflicts with other commands*}
       
   231                 SDL_MOUSEBUTTONDOWN: begin
       
   232 {$IFDEF DEBUGFILE}
       
   233                         AddFileLog('*********************************************       touch down');
       
   234 {$ENDIF}
       
   235 						SDL_SelectMouse(event.motion.which);
       
   236                         mouseState:= SDL_GetMouseState(@x, @y);
       
   237                         SDL_GetRelativeMouseState(event.motion.which, @dx, @dy);
       
   238                         {* attack *}
       
   239                         if (x > 50) and (x <= 270) and (y <= 50) then
       
   240                         begin
       
   241                                 WriteLnToConsole('Space DOWN -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
       
   242                                 uKeys.spaceKey:= true;
       
   243                                 uKeys.isAttacking:= true;
       
   244                         end;
       
   245 
       
   246                         if (x <= 50) and (y <= 50) then
       
   247                         begin
       
   248                                 WriteLnToConsole('Left Arrow Key DOWN -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
       
   249                                 direction:= left;
       
   250 				movedbybuttons:= true;
       
   251                         end;
       
   252                         
       
   253                         if (x > 270) and (y <= 50) then
       
   254                         begin
       
   255                                 WriteLnToConsole('Right Arrow Key DOWN -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
       
   256                                 direction:= right;
       
   257 				movedbybuttons:= true;
       
   258                         end;
       
   259 
       
   260                         {* high jump *}                        
       
   261                         if (x > 160) and (x <= 270) and (y > 400) then
       
   262                         begin
       
   263 {$IFDEF DEBUGFILE}
       
   264                                 AddFileLog('Backspace -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
       
   265 {$ENDIF}
       
   266                                 uKeys.backspaceKey:= true;
       
   267                         end;
       
   268                 end;
       
   269                 SDL_MOUSEBUTTONUP: begin
       
   270 {$IFDEF DEBUGFILE}
       
   271                         AddFileLog('*********************************************       touch up');
       
   272 {$ENDIF}
       
   273 
       
   274 						SDL_SelectMouse(event.motion.which);
       
   275                         mouseState:= SDL_GetMouseState(@x, @y);
       
   276                         SDL_GetRelativeMouseState(event.motion.which, @dx, @dy);
       
   277 						uKeys.leftClick:= true;
       
   278 
       
   279                         {* open ammo menu *}
       
   280                         if (y > 430) and (x > 270) then
       
   281                         begin
       
   282 {$IFDEF DEBUGFILE}
       
   283                                 AddFileLog('Right Click -- x: ' + inttostr(x) + ' y: ' + inttostr(y) );
       
   284 {$ENDIF}
       
   285                                 uKeys.rightClick:= true;
       
   286                         end;
       
   287 
       
   288                         {* long jump *}                        
       
   289                         if (x > 50) and (x <= 160) and (y > 400) then
       
   290                         begin
       
   291 {$IFDEF DEBUGFILE}
       
   292                                 AddFileLog('Enter -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
       
   293 {$ENDIF}
       
   294                                 uKeys.enterKey:= true;
       
   295                         end;
       
   296 
       
   297                         {* reset zoom *}
       
   298                         if (x <= 50) and (y > 430) then
       
   299                         begin
       
   300 {$IFDEF DEBUGFILE}
       
   301                                 AddFileLog('Middle Click -- x: ' + inttostr(x) + ' y: ' + inttostr(y));
       
   302 {$ENDIF}
       
   303                                 uKeys.middleClick:= true;
       
   304                         end;
       
   305 
       
   306                         {* end movement and attack *}
       
   307 {$IFDEF DEBUGFILE}
       
   308                         AddFileLog('Arrow Keys UP | Space UP -- x: ' + inttostr(x) + ' y: ' + inttostr(y) );
       
   309 {$ENDIF}
       
   310                         direction:= nodir;
       
   311 			movedbybuttons:= false;
       
   312                         uKeys.isAttacking:= false;
       
   313 		end;
       
   314 {$ENDIF}
       
   315 {$IFDEF IPHONEOS}
   192 {$IFDEF IPHONEOS}
   316 (*		SDL_JOYAXISMOTION: begin
   193 (*		SDL_JOYAXISMOTION: begin
   317                 {* axis 0 = left and right;
   194                 {* axis 0 = left and right;
   318                    axis 1 = up and down;
   195                    axis 1 = up and down;
   319                    axis 2 = back and forth; *}
   196                    axis 2 = back and forth; *}
   320 
   197 
   321                         WriteLnToConsole('*********************************************       accelerometer');
   198 			WriteLnToConsole('*********************************************       accelerometer');
   322 			
   199 			
   323 			tiltValue:= SDL_JoystickGetAxis(uKeys.theJoystick, 0);
   200 			tiltValue:= SDL_JoystickGetAxis(uKeys.theJoystick, 0);
   324 
   201 
   325                         if (CurrentTeam <> nil) then
   202 			if (CurrentTeam <> nil) then
   326                         begin
   203 			begin
   327 {$IFDEF DEBUGFILE}
   204 {$IFDEF DEBUGFILE}
   328 				AddFileLog('Joystick: 0; Axis: 0; Value: ' + inttostr(tiltValue));
   205 				AddFileLog('Joystick: 0; Axis: 0; Value: ' + inttostr(tiltValue));
   329 {$ENDIF}
   206 {$ENDIF}
   330 
   207 					if tiltValue > 1500 then
   331                                 if tiltValue > 1500 then
   208 					begin
   332                                 begin
   209 						uKeys.rightKey:= true;
   333 					uKeys.rightKey:= true;
       
   334 					uKeys.isWalking:= true;
       
   335                                 end
       
   336                                 else
       
   337                                         if tiltValue <= -1500 then
       
   338                                         begin 
       
   339 						uKeys.leftKey:= true;
       
   340 						uKeys.isWalking:= true;
   210 						uKeys.isWalking:= true;
   341                                         end
   211 					end
   342                                         else
   212 					else
   343                                                 if (tiltValue  > -1500) and (tiltValue <= 1500) and (movedbybuttons = false) then uKeys.isWalking:= false;  
   213 						if tiltValue <= -1500 then
       
   214 						begin 
       
   215 							uKeys.leftKey:= true;
       
   216 							uKeys.isWalking:= true;
       
   217 						end
       
   218 						else
       
   219 							if (tiltValue  > -1500) and (tiltValue <= 1500) and (movedbybuttons = false) then uKeys.isWalking:= false;  
   344                         end;
   220                         end;
   345                 end;*)
   221 			end;*)
   346 {$ELSE}
   222 {$ELSE}
   347 		SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
   223 		SDL_MOUSEBUTTONDOWN: if event.button.button = SDL_BUTTON_WHEELDOWN then uKeys.wheelDown:= true;
   348 		SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then uKeys.wheelUp:= true;
   224 		SDL_MOUSEBUTTONUP: if event.button.button = SDL_BUTTON_WHEELUP then uKeys.wheelUp:= true;
   349 		SDL_JOYAXISMOTION: ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   225 		SDL_JOYAXISMOTION: ControllerAxisEvent(event.jaxis.which, event.jaxis.axis, event.jaxis.value);
   350 		SDL_JOYHATMOTION: ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);
   226 		SDL_JOYHATMOTION: ControllerHatEvent(event.jhat.which, event.jhat.hat, event.jhat.value);