Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - WarpedJoy

Pages: [1]
1
Window / Scaling to pixels math
« on: February 18, 2025, 07:07:33 pm »
Hello, I am a new SFML and C++ programmer (but I've coded others) btw.

I am trying to get my game window to stay 16:9 whenever it's resized by scaling a 160 x 90 image and resizing the window to that sprite's scale.

Currently, it just.. well, I don't know how exactly to explain it, but the image is not scaling to the window's width.

Here's the code only necessary to share
int lastW = 1600; //window size at startup
int lastH = 900;
bool showDevTool1 = true;
float ratioW = 16.0f / 9.0f;
float ratioH = 9.0f / 16.0f;
std::string RatioHandles = "Images/Debug/Aspect_ratio_handles.png";

//Render window
int main() {
    sf::RenderWindow
    window(sf::VideoMode(lastW, lastH), "Gaem");

//16:9 ratio handles
        sf::Texture RHtexture;
        if (!RHtexture.loadFromFile(RatioHandles)) {
            return -1; // Exit if loading fails
        };
        sf::Sprite devTool1;
        devTool1.setTexture(RHtexture);

//Keep window open
    while (window.isOpen()) {
        sf::Event event;

//Close button closes window
    while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();

//Can only resize as 16:9 ratio
            else if (event.type == sf::Event::Resized) {
                window.draw(devTool1);

//if width resized, do this
                if (lastW != event.size.width) {
                    std::cout << event.size.width;
                    std::cout << "---";
                    std::cout << RHtexture.getSize().x;
                    std::cout << "---";
                    devTool1.setScale(
                        event.size.width / RHtexture.getSize().x / 10,
                        lastH / RHtexture.getSize().y / 10);
                    window.setSize(sf::Vector2u(RHtexture.getSize().x * event.size.width, event.size.height));
                    lastW = event.size.width;
                    break;
                }

Pages: [1]