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 - Deathslice246

Pages: [1]
1
General / Re: How to resize my sprite and not have it so stretched out?
« on: November 08, 2016, 08:25:14 pm »
Interesting, if I change

const sf::Vector2f windowSize(sf::Vector2f(gameWindow.getSize()));
 

to

const sf::Vector2f windowSize(gameWindow.getView().getSize());
 

it removes the black portion that appears when I maximized the screen but now the image is even bigger. :/

2
General / Re: How to resize my sprite and not have it so stretched out?
« on: November 08, 2016, 04:34:45 pm »
So this is what I was able to come up with. it's ok but the problem is that as I increase the size of the screen, the image does not increase and a black portion appears on the screen(meaning it is not fitting to the entire screen).

Here is the relevant code

else if (event.type == sf::Event::Resized)
{
                        // Get the new size of the window
                        sf::Vector2f visibleArea = sf::Vector2f((float)event.size.width, (float)event.size.height);

                        // Find the scale factor in the x and the y axis
                        const float scaleXFactor = visibleArea.x / background.getLocalBounds().width;
                        const float scaleYFactor = visibleArea.y / background.getLocalBounds().height;

                        // Pick the smallest factor and get the new view size.
                        const float lowestFactor = std::min(scaleXFactor, scaleYFactor);
                        const sf::Vector2f windowSize(sf::Vector2f(gameWindow.getSize()));
                        const sf::Vector2f viewSize = windowSize / lowestFactor;

                        gameView.setSize(viewSize);
                        gameView.setCenter(viewSize / 2.0f);
                        gameWindow.setView(gameView);
}
 

To put it into perspective, this is what I do initially at the start of the program.

background.setTexture(textureManager.getTexture("ChessBoard")); // get the texture and set it.

sf::Vector2u backgroundSize = background.getTexture()->getSize(); // get its size

// initially set the window size to be the max size of the image.
gameWindow.create(sf::VideoMode(backgroundSize.x, backgroundSize.y), "Chess", sf::Style::Default);
gameWindow.setFramerateLimit(60);

// and set it as the view.
sf::FloatRect visibleArea(0.0f, 0.0f, (float)backgroundSize.x, (float)backgroundSize.y);
gameView = sf::View(visibleArea);
gameWindow.setView(gameView);
 

3
General / Re: How to resize my sprite and not have it so stretched out?
« on: November 08, 2016, 03:51:04 am »
To put it this way, I want my image to look more or less the same quality before it was resizes(so I guess to resize with the window and not lose it's aspect ratio). I don't know how else I can say it to you.

4
General / Re: How to resize my sprite and not have it so stretched out?
« on: November 08, 2016, 01:36:01 am »
Well, I would like to adjust the size of the image as the screen grows and shrink so that it doesn't look all stretched and I don't really want to go for a letterbox effect.

5
General / Re: How to resize my sprite and not have it so stretched out?
« on: November 07, 2016, 11:47:12 pm »
Here are two snapshots of the image with and without the screen being maximized.

http://imgur.com/a/UTZO2

See how the second image looks all ugly and stretched.

That is what I mean.

6
General / Re: How to resize my sprite and not have it so stretched out?
« on: November 07, 2016, 08:44:22 pm »
Well I need a little bit of help with this.

So initially, I load my sprite like this 

textureBoardPath.loadFromFile("ChessBoard.png");

sf::Vector2u backgroundSize = background.getTexture()->getSize();

gameWindow.create(sf::VideoMode(backgroundSize.x, backgroundSize.y), "Chess", sf::Style::Close | sf::Style::Resize);
 

This is fine because it will load the image properly and the image will not looked scretch because it will be set to the size of the image.  The problem is when the user resizes it.

 I have an if that checks for resize events and I do this in that if statement

gameView.setSize((float)event.size.width, (float)event.size.height);
gameWindow.setView(gameView);
background.setScale(
                                float(event.size.width) / float(background.getTexture()->getSize().x),
                                float(event.size.height) / float(background.getTexture()->getSize().y));
 

This stretches the image quite a bit and the reason is probably because as the targetSize gets bigger, the background width and height does not change which means that the scale ratio is off(I'm not sure if that is the case though). Any guidances?

7
General / Re: How to resize my sprite and not have it so stretched out?
« on: November 03, 2016, 08:30:32 pm »
No, I have not use view anywhere in my code.  I figured that if I scaled the image, that the changes would be reflected when I executed the program. I will read what SFML has to say about the view class.

8
General / How to resize my sprite and not have it so stretched out?
« on: November 03, 2016, 02:26:04 pm »
What I'm trying to do as the moment is trying to resize my chessboard sprite so that it fits the entire screen when the user tries to resize the window but the problem that I'm having is that the sprite looks all stretched out and ugly.

So what I tried to do was to check for a resizing event and then I divide the new screen size with the old screen size and multiply it by 100 to get the scale factor in the x and y axis but this didn't help at all.

The video mode is set to 700, 703, 24 because that is the exact width, height and bit depth that the png image has so that is why I made that resolution the default one.

Main.cpp

#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include "Board.hpp"

void checkInput(sf::RenderWindow &, Board chessBoard);
void resizeObjects(sf::RenderWindow &, Board chessBoard, sf::Event);
void updateScreen(sf::RenderWindow &, sf::Sprite);

int main()
{
        sf::RenderWindow gameWindow(sf::VideoMode(700, 703, 24), "Chess");

        sf::Texture textureBoardPath;
     
        textureBoardPath.loadFromFile("ChessBoard.png");

        Board chessBoard(textureBoardPath);
       
        while (gameWindow.isOpen())
        {
                checkInput(gameWindow, chessBoard);
                updateScreen(gameWindow, chessBoard.getBoard());
        }

        return 0;
}

void checkInput(sf::RenderWindow &gameWindow, Board chessBoard)
{
        sf::Event event;

        while (gameWindow.pollEvent(event))
        {
                if (event.type == sf::Event::Closed)
                {
                        gameWindow.close();
                }
                else if (event.type == sf::Event::Resized)
                {
                        resizeObjects(gameWindow, chessBoard, event);
                }
        }
}

void resizeObjects(sf::RenderWindow &gameWindow, Board chessBoard, sf::Event event)
{
        float scaleXFactor = event.size.width / gameWindow.getView().getSize().x * 100;
        float scaleYFactor = event.size.height / gameWindow.getView().getSize().y * 100;

        chessBoard.resizeBoard(scaleXFactor, scaleYFactor);
}

void updateScreen(sf::RenderWindow &gameWindow, sf::Sprite chessBoard)
{
        gameWindow.clear();
        gameWindow.draw(chessBoard);
        gameWindow.display();
}
 

Board.hpp

#pragma once

#include <SFML/Graphics/Sprite.hpp>

class Board
{
        public:
                Board(const sf::Texture &);
                sf::Sprite getBoard() const;
                void resizeBoard(float, float);

        private:
                int board[8][8] =
                { -1,-2,-3,-4,-5,-3,-2,-1
                  -6,-6,-6,-6,-6,-6,-6,-6,
                   0, 0, 0, 0, 0, 0, 0, 0,
                   0, 0, 0, 0, 0, 0, 0, 0,
                   0, 0, 0, 0, 0, 0, 0, 0,
                   0, 0, 0, 0, 0, 0, 0, 0,
                   6, 6, 6, 6, 6, 6, 6, 6,
                   1, 2, 3, 4, 5, 3, 2, 1 };

                sf::Sprite spriteBoard;
                sf::Sprite chessPieces[32];

};
 

Board.cpp

#include "Board.hpp"
#include <SFML/Graphics/Texture.hpp>

Board::Board(const sf::Texture &textureBoardPath): spriteBoard(textureBoardPath)
{
}

sf::Sprite Board::getBoard() const
{
        return spriteBoard;
}

void Board::resizeBoard(float scaleXFactor, float scaleYFactor)
{
        spriteBoard.setScale(scaleXFactor, scaleYFactor);
}
 

Pages: [1]