[SDL] Every time I move the mouse the sprite redraws need help.
#1 Members - Reputation: 118
Posted 06 December 2012 - 10:26 AM
[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]
#2 Members - Reputation: 341
Posted 06 December 2012 - 10:42 AM
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 Moderators - Reputation: 5028
Posted 06 December 2012 - 10:47 AM
- 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 Members - Reputation: 118
Posted 06 December 2012 - 10:49 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.)
It still draws the sprite every time even after I moved the apply_surface() and changed it to while(SDL_PollEvent(&event)).
#6 Moderators - Reputation: 5028
Posted 06 December 2012 - 11:26 AM
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.Personally, I think is better to check SDL_PollEvent on a "while" like this...






