Page 2 of 4

Re: NXC code to NBC code

Posted: 13 Jun 2011, 12:20
by nxtboyiii
oh! thanks!

EDIT: Heres the RICS and sounds for games(attachment):

EDIT2: Here's the code for the game:
NOTE: All I did was convert the original pics from a mario website to black and white. The only ric I actually created was the boulder("T.ric"). I did not make any sounds.
For the gmtilesb.ric here is who-made-what(not updated yet!):

Code: Select all

stone-minecraft!!
bag of money and TNT- sidneys1 from mindboards.net
ladder- me
broken brick wall and brick wall- turiqwalrus from omnimaga.org
gravestone and both water sprites- Ashbad from omnimaga.org
fire- yunhua98 from omnimaga.org
stone wall- ZippyDee from omnimaga.org  

I need help making the for-loop at the end of task dostuff() run faster.

Code: Select all

struct brk
{
 byte x;
 byte y;
 bool sld;
// byte alv;
// char T;
};

#download "Platformer!!!!\gmtilesb.ric"
#download "Platformer!!!!\guy2.ric"
#download "Platformer!!!!\T.ric"
#download "Platformer!!!!\R.ric"
#download "Platformer!!!!\mjump2.rso"
#download "Platformer!!!!\ld.ric"
int EM;
const int MAXEM=15;
brk map[30][25];
float xxe[MAXEM];
float yye[MAXEM];
float fex[MAXEM];
float fey[MAXEM];
bool eli[MAXEM];
byte eat[MAXEM];
char sc[MAXEM];
int ex[MAXEM];
int ey[MAXEM];
int pex[MAXEM];
int pey[MAXEM];
int mex[MAXEM];
int mey[MAXEM];
string estr="";
float fmapx,fmapy;
int mapx,mapy;
bool gl;
int x,y;
byte paddx,paddy;
int px,py;
float xx;
float yy;
int xadd,yadd;
bool s,done,jumping;
int xpp,ypp;
byte height=11;
byte width=15;
byte var[2];
byte yj;
int life;
byte prx,pry,pw,ph;
byte vare[1];
byte gvar[1];

#define EPSILON 0.125 // Make this "as small as possible", and make sure it's a power of 2.
                      // Try 0.015625, but that may fail with larger numbers.

inline long myfloor(float x)
{
    return(x + EPSILON);
}

bool OnTile(int pointx, int pointy, int paddx2, int paddy2, byte xtile, byte ytile)
{
 return((map[pointx][pointy].x == xtile && map[pointx][pointy].y == ytile) || (map[pointx+paddx2][pointy+paddy2].x == xtile && map[pointx+paddx2][pointy+paddy2].y == ytile) || (map[pointx][pointy+paddy2].x == xtile && map[pointx][pointy+paddy2].y == ytile) || (map[pointx+paddx2][pointy].x == xtile && map[pointx+paddx2][pointy].y == ytile));
}

void Tile()
{
 if(OnTile(mapx,mapy,paddx,paddy,16,16))
 {
  PlayToneEx(700,20,1,0);
  life-=10;
 }
 if(map[mapx+paddx][mapy-1].sld == 3 || map[mapx][mapy-1].sld == 3 || map[mapx-1][mapy+paddy].sld == 3 || map[mapx-1][mapy].sld == 3 || map[mapx+1][mapy+paddy].sld == 3 || map[mapx+1][mapy].sld == 3)
 {
  life-=20;
  for(int s=620; s < 700; s+=20)
  {
   PlayToneEx(s,10,Volume(),0);
   Wait(20);
  }
 }
 if(map[mapx][mapy].sld == 4 || map[mapx+paddx][mapy].sld == 4 || map[mapx][mapy+paddy].sld == 4 || map[mapx+paddx][mapy+paddy].sld == 4)
 {
  StopAllTasks();
 }
 if(OnTile(mapx,mapy,paddx,paddy,112,16))
 {
  //Display winning screen or go to next level
  StopAllTasks();
 }
}

bool OnTextTile(string text, char vars[], byte on)
{
 char dat[3];
 StrToByteArray(text,dat);
 return(vars[on] == dat[0] && vars[on+1] == dat[1] && vars[on+2] == dat[2]);
}
bool OnETile(int xa,int ya,int pox,int poy)
{
 return(map[pox][poy].x == xa && map[pox][poy].y == ya);
}
void DrawEMY()
{
 for(int e=0; e < EM; e++)
 {
  if(map[mex[e]][mey[e]].sld >= 3 && eli[e])
  {
   if(map[mex[e]][mey[e]].sld == 3)
   {
    estr="T.ric";
   }
   if(map[mex[e]][mey[e]].sld == 4)
   {
    estr="R.ric";
   }
   GraphicOut(ex[e],ey[e],estr);
  }
 }
}

void amove(int xp,int yp)
{
 s=0;
 until(done);
 xadd+=xp;
 yadd+=yp;
 s=1;
 until(!done);
}

void LoadMap()
{
 byte handle;
 byte fsize=1500;
 string hei="";
 string wid="";
 byte hon,won;
 string line="";
 byte data[300];
 int ccx,ccy;
 int con;
 int eex,eey,eon;
 char ch=' ';
 OpenFileRead("mapt1.txt",fsize,handle);
 ReadLnString(handle,hei);
 height=StrToNum(hei);
 ReadLnString(handle,wid);
 width=StrToNum(wid);
 ccy=height-1;
 byte tmp[height];
 ArrayInit(map,tmp,width);
 for(int c=0; c < width; c++)
 {
  for(int d=0; d < height; d++)
  {
    map[c][d].x=16;
    map[c][d].y=0;
    map[c][d].sld=0;
  }
 }
 eex=0;
 eey=(height-1)*16;
 until(hon == height)
 {
  ReadLnString(handle,line);
  StrToByteArray(line,data);
  until(won == width)
  {
      if(OnTextTile("SEK",data,con))
   {
    map[ccx][ccy].x=0;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=3;
    xxe[eon]=ccx*100;
    yye[eon]=ccy*100;
    fex[eon]=ccx;
    fey[eon]=ccy;
    ex[eon]=eex;
    ey[eon]=eey;
    mex[eon]=ccx;
    mey[eon]=ccy;
    sc[eon]='N';
    eli[eon]=1;
    eat[eon]=3;
    eon++;
    EM++;
   }
   if(OnTextTile("ROK",data,con))
   {
    map[ccx][ccy].x=0;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=4;
    xxe[eon]=ccx*100;
    yye[eon]=ccy*100;
    fex[eon]=ccx;
    fey[eon]=ccy;
    ex[eon]=eex;
    ey[eon]=eey;
    mex[eon]=ccx;
    mey[eon]=ccy;
    sc[eon]='N';
    eli[eon]=1;
    eat[eon]=4;
    eon++;
    EM++;
   }
   if(OnTextTile("BRK",data,con))
   {
    map[ccx][ccy].x=0;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=1;
   }
      if(OnTextTile("LDR",data,con))
   {
    map[ccx][ccy].x=32;
    map[ccx][ccy].y=16;
   }
   if(OnTextTile("STN",data,con))
   {
    map[ccx][ccy].x=96;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=1;
   }
   if(OnTextTile("FIR",data,con))
   {
    map[ccx][ccy].x=16;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=0;
   }
      if(OnTextTile("CSN",data,con))
   {
    map[ccx][ccy].x=96;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=2;
   }
   if(OnTextTile("BBK",data,con))
   {
    map[ccx][ccy].x=32;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=2;
   }
   if(OnTextTile("BLK",data,con))
   {
    map[ccx][ccy].x=48;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=0;
   }
   if(OnTextTile("TNT",data,con))
   {
    map[ccx][ccy].x=48;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=2;
   }
   if(OnTextTile("GRV",data,con))
   {
    map[ccx][ccy].x=64;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=0;
   }
      if(OnTextTile("WTR",data,con))
   {
    map[ccx][ccy].x=80;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=0;
   }
      if(OnTextTile("WTP",data,con))
   {
    map[ccx][ccy].x=80;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=0;
   }
         if(OnTextTile("END",data,con))
   {
    map[ccx][ccy].x=112;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=0;
   }
         if(OnTextTile("BAG",data,con))
   {
    map[ccx][ccy].x=112;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=0;
   }
         if(OnTextTile("GRS",data,con))
   {
    map[ccx][ccy].x=128;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=2;
   }
            if(OnTextTile("DRT",data,con))
   {
    map[ccx][ccy].x=128;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=2;
   }
            if(OnTextTile("LEV",data,con))
   {
    map[ccx][ccy].x=144;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=2;
   }
            if(OnTextTile("TRE",data,con))
   {
    map[ccx][ccy].x=144;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=2;
   }
            if(OnTextTile("LAV",data,con))
   {
    map[ccx][ccy].x=160;
    map[ccx][ccy].y=16;
    map[ccx][ccy].sld=0;
   }
            if(OnTextTile("LAB",data,con))
   {
    map[ccx][ccy].x=160;
    map[ccx][ccy].y=0;
    map[ccx][ccy].sld=0;
   }
   con+=4;
   won++;
   ccx++;
   eex+=16;
  }
  eex=0;
  eey-=16;
  ccx=0;
  ccy--;
  hon++;
  won=0;
  con=0;
 }
 CloseFile(handle);
}

task dostuff()
{
 while(1)
 {
  if(SENSOR_2)
  {
   StopAllTasks();
  }
  if(fmapx == floor(fmapx))
  {
   paddx=0;
  }
  else
  {
   paddx=1;
  }
    if((ButtonPressed(BTNCENTER,0) && jumping == 0 && map[mapx][mapy+1].sld == 0 && map[mapx+paddx][mapy+1].sld == 0 && yj < 16) || (jumping == 1 && !OnTile(mapx,mapy,paddx,paddy,32,16)))
  {
 //  until(!ButtonPressed(BTNCENTER,0));
 if(gvar[0] == 0 && !OnTile(mapx,mapy,paddx,paddy,32,16))
 {
  gvar[0]=32;
 }
  if(gvar[0] == 16 && !OnTile(mapx,mapy,paddx,paddy,32,16))
 {
  gvar[0]=48;
 }
  if(!OnTile(mapx,mapy,paddx,paddy,32,16) && yj == 0)
  {
   PlayFileEx("mjump2.rso",Volume(),0);
  }
   yy+=25;
   fmapy=yy/100;
   if(yadd/-16 < height-4)
   {
    ypp+=-4;
   }
   else
   {
    y+=4;
    ypp=0;
   }
   jumping=1;
   yj+=4;
  // amove(0,ypp);
  }
   if(yj >= 16)
   {
    jumping=0;
   }
      mapy=floor(fmapy);
   mapx=floor(fmapx);
     if(fmapy == floor(fmapy))
  {
   paddy=0;
  }
  else
  {
   paddy=1;
  }
   if(ButtonPressed(BTNLEFT,0))
   {
       if(jumping == 0)
   {
    gvar[0]=16;
   }
   }
  if(ButtonPressed(BTNLEFT,0) && ((map[mapx-1][mapy].sld == 0 && map[mapx-1][mapy+paddy].sld == 0) || fmapx > floor(fmapx)))
  {
   xx-=25;
   fmapx=xx/100;
   if(x == 16)
   {
    xpp+=4;
   }
   else
   {
    x-=4;
    xpp=0;
   }
//   amove(xpp,0);
  }
   mapy=floor(fmapy);
  mapx=floor(fmapx);
      if(fmapy == floor(fmapy))
  {
   paddy=0;
  }
  else
  {
   paddy=1;
  }

     if(ButtonPressed(BTNRIGHT,0))
   {
       if(jumping == 0)
   {
    gvar[0]=0;
   }
   }
  if(ButtonPressed(BTNRIGHT,0) && map[mapx+1][mapy].sld == 0 && map[mapx+1][mapy+paddy].sld == 0)
  {
   if(jumping == 0)
   {
    gvar[0]=0;
   }
   xx+=25;
   fmapx=xx/100;
    if(xadd/-16 < width-6)
   {
    xpp+=-4;
   }
   else
   {
    x+=4;
    xpp=0;
   }
//   amove(xpp,0);
  }
    mapy=floor(fmapy);
   mapx=floor(fmapx);
    if(fmapx == floor(fmapx))
  {
   paddx=0;
  }
  else
  {
   paddx=1;
  }
   if(SENSOR_1)
  {
   if(ButtonPressed(BTNEXIT,0))
   {
    px=0;
    py=-1;
    prx=x;
    pry=y;
    pw=2;
    ph=5;
   }
   else if(ButtonPressed(BTNLEFT,0))
   {
    px=-1;
    py=0;
    prx=x;
    pry=y;
    pw=5;
    ph=2;
   }
   else if(ButtonPressed(BTNRIGHT,0))
   {
    px=1;
    py=0;
    prx=x+11;
    pry=y;
    pw=5;
    ph=2;
   }
   else if(ButtonPressed(BTNCENTER,0))
   {
    px=0;
    py=1;
    prx=x;
    pry=y+11;
    pw=2;
    ph=5;
   }
   else
   {
    goto endp;
   }
   if(map[mapx+px][mapy+py].sld >= 2/* || map[mapx+px+paddx][mapy+py].sld == 2 || map[mapx+px][mapy+py+paddy].sld == 2 || map[mapx+px+paddx][mapy+py+paddy].sld == 2*/)
   {
       repeat(300)
   {
    RectOut(prx,pry,pw,ph,DRAW_OPT_FILL_SHAPE);
    Wait(1);
   }
    if(map[mapx+px][mapy+py].sld >= 3)
    {
     if(map[mapx+px][mapy+py].sld == 3)
     {
      estr="T.ric";
     }
     for(vare[0]=16; vare[0] < 80; vare[0]+=16)
	  {
	    repeat(50)
	    {
      //GraphicOutEx(xg,yl,"guy2.ric",varg);
	     GraphicOutEx(x+(px*16),y+(py*16),estr,vare);
	     Wait(1);
      }
	  }
	  for(int e=0; e < EM; e++)
	  {
	   if(mex[e] == mapx+px && mey[e] == mapy+py)
	   {
      eli[e]=0;
	   }
	  }
	  for(int c=mapx+px-1; c < mapx+px+2; c++)
    {
 	   for(int d=mapy+py-1; d < mapy+py+2; d++)
     {
      if(OnETile(0,0,c,d))
      {
       map[c][d].sld=1;
      }
            if(OnETile(32,0,c,d))
      {
       map[c][d].sld=2;
      }
            if(OnETile(16,0,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(48,16,c,d))
      {
       map[c][d].sld=0;
      }
           if(OnETile(64,0,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(16,16,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(192,16,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(48,0,c,d))
      {
       map[c][d].sld=2;
      }
            if(OnETile(32,16,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(64,16,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(80,16,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(80,0,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(96,16,c,d))
      {
       map[c][d].sld=1;
      }
            if(OnETile(96,0,c,d))
      {
       map[c][d].sld=2;
      }
            if(OnETile(112,0,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(112,16,c,d))
      {
       map[c][d].sld=0;
      }
            if(OnETile(128,0,c,d))
      {
       map[c][d].sld=2;
      }
                  if(OnETile(128,16,c,d))
      {
       map[c][d].sld=2;
      }
                  if(OnETile(144,0,c,d))
      {
       map[c][d].sld=2;
      }
                  if(OnETile(144,16,c,d))
      {
       map[c][d].sld=2;
      }
                  if(OnETile(160,16,c,d))
      {
       map[c][d].sld=0;
      }
                  if(OnETile(160,0,c,d))
      {
       map[c][d].sld=0;
      }
                  if(OnETile(176,16,c,d))
      {
       map[c][d].sld=0;
      }
      }
    }
    }
    else
    {
     map[mapx+px][mapy+py].x=16;
     map[mapx+px][mapy+py].y=0;
    }
    map[mapx+px][mapy+py].sld=0;
    for(int e=0; e < EM; e++)
    {
     if(eli[e])
     {
      map[mex[e]][mey[e]].sld=eat[e];
     }
    }
   }
  }
  endp:
      if((jumping == 0 && (!OnTile(mapx,mapy,paddx,paddy,32,16) || (ButtonPressed(BTNEXIT,0) && OnTile(mapx,mapy,paddx,paddy,32,16)))) && ((map[mapx][mapy-1].sld == 0 && map[mapx+paddx][mapy-1].sld == 0) || fmapy > floor(fmapy)))
  {
   jumping=0;
   yy-=25;
   fmapy=yy/100;
      if(y == 16)
   {
    ypp+=4;
   }
   else
   {
    y-=4;
    ypp=0;
   }
//   amove(0,ypp);
  }
  if((map[mapx][mapy-1].sld >= 1 || map[mapx+paddx][mapy-1].sld >= 1 || OnTile(mapx,mapy,paddx,paddy,32,16)) && (paddy == 0 || OnTile(mapx,mapy,paddx,paddy,32,16)))
  {
   if(gvar[0] == 32)
   {
    gvar[0]=0;
   }
   if(gvar[0] == 48)
   {
    gvar[0]=16;
   }
   yj=0;
   jumping=0;
  }
   Tile();
    s=0;
     mapy=floor(fmapy);
   mapx=floor(fmapx);
   for(int c=0; c < EM; c++)
   {
     ex[c]+=xpp;
     ey[c]+=ypp;
    if(map[mex[c]][mey[c]].sld >= 3 && ex[c] <= 60  && ex[c] >= -32 && ey[c] >= 0 && ((ey[c] <= 64 && eat[c] == 3) || (eat[c] == 4 && ey[c] <= 100)) && eli[c] == 1)
    {
     //eli[c]=map[mex[c]][mey[c]].sld;
    if(map[mex[c]][mey[c]].sld == 3)
   {
    estr="T.ric";
   }
   if(map[mex[c]][mey[c]].sld == 4)
   {
    estr="R.ric";
   }
     if(fey[c] == floor(fey[c]))
     {
      pey[c]=0;
     }
     else
     {
      pey[c]=1;
     }
     if((mex[c] > mapx && map[mex[c]-1][mey[c]].sld == 0  && eat[c] != 4 && map[mex[c]-1][mey[c]+pey[c]].sld == 0 && sc[c] == 'N' && mex[c]-1 != mapx && mex[c]-1 != mapx+paddx) || sc[c] == 'L')
     {
      ex[c]-=4;
      xxe[c]-=25;
      fex[c]=xxe[c]/100;
      sc[c]='L';
     }
     mex[c]=floor(fex[c]);
     mey[c]=floor(fey[c]);
     if(fey[c] == floor(fey[c]))
     {
      pey[c]=0;
     }
     else
     {
      pey[c]=1;
     }
     if((mex[c] < mapx && map[mex[c]+1][mey[c]].sld == 0 && eat[c] != 4 && map[mex[c]+1][mey[c]+pey[c]].sld == 0 && sc[c] == 'N' && mex[c]+1 != mapx) || sc[c] == 'R')
     {
      ex[c]+=4;
      xxe[c]+=25;
      fex[c]=xxe[c]/100;
      sc[c]='R';
     }
     mex[c]=floor(fex[c]);
     mey[c]=floor(fey[c]);
     if(fex[c] == floor(fex[c]))
     {
      pex[c]=0;
     }
     else
     {
      pex[c]=1;
     }
     if((map[mex[c]][mey[c]-1].sld == 0 && map[mex[c]+pex[c]][mey[c]-1].sld == 0 && sc[c] == 'N' && ((mey[c]-1 != mapy && mey[c]-1 != mapy+paddy && eat[c] == 3) || eat[c] == 4)) || sc[c] == 'D')
     {
      ey[c]-=4;
      yye[c]-=25;
      fey[c]=yye[c]/100;
      sc[c]='D';
     }
     mex[c]=floor(fex[c]);
     mey[c]=floor(fey[c]);
     if(fex[c] != floor(fex[c]) || fey[c] != floor(fey[c]))
     {
      map[mex[c]][mey[c]].sld=eat[c];
     }
     if(fex[c] == floor(fex[c]))
     {
     switch(sc[c])
     {
      case 'L':
      map[mex[c]+1][mey[c]].sld=0;
      map[mex[c]][mey[c]].sld=eat[c];
      sc[c]='N';
      break;
      case 'R':
      map[mex[c]-1][mey[c]].sld=0;
      map[mex[c]][mey[c]].sld=eat[c];
      sc[c]='N';
      break;
     }
     }
     if(fey[c] == floor(fey[c]))
     {
      switch(sc[c])
      {
      case 'U':
      map[mex[c]][mey[c]-1].sld=0;
      map[mex[c]][mey[c]].sld=eat[c];
      sc[c]='N';
      break;
      case 'D':
      map[mex[c]][mey[c]+1].sld=0;
      map[mex[c]][mey[c]].sld=eat[c];
      sc[c]='N';
      break;
      }
     }
      GraphicOut(ex[c],ey[c],estr);
      if(eat[c] == 4 && (map[mex[c]][mey[c]-1].sld == 1 || map[mex[c]][mey[c]-1].sld == 2) && fey[c] == floor(fey[c]))
      {
       eli[c]=0;
       map[mex[c]][mey[c]].sld=0;
      }
       //  Yield();
    }
   }
    until(done);
 xadd+=xpp;
 yadd+=ypp;
 s=1;
 until(!done);
    xpp=0;
    ypp=0;
 }
}

task draw()
{
 // Drawing stuff
DrawGraphicType dgArgs2;
DrawGraphicType dgArgs;
// This part IS necessary.
dgArgs.Filename = "gmtilesb.ric";
ArrayInit(dgArgs.Variables, 0, 2);
dgArgs.Options = 0;
dgArgs2.Filename = "guy2.ric";
ArrayInit(dgArgs2.Variables, 0, 1);
dgArgs2.Options = 0;
int cen,den;
 while(1)
 {
  until(s);
  done=0;
  cen=ceil(xadd/-16)+6;
  den=ceil(yadd/-16)+4;
  for(int c=floor(xadd/-16); c < cen; c++)
  {
   dgArgs2.Location.X=x;
   dgArgs2.Location.Y=y;
   dgArgs2.Variables[0]=gvar[0];
   SysDrawGraphic(dgArgs2);
   for(int d=floor(yadd/-16); d < den; d++)
   {
        dgArgs.Location.X = (c << 4)+xadd;
        dgArgs.Location.Y = (d << 4)+yadd;
        dgArgs.Variables[0] = map[c][d].x;
        dgArgs.Variables[1] = map[c][d].y;
        SysDrawGraphic(dgArgs);
   }
  }
  RectOut(96,0,4,64,DRAW_OPT_FILL_SHAPE | DRAW_OPT_CLEAR);
//  DrawEMY();
  done=1;
 }
}

task main()
{
 SetAbortFlag(BTNSTATE_NONE);
 SetSensorTouch(S1);
 SetSensorTouch(S2);
 mapx=1;
 mapy=1;
 xx=100;
 yy=100;
 fmapx=1;
 fmapy=1;
 x=16;
 y=16;
 s=1;
 GraphicOut(0,0,"ld.ric");
 for(int c=0; c < width; c++)
 {
  for(int d=0; d < height; d++)
  {
   map[c][d].x=16;
   map[c][d].y=0;
   map[c][d].sld=0;
  }
 }
 LoadMap();
 life=300;
 Precedes(draw,dostuff);
}

Re: NXC code to NBC code

Posted: 13 Jun 2011, 18:17
by spillerrec
As I suspected, the parameters for the RIC file simply maps to the X and Y positions on the sprite. This solution is far from optimal.
Instead, make use of VarMaps, so when you pass it 1 in the first parameter, it draws the first sprite. If you pass it 5, it draws the fifth sprite. The RIC file takes care of the X and Y coordinates itself.

You can optimize the map drawing by changing it into a RIC font and then use FontTextOut() to draw an entire line at a time.


In your for loop you use arrays a lot. Consider this code:

Code: Select all

char myarr[] = { 20, 10, 40 };
int var1;
int var2;
int var3;

task main(){
	unsigned int t0 = CurrentTick();

	//Takes 498 msec
	repeat(2000){
		var1 = 2*myarr[1];
		var2 = 4*myarr[1];
		var3 = 5*myarr[1];
	}
	//*/

	/* /Takes 296 msec
	repeat(2000){
		char temp = myarr[1];
		var1 = 2*temp;
		var2 = 4*temp;
		var3 = 5*temp;
	}
	//*/

	/* /Takes 208 msec
	char temp = myarr[1];
	repeat(2000){
		var1 = 2*temp;
		var2 = 4*temp;
		var3 = 5*temp;
	}
	//*/

	unsigned int td = CurrentTick() - t0;

	NumOut( 0, LCD_LINE1, var1 );
	NumOut( 0, LCD_LINE2, var2 );
	NumOut( 0, LCD_LINE3, var3 );
	NumOut( 0, LCD_LINE4, td );
	
	Wait( 2000 );
}

Re: NXC code to NBC code

Posted: 13 Jun 2011, 20:02
by mattallen37
Just for the fun of it, I like to optimize NXC drawing functions by using NBC; especially things like NumOut and TextOut.

In that code spillerrec posted, instead of using those four lines of "NumOut", you could use a little NBC code. Those four lines of NXC generate this NBC:

Code: Select all

	set __signed_stack_002main, 0
	set __signed_stack_003main, 56
	mov __D0main, var1
	mov __TextOutArgs.Location.X, __signed_stack_002main
	mov __TextOutArgs.Location.Y, __signed_stack_003main
	set __TextOutArgs.Options, 0
	numtostr __TextOutArgs.Text, __D0main
	syscall 13, __TextOutArgs
	set __signed_stack_002main, 0
	set __signed_stack_003main, 48
	mov __D0main, var2
	mov __TextOutArgs.Location.X, __signed_stack_002main
	mov __TextOutArgs.Location.Y, __signed_stack_003main
	set __TextOutArgs.Options, 0
	numtostr __TextOutArgs.Text, __D0main
	syscall 13, __TextOutArgs
	set __signed_stack_002main, 0
	set __signed_stack_003main, 40
	mov __D0main, var3
	mov __TextOutArgs.Location.X, __signed_stack_002main
	mov __TextOutArgs.Location.Y, __signed_stack_003main
	set __TextOutArgs.Options, 0
	numtostr __TextOutArgs.Text, __D0main
	syscall 13, __TextOutArgs
	set __signed_stack_002main, 0
	set __signed_stack_003main, 32
	mov __DU0main, td
	mov __TextOutArgs.Location.X, __signed_stack_002main
	mov __TextOutArgs.Location.Y, __signed_stack_003main
	set __TextOutArgs.Options, 0
	numtostr __TextOutArgs.Text, __DU0main
	syscall 13, __TextOutArgs
That is 32 lines. However, a lot of it is redundant, so you can take out a great deal of it, and it will function exactly the same. These lines of code do exactly the same thing, but without being redundant.

Code: Select all

    mov __TextOutArgs.Location.X, 0
    mov __TextOutArgs.Location.Y, 56
    set __TextOutArgs.Options, 0
    numtostr __TextOutArgs.Text, var1
    syscall 13, __TextOutArgs
    mov __TextOutArgs.Location.Y, 48
    numtostr __TextOutArgs.Text, var2
    syscall 13, __TextOutArgs
    mov __TextOutArgs.Location.Y, 40
    numtostr __TextOutArgs.Text, var3
    syscall 13, __TextOutArgs
    mov __TextOutArgs.Location.Y, 32
    numtostr __TextOutArgs.Text, td
    syscall 13, __TextOutArgs
That is only 14 lines. I doubt it makes much of a timing difference, but it's just something I enjoy doing.

Once you get your NXC code as efficient as you think you can, then you should try eliminating some useless lines of assembler by rewriting sections in NBC. I really haven't done much of any testing of speeds, but it's fun to play with.

Re: NXC code to NBC code

Posted: 13 Jun 2011, 20:23
by spillerrec
In general I hate rewriting stuff to NBC because it reminds me of how much bloat there is in it normally...

Normally there wouldn't be much difference if it is not in a loop that gets repeated several times. So unless it is performance critical code I wouldn't do it, it makes the code harder to maintain. Especially when you start rewriting all if, for and while statements...

Re: NXC code to NBC code

Posted: 13 Jun 2011, 21:28
by nxtboyiii
Wow! You guys are really smart!! Thanks!!


spillerrec-

which for-loop are you talking about?
Can you help me with the VarMap?
Can you show me how to do it?
Is it hard?

Thank you so much,
nxtboy III

Re: NXC code to NBC code

Posted: 13 Jun 2011, 23:31
by muntoo
I've attached the gmtilesb.ric that you can use for FontTextOut().

Re: NXC code to NBC code

Posted: 13 Jun 2011, 23:55
by linusa
Another piece of advice: "Measure twice, optimize once!". You should make sure that you absolutely know for sure which parts of the code take longest to execute. Especially the NXT brick and the firmware doesn't behave as you might expect from your experience with "classic programming languages on a computer". One important example: Lookup tables (i.e. array lookups via []) don't seem to be worth it for simple calculations -- those lookups are usually magnitudes faster, compared to mathematical operations, on PCs.

So you need to do benchmarking and testing, and especially measuring. Use the tick counter, print out runtimes in millisecs. Preferrably for each line or major operation of your code. Then focus on the most expensive things (CPU wise) first. You might also want to have a look at the NXC Profiler here http://www.mindstorms.rwth-aachen.de/tr ... XCProfiler or its source-code (you could just copy-paste the timing bits).

I also like this quote (but it doesn't really apply in this situation, I just thought of it ;-) ):
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.”
- Michael A. Jackson

Re: NXC code to NBC code

Posted: 14 Jun 2011, 00:03
by nxtboyiii
Can you post example code for using FontTextOut?
How do I input which 16x16 tile I want to show??

Re: NXC code to NBC code

Posted: 14 Jun 2011, 00:12
by muntoo
Using the gmtilesb.ric file I uploaded, something like this should give you a nice demo:

Code: Select all

task main()
{
    string all = "";

    for(byte tile = 0; tile < 28; ++tile)
        all += FlattenVar(tile);

    FontTextOut(0, 64 - 16,
                "gmtilesb.ric", all,
                DRAW_OPT_NORMAL | DRAW_OPT_FONT_DIR_L2RB | DRAW_OPT_FONT_WRAP);

    Wait(2000);
}

Re: NXC code to NBC code

Posted: 14 Jun 2011, 02:59
by nxtboyiii
The code you posted shows nothing on the screen! Aren't you supposed to add CopyBits to the gmtilesb.ric(you have not copybits in it!).