Problem in game startup

1

I'm trying to run a game I made in Allegro 4, the application only runs normally on first compilation / execution. If I close DevC ++ and re-open or try to run the generated .exe file, the game stops working.

After compiling the game opens, but gets minimized and locked in the taskbar, the only way to close it is through the task manager.

I noticed that this problem appeared after adding sound to the game.

Information:

System: Windows 7 Professional

IDE: DevC ++ 4.9.9.3 (I'm just using DevC ++ because I'm following a YouTube tutorial)

Library: Allegro 4.1

Here is the code below:

#include <allegro.h>

int main(){

// Inicialização
allegro_init();
install_keyboard();
set_color_depth(32);
set_window_title("The Emoji 2");
install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0);


// Variáveis
int x = 100;
int y = 100;

// Imagens
BITMAP *buffer = create_bitmap(800,600);
BITMAP *imagem1 = load_bitmap("Emoji1.bmp",NULL);
BITMAP *imagem2 = load_bitmap("Emoji2.bmp",NULL);
BITMAP *face = load_bitmap("Emoji1.bmp",NULL);

// Sons
MIDI *midi = load_midi("midi.mid"); 

play_midi(midi,TRUE); 

while(!key[KEY_ESC]){
    if(key[KEY_RIGHT]){
        x += 1;
        face = imagem2;
    }   
    else if(key[KEY_LEFT]){
        x -= 1;
        face = imagem1;
    }   
    else if(key[KEY_UP]){
        y -= 1;
    }
    else if(key[KEY_DOWN]){
        y += 1;
    }
    draw_sprite(buffer,face, 100 + x, 100 + y);
    draw_sprite(screen,buffer, 0, 0);
    rest(5);
    clear(buffer);  
} // fim do while

destroy_bitmap(buffer);
destroy_bitmap(face);
destroy_bitmap(imagem1);
destroy_bitmap(imagem2);
destroy_midi(midi);
return 0;

} 
END_OF_MAIN()

Note: The files called in the code are already inside the project folder.

    
asked by anonymous 05.08.2018 / 05:15

1 answer

2

The first thing to check is if the files Emoji1.bmp , Emoji2.bmp and midi.mid are present in the same folder as the executable is.

To protect against problems that the resources (images and music) can not be loaded, would be checking the returned pointers. For example:

BITMAP *imagem1 = load_bitmap("Emoji1.bmp", NULL);
if (imagem1 == NULL) {
    printf("Não conseguiu abrir o arquivo Emoji1.bmp");
    goto fim;
}
BITMAP *imagem2 = load_bitmap("Emoji2.bmp", NULL);
if (imagem2 == NULL) {
    printf("Não conseguiu abrir o arquivo Emoji2.bmp");
    goto fim;
}
MIDI *midi = load_midi("midi.mid");
if (midi == NULL) {
    printf("Não conseguiu abrir o arquivo midi.mid");
    goto fim;
}

The label fim , you put before destroy_bitmap(buffer); . However, to avoid a compilation error, you will first have to declare the bitmaps and the mid and assign them NULL before the first goto . There are ways to do this without using this goto , but it gets reasonably complicated.

There is also a memory leak here. Note that Emoji1.bmp is opened twice and one of the references is the face variable.

You load face like this:

BITMAP *face = load_bitmap("Emoji1.bmp",NULL);

And then, depending on the keystrokes, do this:

face = imagem2;

This will cause the originally loaded bitmap to be lost in memory.

The right thing would be for you to initialize face like this:

BITMAP *face = imagem1;

And then you can remove this line:

destroy_bitmap(face);

Your code should look like this:

#include <allegro.h>

int main() {

    // Inicialização
    allegro_init();
    install_keyboard();
    set_color_depth(32);
    set_window_title("The Emoji 2");
    install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);

    // Variáveis
    int x = 100;
    int y = 100;
    MIDI *mid = NULL;
    BITMAP *buffer = NULL;
    BITMAP *imagem1 = NULL;
    BITMAP *imagem2 = NULL;

    // Imagens       
    buffer = create_bitmap(800, 600);
    imagem1 = load_bitmap("Emoji1.bmp", NULL);
    if (imagem1 == NULL) {
        printf("Não conseguiu abrir o arquivo Emoji1.bmp");
        goto fim;
    }
    imagem2 = load_bitmap("Emoji2.bmp", NULL);
    if (imagem2 == NULL) {
        printf("Não conseguiu abrir o arquivo Emoji2.bmp");
        goto fim;
    }
    BITMAP *face = imagem1;

    // Sons
    midi = load_midi("midi.mid"); 
    if (midi == NULL) {
        printf("Não conseguiu abrir o arquivo midi.mid");
        goto fim;
    }

    play_midi(midi, TRUE); 

    while (!key[KEY_ESC]) {
        if (key[KEY_RIGHT]) {
            x++;
            face = imagem2;
        } else if (key[KEY_LEFT]) {
            x--;
            face = imagem1;
        } else if (key[KEY_UP]) {
            y--;
        } else if (key[KEY_DOWN]) {
            y++;
        }
        draw_sprite(buffer, face, 100 + x, 100 + y);
        draw_sprite(screen, buffer, 0, 0);
        rest(5);
        clear(buffer);
    } // fim do while

    fim:
    destroy_bitmap(buffer);
    destroy_bitmap(imagem1);
    destroy_bitmap(imagem2);
    destroy_midi(midi);
    return 0;
}
END_OF_MAIN()

In fact, Allegro 4 is dead. Now all development goes on with Allegro 5. Dev-C ++ is also a dinosaur.

    
05.08.2018 / 07:31