hedgewars/uMisc.pas
changeset 1896 fdacad5d0acc
parent 1895 7ba647a88b2f
child 1906 644f93d8f148
equal deleted inserted replaced
1895:7ba647a88b2f 1896:fdacad5d0acc
   139 
   139 
   140 var CursorPoint: TPoint;
   140 var CursorPoint: TPoint;
   141     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   141     TargetPoint: TPoint = (X: NoPointX; Y: 0);
   142 
   142 
   143 implementation
   143 implementation
   144 uses uConsole, uStore, uIO, Math, uRandom, GLU;
   144 uses uConsole, uStore, uIO, Math, uRandom;
   145 var KBnum: Longword = 0;
   145 var KBnum: Longword = 0;
   146 {$IFDEF DEBUGFILE}
   146 {$IFDEF DEBUGFILE}
   147 var f: textfile;
   147 var f: textfile;
   148 {$ENDIF}
   148 {$ENDIF}
   149 
   149 
   281 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
   281 function NewTexture(width, height: Longword; buf: Pointer): PTexture;
   282 begin
   282 begin
   283 new(NewTexture);
   283 new(NewTexture);
   284 NewTexture^.w:= width;
   284 NewTexture^.w:= width;
   285 NewTexture^.h:= height;
   285 NewTexture^.h:= height;
       
   286 NewTexture^.rx:= 1.0;
       
   287 NewTexture^.ry:= 1.0;
   286 
   288 
   287 glGenTextures(1, @NewTexture^.id);
   289 glGenTextures(1, @NewTexture^.id);
   288 
   290 
   289 glBindTexture(GL_TEXTURE_2D, NewTexture^.id);
   291 glBindTexture(GL_TEXTURE_2D, NewTexture^.id);
   290 
   292 
   294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   296 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
   295 end;
   297 end;
   296 
   298 
   297 function Surface2Tex(surf: PSDL_Surface): PTexture;
   299 function Surface2Tex(surf: PSDL_Surface): PTexture;
   298 var mode: LongInt;
   300 var mode: LongInt;
   299     tw, th: Longword;
   301 	tw, th, x, y: Longword;
   300     tmpp: pointer;
   302 	tmpp: pointer;
       
   303 	fromP4, toP4: PLongWordArray;
       
   304 	fromP1, toP1: PByteArray;
   301 begin
   305 begin
   302 new(Surface2Tex);
   306 new(Surface2Tex);
   303 Surface2Tex^.w:= surf^.w;
   307 Surface2Tex^.w:= surf^.w;
   304 Surface2Tex^.h:= surf^.h;
   308 Surface2Tex^.h:= surf^.h;
   305 
   309 
   317 
   321 
   318 if SDL_MustLock(surf) then
   322 if SDL_MustLock(surf) then
   319    SDLTry(SDL_LockSurface(surf) >= 0, true);
   323    SDLTry(SDL_LockSurface(surf) >= 0, true);
   320 
   324 
   321 if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then
   325 if not (isPowerOf2(Surf^.w) and isPowerOf2(Surf^.h)) then
   322    begin
   326 	begin
   323    tw:= toPowerOf2(Surf^.w);
   327 	tw:= toPowerOf2(Surf^.w);
   324    th:= toPowerOf2(Surf^.h);
   328 	th:= toPowerOf2(Surf^.h);
   325 
   329 
   326    GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel);
   330 	Surface2Tex^.rx:= Surf^.w / tw;
   327 
   331 	Surface2Tex^.ry:= Surf^.h / th;
   328    gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE,
   332 	
   329         Surf^.pixels, tw, th, GL_UNSIGNED_BYTE,
   333 	GetMem(tmpp, tw * th * surf^.format^.BytesPerPixel);
   330         tmpp);
   334 
   331 
   335 	if surf^.format^.BytesPerPixel = 4 then
   332    glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp);
   336 		begin
   333 
   337 		fromP4:= Surf^.pixels;
   334    FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
   338 		toP4:= tmpp;
   335    end else
   339 
   336    glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels);
   340 		for y:= 0 to Pred(Surf^.h) do
       
   341 			begin
       
   342 			for x:= 0 to Pred(Surf^.w) do
       
   343 				toP4^[x]:= fromP4^[x];
       
   344 			for x:= Surf^.w to Pred(tw) do
       
   345 				toP4^[x]:= 0;
       
   346 			toP4:= @(toP4^[tw]);
       
   347 			fromP4:= @(fromP4^[Surf^.w]);
       
   348 			end;
       
   349 
       
   350 		for y:= Surf^.h to Pred(th) do
       
   351 			begin
       
   352 			for x:= 0 to Pred(tw) do
       
   353 				toP4^[x]:= 0;
       
   354 			toP4:= @(toP4^[tw]);
       
   355 			end;
       
   356 		end
       
   357 	else
       
   358 		begin
       
   359 		fromP1:= Surf^.pixels;
       
   360 		toP1:= tmpp;
       
   361 
       
   362 		for y:= 0 to Pred(Surf^.h) do
       
   363 			begin
       
   364 			for x:= 0 to Pred(Surf^.w) do
       
   365 				begin
       
   366 				toP1^[x * 3]:= fromP1^[x * 3];
       
   367 				toP1^[x * 3 + 1]:= fromP1^[x * 3 + 1];
       
   368 				toP1^[x * 3 + 2]:= fromP1^[x * 3 + 2];
       
   369 				end;
       
   370 			for x:= Surf^.w to Pred(tw) do
       
   371 				begin
       
   372 				toP1^[x * 3]:= 0;
       
   373 				toP1^[x * 3 + 1]:= 0;
       
   374 				toP1^[x * 3 + 2]:= 0;
       
   375 				end;
       
   376 			toP1:= @(toP1^[tw * 3]);
       
   377 			fromP1:= @(fromP1^[Surf^.pitch]);
       
   378 			end;
       
   379 
       
   380 		for y:= Surf^.h to Pred(th) do
       
   381 			begin
       
   382 			for x:= 0 to Pred(tw) do
       
   383 				begin
       
   384 				toP1^[x * 3]:= 0;
       
   385 				toP1^[x * 3 + 1]:= 0;
       
   386 				toP1^[x * 3 + 2]:= 0;
       
   387 				end;
       
   388 			toP1:= @(toP1^[tw * 3]);
       
   389 			end;
       
   390 		end;
       
   391 
       
   392 //   gluScaleImage(mode, Surf^.w, Surf^.h, GL_UNSIGNED_BYTE,
       
   393 //        Surf^.pixels, tw, th, GL_UNSIGNED_BYTE,
       
   394 //        tmpp);
       
   395 
       
   396 	glTexImage2D(GL_TEXTURE_2D, 0, mode, tw, th, 0, mode, GL_UNSIGNED_BYTE, tmpp);
       
   397 	
       
   398 	FreeMem(tmpp, tw * th * surf^.format^.BytesPerPixel)
       
   399 	end else
       
   400 	begin
       
   401 	Surface2Tex^.rx:= 1.0;
       
   402 	Surface2Tex^.ry:= 1.0;
       
   403 	glTexImage2D(GL_TEXTURE_2D, 0, mode, surf^.w, surf^.h, 0, mode, GL_UNSIGNED_BYTE, surf^.pixels);
       
   404 	end;
   337 
   405 
   338 if SDL_MustLock(surf) then
   406 if SDL_MustLock(surf) then
   339    SDL_UnlockSurface(surf);
   407    SDL_UnlockSurface(surf);
   340 
   408 
   341 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);