[NXC] Optimize this code?
Posted: 27 Jan 2012, 01:17
Hi,
If you download muntoo's SCR_FILE_LIB and run this program you will notice how slow the particles move.
**BTW, I did not make the algorithm for this water simulation.
I want the water particles to move faster but still smooth, but I do not know how to do it other than decreasing the # of water particles.
Here's the code:
Thanks, and have a nice day,
nxtboy III
If you download muntoo's SCR_FILE_LIB and run this program you will notice how slow the particles move.
**BTW, I did not make the algorithm for this water simulation.
I want the water particles to move faster but still smooth, but I do not know how to do it other than decreasing the # of water particles.
Here's the code:
Code: Select all
#include "SCR_File_lib.nxc"
#define NUM_OF_PARTICLES 100
task main()
{
byte map[100][64];
int px[NUM_OF_PARTICLES];
int py[NUM_OF_PARTICLES];
bool pactive[NUM_OF_PARTICLES];
int tpx,tpy;
byte ScreenMem[];
ArrayInit(ScreenMem, 0x00, 800);
ScreenMem[799] = 0x0F;
for(int c=0; c < NUM_OF_PARTICLES; c++)
{
pactive[c]=1;
}
for(int c=0; c < 24; c++)
{
px[c]=40;
py[c]=c+40;
}
for(int c=24; c < 40; c++)
{
px[c]=41;
py[c]=c+40-24;
}
for(int c=40; c < NUM_OF_PARTICLES; c++)
{
px[c]=42;
py[c]=c;
}
for(int c=60; c < NUM_OF_PARTICLES; c++)
{
px[c]=39;
py[c]=c-20;
}
for(int c=80; c < NUM_OF_PARTICLES; c++)
{
px[c]=38;
py[c]=c-40;
}
for(int c=0; c < 100; c++)
{
map[c][30]=1;
}
for(int c=0; c < 100; c++)
{
map[c][0]=1;
}
for(int c=0; c < 64; c++)
{
map[99][c]=1;
}
for(int c=0; c < 64; c++)
{
map[0][c]=1;
}
for(int c=0; c < 20; c++)
{
map[Random(98)+1][30]=0;
}
int x,y;
x=30;
y=20;
repeat(20)
{
x++;
y++;
map[x][y]=1;
}
x=20;
y=40;
repeat(20)
{
x++;
y--;
map[x][y]=1;
}
unsigned long dispAddr; //Draws the map and saves it to ScreenMem
dispAddr = DisplayDisplay();
SetDisplayDisplay(addressOf(ScreenMem));
for(int c=0; c < 100; c++)
{
for(int d=0; d < 64; d++)
{
if(map[c][d] == 1)
{
PointOut(c,d);
}
}
}
SetDisplayDisplay(dispAddr);
DisplayScreenMem(ScreenMem);
while(1)
{
SetDisplayFlags( DISPLAY_REFRESH_DISABLED );
ClearScreen();
DisplayScreenMem(ScreenMem);
for(int c=0; c < NUM_OF_PARTICLES; c++)
{
if(pactive[c])
{
tpx=px[c];
tpy=py[c];
map[tpx][tpy]=0;
if(map[tpx][tpy-1] == 0)
{
tpy--;
}
else if(map[tpx+1][tpy] == 0)
{
if(map[tpx-1][tpy] == 0)
{
tpx+=Random(3)-1;
}
else
tpx++;
}
else if(map[tpx-1][tpy] == 0)
{
tpx--;
}
PointOut(tpx,tpy);
map[tpx][tpy]=1;
px[c]=tpx;
py[c]=tpy;
}
}
SetDisplayFlags( DISPLAY_ON | DISPLAY_REFRESH );
Wait(17);
}
}
nxtboy III