My library

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

My library

Post 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;
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: My library

Post by short »

46 posts here. You have to try harder then this. /palmface
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: My library

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Pickzell
Chaos Rift Junior
Chaos Rift Junior
Posts: 233
Joined: Sat May 16, 2009 10:21 am

Re: My library

Post 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...
I'm an altogether bad-natured Cupid.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: My library

Post 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?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: My library

Post by hurstshifter »

short wrote:46 posts here. You have to try harder then this. /palmface
This
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: My library

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: My library

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: My library

Post 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
User avatar
Moosader
Game Developer
Game Developer
Posts: 1081
Joined: Wed May 07, 2008 12:29 am
Current Project: Find out at: http://www.youtube.com/coderrach
Favorite Gaming Platforms: PC, NES, SNES, PS2, PS1, DS, PSP, X360, WII
Programming Language of Choice: C++
Location: Kansas City
Contact:

Re: My library

Post by Moosader »

Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: My library

Post 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);
      }
RandomDever
Chaos Rift Regular
Chaos Rift Regular
Posts: 198
Joined: Thu Mar 26, 2009 8:42 pm
Current Project: My Engine
Programming Language of Choice: C++

Re: My library

Post 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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: My library

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply