Jump to content
  • Advertisement
Sign in to follow this  
Chad Smith

Question on this image in SDL

This topic is 4832 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

Alright, I am starting SDL and I am studing more and more. I wrote this code only looking at my tutorial 2 times. I know how some stuff should be used, so now I am trying to remember some of it by heart. Anyway, when I was putting a sprite up on the screen, I tested it out and it never showed up. I couldn't find out why. So, I continued and made it where the background I had for the Sprite was to where I couldn't see it. Anyway, I compiled it and it compiles perfectly. The only problem is, is that it comes up and automatically disapears. I have made a loop that will loop to keep the window up. Anyway, here is the code:
#include <SDL.h>



const int WINDOW_HEIGHT=800;
const int WINDOW_WIDTH=600;
const char* WINDOW_TITLE="Dislpaying A Window in SDL";

int main(int argc, char **argv)
{
     SDL_Init(SDL_INIT_VIDEO);
     SDL_Surface* screen= SDL_SetVideoMode(WINDOW_HEIGHT, WINDOW_WIDTH,0,
                                           SDL_HWSURFACE | SDL_DOUBLEBUF);
     SDL_WM_SetCaption(WINDOW_TITLE,0);

    SDL_Surface* bitmap= SDL_LoadBMP("sprite.bmp");
    SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) );

    //Part of the bitmap that we have, that we want to draw
    SDL_Rect source;
    source.x=25;
    source.y=64;
    source.w=66;
    source.h=45;

   SDL_Rect destination;
   destination.x=100;
   destination.y=100;
   destination.w=65;
   destination.h=44;


    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();
     
}

I am sure once I find out what it is I will go..."OH YEAH, I KNEW THAT!" Anyways, why is it doing that? I feel stupid posting this, as I should know this! Also, can anyone post the link to the download for the TTF_lib? I am using Dev-CPP as my compiler, so make sure it will work for that. I can only find it for Visual Studio.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You have a problem with the line

SDL_SetColorKey(bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(bitmap->format, 255, 0, 255) );

Maybe it should have been:

SDL_SetColorKey(screen, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 255, 0, 255) );

I don't know SDL so I am just guessing...

Like this it compiles and runs the loop.

-- Per

Share this post


Link to post
Share on other sites
lazyfoo, I will check out your tutorials tommorow when I have a chance.


Also,
I will try what the person above you said. Let me go try real fast.

*goes and tries*


No, it didn't help. It does the same thing. Thanks for trying to help anyway.

Anyone else?

Share this post


Link to post
Share on other sites
Wait..you can like see it for a split second then cant?

If you cant see it at all, try taking out the source rect from the BlitSurface function. If you see the rest of the bitmap, that's your issue..but if not..eh i'm outta ideas. You also might try taking out the SDL_DOUBLEBUF flag and see what that does.

Also, you might throw in some error checking..make sure the image is there, the bg is ok, etc..

Hope it helps.

Share this post


Link to post
Share on other sites
I put in the exact same code into my system and the image displays. I would clean your build and them play around with the sprite. It the only thing different then in my system.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zaris
I put in the exact same code into my system and the image displays. I would clean your build and them play around with the sprite. It the only thing different then in my system.


Same here (well with visual studio), make sure you check your image's size as well as the coords you are using. I used a 640x480 sprite.bmp file with Magneta as the fill color and scribbled over it.

For SDL_ttf get the Dev-Pak here.

Share this post


Link to post
Share on other sites
Ok, thanks for trying to help people.

Yeah, the screen pops up for not even a tenth of a sec.


It works for y'all? Did anyone of y'all use Dev-CPP? my friend used Dev-CPP and I can compile all of his source code that he uses esspically a pong clone we are working on. I will work on this more tommorow by cleaning up my system more and checking some other stuff out. It just seems weird that a simple thing like this isn't working.


Also,
Thanks for the TTF Dev Pack. I will need that to program the Menu system in our pong game. the pong game might be finished in 2 days. Only got the scoring to test and program the menu systems. Anyways, thanks again.


Chad.

Share this post


Link to post
Share on other sites
I used the SDL 1.2.8 DevPak with DevCPP 4.9.9.2 and I got the same results as I did with Visual Studio. I just did a copy paste of your code. Can you tell us the dimensions of your BMP that you are using? Perhaps if you can upload it somewhere we can test using that. I mean other than that, I'm just not sure why your program isn't working right.

Share this post


Link to post
Share on other sites
I dont know if this will help you, but:
<source>
destination.w=65;
destination.h=44;
</source>

These lines are useless, as the docs tell us that these values are ignored. It might be a long shot, but try omitting them.

Otherwise, play around with your video settings. Change your bit depth, resolution, toggle fullscreen, etc.

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!