hedgewars/uStore.pas
branchsdl2transition
changeset 11360 7a7611adf715
parent 9798 f2b18754742f
child 11362 ed5a6478e710
equal deleted inserted replaced
9798:f2b18754742f 11360:7a7611adf715
    51 procedure MakeCrossHairs;
    51 procedure MakeCrossHairs;
    52 {$IFDEF USE_VIDEO_RECORDING}
    52 {$IFDEF USE_VIDEO_RECORDING}
    53 procedure InitOffscreenOpenGL;
    53 procedure InitOffscreenOpenGL;
    54 {$ENDIF}
    54 {$ENDIF}
    55 
    55 
    56 {$IFDEF SDL2}
       
    57 procedure WarpMouse(x, y: Word); inline;
    56 procedure WarpMouse(x, y: Word); inline;
    58 {$ENDIF}
       
    59 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    57 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
    60 procedure SetSkyColor(r, g, b: real);
    58 procedure SetSkyColor(r, g, b: real);
    61 
    59 
    62 implementation
    60 implementation
    63 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    61 uses uMisc, uConsole, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands
    64     , uPhysFSLayer
    62     , uPhysFSLayer
    65     , uDebug
    63     , uDebug
    66     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF}
    64     {$IFDEF USE_CONTEXT_RESTORE}, uWorld{$ENDIF};
    67     {$IF NOT DEFINED(SDL2) AND DEFINED(USE_VIDEO_RECORDING)}, glut {$ENDIF};
       
    68 
    65 
    69 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    66 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple);
    70 
    67 
    71 var MaxTextureSize: LongInt;
    68 var MaxTextureSize: LongInt;
    72 {$IFDEF SDL2}
       
    73     SDLwindow: PSDL_Window;
    69     SDLwindow: PSDL_Window;
    74     SDLGLcontext: PSDL_GLContext;
    70     SDLGLcontext: PSDL_GLContext;
    75 {$ELSE}
       
    76     SDLPrimSurface: PSDL_Surface;
       
    77 {$ENDIF}
       
    78     squaresize : LongInt;
    71     squaresize : LongInt;
    79     numsquares : LongInt;
    72     numsquares : LongInt;
    80     ProgrTex: PTexture;
    73     ProgrTex: PTexture;
    81 
    74 
    82 const
    75 const
   687     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
   680     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
   688     SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
   681     SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
   689 {$ELSE}
   682 {$ELSE}
   690     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   683     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   691 {$ENDIF}
   684 {$ENDIF}
   692 {$IFNDEF SDL2} // vsync is default in SDL2
       
   693     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0));
       
   694 {$ENDIF}
       
   695     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   685     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   696     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
   686     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
   697     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   687     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   698     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);         // no depth buffer
   688     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);         // no depth buffer
   699     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);         // no alpha channel
   689     SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);         // no alpha channel
   706     AuxBufNum: LongInt = 0;
   696     AuxBufNum: LongInt = 0;
   707     tmpstr: AnsiString;
   697     tmpstr: AnsiString;
   708     tmpint: LongInt;
   698     tmpint: LongInt;
   709     tmpn: LongInt;
   699     tmpn: LongInt;
   710 begin
   700 begin
   711 {$IFDEF SDL2}
       
   712     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_GetCurrentVideoDriver()) + ')');
   701     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_GetCurrentVideoDriver()) + ')');
   713 {$ELSE}
       
   714     buf[0]:= char(0); // avoid compiler hint
       
   715     AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')');
       
   716 {$ENDIF}
       
   717 
   702 
   718     AuxBufNum:= AuxBufNum;
   703     AuxBufNum:= AuxBufNum;
   719 
   704 
   720 {$IFDEF SDL2}
       
   721     // TODO: this function creates an opengles1.1 context
   705     // TODO: this function creates an opengles1.1 context
   722     // un-comment below and add proper logic to support opengles2.0
   706     // un-comment below and add proper logic to support opengles2.0
   723     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
   707     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
   724     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
   708     //SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
   725     if SDLGLcontext = nil then
   709     if SDLGLcontext = nil then
   726         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
   710         SDLGLcontext:= SDL_GL_CreateContext(SDLwindow);
   727     SDLTry(SDLGLcontext <> nil, 'SDLGLcontext', true);
   711     SDLTry(SDLGLcontext <> nil, 'SDLGLcontext', true);
   728     SDLTry(SDL_GL_SetSwapInterval(1) = 0, 'SDL_GL_SetSwapInterval', true);
   712     SDLTry(SDL_GL_SetSwapInterval(1) = 0, 'SDL_GL_SetSwapInterval', true);
   729 {$ENDIF}
       
   730 
   713 
   731     // get the max (h and v) size for textures that the gpu can support
   714     // get the max (h and v) size for textures that the gpu can support
   732     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
   715     glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize);
   733     if MaxTextureSize <= 0 then
   716     if MaxTextureSize <= 0 then
   734         begin
   717         begin
  1082 FreeTexture(WeaponTooltipTex);
  1065 FreeTexture(WeaponTooltipTex);
  1083 WeaponTooltipTex:= nil
  1066 WeaponTooltipTex:= nil
  1084 end;
  1067 end;
  1085 
  1068 
  1086 {$IFDEF USE_VIDEO_RECORDING}
  1069 {$IFDEF USE_VIDEO_RECORDING}
  1087 {$IFDEF SDL2}
       
  1088 procedure InitOffscreenOpenGL;
  1070 procedure InitOffscreenOpenGL;
  1089 begin
  1071 begin
  1090     // create hidden window
  1072     // create hidden window
  1091     SDLwindow:= SDL_CreateWindow('hedgewars video rendering (SDL2 hidden window)',
  1073     SDLwindow:= SDL_CreateWindow('hedgewars video rendering (SDL2 hidden window)',
  1092                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
  1074                                  SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
  1093                                  cScreenWidth, cScreenHeight,
  1075                                  cScreenWidth, cScreenHeight,
  1094                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
  1076                                  SDL_WINDOW_HIDDEN or SDL_WINDOW_OPENGL);
  1095     SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
  1077     SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
  1096     SetupOpenGL();
  1078     SetupOpenGL();
  1097 end;
  1079 end;
  1098 {$ELSE}
       
  1099 procedure InitOffscreenOpenGL;
       
  1100 var ArgCount: LongInt;
       
  1101     PrgName: pchar;
       
  1102 begin
       
  1103     ArgCount:= 1;
       
  1104     PrgName:= 'hwengine';
       
  1105     glutInit(@ArgCount, @PrgName);
       
  1106     glutInitWindowSize(cScreenWidth, cScreenHeight);
       
  1107     // we do not need a window, but without this call OpenGL will not initialize
       
  1108     glutCreateWindow('hedgewars video rendering (glut hidden window)');
       
  1109     glutHideWindow();
       
  1110     // we do not need to set this callback, but it is required for GLUT3 compat
       
  1111     glutDisplayFunc(@SwapBuffers);
       
  1112     SetupOpenGL();
       
  1113 end;
       
  1114 {$ENDIF} // SDL2
       
  1115 {$ENDIF} // USE_VIDEO_RECORDING
  1080 {$ENDIF} // USE_VIDEO_RECORDING
  1116 
  1081 
  1117 procedure chFullScr(var s: shortstring);
  1082 procedure chFullScr(var s: shortstring);
  1118 var flags: Longword = 0;
  1083 var flags: Longword = 0;
  1119     reinit: boolean = false;
  1084     reinit: boolean = false;
  1120     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1085     {$IFNDEF DARWIN}ico: PSDL_Surface;{$ENDIF}
  1121     {$IFDEF SDL2}x, y: LongInt;{$ENDIF}
  1086     x, y: LongInt;
  1122 begin
  1087 begin
  1123     if cOnlyStats then
  1088     if cOnlyStats then
  1124         begin
  1089         begin
  1125         MaxTextureSize:= 1024;
  1090         MaxTextureSize:= 1024;
  1126         exit
  1091         exit
  1139         cScreenWidth:= cWindowedWidth;
  1104         cScreenWidth:= cWindowedWidth;
  1140         cScreenHeight:= cWindowedHeight;
  1105         cScreenHeight:= cWindowedHeight;
  1141         end;
  1106         end;
  1142 
  1107 
  1143     AddFileLog('Preparing to change video parameters...');
  1108     AddFileLog('Preparing to change video parameters...');
  1144 {$IFDEF SDL2}
       
  1145     if SDLwindow = nil then
  1109     if SDLwindow = nil then
  1146 {$ELSE}
       
  1147     if SDLPrimSurface = nil then
       
  1148 {$ENDIF}
       
  1149         begin
  1110         begin
  1150         // set window title
  1111         // set window title
  1151     {$IFNDEF SDL2}
       
  1152         SDL_WM_SetCaption(_P'Hedgewars', nil);
       
  1153     {$ENDIF}
       
  1154         WriteToConsole('Init SDL_image... ');
  1112         WriteToConsole('Init SDL_image... ');
  1155         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, 'IMG_Init', true);
  1113         SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, 'IMG_Init', true);
  1156         WriteLnToConsole(msgOK);
  1114         WriteLnToConsole(msgOK);
  1157         // load engine icon
       
  1158     {$IFNDEF DARWIN}
       
  1159     {$IFNDEF SDL2}
       
  1160         ico:= LoadDataImage(ptGraphics, 'hwengine', ifIgnoreCaps);
       
  1161         if ico <> nil then
       
  1162             begin
       
  1163             SDL_WM_SetIcon(ico, 0);
       
  1164             SDL_FreeSurface(ico)
       
  1165             end;
       
  1166     {$ENDIF}
       
  1167     {$ENDIF}
       
  1168         end
  1115         end
  1169     else
  1116     else
  1170         begin
  1117         begin
  1171         AmmoMenuInvalidated:= true;
  1118         AmmoMenuInvalidated:= true;
  1172 {$IFDEF IPHONEOS}
  1119 {$IFDEF IPHONEOS}
  1187         ResetLand;
  1134         ResetLand;
  1188         ResetWorldTex;
  1135         ResetWorldTex;
  1189         //uTextures.freeModule; //DEBUG ONLY
  1136         //uTextures.freeModule; //DEBUG ONLY
  1190     {$ENDIF}
  1137     {$ENDIF}
  1191         AddFileLog('Freeing old primary surface...');
  1138         AddFileLog('Freeing old primary surface...');
  1192     {$IFNDEF SDL2}
       
  1193         SDL_FreeSurface(SDLPrimSurface);
       
  1194         SDLPrimSurface:= nil;
       
  1195     {$ENDIF}
       
  1196 {$ENDIF}
  1139 {$ENDIF}
  1197         end;
  1140         end;
  1198 
  1141 
  1199     // these attributes must be set up before creating the sdl window
  1142     // these attributes must be set up before creating the sdl window
  1200 {$IFNDEF WIN32}
  1143 {$IFNDEF WIN32}
  1204    IMO we should rely on the gl_config defaults from SDL, and use
  1147    IMO we should rely on the gl_config defaults from SDL, and use
  1205    SDL_GL_GetAttribute to possibly post warnings if any bad values are set.
  1148    SDL_GL_GetAttribute to possibly post warnings if any bad values are set.
  1206  *)
  1149  *)
  1207     SetupOpenGLAttributes();
  1150     SetupOpenGLAttributes();
  1208 {$ENDIF}
  1151 {$ENDIF}
  1209 {$IFDEF SDL2}
       
  1210     // these values in x and y make the window appear in the center
  1152     // these values in x and y make the window appear in the center
  1211     x:= SDL_WINDOWPOS_CENTERED_MASK;
  1153     x:= SDL_WINDOWPOS_CENTERED_MASK;
  1212     y:= SDL_WINDOWPOS_CENTERED_MASK;
  1154     y:= SDL_WINDOWPOS_CENTERED_MASK;
  1213     // SDL_WINDOW_RESIZABLE makes the window resizable and
  1155     // SDL_WINDOW_RESIZABLE makes the window resizable and
  1214     //  respond to rotation events on mobile devices
  1156     //  respond to rotation events on mobile devices
  1225         flags:= flags or SDL_WINDOW_FULLSCREEN;
  1167         flags:= flags or SDL_WINDOW_FULLSCREEN;
  1226 
  1168 
  1227     if SDLwindow = nil then
  1169     if SDLwindow = nil then
  1228         SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
  1170         SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags);
  1229     SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
  1171     SDLTry(SDLwindow <> nil, 'SDL_CreateWindow', true);
  1230 {$ELSE}
       
  1231     flags:= SDL_OPENGL or SDL_RESIZABLE;
       
  1232     if cFullScreen then
       
  1233         flags:= flags or SDL_FULLSCREEN;
       
  1234     if not cOnlyStats then
       
  1235         begin
       
  1236     {$IFDEF WIN32}
       
  1237         s:= SDL_getenv('SDL_VIDEO_CENTERED');
       
  1238         SDL_putenv('SDL_VIDEO_CENTERED=1');
       
  1239     {$ENDIF}
       
  1240         SDLPrimSurface:= SDL_SetVideoMode(cScreenWidth, cScreenHeight, 0, flags);
       
  1241         SDLTry(SDLPrimSurface <> nil, true);
       
  1242     {$IFDEF WIN32}
       
  1243         SDL_putenv(str2pchar('SDL_VIDEO_CENTERED=' + s));
       
  1244     {$ENDIF}
       
  1245         end;
       
  1246 {$ENDIF}
       
  1247 
  1172 
  1248     SetupOpenGL();
  1173     SetupOpenGL();
  1249     if reinit then
  1174     if reinit then
  1250         begin
  1175         begin
  1251         // clean the window from any previous content
  1176         // clean the window from any previous content
  1264         // redraw all land
  1189         // redraw all land
  1265         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
  1190         UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT, false);
  1266         end;
  1191         end;
  1267 end;
  1192 end;
  1268 
  1193 
  1269 {$IFDEF SDL2}
       
  1270 // for sdl1.2 we directly call SDL_WarpMouse()
  1194 // for sdl1.2 we directly call SDL_WarpMouse()
  1271 // for sdl2 we provide a SDL_WarpMouse() which just calls this function
  1195 // for sdl2 we provide a SDL_WarpMouse() which just calls this function
  1272 // this has the advantage of reducing 'uses' and 'ifdef' statements
  1196 // this has the advantage of reducing 'uses' and 'ifdef' statements
  1273 // (SDLwindow is a private member of this module)
  1197 // (SDLwindow is a private member of this module)
  1274 procedure WarpMouse(x, y: Word); inline;
  1198 procedure WarpMouse(x, y: Word); inline;
  1275 begin
  1199 begin
  1276     SDL_WarpMouseInWindow(SDLwindow, x, y);
  1200     SDL_WarpMouseInWindow(SDLwindow, x, y);
  1277 end;
  1201 end;
  1278 {$ENDIF}
       
  1279 
  1202 
  1280 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
  1203 procedure SwapBuffers; {$IFDEF USE_VIDEO_RECORDING}cdecl{$ELSE}inline{$ENDIF};
  1281 begin
  1204 begin
  1282     if GameType = gmtRecord then
  1205     if GameType = gmtRecord then
  1283         exit;
  1206         exit;
  1284 {$IFDEF SDL2}
       
  1285     SDL_GL_SwapWindow(SDLwindow);
  1207     SDL_GL_SwapWindow(SDLwindow);
  1286 {$ELSE}
       
  1287     SDL_GL_SwapBuffers();
       
  1288 {$ENDIF}
       
  1289 end;
  1208 end;
  1290 
  1209 
  1291 procedure SetSkyColor(r, g, b: real);
  1210 procedure SetSkyColor(r, g, b: real);
  1292 begin
  1211 begin
  1293     glClearColor(r, g, b, 0.99)
  1212     glClearColor(r, g, b, 0.99)
  1310         Ammoz[ai].NameTex := nil;
  1229         Ammoz[ai].NameTex := nil;
  1311     end;
  1230     end;
  1312     // init all count texture pointers
  1231     // init all count texture pointers
  1313     for i:= Low(CountTexz) to High(CountTexz) do
  1232     for i:= Low(CountTexz) to High(CountTexz) do
  1314         CountTexz[i] := nil;
  1233         CountTexz[i] := nil;
  1315 {$IFDEF SDL2}
       
  1316     SDLwindow:= nil;
  1234     SDLwindow:= nil;
  1317     SDLGLcontext:= nil;
  1235     SDLGLcontext:= nil;
  1318 {$ELSE}
       
  1319     SDLPrimSurface:= nil;
       
  1320 {$ENDIF}
       
  1321 end;
  1236 end;
  1322 
  1237 
  1323 procedure freeModule;
  1238 procedure freeModule;
  1324 begin
  1239 begin
  1325     StoreRelease(false);
  1240     StoreRelease(false);
  1326     TTF_Quit();
  1241     TTF_Quit();
  1327 {$IFDEF SDL2}
       
  1328     SDL_GL_DeleteContext(SDLGLcontext);
  1242     SDL_GL_DeleteContext(SDLGLcontext);
  1329     SDL_DestroyWindow(SDLwindow);
  1243     SDL_DestroyWindow(SDLwindow);
  1330 {$ENDIF}
       
  1331     SDL_Quit();
  1244     SDL_Quit();
  1332 end;
  1245 end;
  1333 end.
  1246 end.