Jump to content
  • Advertisement
Sign in to follow this  
AlexFZ

SDL_BlitSurface not working correctly

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

Hey, I'm having a very odd issue with just simply drawing surfaces onto the screen. I have a method, applySurface, which just gets the offsets into a rectangle for me then blits the surface:
void applySurface (int x, int y, SDL_Surface* source,
		SDL_Surface* destination, SDL_Rect* clip = NULL) {
	// SDL only accepts coordinates from rectangles.
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;

	// Draw or "blit" the source onto the destination
	SDL_BlitSurface(source, clip, destination, &offset);
}
Now I am just simply trying to draw the basic outline of the background and menus onto my game (this is for an RPG battle system). Here is what the relevant part of the code looks like:
void applySurfaces() {
	// Grass background
	applySurface(0,0,background,screen);

	// Top battle message
	applySurface(147,20,message,screen);

	/** THE MAIN BATTLE MENU **/
	applySurface(0, 437, menu_options, screen);
	applySurface(33, 13, attack, menu_options);
	applySurface(33, 43, magic, menu_options);
	applySurface(33, 73, summon, menu_options);
	applySurface(33, 103, item, menu_options);

	// Party window
	applySurface(200, 437, menu_party, screen);

	applySurface(7, 453, cursor, screen);
}

void reDrawMenu() {
	applySurface(0, 437, menu_options, screen);
	applySurface(33, 13, attack, menu_options);
	applySurface(33, 43, magic, menu_options);
	applySurface(33, 73, summon, menu_options);
	applySurface(33, 103, item, menu_options);
}

int main( int argc, char* args[] ) {
	bool quit = false;

	if (!init() || !loadFiles())
		return 1;

	applySurfaces();
	SDL_Flip(screen);

	cleanUp();
	return 0;
}
When I run this, the following surfaces don't show up:
	applySurface(33, 13, attack, menu_options);
	applySurface(33, 43, magic, menu_options);
	applySurface(33, 73, summon, menu_options);
	applySurface(33, 103, item, menu_options);
Everything else shows up fine. What is VERY odd though, is that if I call reDrawMenu() right after applySurfaces(), everything shows up. A;; reDrawMenu() does is repeat the same commands. I have no idea why this will not display properly. Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Look at the order of operations:

applySurface(0, 437, menu_options, screen);

applySurface(33, 13, attack, menu_options);
applySurface(33, 43, magic, menu_options);
applySurface(33, 73, summon, menu_options);
applySurface(33, 103, item, menu_options);


You apply menu_options to the screen, then you apply lots of surfaces to menu options. Can you see how this won't work?

Share this post


Link to post
Share on other sites
Apparently I don't understand how surfaces work then. It seems to me that it would make sense to put a menu box on the main screen, and then all the menu items on the menu.

I just tried moving the line to add menu_options to the bottom, but that didn't help out.

What's wrong with it?

Share this post


Link to post
Share on other sites
Quote:
Original post by AlexFZ
Apparently I don't understand how surfaces work then. It seems to me that it would make sense to put a menu box on the main screen, and then all the menu items on the menu.


A surface in SDL is just a image; that's all it is. The screen is also a image. If you draw a image to the screen, when the image updates, the screen does not update with it. If you change a image, and you want the screen to change also, you must change the screen by redrawing the image.

Observe:


(Image hosted by Imageshack)

The screen doesn't know that you changed a image; it doesn't even know the image exists. All drawing does, is copy a square of color from the drawing-image, and pastes it into the screen-image. The only way you can get the screen to change, is by drawing the image again.

Share this post


Link to post
Share on other sites
Alright, that makes much more sense now. Got everything working fine. Thanks a lot for the explanation and the visual aid :)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!