Anyone know SFML to answer this screen mapping question?

In summary, the author is having difficulty setting up his graphics in a program and is wondering if anyone can help him.
  • #1
yungman
5,718
241
Hi

I am just starting to studying games with C++ by Horton for a few days. It uses SFML for graphics, sound and fonts. I am puzzled with the screen map location. In the program below, you can see the resolution is 1920 X 1080 full screen. But there are something funny when I create a sprite and put it in specified location. I actually took a picture with camera and screen capture and write the coordinates where I put the tree (810, 0)and the bee(400,510). But if I count on the resolution of 1920 X 1080, as I labeled top left is (0,0), bottom right is (1920, 1080), it way off!
Full screen.jpg


This is the program
C++:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace sf;
using namespace std;
int main()
{   VideoMode vm(1920, 1080);//set screen resolution 1920 X 1080
    RenderWindow window(vm, "Timber!", Style::Fullscreen);//create window object
    Texture textureBackground;//Create texture to hold graphic on GPU
    textureBackground.loadFromFile("graphics/background.png");//load graphic into texture
    Sprite spriteBackground;//create a sprite
    spriteBackground.setTexture(textureBackground);//attach texture to sprite
    spriteBackground.setPosition(0, 0);//set sprite to cover the screen

    Texture textureTree;
    textureTree.loadFromFile("graphics/tree.png");
    Sprite spriteTree;
    spriteTree.setTexture(textureTree);
    spriteTree.setPosition(810, 0);//810 should put a little left from center

    Texture textureBee;
    textureBee.loadFromFile("graphics/bee.png");
    Sprite spriteBee;
    spriteBee.setTexture(textureBee);
    spriteBee.setPosition(400, 510);
    bool beeActive = false;
    float beeSpeed = 0.0f;

    Texture textureCloud;
    textureCloud.loadFromFile("graphics/cloud.png");
    Sprite spriteCloud1;
    Sprite spriteCloud2;
    Sprite spriteCloud3;
    spriteCloud1.setTexture(textureCloud);
    spriteCloud2.setTexture(textureCloud);
    spriteCloud3.setTexture(textureCloud);
    spriteCloud1.setPosition(0, 0);
    spriteCloud2.setPosition(0, 250);
    spriteCloud3.setPosition(0, 500);
    bool cloud1Active = false;
    bool cloud2Active = false;
    bool cloud3Active = false;
    float cloud1Speed = 0.0f;
    float cloud2Speed = 0.0f;
    float cloud3Speed = 0.0f;
    window.clear();
    window.draw(spriteBackground);
    window.draw(spriteTree);
    window.draw(spriteBee);
    window.draw(spriteCloud1);
    window.draw(spriteCloud2);
    window.draw(spriteCloud3);
    window.display();//Update screen with new background
    while (window.isOpen())//Stay looping until ESC
    { if (Keyboard::isKeyPressed(Keyboard::Escape))
          { window.close(); }//Hit ESC key to exit
    }
    window.clear();
    window.display();
    return 0;
}

you can see I see the coordinates of the sprite of tree at (810,0) in line 18. and sprite of bee is (400, 510) in line 24. Why the sprites display so off on the screen? Got to have a very simple explanation why I am off that I missed.

Thanks
 
Technology news on Phys.org
  • #2
I found out what really is wrong. I still don't know how to fix it. This is the image from the book running the program ( I change the position of the bee, everything else is the same position). The book is cheap to only have black and white picture!
Full screen 1.jpg

This looks correct. The problem is my screen ONLY display part of the picture as shown with the red outline. You can see the part below the tree is not shown in my program with my computer.

It must be some sort of setup problem that when the program is set to full screen, it ZOOM in too much and cut out the higher number of x and y. That's why my image is off. Must be the setting in line 7 in the program in post 1 is off.

Any suggestion?

thanksEDIT: I checked my laptop, it can only set to 1366 X 768 resolution. Maybe that's the problem.
 
Last edited:
  • #3
yungman said:
EDIT: I checked my laptop, it can only set to 1366 X 768 resolution. Maybe that's the problem.
Ya think?
 
  • Like
Likes Vanadium 50
  • #4
Mark44 said:
Ya think?
I have no idea how to fix this. I tried changing line 6 VideoMode vm(1920, 1080) to match, it did not work. I tried other numbers also, it doesn't seem to make any difference.

I tried going to computer->setting-> display and play with these, it didn't work either
Display setting.jpg


Anything I can try?

The book doesn't not talk about this at all.

Thanks
 
  • #5
A book on SFML is unlikely to contain instructions on how to operate your laptop.
If your laptop screen has a maximum resolution of 1366x768 you will not be able to set a mode of 1920x1080.
 
  • #6
Vanadium 50 said:
A book on SFML is unlikely to contain instructions on how to operate your laptop.
If your laptop screen has a maximum resolution of 1366x768 you will not be able to set a mode of 1920x1080.
Thanks

I tried everything on my laptop. Nothing work.

I understand this is a programming forum, not SFML graphics forum. I just join the SFML forum and trying to ask the same question in the SFML forum, hopefully I can get it resolved.

Thanks
 
  • #7
yungman said:
I tried everything on my laptop. Nothing work.

That's because if your laptop screen has a maximum resolution of 1366x768 you will not be able to set a mode of 1920x1080.
 
  • #8
Vanadium 50 said:
That's because if your laptop screen has a maximum resolution of 1366x768 you will not be able to set a mode of 1920x1080.
I tried setting the program resolution to 1366 X 768 in line 6 of the program in post 1. That was the first thing I tried. It did not work. I think the image is programmed to be 1920X1080. So the image is bigger.

I have a sinking feeling I need to buy a new laptop. I am going to confirm with the staff in SFML. When I bought this laptop, I did not think about video games. I hate video games, I want to learn this just for the graphics.

Thanks
 
  • #9
I am pretty sure it's my laptop. I put the bee in (683, 384) which is half way of (1366, 768). I have the stupid bee at DEAD CENTER!

So this is the end of the road unless I can change the image of the .png file to conform to 1366 X 768. is there any way to do that? I am born cheap, I don't want to buy a new laptop, this is only 7 months old! This was not a cheap computer by any stretch, I got a lot of good stuffs on this, just not graphics. I think I paid like $700 for this. It's like 512G SSD drive and 8G rams and latest gen I-5 at the time.
 
  • #10
It's in PNG (portable network graphics) format, so you can edit the image in, say, Paint, to change its size, to get the width and height down to 1366 pixels by 768 pixels.
 
  • #11
I think I got the answer from SFML, I have to read about sf::view, view is a class in SFML and view has member function for setting center and all sort of stuffs. This is the link I am reading now. Maybe you guys might find interesting:
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1View.php

My question to you guys is this. I am really trying to learn graphics, I am not exactly interested in gaming.

1) Can I use SFML for graphs, simulation and other stuffs other than games?
2) Is there any free more scientific graphics like SFML I can download?

thanks
 
  • #12
Mark44 said:
It's in PNG (portable network graphics) format, so you can edit the image in, say, Paint, to change its size, to get the width and height down to 1366 pixels by 768 pixels.
Yes, I did try that, I can just size the background easily, but I have to size down each of the sprite to fit in the picture. eg. if the tree is 200X600, I have to multiply the number by (1366/1920) to get 142 X 427 to scale down the size of the tree. Then I have to know the new coordinates in 1366 X 768 on the screen.

But I want to follow what suggested to me and read the class view of SFML first to see whether I can do it easier.

Thanks
 
  • #13
You are making life very difficult for yourself by trying to write software on such a small screen, no wonder you print everything out. It is simply not feasible to use development tools like Visual Studio effectively at less than 1920x1080.

Assuming your lappy has an HDMI connection, spend <$200 (you are in the US, right?) on a decent 24" or peferably 27-28" monitor.
 
  • Like
Likes Vanadium 50
  • #14
pbuk said:
spend <$200 (you are in the US, right?) on a decent 24" or peferably 27-28" monitor.

And < $12 for a USB keyboard and mouse.
 
  • Like
Likes pbuk
  • #15
Ha ha, you guys are spoiled. Talking about critical, I design complicated multi layers pcb on a 12" to 13" laptop for years. This is the first time I have a 15" laptop. I have big monitor like 22" or something, it's sitting in the closet. I saw mechanical engineer doing their design on laptops, I have not seen desktop and monitors for a long time at work.
 
  • #16
Using the right tool for the job is not 'being spoiled'.
 
  • Like
Likes Vanadium 50
  • #17
If you have a 1080p monitor in the closet and used it, as @pbuk suggested, you wouldn't have had this problem to begin with.
 
  • #18
pbuk said:
Using the right tool for the job is not 'being spoiled'.
We got the job done all these years. We are talking about really critical graphics in pcb design at work, so much more critical than games. Everyone use laptop. Nobody complain.
 
  • #19
Vanadium 50 said:
If you have a 1080p monitor in the closet and used it, as @pbuk suggested, you wouldn't have had this problem to begin with.
You mean the resolution follows the monitor, not the laptop? I did not know that.
 
  • #20
yungman said:
You mean the resolution follows the monitor, not the laptop?

The maximum resolution is the smaller of what the laptop can do and what the monitor can do. Most laptops today can put out better than 1920x1080.
 
  • #21
Well it's now 2021 and anyone who spends $100s on books and ink cartridges instead of plugging in a monitor he already owns because of some misplaced nostalgia is wasting his money and everyone's time.
 
  • Like
Likes Vanadium 50
  • #22
pbuk said:
Using the right tool for the job is not 'being spoiled'.
Indeed! More screen real estate = more productive software development. Before Covid, when I used to work at an office (remember those?), I used three displays including my MacBook Pro screen, which itself has 3072x1920 resolution. I could easily have made good use of four or even five displays if my desk had room for them.

I'm starting to understand why @yungman insists on using such a compressed coding style.
 
  • #23
yungman said:
Ha ha, you guys are spoiled. Talking about critical, I design complicated multi layers pcb on a 12" to 13" laptop for years. This is the first time I have a 15" laptop. I have big monitor like 22" or something, it's sitting in the closet. I saw mechanical engineer doing their design on laptops, I have not seen desktop and monitors for a long time at work.
Sounds like a cheap-*ss employer which fails to recognize that lost productivity is far more expensive than better equipment. Avoid such employers like the plague that they are.
 
  • #24
We are very proud of what we accomplished with the little we had. We are in very high tech, literally a mini version of Stanford Linear Accelerator type type of technology , we beat out competitors. We did a lot with less. Till today, I have better electronic test equipment than they have in my home. When I was contracting with them a few years ago, I did all my testing at home, I didn't even have to go there. I still have a whole lab in one of my room. It's the brain, not the tools.
 
  • #25
pbuk said:
ou are making life very difficult for yourself by trying to write software on such a small screen, no wonder you print everything out.
This...
And no wonder that you ask questions that were already answered several posts back in a thread.
yungman said:
Ha ha, you guys are spoiled. Talking about critical, I design complicated multi layers pcb on a 12" to 13" laptop for years. This is the first time I have a 15" laptop. I have big monitor like 22" or something, it's sitting in the closet.
Spoiled? I don't think so. Did you ever see a carpenter whose only tool was an axe? He probably wasn't making pianos, or for that matter, any structure more elegant than an outhouse.

You've obviously never seen the setups that software developers use -- typically three, four, or more flat screen monitors of about 27" each.

If I were you I would recycle the 22" monitor, and plunk down the money to get a 27" or larger flatscreen (~$175).

I'm constantly amazed that you 1) ask a question about how to do something, and then 2) ignore that well-thought-out advice for some silly reason or other.
 
Last edited:
  • Like
Likes pbuk

1. What is SFML?

SFML (Simple and Fast Multimedia Library) is a cross-platform software library designed for developing multimedia applications such as games.

2. How do I map a screen in SFML?

To map a screen in SFML, you will need to use the sf::View class. You can create a view with a specific size and position, and then use the sf::RenderWindow::setView() function to set the view for your window.

3. What is screen mapping?

Screen mapping is the process of defining how objects in a 3D world are projected onto a 2D screen. It involves setting up a virtual camera and transforming 3D coordinates into 2D coordinates that can be displayed on the screen.

4. How can I learn more about SFML?

You can learn more about SFML by visiting the official website or checking out online tutorials and resources. Additionally, you can join online communities and forums dedicated to SFML to connect with other users and ask for help.

5. Is SFML suitable for professional game development?

Yes, SFML is suitable for professional game development as it offers a wide range of features and is used by many professional game developers. However, it is always recommended to explore and compare different game development libraries before choosing the one that best fits your needs.

Back
Top