3
« 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?
#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;
}