hedgewars/uMisc.pas
changeset 771 86fbe8753a7c
parent 762 5ecf042f6113
child 775 23c253aae336
equal deleted inserted replaced
770:0138f1b5a804 771:86fbe8753a7c
   121 
   121 
   122 var CursorPoint: TPoint;
   122 var CursorPoint: TPoint;
   123     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   123     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   124 
   124 
   125 implementation
   125 implementation
   126 uses uConsole, uStore, uIO, Math, uRandom;
   126 uses uConsole, uStore, uIO, Math, uRandom, GLU;
   127 var KBnum: Longword = 0;
   127 var KBnum: Longword = 0;
   128 {$IFDEF DEBUGFILE}
   128 {$IFDEF DEBUGFILE}
   129 var f: textfile;
   129 var f: textfile;
   130 {$ENDIF}
   130 {$ENDIF}
   131 
   131 
   253 function RectToStr(Rect: TSDL_Rect): shortstring;
   253 function RectToStr(Rect: TSDL_Rect): shortstring;
   254 begin
   254 begin
   255 RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')'
   255 RectToStr:= '(x: ' + inttostr(rect.x) + '; y: ' + inttostr(rect.y) + '; w: ' + inttostr(rect.w) + '; h: ' + inttostr(rect.h) + ')'
   256 end;
   256 end;
   257 
   257 
       
   258 function isPowerOf2(i: Longword): boolean;
       
   259 begin
       
   260 if i = 0 then exit(true);
       
   261 while (i and 1) = 0 do i:= i shr 1;
       
   262 isPowerOf2:= (i = 1)
       
   263 end;
       
   264 
       
   265 function toPowerOf2(i: Longword): Longword;
       
   266 begin
       
   267 toPowerOf2:= 1;
       
   268 while (toPowerOf2 < i) do toPowerOf2:= toPowerOf2 shl 1
       
   269 end;
       
   270 
   258 function Surface2Tex(surf: PSDL_Surface): PTexture;
   271 function Surface2Tex(surf: PSDL_Surface): PTexture;
   259 var mode: LongInt;
   272 var mode: LongInt;
   260     texId: GLuint;
   273     texId: GLuint;
   261 begin
   274     tw, th: Longword;
   262 if SDL_MustLock(surf) then
   275     tmpp: pointer;
   263    SDLTry(SDL_LockSurface(surf) >= 0, true);
   276 begin
   264 
       
   265 new(Surface2Tex);
   277 new(Surface2Tex);
   266 Surface2Tex^.w:= surf^.w;
   278 Surface2Tex^.w:= surf^.w;
   267 Surface2Tex^.h:= surf^.h;
   279 Surface2Tex^.h:= surf^.h;
   268 
   280 
   269 if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else
   281 if (surf^.format^.BytesPerPixel = 3) then mode:= GL_RGB else
   270 if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else
   282 if (surf^.format^.BytesPerPixel = 4) then mode:= GL_RGBA else
   271    begin
   283    begin
   272    TryDo(false, 'Surface2Tex: BytePerPixel not in [3, 4]', false);
   284    TryDo(false, 'Surface2Tex: BytePerPixel not in [3, 4', false);
   273    Surface2Tex^.id:= 0;
   285    Surface2Tex^.id:= 0;
   274    exit
   286    exit
   275    end;
   287    end;
   276 
   288 
   277 glGenTextures(1, @Surface2Tex^.id);
   289 glGenTextures(1, @Surface2Tex^.id);
   278 
   290 
   279 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
   291 glBindTexture(GL_TEXTURE_2D, Surface2Tex^.id);
   280 
   292 
   281 glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels);
   293 if SDL_MustLock(surf) then
       
   294    SDLTry(SDL_LockSurface(surf) >= 0, true);
       
   295 
       
   296 if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then
       
   297    begin
       
   298    tw:= toPowerOf2(Surf^.w);
       
   299    th:= toPowerOf2(Surf^.h);
       
   300 
       
   301    GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel);
       
   302 
       
   303    gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE,
       
   304         Surf^.pixels, tw, th, GL_UNSIGNED_BYTE,
       
   305         tmpp);
       
   306 
       
   307    glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp);
       
   308 
       
   309    FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
       
   310    end else
       
   311    glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels);
   282 
   312 
   283 if SDL_MustLock(surf) then
   313 if SDL_MustLock(surf) then
   284    SDL_UnlockSurface(surf);
   314    SDL_UnlockSurface(surf);
   285 
   315 
   286 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   316 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   317 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   288 end;
   318 end;
   289 
   319 
   290 procedure FreeTexture(tex: PTexture);
   320 procedure FreeTexture(tex: PTexture);
   291 begin
   321 begin