# timebased sleep, 0 FPS issue.

24 replies to this topic

### #21fdmfdm

Member

• Members
• 25 posts

Posted 02 January 2013 - 02:36 PM

Kenneth Gorking, on 31 December 2012 - 04:47 PM, said:

Keep in mind that casting a float to int, internally generates a call to ftol, which is quite slow. As for the VSync issue, I watched a Google I/O video yesterday that describes it pretty well, and why it helps:

thanks for the video, ill watch that as soon as i can ( got work atm) but about the casting a float to int, which is slow.
in the tutorial i followed they had 1 part where they zoomed into a image by a float deltax:
[size=3]Surface* img = [/size][size=3]new [/size][size=3]Surface( "assets/aagun.tga" );[/size]
float dx = 2.0f;
void Game::Tick( float a_DT )
{
Pixel* screen = m_Screen->GetBuffer(), *image = img->GetBuffer();
for ( int x = 0; x < SCRWIDTH; x++ )
for ( int y = 0; y < img->GetHeight(); y++ )
{
int readxpos = (int)(dx * x);
Pixel sample = image[readxpos + y * img->GetPitch()];
screen[x + y * m_Screen->GetPitch()] = sample;
}

dx *= 0.999f;
Sleep( 10 );
}

they later changed that into

Surface* img = new Surface( "assets/aagun.tga" );
int dx = (int)(2.0f * 1024.0f);
void Game::Tick( float a_DT )
{
Pixel* screen = m_Screen->GetBuffer(), *image = img->GetBuffer();
for ( int x = 0; x < SCRWIDTH; x++ )
for ( int y = 0; y < img->GetHeight(); y++ )
{
int readxpos = (dx * x) >> 10;
Pixel sample = image[readxpos + y * img->GetPitch()];
screen[x + y * m_Screen->GetPitch()] = sample;
}

dx = (dx * 999) >> 10;
Sleep( 10 );
}

and this was indeed quit a bit faster
(just to prevent explenation, i understand the concept of this bitshift, and i understand why it is faster, im just asking if it is worth it to change the code to get this, as im making this as an assignment to get into the NHTV "game architecture and design" study (the programming part), and this is the first actual game im making, so i want it to be good, and the second good point is that it would be best to not learn things wrong at the start, to prevent bad habbits (like forgetting that damn ; )

### #22Stainless

Member

• Members
• 582 posts
• LocationSouthampton

Posted 02 January 2013 - 03:31 PM

Well , if someone gave me that code to mark, it wouldn't score very highly.

There are loads of changes you could make to improve the speed.

If it is worthwhile in a real world application is a completely different question.

For each pixel you are doing two multiplies and two method calls which you can remove easily

	 int ipitch =img->GetPitch();
int spitch=m_Screen->GetPitch();
int maxs=ipitch*img->GetHeight();
int rys = ryi = 0;
while (ryi<maxs)
{
ryi+=ipitch;
rys+=spitch;
}


Replacing multiplies with additions is a standard practice from the old days when a multiply was a hell of a lot slower than an addition.
Also moving anything that can be pre-calculated out of an inner loop is good practice.

### #23fdmfdm

Member

• Members
• 25 posts

Posted 02 January 2013 - 04:00 PM

stainless im not sure if i follow you here,
i can understand the part of not putting to much in the inner loop, i have actually started with replacing my current max x and maxy system with a tilecollision system(which is a pain because my hight of a tile is not the same as the height of the upper and lower tile (they both have a part black in them) but it is working as far as i am now)

but this part:

Quote

For each pixel you are doing two multiplies and two method calls which you can remove easily
are you talking about my background?
m_Screen->Clear( 0 );
for (int indexX = 0; indexX <= 39; indexX++)
{
for (int indexY = 0; indexY <= 10; indexY++)
{
int tile = landTile[indexY][indexX];
tileSet[tile]->CopyTo( m_Screen, indexX * 20, indexY * 60 );
}
}

should be something like
m_Screen->Clear( 0 );

while (x < 40)
{
int indexY=0;
while (y<11)
{
int tile = landTile[y][x];
tileSet[tile]->CopyTo( m_Screen, indexX, indexY);
indexY+=60;
y++;
}
indexX+=20;
x++;
y=0;
}
x=0;
indexX=0;


is that what you mean?

(and i can understand that my coding wont get top grade yet XD)

### #24fireside

Senior Member

• Members
• 1590 posts

Posted 02 January 2013 - 05:34 PM

Quote

as im making this as an assignment to get into the NHTV "game architecture and design" study (the programming part)

It looks to me like you may be getting caught up a little too much in details, at least I consider architecture and design to be higher level. I'm just a hobbyist, but what I like to do is write a game so it works, then rewrite it so it's designed well. Things like this would go last if there was a speed problem, but for me, that never happened. The games were too simple. IMO, if it's designed well, it's easy to add variations in game play.
Currently using Blender and Unity.

### #25fdmfdm

Member

• Members
• 25 posts

Posted 02 January 2013 - 05:58 PM

æwell, actually the name of the course is a bit misleading
there are 4 directions to go, and one is programming, which is what im going for, and the assignment is:

Write a small 2D single-player game based on the following theme: 'Bubbles'. Observe the following requirements:
• The maximum upload of your zipped (compressed) game is 10Mb (This should include your executable and files required to run your game. To test that you have all the files make sure you try your game on another computer. Also include the files left in your project after you have done a "clean all" to remove all compiled files).
• The game should be real-time (as opposed to turn-based).
• The game should be your own work. If you use source-code, tutorials and/or art from others then state this clearly.
• The game must run on a PC with Windows 7.
• If your game requires a tool to run it then make sure this tool is available in a free version and make sure the free edition actually runs your code.
• Graphics are completely irrelevant, unless produced by your code (e.g. a particle effect). Feel free to use third-party art or use simple lines and boxes (again state where everything comes from). The presentation is still relevant.

Submit your game, the source-code and the read-me in a single zip-file using the file upload system on this assessment website.

which reminds me, does anyone know how to do that clear all on microsoft visual studio 2010?

as said before, never done this before

and kenneth gorking, that video is amazing, it explains things so beautifully and one last thing fireside,

im actually changing my code so that i can change things quite easily, such as making new lvls with different bubblestartingplaces and different sizes bubbles, and a different levellayout (like my lvl 2 atm in the code that i have on my computer, not here, gives a small pillar in the enter on which the player can jump (and which prevents the player from just walking from left to right

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users