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

Author Topic: Trouble understanding SFML  (Read 4987 times)

0 Members and 1 Guest are viewing this topic.

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
Trouble understanding SFML
« on: December 29, 2009, 01:00:44 am »
Since I'm one of the people switching from Allegro to SFML I'm just having a bit of trouble understanding how to correctly use the API (my brain is stuck to think in the way that works for Allegro).  My main problem right now is not knowing where to correctly use sf::Event, sf:: Input, and sf::RenderWindow.  Like if I have a separate function for my games main menu, another function for the options screen, and another function for the actual game itself do I need to create a whole new set of Event, Input, and RenderWindow classes for each function?  Example:

Code: [Select]
void main_menu_loop()
{
// New RenderWindow?
// New Input?
// New Event?

/*
The main menu loop.
*/
}

 int main()
{
RenderWindow Screen(VideoMode(800, 600, 32), "DUEL", Style::Close);
const Input & iInput = Screen.GetInput();
Event eEvent;

main_menu_loop();
while(Screen.IsOpened())
{
//Main game loop
}
return 0;
}


And another thing...  is there a way to quickly output numbers to the screen?  sf::String doesn't work with integers (atleast it won't for me).

-Thanks in advance.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Trouble understanding SFML
« Reply #1 on: December 29, 2009, 01:37:16 am »
Take a look at the tutorials section. It is explained very well.

As for sf::String, it works fine with numbers. Maybe you want to use std::stringstream to number-to-string conversion.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble understanding SFML
« Reply #2 on: December 29, 2009, 10:11:17 am »
- RenderWindow is the main window of your application, so you can't create new instances in every function, you must always use the same.
- You never create Input objects, you always retrieve the RenderWindow's input, so no problem here.
- You should only have one event loop, so one Event instance is enough.

Number to string conversion is a basic C++ feature that can be achieved with the help of the standard library, SFML doesn't need to handle that directly.

And yes, you should read the tutorials ;)
Laurent Gomila - SFML developer

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
Trouble understanding SFML
« Reply #3 on: December 30, 2009, 11:11:42 pm »
Thanks for the reply I'll go over the tutorials more thoroughly and see what I missed.  And with the number thing I know you can convert it with the standard library I just thought maybe sfml could do it automatically like Allegro (like I said my mind is stuck on Allegro).

Thanks though.  I'll post back here if I have any more questions.

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
Trouble understanding SFML
« Reply #4 on: December 31, 2009, 08:42:58 am »
Ok I've read through the tutorials and they aren't telling me much (unless I missed something :/)  but heres my problem...  Whenever I try to declare anything outside of main my program will crash when I close it.

For example:

Code: [Select]


// Header files
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

using namespace sf;

int main()
{
RenderWindow Screen(VideoMode(800, 600, 32), "DUEL", Style::Close);
Event eEvent;

Font Kosh;
Font Half;
Image imgMenu;

//Load fonts.
Kosh.LoadFromFile("Materials/Fonts/kosh.ttf", 30);
Half.LoadFromFile("Materials/Fonts/half.ttf", 30);

// Load images.
imgMenu.LoadFromFile("Materials/Graphics/menu_background.tga");

while(Screen.IsOpened())
{
Screen.GetEvent(eEvent);
if(eEvent.Type == Event::Closed)Screen.Close();
if(Screen.GetInput().IsKeyDown(Key::Escape))Screen.Close();

Screen.Clear();
Screen.Display();
}
return 0;
}


If I want to declare anything outside of the main loop my program has a runtime error when I close it...  Not sure what the problem is here.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble understanding SFML
« Reply #5 on: December 31, 2009, 11:36:12 am »
Can you show the actual code that crashes? ;)
Laurent Gomila - SFML developer

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
Trouble understanding SFML
« Reply #6 on: December 31, 2009, 09:42:49 pm »
Lol I don't know what I was trying to show in that last post... sorry.  I was tired when I wrote that.

Code: [Select]


Font Kosh;

int main()
{
// All of the main code
}


If I declare the font, or any other SFML class, outside of main then I get the runtime error when I end the program and it just shows me the disassembly (doesn't lead me to anywhere in the code).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble understanding SFML
« Reply #7 on: January 01, 2010, 02:21:52 am »
You shouldn't declare this kind of classes outside main, as their destruction will happen after the last line of main, probably after SFML has cleaned up its managers and internal stuff.
Laurent Gomila - SFML developer

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
Trouble understanding SFML
« Reply #8 on: January 01, 2010, 03:18:39 am »
So you're saying I can't do something like this?

Code: [Select]

class clsMenuText
{
public:
menu(std::string sText, Font& fFont)
{
SText.SetFont(fFont);
SText.SetColor(Color(255, 255, 255));
SText.SetText(sText);
}
~menu()
{
}
String SText;
};


If I wanted to make a class that holds SFML classes how would I go about doing it?  I even tried deconstructing SText before I close the window but it's just no good.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble understanding SFML
« Reply #9 on: January 01, 2010, 12:58:23 pm »
What's the relation between this clsMenuText class and global objects?
Laurent Gomila - SFML developer

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
Trouble understanding SFML
« Reply #10 on: January 01, 2010, 10:00:03 pm »
clsMenuText is global... If I create a clsMenuText object in main it will still give runtime errors when I exit the program.

Code: [Select]

int main()
{
////////////////////////
//Declare everything
////////////////////////

clsMenuText MenuText1("Play", Kosh);
};


Something like that would crash because of the String SText in clsMenuText.  If I remove String SText (and the constructor using SText) then the program quits without problems....

Are you saying I need to have the classes inside main as well?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble understanding SFML
« Reply #11 on: January 01, 2010, 10:21:43 pm »
Your crash may be related to the known bug with sf::String. You can search this forum to find more information about it ;)
Laurent Gomila - SFML developer

inlinevoid

  • Newbie
  • *
  • Posts: 49
    • MSN Messenger - inlinevoidmain@gmail.com
    • AOL Instant Messenger - inlinevoid
    • View Profile
    • Email
Trouble understanding SFML
« Reply #12 on: January 01, 2010, 10:49:43 pm »
Gah I didn't even think to try Sprite or Image.  They both work fine so yea it might just be string...  I'll do some research.

If I get stuck again I'll post back here.  Thanks for the help Laurent.

 

anything