boxes

Tracer des diagrammes dans un terminal - retour accueil

git clone git://bebou.netlib.re/boxes

Log | Files | Refs | README |

commit 4524a6b416a91584cee2250c76749a7242ab95aa
parent 1a199e8a17a9642b6e83527806a845caebe1c58e
Auteurice: Arthur Pons <arthur.pons@unistra.fr>
Date:   Fri, 13 Jun 2025 22:03:56 +0200

Création structure de pile

Permet de facto un peu
Aussi quand on suppr avec clique droit on s'arrête à la première boite
que l'on trouve (sinon on suppr toutes les boites dont l'origine est la
même)

Diffstat:
Mboxes.c | 33+++++++++++++++++----------------
1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/boxes.c b/boxes.c @@ -7,6 +7,7 @@ int max(int a, int b) { return a > b ? a : b; } int min(int a, int b) { return a < b ? a : b; } struct box { int x; int y; int h; int w; }; +struct pile { struct box* list; int count; }; void drawboxcoord(int x, int y, int w, int h) { if(w<0) { x=x+w; w=-w; } @@ -32,13 +33,14 @@ struct box createbox(int x, int y, int w, int h) { struct box b; b.x=x; b.y=y; b.w=w; b.h=h; return b; } -void addbox(struct box* boxes, struct box b, int boxescount) { - boxes[boxescount]=b; +void addbox(struct pile *boxes, struct box b) { + boxes->list[boxes->count]=b; + boxes->count++; } -int deletebox(struct box* boxes, int index, int boxescount) { - for (int i=index;i<boxescount-1;i++) boxes[i]=boxes[i+1]; - return boxescount-1; +void deletebox(struct pile *boxes, int index) { + for (int i=index;i<boxes->count-1;i++) boxes->list[i]=boxes->list[i+1]; + boxes->count--; } int main(int argc, char **argv) { @@ -46,12 +48,12 @@ int main(int argc, char **argv) { tb_hide_cursor(); tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); struct tb_event ev; - struct box boxes[MAX_BOX_NB]; int boxescount=0; + struct pile boxes; boxes.list=malloc(MAX_BOX_NB); boxes.count=0; int curx; int cury; while(1) { tb_clear(); - for (int i=0;i<boxescount;i++) drawbox(boxes[i]); - tb_printf(tb_height(),0,TB_DEFAULT,TB_DEFAULT,"count: %d",boxescount); + for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); + tb_printf(tb_height(),0,TB_DEFAULT,TB_DEFAULT,"count: %d",boxes.count); tb_present(); tb_poll_event(&ev); switch(ev.key) { @@ -59,25 +61,24 @@ int main(int argc, char **argv) { tb_shutdown(); return 0; case 65512: /*MouseLeft*/ - if(boxescount==MAX_BOX_NB) break; + if(boxes.count==MAX_BOX_NB) break; curx=ev.x; cury=ev.y; while(ev.key!=65509) { /*MouseRelease*/ tb_clear(); - for (int i=0;i<boxescount;i++) drawbox(boxes[i]); + for (int i=0;i<boxes.count;i++) drawbox(boxes.list[i]); drawboxcoord(curx,cury,ev.x-curx,ev.y-cury); tb_present(); tb_poll_event(&ev); } - struct box b=createbox(curx,cury,ev.x-curx,ev.y-cury); - addbox(boxes,b,boxescount); boxescount++; + addbox(&boxes,createbox(curx,cury,ev.x-curx,ev.y-cury)); break; case 26: /*ctrl+z*/ - boxescount=deletebox(boxes,boxescount-1,boxescount); + deletebox(&boxes,boxes.count-1); break; case 65511: /*MouseRight*/ - for(int i=0;i<boxescount;i++) - if(ev.x==boxes[i].x && ev.y==boxes[i].y) { - boxescount=deletebox(boxes,i,boxescount); + for(int i=0;i<boxes.count;i++) + if(ev.x==boxes.list[i].x && ev.y==boxes.list[i].y) { + deletebox(&boxes,i); break; } break;