Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 06 December 2012 - 10:26 AM

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]

Sponsor:

#2 ptrrf   Members   -  Reputation: 395

Like
2Likes
Like

Posted 06 December 2012 - 10:42 AM

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.)

#3 rip-off   Moderators   -  Reputation: 8727

Like
1Likes
Like

Posted 06 December 2012 - 10:47 AM

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.

#4 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 06 December 2012 - 10:49 AM

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)).

#5 ptrrf   Members   -  Reputation: 395

Like
1Likes
Like

Posted 06 December 2012 - 10:56 AM

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?

Edited by ptrrf, 06 December 2012 - 10:57 AM.


#6 rip-off   Moderators   -  Reputation: 8727

Like
3Likes
Like

Posted 06 December 2012 - 11:26 AM

Personally, I think is better to check SDL_PollEvent on a "while" like this...

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.

#7 Dylan_DE   Members   -  Reputation: 131

Like
0Likes
Like

Posted 06 December 2012 - 12:20 PM

My fault I forgot to clear the screen problem solved thanks for all the help!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS