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

Author Topic: [Solved] Loading new images into textures causes strange effects.  (Read 1342 times)

0 Members and 1 Guest are viewing this topic.

qaz12321

  • Newbie
  • *
  • Posts: 3
  • What does this personal text option do?
    • View Profile
I am trying to make a program which involves the display images of variable dimensions. I am using a Json file to store the paths to images which I use to update the used image in a texture which is used by a sprite. I am having unforeseen issues with this approach, but I do not know whether it is my execution that is the issue or the approach itself.

I initially create a 650x650 texture and update the texture using a function in a header file.

main.cpp
#include <SFML/Graphics.hpp>
#include <json/value.h>
#include <json/json.h>
#include <fstream>
#include <string>
#include <iostream>
#include "playerFunctions.h"

int main()
{
    int currentSongID = 1;

    sf::RenderWindow window(sf::VideoMode(1680, 1680*2),"SFML Player", sf::Style::Default);
    window.setFramerateLimit(60);

    //Music Cover code
    std::string coverPath = "res/" + currentSong["Cover"].asString();

    sf::Texture coverImg;
    coverImg.setSmooth(true);
    if (!coverImg.create(650, 650))
    {
        // error...
        coverImg.loadFromFile("res/NoImageFound.png");
    }
    sf::Sprite cover;
    cover.setTexture(coverImg);

    loadCover(cover, coverImg, coverPath);
   
    //Window
    window.setSize(sf::Vector2u(300,600));
    window.setPosition(sf::Vector2i(50,50));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Resized)
            {
                window.setSize(sf::Vector2u(window.getSize().x, window.getSize().x * 2));

            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
            {
                if (currentSongID == 2) {
                    currentSongID = 0;
                }
                else {
                    currentSongID++;
                }

                currentSong = songRoot[currentSongID];
                coverPath = "res/" + currentSong["Cover"].asString();
                std::cout << "Pressed\n" << currentSongID;
            }
        }

        loadCover(cover, coverImg, coverPath);

        //render
        window.clear(sf::Color(49,51,56));

        window.draw(cover);

        window.display();

    }

    return 0;
playerFunctions.h
#pragma once

void loadCover(sf::Sprite& cover, sf::Texture& image, std::string path) {
    if (!image.loadFromFile(path))
    {
        // error...
        image.loadFromFile("res/NoImageFound.png");
    }
    unsigned int a;
    image.getSize().x > image.getSize().y ? a = image.getSize().x : a = image.getSize().y;
    cover.setScale(sf::Vector2f(1680 / (float)a, 1680 / (float)a));
    //std::cout << a;
}
When running the program unexpected problems occur. I programmed my sprite to scale based on the size of the image however the image gets cut off if it is bigger than the image I initially created (attached is a picture of a 800x800 texture being drawn when the initial texture is 650x650). If I change the values of the initial image to be larger than the drawn image it creates a strange "bleeding" effect (attached is a picture of a 800x800 texture being drawn when the initial texture is 1000x1000). If the image is the same size as the initial image it is drawn without issue (attached is a picture of a 800x800 texture being drawn when the initial texture is 800x800), but if the image ever changes the issues return (attached is a picture of a 800x500 texture being drawn when the initial texture is 8000x800.

I have tried to no avail to figure out a solution to this. I apologize if this is obvious, and I am simply blind.

EDIT: I tried to clean up this post by removing irrelevant code, leaving the parts that could possibly be relevant.
« Last Edit: February 28, 2023, 10:44:17 am by qaz12321 »

qaz12321

  • Newbie
  • *
  • Posts: 3
  • What does this personal text option do?
    • View Profile
Re: Loading new images into textures causes strange effects.
« Reply #1 on: February 28, 2023, 09:19:47 am »
I just realized attachments need to be downloaded to be seen, here is an imgur link.
https://imgur.com/a/qx9uEA7

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Loading new images into textures causes strange effects.
« Reply #2 on: February 28, 2023, 10:22:48 am »
I'm not sure because I've never replaced a texture this way. I think it may be because the initial TextureRect of your sprite is set to 650 650 when you call the first setTexture and you never change it despite loading a new texture to the same variable.
Maybe try using setTextureRect with the size of your new texture after you load a new one, or call setTexture with your new texture and the resetRect argument set to true after you load a new one

qaz12321

  • Newbie
  • *
  • Posts: 3
  • What does this personal text option do?
    • View Profile
Re: Loading new images into textures causes strange effects.
« Reply #3 on: February 28, 2023, 10:43:37 am »
I'm not sure because I've never replaced a texture this way. I think it may be because the initial TextureRect of your sprite is set to 650 650 when you call the first setTexture and you never change it despite loading a new texture to the same variable.
Maybe try using setTextureRect with the size of your new texture after you load a new one, or call setTexture with your new texture and the resetRect argument set to true after you load a new one

Thank you very much. I tried setting the textureRect of the sprite to be the size of the new image and it worked.