SDL Programm zu langsam
-
Mein SDL Programm läuft recht langsam. Was kann ich tun, damit es besser und optimaler funktioniert?
Es funktioniert fehlerfrei, nur braucht das Programm viel zu lange, bis alle Events abgehandelt sind. Das geht ziemlich schnell, bis die Animation mehrere Sekunden hinter meiner Maus hinterherhinkt.
Dieser Quellcode ist aus einem Tutorial, wo ich noch dabei bin es Objektorientiert zu gestalten. Seit ich den Boublebuffer eingebaut habe läuft das Programm langsam.
Gibt es eigentlich noch eine weitere möglichkeit, in der man die Mausposition direkt abfragen kann?main.cpp:
#include <stdlib.h> #include <iostream> #include "SDL.h" #include "SDL_image.h" #include "animation.h" using namespace std; int main() { // Die zwei bereits bekannten Surfaces deklarieren SDL_Surface *display; SDL_Surface *image; SDL_Surface *hintergrund; SDL_Event event; int quit = 0; SDL_Rect drect; SDL_Rect srect; if ( SDL_Init( SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "SDL konnte nicht initialisiert werden: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); display = SDL_SetVideoMode( 800, 600, 16, SDL_HWSURFACE | SDL_DOUBLEBUF); if ( display == NULL ) { fprintf(stderr, "Konnte kein Fenster: Auflösung 800x600 oeffnen werden: %s\n", SDL_GetError()); exit(-1); } // Bild laden image = IMG_Load("kritzel.jpg"); //400x100 pixel if (image == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden: %s\n", SDL_GetError()); exit(-1); } // Hintergrundbild laden hintergrund = IMG_Load("dawn.jpg"); //800*600 pixel if (hintergrund == NULL) { fprintf(stderr, "Das Bild konnte nicht geladen werden: %s\n", SDL_GetError()); exit(-1); } Animation kritzel(image, 0,0,100,100,4,display); // Mauszeiger verstecken SDL_ShowCursor(0); // Event-Handling while( quit == 0) { while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_MOUSEMOTION: //printf( "Mausposition: x: %i und y: %i\n", event.motion.x, event.motion.y); SDL_BlitSurface( hintergrund, 0, display, 0 ); kritzel.draw(event.motion.x, event.motion.y); SDL_Flip(display); break; case SDL_MOUSEBUTTONDOWN: // Quit beim druecken des Mousbuttons quit = 1; break; default: break; } } } // Das Bitmap-Surface löschen SDL_FreeSurface(image); exit(0); }animation.h:
//Soll eine Klasse sein, die man nur einmal erstellen muss, und dann kann die Animation mit bloser Eingabe der Koordinaten dargestellt werden. //Der nächste Frame wird automatisch gewählt (ändere ich aber glaub ich noch) #include "SDL.h" #include "SDL_image.h" class Animation{ private: SDL_Surface *image; SDL_Surface *display; SDL_Rect drect; SDL_Rect srect; int animationsstufen; int animationsstufe; int x, breite; public: Animation(SDL_Surface *image, int x, int y, int breite, int hoehe, int animationsstufen, SDL_Surface *display ); void draw(int x, int y); }; Animation::Animation(SDL_Surface *image, int x, int y, int breite, int hoehe, int animationsstufen, SDL_Surface *display ){ this -> image = image; this -> display = display; this -> animationsstufen = animationsstufen; this -> x = x; //x muss lokal gespeichert werden, denn es muss die ursrüngliche x koordimate enthalten. this -> breite = breite; srect.y = y; srect.w = breite; srect.h = hoehe; drect.w = breite; drect.h = hoehe; animationsstufe=0; } void Animation::draw(int x, int y){ srect.x = this->x + animationsstufe * breite; animationsstufe++; if (animationsstufe >= animationsstufen) animationsstufe = 0; drect.x = x; drect.y = y; SDL_BlitSurface(image, &srect, display, &drect); SDL_UpdateRects(display,1,&drect); }CC = g++ OBJ = main.o APP = SDL1 CFLAGS = $(shell sdl-config --cflags) # evtl auch noch -O2 für Optimierungen, oder -Wall für Warnungen LDFLAGS = -lm $(shell sdl-config --libs) -lSDL_image all: $(OBJ) $(CC) $(LDFLAGS) -o $(APP) $(OBJ) .cpp.o: $(CC) $(CFLAGS) -c $< clean: rm -rf $(OBJ) $(APP) main.o: main.cpp animation.h
-
while( quit == 0) { while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_MOUSEMOTION: //printf( "Mausposition: x: %i und y: %i\n", event.motion.x, event.motion.y); SDL_BlitSurface( hintergrund, 0, display, 0 ); kritzel.draw(event.motion.x, event.motion.y); SDL_Flip(display); break; case SDL_MOUSEBUTTONDOWN: // Quit beim druecken des Mousbuttons quit = 1; break; default: break; } } }->
int mouseX = 0; int mouseY = 0; while( quit == 0) { while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_MOUSEMOTION: //printf( "Mausposition: x: %i und y: %i\n", event.motion.x, event.motion.y); mouseX = event.motion.x; mouseY = event.motion.y; break; case SDL_MOUSEBUTTONDOWN: // Quit beim druecken des Mousbuttons quit = 1; break; default: break; } } SDL_BlitSurface( hintergrund, 0, display, 0 ); kritzel.draw(mouseX, mouseY); SDL_Flip(display); }