Sign in to follow this  
Thrust

Slow performance from SDL

Recommended Posts

Hi, I am currently making a 2d online platformer using SDL, SDL_Image, SFont, and Winsock as my librarys. For some reasons when my game is run on my home PC is acts exactly as we would want it to. It has no slowdown, only when more then one client is running. Now no matter where else I run the game I get slowdown. Our character is suppost to move fluidly around the screen where as on a computer capable of running a more complex 2d game called GISH, our slows down terribly. There is no hardware specific code in our programming and I dont believe I am creating any hooks anywhere. What could be a cause of that and what could I do to change it. You can download the game here: http://www.sffkt.tk and check out the problem for yourself.

Share this post


Link to post
Share on other sites
Make sure that you convert all surfaces to the native display format.
Kinda basic, but it never hurts to check..

Oh, btw, you project page doesn't appear to render correctly in firefox.

Share this post


Link to post
Share on other sites
Quote:
Original post by doynax
Make sure that you convert all surfaces to the native display format.
Kinda basic, but it never hurts to check..

Oh, btw, you project page doesn't appear to render correctly in firefox.



Our whole site is destroyed. It runs really bad. Im working on a new one.

Can you explain what you mean about the surfaces?

Share this post


Link to post
Share on other sites
If your surfaces are not the same format as the display, they have to be converted each time you render them. You can overcome this by using SDL_DisplayFormat, which takes a surface and converts it to the same format as the display.

Share this post


Link to post
Share on other sites
Quote:
Original post by The Rug
If your surfaces are not the same format as the display, they have to be converted each time you render them. You can overcome this by using SDL_DisplayFormat, which takes a surface and converts it to the same format as the display.


when I run SDL_Displayformat I get an illegal operation while trying to run my program


SDL_Surface *tsprite1 = IMG_Load("gfx/sprites/hentai1.png");
SDL_Surface *sprite1 = SDL_DisplayFormat(tsprite1);



It looks right, but for some reason it wont work. Is there any other boosts I can do to performace so that the game is actuially playable on other peoples PCs? I dont understand why it does this for mine. When I look at code from other games theres is ran the same way as mine, yet mine runs slow on other PCs, even ones that are better then mine.

Share this post


Link to post
Share on other sites
Maybe you forgot to created the display before try to convert to it's format?
Make sure that you release the original surface too.

Share this post


Link to post
Share on other sites
Ah now this seems to be a pretty big problem. I use all of my graphics as globals so that I can use them in any function without having to send anything between them. The problem is, I cant set the display before the globals. Eek! Here is my code:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <winsock2.h>
#include <SDL/SDL.h>
#include "SDL/SFont.h"
#include "SDL/SDL_image.h"


//Globals
std::string ipAddress = "";
std::string text = "";
int screen;
int map [20][25]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//Functions
void drawMap();
void drawScene();
void Jump(void);
void FallDown(void);
void HandleJump(void);
void HandleFall(void);
void PushUp(void);
void PullDown(void);
void drawTitle();
void drawTitle2();
void mainMenu();
void playerSelect();
void connectionMenu();
void game();
void getInput();
void host();
void join();
//draws bitmaps, x = o is top, y = 0 is left, sprite is the image
void draw(int x, int y, SDL_Surface *sprite, SDL_Surface *screen);

//Surfaces
SDL_Surface *Menu;
SDL_Surface *Playerselect;
SDL_Surface *tilemap;
SDL_Surface *Connectmenu;
//Sprite
SDL_Surface *tsprite1 = IMG_Load("gfx/sprites/hentai1.png");
SDL_Surface *sprite1 = SDL_DisplayFormat(tsprite1);
SDL_Surface *tsprite2 = IMG_Load("gfx/sprites/eye1.png");
SDL_Surface *sprite2 = SDL_DisplayFormat(tsprite2);
SDL_Surface *tsprite3 = IMG_Load("gfx/sprites/robot1.png");
SDL_Surface *sprite3 = SDL_DisplayFormat(tsprite3);
SDL_Surface *tsprite4 = IMG_Load("gfx/sprites/ratzilla1.png");
SDL_Surface *sprite4 = SDL_DisplayFormat(tsprite4);
SDL_Surface *tsprite5 = IMG_Load("gfx/sprites/ninja1.png");
SDL_Surface *sprite5 = SDL_DisplayFormat(tsprite5);
SDL_Surface *tsprite6 = IMG_Load("gfx/sprites/legendaryfighter1.png");
SDL_Surface *sprite6 = SDL_DisplayFormat(tsprite6);
SDL_Surface *tsprite7 = IMG_Load("gfx/sprites/schoolgirl1.png");
SDL_Surface *sprite7 = SDL_DisplayFormat(tsprite7);
SDL_Surface *tsprite8 = IMG_Load("gfx/sprites/swordfighter1.png");
SDL_Surface *sprite8 = SDL_DisplayFormat(tsprite8);
SDL_Surface *tcloud = IMG_Load("gfx/tiles/cloud.png");
SDL_Surface *cloud = SDL_DisplayFormat(tcloud);
SDL_Surface *tsun = IMG_Load("gfx/tiles/sun.png");
SDL_Surface *sun = SDL_DisplayFormat(tsun);
//Tiles
SDL_Surface *ttile1 = IMG_Load("gfx/tiles/tile1.png");
SDL_Surface *tile1 = SDL_DisplayFormat(ttile1);
SDL_Surface *ttile2 = IMG_Load("gfx/tiles/tile2.png");
SDL_Surface *tile2 = SDL_DisplayFormat(ttile2);
SDL_Surface *tgrass1 = IMG_Load("gfx/tiles/grass1.png");
SDL_Surface *grass1 = SDL_DisplayFormat(tgrass1);
//GUI
SDL_Surface *tmain1 = IMG_Load("gfx/gui/main1.png");
SDL_Surface *main1 = SDL_DisplayFormat(tmain1);
SDL_Surface *tbackground = IMG_Load("gfx/gui/background1.png");
SDL_Surface *background = SDL_DisplayFormat(tbackground);
SDL_Surface *tinfoback = IMG_Load("gfx/gui/infoback.png");
SDL_Surface *infoback = SDL_DisplayFormat(tinfoback);
//Player selection screen
SDL_Surface *tchoicebackground = IMG_Load("gfx/gui/choicebackground.png");
SDL_Surface *choicebackground = SDL_DisplayFormat(tchoicebackground);
SDL_Surface *tchoice1 = IMG_Load("gfx/gui/playerchoice1.png");
SDL_Surface *choice1 = SDL_DisplayFormat(tchoice1);
SDL_Surface *thentai = IMG_Load("gfx/gui/hentai.png");
SDL_Surface *hentai = SDL_DisplayFormat(thentai);
SDL_Surface *tchoice2 = IMG_Load("gfx/gui/playerchoice2.png");
SDL_Surface *choice2 = SDL_DisplayFormat(tchoice2);
SDL_Surface *teye = IMG_Load("gfx/gui/eye.png");
SDL_Surface *eye = SDL_DisplayFormat(teye);
SDL_Surface *tchoice3 = IMG_Load("gfx/gui/playerchoice3.png");
SDL_Surface *choice3 = SDL_DisplayFormat(tchoice3);
SDL_Surface *trobot = IMG_Load("gfx/gui/robot.png");
SDL_Surface *robot = SDL_DisplayFormat(trobot);
SDL_Surface *tchoice4 = IMG_Load("gfx/gui/playerchoice4.png");
SDL_Surface *choice4 = SDL_DisplayFormat(tchoice4);
SDL_Surface *tratzilla = IMG_Load("gfx/gui/ratzilla.png");
SDL_Surface *ratzilla = SDL_DisplayFormat(tratzilla);
SDL_Surface *tchoice5 = IMG_Load("gfx/gui/playerchoice5.png");
SDL_Surface *choice5 = SDL_DisplayFormat(tchoice5);
SDL_Surface *tninja = IMG_Load("gfx/gui/ninja.png");
SDL_Surface *ninja = SDL_DisplayFormat(tninja);
SDL_Surface *tchoice6 = IMG_Load("gfx/gui/playerchoice6.png");
SDL_Surface *choice6 = SDL_DisplayFormat(tchoice6);
SDL_Surface *tlegendaryfighter = IMG_Load("gfx/gui/legendaryfighter.png");
SDL_Surface *legendaryfighter = SDL_DisplayFormat(tlegendaryfighter);
SDL_Surface *tchoice7 = IMG_Load("gfx/gui/playerchoice7.png");
SDL_Surface *choice7 = SDL_DisplayFormat(tchoice7);
SDL_Surface *tschoolgirl = IMG_Load("gfx/gui/schoolgirl.png");
SDL_Surface *schoolgirl = SDL_DisplayFormat(tschoolgirl);
SDL_Surface *tchoice8 = IMG_Load("gfx/gui/playerchoice8.png");
SDL_Surface *choice8 = SDL_DisplayFormat(tchoice8);
SDL_Surface *tswordfighter = IMG_Load("gfx/gui/swordfighter.png");
SDL_Surface *swordfighter = SDL_DisplayFormat(tswordfighter);
SDL_Surface *tchoice1a = IMG_Load("gfx/gui/playerchoice1.1.png");
SDL_Surface *choice1a = SDL_DisplayFormat(tchoice1a);
SDL_Surface *tchoice2a = IMG_Load("gfx/gui/playerchoice2.1.png");
SDL_Surface *choice2a = SDL_DisplayFormat(tchoice2a);
SDL_Surface *tchoice3a = IMG_Load("gfx/gui/playerchoice3.1.png");
SDL_Surface *choice3a = SDL_DisplayFormat(tchoice3a);
SDL_Surface *tchoice4a = IMG_Load("gfx/gui/playerchoice4.1.png");
SDL_Surface *choice4a = SDL_DisplayFormat(tchoice4a);
SDL_Surface *tchoice5a = IMG_Load("gfx/gui/playerchoice5.1.png");
SDL_Surface *choice5a = SDL_DisplayFormat(tchoice5a);
SDL_Surface *tchoice6a = IMG_Load("gfx/gui/playerchoice6.1.png");
SDL_Surface *choice6a = SDL_DisplayFormat(tchoice6a);
SDL_Surface *tchoice7a = IMG_Load("gfx/gui/playerchoice7.1.png");
SDL_Surface *choice7a = SDL_DisplayFormat(tchoice7a);
SDL_Surface *tchoice8a = IMG_Load("gfx/gui/playerchoice8.1.png");
SDL_Surface *choice8a = SDL_DisplayFormat(tchoice8a);

//Font
SFont_Font* Font = SFont_InitFont(IMG_Load("font/font1.png"));

//struct Player
typedef struct Player
{
int x_pos;
int y_pos;
int x_vel;
int y_vel;
int chartype;
int playtype;
bool jump;
bool connected;
bool inair;
}Player;
//for GUI
typedef struct button
{
int x_pos;
int y_pos;
}button;

Player p1;
Player p2;

//for the first player
int spriteh;
int spritew;

button JoinButton;
button HostButton;
//------------------------------------------------------------------------------

using namespace std;

int main(int argc, char *argv[])
{
mainMenu();
return 0;
}



How the heck would I fix that problem?

Share this post


Link to post
Share on other sites
Putting aside the reasons to not use globals here :) you could originally set your surfaces to NULL and make a new function called init_graphics() where you:
1. initialize SDL
2. initialize the screen to what size + bpp + flags
3. load up the graphics to surfaces
then you call intit_graphics before your mainMenu function so that things still happen in the same order except that SDL and the screen get initialized first.

Share this post


Link to post
Share on other sites
Thank you, that was a very good idea. Now I can fortunatly move on to bigger and better things. I have also tried using SDL_UpdateRect instead of SDL_Flip, I hope that increases some fps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this