(another) simple SDL problem

Started by
7 comments, last by gav86 18 years, 8 months ago
*sigh* all these little hickups are really annoying, my progress would be much quicker if the tutorials actually worked how they say they will! (probably my fault but :P) On execution, the window opens and displays the background, but no image is displayed. The image is in the correct directory. *edit* I was messing around with the rect settings, these are not those used in the tutorial, but i am absolutely certain the ones in the tutorial didnt work either. i cant remember what they are exactly but it shouldnt matter, as I also tried using NULL instead of the rects and had the same problem.
#include "SDL.h"
#include "Defines.h"

#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")

int main(int argc, char **argv)
{
   SDL_Init( SDL_INIT_VIDEO );

   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
      SDL_HWSURFACE | SDL_DOUBLEBUF );
   SDL_WM_SetCaption( WINDOW_TITLE, 0 );

   SDL_Surface* bitmap = SDL_LoadBMP("bat.bmp");

   // Part of the bitmap that we want to draw
   SDL_Rect source;
   source.x = 0;
   source.y = 200;
   source.w = 200;
   source.h = 200;

   // Part of the screen we want to draw the sprite to
   SDL_Rect destination;
   destination.x = 100;
   destination.y = 200;
   destination.w = 200;
   destination.h = 200;

   SDL_Event event;
   bool gameRunning = true;

   while (gameRunning)
   {
      if (SDL_PollEvent(&event))
      {
         if (event.type == SDL_QUIT)
         {
            gameRunning = false;
         }
      }

      SDL_BlitSurface(bitmap, &source, screen, &destination);

      SDL_Flip(screen);
   }

   SDL_FreeSurface(bitmap);

   SDL_Quit();

   return 0;
}
Advertisement
Is source.y = 200; intentional? (i.e. do you really want to skip the first 200 lines in the source image, or was that a mistake?) Anyway, setting source to NULL should cause it to copy the entire image, so that's probably not it.
(destination.w and destination.h are ignored, so you don't need to set those.)

Have you checked the return value from SDL_BlitSurface? It returns -1 if something goes wrong..
Yes, I think that's your problem, except if you try only to display your pic from pixel 200 downwards...
source.x = 0;
source.y = 0;
source.w = 200;
source.h = 200;
would work better...
I just tested the code and it seems fine... only the source might be the problem.
Now get down on your hands and knees and start repeating "Open Source Good, M$ Evil", smacking your head against the pavement after each repetition. Once you have completed your training you may change your first name to GNU/, to show that you are free from the slavery of the closed source world. -Michalson
Thanks , I'll give that a shot right now..

..
..
..

Yes it's returning -1. . . :( Where can I go to figure out whats happening now?

*edit* That test I just did was using NULL instead of the SDL_Rect s - Should both values be null on the blit function or just the source?

Just tried it using BIGFOOT's idea and still not working!
Use SDL's get error function to find the exact error cause.
I copied and pasted your code and it worked when I changed x & y of the source to 0...
   // Part of the bitmap that we want to draw   SDL_Rect source;   source.x = 0;   source.y = 0;   // Part of the screen we want to draw the sprite to   SDL_Rect destination;   destination.x = 0;   destination.y = 0;   destination.w = 200;   destination.h = 200;   SDL_Event event;   bool gameRunning = true;   while (gameRunning)   {      if (SDL_PollEvent(&event))      {         if (event.type == SDL_QUIT)         {            gameRunning = false;         }      }      SDL_BlitSurface(bitmap, &source, screen, &destination);      SDL_Flip(screen);   }



EDIT:ok, then I'm out of ideas :| check the error codes and maybe that might help.
Now get down on your hands and knees and start repeating "Open Source Good, M$ Evil", smacking your head against the pavement after each repetition. Once you have completed your training you may change your first name to GNU/, to show that you are free from the slavery of the closed source world. -Michalson
Quote:Original post by gav86
Yes it's returning -1. . . :( Where can I go to figure out whats happening now?

Try http://www.libsdl.org/cgi/docwiki.cgi/
Also:
printf("SDL_Init Failed: %s\n", SDL_GetError());

should give you more info about the last error.

Quote:*edit* That test I just did was using NULL instead of the SDL_Rect s - Should both values be null on the blit function or just the source?

Just tried it using BIGFOOT's idea and still not working!

SDL_BlitSurface(bitmap, null, screen, &destination); is what I would have suggested, but the other way should have worked too.
Oh, yes.. Have you checked that SDL_LoadBMP doesn't return NULL? It is possible that it failed to load the picture for some reason..
the error was. . .

SDL_UpperBlit: Passed a NULL surface

Does this mean either the source or destination surface isnt properly being created?

..Just checked the error after SD_LoadBMP - "Could not load BMP"

well theres the problem . . hmmm...


BAAAAH!

the bitmap had the "archive" property enabled...

Problem solved. . thanks all involved.

At least I learnt how to find errors in an SDL program and got this damn image loading!

This topic is closed to new replies.

Advertisement