[SDL] Every time I move the mouse the sprite redraws need help.

Started by
5 comments, last by xDancinpoptartx 11 years, 4 months ago
I made my sprite move with the mouse but everytime I move it redraws to the screen what can I do to fix this?

[source lang="cpp"]#include "SDL/SDL.h"
#include <stdbool.h>

int x;
int y;
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}

int main(int argc, char* args[])
{
//images/ints
SDL_Event event;
SDL_Surface* sprite = NULL;
SDL_Surface* back = NULL;
SDL_Surface* screen = NULL;
back = SDL_LoadBMP("back.bmp");
sprite = SDL_LoadBMP("char.bmp");
bool done = true;
//init
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(560, 560, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Game", NULL);
apply_surface(0, 0, back, screen);
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, SDL_MapRGB(sprite->format, 255, 0, 255));
while (done)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = false;
}

if(event.type == SDL_MOUSEMOTION)
{
x = event.motion.x;
y = event.motion.y;
apply_surface(x, y, sprite, screen);
}
}
SDL_ShowCursor(SDL_DISABLE);
SDL_Flip(screen);
}
SDL_FreeSurface(sprite);
SDL_FreeSurface(back);
return 0;
}[/source]
Advertisement
1) You don't need to call SDL_ShowCursor on every cycle. Call this function before the main loop.

2) Personally, I think is better to check SDL_PollEvent on a "while" like this:

[source lang="cpp"]while(SDL_PollEvent(&event))
{
// Handle events here
}[/source]
3) Put the function apply_surface() after manage the events. You must separate the event manager code and the draw code. (Just put that apply_surface() before the SDL_Flip(), on the place where SDL_ShowCursor() is actually.)
A simple way to handle this is to, every frame:

  • If the mouse has moved, save the new mouse position in the "x" and "y" variables
  • Clear the screen (e.g. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)))
  • Draw any objects you wish at their current position
  • Flip the screen



Most simple games have a main loop which looks (essentially) like this:

while(running)
{
handle_input();
update_game_logic();
draw_screen();
}


It is better not to mix input code with game logic code, and not to mix drawing code with either.
It still

1) You don't need to call SDL_ShowCursor on every cycle. Call this function before the main loop.

2) Personally, I think is better to check SDL_PollEvent on a "while" like this:

[source lang="cpp"]while(SDL_PollEvent(&event))
{
// Handle events here
}[/source]
3) Put the function apply_surface() after manage the events. You must separate the event manager code and the draw code. (Just put that apply_surface() before the SDL_Flip(), on the place where SDL_ShowCursor() is actually.)




It still draws the sprite every time even after I moved the apply_surface() and changed it to while(SDL_PollEvent(&event)).
What's the reason to not draw the sprite every time you move the mouse?
Are you clearing the screen before redraw all the content?

Personally, I think is better to check SDL_PollEvent on a "while" like this...
[/quote]
It is actually objectively better to poll events in a loop. If you do not, you introduce a frame of latency between each input event. In a fast paced game with lots of mouse and keyboard events, this can result in the game feeling much slower.
My fault I forgot to clear the screen problem solved thanks for all the help!

This topic is closed to new replies.

Advertisement