Is SDL slow or?

I’ve just started codeing a SDL game engine for a RTS game. But The frame
rate sucks. I want to use Isometric tiles in my RTS, my test engine give me
8fps when i blit about 450 objects to the screen. Im running RedHat 7.3 on
a 800mhz 128 mb RAM and a nvida graphiccard. My code isn’t special and I
don’t draw any objects ouside the screen. If anyone like to test compile my
code send a private e-mail to me (perrascout at linux.nu) and I’ll share the
complete source code and grahics.

My goal is o create a rts that can run on a PII 300Mhz or slower if
possible.

A smilar code i M$ Visual Basic and DirectX 7.0 give me at least 30fps with
500 object on the screen.

-------------------------- my code -----------------------------
/*I know the code is a mess of coments i swedish and english
*but the code is just created to test the framrate.
*/

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include “CBmpFont.h”

//Functions
int SetFullscreenFlag(int flag, int width, int height, int depth, char
*name, char *icon);
int InitImages(); //Load all images thats we might need
void DrawScene(); //Main routine to call DrawIMG
void DrawIMG(SDL_Surface *img, int x, int y); //a primitive DrawIMG funct
void DrawIMG(SDL_Surface *img, int SpriteNr, int x, int y, int w, int h);

int frames; //store fps
int gTimeLastSecond; //handy helper for timer.
char strFPS[255];

void createmap();

//Some global stuff
SDL_Surface *screen; //The visable surface
SDL_Surface *back; //a surface to store the back img loaded in InitImage
SDL_Surface *sprite; //same as back…
SDL_Surface *meny;

//N?gra globala variabler att sno.
int gTileWidth; //Pixel
int gTileHeight;//Pixel
int gMapWidth=128; //Rutor
int gMapHeight=128; //Rutor
int gGameWindowWidth; //Rutor
int gGameWindowHeight; //Rutor
int gScreenWidth=800;
int gScreenHeight=600;

int scrollspeed=40; //scroll speeden b?r vara halva tile bredden

unsigned long offsetX;
unsigned long offsety;

class CTile
{
public:
//givetvis s? f?r dessa inte vara publika senare
int tilenr;
int posx;
int posy;
};

CTile gTile[128][128];
CTile gTrees[128][128]; //lager 2
CBmpFont akbar;

//////////////////////////////////////////////////////////////////////////
//class CScreenData contains all useful data about the screen
//

class CScreenData
{
public:
CScreenData(); //constructor
~CScreenData(); //destructor
char *Name; //Project name - shown in windows mode
char *Icon; //Project Icon - shown in windows mode
int ScreenWidth;
int ScreenHeight;
int ScreenBpp;
int Fullscreen; //fullscreen flag (0= fullscreen)

private:
//no private stuff yet
};

CScreenData::CScreenData()
{
//the constructor assing the value to the CScreenData
Name = “No Use For A Name”;
Icon = NULL; //if Icon = NULL: SDL standard icon will be used
ScreenWidth = gScreenWidth;
ScreenHeight = gScreenHeight;
ScreenBpp = 16;
Fullscreen = 0; // 0 set the Fullscreen flag to full screen
}

CScreenData::~CScreenData()
{
}

/////////////////////////////////////////////////////////////////////////
//
//

int main(int argc, char argv[])
{
Uint8
keys; //anv?nd f?r tagent bordet senare…
CScreenData mySetings; //create mySetings
InitImages();
createmap();
akbar.create(“akbar512”, 512);

//init video (add sound if wanted)
if(SDL_Init(SDL_INIT_VIDEO)<0)
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
}
atexit(SDL_Quit);

//seting screen res
SetFullscreenFlag(mySetings.Fullscreen, mySetings.ScreenWidth,
mySetings.ScreenHeight,mySetings.ScreenBpp,
mySetings.Name, mySetings.Icon);

//check if the user want to quit
int done = 0;
while(done == 0)
{
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;}
}
}
/*
Scrollen flyttar “sk?rmen” och inte kartan.
*/
keys = SDL_GetKeyState(NULL);
if(keys[SDLK_RIGHT])
{
for(int y=0; y<gMapHeight; y++)
{
for(int x=0; x<gMapWidth; x++)
{
gTile[y][x].posx-=scrollspeed;
gTrees[y][x].posx-=scrollspeed;
}
}
}

       if(keys[SDLK_LEFT])
       {
       for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posx+=scrollspeed;
          gTrees[y][x].posx+=scrollspeed;
          }
        }
       }

       if(keys[SDLK_DOWN])
       {
        for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posy-=scrollspeed;
            gTrees[y][x].posy-=scrollspeed;
          }
        }
       }
       if(keys[SDLK_UP])
       {
        for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posy+=scrollspeed;
          gTrees[y][x].posy+=scrollspeed;
          }
        }
       }

DrawScene();
}

return 0;
}

/////////////////////////////////////////////////////////////////////////
// Function InitImage() load all graphics into desired surfaces
// Function DrawScene() calls DrawIMG in order to draw a sprite on screen
// Functions DrawIMG() is overlayed to fit

void DrawScene()
{
SDL_FillRect(screen, NULL, 0x000000);
//DrawIMG(back,0,0); //draw the background image
for(int y=0; y<gMapHeight; y++)
{
for(int x=0; x<gMapWidth; x++)
{
if(((gTile[y][x].posx>-80) && (gTile[y][x].posx < (gScreenWidth-170)))&&
((gTile[y][x].posy >-80) && (gTile[y][x].posy<gScreenHeight)))
{
DrawIMG(sprite,gTile[y][x].tilenr,gTile[y][x].posx,gTile[y][x].posy,80,80);
//lite fusk…
if(gTrees[y][x].tilenr>0){DrawIMG(back,gTrees[y][x].tilenr,gTrees[y][x].posx,gTrees[y][x].posy,80,80);}

}
}
}
//Calculate fps:
if( gTimeLastSecond + 1000 <= SDL_GetTicks() )
{
gTimeLastSecond=SDL_GetTicks();
sprintf(strFPS,“FPS: %i”, frames);
frames=0;
}

DrawIMG(meny,640,0);

//Prints FPS:
akbar.drawString(screen,2,5,strFPS);

SDL_Flip(screen); //flip backbuf
frames+=1;
}

int InitImages()
{
//write the rutine to load images here as shown
//define SDL_Surfacesd as Global in the beginig of the .cpp file
back = SDL_LoadBMP(“tr?d.bmp”);
sprite = SDL_LoadBMP(“mark_tiles.bmp”);
meny = SDL_LoadBMP(“meny.bmp”);

//error handleing, edit to fit your needs
if(back == NULL){printf(“could not load tr?d.bmp\n”);}
if(sprite == NULL){printf(“could not load sprites.bmp\n”);}
if(meny == NULL){printf(“could not load meny.bmp\n”);}

if(sprite != NULL){SDL_SetColorKey(sprite,SDL_SRCCOLORKEY,
SDL_MapRGB(sprite->format, 255,0,255));}

if(back != NULL){SDL_SetColorKey(back,SDL_SRCCOLORKEY,
SDL_MapRGB(back->format, 255,0,255));}

return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y= y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int SpriteNr, int x, int y, int w, int h)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;

    SDL_Rect scrImg;
    scrImg.x= w*SpriteNr;
    scrImg.y= 0;
    scrImg.w= w;
    scrImg.h= h;

    SDL_BlitSurface(img ,&scrImg , screen, &dest);

}

/////////////////////////////////////////////////////////////////////////
// Function SetFullscreenFlag() handle the the screen res, it’s might
// be changed at runtime.

int SetFullscreenFlag(int flag, int width, int height, int depth, char
*name, char *icon)
{

if(flag == 0) //set to fullscreen
{
screen =
SDL_SetVideoMode(width,height,depth,SDL_FULLSCREEN|SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
printf(“Unable to set requested screen res: %s\n”,
SDL_GetError());
}
}

if(flag == 1) //set to window mode
{
screen = SDL_SetVideoMode(width,height,depth,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
printf(“Unable to set requested screen res: %s\n”,
SDL_GetError());
};
SDL_WM_SetCaption(name, icon);
}

return 0;
}
int ett_tal;
void createmap()
{
for(int y=0; y < gMapHeight; y++)
{
for(int x=0; x < gMapWidth; x++)
{
ett_tal=(int)((100.0)*rand()/RAND_MAX);
if(ett_tal < 50){gTile[y][x].tilenr=0;}
if((ett_tal > 49)&&(ett_tal <
95)){gTile[y][x].tilenr=(int)((7.0)*rand()/RAND_MAX)+1;}
//(int)((7.0)*rand()/RAND_MAX)+1
if(ett_tal>94){gTile[y][x].tilenr=(int)((2.0)*rand()/RAND_MAX)+8;}
//gTile[y][x].tilenr=ett_tal;

    //mer fusk

ett_tal=(int)((100.0)*rand()/RAND_MAX);
if(ett_tal < 50){gTrees[y][x].tilenr=0;}
if((ett_tal > 49)&&(ett_tal <
95)){gTrees[y][x].tilenr=(int)((7.0)*rand()/RAND_MAX)+1;}
//(int)((7.0)*rand()/RAND_MAX)+1
if(ett_tal>94){gTrees[y][x].tilenr=(int)((2.0)*rand()/RAND_MAX)+8;}

//80=tilebredd(och h?jd) 40 = HALVA tilebredden(h?jden) 20 = 1/4 av tile
BREDD(h?jd)
//y MOD 2 ?r 1 n?r talet ?r udda, detta ?r s? h?rdk?dat att man vill
gr?ta.
if(y%2==1){gTile[y][x].posx=(80x)+40; gTile[y][x].posy=(20y)-40;}
else{gTile[y][x].posx=(80x); gTile[y][x].posy=(20y)-40;}

if(y%2==1){gTrees[y][x].posx=(80x)+40; gTrees[y][x].posy=(20y)-40;}
else{gTrees[y][x].posx=(80x); gTrees[y][x].posy=(20y)-40;}

/*
if(y%2==1){DrawIMG(sprite,gTile[y][x].tilenr,(80x)+40,(20y),80,80);}
else{DrawIMG(sprite,gTile[y][x].tilenr,(80x),(20y),80,80);}
*/
}
}
}

hi, it speeds things up if you convert the gfx files u load in, into the
same format as the screen:
heres example,
Temp = SDL_LoadBMP(“gfx/back.bmp”);
BackDrop=SDL_DisplayFormat(Temp); //convert to screen format for quicker
blitting

hope this helps> ----- Original Message -----

From: perrascout@linux.nu (Fredrik Persson)
Newsgroups: loki.open-source.sdl
To:
Sent: Thursday, November 21, 2002 8:39 PM
Subject: [SDL] is SDL slow or?

I’ve just started codeing a SDL game engine for a RTS game. But The frame
rate sucks. I want to use Isometric tiles in my RTS, my test engine give
me
8fps when i blit about 450 objects to the screen. Im running RedHat 7.3 on
a 800mhz 128 mb RAM and a nvida graphiccard. My code isn’t special and I
don’t draw any objects ouside the screen. If anyone like to test compile
my
code send a private e-mail to me (perrascout at linux.nu) and I’ll share the
complete source code and grahics.

My goal is o create a rts that can run on a PII 300Mhz or slower if
possible.

A smilar code i M$ Visual Basic and DirectX 7.0 give me at least 30fps
with
500 object on the screen.

-------------------------- my code -----------------------------
/*I know the code is a mess of coments i swedish and english
*but the code is just created to test the framrate.
*/

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include “CBmpFont.h”

//Functions
int SetFullscreenFlag(int flag, int width, int height, int depth, char
*name, char *icon);
int InitImages(); //Load all images thats we might need
void DrawScene(); //Main routine to call DrawIMG
void DrawIMG(SDL_Surface *img, int x, int y); //a primitive DrawIMG funct
void DrawIMG(SDL_Surface *img, int SpriteNr, int x, int y, int w, int h);

int frames; //store fps
int gTimeLastSecond; //handy helper for timer.
char strFPS[255];

void createmap();

//Some global stuff
SDL_Surface *screen; //The visable surface
SDL_Surface *back; //a surface to store the back img loaded in
InitImage
SDL_Surface *sprite; //same as back…
SDL_Surface *meny;

//N?gra globala variabler att sno.
int gTileWidth; //Pixel
int gTileHeight;//Pixel
int gMapWidth=128; //Rutor
int gMapHeight=128; //Rutor
int gGameWindowWidth; //Rutor
int gGameWindowHeight; //Rutor
int gScreenWidth=800;
int gScreenHeight=600;

int scrollspeed=40; //scroll speeden b?r vara halva tile bredden

unsigned long offsetX;
unsigned long offsety;

class CTile
{
public:
//givetvis s? f?r dessa inte vara publika senare
int tilenr;
int posx;
int posy;
};

CTile gTile[128][128];
CTile gTrees[128][128]; //lager 2
CBmpFont akbar;

//////////////////////////////////////////////////////////////////////////
//class CScreenData contains all useful data about the screen
//

class CScreenData
{
public:
CScreenData(); //constructor
~CScreenData(); //destructor
char *Name; //Project name - shown in windows mode
char *Icon; //Project Icon - shown in windows mode
int ScreenWidth;
int ScreenHeight;
int ScreenBpp;
int Fullscreen; //fullscreen flag (0= fullscreen)

private:
//no private stuff yet
};

CScreenData::CScreenData()
{
//the constructor assing the value to the CScreenData
Name = “No Use For A Name”;
Icon = NULL; //if Icon = NULL: SDL standard icon will be used
ScreenWidth = gScreenWidth;
ScreenHeight = gScreenHeight;
ScreenBpp = 16;
Fullscreen = 0; // 0 set the Fullscreen flag to full screen
}

CScreenData::~CScreenData()
{
}

/////////////////////////////////////////////////////////////////////////
//
//

int main(int argc, char argv[])
{
Uint8
keys; //anv?nd f?r tagent bordet senare…
CScreenData mySetings; //create mySetings
InitImages();
createmap();
akbar.create(“akbar512”, 512);

//init video (add sound if wanted)
if(SDL_Init(SDL_INIT_VIDEO)<0)
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
}
atexit(SDL_Quit);

//seting screen res
SetFullscreenFlag(mySetings.Fullscreen, mySetings.ScreenWidth,
mySetings.ScreenHeight,mySetings.ScreenBpp,
mySetings.Name, mySetings.Icon);

//check if the user want to quit
int done = 0;
while(done == 0)
{
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;}
}
}
/*
*Scrollen flyttar “sk?rmen” och inte kartan.
*
*/
keys = SDL_GetKeyState(NULL);
if(keys[SDLK_RIGHT])
{
for(int y=0; y<gMapHeight; y++)
{
for(int x=0; x<gMapWidth; x++)
{
gTile[y][x].posx-=scrollspeed;
gTrees[y][x].posx-=scrollspeed;
}
}
}

       if(keys[SDLK_LEFT])
       {
       for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posx+=scrollspeed;
          gTrees[y][x].posx+=scrollspeed;
          }
        }
       }

       if(keys[SDLK_DOWN])
       {
        for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posy-=scrollspeed;
            gTrees[y][x].posy-=scrollspeed;
          }
        }
       }
       if(keys[SDLK_UP])
       {
        for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posy+=scrollspeed;
          gTrees[y][x].posy+=scrollspeed;
          }
        }
       }

DrawScene();
}

return 0;
}

/////////////////////////////////////////////////////////////////////////
// Function InitImage() load all graphics into desired surfaces
// Function DrawScene() calls DrawIMG in order to draw a sprite on screen
// Functions DrawIMG() is overlayed to fit

void DrawScene()
{
SDL_FillRect(screen, NULL, 0x000000);
//DrawIMG(back,0,0); //draw the background image
for(int y=0; y<gMapHeight; y++)
{
for(int x=0; x<gMapWidth; x++)
{
if(((gTile[y][x].posx>-80) && (gTile[y][x].posx < (gScreenWidth-170)))&&
((gTile[y][x].posy >-80) && (gTile[y][x].posy<gScreenHeight)))
{

DrawIMG(sprite,gTile[y][x].tilenr,gTile[y][x].posx,gTile[y][x].posy,80,80);

//lite fusk…

if(gTrees[y][x].tilenr>0){DrawIMG(back,gTrees[y][x].tilenr,gTrees[y][x].posx
,gTrees[y][x].posy,80,80);}

}
}
}
//Calculate fps:
if( gTimeLastSecond + 1000 <= SDL_GetTicks() )
{
gTimeLastSecond=SDL_GetTicks();
sprintf(strFPS,“FPS: %i”, frames);
frames=0;
}

DrawIMG(meny,640,0);

//Prints FPS:
akbar.drawString(screen,2,5,strFPS);

SDL_Flip(screen); //flip backbuf
frames+=1;
}

int InitImages()
{
//write the rutine to load images here as shown
//define SDL_Surfacesd as Global in the beginig of the .cpp file
back = SDL_LoadBMP(“tr?d.bmp”);
sprite = SDL_LoadBMP(“mark_tiles.bmp”);
meny = SDL_LoadBMP(“meny.bmp”);

//error handleing, edit to fit your needs
if(back == NULL){printf(“could not load tr?d.bmp\n”);}
if(sprite == NULL){printf(“could not load sprites.bmp\n”);}
if(meny == NULL){printf(“could not load meny.bmp\n”);}

if(sprite != NULL){SDL_SetColorKey(sprite,SDL_SRCCOLORKEY,
SDL_MapRGB(sprite->format, 255,0,255));}

if(back != NULL){SDL_SetColorKey(back,SDL_SRCCOLORKEY,
SDL_MapRGB(back->format, 255,0,255));}

return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y= y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int SpriteNr, int x, int y, int w, int h)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;

    SDL_Rect scrImg;
    scrImg.x= w*SpriteNr;
    scrImg.y= 0;
    scrImg.w= w;
    scrImg.h= h;

    SDL_BlitSurface(img ,&scrImg , screen, &dest);

}

/////////////////////////////////////////////////////////////////////////
// Function SetFullscreenFlag() handle the the screen res, it’s might
// be changed at runtime.

int SetFullscreenFlag(int flag, int width, int height, int depth, char
*name, char *icon)
{

if(flag == 0) //set to fullscreen
{
screen =

SDL_SetVideoMode(width,height,depth,SDL_FULLSCREEN|SDL_HWSURFACE|SDL_DOUBLEB
UF);

if(screen == NULL)
{
printf(“Unable to set requested screen res: %s\n”,
SDL_GetError());
}
}

if(flag == 1) //set to window mode
{
screen =
SDL_SetVideoMode(width,height,depth,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
printf(“Unable to set requested screen res: %s\n”,
SDL_GetError());
};
SDL_WM_SetCaption(name, icon);
}

return 0;
}
int ett_tal;
void createmap()
{
for(int y=0; y < gMapHeight; y++)
{
for(int x=0; x < gMapWidth; x++)
{
ett_tal=(int)((100.0)*rand()/RAND_MAX);
if(ett_tal < 50){gTile[y][x].tilenr=0;}
if((ett_tal > 49)&&(ett_tal <
95)){gTile[y][x].tilenr=(int)((7.0)*rand()/RAND_MAX)+1;}
//(int)((7.0)*rand()/RAND_MAX)+1
if(ett_tal>94){gTile[y][x].tilenr=(int)((2.0)*rand()/RAND_MAX)+8;}
//gTile[y][x].tilenr=ett_tal;

    //mer fusk

ett_tal=(int)((100.0)*rand()/RAND_MAX);
if(ett_tal < 50){gTrees[y][x].tilenr=0;}
if((ett_tal > 49)&&(ett_tal <
95)){gTrees[y][x].tilenr=(int)((7.0)*rand()/RAND_MAX)+1;}
//(int)((7.0)*rand()/RAND_MAX)+1
if(ett_tal>94){gTrees[y][x].tilenr=(int)((2.0)*rand()/RAND_MAX)+8;}

//80=tilebredd(och h?jd) 40 = HALVA tilebredden(h?jden) 20 = 1/4 av tile
BREDD(h?jd)
//y MOD 2 ?r 1 n?r talet ?r udda, detta ?r s? h?rdk?dat att man vill
gr?ta.
if(y%2==1){gTile[y][x].posx=(80x)+40; gTile[y][x].posy=(20y)-40;}
else{gTile[y][x].posx=(80x); gTile[y][x].posy=(20y)-40;}

if(y%2==1){gTrees[y][x].posx=(80x)+40; gTrees[y][x].posy=(20y)-40;}
else{gTrees[y][x].posx=(80x); gTrees[y][x].posy=(20y)-40;}

/*
if(y%2==1){DrawIMG(sprite,gTile[y][x].tilenr,(80x)+40,(20y),80,80);}
else{DrawIMG(sprite,gTile[y][x].tilenr,(80x),(20y),80,80);}
*/
}
}
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

And how (and where) do I use the BackDrop ?

//Perra

Rewsta wrote:

hi, it speeds things up if you convert the gfx files u load in, into the
same format as the screen:
heres example,
Temp = SDL_LoadBMP(“gfx/back.bmp”);
BackDrop=SDL_DisplayFormat(Temp); //convert to screen format for
quicker
blitting

hope this helps

Newsgroups: loki.open-source.sdl

I’ve just started codeing a SDL game engine for a RTS game. But The frame
rate sucks. I want to use Isometric tiles in my RTS, my test engine give
me
8fps when i blit about 450 objects to the screen. Im running RedHat 7.3
on a 800mhz 128 mb RAM and a nvida graphiccard. My code isn’t special and
I don’t draw any objects ouside the screen. If anyone like to test
compile
my
code send a private e-mail to me (perrascout at linux.nu) and I’ll share the
complete source code and grahics.

My goal is o create a rts that can run on a PII 300Mhz or slower if
possible.

A smilar code i M$ Visual Basic and DirectX 7.0 give me at least 30fps
with
500 object on the screen.

-------------------------- my code -----------------------------
/*I know the code is a mess of coments i swedish and english
*but the code is just created to test the framrate.
*/

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include “CBmpFont.h”

//Functions
int SetFullscreenFlag(int flag, int width, int height, int depth, char
*name, char *icon);
int InitImages(); //Load all images thats we might need
void DrawScene(); //Main routine to call DrawIMG
void DrawIMG(SDL_Surface *img, int x, int y); //a primitive DrawIMG funct
void DrawIMG(SDL_Surface *img, int SpriteNr, int x, int y, int w, int h);

int frames; //store fps
int gTimeLastSecond; //handy helper for timer.
char strFPS[255];

void createmap();

//Some global stuff
SDL_Surface *screen; //The visable surface
SDL_Surface *back; //a surface to store the back img loaded in
InitImage
SDL_Surface *sprite; //same as back…
SDL_Surface *meny;

//N?gra globala variabler att sno.
int gTileWidth; //Pixel
int gTileHeight;//Pixel
int gMapWidth=128; //Rutor
int gMapHeight=128; //Rutor
int gGameWindowWidth; //Rutor
int gGameWindowHeight; //Rutor
int gScreenWidth=800;
int gScreenHeight=600;

int scrollspeed=40; //scroll speeden b?r vara halva tile bredden

unsigned long offsetX;
unsigned long offsety;

class CTile
{
public:
//givetvis s? f?r dessa inte vara publika senare
int tilenr;
int posx;
int posy;
};

CTile gTile[128][128];
CTile gTrees[128][128]; //lager 2
CBmpFont akbar;

//////////////////////////////////////////////////////////////////////////

//class CScreenData contains all useful data about the screen
//

class CScreenData
{
public:
CScreenData(); //constructor
~CScreenData(); //destructor
char *Name; //Project name - shown in windows mode
char *Icon; //Project Icon - shown in windows mode
int ScreenWidth;
int ScreenHeight;
int ScreenBpp;
int Fullscreen; //fullscreen flag (0= fullscreen)

private:
//no private stuff yet
};

CScreenData::CScreenData()
{
//the constructor assing the value to the CScreenData
Name = “No Use For A Name”;
Icon = NULL; //if Icon = NULL: SDL standard icon will be used
ScreenWidth = gScreenWidth;
ScreenHeight = gScreenHeight;
ScreenBpp = 16;
Fullscreen = 0; // 0 set the Fullscreen flag to full screen
}

CScreenData::~CScreenData()
{
}

/////////////////////////////////////////////////////////////////////////
//
//

int main(int argc, char argv[])
{
Uint8
keys; //anv?nd f?r tagent bordet senare…
CScreenData mySetings; //create mySetings
InitImages();
createmap();
akbar.create(“akbar512”, 512);

//init video (add sound if wanted)
if(SDL_Init(SDL_INIT_VIDEO)<0)
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
}
atexit(SDL_Quit);

//seting screen res
SetFullscreenFlag(mySetings.Fullscreen, mySetings.ScreenWidth,
mySetings.ScreenHeight,mySetings.ScreenBpp,
mySetings.Name, mySetings.Icon);

//check if the user want to quit
int done = 0;
while(done == 0)
{
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;}
}
}
/*
*Scrollen flyttar “sk?rmen” och inte kartan.
*
*/
keys = SDL_GetKeyState(NULL);
if(keys[SDLK_RIGHT])
{
for(int y=0; y<gMapHeight; y++)
{
for(int x=0; x<gMapWidth; x++)
{
gTile[y][x].posx-=scrollspeed;
gTrees[y][x].posx-=scrollspeed;
}
}
}

       if(keys[SDLK_LEFT])
       {
       for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posx+=scrollspeed;
          gTrees[y][x].posx+=scrollspeed;
          }
        }
       }

       if(keys[SDLK_DOWN])
       {
        for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posy-=scrollspeed;
            gTrees[y][x].posy-=scrollspeed;
          }
        }
       }
       if(keys[SDLK_UP])
       {
        for(int y=0; y<gMapHeight; y++)
        {
         for(int x=0; x<gMapWidth; x++)
         {
          gTile[y][x].posy+=scrollspeed;
          gTrees[y][x].posy+=scrollspeed;
          }
        }
       }

DrawScene();
}

return 0;
}

/////////////////////////////////////////////////////////////////////////
// Function InitImage() load all graphics into desired surfaces
// Function DrawScene() calls DrawIMG in order to draw a sprite on screen
// Functions DrawIMG() is overlayed to fit

void DrawScene()
{
SDL_FillRect(screen, NULL, 0x000000);
//DrawIMG(back,0,0); //draw the background image
for(int y=0; y<gMapHeight; y++)
{
for(int x=0; x<gMapWidth; x++)
{
if(((gTile[y][x].posx>-80) && (gTile[y][x].posx < (gScreenWidth-170)))&&
((gTile[y][x].posy >-80) && (gTile[y][x].posy<gScreenHeight)))
{

DrawIMG(sprite,gTile[y][x].tilenr,gTile[y][x].posx,gTile[y][x].posy,80,80);

//lite fusk…

if(gTrees[y][x].tilenr>0){DrawIMG(back,gTrees[y][x].tilenr,gTrees[y][x].posx

,gTrees[y][x].posy,80,80);}

}
}
}
//Calculate fps:
if( gTimeLastSecond + 1000 <= SDL_GetTicks() )
{
gTimeLastSecond=SDL_GetTicks();
sprintf(strFPS,“FPS: %i”, frames);
frames=0;
}

DrawIMG(meny,640,0);

//Prints FPS:
akbar.drawString(screen,2,5,strFPS);

SDL_Flip(screen); //flip backbuf
frames+=1;
}

int InitImages()
{
//write the rutine to load images here as shown
//define SDL_Surfacesd as Global in the beginig of the .cpp file
back = SDL_LoadBMP(“tr?d.bmp”);
sprite = SDL_LoadBMP(“mark_tiles.bmp”);
meny = SDL_LoadBMP(“meny.bmp”);

//error handleing, edit to fit your needs
if(back == NULL){printf(“could not load tr?d.bmp\n”);}
if(sprite == NULL){printf(“could not load sprites.bmp\n”);}
if(meny == NULL){printf(“could not load meny.bmp\n”);}

if(sprite != NULL){SDL_SetColorKey(sprite,SDL_SRCCOLORKEY,
SDL_MapRGB(sprite->format, 255,0,255));}

if(back != NULL){SDL_SetColorKey(back,SDL_SRCCOLORKEY,
SDL_MapRGB(back->format, 255,0,255));}

return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y= y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int SpriteNr, int x, int y, int w, int h)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;

    SDL_Rect scrImg;
    scrImg.x= w*SpriteNr;
    scrImg.y= 0;
    scrImg.w= w;
    scrImg.h= h;

    SDL_BlitSurface(img ,&scrImg , screen, &dest);

}

/////////////////////////////////////////////////////////////////////////
// Function SetFullscreenFlag() handle the the screen res, it’s might
// be changed at runtime.

int SetFullscreenFlag(int flag, int width, int height, int depth, char
*name, char *icon)
{

if(flag == 0) //set to fullscreen
{
screen =

SDL_SetVideoMode(width,height,depth,SDL_FULLSCREEN|SDL_HWSURFACE|SDL_DOUBLEB> ----- Original Message -----

From: “Fredrik Persson”
To:
Sent: Thursday, November 21, 2002 8:39 PM
Subject: [SDL] is SDL slow or?
UF);

if(screen == NULL)
{
printf(“Unable to set requested screen res: %s\n”,
SDL_GetError());
}
}

if(flag == 1) //set to window mode
{
screen =
SDL_SetVideoMode(width,height,depth,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(screen == NULL)
{
printf(“Unable to set requested screen res: %s\n”,
SDL_GetError());
};
SDL_WM_SetCaption(name, icon);
}

return 0;
}
int ett_tal;
void createmap()
{
for(int y=0; y < gMapHeight; y++)
{
for(int x=0; x < gMapWidth; x++)
{
ett_tal=(int)((100.0)*rand()/RAND_MAX);
if(ett_tal < 50){gTile[y][x].tilenr=0;}
if((ett_tal > 49)&&(ett_tal <
95)){gTile[y][x].tilenr=(int)((7.0)*rand()/RAND_MAX)+1;}
//(int)((7.0)*rand()/RAND_MAX)+1
if(ett_tal>94){gTile[y][x].tilenr=(int)((2.0)*rand()/RAND_MAX)+8;}
//gTile[y][x].tilenr=ett_tal;

    //mer fusk

ett_tal=(int)((100.0)*rand()/RAND_MAX);
if(ett_tal < 50){gTrees[y][x].tilenr=0;}
if((ett_tal > 49)&&(ett_tal <
95)){gTrees[y][x].tilenr=(int)((7.0)*rand()/RAND_MAX)+1;}
//(int)((7.0)*rand()/RAND_MAX)+1
if(ett_tal>94){gTrees[y][x].tilenr=(int)((2.0)*rand()/RAND_MAX)+8;}

//80=tilebredd(och h?jd) 40 = HALVA tilebredden(h?jden) 20 = 1/4 av
tile
BREDD(h?jd)
//y MOD 2 ?r 1 n?r talet ?r udda, detta ?r s? h?rdk?dat att man vill
gr?ta.
if(y%2==1){gTile[y][x].posx=(80x)+40; gTile[y][x].posy=(20y)-40;}
else{gTile[y][x].posx=(80x); gTile[y][x].posy=(20y)-40;}

if(y%2==1){gTrees[y][x].posx=(80x)+40; gTrees[y][x].posy=(20y)-40;}
else{gTrees[y][x].posx=(80x); gTrees[y][x].posy=(20y)-40;}

/*
if(y%2==1){DrawIMG(sprite,gTile[y][x].tilenr,(80x)+40,(20y),80,80);}
else{DrawIMG(sprite,gTile[y][x].tilenr,(80x),(20y),80,80);}
*/
}
}
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

The same way, presumably, as you were using it when you simply
loaded it directly (SDL_LoadBMP).

In your original code, I think it was this:

back = SDL_LoadBMP(“tr?d.bmp”);

Note: You’ll want to do a few tests & a free when you do what Rewsta
suggested:

tmp = SDL_LoadBMP(…);

if (tmp == NULL)
{
/* Abort! Print SDL_GetError() to stderr is typically good idea;
then exit(1); */
}

back = SDL_DisplayFormat(tmp);

if (back == NULL)
{
/* Abort! (See above) */
}

SDL_FreeSurface(tmp);

Lather, rinse, repeat.

Check most any SDL project and you should see examples of code like this.

-bill!On Thu, Nov 21, 2002 at 10:51:09PM +0100, Fredrik Persson wrote:

And how (and where) do I use the BackDrop ?

Thanks.

My code compile but when i try to run it i get a error message
"Segmenteringsfel", I use g++ to compile my code. Im not shure but
I think the problem is ‘back = SDL_DisplayFormat(tmpSurface);’ both back and
tmpSurface are global. I never get any msg from ‘if(back == NULL)’.

tmpSurface=SDL_LoadBMP(“tr?d.bmp”);
if(tmpSurface == NULL){printf( “SDL error: %s\n”, SDL_GetError());}
if(tmpSurface !=
NULL){SDL_SetColorKey(tmpSurface,SDL_SRCCOLORKEY,(Uint16)SDL_MapRGB(tmpSurface->format,
255,0,255));}
back = SDL_DisplayFormat(tmpSurface);
if(back == NULL){printf( “SDL error: %s\n”, SDL_GetError());}
SDL_FreeSurface(tmpSurface);

Bill Kendrick wrote:> On Thu, Nov 21, 2002 at 10:51:09PM +0100, Fredrik Persson wrote:

And how (and where) do I use the BackDrop ?

The same way, presumably, as you were using it when you simply
loaded it directly (SDL_LoadBMP).

In your original code, I think it was this:

back = SDL_LoadBMP(“tr?d.bmp”);

Note: You’ll want to do a few tests & a free when you do what Rewsta
suggested:

tmp = SDL_LoadBMP(…);

if (tmp == NULL)
{
/* Abort! Print SDL_GetError() to stderr is typically good idea;
then exit(1); */
}

back = SDL_DisplayFormat(tmp);

if (back == NULL)
{
/* Abort! (See above) */
}

SDL_FreeSurface(tmp);

Lather, rinse, repeat.

Check most any SDL project and you should see examples of code like this.

-bill!

Thanks.

My code compile but when i try to run it i get a error message
"Segmenteringsfel", I use g++ to compile my code. Im not shure but
I think the problem is ‘back = SDL_DisplayFormat(tmpSurface);’ both back and
tmpSurface are global. I never get any msg from ‘if(back == NULL)’.

tmpSurface=SDL_LoadBMP(“tr?d.bmp”);
if(tmpSurface == NULL){printf( “SDL error: %s\n”, SDL_GetError());}

You’re not exiting.
It could be that the segfault occurs before the STDOUT buffer (“printf()”)
has a chance to spit out.

Try adding
fflush(stdout);

after the printf()

Of course, when the error happens, you seem to ignore it.

if(tmpSurface !=
NULL){SDL_SetColorKey(tmpSurface,SDL_SRCCOLORKEY,(Uint16)SDL_MapRGB(tmpSurface->format,
255,0,255));}

Well, except here, where you don’t set color key if it failed.

back = SDL_DisplayFormat(tmpSurface);

Here, you still use it, even if it’s NULL!

if(back == NULL){printf( “SDL error: %s\n”, SDL_GetError());}
SDL_FreeSurface(tmpSurface);

Here, you free tmpSurface, even if it’s NULL :slight_smile:

I assume later on you still use “back” even if it’s NULL :slight_smile:

Best thing to do in this case is to exit(1)

Can’t find your images, then somethings wrong!

-bill!On Fri, Nov 22, 2002 at 12:16:18AM +0100, Fredrik Persson wrote:

I think the problem is ‘back = SDL_DisplayFormat(tmpSurface);’ both back and
tmpSurface are global. I never get any msg from ‘if(back == NULL)’.
tmpSurface=SDL_LoadBMP(“tr?d.bmp”);
if(tmpSurface == NULL){printf( “SDL error: %s\n”, SDL_GetError());}
if(tmpSurface !=

Well, if you get NULL back you probably don’t want to keep running as the
code currently does. :wink:

Remember that printf is buffered output, so it may crash the app before it
has a chance to get dumped to the console.

This is one of the aspects of SDL that I don’t like - it automatically
will do pixel conversion for you without your knowledge. This results in
really slow performance. The emulator I ported to using SDL had major
speed problems because of it and I had no idea why until I REALLY dug into
it. Of course the alternative is getting errors when blitting two
nonidentical surfaces but that seems preferable to having really sluggish
performance.

–>Neil-------------------------------------------------------------------------------
Neil Bradley In the land of the blind, the one eyed man is not
Synthcom Systems, Inc. king - he’s a prisoner.
ICQ #29402898

I’ve just started codeing a SDL game engine for a RTS game. But The frame
rate sucks.

But why is it so slow ? I have a similar problem. First i thought it’s because
i update the whole screen with SDL_Flip(). Then i reprogrammed my engine, so
that it used SDL_UpdateRects(). But even now graphics output is quite slow
and choppy. Yes, i convert all images with SDL_DisplayFormat. I’m still not
satisfied. Scrolling is just not smooth enough. Guess, i will start using
OpenGL for 2D Output.

sorry my bad, pinched from my own code hehe.
heres your code.

int InitImages()
{
//write the rutine to load images here as shown
//define SDL_Surfacesd as Global in the beginig of the .cpp file
back = SDL_LoadBMP(“tr?d.bmp”);
sprite = SDL_LoadBMP(“mark_tiles.bmp”);
meny = SDL_LoadBMP(“meny.bmp”);

do this,
TEMP = SDL_LoadBMP(“tr?d.bmp”);
back=SDL_DisplayFormat(Temp); //convert to screen format
TEMP = SDL_LoadBMP(“mark_tiles.bmp”);
sprite=SDL_DisplayFormat(Temp); //convert to screen format

kind of thing. yeh! have fun

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1On Friday 22 November 2002 13:40, Rewsta wrote:

do this,
TEMP = SDL_LoadBMP(“tr?d.bmp”);
back=SDL_DisplayFormat(Temp); //convert to screen format
TEMP = SDL_LoadBMP(“mark_tiles.bmp”);
sprite=SDL_DisplayFormat(Temp); //convert to screen format

Note that you’ll have to perform SDL_FreeSurface(TEMP); twice in there, or
you’ll leak memory. Bill Kendrick already posted a correct version
(complete with error checking), just thought I’d point that out again.

cu,
Nicolai
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE93i48sxPozBga0lwRAvJ0AJ9rhTfJey9H4u80MmlrUBDJ+DjK9gCgjUWR
qgn/8C1yIWsOMAXrG2V38J4=
=21I5
-----END PGP SIGNATURE-----

But why is it so slow ? I have a similar problem. First i thought it’s
because
i update the whole screen with SDL_Flip(). Then i reprogrammed my engine, so
that it used SDL_UpdateRects(). But even now graphics output is quite slow
and choppy. Yes, i convert all images with SDL_DisplayFormat. I’m still not
satisfied. Scrolling is just not smooth enough. Guess, i will start using
OpenGL for 2D Output.

Some common mistakes you must avoid:

1)blitting from software RAM to video RAM is sluggish. screen buffer and
textures must all be in the same place.
2)SDL_Flip must be used only with SDL_HWSURFACE | SDL_DOUBLEBUF; if you’re
not using a double buffer, SDL_Flip is equivalent to
SDL_FillRect(screen,0,0,0,0) (the whole screen).
3)If you’re using Linux, you can’t have any hardware acceleration with
plain SDL. Consider using glSDL
If you’re using Windows, SDL_HWSURFACE uses full DirectDraw acceleration.

Guido Imperiale

@CRUSADER_KY-------------------------------
CRV?ADER/KY
KnowledgE is PoweR

Altay Cebe wrote:

But why is it so slow ? I have a similar problem. First i thought it’s because
i update the whole screen with SDL_Flip(). Then i reprogrammed my engine, so
that it used SDL_UpdateRects(). But even now graphics output is quite slow
and choppy. Yes, i convert all images with SDL_DisplayFormat. I’m still not
satisfied. Scrolling is just not smooth enough. Guess, i will start using
OpenGL for 2D Output.

That’s very hard to say without seeing your code. There could be a variety of
reasons:

  1. Are you calling SDL_SetVideoMode with the SDL_ANYFORMAT flag? Without this
    flag, you will -always- get the pixel depth you specify - even if it doesn’t match
    the physical screen. Yet more realtime surface conversion.

  2. Are you calling SDL_SetVideoMode with the SDL_HWSURFACE flag? Without this,
    I’m fairly certain SDL will always create a software backbuffer for the screen.

  3. Are you calling SDL_SetVideoMode with the SDL_DOUBLEBUF flag? If you specify
    SDL_HWSURFACE and don’t specify SDL_DOUBLEBUF, you might actually be writing
    direct to the screen, which is not good because of sync problems etc.

  4. Are you calling SDL_UpdateRects several times per refresh? There is overhead
    behind this operation, it is reccomended to be called once per screen update.

  5. Are you using alpha channels? If so, it might be faster to use software
    surfaces than hardware ones. No known video card supports alpha-channel
    acceleration in 2D.

SDL’s got fiddly stuff like this, 'cause it has to work with dozens of different
platforms, not just DirectX.> SDL mailing list

SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Thanks Crusader and Corona688 for your help ! Unfortunately it’s still quite
slow. Guess i have to check my code again.

Altay Cebe wrote:

Thanks Crusader and Corona688 for your help ! Unfortunately it’s still quite
slow. Guess i have to check my code again.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hm… could be helpful to find out exactly WHAT is being so slow. Whipped up
some code-clocking stuff - based on SDL, but only uses SDL_GetTicks() - which
produces an output like this on stderr:

[ VideoStuff()]
[ SDL_SetVideoMode()]
[ SDL_SetVideoMode()] Took 19 ticks(-+10)
[ VideoStuff()] Took 121 ticks(-+10)

It’s nested, i.e. VideoStuff() calls SDL_SetVideoMode(), and as usual, can’t
expect it to be more accurate than 10 ticks.

CodeClock.c, CodeClock.h, and an example main.c are attached. Hope this’ll help
stort stuff out.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: main.c
Type: application/x-unknown-content-type-cfile
Size: 424 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20021124/281db77a/attachment.bin
-------------- next part --------------
A non-text attachment was scrubbed…
Name: CodeClock.h
Type: application/x-unknown-content-type-hfile
Size: 435 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20021124/281db77a/attachment-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed…
Name: CodeClock.c
Type: application/x-unknown-content-type-cfile
Size: 1327 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20021124/281db77a/attachment-0002.bin

Just a reminder… as far as I know there has never been an issue with
SDL_GetTicks() accuracy more than +/- 1 ms… It’s SDL_Delay() that’s
only accurate to about 10 ms on most systems, and it always rounds up.
Just to clarify: The issue (on Linux anyway) is that the scheduler’s
time-slice size is 10 ms, so when you give up your time slice to another
process, the kernel won’t interrupt that process for about 10ms…

Hope that helped…

-LorenOn Sun, 2002-11-24 at 11:54, Corona688 wrote:

It’s nested, i.e. VideoStuff() calls SDL_SetVideoMode(), and as usual, can’t
expect it to be more accurate than 10 ticks.

Hello!

I can’t confirm in loco now so I’ll have to ask :slight_smile:
Does the SDL_ttf lib works on fb console? Looks like I can’t render ttf
fonts on console.

TIA and []s

Adilson.