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

Author Topic: Simple SFML program, doesn't display or terminate correctly  (Read 2479 times)

0 Members and 3 Guests are viewing this topic.

Mathhead200

  • Newbie
  • *
  • Posts: 3
    • MSN Messenger - mathhead200@aol.com
    • View Profile
    • http://www.mathhead200.com
Simple SFML program, doesn't display or terminate correctly
« on: April 06, 2011, 10:43:44 pm »
The code compiles using cl.exe (VC++) fine, and run correctly if I omit lines 22-26 (from Sprite(me) to Draw(you)); however, when I run the program, the sprites don't display, and when I close the window I see the 'Goodbye!' message but then the program doesn't close correctly (i.e. 'windows is checking for a solution to your problem...')

This is the first SFML program I've written using sprites, can someone explain where (or what) the problem is?

Code: [Select]

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

using namespace std;
using namespace sf;

int main()
{
RenderWindow window( VideoMode(400, 300), "Hello SFML", Style::Titlebar | Style::Resize | Style::Close );
Event e;

//initialize two sprites
const int WIDTH = 32, HEIGHT = 32;
Image myImg( WIDTH, HEIGHT, Color(0, 0, 0, 0) ),
yourImg( WIDTH, HEIGHT, Color(0, 0, 0, 0) );
for( int i = 0; i < WIDTH && i < HEIGHT; i++ ) {
myImg.SetPixel( i, i, Color(200, 0, 0) );
yourImg.SetPixel( WIDTH - 1 - i, i, Color(0, 200, 200) );
}
Sprite me(myImg), you(yourImg);
me.SetPosition(0.f, 0.f);
you.SetPosition( (float)WIDTH, (float)HEIGHT );
window.Draw(me);
window.Draw(you);

//display the window
do {
window.Clear( Color(255, 255, 255) );
window.Display();
while( window.GetEvent(e) ) {
switch(e.Type) {
case Event::Closed:
window.Close();
break;
case Event::KeyPressed: {
Key::Code k = e.Key.Code;
if( k == Key::Return )
cout << endl;
else if( k == Key::F1 ) {
window.Capture().SaveToFile("screenshot.png");
cout << "Screenshot, saved to file: screenshot.png" << endl;
} else if( ('a' <= k && k <= 'z') || ('A' <= k && k <= 'Z') || ('0' <= k && k <= '9') )
cout << (char)e.Key.Code;
break;
} case Event::MouseButtonPressed:
if( e.MouseButton.Button == Mouse::Left )
cout << "Framerate = " << 1.0 / window.GetFrameTime() << endl;
break;
}
}
Sleep(0.25f);
} while( window.IsOpened() );

cout << "\nGoodbye!\n";
return 0;
}


Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Simple SFML program, doesn't display or terminate correctly
« Reply #1 on: April 07, 2011, 01:48:26 am »
Looks good, however your ordering is wrong. You do:

- 2x Draw
- Clear screen
- Display screen

See the problem? ;) Just put the Draw() calls between Clear() and Display(). You need to draw everything every frame, not just once.

Mathhead200

  • Newbie
  • *
  • Posts: 3
    • MSN Messenger - mathhead200@aol.com
    • View Profile
    • http://www.mathhead200.com
Simple SFML program, doesn't display or terminate correctly
« Reply #2 on: April 07, 2011, 06:29:24 am »
Okay, it displays correctly now. I was under the impression that the Window::Draw method would add the sprites to some sort of rsc list in the window, and then Window::Display would show them.
Thanks for clearing that up.

I'm still having the termination problem. Do you think it has anything to do with the ~Sprite deconstructor?

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Simple SFML program, doesn't display or terminate correctly
« Reply #3 on: April 07, 2011, 12:23:02 pm »
Quote from: "Mathhead200"
Okay, it displays correctly now. I was under the impression that the Window::Draw method would add the sprites to some sort of rsc list in the window, and then Window::Display would show them.
Thanks for clearing that up.

I'm still having the termination problem. Do you think it has anything to do with the ~Sprite deconstructor?

Sounds like the ATI issue from SFML 1.6, switching to 2 will fix it.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Simple SFML program, doesn't display or terminate correctly
« Reply #4 on: April 07, 2011, 12:34:31 pm »
The reason why SFML is not remembering what you're drawing is that it's not a game engine. ;)

Which SFML version are you using? If it's the ATI bug, it's fixed in SFML 2 and worth a try.

Mathhead200

  • Newbie
  • *
  • Posts: 3
    • MSN Messenger - mathhead200@aol.com
    • View Profile
    • http://www.mathhead200.com
Simple SFML program, doesn't display or terminate correctly
« Reply #5 on: April 07, 2011, 07:19:40 pm »
Where can I download SFML verson 2.0?
From the looks of the download page it's still a beta verson.
Can someone give me a link?

Are there any other possible explanations, in my code alone? (i.e. Maybe it's not a bug.)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Simple SFML program, doesn't display or terminate correctly
« Reply #6 on: April 07, 2011, 07:29:42 pm »
SFML 2 is a beta version (well, actually not even a beta) ;)
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Simple SFML program, doesn't display or terminate correctly
« Reply #7 on: April 08, 2011, 12:54:36 am »
But already very stable. ;)