<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[SDL Programm zu langsam]]></title><description><![CDATA[<p>Mein SDL Programm läuft recht langsam. Was kann ich tun, damit es besser und optimaler funktioniert?<br />
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.<br />
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.<br />
Gibt es eigentlich noch eine weitere möglichkeit, in der man die Mausposition direkt abfragen kann?</p>
<p>main.cpp:</p>
<pre><code class="language-cpp">#include &lt;stdlib.h&gt;
#include &lt;iostream&gt;
#include &quot;SDL.h&quot;
#include &quot;SDL_image.h&quot;
#include &quot;animation.h&quot;

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) &lt; 0 )
	{
		fprintf(stderr, &quot;SDL konnte nicht initialisiert werden:  %s\n&quot;, SDL_GetError());
		exit(1);
	}

	atexit(SDL_Quit);

	display = SDL_SetVideoMode( 800, 600, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if ( display == NULL )
	{
		fprintf(stderr, &quot;Konnte kein Fenster: Auflösung 800x600 oeffnen werden: %s\n&quot;, SDL_GetError());
		exit(-1);
	}

  // Bild laden
	image = IMG_Load(&quot;kritzel.jpg&quot;); //400x100 pixel
	if (image == NULL)
	{
		fprintf(stderr, &quot;Das Bild konnte nicht geladen werden: %s\n&quot;, SDL_GetError());
		exit(-1);
	}

  // Hintergrundbild laden
	hintergrund = IMG_Load(&quot;dawn.jpg&quot;);  //800*600 pixel
	if (hintergrund == NULL)
	{
		fprintf(stderr, &quot;Das Bild konnte nicht geladen werden: %s\n&quot;, 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( &amp;event ) )
		{
			switch( event.type )
			{
				case SDL_MOUSEMOTION:
					//printf( &quot;Mausposition: x: %i und y: %i\n&quot;, 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);
}
</code></pre>
<p>animation.h:</p>
<pre><code class="language-cpp">//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 &quot;SDL.h&quot;
#include &quot;SDL_image.h&quot;

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 -&gt; image   = image;
	this -&gt; display = display;
	this -&gt; animationsstufen = animationsstufen;

	this -&gt; x 	   = x;  //x muss lokal gespeichert werden, denn es muss die ursrüngliche x koordimate enthalten.
	this -&gt; 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-&gt;x + animationsstufe * breite;

	animationsstufe++;
	if (animationsstufe &gt;= animationsstufen)
		animationsstufe = 0;

	drect.x = x;
	drect.y = y;

	SDL_BlitSurface(image, &amp;srect, display, &amp;drect);
	SDL_UpdateRects(display,1,&amp;drect);
}
</code></pre>
<pre><code>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 $&lt;

clean:
	rm -rf $(OBJ) $(APP)

main.o: main.cpp animation.h
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/163375/sdl-programm-zu-langsam</link><generator>RSS for Node</generator><lastBuildDate>Sun, 13 Sep 2026 01:10:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/163375.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 29 Oct 2006 00:28:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to SDL Programm zu langsam on Sun, 29 Oct 2006 00:39:22 GMT]]></title><description><![CDATA[<p>Mein SDL Programm läuft recht langsam. Was kann ich tun, damit es besser und optimaler funktioniert?<br />
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.<br />
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.<br />
Gibt es eigentlich noch eine weitere möglichkeit, in der man die Mausposition direkt abfragen kann?</p>
<p>main.cpp:</p>
<pre><code class="language-cpp">#include &lt;stdlib.h&gt;
#include &lt;iostream&gt;
#include &quot;SDL.h&quot;
#include &quot;SDL_image.h&quot;
#include &quot;animation.h&quot;

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) &lt; 0 )
	{
		fprintf(stderr, &quot;SDL konnte nicht initialisiert werden:  %s\n&quot;, SDL_GetError());
		exit(1);
	}

	atexit(SDL_Quit);

	display = SDL_SetVideoMode( 800, 600, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if ( display == NULL )
	{
		fprintf(stderr, &quot;Konnte kein Fenster: Auflösung 800x600 oeffnen werden: %s\n&quot;, SDL_GetError());
		exit(-1);
	}

  // Bild laden
	image = IMG_Load(&quot;kritzel.jpg&quot;); //400x100 pixel
	if (image == NULL)
	{
		fprintf(stderr, &quot;Das Bild konnte nicht geladen werden: %s\n&quot;, SDL_GetError());
		exit(-1);
	}

  // Hintergrundbild laden
	hintergrund = IMG_Load(&quot;dawn.jpg&quot;);  //800*600 pixel
	if (hintergrund == NULL)
	{
		fprintf(stderr, &quot;Das Bild konnte nicht geladen werden: %s\n&quot;, 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( &amp;event ) )
		{
			switch( event.type )
			{
				case SDL_MOUSEMOTION:
					//printf( &quot;Mausposition: x: %i und y: %i\n&quot;, 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);
}
</code></pre>
<p>animation.h:</p>
<pre><code class="language-cpp">//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 &quot;SDL.h&quot;
#include &quot;SDL_image.h&quot;

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 -&gt; image   = image;
	this -&gt; display = display;
	this -&gt; animationsstufen = animationsstufen;

	this -&gt; x 	   = x;  //x muss lokal gespeichert werden, denn es muss die ursrüngliche x koordimate enthalten.
	this -&gt; 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-&gt;x + animationsstufe * breite;

	animationsstufe++;
	if (animationsstufe &gt;= animationsstufen)
		animationsstufe = 0;

	drect.x = x;
	drect.y = y;

	SDL_BlitSurface(image, &amp;srect, display, &amp;drect);
	SDL_UpdateRects(display,1,&amp;drect);
}
</code></pre>
<pre><code>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 $&lt;

clean:
	rm -rf $(OBJ) $(APP)

main.o: main.cpp animation.h
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1163492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1163492</guid><dc:creator><![CDATA[Krux]]></dc:creator><pubDate>Sun, 29 Oct 2006 00:39:22 GMT</pubDate></item><item><title><![CDATA[Reply to SDL Programm zu langsam on Sun, 29 Oct 2006 05:12:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">while( quit == 0)
    {
        while( SDL_PollEvent( &amp;event ) )
        {
            switch( event.type )
            {
                case SDL_MOUSEMOTION:
                    //printf( &quot;Mausposition: x: %i und y: %i\n&quot;, 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;
            }
        }
    }
</code></pre>
<p>-&gt;</p>
<pre><code class="language-cpp">int mouseX = 0;
    int mouseY = 0;
    while( quit == 0)
    {
        while( SDL_PollEvent( &amp;event ) )
        {
            switch( event.type )
            {
                case SDL_MOUSEMOTION:
                    //printf( &quot;Mausposition: x: %i und y: %i\n&quot;, 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);
    }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1163510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1163510</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 29 Oct 2006 05:12:41 GMT</pubDate></item></channel></rss>