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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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!
Last edited by civicdude95 on Mon Feb 28, 2011 12:34 am, edited 1 time in total.
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

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

Post by like80ninjas »

Post the entire draw event.
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post by sk1v3r »

is background a surface that you are drawing to, or a surface that you have loaded a single image into?
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post 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
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post by sk1v3r »

if you comment out when you first blit the menu screen does it stop the problem? (just troubleshooting here)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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 1224 times
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

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

Post by like80ninjas »

Is your player capable of moving off of the background and into the black error?
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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.
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post 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.
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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?
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post by sk1v3r »

I guess I might as well have a poke around in there :)
Post Reply