Jump to content
  • Advertisement
Sign in to follow this  
gav86

(another) simple SDL problem

This topic is 4769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

*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;
}

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!