Zwei Grafiken Bild verschmiert

scaleo2405

Grünschnabel
Hallo,

Um kurz etwas zum Sachverhalt zu sagen. Ich habe vor ein Paar Wochen begonnen C/C++ Programmierung zu beginnen. Ich habe nun begonnen aus vielen Tutorials mein kleines erstes eigenes Spiel mit Hilfe der SDL zu schreiben. So nun habe ich folgendes Problem. Ich habe ein Hintergundbild und eine zweite Grafik die sich darauf bewegt. Jedoch wenn sich diese bewegt verschmiert sie die andere, es werden die Grafiken also nicht voneinander Getrennt. Ich weis das das eine Anfängerfrage ist aber bitte helft mir.
Ich brauche das ganze für meine Facharbeit anfang nächsten Schuljahres. Hier mal meine *.cpp


Code:
#include <stdlib.h>
#include <SDL/SDL.h>
#include "SDL/SDL_image.h"
#include <string>

int main(int argc, char *argv[])
{
    SDL_Surface *screen, *image, *backg;
    SDL_Rect dst, dstblue;
 

//startposition pinguin	
	
	
	SDL_Event event;
    Uint8 *keys;
    int tuxX = 370, tuxY = 430;
    int done = 0;
    if (SDL_Init(SDL_INIT_VIDEO) == -1) {
        printf("Can't init SDL:  %s\n", SDL_GetError());
        exit(1);
    }

//Screnn initialisieren
    atexit(SDL_Quit); 
    screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 255));
    if (screen == NULL) {
        printf("Can't set video mode: %s\n", SDL_GetError());
        exit(1);
    }
    
	




//hintergrundbild

backg = SDL_LoadBMP("back.bmp");

SDL_Rect bac;

bac.x = 0;
bac.y = 0;
bac.w = backg->w;
bac.h = backg->h;



SDL_BlitSurface(backg, NULL, screen, &bac);




	
	
	
	//Pinguin Laden
	
	image = IMG_Load("pinguin.png");
    if (image == NULL) {
        printf("Can't load image of tux: %s\n", SDL_GetError());
        exit(1);
    } 

// Pinguin bewegung  
    dst.w = backg->w;
    dst.h = backg->h;
    dstblue.w = backg->w + 1;
    dstblue.h = backg->h + 1;
    SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 0, 0, 0));    
    while (!done) {
        while (SDL_PollEvent(&event)) {
            switch(event.type) {
            case SDL_QUIT:
                done = 1;
                break;
            }
        }
       dstblue.x = tuxX;
       dstblue.y = tuxY;
       keys = SDL_GetKeyState(NULL);
       if (keys[SDLK_UP]) {
           if (tuxY > 10) {
               tuxY--;
           }
       }
       if (keys[SDLK_UP]) {
           if (tuxY < 610 - image->h) {
               tuxY++;
           }
       }
       if (keys[SDLK_RIGHT]) {
           if(tuxX < 790 - image->w) {
               tuxX++;
           }
       }
       if (keys[SDLK_LEFT]) {
           if (tuxX > 10) {
               tuxX--;
           }
       }
       dst.x = tuxX;
       dst.y = tuxY;
       //SDL_FillRect(screen, &dstblue, SDL_MapRGB(screen->format, 0, 0, 255));
       SDL_BlitSurface(image, NULL, screen, &dst);
       SDL_Flip(screen);
    }
    SDL_FreeSurface(image);
    return 0;
}
 
Für mich sieht das so aus, als müsstest du das Surface wieder in den Ausgangszustand zurücksetzen bevor du mit SDL_BlitSurface() das Bild raufmalst.

Ich tippe mal, dass das funktionieren könnte (habs aber nicht ausprobiert)

PHP:
.
.
.
       dst.x = tuxX;
       dst.y = tuxY;
       //SDL_FillRect(screen, &dstblue, SDL_MapRGB(screen->format, 0, 0, 255));

[edit]
       SDL_BlitSurface(backg, NULL, screen, &bac);
[/edit]

       SDL_BlitSurface(image, NULL, screen, &dst);
       SDL_Flip(screen);
    }
    SDL_FreeSurface(image);
    return 0;
}

Da ich mich mit SDL leider nicht wirklich auskenne, kann ich dir nicht sagen ob es da noch bessere Lösungen gibt.

MfG. Leever
 
Ich hab das nochmal angeschaut und etwas umgebastelt (mit dem auskommentierten FillRect() ). Nun sollte es, falls du nen einfarbigen Hintergrund verwendest gehen. Du muss dann nur noch die Farbe im Aufruf von FillRect() ändern.

Code:
#include <stdlib.h>
#include <SDL/SDL.h>
#include "SDL/SDL_image.h"
#include <string>

int main(int argc, char *argv[])
{
	SDL_Surface *screen, *image, *backg;
	SDL_Rect dst, dstblue;

	//startposition pinguin

	SDL_Event event;
	Uint8 *keys;
	int tuxX = 370, tuxY = 430;
	int done = 0;
	if (SDL_Init(SDL_INIT_VIDEO) == -1)
	{
		printf("Can't init SDL:  %s\n", SDL_GetError());
		exit(1);
	}

	//Screnn initialisieren
	atexit(SDL_Quit);
	screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 255));
	if (screen == NULL)
	{
		printf("Can't set video mode: %s\n", SDL_GetError());
		exit(1);
	}

	//hintergrundbild

	backg = SDL_LoadBMP("back.bmp");

	SDL_Rect bac;

	bac.x = 0;
	bac.y = 0;
	bac.w = backg->w;
	bac.h = backg->h;

	SDL_BlitSurface(backg, NULL, screen, &bac);

	//Pinguin Laden

	image = IMG_Load("tux.jpg");
	if (image == NULL)
	{
		printf("Can't load image of tux: %s\n", SDL_GetError());
		exit(1);
	}

	// Pinguin bewegung
	dst.w = backg->w;
	dst.h = backg->h;
	dstblue.w = image->w + 2; //edit
	dstblue.h = image->h + 2; //edit

	SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 0, 0, 0));
	while (!done)
	{
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
			case SDL_QUIT:
				done = 1;
				break;
			}
		}
		dstblue.x = tuxX - 1;
		dstblue.y = tuxY - 1;
		keys = SDL_GetKeyState(NULL);
		if (keys[SDLK_UP])
		{
			if (tuxY > 10)
			{
				tuxY--;
			}
		}
		if (keys[SDLK_DOWN])
		{
			if (tuxY < 590 - image->h) // edit
			{
				tuxY++;
			}
		}
		if (keys[SDLK_RIGHT])
		{
			if (tuxX < 790 - image->w)
			{
				tuxX++;
			}
		}
		if (keys[SDLK_LEFT])
		{
			if (tuxX > 10)
			{
				tuxX--;
			}
		}
		dst.x = tuxX;
		dst.y = tuxY;

		SDL_FillRect(screen, &dstblue, SDL_MapRGB(screen->format, 0, 0, 0)); // Farbe anpassen

		SDL_BlitSurface(image, NULL, screen, &dst);
		SDL_Flip(screen);
	}

	SDL_FreeSurface(image);

	return 0;
}

mfg. Leever
 
Zuletzt bearbeitet von einem Moderator:
Zurück