Permalink
Browse files

Convert y coordinates to float. No more Px/Py

  • Loading branch information...
1 parent f707251 commit 78632474f94c8deb68b23c6233191146628e3004 @serprex committed Mar 27, 2012
Showing with 52 additions and 59 deletions.
  1. +33 −44 ef.c
  2. +5 −1 ef.h
  3. +8 −9 o.c
  4. +6 −5 o.h
View
77 ef.c
@@ -1,32 +1,26 @@
#include "ef.h"
-#define LEN(x) (sizeof(x)/sizeof((x)[0]))
+#define LEN(x) (sizeof(x)/sizeof(*(x)))
#define SQR(x) ((x)*(x))
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)>(y)?(y):(x))
-#define case(x) break;case x:
-#define else(x) else if(x)
-double Wy=0,Py=900,Pya=0,Pj=-1;
-int Wx=0,Px=1800,t=0,Pd=1,Pjd=0,Pcrawl;
+float Wy=0,Pya=0,Pj=-1;
+int Wx=0,t=0,Pd=1,Pjd=0,Pcrawl;
obj*P;
-struct Pmask{
+struct{
union{uint16_t x0;struct{uint8_t x0l,x0h;};};
union{uint16_t x6;struct{uint8_t x6l,x6h;};};
uint8_t y0,y15,y16;
}Pmask;
-obj*RR;
void Pupmask(){
- int px=P->x,py=P->y;
- P->x=Px;
- P->y=ceil(Py);
memset(&Pmask,0,sizeof(Pmask));
for(int i=0;i<6;i++){
- if(getbxyi(P->x+i,P->y))Pmask.y0|=1<<i;
- if(getbxyi(P->x+i,P->y+15))Pmask.y15|=1<<i;
- if(getbxyi(P->x+i,P->y+16))Pmask.y16|=1<<i;
+ if(getbxy(P->x+i,P->y))Pmask.y0|=1<<i;
+ if(getbxy(P->x+i,P->y+15))Pmask.y15|=1<<i;
+ if(getbxy(P->x+i,P->y+16))Pmask.y16|=1<<i;
}
for(int i=0;i<16;i++){
- if(getbxyi(P->x,P->y+i))Pmask.x0|=1<<i;
- if(getbxyi(P->x+6,P->y+i))Pmask.x6|=1<<i;
+ if(getbxy(P->x,P->y+i))Pmask.x0|=1<<i;
+ if(getbxy(P->x+6,P->y+i))Pmask.x6|=1<<i;
}
qthit(P);
for(int i=0;i<P->c->n;i++){
@@ -41,9 +35,6 @@ void Pupmask(){
if(pino(P->x+6,P->y+i,o))Pmask.x6|=1<<i;
}
}
- P->x=px;
- P->y=py;
- qthit(P);
}
int main(int argc,char**argv){
glfwInit();
@@ -60,8 +51,8 @@ int main(int argc,char**argv){
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2048,2048,0,GL_RGBA,GL_UNSIGNED_BYTE,S);
qtinit();
- P=omake(0,Man,Px,Py,6,16);
- obj*R=RR=omake(0,RClean,1700,900,8,8);
+ P=omake(0,Man,1800,900,6,16);
+ obj*R=omake(0,RClean,1700,900,8,8);
srand(glfwGetTime()*10e5);
for(;;){
t++;
@@ -70,72 +61,70 @@ int main(int argc,char**argv){
R->y++;
qtmove(R);
drawSpr(RClean,R->x-Wx,R->y-Wy,!(t&16),0);
- double oPy=Py;
- int oPx=Px;
- Px+=glfwGetKey(GLFW_KEY_RIGHT)-glfwGetKey(GLFW_KEY_LEFT);
+ float oPy=P->y;
+ int oPx=P->x;
+ P->x+=glfwGetKey(GLFW_KEY_RIGHT)-glfwGetKey(GLFW_KEY_LEFT);
Pupmask();
- if(oPx!=Px){
- Pd=Px>oPx;
+ if(oPx!=P->x){
+ Pd=P->x>oPx;
int xm=Pd?Pmask.x6:Pmask.x0;
if(!(xm&15)&&xm&0xFFF)Pya=fmin(Pya,-1);
}
Pcrawl=!Pya&&(Pmask.x0l&&!Pmask.x0h||Pmask.x6l&&!Pmask.x6h);
while(Pcrawl?Pmask.x0h:Pmask.x0){
- Px++;
+ P->x++;
Pupmask();
}
while(Pcrawl?Pmask.x6h:Pmask.x6){
- Px--;
+ P->x--;
Pupmask();
}
if(Pcrawl){
- if(t&1)Px=oPx;
- drawSpr(ManCrawl,Px-Wx,Py-Wy+10,!(t&32),Pd);
+ if(t&1)P->x=oPx;
+ drawSpr(ManCrawl,P->x-Wx,P->y-Wy+10,!(t&32),Pd);
}else{
Pjd=0;
if(glfwGetKey(GLFW_KEY_UP)&&Pmask.y16){
Pj=fmax(Pj-.125,-3);
Pjd=1;
}
if(Pj<-1){
- if(Pjd)Px=oPx;
+ if(Pjd)P->x=oPx;
else{
Pya=Pj;
Pj=-1;
}
}
Pya=fmin(Pya+.09375,3);
- Py+=Pya;
+ P->y+=Pya;
Pupmask();
if(Pya>0&&Pmask.y15){
Pya=0;
- Py=ceil(Py);
- do Pupmask(); while(Pmask.y15&&(Py--,1));
+ P->y=ceil(P->y);
+ do Pupmask(); while(Pmask.y15&&(P->y--,1));
}
if(Pya<0&&Pmask.y0){
Pya=0;
- Py=floor(Py);
- do Pupmask(); while(Pmask.y0&&(Py++,1));
+ P->y=floor(P->y);
+ do Pupmask(); while(Pmask.y0&&(P->y++,1));
}
- drawSpr(Man,Px-Wx,Py-Wy,Pya>1.125?4:Pj<-1?3:oPx==Px?0:1+!(t&32),Pd);
+ drawSpr(Man,P->x-Wx,P->y-Wy,Pya>1.125?4:Pj<-1?3:oPx==P->x?0:1+!(t&32),Pd);
}
- P->x=Px;
- P->y=ceil(Py);
qtmove(P);
- qtdraw(Wx,Wy);
- if(Px<Wx+512-128)Wx=Px-512+128;
- else if(Px>Wx+512+128)Wx=Px-512-128;
+ 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 if(Wx>2048*8)Wx=2048*8;
- Wy=Py-160;
+ else(Wx>2048*8)Wx=2048*8;
+ Wy=P->y-160;
glfwSwapBuffers();
double gT=1./59-glfwGetTime();
if(gT>0&&!glfwGetKey(GLFW_KEY_SPACE))glfwSleep(gT);
else printf("%f\n",-gT);
glfwSetTime(0);
glfwPollEvents();
if(glfwGetKey(GLFW_KEY_ESC)||!glfwGetWindowParam(GLFW_OPENED)){
- printf("%f %f\n",Px,Py);
+ printf("%d %f\n",P->x,P->y);
return 0;
}
}
View
6 ef.h
@@ -10,4 +10,8 @@
#include "o.h"
static inline int nthbit(int x,int n){
return x&(1<<n);
-}
+}
+extern float Wy;
+extern int t,Wx;
+#define case(x) break;case x:
+#define else(x) else if(x)
View
17 o.c
@@ -6,7 +6,7 @@ int getbxyi(int x,int y)
y>>=3;
return mapB[(2047-y)*256+(x>>3)]&1<<(x&7);
}
-int getbxy(int x,double dy)
+int getbxy(int x,float dy)
{
return getbxyi(x,ceil(dy));
}
@@ -43,7 +43,7 @@ static qtree*qtmake(qtree*p,int x,int y,int w){
void qtinit(){
qroot=qtmake(0,0,0,8192);
}
-void qtdraw_(qtree*q,int Wx,int Wy){
+void qtdraw_(qtree*q){
glBegin(GL_LINES);
glVertex2f(q->x+q->w-Wx,q->y-Wy);
glVertex2f(q->x+q->w-Wx,q->y+q->w*2-Wy);
@@ -55,11 +55,11 @@ void qtdraw_(qtree*q,int Wx,int Wy){
glVertex2f(q->x-Wx,q->y+q->w*2-Wy);
glEnd();
for(int i=0;i<4;i++)
- if(q->q[i])qtdraw_(q->q[i],Wx,Wy);
+ if(q->q[i])qtdraw_(q->q[i]);
}
void qtdraw(int Wx,int Wy){
glDisable(GL_TEXTURE_2D);
- qtdraw_(qroot,Wx,Wy);
+ qtdraw_(qroot);
glEnable(GL_TEXTURE_2D);
}
static void qthit_(qtree*q,obj*o){
@@ -78,16 +78,16 @@ void qthit(obj*o){
}
static void qtadd_(qtree*q,obj*o){
unsigned xw=q->x+q->w,yh=q->y+q->w;
- if(q->w==1||o->x<=xw&&o->x+o->w>xw||o->y<=yh&&o->y+o->h>yh){
+ if(q->w==64||o->x<=xw&&o->x+o->w>xw||o->y<=yh&&o->y+o->h>yh){
o->o=q->o;
q->o=o;
o->q=q;
return;
}
int qid;
if(o->x<=xw&&o->y<=yh)qid=0;
- else if(o->x>xw&&o->y<=yh)qid=1;
- else if(o->x<=xw&&o->y>yh)qid=2;
+ else(o->x>xw&&o->y<=yh)qid=1;
+ else(o->x<=xw&&o->y>yh)qid=2;
else qid=3;
if(!q->q[qid])q->q[qid]=qtmake(q,qid==0||qid==2?q->x:xw,qid==0||qid==1?q->y:yh,q->w/2);
qtadd_(q->q[qid],o);
@@ -99,10 +99,9 @@ void qtrm(obj*o){
if(o->q->o==o)o->q->o=o->o;
else orm(o->q->o,o);
}
-obj*omake(int sz,int t,int x,int 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);
o->t=t;
- o->s=sz;
o->x=x;
o->y=y;
o->w=w;
View
11 o.h
@@ -1,8 +1,9 @@
struct jray;
struct qtree;
typedef struct obj{
- uint8_t t,s,w,h;
- uint16_t x,y;
+ uint8_t t,w,h;
+ uint16_t x;
+ float y;
struct qtree*q;
struct jray*c;
struct obj*o;
@@ -19,13 +20,13 @@ typedef struct jray{
obj*o[];
}jray;
int getbxyi(int x,int y);
-int getbxy(int x,double dy);
-obj*omake(int sz,int t,int x,int y,int w,int h);
+int getbxy(int x,float dy);
+obj*omake(int sz,int t,int x,float y,int w,int h);
int ohit(obj*a,obj*b);
void qtmove(obj*o);
void qtinit();
void qthit(obj*o);
-void qtdraw(int,int);
+void qtdraw();
static inline int pino(int x,int y,obj*o){
return x>=o->x&&x<=o->x+o->w&&y>=o->y&&y<=o->y+o->h;
}

0 comments on commit 7863247

Please sign in to comment.