Page 1 of 2

[Solved] Need help with c++/sdl and drawing

Posted: Fri Feb 25, 2011 9:51 pm
by civicdude95
Hey guys, so I'm making a very simple c++/sdl gather-collect-things game and I currently have two screens (main menu and gameplay). I'm in the process of cleaning up my code and moving things out of main.cpp into other files/methods and it seems like my gameplay screen is not displaying properly. At first, before I moved stuff out of main.cpp, it was displaying just fine. Then it started displaying the top 1/3 ish of the main menu screen on top of the gameplay screen. I thought that it I might need to clear the screen before drawing the gameplay so I added in a call to SDL_FillRect() and cleared the screen black:

Code: Select all

SDL_FillRect(game.GetBuffer(), NULL, SDL_MapRGB(game.GetBuffer()->format, 0, 0, 0));
I know my screen size is the same size as my background and I'm telling SDL to display it at 0,0 so I'm kind of out of ideas atm. Any ideas? Has anyone run into this problem before?

Thanks in advance everyone!

Re: Need help with c++/sdl and drawing

Posted: Fri Feb 25, 2011 11:24 pm
by like80ninjas
Post the entire draw event.

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 9:03 am
by civicdude95
Ok sure, warning: it's kind of long.

Code: Select all

while(!gameOver)
{
......///////////////Update Stuff////////////////////////////.....
.
.
.
.
.

//////////////////////////// DRAW STUFF \\\\\\\\\\\\\\\\\\\\\\\\\\\\

		if (screen == 1){      ////// FOR MENU SCREEN \\\\\\\\\
			if (SDL_FillRect(	game.GetBuffer(), NULL, SDL_MapRGB(game.GetBuffer()->format, 0, 0, 0)) != 0){ 
		                cerr << "SDL_FillRect() Failed: " << SDL_GetError() << endl;
				exit(1);
			}

			if (SDL_BlitSurface(menuScreen, NULL, game.GetBuffer(), NULL) != 0) {
				cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
				exit(1);
			}
		}

		else if (screen == 2) {     ////////// FOR ABOUT SCREEN (not done yet) \\\\\\\\\\\\\\\\			
			if (SDL_FillRect(	game.GetBuffer(), NULL, SDL_MapRGB(game.GetBuffer()->format, 0, 0, 0)) != 0) {
				cerr << "SDL_FillRect() Failed: " << SDL_GetError() << endl;
				exit(1);
			}
			// Draw the about screen
		}

		else if (screen == 3) {        ////////////// DRAW THE GAMEPLAY SCREEN \\\\\\\\\\\\\\\\\
			if (SDL_FillRect(	game.GetBuffer(), NULL, SDL_MapRGB(game.GetBuffer()->format, 0, 0, 0)) != 0) {
				cerr << "SDL_FillRect() Failed: " << SDL_GetError() << endl;
				exit(1);
			}
			
			if (SDL_BlitSurface(bkground, NULL, game.GetBuffer(), NULL) != 0) {
				cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
				exit(1);
			}

			pillRect.x = static_cast<int>(pillX);  // For calculating where to draw the pills as they fall from the
			pillRect.y = static_cast<int>(pillY);  // top of the screen to the bottom 
		
                        // Draw the current pill
			if (SDL_BlitSurface(pill, NULL, game.GetBuffer(), &pillRect) != 0) {
				cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
				exit(1);
			}

			rect.x = static_cast<int>(x); // For calculating where to draw the player this frame
			rect.y = static_cast<int>(y);

                        // Draw the player
			if (SDL_BlitSurface(image, NULL, game.GetBuffer(), &rect) != 0) {
				cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
				exit(1);
			}

			std::stringstream ss;
			ss << score;
			DrawText(game.GetBuffer(), scoreText + ss.str(), 10, 10, 14, WHITE);
		}		

			// Update the display
		SDL_Flip(game.GetBuffer());		
}
This is everything that is in my draw section of code. I have a similar "if (this screen) or if (that screen) then update this or that" earlier in my while loop. Let me know if you spot anything. Thanks

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 10:44 am
by Ginto8
I must admit that I'm not fond of debugging other people's code, but looking at your description of the issue, you're either only drawing part of the "game screen" or you're drawing the top part of the menu screen after you draw the "game screen." Make sure you're only drawing the menu screen when you need to, and the same with the game screen, and you should figure out which issue it is, and then you can fix it.

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 7:07 pm
by sk1v3r
is background a surface that you are drawing to, or a surface that you have loaded a single image into?

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 7:18 pm
by civicdude95
it is an image that is defined as a surface

Code: Select all

	SDL_Surface *bkground;
	bkground = game.LoadImage("bkground.png");
And here's my LoadImage() function from Game.h

Code: Select all

SDL_Surface* Game::LoadImage(const char *filename)
{
	SDL_Surface *image = IMG_Load(filename);
	if (image == NULL)
	{
		std::cerr << "SDL_LoadBMP() Failed: " << SDL_GetError() << std::endl;
		exit(1);
	}
		
	Uint32 colorkey = SDL_MapRGB(image->format, 0xFF, 0, 0xFF);
	
	SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
	return image;
}
It's got to be something with the way I'm storing the images or something because it was working fine when I had everything in main.cpp

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 7:36 pm
by sk1v3r
I'm not quite sure :/
One nitpick though, how come you aren't using

Code: Select all

 SDL_DisplayFormatAlpha(surface);
?
It probably won't have anything to do with it , but you've been very error safe with the rest of your code

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 7:39 pm
by sk1v3r
if you comment out when you first blit the menu screen does it stop the problem? (just troubleshooting here)

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 8:47 pm
by Ginto8
post a screenshot and tell us what state the game is in. Both of these things will be serious helps with trying to help you debug this.

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 9:57 pm
by civicdude95
sk1v3r wrote:I'm not quite sure :/
One nitpick though, how come you aren't using

Code: Select all

 SDL_DisplayFormatAlpha(surface);
?
It probably won't have anything to do with it , but you've been very error safe with the rest of your code
Which drawing statement are you talking about specifically? And the answer is because 1. I don't know the difference between the two and 2. I didn't even know about SDL_DisplayFormatAlpha(); (I'm still learning SDL)
sk1v3r wrote:if you comment out when you first blit the menu screen does it stop the problem? (just troubleshooting here)
I commented out all of the drawing code for when screen == 1 (menuscreen) and it didn't help at all. Here is a screen shot of the game when it is in the gameplay state and displaying the gameplayscreen (i.e. screen == 3)
screenshot1.png
screenshot1.png (17.55 KiB) Viewed 1487 times

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 10:14 pm
by like80ninjas
Is your player capable of moving off of the background and into the black error?

Re: Need help with c++/sdl and drawing

Posted: Sat Feb 26, 2011 10:20 pm
by civicdude95
Yes (after I commented out the code to limit his vertical movement to the bottom third of the screen) and the pills are being drawn in the black area also (as opposed to the black being drawn on top of the pills)

By the way, how is your platformer coming along? I liked the video you posted in your thread on this site.

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 7:15 am
by sk1v3r
SDL_DisplayFormatAlpha(surface) is an SDL function that sets your screens format to the same bits per pixel as your main screen, and also enables an alpha channel for transparency.
The fact that when you comment it out there is still a black screen is very odd ...
This time (still just guessing here to try and figure it out) instead of blitting the menu in the first screen blit the background, then don't blit the background later, just to see if it is there on the first blit.

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 8:30 am
by civicdude95
Ok I tried commenting out the SDL_Blit() for the gameplay background and replaced the menuScreen draw function with the background draw and it still had the black at the top.

After that I tried making a completely new background image and it won't even draw that at all! I made it gray and a PNG (just like my first background image). This is really kind of annoying.

Would uploading my project so you could see some source code help?

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 8:36 am
by sk1v3r
I guess I might as well have a poke around in there :)