Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - SDL aggiornamento sfondo
Forum - C/C++ - SDL aggiornamento sfondo

Avatar
Driverfury (Normal User)
Rookie


Messaggi: 45
Iscritto: 21/09/2011

Segnala al moderatore
Postato alle 16:22
Giovedì, 26/07/2012
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
Codice sorgente - presumibilmente C++

  1. #include "SDL/SDL.h"
  2. #include "game.h"
  3.  
  4. int main( int argc, char* argv[] ) {
  5.  
  6.         // Variabili della dialog
  7.         int xDialog = 640; //Dimensione X della finestra
  8.         int yDialog = 480; //DImensione Y della finestra
  9.         int bits = 16;
  10.         int flags = SDL_SWSURFACE;
  11.        
  12.         // Elementi del gioco
  13.         SDL_Surface *screen; // Surface Principale
  14.         Background *background; // Sfondo
  15.         Ball *ball; // Palla
  16.         Paddle *pad1, *pad2; // Paddles
  17.        
  18.         // Variabili del ciclo del gioco
  19.         SDL_Event evento, test_event;
  20.         int frameSkip = 10;
  21.         Uint8 *keys = NULL;
  22.        
  23.         // Inizializzazione libreria SDL
  24.         if ( SDL_Init( SDL_INIT_VIDEO ) != 0 ) {
  25.                 return 1;
  26.         }
  27.  
  28.         // Inizializzazione surface principale dello schermo
  29.         screen = SDL_SetVideoMode( xDialog, yDialog, bits, flags );
  30.         SDL_LockSurface( screen ); // Blocco dello screen per evitare modifiche indesiderate
  31.        
  32.         // Setto lo sfondo
  33.         SDL_UnlockSurface( screen );
  34.         background = createBackground();
  35.         refreshBackground( background, screen );
  36.         SDL_LockSurface( screen );
  37.        
  38.         //Imposto il titolo della dialog
  39.         SDL_WM_SetCaption ( "GoldPong", "GoldPong" );
  40.        
  41.         // Inizializzazione degli elementi del gioco
  42.         ball = createBall( 16, 16, 4 );
  43.         pad1 = createPaddle( 4, 16, 3 );
  44.         pad2 = createPaddle( 50, 256, 3 );
  45.        
  46.         // Ciclo del gioco
  47.         while ( 1 ) {
  48.        
  49.                 SDL_Delay( frameSkip ); // Rallenta il ciclo
  50.                
  51.                 if ( SDL_PushEvent( &test_event ) == 0 ) {
  52.                
  53.                         // Catturo l'evento
  54.                         SDL_PollEvent( &evento );
  55.                        
  56.                         keys = SDL_GetKeyState( NULL );
  57.                        
  58.                         // Pressione del tasto ESCAPE
  59.                         if ( keys[SDLK_ESCAPE] == SDL_PRESSED ) {
  60.                        
  61.                                 // Libero le risorse occupate dalla libreria SDL ed esco dal gioco
  62.                                 SDL_Quit();
  63.                                 return 0;
  64.                        
  65.                         }
  66.                        
  67.                         // Pressione del tasto RIGHT
  68.                         if ( keys[SDLK_RIGHT] == SDL_PRESSED ) {
  69.                                
  70.                                 // Setto la direzione del paddle
  71.                                 pad1->dst.x += pad1->speed;
  72.                        
  73.                         }
  74.                        
  75.                         // Pressione del tasto LEFT
  76.                         if ( keys[SDLK_LEFT] == SDL_PRESSED ) {
  77.                        
  78.                                 // Setto la direzione del paddle
  79.                                 pad1->dst.x -= pad1->speed;
  80.                        
  81.                         }
  82.                
  83.                 }
  84.                
  85.                 // Aggiorno gli elementi del gioco
  86.                 SDL_UnlockSurface( screen );
  87.                 refreshBackground( background, screen );
  88.                 refreshPaddle( pad1, screen );
  89.                 refreshPaddle( pad2, background->surface );
  90.                 SDL_LockSurface( screen );     
  91.                
  92.                 refreshScreen( screen );
  93.        
  94.         }
  95.        
  96.         // Libero le risorse occupate dalla libreria SDL
  97.         SDL_Quit();
  98.        
  99.         return 0;
  100.        
  101. }




game.h
Codice sorgente - presumibilmente C++

  1. #include "SDL/SDL.h"
  2.  
  3. #define BOOL unsigned short
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. // Struttura direction
  8. typedef struct Direction {
  9.  
  10.         // Ogni unsigned short può essere 0 o 1 (true o false)
  11.         BOOL right;
  12.         BOOL left;
  13.         BOOL up;
  14.         BOOL down;
  15.  
  16. } Direction;
  17.  
  18. // Struttura Ball
  19. typedef struct Ball {
  20.  
  21.         SDL_Surface *surface;
  22.         SDL_Rect dst;
  23.         Direction direction;
  24.         unsigned short speed;
  25.  
  26. } Ball;
  27.  
  28. // Struttura Paddle
  29. typedef struct Paddle {
  30.  
  31.         SDL_Surface *surface;
  32.         SDL_Rect dst;
  33.         unsigned short speed;
  34.  
  35. } Paddle;
  36.  
  37. // Struttura Background
  38. typedef struct Background {
  39.  
  40.         SDL_Surface *surface;
  41.  
  42. } Background;
  43.  
  44. Ball* createBall( int, int, unsigned short );
  45. Paddle* createPaddle( int, int, unsigned short );
  46. Background* createBackground();
  47.  
  48. void destroyBall( Ball* );
  49. void destroyPaddle( Paddle* );
  50. void destroyBackground( Background* );
  51.  
  52. void setBallDirection( Ball*, BOOL, BOOL, BOOL, BOOL );
  53.  
  54. void moveBall( Ball* );
  55.  
  56. void refreshBall( Ball*, SDL_Surface* );
  57. void refreshPaddle( Paddle*, SDL_Surface* );
  58.  
  59. void refreshScreen( SDL_Surface* );
  60. void refreshBackground( Background*, SDL_Surface* );





game.c
Codice sorgente - presumibilmente C++

  1. #include "SDL/SDL.h"
  2. #include "game.h"
  3.  
  4. /* Implementazione delle funzioni */
  5.  
  6. // Funzione che crea una palla
  7. Ball* createBall( int x, int y, unsigned short speed ) {
  8.  
  9.         Ball* ball;
  10.  
  11.         // Setto le variabili della palla
  12.         ball->surface = SDL_LoadBMP( "ball.bmp" );
  13.         ball->dst.x = x;
  14.         ball->dst.y = y;
  15.         setBallDirection( ball, FALSE, FALSE, FALSE, FALSE );
  16.         ball->speed = speed;
  17.        
  18.         return ball;
  19.  
  20. }
  21.  
  22. // Funzione che distrugge una palla
  23. void destroyBall( Ball* ball ) {
  24.  
  25.         SDL_FreeSurface( ball->surface );
  26.  
  27. }
  28.  
  29. // Funzione che crea un paddle
  30. Paddle* createPaddle( int x, int y, unsigned short speed ) {
  31.  
  32.         Paddle* paddle;
  33.  
  34.         // Setto le variabili del paddle
  35.         paddle->surface = SDL_LoadBMP( "paddle.bmp" );
  36.         paddle->dst.x = x;
  37.         paddle->dst.y = y;
  38.         paddle->speed = speed;
  39.        
  40.         return paddle;
  41.  
  42. }
  43.  
  44. // Funzione che distrugge un paddle
  45. void destroyPaddle( Paddle* paddle ) {
  46.  
  47.         SDL_FreeSurface( paddle->surface );
  48.  
  49. }
  50.  
  51. // Funziona che crea un background
  52. Background* createBackground() {
  53.  
  54.         Background *background;
  55.  
  56.         background->surface = SDL_LoadBMP( "background.bmp" );
  57.        
  58.         return background;
  59.  
  60. }
  61.  
  62. // Funzione che distrugge un background
  63. void destroyBackground( Background* background ) {
  64.  
  65.         SDL_FreeSurface( background->surface );
  66.  
  67. }
  68.  
  69. // Funzione che setta la direzione di una palla
  70. void setBallDirection( Ball* ball, BOOL right, BOOL left, BOOL up, BOOL down ) {
  71.  
  72.         ball->direction.right = right;
  73.         ball->direction.left = left;
  74.         ball->direction.up = up;
  75.         ball->direction.down = down;
  76.  
  77. }
  78.  
  79. // Funzione che muove una palla
  80. void moveBall( Ball* ball ) {
  81.  
  82.         if ( ball->direction.right == TRUE ) {
  83.        
  84.                 ball->dst.x += ball->speed;
  85.        
  86.         }
  87.        
  88.         if ( ball->direction.left == TRUE ) {
  89.        
  90.                 ball->dst.x -= ball->speed;
  91.        
  92.         }
  93.        
  94.         if ( ball->direction.up == TRUE ) {
  95.        
  96.                 ball->dst.y -= ball->speed;
  97.        
  98.         }
  99.        
  100.         if ( ball->direction.down == TRUE ) {
  101.        
  102.                 ball->dst.y += ball->speed;
  103.        
  104.         }
  105.  
  106. }
  107.  
  108. // Aggiorna la palla sullo schermo
  109. void refreshBall( Ball* ball, SDL_Surface* screen ) {
  110.  
  111.         SDL_BlitSurface( ball->surface, NULL, screen, &ball->dst );
  112.  
  113. }
  114.  
  115. // Aggiorna il paddle sullo schermo
  116. void refreshPaddle( Paddle* paddle, SDL_Surface* screen ) {
  117.  
  118.         SDL_BlitSurface( paddle->surface, NULL, screen, &paddle->dst );
  119.  
  120. }
  121.  
  122. // Aggiorna lo schermo
  123. void refreshScreen( SDL_Surface* screen ) {
  124.  
  125.         //Aggiorna screen
  126.         SDL_UnlockSurface( screen );
  127.         SDL_BlitSurface( screen, NULL, NULL, NULL );
  128.         SDL_UpdateRect ( screen, 0, 0, 0, 0 ); // Gli zero stanno ad indicare che aggiornerà tutto la dialog
  129.         SDL_LockSurface( screen );
  130.        
  131. }
  132.  
  133. // Aggiorna il background
  134. void refreshBackground( Background* background, SDL_Surface* screen ) {
  135.  
  136.         // Aggiorna background
  137.         SDL_BlitSurface( background->surface, NULL, screen, NULL );
  138.  
  139. }



Potreste aiutarmi?

Ultima modifica effettuata da Driverfury il 26/07/2012 alle 16:23
PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 1:46
Venerdì, 27/07/2012
Potresti aggiungere qualche screenshot spiegando meglio cosa succede?


Il mio blog: https://piero.dev
PM Quote
Avatar
carlduke (Member)
Pro


Messaggi: 153
Iscritto: 29/01/2011

Segnala al moderatore
Postato alle 10:41
Venerdì, 27/07/2012
per aggiornare lo schermo io us(avo) questo codice:

Codice sorgente - presumibilmente C/C++

  1. while(running)
  2. {
  3.    SDL_Flip(screen);
  4.    SDL_BlitSurface(buffer,NULL,screen,NULL);
  5.    
  6.    . . .
  7.  
  8. }



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

PM Quote