Problem on game startup

Asked

Viewed 55 times

1

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

After the build the game opens, however it is 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 only using Devc++ because I’m following a Youtube tutorial)

Library: Allegro 4.1

Follow 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.

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 resource problems (images and music) cannot be loaded, it would be by 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 the destroy_bitmap(buffer);. However, to avoid a build 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 fairly complicated.

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

You carry face thus:

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

And then, depending on the keystrokes, it does this:

face = imagem2;

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

The right thing would be for you to initialize face thus:

BITMAP *face = imagem1;

And then, you can remove that 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 addition, Allegro 4 is dead. Now all development follows with Allegro 5. Dev-C++ is also a dinosaur.

  • Very obg for the help Victor. After putting the verification of files in the code, I realized that the game for some reason can not load the images, you know more or less what could be ? The files are already in the folder.

  • Also, when the code jumps with the drop, the compiler sends three messages and the program fails. Messages are:"crosses boot of MIDI *midi", crosses boot of BITMAP *face and "crosses boot of BITMAP *imagem2"

  • @Rogi93 Reply edited.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.