42 procedure MakeCrossHairs; |
42 procedure MakeCrossHairs; |
43 |
43 |
44 implementation |
44 implementation |
45 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands, uDebug; |
45 uses uMisc, uConsole, uMobile, uVariables, uUtils, uTextures, uRender, uRenderUtils, uCommands, uDebug; |
46 |
46 |
47 type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple); |
47 //type TGPUVendor = (gvUnknown, gvNVIDIA, gvATI, gvIntel, gvApple); |
48 |
48 |
49 var MaxTextureSize: LongInt; |
49 var MaxTextureSize: LongInt; |
50 cGPUVendor: TGPUVendor; |
50 // cGPUVendor: TGPUVendor; |
51 |
51 |
52 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect; |
52 function WriteInRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect; |
53 var w, h: LongInt; |
53 var w, h: LongInt; |
54 tmpsurf: PSDL_Surface; |
54 tmpsurf: PSDL_Surface; |
55 clr: TSDL_Color; |
55 clr: TSDL_Color; |
257 fi: THWFont; |
257 fi: THWFont; |
258 ai: TAmmoType; |
258 ai: TAmmoType; |
259 tmpsurf: PSDL_Surface; |
259 tmpsurf: PSDL_Surface; |
260 i: LongInt; |
260 i: LongInt; |
261 begin |
261 begin |
262 |
262 AddFileLog('StoreLoad()'); |
263 for fi:= Low(THWFont) to High(THWFont) do |
263 |
264 with Fontz[fi] do |
264 if not reload then |
265 begin |
265 for fi:= Low(THWFont) to High(THWFont) do |
266 s:= UserPathz[ptFonts] + '/' + Name; |
266 with Fontz[fi] do |
267 if not FileExists(s) then s:= Pathz[ptFonts] + '/' + Name; |
267 begin |
268 WriteToConsole(msgLoading + s + ' (' + inttostr(Height) + 'pt)... '); |
268 s:= UserPathz[ptFonts] + '/' + Name; |
269 Handle:= TTF_OpenFont(Str2PChar(s), Height); |
269 if not FileExists(s) then s:= Pathz[ptFonts] + '/' + Name; |
270 SDLTry(Handle <> nil, true); |
270 WriteToConsole(msgLoading + s + ' (' + inttostr(Height) + 'pt)... '); |
271 TTF_SetFontStyle(Handle, style); |
271 Handle:= TTF_OpenFont(Str2PChar(s), Height); |
272 WriteLnToConsole(msgOK) |
272 SDLTry(Handle <> nil, true); |
273 end; |
273 TTF_SetFontStyle(Handle, style); |
|
274 WriteLnToConsole(msgOK) |
|
275 end; |
274 |
276 |
275 WriteNames(fnt16); |
277 WriteNames(fnt16); |
276 MakeCrossHairs; |
278 MakeCrossHairs; |
277 LoadGraves; |
279 LoadGraves; |
278 |
280 |
561 else |
563 else |
562 AddFileLog('OpenGL - "' + extension + '" failed to load'); |
564 AddFileLog('OpenGL - "' + extension + '" failed to load'); |
563 {$ENDIF} |
565 {$ENDIF} |
564 end; |
566 end; |
565 |
567 |
566 procedure SetupOpenGL; |
568 procedure SetupOpenGLAttributes; |
567 var vendor: shortstring = ''; |
569 begin |
568 {$IFDEF DARWIN} |
|
569 const one : LongInt = 1; |
|
570 {$ENDIF} |
|
571 begin |
|
572 {$IFDEF SDL13} |
|
573 // this function creates an opengles1.1 context by default on mobile devices |
|
574 // use SDL_GL_SetAttribute to change this behaviour |
|
575 SDLGLcontext:=SDL_GL_CreateContext(SDLwindow); |
|
576 SDLTry(SDLGLcontext <> nil, true); |
|
577 SDL_GL_SetSwapInterval(1); |
|
578 {$ENDIF} |
|
579 |
|
580 {$IFDEF IPHONEOS} |
570 {$IFDEF IPHONEOS} |
581 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0); |
571 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0); |
582 SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1); |
572 SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1); |
583 vendor:= vendor; // avoid hint |
|
584 one:= one; // avoid hint |
|
585 {$ELSE} |
573 {$ELSE} |
586 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
574 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
587 vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR)))); |
575 {$IFNDEF SDL13} // vsync is default in 1.3 |
588 {$IFNDEF SDL13} |
|
589 // this attribute is default in 1.3 and must be enabled in MacOSX |
|
590 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0)); |
576 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, LongInt((cReducedQuality and rqDesyncVBlank) = 0)); |
591 |
|
592 {$IFDEF DARWIN} |
|
593 // fixes vsync in Snow Leopard |
|
594 CGLSetParameter(CGLGetCurrentContext(), 222, @one); |
|
595 {$ENDIF} |
|
596 {$ENDIF} |
577 {$ENDIF} |
597 {$ENDIF} |
578 {$ENDIF} |
598 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); // no depth buffer |
579 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); // no depth buffer |
599 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
580 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
600 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); |
581 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); |
601 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
582 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); |
602 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required |
583 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); // no alpha channel required |
603 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only |
584 SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 16); // buffer has to be 16 bit only |
604 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering |
585 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); // try to prefer hardware rendering |
605 |
586 end; |
|
587 |
|
588 procedure SetupOpenGL; |
|
589 //var vendor: shortstring = ''; |
|
590 begin |
|
591 {$IFDEF SDL13} |
|
592 // this function creates an opengles1.1 context by default on mobile devices |
|
593 // use SDL_GL_SetAttribute to change this behaviour |
|
594 SDLGLcontext:=SDL_GL_CreateContext(SDLwindow); |
|
595 SDLTry(SDLGLcontext <> nil, true); |
|
596 SDL_GL_SetSwapInterval(1); |
|
597 {$ENDIF} |
|
598 |
|
599 // get the max (horizontal and vertical) size for textures that the gpu can support |
606 glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize); |
600 glGetIntegerv(GL_MAX_TEXTURE_SIZE, @MaxTextureSize); |
607 |
|
608 AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER)))); |
|
609 AddFileLog(' |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR)))); |
|
610 AddFileLog(' |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION)))); |
|
611 AddFileLog(' \----- GL_MAX_TEXTURE_SIZE: ' + inttostr(MaxTextureSize)); |
|
612 |
|
613 if MaxTextureSize <= 0 then |
601 if MaxTextureSize <= 0 then |
614 begin |
602 begin |
615 MaxTextureSize:= 1024; |
603 MaxTextureSize:= 1024; |
616 AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024'); |
604 AddFileLog('OpenGL Warning - driver didn''t provide any valid max texture size; assuming 1024'); |
617 end |
605 end |
619 begin |
607 begin |
620 cReducedQuality := cReducedQuality or rqNoBackground; |
608 cReducedQuality := cReducedQuality or rqNoBackground; |
621 AddFileLog('Texture size too small for backgrounds, disabling.'); |
609 AddFileLog('Texture size too small for backgrounds, disabling.'); |
622 end; |
610 end; |
623 |
611 |
|
612 (* // find out which gpu we are using (for extension compatibility maybe?) |
624 {$IFDEF IPHONEOS} |
613 {$IFDEF IPHONEOS} |
|
614 vendor:= vendor; // avoid hint |
625 cGPUVendor:= gvApple; |
615 cGPUVendor:= gvApple; |
626 {$ELSE} |
616 {$ELSE} |
|
617 vendor:= LowerCase(shortstring(pchar(glGetString(GL_VENDOR)))); |
627 if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then |
618 if StrPos(Str2PChar(vendor), Str2PChar('nvidia')) <> nil then |
628 cGPUVendor:= gvNVIDIA |
619 cGPUVendor:= gvNVIDIA |
629 else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then |
620 else if StrPos(Str2PChar(vendor), Str2PChar('intel')) <> nil then |
630 cGPUVendor:= gvATI |
621 cGPUVendor:= gvATI |
631 else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then |
622 else if StrPos(Str2PChar(vendor), Str2PChar('ati')) <> nil then |
632 cGPUVendor:= gvIntel; |
623 cGPUVendor:= gvIntel |
|
624 else |
|
625 AddFileLog('OpenGL Warning - unknown hardware vendor; please report'); |
633 {$ENDIF} |
626 {$ENDIF} |
634 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two'); |
627 //SupportNPOTT:= glLoadExtension('GL_ARB_texture_non_power_of_two'); |
|
628 *) |
|
629 |
|
630 // everyone love debugging |
|
631 AddFileLog('OpenGL-- Renderer: ' + shortstring(pchar(glGetString(GL_RENDERER)))); |
|
632 AddFileLog(' |----- Vendor: ' + shortstring(pchar(glGetString(GL_VENDOR)))); |
|
633 AddFileLog(' |----- Version: ' + shortstring(pchar(glGetString(GL_VERSION)))); |
|
634 AddFileLog(' \----- Texture Size: ' + inttostr(MaxTextureSize)); |
|
635 |
635 {$IFNDEF S3D_DISABLED} |
636 {$IFNDEF S3D_DISABLED} |
636 if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then |
637 if (cStereoMode = smHorizontal) or (cStereoMode = smVertical) or (cStereoMode = smAFR) then |
637 begin |
638 begin |
638 // prepare left and right frame buffers and associated textures |
639 // prepare left and right frame buffers and associated textures |
639 if glLoadExtension('GL_EXT_framebuffer_object') then |
640 if glLoadExtension('GL_EXT_framebuffer_object') then |
672 else |
673 else |
673 cStereoMode:= smNone; |
674 cStereoMode:= smNone; |
674 end; |
675 end; |
675 {$ENDIF} |
676 {$ENDIF} |
676 |
677 |
677 if cGPUVendor = gvUnknown then |
|
678 AddFileLog('OpenGL Warning - unknown hardware vendor; please report'); |
|
679 |
|
680 // set view port to whole window |
678 // set view port to whole window |
681 if (rotationQt = 0) or (rotationQt = 180) then |
679 if (rotationQt = 0) or (rotationQt = 180) then |
682 glViewport(0, 0, cScreenWidth, cScreenHeight) |
680 glViewport(0, 0, cScreenWidth, cScreenHeight) |
683 else |
681 else |
684 glViewport(0, 0, cScreenHeight, cScreenWidth); |
682 glViewport(0, 0, cScreenHeight, cScreenWidth); |
758 SDL_GL_SwapWindow(SDLwindow); |
756 SDL_GL_SwapWindow(SDLwindow); |
759 {$ELSE} |
757 {$ELSE} |
760 SDL_GL_SwapBuffers(); |
758 SDL_GL_SwapBuffers(); |
761 {$ENDIF} |
759 {$ENDIF} |
762 inc(Step); |
760 inc(Step); |
763 |
|
764 end; |
761 end; |
765 |
762 |
766 procedure FinishProgress; |
763 procedure FinishProgress; |
767 begin |
764 begin |
768 WriteLnToConsole('Freeing progress surface... '); |
765 WriteLnToConsole('Freeing progress surface... '); |
769 FreeTexture(ProgrTex); |
766 FreeTexture(ProgrTex); |
770 uMobile.GameLoaded(); |
767 uMobile.GameLoaded(); |
|
768 Step:= 0 |
771 end; |
769 end; |
772 |
770 |
773 function RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture; |
771 function RenderHelpWindow(caption, subcaption, description, extra: ansistring; extracolor: LongInt; iconsurf: PSDL_Surface; iconrect: PSDL_Rect): PTexture; |
774 var tmpsurf: PSDL_SURFACE; |
772 var tmpsurf: PSDL_SURFACE; |
775 w, h, i, j: LongInt; |
773 w, h, i, j: LongInt; |
950 ico: PSDL_Surface; |
948 ico: PSDL_Surface; |
951 buf: array[byte] of char; |
949 buf: array[byte] of char; |
952 reinit: boolean; |
950 reinit: boolean; |
953 {$IFDEF SDL13}x, y: LongInt;{$ENDIF} |
951 {$IFDEF SDL13}x, y: LongInt;{$ENDIF} |
954 begin |
952 begin |
955 s:= s; // avoid compiler hint |
|
956 if Length(s) = 0 then cFullScreen:= not cFullScreen |
953 if Length(s) = 0 then cFullScreen:= not cFullScreen |
957 else cFullScreen:= s = '1'; |
954 else cFullScreen:= s = '1'; |
958 |
955 |
959 buf[0]:= char(0); // avoid compiler hint |
956 buf[0]:= char(0); // avoid compiler hint |
960 AddFileLog('Prepare to change video parameters...'); |
957 AddFileLog('Preparing to change video parameters...'); |
961 |
958 |
962 flags:= SDL_OPENGL;// or SDL_RESIZABLE; |
959 reinit:= false; |
963 |
960 if SDLPrimSurface = nil then |
964 if cFullScreen then |
961 begin |
965 flags:= flags or SDL_FULLSCREEN; |
962 // set window title |
966 |
963 SDL_WM_SetCaption('Hedgewars', nil); |
967 {$IFDEF SDL_IMAGE_NEWER} |
964 {$IFDEF SDL_IMAGE_NEWER} |
968 WriteToConsole('Init SDL_image... '); |
965 WriteToConsole('Init SDL_image... '); |
969 SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true); |
966 SDLTry(IMG_Init(IMG_INIT_PNG) <> 0, true); |
970 WriteLnToConsole(msgOK); |
967 WriteLnToConsole(msgOK); |
971 {$ENDIF} |
968 {$ENDIF} |
972 // load engine icon |
969 // load engine icon |
973 {$IFDEF DARWIN} |
970 {$IFDEF DARWIN} |
974 ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps); |
971 ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps); |
975 if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps); |
972 if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine_mac', ifIgnoreCaps); |
976 {$ELSE} |
973 {$ELSE} |
977 ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine', ifIgnoreCaps); |
974 ico:= LoadImage(UserPathz[ptGraphics] + '/hwengine', ifIgnoreCaps); |
978 if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine', ifIgnoreCaps); |
975 if ico = nil then ico:= LoadImage(Pathz[ptGraphics] + '/hwengine', ifIgnoreCaps); |
979 {$ENDIF} |
976 {$ENDIF} |
980 if ico <> nil then |
977 if ico <> nil then |
981 begin |
978 begin |
982 SDL_WM_SetIcon(ico, 0); |
979 SDL_WM_SetIcon(ico, 0); |
983 SDL_FreeSurface(ico) |
980 SDL_FreeSurface(ico) |
984 end; |
981 end; |
985 |
982 end |
986 // set window title |
983 else |
987 SDL_WM_SetCaption('Hedgewars', nil); |
984 begin |
988 reinit:= false; |
985 {$IF DEFINED(DARWIN) OR DEFINED(WIN32)} |
989 if SDLPrimSurface <> nil then |
|
990 begin |
|
991 {$IFDEF DARWIN | WIN32} |
|
992 reinit:= true; |
986 reinit:= true; |
993 {$ENDIF} |
987 {$ENDIF} |
994 AddFileLog('Freeing old primary surface...'); |
988 AddFileLog('Freeing old primary surface...'); |
995 SDL_FreeSurface(SDLPrimSurface); |
989 SDL_FreeSurface(SDLPrimSurface); |
996 SDLPrimSurface:= nil; |
990 SDLPrimSurface:= nil; |
997 end; |
991 end; |
998 |
992 |
|
993 // these attributes must be set up before creating the sdl window |
|
994 SetupOpenGLAttributes(); |
999 {$IFDEF SDL13} |
995 {$IFDEF SDL13} |
1000 // these values in x and y make the window appear in the center |
996 // these values in x and y make the window appear in the center |
1001 x:= SDL_WINDOWPOS_CENTERED_MASK; |
997 x:= SDL_WINDOWPOS_CENTERED_MASK; |
1002 y:= SDL_WINDOWPOS_CENTERED_MASK; |
998 y:= SDL_WINDOWPOS_CENTERED_MASK; |
1003 flags:= SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN; |
999 flags:= SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN; |
1005 {$IFDEF MOBILE} |
1001 {$IFDEF MOBILE} |
1006 // make the sdl window appear on the second monitor when present |
1002 // make the sdl window appear on the second monitor when present |
1007 x:= x or (SDL_GetNumVideoDisplays() - 1); |
1003 x:= x or (SDL_GetNumVideoDisplays() - 1); |
1008 y:= y or (SDL_GetNumVideoDisplays() - 1); |
1004 y:= y or (SDL_GetNumVideoDisplays() - 1); |
1009 |
1005 |
1010 flags:= flags or SDL_WINDOW_BORDERLESS; // do not use SDL_WINDOW_RESIZABLE on ios (yet) |
1006 SDL_SetHint('SDL_IOS_ORIENTATIONS','LandscapeLeft LandscapeRight'); |
1011 {$ENDIF} |
1007 flags:= flags or SDL_WINDOW_BORDERLESS or SDL_WINDOW_RESIZABLE; |
1012 |
1008 {$ENDIF} |
1013 SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags); |
1009 |
|
1010 if cFullScreen then SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cOrigScreenWidth, cOrigScreenHeight, flags or SDL_WINDOW_FULLSCREEN) |
|
1011 else SDLwindow:= SDL_CreateWindow('Hedgewars', x, y, cScreenWidth, cScreenHeight, flags); |
1014 SDLTry(SDLwindow <> nil, true); |
1012 SDLTry(SDLwindow <> nil, true); |
1015 {$ELSE} |
1013 {$ELSE} |
|
1014 flags:= SDL_OPENGL or SDL_RESIZABLE; |
|
1015 if cFullScreen then |
|
1016 flags:= flags or SDL_FULLSCREEN; |
|
1017 |
1016 if not cOnlyStats then |
1018 if not cOnlyStats then |
1017 begin |
1019 begin |
1018 {$IFDEF WIN32} |
1020 {$IFDEF WIN32} |
1019 s:= SDL_getenv('SDL_VIDEO_CENTERED'); |
1021 s:= SDL_getenv('SDL_VIDEO_CENTERED'); |
1020 SDL_putenv('SDL_VIDEO_CENTERED=1'); |
1022 SDL_putenv('SDL_VIDEO_CENTERED=1'); |
1027 |
1029 |
1028 AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')'); |
1030 AddFileLog('Setting up OpenGL (using driver: ' + shortstring(SDL_VideoDriverName(buf, sizeof(buf))) + ')'); |
1029 SetupOpenGL(); |
1031 SetupOpenGL(); |
1030 if reinit then |
1032 if reinit then |
1031 begin |
1033 begin |
|
1034 if SuddenDeathDmg then |
|
1035 glClearColor(SDSkyColor.r * (SDTint/255) / 255, SDSkyColor.g * (SDTint/255) / 255, SDSkyColor.b * (SDTint/255) / 255, 0.99) |
|
1036 else if ((cReducedQuality and rqNoBackground) = 0) then |
|
1037 glClearColor(SkyColor.r / 255, SkyColor.g / 255, SkyColor.b / 255, 0.99) |
|
1038 else glClearColor(RQSkyColor.r / 255, RQSkyColor.g / 255, RQSkyColor.b / 255, 0.99); |
|
1039 |
1032 StoreRelease(true); |
1040 StoreRelease(true); |
1033 StoreLoad(true); |
1041 StoreLoad(true); |
|
1042 |
|
1043 ResetLand; |
|
1044 |
1034 UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT) |
1045 UpdateLandTexture(0, LAND_WIDTH, 0, LAND_HEIGHT) |
1035 end; |
1046 end; |
1036 end; |
1047 end; |
1037 |
1048 |
1038 procedure initModule; |
1049 procedure initModule; |