   I ruined SDL?
  I wanted to quickly finish this sliding puzzle game. So instead of going with OpenGL I decided to try SDL. However, here is the problem. I am getting like 2 FPS  if I blit 16 squares!!!! I get 30 FPS if I blit 4 squares. I looked over the code, it looks just way too simple and yet it works not.Can someone please look over this and tell me where I did it wrong?I am 100% that the error will be obvious to anyone who is a little better than me.Here is the pastebin link to the code (I think it is easier to read it that way) http://pastebin.com/d6568a09eHere is the direct code:#include <SDL/SDL.h>#include <cstdio>#include <cstdlib>#include <ctime>#include <SDL/SDL_gfxPrimitives.h>// SDL_gfx Primitives#include <SDL/SDL_framerate.h>// SDL_gfx Framerate Manager#include <SDL/SDL_ttf.h>#include <windows.h>const int SQUARE=8;  // Number of squares to cut the picture inconst int RESOL=512; // Resolution of screen to useint main(int argc, char *argv[]){int frames=0;   int fps=0;  unsigned long tim=0;// Initialize SDLif(SDL_Init(SDL_INIT_VIDEO) == -1){fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());exit(1);}atexit(SDL_Quit);// Initilize the screenSDL_Surface *screen = SDL_SetVideoMode(RESOL, RESOL, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);if(screen == NULL){fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());exit(1);}// Load the imageSDL_Surface* image = SDL_LoadBMP("sampleM.bmp"); // a 512x512 pictureSDL_Surface* square[SQUARE*SQUARE];   // array to keep the square x square imageSDL_Rect rectS;  // SOURCErectS.w=RESOL/SQUARE;rectS.h=RESOL/SQUARE;SDL_Rect rectD;  // DESTINATIONrectD.x=0;rectD.y=0;for(int y=0;y < SQUARE; y++)    // Cut the 512x512 picture into SquarexSquare images.   for(int x=0;x<SQUARE; x++)   {   square[x+y*SQUARE]=SDL_CreateRGBSurface(SDL_HWSURFACE, RESOL/SQUARE,RESOL/SQUARE,   32,0,0,0,0);   rectS.x= x*rectS.w;   rectS.y= y*rectS.h;   SDL_BlitSurface(image,&rectS,square[x+y*SQUARE],&rectD);   square[x+y*SQUARE]=SDL_DisplayFormat(square[x+y*SQUARE]);   }    if(image == NULL)fprintf(stderr, "Unable to load image: %s\n", SDL_GetError());// FRAME RATE MANAGER EXAMPLE// Initialize Frame Rate ManagerFPSmanager fpsm;SDL_initFramerate(&fpsm);SDL_setFramerate(&fpsm, 60); // 60 Frames Per Second    TTF_Init(); atexit(TTF_Quit);TTF_Font *fntArial12 = TTF_OpenFont( "arial.ttf", 12 );bool isRunning = true;  while(isRunning){// Clear the screen//SDL_FillRect(screen, NULL, 0);// Draw the rotated imagefor(int y=0;y < SQUARE; y++)   for(int x=0;x<SQUARE; x++)   {   rectS.x= x*rectS.w;   rectS.y= y*rectS.h;   SDL_BlitSurface(image,NULL,screen,&rectD);   }// GRAPHICS PRIMATIVES EXAMPLEfilledCircleRGBA(screen, 200, 200, 50, 255, 0, 0, 150);SDL_Color clrBlack = { 0,122,0, 0 };char *buffer=(char*)malloc(128*sizeof(char));buffer=itoa(fps,buffer,10);SDL_Surface *sText =  TTF_RenderText_Solid( fntArial12, buffer, clrBlack );// Step 3: Blit that to the screen:SDL_Rect rcDest = { 20,50, 0,0 };// (or use newSDL_Rect( 20,50, 0,0 );)SDL_BlitSurface(sText,NULL, screen,&rcDest ); frames++; // Output to the screenSDL_Flip(screen);//Sleep(10);// FRAME RATE MANAGER EXAMPLE// Delay using the frame rate manager//SDL_framerateDelay(&fpsm);// Check if user quitsSDL_Event event;while(SDL_PollEvent(&event)){switch(event.type){case SDL_QUIT:isRunning = false;break;case SDL_KEYDOWN:if(event.key.keysym.sym == SDLK_ESCAPE)isRunning = false;break;}}float elapsed = abs(clock()-tim)/CLOCKS_PER_SEC;if (elapsed > 1.0)   {fps=frames/(int)(elapsed+.5); frames=0; tim=clock();}}// Clean up the loaded imagereturn 0;}
  I don't mean to be rude, so if I come across that way then I am sorry.Did gamedev do this, or what.  As your code is very hard to understand due to poor formatting.  I've never seen gamedev screw up a format that bad.  Yea...I lost track where I was, so I couldn't even try to see what was causing the problem.Also, may I ask why EVERYTHING is in main?Again, sorry if I came across rude.  If I did then I will edit my post.;Quote:Original post by Chad SmithAlso, may I ask why EVERYTHING is in main?Looks like me, before I understood the benefits of OOP.Sorry I'm not able to read your code because I've never done anything in C++, but I can tell you, your code is a lot more readable if you put things into classes and methods.Hope someone can debug your project, might want to try some SDL specific sites.;  It could be to do with how your using malloc in the middle of your game loop, then  fill it up:char *buffer=(char*)malloc(128*sizeof(char));buffer=itoa(fps,buffer,10);If you move the char* allocation outside the main loop, but keep the second line inside, you should save some time.[edit: I found another example of this:SDL_Surface *sText =TTF_RenderText_Solid( fntArial12, buffer, clrBlack );try putting the declaration outside of the game loop./ edit ]Also:screen = SDL_SetVideoMode(RESOL, RESOL, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);There is no 32 bit color mode as far as graphics cards are concerned. they only work with rgb. The fourth 8 bytes in a 32 bit color image is used to calculate transparency. Try 24. IF you use 0, SDL will decide for itself.   ;Quote:Original post by Chad SmithI don't mean to be rude, so if I come across that way then I am sorry.Did gamedev do this, or what.  As your code is very hard to understand due to poor formatting.  I've never seen gamedev screw up a format that bad.  Yea...I lost track where I was, so I couldn't even try to see what was causing the problem.Also, may I ask why EVERYTHING is in main?Again, sorry if I came across rude.  If I did then I will edit my post.It would have been more readable, despite everything being in main, if the formatting had been correct. Try using either a single tab (my favorite, controversial i know) or a fixed number of spaces, to indent code blocks.   ;Quote:Original post by shotgunnutterIt could be to do with how your using malloc in the middle of your game loop, then  fill it up:char *buffer=(char*)malloc(128*sizeof(char));buffer=itoa(fps,buffer,10);If you move the char* allocation outside the main loop, but keep the second line inside, you should save some time.Also:screen = SDL_SetVideoMode(RESOL, RESOL, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);There is no 32 bit color mode as far as graphics cards are concerned. they only work with rgb. The fourth 8 bytes in a 32 bit color image is used to calculate transparency. Try 24. IF you use 0, SDL will decide for itself.Thanks! The 32 slowed it down 6x times! It still only works at 13 FPS :/Next time I will have to format it nicer and move everything to methods. Thank you everyone for pointing that out.   ;  Some general remarks regarding performance in SDL:- make sure your surfaces are in the same format as the display surface, see SDL_DisplayFormat for that.- avoid using surfaces with an alpha channel set and a hardware display surface (when using alpha channel's, just make a software display surface)- the topic of performance in SDL comes up often, search gamedev.net for some more tips. Now regarding your code, I have just skimmed over it but it looks like you're doing more than blitting 16 sprites. Doesn't this result in 64 blits?for(int y=0;y < SQUARE; y++)   for(int x=0;x<SQUARE; x++) /*blit*/  You haven't posted the circle drawing code, you could try checking what that does. Also, make sure the code that checks the frame rate is good too, not implying it isn't but that's the kind of thing that I always check first since a mistake there renders everything else useless.   ;Quote:Original post by ShadowPhoenixQuote:Original post by shotgunnutterIt could be to do with how your using malloc in the middle of your game loop, then  fill it up:char *buffer=(char*)malloc(128*sizeof(char));buffer=itoa(fps,buffer,10);If you move the char* allocation outside the main loop, but keep the second line inside, you should save some time.Also:screen = SDL_SetVideoMode(RESOL, RESOL, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);There is no 32 bit color mode as far as graphics cards are concerned. they only work with rgb. The fourth 8 bytes in a 32 bit color image is used to calculate transparency. Try 24. IF you use 0, SDL will decide for itself.Thanks! The 32 slowed it down 6x times! It still only works at 13 FPS :/Next time I will have to format it nicer and move everything to methods. Thank you everyone for pointing that out.13? Did we make progress then?OK. I have a few suggestions for how to structure such a program in future. I would make it look something like this:INITIALIZE_SUBSYSTEMS//this section includes SDL_init, TTF_init, etc. Libraries are initialized at this stage. Bare minimum use of variables.ALLOCATE POINTERS//anything which is a pointer to something is allocated here. This includes any pointer which is allocated by a function; set it to NULL here just to be safe, SDL_Surface*'s included.INITIALIZE VARS//anything which is set to a starting value is initialize here. This is where you call functions such as SDL_SetVideoMode(RESOL, RESOL, 24, SDL_HWSURFACE|SDL_DOUBLEBUF); You would also put your loop which you say is to "// Cut the 512x512 picture into SquarexSquare images."START GAME LOOP  HANDLE_EVENTS  UPDATE OBJECTS  DRAW  GET FRAMERATEEND GAME LOOPDELETE POINTERS.SDL_Quit();;Quote:Original post by shotgunnutterQuote:Original post by ShadowPhoenixQuote:Original post by shotgunnutterIt could be to do with how your using malloc in the middle of your game loop, then  fill it up:char *buffer=(char*)malloc(128*sizeof(char));buffer=itoa(fps,buffer,10);If you move the char* allocation outside the main loop, but keep the second line inside, you should save some time.Also:screen = SDL_SetVideoMode(RESOL, RESOL, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);There is no 32 bit color mode as far as graphics cards are concerned. they only work with rgb. The fourth 8 bytes in a 32 bit color image is used to calculate transparency. Try 24. IF you use 0, SDL will decide for itself.Thanks! The 32 slowed it down 6x times! It still only works at 13 FPS :/Next time I will have to format it nicer and move everything to methods. Thank you everyone for pointing that out.13? Did we make progress then?OK. I have a few suggestions for how to structure such a program in future. I would make it look something like this:*** Source Snippet Removed ***Thanks! I will certainly try that for future!As for progress... I just can't think of a reason why 64 blits result in 13 FPS.  In OpenGL I could get millions!Quote:Some general remarks regarding performance in SDL:- make sure your surfaces are in the same format as the display surface, see SDL_DisplayFormat for that.- avoid using surfaces with an alpha channel set and a hardware display surface (when using alpha channel's, just make a software display surface)- the topic of performance in SDL comes up often, search gamedev.net for some more tips.Now regarding your code, I have just skimmed over it but it looks like you're doing more than blitting 16 sprites.Doesn't this result in 64 blits?for(int y=0;y < SQUARE; y++)for(int x=0;x<SQUARE; x++)/*blit*/You haven't posted the circle drawing code, you could try checking what that does. Also, make sure the code that checks the frame rate is good too, not implying it isn't but that's the kind of thing that I always check first since a mistake there renders everything else useless.FPS counter: checked, works flawlessly, accurate to within ~2FPS. I tried setting the surfaces to software, it doesn't help. I do have the DisplayFormat there already :/.   ;  Thank you everyone for support! I got it! :DWhen blitting I did SDL_BlitSurface(image,NULL,screen,&rectD; instead of SDL_BlitSurface(square[x+y*8],NULL,screen,&rectD;!This way I had the whole screen copy over itself 64 times instead of little squares!   