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

Author Topic: Window to fill all screen and Texture transparent color  (Read 2785 times)

0 Members and 1 Guest are viewing this topic.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 233
    • View Profile
    • Email
Window to fill all screen and Texture transparent color
« on: March 04, 2025, 07:14:10 pm »
Hello all

I have two questions:

Is there a way for window with VideoMode(400, 300, 24) to fill all screen? Well, not all the width exactly (because modern screens' widths are more than 4/3) but keeping aspect ratio.

Is there a way to assign transparent color to a Texture without using the Image class? The code I have by now is below, the purpose is to dispense with the Image object/code.

Image image = new Image("file.png");
image.CreateMaskFromColor(color);
Texture texture = new Texture(image);
 

Thank you very much
Pablo

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11173
    • View Profile
    • development blog
    • Email
Re: Window to fill all screen and Texture transparent color
« Reply #1 on: March 06, 2025, 08:12:27 am »
Is there a way for window with VideoMode(400, 300, 24) to fill all screen? Well, not all the width exactly (because modern screens' widths are more than 4/3) but keeping aspect ratio.
Not by sticking to the given video mode. You'd need to use a video mode that matches the aspect ratio of the monitor and then you can use the sf::View to retain the 4:3 aspect ratio, while keeping black bars on the side. This is the so called letterbox effect.

Is there a way to assign transparent color to a Texture without using the Image class? The code I have by now is below, the purpose is to dispense with the Image object/code.
You could use a render texture and call Clear() on it with the transparent color you want.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tigre Pablito

  • Full Member
  • ***
  • Posts: 233
    • View Profile
    • Email
Re: Window to fill all screen and Texture transparent color
« Reply #2 on: March 10, 2025, 03:50:43 pm »
Hello

Thanks for your reply.

I saw the letterbox effect code. Could you tell me how to connect my RenderWindow object with the function getLetterboxView? In the post/example it is not shown.

Thank you
Pablo

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11173
    • View Profile
    • development blog
    • Email
Re: Window to fill all screen and Texture transparent color
« Reply #3 on: March 10, 2025, 03:55:54 pm »
It's the same for SFML.Net as it is for SFML and it's shown in the example code, assuming you're referring to the Wiki entry: https://github.com/SFML/SFML/wiki/Source%3A-Letterbox-effect-using-a-view

You pass in the current view and the wanted resolution and you get out a view that you can use with setView. It needs to be updated when the window size changes, i.e. when the Resized event is triggered.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tigre Pablito

  • Full Member
  • ***
  • Posts: 233
    • View Profile
    • Email
Re: Window to fill all screen and Texture transparent color
« Reply #4 on: March 10, 2025, 08:56:33 pm »
Hi

I already got the getLetterboxView() code and I'm using it but I don't get the result I want. What I want is a 400x300 window/View that fills all the screen (yes, keeping aspect ratio 4/3 so with the black side bars). Is it possible in SFML.Net? My current code (not working as I expect) is below (the main loop).

            while (true)
            {
                time = time.AddMilliseconds(20);
                if (exit == true)
                    break;
                Images.window.Clear();
                Images.window.Draw(Images.place);
                player.Update();
                player.Shoots.Update();
                zombies.Update();
                player.Display();
                player.Shoots.Display();
                zombies.Display();
                Images.window.SetView(getLetterboxView(Images.window.GetView(), 400, 300));
                Images.window.Display();
                Images.window.DispatchEvents();
                while (DateTime.Now < time)
                    ;
            }
 

Your help is much appreciated.
Thanks
Pablo

Hapax

  • Hero Member
  • *****
  • Posts: 3415
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Window to fill all screen and Texture transparent color
« Reply #5 on: March 10, 2025, 09:57:51 pm »
We should clarify something here.

A window that is 400x300 will not fill the screen unless the screen's resolution is 400x300.
So, to fill the screen, we can change its resolution and go fullscreen with the window,
or we can make the size of the window the same size as the current resolution (this is commonly called "Windowed Fullscreen".)

The other thing to consider is that I expect you want to work with co-ordinates that go from (0, 0) to (399, 299) and to set that, you use a view.

So, first create a "target view" (the one you want to use within the window) like this:
sf::View targetView{};
targetView.setSize({ 400.f, 300.f });
targetView.setCenter({ 200.f, 150.f });
NOTE: I've set the size and center separately so it's clear what is happening but you can also set it in its constructor.

Then, set the window to use this target view:
window.setView(targetView);

Then, you can change the size of the window to match the full screen (or allow it to be resized).
The window will change size but the view will always be 400x300 so drawing at (200, 150) will always be in the centre.

You can just leave it there if you don't don't want the letterboxing effect and the window will always use the co-ordinates (0, 0) - (400, 300)-ish

But, since you do also want the letterboxing effect, you can use that function like this (whenever the window changes size, including when you set it or when it is resized):
window.setView(getLetterboxView(view, windowSize.x, windowSize.y));
windowSize is the actual current size of the window, and
view is the view we created earlier (it doesn't change)

This is obviously C++ code that I've used for the examples so you'll need to adapt to the version/language you're using.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tigre Pablito

  • Full Member
  • ***
  • Posts: 233
    • View Profile
    • Email
Re: Window to fill all screen and Texture transparent color
« Reply #6 on: March 11, 2025, 03:10:12 am »
Hello Hapax

I retouched my code to this:

        public void Run()
        {
            View target = new View(new Vector2f(200, 150), new Vector2f(400, 300));
            //Images.window.SetView(target);
            Images.window.SetView(getLetterboxView(target, 400, 300));

            DateTime time = DateTime.Now;

            while (true)
            {
                time = time.AddMilliseconds(20);
                if (exit == true)
                    break;
                Images.window.Clear();
                Images.window.Draw(Images.place);
                player.Update();
                player.Shoots.Update();
                zombies.Update();
                player.Display();
                player.Shoots.Display();
                zombies.Display();
                Images.window.Display();
                Images.window.DispatchEvents();
                while (DateTime.Now < time)
                    ;
            }
        }
 

and now it is a bit better, but this time it fills the whole screen, this is, no black side bars.
What did I do wrong?

Thank you
Pablo


Hapax

  • Hero Member
  • *****
  • Posts: 3415
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Window to fill all screen and Texture transparent color
« Reply #7 on: March 12, 2025, 04:05:32 pm »
The size parameters for getLetterboxView are the actual width and height of the window. If your window is full-screen, these parameters should be the current resolution. Otherwise it should be the size of window in pixels.

sf::Vector2u windowSize{ window.getSize() }; // get the current size of the window
getLetterboxView(target, windowSize.x, windowSize.y) // use the window size here; the function sorts out the rest.

My guess as to how it would look in your code would be something like:
Vector2u windowSize = new Vector2u(Images.window.getSize()); // get the current size of the window
View target = new View(new Vector2f(200, 150), new Vector2f(400, 300));
Images.window.SetView(getLetterboxView(target, windowSize.x, windowSize.y); // use the window size here; the function sorts out the rest.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tigre Pablito

  • Full Member
  • ***
  • Posts: 233
    • View Profile
    • Email
Re: Window to fill all screen and Texture transparent color
« Reply #8 on: March 13, 2025, 05:09:55 am »
Hello!

@Hapax you finished to solved muy issue. Thank you! You're very nice!

Also much thanks to @eXpl0it3r for his before replies, very nice too!

Regards
Pablo