11 Replies - 2811 Views - Last Post: 30 September 2011 - 11:35 AM Rate Topic: -----

#1 BlackPhoenix   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

[SDL] image leaving a trail. I think blitting screen is successful..

Posted 19 August 2009 - 07:49 PM

I have a BIG issue. The colorkey is working properly, but his blue body is now leaving a trail.

Posted Image

Saw this from a post online, but was the ONLY thing I could find related to the issue.

"This way you are blitting the image every frame, not just when you are moving. Also, be sure to clear the screen (blit the background image) each frame, otherwise you will get a trailing effect."


I thought I was? Let me show you some code segments:

This is my handle events function:

void Game::HandleEvents(Game* game)
{
	SDL_Event event;

	while(SDL_PollEvent(&event))
	{
		switch (event.type)
		{	
			case SDL_QUIT:
				m_bRunning = false;
				break;
			
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym)
				{
					case SDLK_ESCAPE:
						m_bRunning = false;
					case SDLK_LEFT:
						hero_xvel = -hero_speed;
						break;
					case SDLK_RIGHT:
						hero_xvel = hero_speed;
						break;
					case SDLK_UP:
						hero_yvel = -hero_speed;
						break;
					case SDLK_DOWN:
						hero_yvel = hero_speed;
						break;
					default:
						break;
				}
				break;
			
			case SDL_KEYUP:
				switch(event.key.keysym.sym)
				{
					case SDLK_LEFT:
						hero_xvel = 0;
						break;
					case SDLK_RIGHT:
						hero_xvel = 0;
						break;
					case SDLK_UP:
						hero_yvel = 0;
						break;
					case SDLK_DOWN:
						hero_yvel = 0;
						break;
					default:
						break;
				}
				break;

			default:
				break;
		}
	}

	hero_x += hero_xvel;
	hero_y += hero_yvel;
}



This gets called in the game's main loop, which loops like this:

while(game.Running())
{
	game.HandleEvents(&game);
	game.Draw();
	game.Update();
}



Once handleevents closes, it draws and then Update() is:

void Game::Update()
{
	SDL_Flip(screen);
}



Is it because I'm not passing screen into the Update() function? I was pretty sure I was re-drawing properly, how else would I get the character to move?..... :blink: :blink:

Is This A Good Question/Topic? 0
  • +

Replies To: [SDL] image leaving a trail. I think blitting screen is successful..

#2 BlackPhoenix   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 19 August 2009 - 07:55 PM

Ah hah!

I added this to debug:

case SDLK_SPACE:
	hero_x =10;
	hero_y = 10;



And my results were the previous location of the character remained where he was prior to hitting Spacebar, but then he was teleported to location 10, 10 as the code says he should be.

I can then move him around, and hit space to warp back to 10, 10. All previous locations I have moved him to (trailing effect) are remaining behind.

This means I am not properly running the SDL_Flip function from within Update, correct?

Please help me spot the error!


Posted Image


EDIT:

Updated my Update() function to this:

void Game::Update()
{
	
		ofstream log;
		log.open("log.txt", ios::app);
		log << SDL_Flip(screen);
		log.close();
}



and the log file is filled with 0s. 0 = success, -1 = fail for screen updating, so I have NO idea what is going wrong now :(

This post has been edited by BlackPhoenix: 19 August 2009 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 19 August 2009 - 08:07 PM

you need to invalidate your screen to repaint it. You can just call the invalidate(); function or create a rect around your drawing and just invalidate that rect. That would be invalidateRect(rect,TRUE);
Was This Post Helpful? 0
  • +
  • -

#4 BlackPhoenix   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 19 August 2009 - 08:31 PM

FIXED!!!

THANK YOU SWEET LORD

Game loop:

handleEvents()
Draw()
Update()


I have added the clear screen line to the Draw function, before redrawing anything, and it did the trick!

void Game::Draw()
{
	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); 
	hero_x += hero_xvel;
	hero_y += hero_yvel;
	Sprite::Draw(hero_x, hero_y, hero, screen);
}

void Game::Update()
{
	SDL_Flip(screen);
}


This post has been edited by BlackPhoenix: 19 August 2009 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 20 August 2009 - 01:11 AM

Put SDL_Flip(screen); into your Draw(); function, it is part of the drawing.

Use Update(); to update timers and movement etc... :^:
Was This Post Helpful? 0
  • +
  • -

#6 BlackPhoenix   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 20 August 2009 - 05:52 AM

Hmm, stayscrisp, I have a new proposal.

In the original framework from the tutorial (which was INSANELY helpful at finally teaching me how to separate code into classes other files to make it easier to work with), we say the main game loop is as such

Init() //initialize the program variables
HandleEvents() //get keyboard/mouse data and do something with those events (example would be to set runningLeft = true)
Draw()
Update()
Clean()

In handleEvents(), I had placed code to move a character around. In draw() I re-drew surfaces, and in Update(), I cleared and flipped the screen.

What I am going to do is have handleEvents just activate variables or functions based on keypresses or other events, have Update FOLLOW HandleEvents(), and have it actually DO the movement, and then have Draw() clear and re-draw the screen. :)

Stinks because last night I was free to program for like 4 hours, and it took me 3 hours to find and add ONE LINE. (SDL_FillRect....)
Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 20 August 2009 - 06:10 AM

I currently working on SDL tutorial part 5 which will cover movement and creating entities, it is a good idea to separate the movement code out from the handle events section (like you said).

For example in your Player class you might have a function such as MoveLeft(); and this could be called when handling events and this code is set into the player class rather than dependant on the handle events.

MoveLeft will set the acceleration/velocity and such to move to the left, then Update will set the player position based on the acceleration/velocity. Then Draw(); will draw the player based on Update();

I think that is what you are thinking yes?

Check out my SDL tutorial part 4 as it is based on states and they are something good to get working before moving onto other parts.

I'm glad your getting on so well :D
Was This Post Helpful? 0
  • +
  • -

#8 BlackPhoenix   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 152
  • Joined: 11-July 09

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 20 August 2009 - 06:43 AM

View Poststayscrisp, on 20 Aug, 2009 - 05:10 AM, said:

MoveLeft will set the acceleration/velocity and such to move to the left, then Update will set the player position based on the acceleration/velocity. Then Draw(); will draw the player based on Update();

I think that is what you are thinking yes?

I'm glad your getting on so well :D



Yep that's exactly it! And thank you :)

I was going to create a new topic but I'll ask here:

Is SDL the best C++ option for game programming?

If you could, I would love to know the best 3D API, and the best API for 2D. Perhaps Simple DirectMedia Layer IS the best for 2D, but I would love to get your opinion.

I keep hearing about these things, but am unsure about which they perform, and how they stack up against the rest. If you could shed some light on each, that would be MANtastic!!

SDL
OpenGL
DirectX
XNA
Was This Post Helpful? 0
  • +
  • -

#9 MrKilltastic   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 19-September 11

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 28 September 2011 - 07:07 PM

View PostImaSexy, on 19 August 2009 - 08:07 PM, said:

you need to invalidate your screen to repaint it. You can just call the invalidate(); function or create a rect around your drawing and just invalidate that rect. That would be invalidateRect(rect,TRUE);


What library is that in, or do i need to make it myself? In that case.... what do i put in it?
Was This Post Helpful? 0
  • +
  • -

#10 Gungnir   User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 29 September 2011 - 05:58 AM

I'm glad you fixed your first issue. Trails are left whenever a part of the screen isn't redrawn. The easiest way to fix this is to give your window a "background", usually a black one (with SDL_FillRect).

Quote

Is SDL the best C++ option for game programming?

If you could, I would love to know the best 3D API, and the best API for 2D. Perhaps Simple DirectMedia Layer IS the best for 2D, but I would love to get your opinion.

I keep hearing about these things, but am unsure about which they perform, and how they stack up against the rest. If you could shed some light on each, that would be MANtastic!!

SDL
OpenGL
DirectX
XNA

When it comes to 2D you have two obvious choices.

- SDL: The common choice as it's more 'correct'. It's been ported to most languages and therefore looks and handles more authentically.
- Allegro: A less common option but it handles just as well.

They both have similar capabilities and syntax, but personally I prefer SDL as I already know the SDL port for Python so it was an easy adjustment.

As for 3D, you'll probably want to go with OpenGL. You can use OpenGL with 2D aswell, think of it as a software accelerator. It draws images more efficiently to the screen. You don't get that annoying 'flicker' that comes with most games with SDL/Allegro (where lots of things are going on at once). Using 3D just uses a slightly different skillset. I've never done a 3D game but the only difference is that depth adds an angle.

Making 3D games is just about moving pixels differently. They need to change in shape respective to the angle of the surrounding 'points'. When we want to find the relationship between an angle and two sides, we use trigonometry. For the most part, it's the same with OpenGL.

I've never tried using DirectX but I imagine it's quite similar. I would personally use OpenGL. But you can try both and see what you like best.

Hope this helped! :D
Was This Post Helpful? 0
  • +
  • -

#11 RevTorA   User is offline

  • D.I.C Regular
  • member icon

Reputation: 78
  • View blog
  • Posts: 255
  • Joined: 22-April 11

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 29 September 2011 - 09:04 AM

View PostMrKilltastic, on 28 September 2011 - 07:07 PM, said:

View PostImaSexy, on 19 August 2009 - 08:07 PM, said:

you need to invalidate your screen to repaint it. You can just call the invalidate(); function or create a rect around your drawing and just invalidate that rect. That would be invalidateRect(rect,TRUE);


What library is that in, or do i need to make it myself? In that case.... what do i put in it?


Wow, how in the world did you manage to dig up a 2 year old thread lol.

I haven't found any invalidate function in SDL. His problem was failing to clear the buffer before drawing the next frame is all. He fixed that with a simple call to SDL_FillRect to fill the screen with the black background.
Was This Post Helpful? 0
  • +
  • -

#12 MrKilltastic   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 19-September 11

Re: [SDL] image leaving a trail. I think blitting screen is successful..

Posted 30 September 2011 - 11:35 AM

View PostRevTorA, on 29 September 2011 - 09:04 AM, said:

View PostMrKilltastic, on 28 September 2011 - 07:07 PM, said:

View PostImaSexy, on 19 August 2009 - 08:07 PM, said:

you need to invalidate your screen to repaint it. You can just call the invalidate(); function or create a rect around your drawing and just invalidate that rect. That would be invalidateRect(rect,TRUE);


What library is that in, or do i need to make it myself? In that case.... what do i put in it?


Wow, how in the world did you manage to dig up a 2 year old thread lol.

I haven't found any invalidate function in SDL. His problem was failing to clear the buffer before drawing the next frame is all. He fixed that with a simple call to SDL_FillRect to fill the screen with the black background.


Oh yeah that works fine, but what if i were to use a picture as a background, any tips on how i would fix that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1