Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
2.2 kB
2
Indexable
Never
 You're absolutely right, and I apologize for that oversight. Let's correct the `Renderer` constructor to properly initialize all members in the initializer list. Here's the corrected version of the `Renderer` constructor:

```cpp
Renderer::Renderer(sf::RenderWindow& window)
    : window(window),
      background(sf::Vector2f(windowWidth, windowHeight)),
      playfieldBoundary(sf::Vector2f(playfieldWidth, playfieldHeight)),
      nextTempoBoundary(sf::Vector2f(playfieldWidth / 1.5f, playfieldHeight / 5.0f)),
      scoreBoundary(sf::Vector2f(playfieldWidth / 1.5f, playfieldHeight / 12.0f)),
      highScoreBoundary(sf::Vector2f(playfieldWidth / 1.5f, playfieldHeight / 12.0f))
{
    if (!tileTexture.loadFromFile("images/tiles.png") ||
        !backgroundTexture.loadFromFile("images/background.png") ||
        !frameTexture.loadFromFile("images/frame.png") ||
        !font.loadFromFile("Roboto-Regular.ttf"))
    {
        throw std::runtime_error("Failed to load textures or font");
    }

    tileSprite.setTexture(tileTexture);
    frameSprite.setTexture(frameTexture);
    tileSprite.setScale(2, 2);

    background.setFillColor(sf::Color(200, 220, 255, 180));

    initializeUI();
}
```

In this corrected version:

1. We initialize all member variables that are objects (like `RectangleShape`s) in the initializer list.
2. We've moved the texture and font loading into the constructor body, with error checking.
3. We still call `initializeUI()` at the end of the constructor to set up the rest of the UI elements.

The `initializeUI()` function remains the same as in the previous version. This approach ensures that all member variables are properly initialized before the constructor body is executed, which is a best practice in C++ and avoids potential issues with uninitialized members.

Make sure to update the `render.h` file to include the necessary #include statements and any additional member variables you might need:

```cpp
#pragma once
#include <SFML/Graphics.hpp>
#include "game.h"
#include <stdexcept>

class Renderer {
    // ... (rest of the class definition remains the same)
};
```

This structure should resolve the initialization order issues and provide a more robust setup for your `Renderer` class.
Leave a Comment