Visualizzazione dei risultati da 1 a 6 su 6

Discussione: [C++] Eventi nelle SDL

  1. #1

    [C++] Eventi nelle SDL

    Ok sto facendo un semplicissimo gioco in c++ con le sdl, usando questi tutorial ...
    ma quando tento di usare le funzioni per gli eventi.. non vanno!! ^^
    Gli esempi dei tutorial si, questo dimostra che è il mio codice! Ma non ne capisco il perchè visto che è praticamente uguale a quello dell'esempio..

    Scusate se il codice è un po' "confuso", ho usato il tag quote per evidenziare dov'è l'utilizzo degli eventi!
    Grazie in anticipo!

    //The headers
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>

    //SDL_Event event;
    //Screen attributes
    const int SCREEN_WIDTH = 800;
    const int SCREEN_HEIGHT = 600;
    const int SCREEN_BPP = 32;

    //The surfaces
    SDL_Surface *screen = NULL;
    SDL_Surface *intro1 = NULL;
    SDL_Surface *intro2 = NULL;
    SDL_Surface *tuxKiller = NULL;
    SDL_Surface *killTux = NULL;
    SDL_Surface *muro = NULL;
    SDL_Surface *mappa1 = NULL;
    SDL_Surface *buf = NULL;

    SDL_Event event;

    SDL_Surface *load_image( std::string filename )
    {
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image using SDL_image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
    //Create an optimized image
    optimizedImage = SDL_DisplayFormat( loadedImage );

    //Free the old image
    SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
    }

    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
    {
    //Rectangle to hold the offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
    }

    bool init()
    {
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
    return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    buf = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    //If there was an error in setting up the screen
    if( screen == NULL || buf == NULL )
    {
    return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "The TuxWars", NULL );

    //If everything initialized fine
    return true;
    }

    void clean_up_intro()
    {
    //Free the surface
    SDL_FreeSurface( intro1 );
    SDL_FreeSurface( intro2 );

    //Quit SDL
    //SDL_Quit();
    }

    void clean_up_map1()
    {
    SDL_FreeSurface( killTux );
    SDL_FreeSurface( tuxKiller );
    SDL_FreeSurface( muro );
    SDL_FreeSurface( mappa1 );
    SDL_Quit();
    }
    int intro()
    {

    //
    //Intro al gioco!
    //
    //intro1
    apply_surface( 0, 0, intro1, screen );
    if( SDL_Flip( screen ) == -1 )
    {
    return 1;
    }
    SDL_Delay( 3000 );

    //intro1
    apply_surface( 0, 0, intro2, screen );
    if( SDL_Flip( screen ) == -1 )
    {
    return 1;
    }
    SDL_Delay( 6000 );
    }

    int CaricaImmagini()
    {
    //Load the image
    intro1 = load_image( "intro1.jpg" );
    intro2 = load_image( "intro2.png" );
    killTux = load_image( "TUX_TRIB.gif" );
    tuxKiller = load_image( "tuxkiller.gif" );
    muro = load_image( "muro.gif" );
    mappa1 = load_image( "mappa1.png" );
    //If there was a problem in loading the image
    if( intro1 == NULL || intro2 == NULL || killTux == NULL || tuxKiller == NULL || muro == NULL)
    {
    return 1;
    }
    }

    int map1()
    {
    SDL_Event event;
    int x_tuxKiller, y_tuxKiller, x_killTux, y_killTux;

    int full_s=0;
    x_tuxKiller = 20;
    y_tuxKiller = 495;
    bool gameover = 0;
    bool quit = false;

    apply_surface( 0, 0, mappa1, buf );
    apply_surface( 0, 0, killTux, buf );
    apply_surface( x_tuxKiller, y_tuxKiller, tuxKiller, buf );
    apply_surface( 0, 0, buf, screen );

    while( quit == false )
    {
    //While there's an event to handle
    while( SDL_PollEvent( &event ) )
    {
    //If the user has Xed out the window
    if( event.type == SDL_QUIT )
    {
    //Quit the program
    quit = true;
    }
    }
    }


    }



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


    int x_tuxKiller, y_tuxKiller, x_killTux, y_killTux;
    x_tuxKiller = 20;
    y_tuxKiller = 495;
    //Initialize
    if( init() == false )
    {
    return 1;
    }

    CaricaImmagini();
    intro();
    map1();

    //Free the surface and quit SDL
    clean_up_map1();

    return 0;
    }

  2. #2
    Hai dichiarato due volte event, non dovrebbe dare problemi, ma prova a toglierlo.

  3. #3
    ho tolto tutti quelli in eccesso, ma non funziona lo stesso!!

    cioè in pratica è come se SDL_QUIT si attivasse

  4. #4
    Originariamente inviato da mainframe.it
    ho tolto tutti quelli in eccesso, ma non funziona lo stesso!!

    cioè in pratica è come se SDL_QUIT si attivasse
    codice:
    while(!quit)
       {
       //While there's an event to handle
          while( SDL_PollEvent( &event ) )
          {
          //If the user has Xed out the window
          if( event.type == SDL_QUIT )
          {
          //Quit the program
          quit = true;
         }
       }
       SDL_Delay(500);
    }

  5. #5
    ho fatto quella modifica ma si comporta in modo identico

  6. #6
    e se fosse SDL_Flip( screen ) che fa ritornare 1?
    codice:
    if( SDL_Flip( screen ) == -1 )
        {
            return 1;    
        }

    EDIT: ok probabilmente era quello, perchè avevo provato a fare il doppio buffer come con allegro, impostando male il codice! Adesso ho messo a posto!


Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.