Public Group

# Question on this image in SDL

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

## 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_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 on other sites
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 on other sites
I'm working on some SDL tutorials:
Lazy Foo's SDL tutorials

They might be useful for you.

[Edited by - Lazy Foo on August 9, 2007 7:26:34 PM]

##### 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 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 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 on other sites
Quote:
 Original post by ZarisI 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 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.

##### 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 on other sites
<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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015226
×