Permalink
Browse files

Iterate over quad tree while mutating. I think there's a segfault in …

…this maybe
  • Loading branch information...
1 parent 8278776 commit d53d1ce6c642cf77a8503f42bf75f0521bff8668 @serprex committed Apr 9, 2012
Showing with 58 additions and 52 deletions.
  1. +1 −1 Makefile
  2. +13 −17 ef.c
  3. +36 −22 o.c
  4. +6 −12 o.h
  5. +2 −0 spr.c
View
@@ -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
View
30 ef.c
@@ -23,8 +23,8 @@ void Pupmask(){
if(getbxyi(P->x+6,P->y+i))Pmask.x6|=1<<i;
}
qthit(P);
- for(int i=0;i<P->c->n;i++){
- obj*o=P->c->o[i];
+ for(int i=0;i<P->cn;i++){
+ obj*o=P->c[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+15,o))Pmask.y15|=1<<i;
@@ -41,7 +41,7 @@ int main(int argc,char**argv){
glfwInit();
glfwDisable(GLFW_AUTO_POLL_EVENTS);
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_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
@@ -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);
qtinit();
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);
for(;;){
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);
- //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->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;
+ qtit();
glfwSwapBuffers();
double gT=1./59-glfwGetTime();
if(gT>0&&!glfwGetKey(GLFW_KEY_SPACE))glfwSleep(gT);
View
58 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->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
- for(;qo;qo=qo->o)
- 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);
}
View
18 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;
View
2 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.);
}

0 comments on commit d53d1ce

Please sign in to comment.