From e02e467681726fbe5e7c32a0970ea6c4b8eb94fc Mon Sep 17 00:00:00 2001 From: tobigun Date: Sat, 12 Jul 2008 16:58:23 +0000 Subject: - cleanup/comments for TDisplay.Create - fading works now, it was broken because RenderW/H was not defined when SwapBuffers() was called for the first time. This caused glOrtho() to set the OpenGL error state. When fading was initialized in UDisplay this error-state was checked to see if glGenTextures failed. Although it didn't the error state was set because of the older error in glOrtho (Note: errors aren't reset after a successful OpenGL call), so it was wrongly assumed that no texture could be allocated. This problem was fixed by defining RenderW/H before the first call of SwapBuffers and resetting OpenGL's error state before calling an OpenGL function whose error state is to be checked. git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@1188 b956fd51-792f-4845-bead-9b4dfca2ff2c --- Game/Code/Menu/UDisplay.pas | 221 ++++++++++++++++++-------------------------- 1 file changed, 90 insertions(+), 131 deletions(-) (limited to 'Game/Code/Menu/UDisplay.pas') diff --git a/Game/Code/Menu/UDisplay.pas b/Game/Code/Menu/UDisplay.pas index f27320fb..b7777353 100644 --- a/Game/Code/Menu/UDisplay.pas +++ b/Game/Code/Menu/UDisplay.pas @@ -22,12 +22,12 @@ type //fade-to-black-hack BlackScreen: Boolean; - doFade : Boolean; - canFade : Boolean; - myFade : integer; - lastTime : Cardinal; - pTexData : Pointer; - pTex : array[1..2] of glUInt; + FadeEnabled: Boolean; // true if fading is enabled + FadeFailed: Boolean; // true if fading is possible (enough memory, etc.) + FadeState: integer; // fading state, 0 means that the fade texture must be initialized + LastFadeTime: Cardinal; // last fade update time + + FadeTex: array[1..2] of GLuint; FPSCounter : Cardinal; LastFPS : Cardinal; @@ -40,10 +40,11 @@ type NextScreen : PMenu; CurrentScreen : PMenu; - //popup hack + //popup data NextScreenWithCheck: Pmenu; CheckOK : Boolean; + // FIXME: Fade is set to 0 in UMain and other files but not used here anymore. Fade : Real; constructor Create; @@ -73,206 +74,173 @@ uses constructor TDisplay.Create; var i: integer; - + errcode: GLenum; begin inherited Create; //popup hack CheckOK := False; - NextScreen := NIL; - NextScreenWithCheck := NIL; + NextScreen := nil; + NextScreenWithCheck := nil; BlackScreen := False; // fade mod - myfade:=0; - - if Ini.ScreenFade=1 then - doFade:=True - else - doFade:=False; + FadeState := 0; + FadeEnabled := (Ini.ScreenFade = 1); + FadeFailed:= false; - canFade:=True; - // generate texture for fading between screens - GetMem(pTexData, 512*512*4); + glGenTextures(2, @FadeTex); - if pTexData <> NIL then - for i:= 1 to 2 do + for i := 1 to 2 do begin - - glGenTextures(1, @pTex[i] ); - - if glGetError <> GL_NO_ERROR then - canFade := False; - - glBindTexture(GL_TEXTURE_2D, pTex[i]); - if glGetError <> GL_NO_ERROR then - canFade := False; - - glTexImage2D(GL_TEXTURE_2D, 0, 4, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, pTexData); - if glGetError <> GL_NO_ERROR then - canFade := False; - + glBindTexture(GL_TEXTURE_2D, FadeTex[i]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - if glGetError <> GL_NO_ERROR then - canFade := False; - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - if glGetError <> GL_NO_ERROR then - canFade := False; - end - else - begin - canFade:=False; end; - FreeMem(pTexData); - //Set LastError for OSD to No Error OSD_LastError := 'No Errors'; end; destructor TDisplay.Destroy; begin - if canFade then - glDeleteTextures(1,@pTex); - + glDeleteTextures(2, @FadeTex); inherited Destroy; end; function TDisplay.Draw: Boolean; var - S: integer; - Col: Real; - myFade2: Real; + S: integer; + FadeStateSquare: Real; currentTime: Cardinal; glError: glEnum; - glErrorStr: String; begin Result := True; - Col := 1; - {if (ParamStr(1) = '-black') or (ParamStr(1) = '-fsblack') then - Col := 0; } - - glClearColor(Col, Col, Col , 0); + glClearColor(1, 1, 1 , 0); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); - for S := 1 to Screens do begin + for S := 1 to Screens do + begin ScreenAct := S; -// if Screens = 1 then ScreenX := 0; -// if (Screens = 2) and (S = 1) then ScreenX := -1; -// if (Screens = 2) and (S = 2) then ScreenX := 1; + //if Screens = 1 then ScreenX := 0; + //if (Screens = 2) and (S = 1) then ScreenX := -1; + //if (Screens = 2) and (S = 2) then ScreenX := 1; ScreenX := 0; glViewPort((S-1) * ScreenW div Screens, 0, ScreenW div Screens, ScreenH); - //popup hack + // popup hack // check was successful... move on if CheckOK then begin - if assigned (NextScreenWithCheck)then + if assigned(NextScreenWithCheck) then begin - NextScreen:=NextScreenWithCheck; - NextScreenWithCheck := NIL; - CheckOk:=False; + NextScreen := NextScreenWithCheck; + NextScreenWithCheck := nil; + CheckOk := False; end else begin - BlackScreen:=True; // end of game - fade to black before exit + // on end of game fade to black before exit + BlackScreen := True; end; end; -// CurrentScreen.SetAnimationProgress(1); - if (not assigned (NextScreen)) and (not BlackScreen) then + if (not assigned(NextScreen)) and (not BlackScreen) then begin CurrentScreen.Draw; //popup mod - if (ScreenPopupError <> NIL) and ScreenPopupError.Visible then + if (ScreenPopupError <> nil) and ScreenPopupError.Visible then ScreenPopupError.Draw - else if (ScreenPopupCheck <> NIL) and ScreenPopupCheck.Visible then + else if (ScreenPopupCheck <> nil) and ScreenPopupCheck.Visible then ScreenPopupCheck.Draw; // fade mod - myfade:=0; - if (Ini.ScreenFade=1) and canFade then - doFade:=True - else if Ini.ScreenFade=0 then - doFade:=False; + FadeState := 0; + if ((Ini.ScreenFade = 1) and (not FadeFailed)) then + FadeEnabled := True + else if (Ini.ScreenFade = 0) then + FadeEnabled := False; end else begin - // check if we had an initialization error (canfade=false, dofade=true) - if doFade and not canFade then + // disable fading if initialization failed + if (FadeEnabled and FadeFailed) then begin - doFade:=False; //disable fading -// ScreenPopupError.ShowPopup('Error initializing\nfade texture\n\nfading\ndisabled'); //show error message + FadeEnabled := False; end; - if doFade and canFade then + + if (FadeEnabled and not FadeFailed) then begin - // fade mod //Create Fading texture if we're just starting - if myfade = 0 then + if FadeState = 0 then begin + // resize viewport to fit texture glViewPort(0, 0, 512, 512); + + // draw screen that will be faded CurrentScreen.Draw; - glBindTexture(GL_TEXTURE_2D, pTex[S]); + + // clear OpenGL errors, otherwise fading might be disabled due to some + // older errors in previous OpenGL calls. + glGetError(); + + // copy screen to texture + glBindTexture(GL_TEXTURE_2D, FadeTex[S]); glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 512, 512, 0); - glError:=glGetError; + glError := glGetError(); if (glError <> GL_NO_ERROR) then begin - canFade := False; - glErrorStr := gluErrorString(glError); -// ScreenPopupError.ShowPopup('Error copying\nfade texture\n('+glErrorStr+')\nfading\ndisabled'); //show error message + FadeFailed := true; + Log.LogWarn('Fading disabled: ' + gluErrorString(glError), 'TDisplay.Draw'); end; glViewPort((S-1) * ScreenW div Screens, 0, ScreenW div Screens, ScreenH); + // blackscreen-hack if not BlackScreen then NextScreen.onShow; - lastTime:=SDL_GetTicks(); - if (S=2) or (Screens = 1) then - myfade:=myfade+1; + // update fade state + LastFadeTime := SDL_GetTicks(); + if (S = 2) or (Screens = 1) then + FadeState := FadeState + 1; end; // end texture creation in first fading step //do some time-based fading - currentTime:=SDL_GetTicks(); - if (currentTime > lastTime+30) and (S=1) then + currentTime := SDL_GetTicks(); + if (currentTime > LastFadeTime+30) and (S = 1) then begin - myfade:=myfade+4; - lastTime:=currentTime; + FadeState := FadeState + 4; + LastFadeTime := currentTime; end; -// LastFade := Fade; // whatever -// Fade := Fade -0.999; // start fading out - - -// CurrentScreen.ShowFinish := false; // no purpose? - -// CurrentScreen.SetAnimationProgress(Fade-1); // nop? - // blackscreen-hack if not BlackScreen then NextScreen.Draw // draw next screen - else if ScreenAct=1 then + else if ScreenAct = 1 then begin glClearColor(0, 0, 0 , 0); glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); end; - // and draw old screen over it... slowly fading out - myfade2:=(myfade*myfade)/10000; - glBindTexture(GL_TEXTURE_2D, pTex[S]); - glColor4f(1, 1, 1, (1000-myfade*myfade)/1000); // strange calculation - alpha gets negative... but looks good this way + // and draw old screen over it... slowly fading out + + FadeStateSquare := (FadeState*FadeState)/10000; + + glBindTexture(GL_TEXTURE_2D, FadeTex[S]); + glColor4f(1, 1, 1, 1-FadeStateSquare); + glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glBegin(GL_QUADS); - glTexCoord2f(0+myfade2,0+myfade2);glVertex2f(0, 600); - glTexCoord2f(0+myfade2,1-myfade2);glVertex2f(0, 0); - glTexCoord2f(1-myfade2,1-myfade2);glVertex2f(800, 0); - glTexCoord2f(1-myfade2,0+myfade2);glVertex2f(800, 600); + glTexCoord2f(0+FadeStateSquare, 0+FadeStateSquare); glVertex2f(0, 600); + glTexCoord2f(0+FadeStateSquare, 1-FadeStateSquare); glVertex2f(0, 0); + glTexCoord2f(1-FadeStateSquare, 1-FadeStateSquare); glVertex2f(800, 0); + glTexCoord2f(1-FadeStateSquare, 0+FadeStateSquare); glVertex2f(800, 600); glEnd; glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); @@ -283,12 +251,13 @@ begin NextScreen.OnShow; end; - if ((myfade > 40) or (not doFade) or (not canFade)) And (S = 1) then - begin // fade out complete... - myFade:=0; + if ((FadeState > 40) or (not FadeEnabled) or FadeFailed) and (S = 1) then + begin + // fade out complete... + FadeState := 0; CurrentScreen.onHide; - CurrentScreen.ShowFinish:=False; - CurrentScreen:=NextScreen; + CurrentScreen.ShowFinish := False; + CurrentScreen := NextScreen; NextScreen := nil; if not blackscreen then begin @@ -297,30 +266,20 @@ begin end else begin - Result:=False; + Result := False; Break; end; - // end of fade mod end; end; // if //Draw OSD only on first Screen if Debug Mode is enabled - if ((Ini.Debug = 1) or (Params.Debug)) and (S=1) then - DrawDebugInformation; - + if ((Ini.Debug = 1) or (Params.Debug)) and (S = 1) then + DrawDebugInformation; end; // for // SDL_GL_SwapBuffers(); end; -{function TDisplay.Fade(FadeIn : Boolean; Steps : UInt8): UInt8; -begin - Self.FadeIn := FadeIn; - FadeStep := (SizeOf(FadeStep) * $FF) div Steps; - ActualStep := $FF; - Result := $FF div FadeStep; -end;} - procedure TDisplay.SaveScreenShot; var Num: integer; -- cgit v1.2.3