hedgewars/uMisc.pas
changeset 2163 12730f5e79b9
parent 2162 2bce91404d49
child 2171 8208946331ba
equal deleted inserted replaced
2162:2bce91404d49 2163:12730f5e79b9
   148 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
   148 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
   149 function  Surface2Tex(surf: PSDL_Surface): PTexture;
   149 function  Surface2Tex(surf: PSDL_Surface): PTexture;
   150 procedure FreeTexture(tex: PTexture);
   150 procedure FreeTexture(tex: PTexture);
   151 function  toPowerOf2(i: Longword): Longword;
   151 function  toPowerOf2(i: Longword): Longword;
   152 function DecodeBase64(s: shortstring): shortstring;
   152 function DecodeBase64(s: shortstring): shortstring;
       
   153 {$IFNDEF IPHONEOS}
   153 procedure MakeScreenshot(s: shortstring);
   154 procedure MakeScreenshot(s: shortstring);
       
   155 {$ENDIF}
       
   156 
   154 function modifyDamage(dmg: Longword): Longword;
   157 function modifyDamage(dmg: Longword): Longword;
   155 
   158 
   156 var CursorPoint: TPoint;
   159 var CursorPoint: TPoint;
   157     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   160     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   158 
   161 
   329 ResetVertexArrays(NewTexture);
   332 ResetVertexArrays(NewTexture);
   330 
   333 
   331 glGenTextures(1, @NewTexture^.id);
   334 glGenTextures(1, @NewTexture^.id);
   332 
   335 
   333 glBindTexture(GL_TEXTURE_2D, NewTexture^.id);
   336 glBindTexture(GL_TEXTURE_2D, NewTexture^.id);
   334 
   337 {$IFDEF IPHONEOS}
       
   338 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, buf);
       
   339 {$ELSE}
   335 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
   340 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
       
   341 {$ENDIF}
   336 
   342 
   337 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   343 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   338 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
   344 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
   339 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   345 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   340 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   341 end;
   347 end;
   342 
   348 
   343 function Surface2Tex(surf: PSDL_Surface): PTexture;
   349 function Surface2Tex(surf: PSDL_Surface): PTexture;
   344 var mode: LongInt;
   350 var modeIntFormat: LongInt;
       
   351     modeFormat: LongInt;
   345 	tw, th, x, y: Longword;
   352 	tw, th, x, y: Longword;
   346 	tmpp: pointer;
   353 	tmpp: pointer;
   347 	fromP4, toP4: PLongWordArray;
   354 	fromP4, toP4: PLongWordArray;
   348 	fromP1, toP1: PByteArray;
   355 	fromP1, toP1: PByteArray;
   349 begin
   356 begin
   350 new(Surface2Tex);
   357 new(Surface2Tex);
   351 Surface2Tex^.w:= surf^.w;
   358 Surface2Tex^.w:= surf^.w;
   352 Surface2Tex^.h:= surf^.h;
   359 Surface2Tex^.h:= surf^.h;
   353 
   360 
   354 if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else
   361 if (surf^.format^.BytesPerPixel = 3) then 
   355 if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else
   362 	begin
       
   363 		modeIntFormat:= GL_RGB;
       
   364 		{$IFDEF IPHONEOS}
       
   365 		modeFormat:= GL_BGR;
       
   366 		{$ELSE}
       
   367 		modeFormat:=modeIntFormat;
       
   368 		{$ENDIF}
       
   369 	end
       
   370 else
       
   371 if (surf^.format^.BytesPerPixel = 4) then
       
   372 	begin
       
   373 		modeIntFormat:= GL_RGBA;
       
   374 		{$IFDEF IPHONEOS}
       
   375 		modeFormat:=GL_BGRA;
       
   376 		{$ELSE}
       
   377 		modeFormat:=modeIntFormat;
       
   378 		{$ENDIF}
       
   379 	end
       
   380 else
   356    begin
   381    begin
   357    TryDo(false, 'Surface2Tex: BytesPerPixel not in [3, 4]', true);
   382    TryDo(false, 'Surface2Tex: BytesPerPixel not in [3, 4]', true);
   358    Surface2Tex^.id:= 0;
   383    Surface2Tex^.id:= 0;
   359    exit
   384    exit
   360    end;
   385    end;
   431 				end;
   456 				end;
   432 			toP1:= @(toP1^[tw * 3]);
   457 			toP1:= @(toP1^[tw * 3]);
   433 			end;
   458 			end;
   434 		end;
   459 		end;
   435 
   460 
   436 //   gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE,
   461 //	legacy resizing function
   437 //        Surf^.pixels, tw, th, GL_UNSIGNED_BYTE,
   462 //	gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE, Surf^.pixels, tw, th, GL_UNSIGNED_BYTE, tmpp);
   438 //        tmpp);
   463 	
   439 
   464 	glTexImage2D(GL_TEXTURE_2D, 0, modeIntFormat, tw, th, 0, modeFormat, GL_UNSIGNED_BYTE, tmpp);
   440 	glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp);
       
   441 	
   465 	
   442 	FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
   466 	FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
   443 	end else
   467 	end else
   444 	begin
   468 	begin
   445 	Surface2Tex^.rx:= 1.0;
   469 	Surface2Tex^.rx:= 1.0;
   446 	Surface2Tex^.ry:= 1.0;
   470 	Surface2Tex^.ry:= 1.0;
   447 	glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels);
   471 	glTexImage2D(GL_TEXTURE_2D, 0, modeIntFormat, surf^.w, surf^.h, 0, modeFormat, GL_UNSIGNED_BYTE, surf^.pixels);
   448 	end;
   472 	end;
   449 
   473 
   450 ResetVertexArrays(Surface2Tex);
   474 ResetVertexArrays(Surface2Tex);
   451 
   475 
   452 if SDL_MustLock(surf) then
   476 if SDL_MustLock(surf) then
   493 if c < 3 then t:= t - c;
   517 if c < 3 then t:= t - c;
   494 
   518 
   495 byte(DecodeBase64[0]):= t - 1
   519 byte(DecodeBase64[0]):= t - 1
   496 end;
   520 end;
   497 
   521 
   498 const GL_BGR = $80E0; // some opengl headers don't have that const (?)'
   522 {$IFNDEF IPHONEOS}
   499 procedure MakeScreenshot(s: shortstring);
   523 procedure MakeScreenshot(s: shortstring);
   500 const head: array[0..8] of Word = (0, 2, 0, 0, 0, 0, 0, 0, 24);
   524 const head: array[0..8] of Word = (0, 2, 0, 0, 0, 0, 0, 0, 24);
   501 var p: Pointer;
   525 var p: Pointer;
   502 	size: Longword;
   526 	size: Longword;
   503 	f: file;
   527 	f: file;
   506 head[7]:= cScreenHeight;
   530 head[7]:= cScreenHeight;
   507 
   531 
   508 size:= cScreenWidth * cScreenHeight * 3;
   532 size:= cScreenWidth * cScreenHeight * 3;
   509 p:= GetMem(size);
   533 p:= GetMem(size);
   510 
   534 
   511 {$IFDEF IPHONEOS}
   535 
   512 //since opengl es operates on a single surface GL_FRONT is implied, but how to test that?
   536 //remember that opengles operates on a single surface, so GL_FRONT *should* be implied
   513 {$ELSE}
       
   514 glReadBuffer(GL_FRONT);
   537 glReadBuffer(GL_FRONT);
   515 {$ENDIF}
       
   516 glReadPixels(0, 0, cScreenWidth, cScreenHeight, GL_BGR, GL_UNSIGNED_BYTE, p);
   538 glReadPixels(0, 0, cScreenWidth, cScreenHeight, GL_BGR, GL_UNSIGNED_BYTE, p);
   517 
   539 
   518 {$I-}
   540 {$I-}
   519 Assign(f, s);
   541 Assign(f, s);
   520 Rewrite(f, 1);
   542 Rewrite(f, 1);
   526 	end;
   548 	end;
   527 {$I+}
   549 {$I+}
   528 
   550 
   529 FreeMem(p)
   551 FreeMem(p)
   530 end;
   552 end;
       
   553 {$ENDIF}
   531 
   554 
   532 function modifyDamage(dmg: Longword): Longword;
   555 function modifyDamage(dmg: Longword): Longword;
   533 begin
   556 begin
   534 ModifyDamage:= hwRound(_0_01 * cDamageModifier * dmg * cDamagePercent)
   557 ModifyDamage:= hwRound(_0_01 * cDamageModifier * dmg * cDamagePercent)
   535 end;
   558 end;