Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Graphic window opening itself and closing instantly (A4)

This thread is locked; no one can reply to it. rss feed Print
Graphic window opening itself and closing instantly (A4)
PapyScotch
Member #23,751
April 2023

Hello i'm a beginner with allegro 4 and c in general and i can't find my mistake on my code, the graphic is opening itself and instantly closing after 0,5 secondes, if anyone could help that could be awesome

(i'm french so fuction are in french sorry, i can translate the if needed)

#SelectExpand
1#include <stdio.h> 2#include <stdlib.h> 3#include <allegro.h> 4#include <time.h> 5 6#define NFOURMI 100 7#define TX 10 8#define TY 20 9 10//Ex2 11 12typedef struct fourmi{ 13 int posx; 14 int posy; 15 int depx; 16 int depy; 17}t_fourmi; 18 19void aleaDepFour (t_fourmi *fourmi); 20 21void creerFourmi(t_fourmi *newfourmi); 22 23void actualiserFourmi(t_fourmi *fourmi); 24 25void dessinerFourmi(BITMAP *page, t_fourmi *fourmii); 26 27void remplirTabFourmis(t_fourmi *newfourmitab[NFOURMI]); 28 29int collisionFourmis(t_fourmi *f1,t_fourmi *f2); 30 31void actualiserTabFourmis(t_fourmi *fourmis[NFOURMI]); 32 33void dessinerTabFourmis(BITMAP *page,t_fourmi *fourmis[NFOURMI]); 34 35///////////////////////////////////////////////////////////////////////////////////// 36 37int main(){ 38 39 srand(time(NULL)); 40 t_fourmi *mesFourmis[NFOURMI]; 41 BITMAP *page; 42 43 allegro_init(); 44 install_keyboard(); 45 46 set_color_depth(desktop_color_depth()); 47 if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0)!=0) 48 { 49 allegro_message("prb gfx mode"); 50 allegro_exit(); 51 exit(EXIT_FAILURE); 52 } 53 54 page=create_bitmap(800,600); 55 remplirTabFourmis(mesFourmis); 56 57 while (!key[KEY_ESC]){ 58 clear_bitmap(page); 59 actualiserTabFourmis(mesFourmis); 60 dessinerTabFourmis(page,mesFourmis); 61 blit(page,screen,0,0,0,0,800,600); 62 rest(10); 63 } 64 65 for(int k=0;k<NFOURMI;k++) 66 free(mesFourmis[k]); 67 68 return 0; 69} 70END_OF_MAIN(); 71////////////////////////////////////////////////////////////////////////////////////////// 72 73 74// sous programmes 75 76void aleaDepFourmi(t_fourmi *fourmi){ 77 do{ 78 fourmi->depx=rand()%5-2; 79 }while(fourmi->depx==0); 80 do{ 81 fourmi->depy=rand()%5-2; 82 }while(fourmi->depy==0); 83 fourmi->posx+=fourmi->depx; 84 fourmi->posy+=fourmi->depy; 85} 86 87void creerFourmi(t_fourmi *newfourmi){ 88 89 newfourmi=malloc(sizeof(t_fourmi)); 90 if(newfourmi==NULL){ 91 allegro_message("Marche pas bonhomme"); 92 exit(0); 93 } 94 aleaDepFourmi(newfourmi); 95} 96 97void actualiserFourmi(t_fourmi *fourmi){ 98 if((fourmi->posx=0)||(fourmi->posx=800)) 99 fourmi->depx= -fourmi->depx; 100 if((fourmi->posy=0) || (fourmi->posy=600)) 101 fourmi->depy= -fourmi->depy; 102} 103 104void dessinerFourmi(BITMAP *page, t_fourmi *fourmii){ 105 rectfill(page,TX,TY,fourmii->posx,fourmii->posy,makeacol(255,0,0,0)); 106} 107 108void remplirTabFourmis(t_fourmi *newfourmitab[NFOURMI]){ 109 for(int i=0;i<NFOURMI;i++){ 110 creerFourmi(newfourmitab[i]); 111 } 112} 113 114int collisionFourmis(t_fourmi *f1,t_fourmi *f2){ 115 116 int retour=0; 117 118 int col[4]; 119 col[0]=f1->posx + TX - f2->posx; 120 col[1]=f2->posx + TX - f1->posx; 121 col[2]=f1->posy + TY - f2->posy; 122 col[3]=f2->posy + TY - f1->posy; 123 124 if(col[0]==0 && col[2]==0) 125 retour=1; 126 127 if(col[1]==0 && col[3]==0) 128 retour=1; 129 130 if(f1->posx == f2->posx && f2->posy == f1->posy) 131 retour=0; 132 133 return retour; 134} 135 136void actualiserTabFourmis(t_fourmi *fourmis[NFOURMI]){ 137 int i,j; 138 for(i=0;i<NFOURMI;i++){ 139 actualiserFourmi(fourmis[i]); 140 for(j=0;j<NFOURMI;j++){ 141 collisionFourmis(fourmis[i],fourmis[j]); 142 if(collisionFourmis(fourmis[i],fourmis[j])==1){ 143 aleaDepFourmi(fourmis[i]); 144 aleaDepFourmi(fourmis[j]); 145 } 146 } 147 } 148} 149 150void dessinerTabFourmis(BITMAP *page,t_fourmi *fourmis[NFOURMI]){ 151 for(int i=0;i<NFOURMI;i++) 152 dessinerFourmi(page,fourmis[i]); 153}

Matthew Leverton
Supreme Loser
January 1999
avatar

Does it crash? Do you get any information printed to the console or your IDE?

There is an issue in the actualiserFourmi function. You are using the assignment operator `=` instead of the equality operator `==`. Here's the corrected function:

void actualiserFourmi(t_fourmi *fourmi){
    if((fourmi->posx == 0)||(fourmi->posx == 800))
        fourmi->depx = -fourmi->depx;
    if((fourmi->posy == 0) || (fourmi->posy == 600))
        fourmi->depy = -fourmi->depy;
}

What does TX, TY represent? The rectfill() in dessinerFourmi() looks like it might be wrong.

DanielH
Member #934
January 2001
avatar

In this function:

void remplirTabFourmis(t_fourmi *newfourmitab[NFOURMI]){
    for(int i=0;i<NFOURMI;i++){
        creerFourmi(newfourmitab[i]);
    }
}

newfourmitab[i] is not defined when you pass to creerFourmi. You need to pass the variable, not the value of the variable.

Change creer so you pass a double pointer.

#SelectExpand
1void creerFourmi(t_fourmi **newfourmi){ 2 3 newfourmi=malloc(sizeof(t_fourmi)); 4 if(newfourmi==NULL){ 5 allegro_message("Marche pas bonhomme"); 6 exit(0); 7 } 8 aleaDepFourmi(newfourmi); 9} 10 11void remplirTabFourmis(t_fourmi *newfourmitab[NFOURMI]){ 12 for(int i=0;i<NFOURMI;i++){ 13 creerFourmi(&newfourmitab[i]); 14 } 15}

PapyScotch
Member #23,751
April 2023

I just fixed both mistakes and it still doesn't work with the same error code (0xC0000005) apparently it's a memory leak

Dizzy Egg
Member #10,824
March 2009
avatar

Seems like NULL is being referenced somewhere - can you make sure allegro_init returns 0, and that page isn’t NULL after calling create_bitmap?

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

PapyScotch
Member #23,751
April 2023

Ok so i used these function to make sure allegro_init returns 0 and that page isn't NULL

It returns nothing so ig it's fine ?

#SelectExpand
1 allegro_init(); 2 if(allegro_init()!=0){ 3 allegro_message("allegro_init doesn't work"); 4 exit(EXIT_FAILURE); 5 }

#SelectExpand
1 page=create_bitmap(800,600); 2 if(page==NULL){ 3 allegro_message("create_bitmap doesn't work"); 4 exit(EXIT_FAILURE); 5 }

Dizzy Egg
Member #10,824
March 2009
avatar

Ok, so that is not the problem.

Maybe you need to set pos_x/pos_y and dep_x/dep_y for each fourmi after you create them with MALLOC, as they may have very large values. Also you should check that pos_x/pos_y are <= 0 or >= 800/600 before inverting depx/depy, because they may not be exactly 0/800/600…

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

AceBlkwell
Member #13,038
July 2011
avatar

In my experience, if the graphics window flashed open and closed instantly, I ended up having something not initialize properly. Like Matthew said, I would end up with text response in the console window and/or IDE.

I don't read (or speak) French, assuming there is a desktop_color_depth() are you sure it's returning a valid number for set_color_depth? I would try replacing it with a number like 32 just to see if it works.

Good Luck.

PapyScotch
Member #23,751
April 2023

Well i just fixed the posx/posy initialisation since i simply forgot about it and i didn't initialize it. But still, nothing changed ):
Changed the posx <= or >= too.

I changed the

set_color_depth(desktop_color_depth());

to

set_color_depth(32);

just to try like AceBlkwell suggested but still, nothing changed

DanielH
Member #934
January 2001
avatar

1. You never set the position of the rectangle.

#SelectExpand
1void aleaDepFourmi(t_fourmi *fourmi){ 2 do{ 3 fourmi->depx=rand()%5-2; 4 }while(fourmi->depx==0); 5 do{ 6 fourmi->depy=rand()%5-2; 7 }while(fourmi->depy==0); 8 9 // posx and posy were never set to a value. Could be anything. 10 11 fourmi->posx+=fourmi->depx; 12 fourmi->posy+=fourmi->depy; 13} 14 15void creerFourmi(t_fourmi** newfourmi) { 16 17 *newfourmi = (t_fourmi*)malloc(sizeof(t_fourmi)); 18 if (*newfourmi == NULL) { 19 exit(0); 20 } 21 22 // set a random position in the screen 23 (*newfourmi)->posx = rand() % 800; 24 (*newfourmi)->posy = rand() % 600; 25 26 aleaDepFourmi(*newfourmi); 27}

2. Movement? Never happens

#SelectExpand
1void actualiserFourmi(t_fourmi* fourmi) { 2 if ((fourmi->posx < 0) || (fourmi->posx >= 800)) 3 fourmi->depx = -fourmi->depx; 4 if ((fourmi->posy < 0) && (fourmi->posy >= 600)) 5 fourmi->depy = -fourmi->depy; 6 7// where do you actually move the rectangle. I added this; 8 fourmi->posx += fourmi->depx; 9 fourmi->posy += fourmi->depy; 10} 11 12// or get fancy 13void actualiserFourmi(t_fourmi* fourmi) 14{ 15 if (fourmi->posx < 0) 16 { 17 fourmi->posx = 0 - fourmi->posx; 18 fourmi->depx = -fourmi->depx; 19 } 20 21 if (fourmi->posx >= 800) 22 { 23 fourmi->posx = 800 - (fourmi->posx - 800); 24 fourmi->depx = -fourmi->depx; 25 } 26 27 if (fourmi->posy < 0) 28 { 29 fourmi->posy = 0 - fourmi->posy; 30 fourmi->depy = -fourmi->depy; 31 } 32 33 if (fourmi->posy >= 600) 34 { 35 fourmi->posy = 600 - (fourmi->posy - 600); 36 fourmi->depy = -fourmi->depy; 37 } 38 39 fourmi->posx += fourmi->depx; 40 fourmi->posy += fourmi->depy; 41}

3. Rectangle drawing

//  void rectfill(BITMAP *bmp, int x1, int y1, int x2, int y2, int color); 

// What is TX and TY? Width and Height?
// the function asks for x1 y1, x2 y2 not width/height

rectfill(page,TX,TY,fourmii->posx,fourmii->posy,makeacol(255,0,0,0));

// maybe change to
rectfill(page,fourmii->posx,fourmii->posy, fourmii->posx + TX,fourmii->posy + TY,makeacol(255,0,0,0));

I don't know what they are, just guessing.

4. Collision detection needs work

// simple 2D box collision
int collisionFourmis(t_fourmi* f1, t_fourmi* f2) {

    int retour = 0;

    if (
        f1->posx < f2->posx + TX &&
        f1->posx + TX > f2->posx &&
        f1->posy < f2->posy + TY &&
        f1->posy + TY > f2->posy
        ) retour = 1;

    return retour;
}

It's funny. Because you randomly select new depx/depy when collision. They kind of jiggle when colliding until they break free.

PapyScotch
Member #23,751
April 2023

It's fixed ! The issue was the dynamic allocation that cause a memory leak, now the graphic window open and the program run !

Thanks a lot everyone. I think it's better for me to try to fix this myself now that i can see in action what i write :D

Go to: