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.