Skip to content

Commit c3f2bfb

Browse files
committed
Refactored window resize handling to prevent memory leaks when updating the GL FBO
Window Resize event is ignored if the resolution didn't actually change The HUD bump magnitude is a lot lower when damage taken is low
1 parent a0a5e3f commit c3f2bfb

5 files changed

+25
-27
lines changed

src/game/CAbstractPlayer.cpp

+7-10
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ void CAbstractPlayer::LoadDashboardParts() {
562562
dashboardSpinSpeed = ToFixed(100);
563563
dashboardSpinHeading = 0;
564564

565-
int layout = itsGame->itsApp->Get(kHUDPreset);
565+
layout = itsGame->itsApp->Get(kHUDPreset);
566566
//float alpha = itsGame->itsApp->Get(kHUDAlpha);
567567
//Fixed hudAlpha = FIX1 * alpha;
568568

@@ -718,21 +718,22 @@ void CAbstractPlayer::RenderDashboard() {
718718
// Lastly set relativeImpulse based on the impact location of the hit to bump the HUD
719719
Fixed hitAngle = FOneArcTan2(dSpeed[2], dSpeed[0]);
720720
Fixed angleDiff = hitAngle - viewYaw;
721+
float magnitude = ToFloat(VectorLength(3, dSpeed));
721722

722723
if (angleDiff > 0) {
723724
// Hit from the right side
724-
relativeImpulse[0] = FIX(1.0);
725+
relativeImpulse[0] = FIX(1.0*magnitude);
725726
} else if (angleDiff < 0) {
726727
// Hit from the left side
727-
relativeImpulse[0] = FIX(-1.0);
728+
relativeImpulse[0] = FIX(-1.0*magnitude);
728729
}
729730

730731
if (dSpeed[1] > FIX(.5)) {
731732
// Hit from the top
732-
relativeImpulse[1] = FIX(1.0);
733+
relativeImpulse[1] = FIX(1.0*magnitude);
733734
} else if (dSpeed[1] < FIX(-.5)) {
734735
// Hit from the bottom
735-
relativeImpulse[1] = FIX(-1.0);
736+
relativeImpulse[1] = FIX(-1.0*magnitude);
736737
}
737738

738739
pidReset(&pMotionX);
@@ -924,11 +925,7 @@ void CAbstractPlayer::DashboardPosition(CScaledBSP *part, bool autoRot, float x,
924925
// X/Y Coordinates on the screen are roughly described as a percentage of the screen away from the bottom and the left
925926
// (-1.0, -1.0) is the bottom left of the screen
926927
// (1.0, 1.0) is the top right of the screen
927-
928-
929-
// TODO: Adjust these until it looks good, then go multiply
930-
// all the DashboardPosition parameters by these numbers, and
931-
// then delete these
928+
932929
float scale_x = 11.12;
933930
float scale_y = 8.23;
934931
Fixed hud_dist = (FIX3(6000) * 25)/8;

src/game/CAvaraApp.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ void CAvaraAppImpl::RenderContents() {
200200
}
201201

202202
void CAvaraAppImpl::WindowResized(int width, int height) {
203-
gRenderer->UpdateViewRect(width, height, mPixelRatio);
204-
gRenderer->ApplyFrameBuffer();
203+
// Only update if the resolution is actually changing
204+
if (gRenderer->viewParams->viewPixelDimensions.h != width || gRenderer->viewParams->viewPixelDimensions.v != height)
205+
gRenderer->UpdateViewRect(width, height, mPixelRatio);
205206
//performLayout();
206207
}
207208

src/render/AbstractRenderer.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ class AbstractRenderer {
3939
*/
4040
virtual void ApplyProjection() = 0;
4141

42-
/**
43-
* Update the frame buffer with the currently configured resolution and FOV.
44-
*/
45-
virtual void ApplyFrameBuffer() = 0;
46-
4742
/**
4843
* Reset the renderer's state back to its defaults.
4944
*/
@@ -108,7 +103,7 @@ class AbstractRenderer {
108103
* @param height The height in pixels.
109104
* @param pixelRatio The pixel ratio.
110105
*/
111-
void UpdateViewRect(int width, int height, float pixelRatio);
106+
virtual void UpdateViewRect(int width, int height, float pixelRatio) = 0;
112107
protected:
113108
float fov = 50.0f;
114109
};

src/render/ModernOpenGLRenderer.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ ModernOpenGLRenderer::ModernOpenGLRenderer(SDL_Window *window) : AbstractRendere
177177
glBindVertexArray(0);
178178
glBindBuffer(GL_ARRAY_BUFFER, 0);
179179

180-
MakeFramebuffer(0, w, h);
181-
MakeFramebuffer(1, w, h);
182-
183180
// Configure alpha blending.
184181
glEnable(GL_BLEND);
185182
glBlendFuncSeparate(GL_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE);
@@ -275,12 +272,15 @@ void ModernOpenGLRenderer::ApplyProjection()
275272
glCheckErrors();
276273
}
277274

278-
void ModernOpenGLRenderer::ApplyFrameBuffer()
275+
void ModernOpenGLRenderer::UpdateViewRect(int width, int height, float pixelRatio)
279276
{
277+
AbstractRenderer::UpdateViewRect(width, height, pixelRatio);
278+
280279
GLsizei w, h;
281280
SDL_GL_GetDrawableSize(window, &w, &h);
282-
MakeFramebuffer(0, w, h);
283-
MakeFramebuffer(1, w, h);
281+
282+
AdjustFramebuffer(0, w, h);
283+
AdjustFramebuffer(1, w, h);
284284
}
285285

286286
void ModernOpenGLRenderer::LevelReset()
@@ -575,8 +575,13 @@ std::unique_ptr<OpenGLShader> ModernOpenGLRenderer::LoadShader(const std::string
575575
return std::make_unique<OpenGLShader>(*vertPath, *fragPath);
576576
}
577577

578-
void ModernOpenGLRenderer::MakeFramebuffer(short index, GLsizei width, GLsizei height)
578+
void ModernOpenGLRenderer::AdjustFramebuffer(short index, GLsizei width, GLsizei height)
579579
{
580+
// Remove previous bound objects
581+
glDeleteTextures(1, &texture[index]);
582+
glDeleteFramebuffers(1, &fbo[index]);
583+
glDeleteRenderbuffers(1, &rbo[index]);
584+
580585
// Create a framebuffer, texture, and renderbuffer for the HUD.
581586
glGenFramebuffers(1, &fbo[index]);
582587
glBindFramebuffer(GL_FRAMEBUFFER, fbo[index]);

src/render/ModernOpenGLRenderer.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class ModernOpenGLRenderer final: public AbstractRenderer {
1919
virtual void AddPart(CBSPPart *part) override;
2020
virtual void ApplyLights() override;
2121
virtual void ApplyProjection() override;
22-
virtual void ApplyFrameBuffer() override;
2322
virtual void LevelReset() override;
2423
virtual std::unique_ptr<VertexData> NewVertexDataInstance() override;
2524
virtual void OverheadPoint(Fixed *pt, Fixed *extent) override;
2625
virtual void RefreshWindow() override;
2726
virtual void RemoveHUDPart(CBSPPart *part) override;
2827
virtual void RemovePart(CBSPPart *part) override;
2928
virtual void RenderFrame() override;
29+
virtual void UpdateViewRect(int width, int height, float pixelRatio) override;
3030
private:
3131
SDL_Window *window;
3232

@@ -55,6 +55,6 @@ class ModernOpenGLRenderer final: public AbstractRenderer {
5555
void Draw(OpenGLShader &shader, const CBSPPart &part, float defaultAmbient, bool useAlphaBuffer = false);
5656
void IgnoreDirectionalLights(OpenGLShader &shader, bool yn);
5757
std::unique_ptr<OpenGLShader> LoadShader(const std::string &vertFile, const std::string &fragFile);
58-
void MakeFramebuffer(short index, GLsizei width, GLsizei height);
58+
void AdjustFramebuffer(short index, GLsizei width, GLsizei height);
5959
void SetTransforms(const CBSPPart &part);
6060
};

0 commit comments

Comments
 (0)