From d53d1ce6c642cf77a8503f42bf75f0521bff8668 Mon Sep 17 00:00:00 2001 From: Demur Rumed Date: Mon, 9 Apr 2012 02:03:56 +0000 Subject: [PATCH] Iterate over quad tree while mutating. I think there's a segfault in this maybe --- Makefile | 2 +- ef.c | 30 +++++++++++++---------------- o.c | 58 +++++++++++++++++++++++++++++++++++--------------------- o.h | 18 ++++++------------ spr.c | 2 ++ 5 files changed, 58 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index 681d1da..738002c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CC=gcc -std=gnu99 -Ofast -s -pipe +CC=gcc -std=gnu99 -Ofast -g -pipe O=tgen.o bgen.o spr.o o.o ef.o all: ${O} ${CC} ${O} -o ElectronicFunk -fwhole-program -lglfw -lGL -lm diff --git a/ef.c b/ef.c index 6c7cf06..4f1774c 100644 --- a/ef.c +++ b/ef.c @@ -23,8 +23,8 @@ void Pupmask(){ if(getbxyi(P->x+6,P->y+i))Pmask.x6|=1<c->n;i++){ - obj*o=P->c->o[i]; + for(int i=0;icn;i++){ + obj*o=P->c[i]; for(int i=0;i<6;i++){ if(pino(P->x+i,P->y,o))Pmask.y0|=1<x+i,P->y+15,o))Pmask.y15|=1<xx-512+128; + else(P->x>Wx+512+128)Wx=P->x-512-128; + if(Wx<0)Wx=0; + else(Wx>2048*8)Wx=2048*8; + Wy=P->y-160; drawRect(0,0,1024,256,Wx/16384.,Wy/16384.,1./16,1./64); - //R->x++; - //R->y++; - //qtmove(R); - drawSpr(RClean,R->x-Wx,R->y-Wy,!(t&16),0); + qtdraw(); float oPy=P->y; int oPx=P->x; P->x+=glfwGetKey(GLFW_KEY_RIGHT)-glfwGetKey(GLFW_KEY_LEFT); @@ -103,21 +105,15 @@ int main(int argc,char**argv){ Pya=0; P->y=ceil(P->y); do Pupmask(); while(Pmask.y15&&(P->y--,1)); - } - if(Pya<0&&Pmask.y0){ + }else if(Pya<0&&Pmask.y0){ Pya=0; P->y=floor(P->y); do Pupmask(); while(Pmask.y0&&(P->y++,1)); } - drawSpr(Man,P->x-Wx,P->y-Wy,Pya>1.125?4:Pj<-1?3:oPx==P->x?0:1+!(t&32),Pd); + drawSpr(Man,P->x,P->y,Pya>1.125?4:Pj<-1?3:oPx==P->x?0:1+!(t&32),Pd); } qtmove(P); - qtdraw(); - if(P->xx-512+128; - else(P->x>Wx+512+128)Wx=P->x-512-128; - if(Wx<0)Wx=0; - else(Wx>2048*8)Wx=2048*8; - Wy=P->y-160; + qtit(); glfwSwapBuffers(); double gT=1./59-glfwGetTime(); if(gT>0&&!glfwGetKey(GLFW_KEY_SPACE))glfwSleep(gT); diff --git a/o.c b/o.c index f7b493e..7b65603 100644 --- a/o.c +++ b/o.c @@ -25,23 +25,9 @@ int ohit(obj*a,obj*b){ int ax2=a->x+a->w,ay2=a->y+a->h,bx2=b->x+b->w,by2=b->y+b->h; return a!=b&&(pino(a->x,a->y,b)||pino(ax2,a->y,b)||pino(a->x,ay2,b)||pino(ax2,ay2,b)||pino(b->x,b->y,a)||pino(bx2,b->y,a)||pino(b->x,by2,a)||pino(bx2,by2,a)||a->x>b->x&&a->xy>b->y&&a->yx>a->x&&b->xy>a->y&&b->yo) - if(qo->o==o){ - qo->o=o->o; - break; - } -} -void jadd(jray**l,obj*o){ - jray*ll; - if(*l){ - (*l)->n++; - ll=*l=realloc(*l,sizeof(jray)+(*l)->n*sizeof(obj*)); - }else{ - ll=*l=malloc(sizeof(jray)+sizeof(obj*)); - ll->n=1; - } - ll->o[ll->n-1]=o; +static void jadd(obj*o,obj*x){ + o->c=realloc(o->c,++o->cn*sizeof(obj*)); + o->c[o->cn-1]=x; } static qtree*qtmake(qtree*p,int x,int y,int w){ qtree*q=calloc(1,sizeof(qtree)); @@ -76,15 +62,16 @@ void qtdraw(int Wx,int Wy){ static void qthit_(qtree*q,obj*o){ unsigned xw=q->x+q->w,yh=q->y+q->w; for(obj*qo=q->o;qo;qo=qo->o) - if(ohit(o,qo))jadd(&o->c,qo); + if(ohit(o,qo))jadd(o,qo); if(o->x<=xw&&o->y<=yh&&q->q[0])qthit_(q->q[0],o); if(o->x+o->w>xw&&o->y<=yh&&q->q[1])qthit_(q->q[1],o); if(o->x<=xw&&o->y+o->h>yh&&q->q[2])qthit_(q->q[2],o); if(o->x+o->w>xw&&o->y+o->h>yh&&q->q[3])qthit_(q->q[3],o); } void qthit(obj*o){ - o->c=realloc(o->c,sizeof(jray)); - o->c->n=0; + free(o->c); + o->c=0; + o->cn=0; qthit_(qroot,o); } static void qtadd_(qtree*q,obj*o){ @@ -108,17 +95,23 @@ void qtadd(obj*o){ } void qtrm(obj*o){ if(o->q->o==o)o->q->o=o->o; - else orm(o->q->o,o); + else{ + obj*qo=o->q->o; + while(qo->o!=o)qo=qo->o; + qo->o=o->o; + } } obj*omake(int sz,int t,int x,float y,int w,int h){ obj*o=malloc(sizeof(obj)+sz); o->t=t; + o->T=t&1; o->x=x; + o->cn=0; o->y=y; o->w=w; o->h=h; o->o=0; - o->c=calloc(1,sizeof(jray)); + o->c=0; memset(&o->d,0,sz); qtadd(o); return o; @@ -145,4 +138,25 @@ void qtmove(obj*o){ qtaddhint(q,o); qtgchint(q); qthit(o); +} +void qtit_(qtree*q){ + for(int i=0;i<4;i++) + if(q->q[i])qtit_(q->q[i]); + for(obj*o=q->o,*no;o;o=no){ + if(o->T==(t&1))return; + o->T=t&1; + no=o->o; + switch(o->t){ + case(RClean) + if(o->x<16000&&o->y<16000){ + o->x++; + o->y++; + qtmove(o); + } + drawSpr(RClean,o->x,o->y,!(t&16),0); + } + } +} +void qtit(){ + qtit_(qroot); } \ No newline at end of file diff --git a/o.h b/o.h index 8d1836e..d96a432 100644 --- a/o.h +++ b/o.h @@ -1,24 +1,17 @@ -struct jray; struct qtree; typedef struct obj{ - uint8_t t,w,h; - uint16_t x; - float y; struct qtree*q; - struct jray*c; - struct obj*o; + struct obj*o,**c; + uint8_t t,T,w,h; + uint16_t x,cn; + float y; uint8_t d[]; }obj; typedef struct qtree{ - struct qtree*p; - struct qtree*q[4]; + struct qtree*p,*q[4]; obj*o; uint16_t x,y,w; }qtree; -typedef struct jray{ - int n; - obj*o[]; -}jray; int getbx8y(int x,int y); int getbxyi(int x,int y); int getbxy(int x,float dy); @@ -27,6 +20,7 @@ int ohit(obj*a,obj*b); void qtmove(obj*o); void qtinit(); void qthit(obj*o); +void qtit(); void qtdraw(); static inline int pino(int x,float y,obj*o){ return x>=o->x&&x<=o->x+o->w&&y>=o->y&&y<=o->y+o->h; diff --git a/spr.c b/spr.c index cec09ee..745c2de 100644 --- a/spr.c +++ b/spr.c @@ -24,5 +24,7 @@ void drawRect(int x,int y,int w,int h,float tx,float ty,float tw,float th){ glEnd(); } void drawSpr(sprid s,int x,int y,int f,int h){ + x-=Wx; + y-=Wy; drawRect(h?x:x+spr[s].w,y,spr[s].w*(h?1:-1),spr[s].h,spr[s].x/2048.+spr[s].w*f/2048.,spr[s].y/2048.,spr[s].w/2048.,spr[s].h/2048.); } \ No newline at end of file