Hallo Forum!
Ich beschäftige mich gerade etwas mit der SFML 2.0, doch will die nicht ganz so, wie ich will.
Ich lasse ein Quadrat zeichnen, welches sich über die Pfeiltasten steuern lässt. Funktioniert auch,
nur stottert die Bewegung zwischendurch extrem. Das Quadrat scheint teilweise mehrere 100 Pixel
zu überspringen. Außerdem bleibt es bei einem schnellen Richtungswechsel eine kurze Zeit unbewegt.
Habe ich irgendwas nicht beachtet?
Folgender Code (zur Erklärung: ich versuche dabei,
eine feste Anzahl Logikframes unabhängig von den visuellen Frames zu bekommen):
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
using std::string;
using std::stringstream;
const int LOGIC_FPS = 100;
const int TIME_BETWEEN_UPDATES = 1000 / LOGIC_FPS;
const int BOX_MOVEMENT = 500 / LOGIC_FPS; // 500 Pixel per Sekunde
string FloatToStr( float f )
{
stringstream ss;
ss << f;
return ss.str();
}
int main()
{
sf::RenderWindow window( sf::VideoMode( 800, 600 ), "SFML-Window" );
sf::Event event;
const sf::Input& input = window.GetInput();
sf::Image image;
image.Create( 50, 50 , sf::Color( 0, 0xAA, 0xFF ) );
sf::Sprite box( image );
box.SetPosition( 375, 275 );
sf::Text boxCoords;
sf::Clock clock;
unsigned int updateTime = clock.GetElapsedTime();
while( window.IsOpened() )
{
while( clock.GetElapsedTime() > updateTime )
{
while( window.PollEvent( event ) )
{
if( event.Type == sf::Event::Closed )
window.Close();
}
if( input.IsKeyDown( sf::Key::Left ) )
box.Move( -BOX_MOVEMENT, 0 );
if( input.IsKeyDown( sf::Key::Right ) )
box.Move( BOX_MOVEMENT , 0 );
if( input.IsKeyDown( sf::Key::Up ) )
box.Move( 0, -BOX_MOVEMENT );
if( input.IsKeyDown( sf::Key::Down ) )
box.Move( 0, BOX_MOVEMENT );
boxCoords.SetString( string( "box.x = " ) + FloatToStr( box.GetPosition().x ) +
string( " box.y = " ) + FloatToStr( box.GetPosition().y ) );
updateTime += TIME_BETWEEN_UPDATES;
}
window.Clear();
window.Draw( box );
window.Draw( boxCoords );
window.Display();
}
}
|
OS ist Ubuntu 10.04, gcc 4.4.3, falls das relevant ist.