Skip to content

Commit

Permalink
Iterate over quad tree while mutating. I think there's a segfault in …
Browse files Browse the repository at this point in the history
…this maybe
  • Loading branch information
serprex committed Apr 9, 2012
1 parent 8278776 commit d53d1ce
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 52 deletions.
2 changes: 1 addition & 1 deletion 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 O=tgen.o bgen.o spr.o o.o ef.o
all: ${O} all: ${O}
${CC} ${O} -o ElectronicFunk -fwhole-program -lglfw -lGL -lm ${CC} ${O} -o ElectronicFunk -fwhole-program -lglfw -lGL -lm
Expand Down
30 changes: 13 additions & 17 deletions ef.c
Expand Up @@ -23,8 +23,8 @@ void Pupmask(){
if(getbxyi(P->x+6,P->y+i))Pmask.x6|=1<<i; if(getbxyi(P->x+6,P->y+i))Pmask.x6|=1<<i;
} }
qthit(P); qthit(P);
for(int i=0;i<P->c->n;i++){ for(int i=0;i<P->cn;i++){
obj*o=P->c->o[i]; obj*o=P->c[i];
for(int i=0;i<6;i++){ for(int i=0;i<6;i++){
if(pino(P->x+i,P->y,o))Pmask.y0|=1<<i; if(pino(P->x+i,P->y,o))Pmask.y0|=1<<i;
if(pino(P->x+i,P->y+15,o))Pmask.y15|=1<<i; if(pino(P->x+i,P->y+15,o))Pmask.y15|=1<<i;
Expand All @@ -41,7 +41,7 @@ int main(int argc,char**argv){
glfwInit(); glfwInit();
glfwDisable(GLFW_AUTO_POLL_EVENTS); glfwDisable(GLFW_AUTO_POLL_EVENTS);
glfwOpenWindow(1024,256,0,0,0,0,0,0,GLFW_WINDOW); glfwOpenWindow(1024,256,0,0,0,0,0,0,GLFW_WINDOW);
glOrtho(0,1024,256,0,1,-1); glOrtho(0,1024,256,0,1,0);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
Expand All @@ -53,15 +53,17 @@ int main(int argc,char**argv){
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2048,2048,0,GL_RGBA,GL_UNSIGNED_BYTE,S); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2048,2048,0,GL_RGBA,GL_UNSIGNED_BYTE,S);
qtinit(); qtinit();
P=omake(0,Man,1800,900,6,16); P=omake(0,Man,1800,900,6,16);
obj*R=omake(0,RClean,1700,900,8,8); omake(0,RClean,1700,900,8,8);
srand(glfwGetTime()*10e5); srand(glfwGetTime()*10e5);
for(;;){ for(;;){
t++; t++;
if(P->x<Wx+512-128)Wx=P->x-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); drawRect(0,0,1024,256,Wx/16384.,Wy/16384.,1./16,1./64);
//R->x++; qtdraw();
//R->y++;
//qtmove(R);
drawSpr(RClean,R->x-Wx,R->y-Wy,!(t&16),0);
float oPy=P->y; float oPy=P->y;
int oPx=P->x; int oPx=P->x;
P->x+=glfwGetKey(GLFW_KEY_RIGHT)-glfwGetKey(GLFW_KEY_LEFT); P->x+=glfwGetKey(GLFW_KEY_RIGHT)-glfwGetKey(GLFW_KEY_LEFT);
Expand Down Expand Up @@ -103,21 +105,15 @@ int main(int argc,char**argv){
Pya=0; Pya=0;
P->y=ceil(P->y); P->y=ceil(P->y);
do Pupmask(); while(Pmask.y15&&(P->y--,1)); do Pupmask(); while(Pmask.y15&&(P->y--,1));
} }else if(Pya<0&&Pmask.y0){
if(Pya<0&&Pmask.y0){
Pya=0; Pya=0;
P->y=floor(P->y); P->y=floor(P->y);
do Pupmask(); while(Pmask.y0&&(P->y++,1)); 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); qtmove(P);
qtdraw(); qtit();
if(P->x<Wx+512-128)Wx=P->x-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;
glfwSwapBuffers(); glfwSwapBuffers();
double gT=1./59-glfwGetTime(); double gT=1./59-glfwGetTime();
if(gT>0&&!glfwGetKey(GLFW_KEY_SPACE))glfwSleep(gT); if(gT>0&&!glfwGetKey(GLFW_KEY_SPACE))glfwSleep(gT);
Expand Down
58 changes: 36 additions & 22 deletions o.c
Expand Up @@ -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; 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->x<bx2&&a->y>b->y&&a->y<by2||b->x>a->x&&b->x<ax2&&b->y>a->y&&b->y<ay2); 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->x<bx2&&a->y>b->y&&a->y<by2||b->x>a->x&&b->x<ax2&&b->y>a->y&&b->y<ay2);
} }
void orm(obj*qo,obj*o){//UNSAFE static void jadd(obj*o,obj*x){
for(;qo;qo=qo->o) o->c=realloc(o->c,++o->cn*sizeof(obj*));
if(qo->o==o){ o->c[o->cn-1]=x;
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 qtree*qtmake(qtree*p,int x,int y,int w){ static qtree*qtmake(qtree*p,int x,int y,int w){
qtree*q=calloc(1,sizeof(qtree)); qtree*q=calloc(1,sizeof(qtree));
Expand Down Expand Up @@ -76,15 +62,16 @@ void qtdraw(int Wx,int Wy){
static void qthit_(qtree*q,obj*o){ static void qthit_(qtree*q,obj*o){
unsigned xw=q->x+q->w,yh=q->y+q->w; unsigned xw=q->x+q->w,yh=q->y+q->w;
for(obj*qo=q->o;qo;qo=qo->o) 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<=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+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<=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); if(o->x+o->w>xw&&o->y+o->h>yh&&q->q[3])qthit_(q->q[3],o);
} }
void qthit(obj*o){ void qthit(obj*o){
o->c=realloc(o->c,sizeof(jray)); free(o->c);
o->c->n=0; o->c=0;
o->cn=0;
qthit_(qroot,o); qthit_(qroot,o);
} }
static void qtadd_(qtree*q,obj*o){ static void qtadd_(qtree*q,obj*o){
Expand All @@ -108,17 +95,23 @@ void qtadd(obj*o){
} }
void qtrm(obj*o){ void qtrm(obj*o){
if(o->q->o==o)o->q->o=o->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*omake(int sz,int t,int x,float y,int w,int h){
obj*o=malloc(sizeof(obj)+sz); obj*o=malloc(sizeof(obj)+sz);
o->t=t; o->t=t;
o->T=t&1;
o->x=x; o->x=x;
o->cn=0;
o->y=y; o->y=y;
o->w=w; o->w=w;
o->h=h; o->h=h;
o->o=0; o->o=0;
o->c=calloc(1,sizeof(jray)); o->c=0;
memset(&o->d,0,sz); memset(&o->d,0,sz);
qtadd(o); qtadd(o);
return o; return o;
Expand All @@ -145,4 +138,25 @@ void qtmove(obj*o){
qtaddhint(q,o); qtaddhint(q,o);
qtgchint(q); qtgchint(q);
qthit(o); 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);
} }
18 changes: 6 additions & 12 deletions o.h
@@ -1,24 +1,17 @@
struct jray;
struct qtree; struct qtree;
typedef struct obj{ typedef struct obj{
uint8_t t,w,h;
uint16_t x;
float y;
struct qtree*q; struct qtree*q;
struct jray*c; struct obj*o,**c;
struct obj*o; uint8_t t,T,w,h;
uint16_t x,cn;
float y;
uint8_t d[]; uint8_t d[];
}obj; }obj;
typedef struct qtree{ typedef struct qtree{
struct qtree*p; struct qtree*p,*q[4];
struct qtree*q[4];
obj*o; obj*o;
uint16_t x,y,w; uint16_t x,y,w;
}qtree; }qtree;
typedef struct jray{
int n;
obj*o[];
}jray;
int getbx8y(int x,int y); int getbx8y(int x,int y);
int getbxyi(int x,int y); int getbxyi(int x,int y);
int getbxy(int x,float dy); int getbxy(int x,float dy);
Expand All @@ -27,6 +20,7 @@ int ohit(obj*a,obj*b);
void qtmove(obj*o); void qtmove(obj*o);
void qtinit(); void qtinit();
void qthit(obj*o); void qthit(obj*o);
void qtit();
void qtdraw(); void qtdraw();
static inline int pino(int x,float y,obj*o){ 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; return x>=o->x&&x<=o->x+o->w&&y>=o->y&&y<=o->y+o->h;
Expand Down
2 changes: 2 additions & 0 deletions spr.c
Expand Up @@ -24,5 +24,7 @@ void drawRect(int x,int y,int w,int h,float tx,float ty,float tw,float th){
glEnd(); glEnd();
} }
void drawSpr(sprid s,int x,int y,int f,int h){ 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.); 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.);
} }

0 comments on commit d53d1ce

Please sign in to comment.