Functions: break to maintain small size?
Yes! less than 1 page!
 38%  [ 5 ]
No. wasteful.
 61%  [ 8 ]
Total Votes : 13

Nice, you're down with c++?
It's my main man baby!

Here's the edited rendering function. I replaced a bunch of if/else loops with smaller checks that should do the exact same thing. I also fixed a few math errors (just a few chunks that canceled each other out). Anywho, I have no way of testing it, though.


Code:

void renderWalls()
{
   u32 loop;
   s16 curAngle;
   s32 gridX;
   s32 gridY;
   u16* destBuffer = videoBuffer;
   u8 x,y;
   double horzLength;
   double vertLength;
   double* minLength;
   u32 lineLength;
   char darkgray = 0;
   double fdistNextX;
   double fdistNextY;
   int ndistNextX;
   int ndistNextY;
   int horzY;
   double horzX;
   int vertX;
   double vertY;
   curAngle = nPlayerAngle + 30;
   if(curAngle >= 360){curAngle -= 360;}

   for(loop = 0; loop < SCREENWIDTH; loop+=4)
   {
      if (curAngle == 0 || curAngle == 180)
      {
         horzLength = 9999999.00;
      }
      else
      {
         if (curAngle < 180)
         {
            horzY = nPlayerY; //EDITED: Math always cancels itself out
            ndistNextY = -64;
            double amountChange = ((s32) (horzY - nPlayerY) ) * tableINVTAN[curAngle];
            if (amountChange != 0){amountChange *= -1;} //EDITED: Should do the same thing
            horzX = nPlayerX + amountChange;
            horzY--;
         }
         else
         {
            horzY = nPlayerY + 64; //EDITED: Math always cancels out
            ndistNextY = 64;
            double amountChange = ((s32)(horzY - nPlayerY)) * tableINVTAN[curAngle];
            if (amountChange != 0){amountChange *= -1;} //EDITED: Should do the same thing
            horzX = nPlayerX + amountChange;
         }
         fdistNextX = (64.00 * tableINVTAN[curAngle]);
         if ( fdistNextX != 0){fdistNextX *= -1;} //EDITED: Should do the same thing
         while (true)
         {
            gridX = (s32)(horzX / 64);
            gridY = (s32)(horzY / 64);
            if (gridX >= MAPWIDTH || gridY >= MAPHEIGHT || gridX < 0 || gridY < 0)
            {
               horzLength = 9999999.00;
               break;
            }
            else if (fMap[gridX+gridY*MAPHEIGHT])
            {
               horzLength = (horzX - nPlayerX) * tableINVCOS[curAngle];
               break;
            }
            horzX += fdistNextX;
            horzY += ndistNextY;
         }
      }
      if (curAngle == 90 || curAngle == 270)
      {
         vertLength = 9999999.00;
      }
      else
      {
         if (curAngle < 90 || curAngle > 270)
         {
            vertX = nPlayerX + 64; //EDITED: Math always cancels out
            ndistNextX = 64;
            double amountChange = tableTAN[curAngle]*((s32)(vertX-nPlayerX));
            if (amountChange != 0){amountChange *= -1;} //EDITED: Should do the same thing
            vertY = nPlayerY + amountChange;
         }
         else
         {
            vertX = nPlayerX; //EDITED: Math always cancels out
            ndistNextX = -64;         
            double amountChange = tableTAN[curAngle]*((s32)(vertX-nPlayerX));
            if (amountChange != 0){amountChange *= -1;} //EDITED: Should do the same thing
            vertY = nPlayerY + amountChange;
            vertX--;
         }
         fdistNextY = 64.00 * tableTAN[curAngle];
         if (fdistNextY != 0){fdistNextY *= -1;} //EDITED: Should do the same thing
         while (true)
         {
            gridX = (s32)(vertX / 64);
            gridY = (s32)(vertY / 64);
            if (gridX >= MAPWIDTH || gridY >= MAPHEIGHT || gridX < 0 || gridY < 0)
            {
               vertLength = 9999999.00;
               break;
            }
            else if (fMap[gridX+gridY*MAPHEIGHT])
            {
               vertLength = (vertY - nPlayerY) * tableINVSIN[curAngle];
               break;
            }
            vertX += ndistNextX;
            vertY += fdistNextY;
         }
      }
      if (vertLength < 0){vertLength *= -1;}
      if (horzLength < 0){horzLength *= -1;}
      if (vertLength < horzLength)
      {
         minLength = &vertLength;
         darkgray = 1;
      }
      else
      {
         darkgray = 0;
         minLength = &horzLength;
      }
      (*minLength) = (*minLength) * tableCOS[distAngle(curAngle, nPlayerAngle)];
      lineLength = absint((s32)((64.00 / *minLength) * PLAY_LENGTH)   );
      int end = (80 - lineLength/2);
      int start;
      if (end < 0)
      {
         end = 160;
         start = 0;
      }
      else
      {
         start = end;
         end += lineLength;
      }
      u32 where = loop/2 + start*120;
      if (darkgray)
      {
         for(y = start; y<end; y++)
         {
               destBuffer[where] = 0x0f0f;
               destBuffer[where+1] = 0x0f0f;
               where += 120;
         }
      }
      else
      {
         for(y = start; y<end; y++)
         {
               destBuffer[where] = 0x0e0e;
               destBuffer[where+1] = 0x0e0e;
               where += 120;
         }
      }
      curAngle -= 1;
      if (curAngle < 0) curAngle += 360;
   }
}


I hope it works better. I'm gonna make a rough guess and say that it'll speed your rendering up by around 25%ish (if it works ok).
Wow I had no idea GBA was programmed in C++. I assumed it would be in assembly... Do they program computer games in assembly or do they use some other high level language? And what about console games?
something1990 wrote:
Wow I had no idea GBA was programmed in C++. I assumed it would be in assembly... Do they program computer games in assembly or do they use some other high level language? And what about console games?


lol! no one (or very few) seriously program anything large in assembly. C/C++ compiled is a near as fast as hand coded assembly (do remember that C/C++ gets compiled to machine code...its not interperated on the fly, and good compilers, such as GCC, optimize the code), yet it is much easier and quicker (and therefore, for the game companies, CHEAPER). Doom3, for example, is programmed in C++

And, actually, newer games are becoming even higher language oriented. Several games, EVE Online being one of them, use C/C++ for the main engine, and Python (or similar) for everything else (such as scripted events, game control, etc...)

The same applies for console games, and has for a looong time (in fact, Sony even had an SDK kit that people could make homebrew games for the PS1, but they only made/sold like 1,000 of em or so - i believe the language of choice was either C or C++)
yo kilr, thanks for the site... ive had trouble finding stuff on raycasting(hence the bad program)

but i still have the main issue... i really dont understand the programming behind it... im just getting into trig in school... and the raycasting thing takes a lil more than my teachers know... i could always pair up with my fav physics teacher... he seems to know a lot about what im doin... but anywhos... that site still didnt help much... the compilation gave me errors... too many missing files... ooh ooh... no to mension i cant translate any of it >.<
Wow. C/C++ is used for everything these days. That and Java... I'm on my way!
JincS wrote:

... I have no way of testing it, though ...


sry i havent gotten back to yall... but yeah.. this code was properly executed. nice work.... but its got a big problem....

Before:
pixels hard to differenciate... but slow
Now:
25% speed increase
program had a huge difficulty in actually finding a wall...

JincS, if you could.... could you find its issue... the wall registry is going straight through the actual wall... or their being draw askew... wierd description i know... but hey... i cant really describe it better...
something1990 wrote:
Wow. C/C++ is used for everything these days. That and Java... I'm on my way!
Always has been...nothing new.
Quote:
Wow I had no idea GBA was programmed in C++. I assumed it would be in assembly... Do they program computer games in assembly or do they use some other high level language? And what about console games?


Game programmers stopped using pure assembly in games way back in the late '80s Very Happy But they still use a lot of inline assembly; the only place that you'd really see it now would be in homebrew apps mostly Smile
Ultimate Dev'r wrote:
Game programmers stopped using pure assembly in games way back in the late '80s[...]

Sorry, what? What have I been doing for several years?? Very Happy
KermMartian wrote:
Ultimate Dev'r wrote:
Game programmers stopped using pure assembly in games way back in the late '80s[...]

Sorry, what? What have I been doing for several years?? Very Happy


programming in one of the only surviving ASM langauge Wink

assembly's like latin - its dead Laughing (yet there are still "enthusiasts" using it Razz )
I learned Latin in school, but it doesn't mean I'm an enthusiast. Taking it for 6 years made me want to run away, not embrace it. Very Happy
And its true, there's nothing like getting right down into actual CPU opcodes for writing incredibly fast and efficient code.
I'll look into it. I think I already know where the problem is, but not for sure (in my edited chunks)...
What do you think it is?
The loops where I did some modifying:

Original code:

Code:

if (curAngle < 90 || curAngle > 270) // facing right
{
    if (amountChange < 0) amountChange *= -1; // should be pos
}
else
{
    if (amountChange > 0) amountChange *= -1; // should be neg
}


I changed to just:

Code:

amountChange *= -1;


But I forgot to take into account any angles that might already be positive. If any were at the time of execution, my little change would make it negative, thus f*in everything up...I'll redo all of the loops and post it in awhile...
'Scuse the double post...

Here's my 2nd Revision of function. Hopefully it'll work better, but it' may be a tiny bit slower (just a smidgen though)...


Code:

void renderWalls()
{
   u32      loop;
   s16      curAngle;
   s32      gridX;
   s32      gridY;
   u16*   destBuffer = videoBuffer;
   u8      x,y;
   
   double   horzLength;
   double   vertLength;
   double*   minLength;
   u32      lineLength;
   char darkgray = 0;
   double   fdistNextX;
   double   fdistNextY;
   int      ndistNextX;
   int      ndistNextY;
   int      horzY;
   double  horzX;
   int      vertX;
   double   vertY;

   curAngle = nPlayerAngle + 30;
   if (curAngle >= 360) curAngle -= 360;

   for (loop = 0; loop < SCREENWIDTH; loop+=4)
   {
      if (curAngle == 0 || curAngle == 180)
      {
         horzLength = 9999999.00;
      }
      else
      {
         if (curAngle < 180)
         {
            horzY = nPlayerY; //2nd revision
            ndistNextY = -64;
            double amountChange = ((s32) (horzY - nPlayerY) ) * tableINVTAN[curAngle];
            if ((curAngle < 90 || curAngle > 270) && amountChange < 0 ){amountChange *= -1;} //2nd revision
            else {if (amountChange > 0){amountChange *= -1;}} //2nd revision
            horzX = nPlayerX + amountChange;
            horzY--;
         }
         else
         {
            horzY = nPlayerY + 64; //2nd revision
            ndistNextY = 64;
            double amountChange = ((s32)(horzY - nPlayerY)) * tableINVTAN[curAngle];
            if ((curAngle < 90 || curAngle > 270) && amountChange < 0 ){amountChange *= -1;} //2nd revision
            else {if (amountChange > 0){amountChange *= -1;}} //2nd revision
            horzX = nPlayerX + amountChange;
         }
         fdistNextX = (64.00 * tableINVTAN[curAngle]);
         if ( (curAngle < 90 || curAngle>270) && fdistNextX < 0){fdistNextX *= -1;} //2nd revision
         else {if (fdistNextX > 0){fdistNextX *= -1;}} //2nd revision
         while (true)
         {
            gridX = (s32)(horzX / 64);
            gridY = (s32)(horzY / 64);
            if (gridX >= MAPWIDTH || gridY >= MAPHEIGHT || gridX < 0 || gridY < 0)
            {
               horzLength = 9999999.00;
               break;
            }
            else if (fMap[gridX+gridY*MAPHEIGHT])
            {
               horzLength = (horzX - nPlayerX) * tableINVCOS[curAngle];
               break;
            }
            horzX += fdistNextX;
            horzY += ndistNextY;
         }
      }
      if (curAngle == 90 || curAngle == 270)
      {
         vertLength = 9999999.00;
      }
      else
      {
         if (curAngle < 90 || curAngle > 270)
         {
            vertX = nPlayerX + 64; //2nd revision
            ndistNextX = 64;
            double amountChange = tableTAN[curAngle]*((s32)(vertX-nPlayerX));
            if (curAngle < 180 && amountChange > 0){amountChange *= -1;} //2nd revision
            else {if (amountChange < 0){amountChange *= -1;}} //2nd revision
            vertY = nPlayerY + amountChange;
         }
         else
         {
            vertX = nPlayerX; //2nd revision
            ndistNextX = -64;         
            double amountChange = tableTAN[curAngle]*((s32)(vertX-nPlayerX));
            if (curAngle < 180 && amountChange > 0){amountChange *= -1;} //2nd revision
            else {if (amountChange < 0){amountChange *= -1;}} //2nd revision
            vertY = nPlayerY + amountChange;
            vertX--;
         }
         fdistNextY = 64.00 * tableTAN[curAngle];
         if (curAngle < 180 && fdistNextY > 0){if (fdistNextY > 0) fdistNextY *= -1;} //2nd revision
         else {if (fdistNextY < 0) fdistNextY *= -1;} //2nd revision
         while (true)
         {
            gridX = (s32)(vertX / 64);
            gridY = (s32)(vertY / 64);
            if (gridX >= MAPWIDTH || gridY >= MAPHEIGHT || gridX < 0 || gridY < 0)
            {
               vertLength = 9999999.00;
               break;
            }
            else if (fMap[gridX+gridY*MAPHEIGHT])
            {
               vertLength = (vertY - nPlayerY) * tableINVSIN[curAngle];
               break;
            }
            vertX += ndistNextX;
            vertY += fdistNextY;
         }
      }
      if (vertLength < 0) vertLength *= -1;
      if (horzLength < 0) horzLength *= -1;
      if (vertLength < horzLength)
      {
         minLength = &vertLength;
         darkgray = 1;
      }
      else
      {
         darkgray = 0;
         minLength = &horzLength;
      }
      (*minLength) = (*minLength) * tableCOS[distAngle(curAngle, nPlayerAngle)];
      lineLength = absint((s32)((64.00 / *minLength) * PLAY_LENGTH)   );
      int end = (80 - lineLength/2);
      int start;
      if (end < 0)
      {
         end = 160;
         start = 0;
      }
      else
      {
         start = end;
         end += lineLength;
      }
      u32 where = loop/2 + start*120;
      if (darkgray)
      {
         for(y = start; y<end; y++)
         {
               destBuffer[where] = 0x0f0f;
               destBuffer[where+1] = 0x0f0f;
               where += 120;
         }
      }
      else
      {
         for(y = start; y<end; y++)
         {
               destBuffer[where] = 0x0e0e;
               destBuffer[where+1] = 0x0e0e;
               where += 120;
         }
      }
      curAngle -= 1;
      if (curAngle < 0) curAngle += 360;
   }
}


Oh, and I'm sorry that I didn't format the code. I hate formatting...
Laughing Don't worry about it. I didn't inspect the code, but for that angle function, it sounds like you just need an abs() function...
That would work just as good, but it would add more function calls to the proggy. That few though prolly wouldn't slow the game down any though...

@Komak57: Just noticed this: uncomment the #include "math.h" line at the top of your code. That way, you can use the abs(), sin(), cos(), and tan() functions (and their inverse functions). That way, you won't have to use lookup tables for your angles (saves you RAM, which WILL speed your game up by a fair amount).
Lookup tables are usually faster than trig functions though, aren't they?
KermMartian wrote:
Lookup tables are usually faster than trig functions though, aren't they?


kinda. On a limited RAM system (like the GBA) the extra RAM is greatly needed for other things, such as images and such. Not sure he'll have to worry about that with a simple map w/ no textures like this one, but it could play a role down the line.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 4
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement