Page 1 of 1

My library

Posted: Sat Oct 10, 2009 10:54 pm
by RandomDever

Code: Select all

if ((time/frame) < (1000/FPS))
		{
			SDL_Delay((1000/FPS) - (time/frame));
		}
What's wrong with this code?
It compiles and runs but it runs at 2 FPS even though I have the const int FPS = 1;

Re: My library

Posted: Sun Oct 11, 2009 4:45 am
by short
46 posts here. You have to try harder then this. /palmface

Re: My library

Posted: Sun Oct 11, 2009 2:05 pm
by MarauderIIC
RandomDever wrote:

Code: Select all

if ((time/frame) < (1000/FPS))
		{
			SDL_Delay((1000/FPS) - (time/frame));
		}
What's wrong with this code?
It compiles and runs but it runs at 2 FPS even though I have the const int FPS = 1;
Let's plug in some values.

time = 500 (a half-second has passed since the start of the program)
frame = 1 (this is the first frame)

if (500/1 < 1000/1)
SDL_Delay((1000/1) - (500/1)) = SDL_Delay(500) = 1/2 second

So, a half-second has passed. And we're delaying a half-second. Hmmm... it could be that you draw before your delay is called, or that I'm making wrong assumptions about what each variable is.

Re: My library

Posted: Sun Oct 11, 2009 2:30 pm
by Pickzell
RandomDever wrote:

Code: Select all

if ((time/frame) < (1000/FPS))
		{
			SDL_Delay((1000/FPS) - (time/frame));
		}
What's wrong with this code?
It compiles and runs but it runs at 2 FPS even though I have the const int FPS = 1;
Why are you dividing things by the FPS then... 1000/1 is 1000...

Re: My library

Posted: Sun Oct 11, 2009 7:29 pm
by MarauderIIC
Pickzell wrote:Why are you dividing things by the FPS then... 1000/1 is 1000...
...
Because maybe he wants to be able to change the FPS later. You know, one of the reasons you use a named constant in the first place?

Re: My library

Posted: Sun Oct 11, 2009 7:47 pm
by hurstshifter
short wrote:46 posts here. You have to try harder then this. /palmface
This

Re: My library

Posted: Mon Oct 12, 2009 4:34 pm
by RandomDever

Code: Select all

#include "MSDL_image.h"
#include "MSDL_render.h"
#include "MSDL_ttf.h"
#include "MSDL_key.h"
#include "MSDL_timer.h"
#include "player.h"

SDL_Event event;

int count = 0;
Uint32 frame = 0;
bool quit = false;
Uint8 *keystates;
Uint32 time = 1;
char buffer[600];
char buffer2[600];

const int Width = 640;
const int Height = 480;
const int BPP = 32;

const Uint32 FPS = 1;

SDL_Surface *screen = NULL;
SDL_Surface *color = NULL;
SDL_Surface *background = NULL;
SDL_Surface *ground = NULL;
SDL_Surface *timer = NULL;
SDL_Surface *keys = NULL;
SDL_Surface *frames =NULL;

Player player;

void Init()
{

	SDL_Init( SDL_INIT_EVERYTHING );
	screen = SDL_SetVideoMode( Width, Height, BPP, SDL_SWSURFACE );
	SDL_WM_SetCaption( "SMB Clone v0.0.1", NULL );
	TTF_Init();
}

void Exit()
{
	SDL_Quit();
}

int main( int argc, char* args[] )
{
	Init();
	background = LoadKeyImage( "background.png", 0, 0, 255 );
	color = LoadImage( "color.png" );
	ground = LoadImage( "ground.png" );
	//, "arblanca.ttf", 140, 0, 0, 255
	BlitSurface( 0, 0, color, screen );
	BlitSurface( 0, 0, background, screen );
	//BlitSurface( (Width / 2) - (timer->w / 2), (Height / 2) - (timer->h / 2), timer, screen );
	while (count < 640)
	{
		BlitSurface( count, 448, ground, screen );
		count += 32;
	}
	RenderTextCenter( "Hello!", "comic.ttf", 140, 255, 0, 0, screen, Height, Width );
	SDL_Flip( screen );
	while( quit == false )
	{
		frame++;
		count = 0;
		time = SDL_GetTicks();
		BlitSurface( 400, 0, keys, screen );
		SDL_Flip( screen );
		keystates = Keystates();
		BlitSurface( 0, 0, color, screen );
		BlitSurface( 0, 0, background, screen );
		while (count < 640)
		{
			BlitSurface( count, 448, ground, screen );
			count += 32;
		}
		RenderTextCenter( "Hello!", "comic.ttf", 140, 255, 0, 0, screen, Height, Width );

		while( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_QUIT )
			{
				quit = true;
			}
		}
		sprintf_s(buffer,"Time: %d", time);
		timer = RenderText( buffer, "arberkley.ttf", 40, 0, 0, 255 );
		BlitSurface ( 0, 0, timer, screen );
		if( keystates[ SDLK_UP ] )
		{
			keys = RenderText( "UP", "comic.ttf", 40, 0, 0, 255  );
		} 
		if ((time/frame) < (1000/FPS))
		{
			SDL_Delay((1000/FPS) - (time/frame));
		}
		sprintf_s(buffer2,"frame: %d", frame);
		frames = RenderText( buffer2, "arberkley.ttf", 40, 0, 0, 255 );
		BlitSurface ( 0, 60, frames, screen );
		SDL_Flip( screen );
		//if ( frame == 1 )
		//{
		//	SDL_Delay( 5000 );
		//}
	}
	Exit();
	return 0;
}
There's the source code.
BTW my MSDL library for time is just returning SDL_GetTicks right now so that shouldn't be a problem.

Re: My library

Posted: Mon Oct 12, 2009 9:01 pm
by MarauderIIC
Nothing jumps out as a problem. How are you telling how many frames happen per second? Maybe that's where the problem is.

Re: My library

Posted: Mon Oct 12, 2009 9:14 pm
by RandomDever
MarauderIIC wrote:Nothing jumps out as a problem. How are you telling how many frames happen per second? Maybe that's where the problem is.
i'm just using SDL_Delay()
It's all handled in the first snippet i posted
pretty much

Re: My library

Posted: Mon Oct 12, 2009 11:48 pm
by Moosader

Re: My library

Posted: Tue Oct 13, 2009 1:53 am
by Scoody
The framelimiter calculates how much time each frame has to its disposal, if it doesn't utilize all that time the rest is put in a delay.

Here you're just dividing the start time with the amount of frames that's been drawn, which makes no sense. You need to take the current ticks and subtract the ticks at the start; then you'll know how much time the current frame used to draw.

Code: Select all

 if ((time/frame) < (1000/FPS))
      {
         SDL_Delay((1000/FPS) - (time/frame));
      }
So (time/frame) should be (SDL_GetTicks() - time)

Code: Select all

Uint32 elapsed = SDL_GetTicks() - time;
 if (elapsed < (1000/FPS))
      {
         SDL_Delay((1000/FPS) - elapsed);
      }

Re: My library

Posted: Thu Oct 15, 2009 12:05 pm
by RandomDever
Scoody wrote:The framelimiter calculates how much time each frame has to its disposal, if it doesn't utilize all that time the rest is put in a delay.

Here you're just dividing the start time with the amount of frames that's been drawn, which makes no sense. You need to take the current ticks and subtract the ticks at the start; then you'll know how much time the current frame used to draw.

Code: Select all

 if ((time/frame) < (1000/FPS))
      {
         SDL_Delay((1000/FPS) - (time/frame));
      }
So (time/frame) should be (SDL_GetTicks() - time)

Code: Select all

Uint32 elapsed = SDL_GetTicks() - time;
 if (elapsed < (1000/FPS))
      {
         SDL_Delay((1000/FPS) - elapsed);
      }

Ummmm.

time is SDL_GetTicks() so subtracting it from itself would mean 0 so it would delay 999 ms every frame.

Re: My library

Posted: Thu Oct 15, 2009 12:38 pm
by MarauderIIC
No, time is the time that passed when you started making the frame. SDL_GetTicks() is the time that has passed until that very point.
But it might be effectively 0.