Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Allegro 5, C++] Problem z oknem i dzwiękiem

Ostatnio zmodyfikowano 2012-06-16 17:29
Autor Wiadomość
wojownik266
Temat założony przez niniejszego użytkownika
[Allegro 5, C++] Problem z oknem i dzwiękiem
» 2012-06-14 15:24:43
Zabrałem się za tworzenie mojej pierwszej gry w Allegro 5. Wszystko jak narazie działa ok. Teraz chciałbym dodać muzyczkę do mojej gry i tu niespodzianka kiedy dodam tą funkcje
al_play_sample( plusk, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL );
 to kiedy skompiluję a potem uruchomię gre to pojawia się na chwilę okienko i szybko znika tak ze nic nie widać i nie słychać. Kiedy zakomentuję tą funkcje o tak:
C/C++
//al_play_sample(plusk,1.0,0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);
to wszystko działa. Gdzie tkwi problem? Wie ktoś czym to może być spowodowane?
P-58475
OSA_PL
» 2012-06-14 15:26:19
Plik z dźwiękiem, na pewno jest tam gdzie powinien? 
P-58476
wojownik266
Temat założony przez niniejszego użytkownika
» 2012-06-14 15:33:03
Tak plik z dzwiękiem jest w katalogu z grą. Co by mój tok myślenia był nieco łatwiejszy do zrozumienia, zamieszczam cały kod gry:

C/C++
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <cmath>

const int WIDTH = 640;
const int HEIGHT = 480;

bool keys[] = { false, false, false, false, false };
enum KEYS { LEFT, RIGHT };

struct Sprite
{
    float x;
    float y;
    float velX;
    float velY;
    int dirX;
    int dirY;
   
    int r;
    int w;
    int h;
   
    ALLEGRO_BITMAP * image;
};
void scroll_piwo( float & y )
{
    if( y < 480 )
    {
        y++;
       
    }
    else if( y >= 480 )
    {
        //x=-SCREEN_W;
        y =- HEIGHT;
    }
}
int main( int argc, char ** argv )
{
    //==============================================
    //PROJECT VARIABLES
    //==============================================
    int punkty = 0;
    time_t czas;
    czas = clock() / 360;
   
    bool done = false;
    bool render = false;
   
    bool bound = false;
    bool collision = false;
    Sprite ball1;
    Sprite ball2, ball3, ball4, ball5, ball6;
   
    ball1.x = 320; //kontener
    ball1.y = 460;
    ball2.x = 400;
    ball2.y = 240;
    ball3.x = 100;
    ball3.y = 20;
    ball4.x = 520;
    ball4.y = - HEIGHT;
    ball5.x = 320;
    ball5.y = - HEIGHT - 200;
    ball6.x = 260;
    ball6.y = - HEIGHT - 100;
   
    //==============================================
    //ALLEGRO VARIABLES
    //==============================================
    ALLEGRO_DISPLAY * display = NULL;
    ALLEGRO_EVENT_QUEUE * event_queue = NULL;
    ALLEGRO_TIMER * timer;
    ALLEGRO_FONT * font18 = NULL;
    ALLEGRO_BITMAP * tlo = NULL;
    //ALLEGRO_SAMPLE *muzyczka = NULL;
    ALLEGRO_SAMPLE * plusk = NULL;
   
    //==============================================
    //ALLEGRO INIT FUNCTIONS
    //==============================================
    if( !al_init() ) //initialize Allegro
         return - 1;
   
    display = al_create_display( WIDTH, HEIGHT ); //create our display object
   
    if( !display ) //test display object
         return - 1;
   
    //==============================================
    //ADDON INSTALL
    //==============================================
    al_install_keyboard();
    al_init_image_addon();
    al_init_font_addon();
    al_init_ttf_addon();
    al_init_acodec_addon();
    al_install_audio();
    al_reserve_samples( 10 );
   
   
   
   
    //==============================================
    //PROJECT INIT
    //==============================================
    font18 = al_load_font( "courbd.ttf", 14, 0 );
    //muzyczka = al_load_sample("muzyczka.midi");
    plusk = al_load_sample( "mu.ogg" );
   
   
   
    ball1.image = al_load_bitmap( "kontener.bmp" );
    ball2.image = al_load_bitmap( "harnas.bmp" ); //puszka piwa 1
    ball3.image = al_load_bitmap( "harnas.bmp" ); //puszka piwa 2
    ball4.image = al_load_bitmap( "harnas.bmp" ); //puszka piwa 3
    ball5.image = al_load_bitmap( "harnas.bmp" ); //puszka piwa 4
    ball6.image = al_load_bitmap( "harnas.bmp" ); //puszka piwa 5
   
    tlo = al_load_bitmap( "tlo.bmp" );
    ball1.w = al_get_bitmap_width( ball1.image );
    ball1.h = al_get_bitmap_height( ball1.image );
   
    ball2.w = al_get_bitmap_width( ball2.image );
    ball2.h = al_get_bitmap_height( ball2.image );
    ball3.w = al_get_bitmap_width( ball3.image );
    ball3.h = al_get_bitmap_height( ball3.image );
    ball4.w = al_get_bitmap_width( ball4.image );
    ball4.h = al_get_bitmap_height( ball4.image );
    ball5.w = al_get_bitmap_width( ball5.image );
    ball5.h = al_get_bitmap_height( ball5.image );
    ball6.w = al_get_bitmap_width( ball6.image );
    ball6.h = al_get_bitmap_height( ball6.image );
   
    ball1.r = ball1.w / 2 - 10;
    ball2.r = ball2.w / 2 - 10;
    ball3.r = ball3.w / 2 - 10;
    ball4.r = ball4.w / 2 - 10;
    ball5.r = ball5.w / 2 - 10;
    ball6.r = ball6.w / 2 - 10;
   
    //==============================================
    //TIMER INIT AND STARTUP
    //==============================================
    event_queue = al_create_event_queue();
    timer = al_create_timer( 1.0 / 60 );
   
    al_register_event_source( event_queue, al_get_timer_event_source( timer ) );
    al_register_event_source( event_queue, al_get_display_event_source( display ) );
    al_register_event_source( event_queue, al_get_keyboard_event_source() );
   
    // al_play_sample(plusk,1.0,0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);
   
    al_start_timer( timer );
    while( !done )
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event( event_queue, & ev );
       
        //==============================================
        //INPUT
        //==============================================
        if( ev.type == ALLEGRO_EVENT_KEY_DOWN )
        {
            switch( ev.keyboard.keycode )
            {
            case ALLEGRO_KEY_ESCAPE:
                done = true;
                break;
            case ALLEGRO_KEY_LEFT:
                keys[ LEFT ] = true;
                break;
            case ALLEGRO_KEY_RIGHT:
                keys[ RIGHT ] = true;
                break;
            }
        }
        else if( ev.type == ALLEGRO_EVENT_KEY_UP )
        {
            switch( ev.keyboard.keycode )
            {
            case ALLEGRO_KEY_ESCAPE:
                done = true;
                break;
            case ALLEGRO_KEY_LEFT:
                keys[ LEFT ] = false;
                break;
            case ALLEGRO_KEY_RIGHT:
                keys[ RIGHT ] = false;
                break;
               
            }
        }
        //==============================================
        //GAME UPDATE
        //==============================================
        else if( ev.type == ALLEGRO_EVENT_TIMER )
        {
            render = true;
           
            if( keys[ LEFT ] )
                 ball1.x -= 5;
            else if( keys[ RIGHT ] )
                 ball1.x += 5;
           
           
            float distance = sqrt( pow( ball1.x - ball2.x, 2 ) + pow( ball1.y - ball2.y, 2 ) );
           
            if( distance < ball1.r + ball2.r )
            {
                collision = true;
                punkty++;
                ball2.y = HEIGHT * 2;
               
            }
           
            else if( ball2.y >= 480 )
            {
                al_draw_text( font18, al_map_rgb( 255, 255, 255 ), 320, 240, ALLEGRO_ALIGN_CENTRE, "GAME OVER" );
                al_flip_display();
                al_rest( 5.0 );
                exit( 1 );
            }
            else
            {
                collision = false;
               
            }
           
            scroll_piwo( ball2.y );
            scroll_piwo( ball3.y );
            scroll_piwo( ball4.y );
            scroll_piwo( ball5.y );
            scroll_piwo( ball6.y );
           
        }
       
        //==============================================
        //RENDER
        //==============================================
        if( render && al_is_event_queue_empty( event_queue ) )
        {
            render = false;
            al_draw_bitmap( tlo, 0, 0, 0 );
           
            al_draw_bitmap( ball2.image, ball2.x - ball2.w / 2, ball2.y - ball2.h / 2, 0 );
            al_draw_bitmap( ball3.image, ball3.x - ball3.w / 3, ball3.y - ball3.h / 2, 0 );
            al_draw_bitmap( ball4.image, ball4.x - ball4.w / 2, ball4.y - ball4.h / 2, 0 );
            al_draw_bitmap( ball5.image, ball5.x - ball5.w / 2, ball5.y - ball5.h / 2, 0 );
            al_draw_bitmap( ball6.image, ball6.x - ball6.w / 2, ball6.y - ball6.h / 2, 0 );
            al_draw_bitmap( ball1.image, ball1.x - ball1.w / 2, ball1.y - ball1.h / 2, 0 );
           
            al_convert_mask_to_alpha( ball2.image, al_map_rgb( 255, 0, 255 ) ); //Usuwanie tla
            al_convert_mask_to_alpha( ball3.image, al_map_rgb( 255, 0, 255 ) );
            al_convert_mask_to_alpha( ball4.image, al_map_rgb( 255, 0, 255 ) );
            al_convert_mask_to_alpha( ball5.image, al_map_rgb( 255, 0, 255 ) );
            al_convert_mask_to_alpha( ball6.image, al_map_rgb( 255, 0, 255 ) );
           
           
           
            /*if(collision){
            al_draw_text(font18, al_map_rgb(255, 100, 255), WIDTH / 2, 100, ALLEGRO_ALIGN_CENTRE, "Collision!");
           
           
            }*/
           
            al_draw_textf( font18, al_map_rgb( 255, 255, 255 ), 20, 20, ALLEGRO_ALIGN_LEFT, "PUNKTY  %i", punkty );
            al_draw_textf( font18, al_map_rgb( 255, 255, 255 ), 240, 20, ALLEGRO_ALIGN_LEFT, "CZAS   %i", czas );
            al_draw_textf( font18, al_map_rgb( 255, 255, 255 ), 460, 20, ALLEGRO_ALIGN_LEFT, "TEMPO  %i", punkty );
            al_flip_display();
            al_clear_to_color( al_map_rgb( 0, 0, 0 ) );
           
        }
    }
   
    //==============================================
    //DESTROY ALLEGRO OBJECTS
    //==============================================
    al_destroy_bitmap( tlo );
    al_destroy_bitmap( ball1.image );
    al_destroy_bitmap( ball2.image );
    al_destroy_bitmap( ball3.image );
    al_destroy_bitmap( ball4.image );
    al_destroy_bitmap( ball5.image );
    al_destroy_bitmap( ball6.image );
   
    al_destroy_font( font18 );
    al_destroy_timer( timer );
    al_destroy_event_queue( event_queue );
    al_destroy_display( display );
   
    //al_destroy_sample(muzyczka);
    al_destroy_sample( plusk );
   
    return 0;
}
P-58478
McAffey
» 2012-06-16 00:04:44
Jesteś pewien, że format pliku masz w porządku (czy allegro czyta Ci ten plik mu.ogg ? Nie bawiłem się nigdy w allegro 5, więc nie wiem jak to u nich wygląda.
P-58550
Dragonit
» 2012-06-16 00:37:35
To jest to jak nie piszecie obsługi błędów. Ja zawsze piszę i wiem od razu o co biega.
P-58553
SeaMonster131
» 2012-06-16 09:39:09
A spróbuj może umieścić te odtwarzanie dźwięku w tej głównej pętli ?
P-58555
Schulze13
» 2012-06-16 17:21:13
@Dragonit
W takim razie jak ma wyglądać taka obsługa błędów? Bardzo mnie to ciekawi ;>
P-58568
wojownik266
Temat założony przez niniejszego użytkownika
» 2012-06-16 17:29:19
Problem z brakiem odtwarzania dzwięku rozwiązałem w ten sposób że przeinstalowałem bibliotekę allegro i kompilator  MinGW oraz dodałem kilka libów typu libgdi32.a itp. Zlinkowałem bibliotekę dynamicznie... Problem z dzwiękiem zniknął. Natomiast pojawił się nowy tak jak poniżej:
http://cpp0x.pl/forum/temat/?id=7482
Z tym że dotyczy teraz wyświetlania czcionki. Jakieś sugestie? Bardzo bym o nie prosił bo czas ucieka a klient czeka.
P-58569
« 1 »
  Strona 1 z 1