Oppure

Loading
26/07/12 14:22
Driverfury
Ciao a tutti ho un problema con le SDL: quando muovo l'immagine mi da la sovrapposizione della stessa nonostante io aggiorni il background. Ecco i sorgenti del programma.


main.c
#include "SDL/SDL.h"
#include "game.h"

int main( int argc, char* argv[] ) {

	// Variabili della dialog
	int xDialog = 640; //Dimensione X della finestra
	int yDialog = 480; //DImensione Y della finestra
	int bits = 16;
	int flags = SDL_SWSURFACE;
	
	// Elementi del gioco
	SDL_Surface *screen; // Surface Principale
	Background *background; // Sfondo
	Ball *ball; // Palla
	Paddle *pad1, *pad2; // Paddles
	
	// Variabili del ciclo del gioco
	SDL_Event evento, test_event;
	int frameSkip = 10;
	Uint8 *keys = NULL;
	
	// Inizializzazione libreria SDL
	if ( SDL_Init( SDL_INIT_VIDEO ) != 0 ) {
		return 1;
	}

	// Inizializzazione surface principale dello schermo
	screen = SDL_SetVideoMode( xDialog, yDialog, bits, flags );
	SDL_LockSurface( screen ); // Blocco dello screen per evitare modifiche indesiderate
	
	// Setto lo sfondo
	SDL_UnlockSurface( screen );
	background = createBackground();
	refreshBackground( background, screen );
	SDL_LockSurface( screen );
	
	//Imposto il titolo della dialog
	SDL_WM_SetCaption ( "GoldPong", "GoldPong" );
	
	// Inizializzazione degli elementi del gioco
	ball = createBall( 16, 16, 4 );
	pad1 = createPaddle( 4, 16, 3 );
	pad2 = createPaddle( 50, 256, 3 );
	
	// Ciclo del gioco
	while ( 1 ) {
	
		SDL_Delay( frameSkip ); // Rallenta il ciclo
		
		if ( SDL_PushEvent( &test_event ) == 0 ) {
		
			// Catturo l'evento
			SDL_PollEvent( &evento );
			
			keys = SDL_GetKeyState( NULL );
			
			// Pressione del tasto ESCAPE
			if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
			
				// Libero le risorse occupate dalla libreria SDL ed esco dal gioco
				SDL_Quit();
				return 0;
			
			}
			
			// Pressione del tasto RIGHT
			if ( keys[SDLK_RIGHT] == SDL_PRESSED ) {
				
				// Setto la direzione del paddle
				pad1->dst.x += pad1->speed;
			
			}
			
			// Pressione del tasto LEFT
			if ( keys[SDLK_LEFT] == SDL_PRESSED ) {
			
				// Setto la direzione del paddle
				pad1->dst.x -= pad1->speed;
			
			}
		
		}
		
		// Aggiorno gli elementi del gioco
		SDL_UnlockSurface( screen );
		refreshBackground( background, screen );
		refreshPaddle( pad1, screen );
		refreshPaddle( pad2, background->surface );
		SDL_LockSurface( screen );	
		
		refreshScreen( screen );
	
	}
	
	// Libero le risorse occupate dalla libreria SDL
	SDL_Quit();
	
	return 0;
	
}



game.h
#include "SDL/SDL.h"

#define BOOL unsigned short
#define TRUE 1
#define FALSE 0

// Struttura direction
typedef struct Direction {

	// Ogni unsigned short può essere 0 o 1 (true o false)
	BOOL right;
	BOOL left;
	BOOL up;
	BOOL down;

} Direction;

// Struttura Ball
typedef struct Ball {

	SDL_Surface *surface;
	SDL_Rect dst;
	Direction direction;
	unsigned short speed;

} Ball;

// Struttura Paddle
typedef struct Paddle {

	SDL_Surface *surface;
	SDL_Rect dst;
	unsigned short speed;

} Paddle;

// Struttura Background
typedef struct Background {

	SDL_Surface *surface;

} Background;

Ball* createBall( int, int, unsigned short );
Paddle* createPaddle( int, int, unsigned short );
Background* createBackground();

void destroyBall( Ball* );
void destroyPaddle( Paddle* );
void destroyBackground( Background* );

void setBallDirection( Ball*, BOOL, BOOL, BOOL, BOOL );

void moveBall( Ball* );

void refreshBall( Ball*, SDL_Surface* );
void refreshPaddle( Paddle*, SDL_Surface* );

void refreshScreen( SDL_Surface* );
void refreshBackground( Background*, SDL_Surface* );




game.c
#include "SDL/SDL.h"
#include "game.h"

/* Implementazione delle funzioni */

// Funzione che crea una palla
Ball* createBall( int x, int y, unsigned short speed ) {

	Ball* ball;

	// Setto le variabili della palla
	ball->surface = SDL_LoadBMP( "ball.bmp" );
	ball->dst.x = x;
	ball->dst.y = y;
	setBallDirection( ball, FALSE, FALSE, FALSE, FALSE );
	ball->speed = speed;
	
	return ball;

}

// Funzione che distrugge una palla
void destroyBall( Ball* ball ) {

	SDL_FreeSurface( ball->surface );

}

// Funzione che crea un paddle
Paddle* createPaddle( int x, int y, unsigned short speed ) {

	Paddle* paddle;

	// Setto le variabili del paddle
	paddle->surface = SDL_LoadBMP( "paddle.bmp" );
	paddle->dst.x = x;
	paddle->dst.y = y;
	paddle->speed = speed;
	
	return paddle;

}

// Funzione che distrugge un paddle
void destroyPaddle( Paddle* paddle ) {

	SDL_FreeSurface( paddle->surface );

}

// Funziona che crea un background
Background* createBackground() {

	Background *background;

	background->surface = SDL_LoadBMP( "background.bmp" );
	
	return background;

}

// Funzione che distrugge un background
void destroyBackground( Background* background ) {

	SDL_FreeSurface( background->surface );

}

// Funzione che setta la direzione di una palla
void setBallDirection( Ball* ball, BOOL right, BOOL left, BOOL up, BOOL down ) {

	ball->direction.right = right;
	ball->direction.left = left;
	ball->direction.up = up;
	ball->direction.down = down;

}

// Funzione che muove una palla
void moveBall( Ball* ball ) {

	if ( ball->direction.right == TRUE ) {
	
		ball->dst.x += ball->speed;
	
	}
	
	if ( ball->direction.left == TRUE ) {
	
		ball->dst.x -= ball->speed;
	
	}
	
	if ( ball->direction.up == TRUE ) {
	
		ball->dst.y -= ball->speed;
	
	}
	
	if ( ball->direction.down == TRUE ) {
	
		ball->dst.y += ball->speed;
	
	}

}

// Aggiorna la palla sullo schermo
void refreshBall( Ball* ball, SDL_Surface* screen ) {

	SDL_BlitSurface( ball->surface, NULL, screen, &ball->dst );

}

// Aggiorna il paddle sullo schermo
void refreshPaddle( Paddle* paddle, SDL_Surface* screen ) {

	SDL_BlitSurface( paddle->surface, NULL, screen, &paddle->dst );

}

// Aggiorna lo schermo
void refreshScreen( SDL_Surface* screen ) {

	//Aggiorna screen
	SDL_UnlockSurface( screen );
	SDL_BlitSurface( screen, NULL, NULL, NULL );
	SDL_UpdateRect ( screen, 0, 0, 0, 0 ); // Gli zero stanno ad indicare che aggiornerà tutto la dialog
	SDL_LockSurface( screen );
	
}

// Aggiorna il background
void refreshBackground( Background* background, SDL_Surface* screen ) {

	// Aggiorna background
	SDL_BlitSurface( background->surface, NULL, screen, NULL );

}


Potreste aiutarmi?
Ultima modifica effettuata da Driverfury 26/07/12 14:23
aaa
26/07/12 23:46
pierotofy
Potresti aggiungere qualche screenshot spiegando meglio cosa succede?
Il mio blog: piero.dev
27/07/12 8:41
carlduke
per aggiornare lo schermo io us(avo) questo codice:

while(running)
{
   SDL_Flip(screen);
   SDL_BlitSurface(buffer,NULL,screen,NULL);
   
   . . .

} 


e poi dopo questo andavo a disegnare tutto sul buffer ..(double buffering) :k:
aaa