Archived

This topic is now archived and is closed to further replies.

scpedicini

Question about game logic loop.

Recommended Posts

I'm fairly new to SDL/game programming. Anyway, I wrote a scrolling star field with variable speed stars to help me understand timing and game loops. I've been using SDL_GetTicks(). My game loop looks like this:
while(RunGame) {

	HandleEvents(MainEvent);
	GameLogic();
	Render();
}
Here is my star struct:
      
struct Star {
	int x, y;
	int size;
	int update_time;
	int last_time_recorded;
} Stars[LARGE_STAR_MAX];
GameLogic() just checks to see if current_time - Star.last_time_recorded >= Star[i].update_time and if this is the case, then increment the stars x position by 1 pixel and Star[i].last_time_recorded = current_time This system seems to work pretty well. The framerate can be really high, but star movement stays pretty constant. Render contains:
void Render()
{
	SDL_FillRect(Screen, NULL, 0x00000000);

	for(int i=0; i<LARGE_STAR_MAX; i++) {
		DrawStar(Stars[i].size, Stars[i].x, Stars[i].y, 1);
	}

	SDL_Flip(Screen);

}
DrawStar just uses pixel arrays and puts them to screen using the sample putpixel method that was included in the SDL documentation. Anyway, I've noticed that even if a Star has an update_time of 1 ms, it definitely does not move 1000 pixels in 1 second. I was wondering if there was any way to speed up my loop. Is SDL_FillRect slow? Should I be erasing the screen in a different manner? Thanks for any help about this, Shaun [edited by - scpedicini on March 29, 2004 1:25:17 AM] [edited by - scpedicini on March 29, 2004 8:59:54 AM] [edited by - scpedicini on March 29, 2004 9:01:19 AM]

Share this post


Link to post
Share on other sites
Alrighty then.

Well, anyway I figured out why I wasn''t getting a very good
framerate. Part of the reason is that I was running in
Windowed mode, but the main problem was that I wasn''t converting
my surfaces to my display format, thus they were being converted manually every time I wanted to blit them to the screen. After I used DisplayFormat, the loop sped up quite a bit.

Shaun

Share this post


Link to post
Share on other sites

Here''s some tips.. It''s pseudocode though, so it wont work without some rewrite.



int stars_lastupdated;

struct Star
{
int y;
float x, z; // z is just to dermine distance - it''s still 2d

// int size; // use Z to determine size

// int update_time; // don''t need to store this

// int last_time_recorded; // or this

} Stars[LARGE_STAR_MAX];

void GameLogic()
{
current_time = SDL_GetTicks();
if(current_time > stars_lastupdated) // it has to be, but you never know :) + you can insert a treshold

{
for(int i=0; i<LARGE_STAR_MAX; i++)
{
Stars[i].x += Stars[i].z * ((current_time - stars_lastupdated) / 1000.0f); // stars far away go slow

if( Stars[i].x > screen_x ) Stars[i].x -= screen_x + 20; // reusable stars! nice :)

}
stars_lastupdated = current_time;
}
}

void Render()
{
SDL_FillRect(Screen, NULL, 0x00000000);
for(int i=0; i<LARGE_STAR_MAX; i++)
{
DrawStar((int)Stars[i].z, Stars[i].x, (int)Stars[i].y, 1);
}
SDL_Flip(Screen);
}



I changed x to float to make it possible to move the star less than 1 pixel. I changed size to z and made it float. Size is based on distance right? Small stars are far away and go slow. Big stars are near and go fast. If I''m wrong just reimplement it .

By doing this you i think it will go smoother and because of the delta time you won''t get "hickups" if the framerate isn''t constant. If nothing else you''ll save memory on the struct

Z = 1 : full speed, while z = 0.5 : half speed

Share this post


Link to post
Share on other sites