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

Author Topic: Mouse coordinates verses game area verses screen size  (Read 6349 times)

0 Members and 1 Guest are viewing this topic.

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Mouse coordinates verses game area verses screen size
« on: June 04, 2012, 07:47:46 am »
I'm learning C++ and my first program will be a craps game.  I'm trying to decide whether to use Allegro, SDL or SFML but it seems that SFML is the latest and greatest from what I've read.  I've watched a lot of SFML tutorial videos on youtube and googled but none cover my questions.

In my craps game, I want to create half of a craps table in Photoshop and save it as a .jpg or .png file.  Assume the size is 1024 x 768 pixels.  Using SFML, I want to display the craps table.  Now I want to click anywhere on the passline and a one unit bet in chips for player1 would appear in the player1 area on the passline.

Now, never having programmed in a graphics environment before, I assume that to know that the mouse is positioned anywhere over the passline rectangle, I will have to know the x & y coordinates of the passline's top-left and bottom-right corners.  Then I would have to verify that the mouse coordinate is
> topLeftX and
> topLeftY and
< bottomRightX and
< bottomRightY coordinates. 
Is that right?  Is there a simpler way?

Now assuming that all of that works, what happens if the user wants a larger display of the crap table to fit his screen size?  When the display is made larger, how will the X and Y coordinates of the passline rectangle adjust in value according to the larger screen size?  Can SFML handle this situation automatically?

Thanks for helping a beginner,
Raptor (Who programmed in Basic, C and Assembler many years ago, but never in a graphics environment.)


Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Mouse coordinates verses game area verses screen size
« Reply #1 on: June 04, 2012, 10:05:47 am »
1. you can have screen mouse coordinates
sf::Mouse::getPosition()
 
2. you can have relative coordinates to sfml window
sf::Mouse::getPosition(sf::RenderWindow)
 
3. you can setup a view and convert relative coordinates to your game coords ( they will always be the same on any resolution)
m_window.setView(m_view);
m_window.convertCoords( m_position );
 

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #2 on: June 04, 2012, 11:15:00 am »
Hi Acrobat,

Thanks for pointing me in the right direction.  I'll research the functions you suggested to see how they work.  The preliminary glimpse appears to indicate that the functions are for SFML 1.6.  Since I'm starting from scratch, should I use 1.6 or 2.0?

Thanks,
Raptor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #3 on: June 04, 2012, 11:16:02 am »
These functions are for SFML 2, which you should definitely use if you're starting from scratch.
Laurent Gomila - SFML developer

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #4 on: June 04, 2012, 11:25:51 am »
These functions are for SFML 2, which you should definitely use if you're starting from scratch.
OK, SFML 2 it is.  BTW, is the method I described to determine if the mouse is hovering over the passline rectangle the best way to do it?  (ie: comparing the mouse coordinates to the topLeft and bottomRight coordinates of the passline rectangle.)

Or should I define a transparent sprite rectangle over the graphic image of the passline rectangle and check for collision instead?  (not sure if this is even possible but just something that comes to mind)

Thanks Laurent!
Raptor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #5 on: June 04, 2012, 11:41:33 am »
Quote
BTW, is the method I described to determine if the mouse is hovering over the passline rectangle the best way to do it?  (ie: comparing the mouse coordinates to the topLeft and bottomRight coordinates of the passline rectangle.)
Yes.
Laurent Gomila - SFML developer

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #6 on: June 04, 2012, 12:37:57 pm »
Quote
BTW, is the method I described to determine if the mouse is hovering over the passline rectangle the best way to do it?  (ie: comparing the mouse coordinates to the topLeft and bottomRight coordinates of the passline rectangle.)
Yes.
Thanks again  8)
Raptor

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #7 on: July 04, 2012, 05:56:14 am »
3. you can setup a view and convert relative coordinates to your game coords ( they will always be the same on any resolution)
m_window.setView(m_view);
m_window.convertCoords( m_position );
 
Can anyone post a link to some sample code for SFML2 that handles different screen sizes?

My object is to create the initial craps table in a 1024 x 768 window.  Rectangles within that display will define the PassLine, NoPassLine, Field, Come, Don'tCome, etc. areas.  When the player mouse clicks in the PassLine rectangle, I can figure out that's where he clicked by the top-left and bottom-right corner coordinates though I haven't actually written any code yet.

But if I display the craps table at a larger resolution but in the same aspect ratio, the coordinates of the PassLine rectange will change.

Being a beginner in C++ and SFML, it sure would be infinitely helpful to see some sample code of how to handle this situation.  It's not for lack of trying since I've spent many, many hours searching the web trying to figure out how to do this and for some sample SFML2 code to do this.

I could calculate the new coordinates of the PassLine rectangle myself by using ratios of the total display size, but I would like to use SFML2 provided solutions instead of reinventing the wheel.

Thanks for any help on this,
Raptor

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #8 on: July 04, 2012, 12:02:48 pm »
The cooridnates don't if you don't change them...
How do change the window resolution? If you're just resizing the window, then you have to update the view respectivly.
If you don't want to change the view since you like the stretched image you can use window.convertCoords() to map the mouse position to the view coordinates.
« Last Edit: July 04, 2012, 10:05:50 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #9 on: July 04, 2012, 10:00:36 pm »
The cooridnates don't if you don't change them...
How do change the wibdow resolution? If you're just resizing the window, then you have to update the view respectivly.
If you don't want to change the view sibce you like the streght image you can use window.convertCoords() to map the mouse position to the view coordinates.
eXploit3r,

Thanks for always helping out.
Regarding what you said, that's the type of sample code that I'm hoping to find.  If anyone can provide a link or two some sample SFML2 code, I'll be very grateful.

Thanks,
Raptor


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #10 on: July 04, 2012, 10:04:08 pm »
Do you mean something like this (includes minimum window size limit):

sf::Vector2f size = static_cast<sf::Vector2f>(mWindow.getSize());

// Minimum size
if(size.x < 800)
        size.x = 800;
if(size.y < 600)
        size.y = 600;

mWindow.setSize(static_cast<sf::Vector2u>(size));
mWindow.setView(sf::View(sf::FloatRect(0.f, 0.f, size.x, size.y)));

Or do you want the second version?
You can look at my tutorial on the sf::View which explains the convertCoords() function.
« Last Edit: July 04, 2012, 10:07:01 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: Mouse coordinates verses game area verses screen size
« Reply #11 on: July 05, 2012, 03:29:19 am »
Do you mean something like this (includes minimum window size limit):

sf::Vector2f size = static_cast<sf::Vector2f>(mWindow.getSize());

// Minimum size
if(size.x < 800)
        size.x = 800;
if(size.y < 600)
        size.y = 600;

mWindow.setSize(static_cast<sf::Vector2u>(size));
mWindow.setView(sf::View(sf::FloatRect(0.f, 0.f, size.x, size.y)));

Or do you want the second version?
You can look at my tutorial on the sf::View which explains the convertCoords() function.
Your "set minimum size" code will come in handy.  I never would have thought of doing that.

I visited your View tutorial and it looks very nice indeed.  I have a question.
So far, it seems to me that the View feature allows one to have a large map and to view a small portion of that map on the screen.  Then one can roam around the map and move the view in synch with the roaming.  Is that correct?  If so, then would using View be appropriate for my problem? 

My problems is that I will always display the entire craps table on the screen and never a portion of it.  My current thinking is to query the user's current screen size.  Then display the largest size crap table that will fit, from a library of different sized crap tables.  I saw a tutorial on youtube and read a comment elsewhere that resizing an image using SFML will make the image look strange or bad.  I'm not sure what they meant by that.  So to get the best quality background image of the craps table, I'm thinking of doing it the way I just mentioned.  Will using View and convertCoords be appropriate when displaying the entire background image rather than a small portion of it?

Either way, I'm going to study your View tutorial to learn about View.
Thanks much,
Raptor

 

anything