Ian_micheal
September 27th, 2004, 21:42
Im writing a frogger type clone game called froggway every thing is fine game-play-wize but the speed... it's some what based on Â*some of the sprite loading routine Â*on this example. The problem is the game runs at 5FPS Â*but fullspeed on my pc but on the dreamcast using SDL it's super slow totaly unplay-able and im left with having no background image if i want the game playable.
Here's the code example of the way im blitting the sprite and back ground. The main problem is the back ground... with out it the sprite runs the, correct speed. I would like any help , on optmizing this for dreamcast. Or some logic on a better way. I did want to use sdl for this but im thinking I might have to use some other lib that would set my game back a long way.
Example code i use the same way for blitting my sprites so ive included this it will compile on dreamcast if you need the grafix to test pm me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <kos.h>
/* romdisk */
extern uint8 romdisk[];
KOS_INIT_ROMDISK(romdisk);
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
SDL_Surface *g_screenSurface = NULL;
SDL_Surface *g_donutSurface Â*= NULL;
SDL_Surface *Back_Surface Â*= NULL;
const int UPDATE_INTERVAL = 20;
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int main(int argc, char *argv[]);
void init(void);
void shutDown(void);
void loadBMP(void);
Uint32 timeLeft(void);
void render(void);
//-----------------------------------------------------------------------------
// Name: main()
// Desc:
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
Â* Â*init();
Â* Â*loadBMP();
int Done; Â*
Â*
Â* Done = 0;
Â* Â*while( 1 )
Â* Â*{
Â* Â* Â* Â*SDL_Event event;
Â* Â* Â* Â*while( SDL_PollEvent( &event ) )
Â* Â* Â* Â*{
Â* Â* Â* Â* Â* Â*if( event.type == SDL_QUIT ) Â*
Â* Â* Â* Â* Â* Â* Â* Â*Done = 1;
Â* Â* Â* Â* Â* Â*if( event.type == SDL_KEYDOWN )
Â* Â* Â* Â* Â* Â*{
Â* Â* Â* Â* Â* Â* Â* Â*if( event.key.keysym.sym == SDLK_ESCAPE )
Â* Â* Â* Â* Â* Â* Â* Â* Â* Â*Done = 1;
Â* Â* Â* Â* Â* Â*}
Â* Â* Â* Â*}
Â* Â* Â* Â*render();
Â* Â*}
Â* Â*shutDown();
Â* Â*return 0;
}
//-----------------------------------------------------------------------------
// Name: init()
// Desc:
//-----------------------------------------------------------------------------
void init( void )
{
Â* Â*if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
Â* Â*{
Â* Â* Â* Â*printf( "Unable to init SDL: %s\n", SDL_GetError() );
Â* Â* Â* Â*exit(1);
Â* Â*}
Â* Â*atexit( SDL_Quit );
Â* Â*g_screenSurface = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
Â* Â*if( g_screenSurface == NULL )
Â* Â*{
Â* Â* Â* Â*printf( "Unable to set 640x480 video: %s\n", SDL_GetError() );
Â* Â* Â* Â*exit(1);
Â* Â*}
}
//-----------------------------------------------------------------------------
// Name: shutDown()
// Desc:
//-----------------------------------------------------------------------------
void shutDown( void )
{
Â* Â*SDL_FreeSurface( g_donutSurface );
Â* Â*SDL_FreeSurface( g_screenSurface );
}
//-----------------------------------------------------------------------------
// Name: loadBMP()
// Desc:
//-----------------------------------------------------------------------------
void loadBMP( void )
{
Â* Â*// Load the BMP file into a surface
Â* Â*g_donutSurface = SDL_LoadBMP( "/rd/donut.bmp" );
Â* Â*Back_Surface = SDL_LoadBMP( "/rd/back.bmp" );
Â* Â*if( g_donutSurface == NULL )
Â* Â*{
Â* Â* Â* Â*//fprintf(stderr, "Couldn't load \"donut.bmp\"", SDL_GetError());
Â* Â* Â* Â*return;
Â* Â*}
Â* Â*SDL_SetColorKey( g_donutSurface, SDL_SRCCOLORKEY,
Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* SDL_MapRGB( g_donutSurface->format, 0, 0, 0) );
}
//-----------------------------------------------------------------------------
// Name: timeLeft()
// Desc:
//-----------------------------------------------------------------------------
Uint32 timeLeft( void )
{
Â* Â*static Uint32 timeToNextUpdate = 0;
Â* Â*Uint32 currentTime;
Â* Â*currentTime = SDL_GetTicks();
Â* Â*if( timeToNextUpdate <= currentTime )
Â* Â*{
Â* Â* Â* Â*timeToNextUpdate = currentTime + UPDATE_INTERVAL;
Â* Â* Â* Â*return 0;
Â* Â*}
Â* Â*return( timeToNextUpdate - currentTime );
}
//-----------------------------------------------------------------------------
// Name: render()
// Desc:
//-----------------------------------------------------------------------------
void render( void )
{
Â* Â*SDL_Delay( timeLeft() );
Â* Â*SDL_BlitSurface( Back_Surface, NULL,g_screenSurface,NULL);
Â* Â*
Â* Â*//
Â* Â*// Blit the bitmap surface to the main surface
Â* Â*//
Â* Â*static int nPosition = 0;
Â* Â* Â*static int nFrame Â* Â*= 0;
Â* Â*SDL_Rect srcRect;
Â* Â*SDL_Rect destRect;
Â* Â*// Build a source SDL_Rect which will copy only a small portion of the texture.
Â* Â*srcRect.x = (( nFrame % 5 ) * 64);
Â* Â*srcRect.y = (( nFrame / 5 ) * 64);
Â* Â* Â*srcRect.w = 64;
Â* Â*srcRect.h = 64;
Â* Â*destRect.x = nPosition;
Â* Â*destRect.y = 200;
Â* Â*destRect.w = 64;
Â* Â*destRect.h = 64;
Â* Â*SDL_BlitSurface( g_donutSurface, &srcRect, g_screenSurface, &destRect );
Â*
Â* Â*// Update the changed portion of the screen
Â* Â*SDL_UpdateRects( g_screenSurface, 1, &destRect );
Â* Â*//
Â* Â* Â*// Increment the sprite's frame number. Our sprite's animation sequence
Â* Â*// consists of 30 frames (0-29).
Â* Â* Â*//
Â* Â* Â*++nFrame;
Â* Â*if( nFrame > 29 )
Â* Â* Â* Â* Â* Â*nFrame = 0;
Â* Â*//
Â* Â*// Slowly move our sprite across the screen. This demonstrates how to use
Â* Â*// a SDL_Rect destination rect with the SDL_BlitSurface method to place a
Â* Â*// sprite on the main screen surface.
Â* Â*//
Â* Â* Â*++nPosition;
Â* Â*if( nPosition > 640 )
Â* Â* Â* Â* Â* Â*nPosition = 0;
Â* Â*SDL_Flip( g_screenSurface );
}
Thanks for reading. Im looking at geting the speed up this affects most sdl ports as well. Back ground image blits every frame to clear the trail left by the sprite. There must be a way using sdl to auto clear behine the sprite as it moves and not the whole screen. Ive just not found any example out there i could understand.
Here's the code example of the way im blitting the sprite and back ground. The main problem is the back ground... with out it the sprite runs the, correct speed. I would like any help , on optmizing this for dreamcast. Or some logic on a better way. I did want to use sdl for this but im thinking I might have to use some other lib that would set my game back a long way.
Example code i use the same way for blitting my sprites so ive included this it will compile on dreamcast if you need the grafix to test pm me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <kos.h>
/* romdisk */
extern uint8 romdisk[];
KOS_INIT_ROMDISK(romdisk);
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
SDL_Surface *g_screenSurface = NULL;
SDL_Surface *g_donutSurface Â*= NULL;
SDL_Surface *Back_Surface Â*= NULL;
const int UPDATE_INTERVAL = 20;
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int main(int argc, char *argv[]);
void init(void);
void shutDown(void);
void loadBMP(void);
Uint32 timeLeft(void);
void render(void);
//-----------------------------------------------------------------------------
// Name: main()
// Desc:
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
Â* Â*init();
Â* Â*loadBMP();
int Done; Â*
Â*
Â* Done = 0;
Â* Â*while( 1 )
Â* Â*{
Â* Â* Â* Â*SDL_Event event;
Â* Â* Â* Â*while( SDL_PollEvent( &event ) )
Â* Â* Â* Â*{
Â* Â* Â* Â* Â* Â*if( event.type == SDL_QUIT ) Â*
Â* Â* Â* Â* Â* Â* Â* Â*Done = 1;
Â* Â* Â* Â* Â* Â*if( event.type == SDL_KEYDOWN )
Â* Â* Â* Â* Â* Â*{
Â* Â* Â* Â* Â* Â* Â* Â*if( event.key.keysym.sym == SDLK_ESCAPE )
Â* Â* Â* Â* Â* Â* Â* Â* Â* Â*Done = 1;
Â* Â* Â* Â* Â* Â*}
Â* Â* Â* Â*}
Â* Â* Â* Â*render();
Â* Â*}
Â* Â*shutDown();
Â* Â*return 0;
}
//-----------------------------------------------------------------------------
// Name: init()
// Desc:
//-----------------------------------------------------------------------------
void init( void )
{
Â* Â*if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
Â* Â*{
Â* Â* Â* Â*printf( "Unable to init SDL: %s\n", SDL_GetError() );
Â* Â* Â* Â*exit(1);
Â* Â*}
Â* Â*atexit( SDL_Quit );
Â* Â*g_screenSurface = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
Â* Â*if( g_screenSurface == NULL )
Â* Â*{
Â* Â* Â* Â*printf( "Unable to set 640x480 video: %s\n", SDL_GetError() );
Â* Â* Â* Â*exit(1);
Â* Â*}
}
//-----------------------------------------------------------------------------
// Name: shutDown()
// Desc:
//-----------------------------------------------------------------------------
void shutDown( void )
{
Â* Â*SDL_FreeSurface( g_donutSurface );
Â* Â*SDL_FreeSurface( g_screenSurface );
}
//-----------------------------------------------------------------------------
// Name: loadBMP()
// Desc:
//-----------------------------------------------------------------------------
void loadBMP( void )
{
Â* Â*// Load the BMP file into a surface
Â* Â*g_donutSurface = SDL_LoadBMP( "/rd/donut.bmp" );
Â* Â*Back_Surface = SDL_LoadBMP( "/rd/back.bmp" );
Â* Â*if( g_donutSurface == NULL )
Â* Â*{
Â* Â* Â* Â*//fprintf(stderr, "Couldn't load \"donut.bmp\"", SDL_GetError());
Â* Â* Â* Â*return;
Â* Â*}
Â* Â*SDL_SetColorKey( g_donutSurface, SDL_SRCCOLORKEY,
Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* SDL_MapRGB( g_donutSurface->format, 0, 0, 0) );
}
//-----------------------------------------------------------------------------
// Name: timeLeft()
// Desc:
//-----------------------------------------------------------------------------
Uint32 timeLeft( void )
{
Â* Â*static Uint32 timeToNextUpdate = 0;
Â* Â*Uint32 currentTime;
Â* Â*currentTime = SDL_GetTicks();
Â* Â*if( timeToNextUpdate <= currentTime )
Â* Â*{
Â* Â* Â* Â*timeToNextUpdate = currentTime + UPDATE_INTERVAL;
Â* Â* Â* Â*return 0;
Â* Â*}
Â* Â*return( timeToNextUpdate - currentTime );
}
//-----------------------------------------------------------------------------
// Name: render()
// Desc:
//-----------------------------------------------------------------------------
void render( void )
{
Â* Â*SDL_Delay( timeLeft() );
Â* Â*SDL_BlitSurface( Back_Surface, NULL,g_screenSurface,NULL);
Â* Â*
Â* Â*//
Â* Â*// Blit the bitmap surface to the main surface
Â* Â*//
Â* Â*static int nPosition = 0;
Â* Â* Â*static int nFrame Â* Â*= 0;
Â* Â*SDL_Rect srcRect;
Â* Â*SDL_Rect destRect;
Â* Â*// Build a source SDL_Rect which will copy only a small portion of the texture.
Â* Â*srcRect.x = (( nFrame % 5 ) * 64);
Â* Â*srcRect.y = (( nFrame / 5 ) * 64);
Â* Â* Â*srcRect.w = 64;
Â* Â*srcRect.h = 64;
Â* Â*destRect.x = nPosition;
Â* Â*destRect.y = 200;
Â* Â*destRect.w = 64;
Â* Â*destRect.h = 64;
Â* Â*SDL_BlitSurface( g_donutSurface, &srcRect, g_screenSurface, &destRect );
Â*
Â* Â*// Update the changed portion of the screen
Â* Â*SDL_UpdateRects( g_screenSurface, 1, &destRect );
Â* Â*//
Â* Â* Â*// Increment the sprite's frame number. Our sprite's animation sequence
Â* Â*// consists of 30 frames (0-29).
Â* Â* Â*//
Â* Â* Â*++nFrame;
Â* Â*if( nFrame > 29 )
Â* Â* Â* Â* Â* Â*nFrame = 0;
Â* Â*//
Â* Â*// Slowly move our sprite across the screen. This demonstrates how to use
Â* Â*// a SDL_Rect destination rect with the SDL_BlitSurface method to place a
Â* Â*// sprite on the main screen surface.
Â* Â*//
Â* Â* Â*++nPosition;
Â* Â*if( nPosition > 640 )
Â* Â* Â* Â* Â* Â*nPosition = 0;
Â* Â*SDL_Flip( g_screenSurface );
}
Thanks for reading. Im looking at geting the speed up this affects most sdl ports as well. Back ground image blits every frame to clear the trail left by the sprite. There must be a way using sdl to auto clear behine the sprite as it moves and not the whole screen. Ive just not found any example out there i could understand.