static int NDS_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect) { NDS_RenderData *data = (NDS_RenderData *) renderer->driverdata; NDS_TextureData *txdat = (NDS_TextureData *) texture->driverdata; int dest_y; if (data->is_sub) { dest_y = dstrect->y; } else { dest_y = dstrect->y-SCREEN_HEIGHT; } if (texture->w == dstrect->w && texture->h == dstrect->h) { /* No scaling */ glSprite(dstrect->x, dest_y, GL_FLIP_NONE, txdat->image); } else { /* Convert the scaling proportion into a 20.12 value. */ s32 scale_w = divf32(dstrect->w << 12, texture->w << 12); s32 scale_h = divf32(dstrect->h << 12, texture->h << 12); glSpriteScaleXY(dstrect->x, dest_y, scale_w, scale_h, GL_FLIP_NONE, txdat->image); } return 0; }
void Quaternion_normalize(Quaternion *quaternion) { int32 magnitude; magnitude = sqrtf32(mulf32(quaternion->x, quaternion->x) + mulf32(quaternion->y, quaternion->y) + mulf32(quaternion->z, quaternion->z) + mulf32(quaternion->w, quaternion->w)); quaternion->x = divf32(quaternion->x, magnitude); quaternion->y = divf32(quaternion->y, magnitude); quaternion->z = divf32(quaternion->z, magnitude); quaternion->w = divf32(quaternion->w, magnitude); }
void Bar::setValue(unsigned int val) { hidden = val <= 0; position.first = initX + (val >> 2); sx = divf32(1 << 12, val << 9); sy = 1 << 8; update(); }
vect3D intersectSegmentPlane(plane_struct* pl, vect3D o, vect3D v, int32 d) { if(!pl)return o; vect3D n=vect(pl->A,pl->B,pl->C); int32 p1=dotProduct(v,n); vect3D p=pl->point; int32 p2=dotProduct(vectDifference(p,o),n); int32 k=divf32(p2,p1); return addVect(o,vectMult(v,k)); }
bool collideLinePlane(vect3D p, vect3D n, vect3D o, vect3D v, vect3D* ip) { int32 p1=dotProduct(v,n); if(!equals(p1,0)) { int32 p2=dotProduct(vectDifference(p,o),n); int32 k=divf32(p2,p1); if(ip)*ip=addVect(o,vectMult(v,k)); return true; } return false; }
//--------------------------------------------------------------------------------- // Draws a 3x3 side of the rubiks cube. as with DrawTile, assumes that all rotation // has already been performed. //--------------------------------------------------------------------------------- void RubiksCube::DrawSide(RubikSide side, int sideNum, int32 x, int32 y, int32 z, int32 size, bool picking) { //--------------------------------------------------------------------------------- int color; // generate an int to signify tile colour (see enum RC_Color) glBegin(GL_QUADS); //start drawin squares for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { color = side.tile[i][j].color; //get this tile's colour if(picking) startCheck(); // if picking... DrawTile( color, x + mulf32(inttof32(i),divf32(size,inttof32(3))), y + mulf32(inttof32(j),divf32(size,inttof32(3))), z, divf32(size,inttof32(3)), picking); // draw the tile at (i,j) if(picking) endCheck(sideNum, i, j); // if picking... } } glEnd(); // stop drawin squares }
vect3D projectPoint(camera_struct* c, vect3D p) { if(!c)c=&playerCamera; vect3D v; int32* m=c->viewMatrix; v.x=mulf32(p.x,m[0])+mulf32(p.y,m[4])+mulf32(p.z,m[8])+m[12]; v.y=mulf32(p.x,m[1])+mulf32(p.y,m[5])+mulf32(p.z,m[9])+m[13]; // v.z=mulf32(p.x,m[2])+mulf32(p.y,m[6])+mulf32(p.z,m[10])+m[14]; int32 w=mulf32(p.x,m[3])+mulf32(p.y,m[7])+mulf32(p.z,m[11])+m[15]; // GBATEK // screen_x = (xx+ww)*viewport_width / (2*ww) + viewport_x1 // screen_y = (yy+ww)*viewport_height / (2*ww) + viewport_y1 v.x=divf32(v.x+(w),(w)*2)*128/2048; v.y=192-divf32(v.y+(w),(w)*2)*96/2048; v.z=0; // v.z=divf32(v.z+(w),(w)*2); return v; }
bool collideLineRectangle(rectangle_struct* rec, vect3D o, vect3D v, int32 d, int32* kk, vect3D* ip) { if(!rec)return false; vect3D n=vect(abs(rec->normal.x),abs(rec->normal.y),abs(rec->normal.z)); int32 p1=dotProduct(v,n); if(!equals(p1,0)) { vect3D p=convertVect(rec->position); //CHECK lightmap generation ? vect3D s=vect(rec->size.x*TILESIZE*2,rec->size.y*HEIGHTUNIT,rec->size.z*TILESIZE*2); int32 p2=dotProduct(vectDifference(p,o),n); int32 k=divf32(p2,p1); s8 sign=((s.x>0)^(s.y<0)^(s.z>0)^(p1<0))?(-1):(1); if(kk) { *kk=k+sign; } if(k<0 || k>d){return false;} vect3D i=addVect(o,vectMult(v,k)); if(ip)*ip=i; i=vectDifference(i,p); bool r=true; if(s.x) { if(s.x>0)r=r&&i.x<s.x&&i.x>=0; else r=r&&i.x>s.x&&i.x<=0; } if(s.y) { if(s.y>0)r=r&&i.y<s.y&&i.y>=0; else r=r&&i.y>s.y&&i.y<=0; } if(s.z) { if(s.z>0)r=r&&i.z<s.z&&i.z>=0; else r=r&&i.z>s.z&&i.z<=0; } return r; } return false; }
//--------------------------------------------------------------------------------- // Draw a single tile of the rubik's cube. assumes all necessary rotation has // been performed already. //--------------------------------------------------------------------------------- void RubiksCube::DrawTile(int color, int32 x, int32 y, int32 z, int32 size, bool picking) { int16 blackbit = f32tov16(divf32(size, inttof32(20))); int16 vx, vy, vz, vsize; vx = f32tov16(x); vy = f32tov16(y); vz = f32tov16(z); vsize = f32tov16(size); if(!picking) SetRCColor(color); //if we're not picking, then we need the right colour // Draw the coloured part of the tile (sans black bits): glVertex3v16(vx + blackbit, vy + blackbit, vz); glVertex3v16(vx + vsize - blackbit, vy + blackbit, vz); glVertex3v16(vx + vsize - blackbit, vy + vsize - blackbit, vz); glVertex3v16(vx + blackbit, vy + vsize - blackbit, vz); if(!picking) //if we're not picking, better draw in the black bits too { glColor3f(0,0,0); //top glVertex3v16(vx, vy, vz + blackbit); glVertex3v16(vx + vsize, vy, vz + blackbit); glVertex3v16(vx + vsize - blackbit, vy + blackbit, vz); glVertex3v16(vx + blackbit, vy + blackbit, vz); //right glVertex3v16(vx + vsize - blackbit, vy + blackbit, vz); glVertex3v16(vx + vsize, vy, vz + blackbit); glVertex3v16(vx + vsize, vy + vsize, vz + blackbit); glVertex3v16(vx + vsize - blackbit, vy + vsize - blackbit, vz); //bottom glVertex3v16(vx + blackbit, vy + vsize - blackbit, vz); glVertex3v16(vx + vsize - blackbit, vy + vsize - blackbit, vz); glVertex3v16(vx + vsize, vy + vsize, vz + blackbit); glVertex3v16(vx, vy + vsize, vz + blackbit); //left glVertex3v16(vx, vy, vz + blackbit); glVertex3v16(vx + blackbit, vy + blackbit, vz); glVertex3v16(vx + blackbit, vy + vsize - blackbit, vz); glVertex3v16(vx, vy + vsize, vz + blackbit); } }
bool collideLineConvertedRectangle(vect3D n, vect3D p, vect3D s, vect3D o, vect3D v, int32 d, int32* kk, vect3D* ip) { int32 p1=dotProduct(v,n); if(!equals(p1,0)) { int32 p2=dotProduct(vectDifference(p,o),n); int32 k=divf32(p2,p1); s8 sign=((s.x>0)^(s.y<0)^(s.z>0)^(p1<0))?(-1):(1); if(kk) { *kk=k+sign; } if(k<0 || k>d){return false;} vect3D i=addVect(o,vectMult(v,k)); if(ip)*ip=i; i=vectDifference(i,p); NOGBA("I %d %d %d",i.x,i.y,i.z); NOGBA("S %d %d %d",s.x,s.y,s.z); bool r=true; if(s.x) { if(s.x>0)r=r&&i.x<s.x&&i.x>=0; else r=r&&i.x>s.x&&i.x<=0; } if(s.y) { if(s.y>0)r=r&&i.y<s.y&&i.y>=0; else r=r&&i.y>s.y&&i.y<=0; } if(s.z) { if(s.z>0)r=r&&i.z<s.z&&i.z>=0; else r=r&&i.z>s.z&&i.z<=0; } return r; } return false; }
void RubiksCube::Draw(touchPosition touchXY) { int viewport[]={0,0,255,191}; // used later for gluPickMatrix() int16 vx, vy, vz, vsize; vx = f32tov16(Position.X); vy = f32tov16(Position.Y); vz = f32tov16(Position.Z); vsize = f32tov16(Size); glPushMatrix(); { glRotateY(90); glRotateZ(180); if(Twisting||AutoTwisting) { int ax=0; // Rotate so we draw the cube the right way round while(ax<Twist.axis) { glRotateX(90); glRotateZ(90); ax++; } if(Twist.position==0 || Twist.position==3) glRotateZ(Twist.rotation); DrawSide(Twist.left.side,Twist.axis,Position.X,Position.Y,Position.Z,Size,false); glBegin(GL_QUADS); glColor3f(0,0,0); glVertex3v16(vx + vsize, vy, vz + f32tov16(divf32(Size,inttof32(3)))); glVertex3v16(vx, vy, vz + f32tov16(divf32(Size,inttof32(3)))); glVertex3v16(vx, vy + vsize, vz + f32tov16(divf32(Size,inttof32(3)))); glVertex3v16(vx + vsize, vy + vsize, vz + f32tov16(divf32(Size,inttof32(3)))); glEnd(); if(Twist.position==0 || Twist.position==3) glRotateZ(-Twist.rotation); glRotateY(180); if(Twist.position==2 || Twist.position==3) glRotateZ(-Twist.rotation); DrawSide(Twist.right.side,Twist.axis+3,Position.X,Position.Y,Position.Z,Size,false); glBegin(GL_QUADS); glColor3f(0,0,0); glVertex3v16(vx + vsize, vy, vz + f32tov16(divf32(Size,inttof32(3)))); glVertex3v16(vx, vy, vz + f32tov16(divf32(Size,inttof32(3)))); glVertex3v16(vx, vy + vsize, vz + f32tov16(divf32(Size,inttof32(3)))); glVertex3v16(vx + vsize, vy + vsize, vz + f32tov16(divf32(Size,inttof32(3)))); glEnd(); if(Twist.position==2 || Twist.position==3) glRotateZ(Twist.rotation); glRotateY(90); glRotateX(-90); if(Twist.position==1 || Twist.position==3) glRotateX(Twist.rotation); glBegin(GL_QUADS); glColor3f(0,0,0); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3))), vy, vz); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3))), vy + vsize, vz); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3))), vy + vsize, vz + vsize); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3))), vy, vz + vsize); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3)))*2, vy, vz); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3)))*2, vy, vz + vsize); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3)))*2, vy + vsize, vz + vsize); glVertex3v16(vx + f32tov16(divf32(Size,inttof32(3)))*2, vy + vsize, vz); glEnd(); if(Twist.position==1 || Twist.position==3) glRotateX(-Twist.rotation); for(int i=0;i<4;i++) { if(Twist.position==0) glRotateX(Twist.rotation); DrawLine(Twist.left.line[i],Position.X,Position.Y,Position.Z,Size/3); if(Twist.position==0) glRotateX(-Twist.rotation); if(Twist.position==1) glRotateX(Twist.rotation); DrawLine(Twist.middle.line[i],Position.X+(Size/3),Position.Y,Position.Z,Size/3); if(Twist.position==1) glRotateX(-Twist.rotation); if(Twist.position==2) glRotateX(Twist.rotation); DrawLine(Twist.right.line[i],Position.X+(Size/3*2),Position.Y,Position.Z,Size/3); if(Twist.position==2) glRotateX(-Twist.rotation); glRotateX(90); } }else{ for(int i=0;i<3;i++) { DrawSide(Side[i],i,Position.X,Position.Y,Position.Z,Size, false); glRotateY(180); DrawSide(Side[i+3],i+3,Position.X,Position.Y,Position.Z,Size, false); glRotateX(90); glRotateZ(-90); } } } glPopMatrix(1); if(Picking) { glPushMatrix(); for(int i=0;i<3;i++) clicked[i]=0; closeW = 0x7FFFFFFF; //reset the distance //set the viewport to just off-screen, this hides all rendering that will be done during picking glViewport(0,192,0,192); // setup the projection matrix for picking glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPickMatrix((touchXY.px),(191-touchXY.py),4,4,viewport); // render only what is below the cursor gluPerspective(70, 256.0 / 192.0, 0.1, 20); // this must be the same as the original perspective matrix glMatrixMode(GL_MODELVIEW); // switch back to modifying the modelview matrix for drawing { glRotateY(90); glRotateZ(180); for(int i=0;i<3;i++) { DrawSide(Side[i],i,Position.X,Position.Y,Position.Z,Size, true); glRotateY(180); DrawSide(Side[i+3],i+3,Position.X,Position.Y,Position.Z,Size, true); glRotateX(90); glRotateZ(-90); } } glViewport(0,0,255,191); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(70, 256.0 / 192.0, 0.1, 20); glMatrixMode(GL_MODELVIEW); if(closeW<0x7FFFFFFF) { if(Painting) { if(Side[clicked[0]].tile[clicked[1]][clicked[2]].color!=paintColour) { if(undoQueue.numEntries==0) { undoQueue.numEntries++; undoQueue.currentEntry=0; } else if(undoQueue.currentEntry < (undoQueue.numEntries-1)) { undoQueue.currentEntry++; undoQueue.numEntries=undoQueue.currentEntry+1; } else if(undoQueue.numEntries==20) { for(int i=0; i<19; i++) { undoQueue.entry[i]=undoQueue.entry[i+1]; } } else { undoQueue.numEntries++; undoQueue.currentEntry++; } undoQueue.entry[undoQueue.currentEntry].from=Side[clicked[0]].tile[clicked[1]][clicked[2]].color; undoQueue.entry[undoQueue.currentEntry].to=paintColour; for(int i=0; i<3; i++) undoQueue.entry[undoQueue.currentEntry].position[i]=clicked[i]; Side[clicked[0]].tile[clicked[1]][clicked[2]].color=paintColour; } }else{ Grabbing=true; Picking=false; } } glPopMatrix(1); }else{ //Twisting=false; } }
void unitVector (int32* vector) { int32 magnitude = sqrtf32(mulf32(vector[0], vector[0]) + mulf32(vector[1], vector[1])); vector[0]=divf32(vector[0], magnitude); vector[1]=divf32(vector[1], magnitude); }