Sign in to follow this  
xDancinpoptartx

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

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
A simple way to handle this is to, every frame:
[list]
[*] 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
[/list]


Most simple games have a main loop which looks (essentially) like this:
[code]
while(running)
{
handle_input();
update_game_logic();
draw_screen();
}
[/code]

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

Share this post


Link to post
Share on other sites
It still [quote name='ptrrf' timestamp='1354812132' post='5007799']
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.)
[/quote]



It still draws the sprite every time even after I moved the apply_surface() and changed it to while(SDL_PollEvent(&event)).

Share this post


Link to post
Share on other sites
[quote]
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this