hedgewars/uMisc.pas
changeset 10633 2f062fac5791
parent 10150 fa5c83fd0ad9
child 10658 a3872ffdeab1
equal deleted inserted replaced
10632:5ae7ba0b2849 10633:2f062fac5791
    26 procedure initModule;
    26 procedure initModule;
    27 procedure freeModule;
    27 procedure freeModule;
    28 
    28 
    29 procedure movecursor(dx, dy: LongInt);
    29 procedure movecursor(dx, dy: LongInt);
    30 function  doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface;
    30 function  doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface;
    31 function  MakeScreenshot(filename: shortstring; k: LongInt): boolean;
    31 function MakeScreenshot(filename: shortstring; k: LongInt; dump: LongWord): boolean;
    32 function  GetTeamStatString(p: PTeam): shortstring;
    32 function  GetTeamStatString(p: PTeam): shortstring;
    33 {$IFDEF SDL2}
    33 {$IFDEF SDL2}
    34 function  SDL_RectMake(x, y, width, height: LongInt): TSDL_Rect; inline;
    34 function  SDL_RectMake(x, y, width, height: LongInt): TSDL_Rect; inline;
    35 {$ELSE}
    35 {$ELSE}
    36 function  SDL_RectMake(x, y: SmallInt; width, height: Word): TSDL_Rect; inline;
    36 function  SDL_RectMake(x, y: SmallInt; width, height: Word): TSDL_Rect; inline;
   219 end;
   219 end;
   220 {$ENDIF}
   220 {$ENDIF}
   221 
   221 
   222 // captures and saves the screen. returns true on success.
   222 // captures and saves the screen. returns true on success.
   223 // saved image will be k times smaller than original (useful for saving thumbnails).
   223 // saved image will be k times smaller than original (useful for saving thumbnails).
   224 function MakeScreenshot(filename: shortstring; k: LongInt): Boolean;
   224 function MakeScreenshot(filename: shortstring; k: LongInt; dump: LongWord): boolean;
   225 var p: Pointer;
   225 var p: Pointer;
   226     size: QWord;
   226     size: QWord;
   227     image: PScreenshot;
   227     image: PScreenshot;
   228     format: GLenum;
   228     format: GLenum;
   229     ext: string[4];
   229     ext: string[4];
       
   230     x,y: LongWord;
   230 begin
   231 begin
   231 {$IFDEF PNG_SCREENSHOTS}
   232 {$IFDEF PNG_SCREENSHOTS}
   232 format:= GL_RGBA;
   233 format:= GL_RGBA;
   233 ext:= '.png';
   234 ext:= '.png';
   234 {$ELSE}
   235 {$ELSE}
   235 format:= GL_BGRA;
   236 format:= GL_BGRA;
   236 ext:= '.bmp';
   237 ext:= '.bmp';
   237 {$ENDIF}
   238 {$ENDIF}
   238 
   239 
   239 size:= toPowerOf2(cScreenWidth) * toPowerOf2(cScreenHeight) * 4;
   240 if dump > 0 then
       
   241      size:= LAND_WIDTH*LAND_HEIGHT*4
       
   242 else size:= toPowerOf2(cScreenWidth) * toPowerOf2(cScreenHeight) * 4;
   240 p:= GetMem(size); // will be freed in SaveScreenshot()
   243 p:= GetMem(size); // will be freed in SaveScreenshot()
   241 
   244 
   242 // memory could not be allocated
   245 // memory could not be allocated
   243 if p = nil then
   246 if p = nil then
   244 begin
   247 begin
   246     MakeScreenshot:= false;
   249     MakeScreenshot:= false;
   247     exit;
   250     exit;
   248 end;
   251 end;
   249 
   252 
   250 // read pixels from the front buffer
   253 // read pixels from the front buffer
   251 glReadPixels(0, 0, cScreenWidth, cScreenHeight, format, GL_UNSIGNED_BYTE, p);
   254 if dump > 0 then
   252 
   255     begin
       
   256     for y:= 0 to LAND_HEIGHT-1 do
       
   257         for x:= 0 to LAND_WIDTH-1 do
       
   258             if dump = 2 then
       
   259                 PLongWordArray(p)^[y*LAND_WIDTH+x]:= LandPixels[LAND_HEIGHT-1-y, x]
       
   260             else
       
   261                 begin
       
   262                 if Land[LAND_HEIGHT-1-y, x] and lfIndestructible = lfIndestructible then
       
   263                     PLongWordArray(p)^[y*LAND_WIDTH+x]:= (AMask or RMask)
       
   264                 else if Land[LAND_HEIGHT-1-y, x] and lfIce = lfIce then
       
   265                     PLongWordArray(p)^[y*LAND_WIDTH+x]:= (AMask or BMask)
       
   266                 else if Land[LAND_HEIGHT-1-y, x] and lfBouncy = lfBouncy then
       
   267                     PLongWordArray(p)^[y*LAND_WIDTH+x]:= (AMask or GMask)
       
   268                 else if Land[LAND_HEIGHT-1-y, x] and lfObject = lfObject then
       
   269                     PLongWordArray(p)^[y*LAND_WIDTH+x]:= $FFFFFFFF
       
   270                 else if Land[LAND_HEIGHT-1-y, x] and lfBasic = lfBasic then
       
   271                     PLongWordArray(p)^[y*LAND_WIDTH+x]:= AMask
       
   272                 else
       
   273                     PLongWordArray(p)^[y*LAND_WIDTH+x]:= 0
       
   274                 end
       
   275     end
       
   276 else
       
   277     begin
       
   278     glReadPixels(0, 0, cScreenWidth, cScreenHeight, format, GL_UNSIGNED_BYTE, p);
   253 {$IFDEF USE_VIDEO_RECORDING}
   279 {$IFDEF USE_VIDEO_RECORDING}
   254 ReduceImage(p, cScreenWidth, cScreenHeight, k);
   280     ReduceImage(p, cScreenWidth, cScreenHeight, k)
   255 {$ENDIF}
   281 {$ENDIF}
       
   282     end;
   256 
   283 
   257 // allocate and fill structure that will be passed to new thread
   284 // allocate and fill structure that will be passed to new thread
   258 New(image); // will be disposed in SaveScreenshot()
   285 New(image); // will be disposed in SaveScreenshot()
   259 image^.filename:= shortstring(UserPathPrefix) + filename + ext;
   286 if dump = 2 then
   260 image^.width:= cScreenWidth div k;
   287      image^.filename:= shortstring(UserPathPrefix) + filename + '_landpixels' + ext
   261 image^.height:= cScreenHeight div k;
   288 else if dump = 1 then
       
   289      image^.filename:= shortstring(UserPathPrefix) + filename + '_land' + ext
       
   290 else image^.filename:= shortstring(UserPathPrefix) + filename + '_land' + ext;
       
   291 
       
   292 if dump <> 0 then
       
   293     begin
       
   294     image^.width:= LAND_WIDTH;
       
   295     image^.height:= LAND_HEIGHT
       
   296     end
       
   297 else
       
   298     begin
       
   299     image^.width:= cScreenWidth div k;
       
   300     image^.height:= cScreenHeight div k
       
   301     end;
   262 image^.size:= size;
   302 image^.size:= size;
   263 image^.buffer:= p;
   303 image^.buffer:= p;
   264 
   304 
   265 SDL_CreateThread(@SaveScreenshot{$IFDEF SDL2}, 'snapshot'{$ENDIF}, image);
   305 SDL_CreateThread(@SaveScreenshot{$IFDEF SDL2}, 'snapshot'{$ENDIF}, image);
   266 MakeScreenshot:= true; // possibly it is not true but we will not wait for thread to terminate
   306 MakeScreenshot:= true; // possibly it is not true but we will not wait for thread to terminate