Skip to content

Commit

Permalink
Refactored window resize handling to prevent memory leaks when updati…
Browse files Browse the repository at this point in the history
…ng 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
  • Loading branch information
Ymihere03 committed Aug 13, 2024
1 parent a0a5e3f commit c3f2bfb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
17 changes: 7 additions & 10 deletions src/game/CAbstractPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ void CAbstractPlayer::LoadDashboardParts() {
dashboardSpinSpeed = ToFixed(100);
dashboardSpinHeading = 0;

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

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

if (angleDiff > 0) {
// Hit from the right side
relativeImpulse[0] = FIX(1.0);
relativeImpulse[0] = FIX(1.0*magnitude);
} else if (angleDiff < 0) {
// Hit from the left side
relativeImpulse[0] = FIX(-1.0);
relativeImpulse[0] = FIX(-1.0*magnitude);
}

if (dSpeed[1] > FIX(.5)) {
// Hit from the top
relativeImpulse[1] = FIX(1.0);
relativeImpulse[1] = FIX(1.0*magnitude);
} else if (dSpeed[1] < FIX(-.5)) {
// Hit from the bottom
relativeImpulse[1] = FIX(-1.0);
relativeImpulse[1] = FIX(-1.0*magnitude);
}

pidReset(&pMotionX);
Expand Down Expand Up @@ -924,11 +925,7 @@ void CAbstractPlayer::DashboardPosition(CScaledBSP *part, bool autoRot, float x,
// X/Y Coordinates on the screen are roughly described as a percentage of the screen away from the bottom and the left
// (-1.0, -1.0) is the bottom left of the screen
// (1.0, 1.0) is the top right of the screen


// TODO: Adjust these until it looks good, then go multiply
// all the DashboardPosition parameters by these numbers, and
// then delete these

float scale_x = 11.12;
float scale_y = 8.23;
Fixed hud_dist = (FIX3(6000) * 25)/8;
Expand Down
5 changes: 3 additions & 2 deletions src/game/CAvaraApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ void CAvaraAppImpl::RenderContents() {
}

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

Expand Down
7 changes: 1 addition & 6 deletions src/render/AbstractRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class AbstractRenderer {
*/
virtual void ApplyProjection() = 0;

/**
* Update the frame buffer with the currently configured resolution and FOV.
*/
virtual void ApplyFrameBuffer() = 0;

/**
* Reset the renderer's state back to its defaults.
*/
Expand Down Expand Up @@ -108,7 +103,7 @@ class AbstractRenderer {
* @param height The height in pixels.
* @param pixelRatio The pixel ratio.
*/
void UpdateViewRect(int width, int height, float pixelRatio);
virtual void UpdateViewRect(int width, int height, float pixelRatio) = 0;
protected:
float fov = 50.0f;
};
Expand Down
19 changes: 12 additions & 7 deletions src/render/ModernOpenGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ ModernOpenGLRenderer::ModernOpenGLRenderer(SDL_Window *window) : AbstractRendere
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

MakeFramebuffer(0, w, h);
MakeFramebuffer(1, w, h);

// Configure alpha blending.
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE);
Expand Down Expand Up @@ -275,12 +272,15 @@ void ModernOpenGLRenderer::ApplyProjection()
glCheckErrors();
}

void ModernOpenGLRenderer::ApplyFrameBuffer()
void ModernOpenGLRenderer::UpdateViewRect(int width, int height, float pixelRatio)
{
AbstractRenderer::UpdateViewRect(width, height, pixelRatio);

GLsizei w, h;
SDL_GL_GetDrawableSize(window, &w, &h);
MakeFramebuffer(0, w, h);
MakeFramebuffer(1, w, h);

AdjustFramebuffer(0, w, h);
AdjustFramebuffer(1, w, h);
}

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

void ModernOpenGLRenderer::MakeFramebuffer(short index, GLsizei width, GLsizei height)
void ModernOpenGLRenderer::AdjustFramebuffer(short index, GLsizei width, GLsizei height)
{
// Remove previous bound objects
glDeleteTextures(1, &texture[index]);
glDeleteFramebuffers(1, &fbo[index]);
glDeleteRenderbuffers(1, &rbo[index]);

// Create a framebuffer, texture, and renderbuffer for the HUD.
glGenFramebuffers(1, &fbo[index]);
glBindFramebuffer(GL_FRAMEBUFFER, fbo[index]);
Expand Down
4 changes: 2 additions & 2 deletions src/render/ModernOpenGLRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class ModernOpenGLRenderer final: public AbstractRenderer {
virtual void AddPart(CBSPPart *part) override;
virtual void ApplyLights() override;
virtual void ApplyProjection() override;
virtual void ApplyFrameBuffer() override;
virtual void LevelReset() override;
virtual std::unique_ptr<VertexData> NewVertexDataInstance() override;
virtual void OverheadPoint(Fixed *pt, Fixed *extent) override;
virtual void RefreshWindow() override;
virtual void RemoveHUDPart(CBSPPart *part) override;
virtual void RemovePart(CBSPPart *part) override;
virtual void RenderFrame() override;
virtual void UpdateViewRect(int width, int height, float pixelRatio) override;
private:
SDL_Window *window;

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

0 comments on commit c3f2bfb

Please sign in to comment.