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

Author Topic: Problem When a Window is Very Small  (Read 5139 times)

0 Members and 1 Guest are viewing this topic.

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Problem When a Window is Very Small
« on: February 13, 2016, 10:39:13 pm »
Hi, I'm using a 64 by 32 pixels window, and this happens :

64 by 32:


The magenta is the "window.clear" color, red is an sf::image with the same width and height as the window, so it should cover the whole window, but it doesn't. I though I was doing something wrong, until I decided to see what happens if I increase the window and image sizes, and it works then as intended - they are still equal - :

640 by 320:


The problem occurs when the width is 115 or less, not sure if it is an sfml bug, or if I'm missing something.  Here's the code:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int main()
{
        const int h = 32;
        const int w = 64;

        sf::RenderWindow window(sf::VideoMode(w, h), "Chip_8");
        sf::Image  image;
        image.create(w, h, sf::Color::Red);
        sf::Texture timage;
        timage.loadFromImage(image);
        sf::Sprite simage(timage);
        window.setSize( sf::Vector2u(600, 300));

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear(sf::Color::Magenta);
                timage.update(image);
                window.draw(simage);
                window.display();
        }


        return 0;
}
« Last Edit: February 13, 2016, 10:59:47 pm by K.F »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem When a Window is Very Small
« Reply #1 on: February 14, 2016, 02:09:00 am »
There is a minimum requirement for the width of a window when it has decorations (title bar etc.)

Windows, in your case, won't create a window as small as you request so it creates one at its minimum. You can see the minimum in action when you try to resize the window; it won't shrink to the size you want.

However, if you remove the decorations (i.e. use sf::Style::None for the window style), it'll happily create any size you want.
Warning: you may want to add a way to close the program with a key otherwise you'll need to close it via the taskbar/task manager.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Problem When a Window is Very Small
« Reply #2 on: February 14, 2016, 11:27:26 am »
Thanks, that makes sense now  ;D

Is there a way to have the window bigger than the min size, while still having a 64 pixels width? I don't care about the window size - actually I prefer it big -, but I do care about the pixel number, and I'd like to avoid making a single pixel 5 pixels wide.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem When a Window is Very Small
« Reply #3 on: February 14, 2016, 05:49:00 pm »
By minimum "size", I really mean minimum "values for each dimension", not minimum "area".

The width of the window cannot be less wide than the operating system requires for its "stuff" (resize and close buttons, even if they aren't being displayed). As I mentioned previously, you can make the window less wide if you remove these "decorations" and use a window with style sf::Style::None.

That is, though, the actual pixel size of the window. The abstract, co-ordinate system used to draw is determined by the view. I'm not sure if this is what you want or what you were saying you don't want but you can have a larger window and use co-ordinates as if the window was smaller by scaling/resizing the view.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Problem When a Window is Very Small
« Reply #4 on: February 14, 2016, 07:25:11 pm »
Yeah, I know, I might have not explained properly, the problem is that sfml does not resize the window to the min size, it actually forces the window to be 115 pixels wide - real framebuffer pixels, not just pixels added from resizing -, so other stuff - on the right side - gets rendered when they shouldn't, if it just scaled the window - as in resized -, it would have been fine in my case.

I solved my problem by just scaling the sprite horizontally until it filled the added gap since it's the only entity I need, and now it works even if it is stretched, but proportion is not a problem in my case. So I guess I'll just need to keep in mind in the future that sfml cannot render bordered windows that are less than 115 pixels wide.

Thanks for the help.  ;D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Problem When a Window is Very Small
« Reply #5 on: February 14, 2016, 08:23:24 pm »
You could also just use a larger image or as it has been said already, adjust the view. There's a dedicated tutorial on how to use views.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Problem When a Window is Very Small
« Reply #6 on: February 14, 2016, 09:08:17 pm »
I know, I know  :)  I just wanted to make sure I wasn't missing an obvious fix. Actually now that I think about it, I'll need a bigger image anyway to display the text overlay later.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem When a Window is Very Small
« Reply #7 on: February 16, 2016, 01:44:56 am »
I'll just need to keep in mind in the future that sfml cannot render bordered windows that are less than 115 pixels wide.
It's not SFML causing this restriction; it's the operating system.
I dare say that the actual value of 115 is not absolute and would depend on your theme/style (sizes of buttons, frames etc.) and the actual operating system as others may well use different values or sizes. In fact, other operating systems may not even have this restriction.
With this in mind, you should consider not using 115 as a base if you want your software to used on other people's computers  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Problem When a Window is Very Small
« Reply #8 on: February 16, 2016, 02:14:22 pm »
It's not SFML causing this restriction; it's the operating system.

Not exactly, as the code shows, I wanted a window with a framebuffer of 64 by 32 pixels, but the actual window size that I wanted is 600 by 300 which is bigger than the os minimum, but the problem still occurs. The limit should not be bound by the framebuffer size.

I'm not familiar with the inner working of sfml, but I'm guessing this could be solved by giving the option of specifying the window size - not just the framebuffer size - before the window is created, not just after. Sounds like an option that could be added to the window style class.

Again, not a big deal that is needed often. Heck, even I don't need it anymore.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem When a Window is Very Small
« Reply #9 on: February 16, 2016, 05:41:04 pm »
The framebuffer is required to be exactly the same size as the window so that it can be instantly swapped.
The effect you are noticing is because the view is set at window construction but not resized to match the window when you resize it. You can simply create the window at the size you want it and then size the view to match the co-ordinate system you wanted.

If you need to actually have a render target with a size different from the window, use an sf::RenderTexture and then draw it to the window.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Problem When a Window is Very Small
« Reply #10 on: February 23, 2016, 11:33:41 pm »
Yay  ;D



It's a chip-8 emulator running a pong rom by the way, not a pong clone  ;)

Thanks everyone for the help  :)

 

anything