Hey guys. I'm new to SDL and C++ and I've stumbled upon a problem here. Here's the code:

#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <string>


using namespace std;

const int screenheight = 1920;
const int screenwidth =  1080;
const int screenbpp = 32;

int right,down = 0;


SDL_Surface *bakgrund = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *gubbe1 = NULL;
const int gubbheight = 5;
const int gubbwidth = 5;

SDL_Event event;



SDL_Surface *load_image(string filename)
{
            SDL_Surface* bild = NULL;
            bild = IMG_Load(filename.c_str());
            
            return bild;
}
            



bool loadstuff()
{
     bakgrund = load_image("bakgrund2.png");
     gubbe1 = load_image("gubbe.png");
     return true;
}

void tabort()
{
     SDL_FreeSurface(bakgrund);
     SDL_QUIT;
}

bool init()
{
     SDL_Init(SDL_INIT_EVERYTHING);
     screen = SDL_SetVideoMode(screenheight,screenwidth,screenbpp,SDL_SWSURFACE);
     SDL_WM_SetCaption( "eyy",NULL);
     return true;
}

SDL_Surface applysurface(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);
            
}




class gubbe
{
      private:
      int x,y;
      int xVel,yVel;
      
      public:
      gubbe();
      void handle_input();
      void move();
      void show();
};

gubbe::gubbe()
{
              x = 0;
              y = 0;
              
              xVel = 0;
              yVel = 0;
}

void gubbe::handle_input()
{
    
   
    if( event.type == SDL_KEYDOWN )
    {
        
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel -= 2; break;
            case SDLK_DOWN: yVel += 2; break;
            case SDLK_LEFT: xVel -= 2; break;
            case SDLK_RIGHT: xVel += 2; break;
        }
    }
   
    else if( event.type == SDL_KEYUP )
    {
        
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel += 2; break;
            case SDLK_DOWN: yVel -= 2; break;
            case SDLK_LEFT: xVel += 2; break;
            case SDLK_RIGHT: xVel -= 2; break;
        }
    }
}

void gubbe::move()
{
     x += xVel;
     y += yVel;
}


void gubbe::show()
{
     applysurface(x, y, gubbe1, screen);
}

      


int main(int argc, char*args[])
{
    
    gubbe mingubbe; 
    init();
    loadstuff();
    
    bool spela = true;
    
    while (spela)
    {
          
     while( SDL_PollEvent( &event ) )
        {
        
           mingubbe.handle_input();
           
           if( event.type == SDL_QUIT )
            {
                
                spela = false;
            }
            
            mingubbe.move();
            applysurface(0,0,bakgrund,screen);
            mingubbe.show();
            SDL_Flip(screen);
}
  

    
}


    void tabort();
    
    
    return 0;
}

Some of the stuff are in swedish but you'll probably understand. It's basically supposed to be a thing that moves around the screen when you push the arrow keys. The only problem is that it only moves if I move my mouse and push the keys stimultaneously. If I don't move the mouse around i can't hold the arrow keys down, only click(spam) them.

Any help appreciated. :)

Just a quick question from myself. I have next to no SDL experience but I was having a logical look through your code.
From lines 93 - 120, in your "handle_input()" function, you check for key downs and key ups. Surely this won't move while the key is held, but only when pressed down or released? It's probably just my lack of knowledge ;)

The problem is that you are updating the screen only when an event comes. So if you move the mouse, you trigger a bunch of unhandled mouse_move events that also makes your screen get updated. You should move the group of lines that perform the move, applysurface, show, and SDL_flip, outside the event loop into the "still-playing" loop, i.e. spela. You might want to add a timing to the loop such that you don't get 100% usage of the CPU. SDL provides timers for that.

Lycka till.

This article has been dead for over six months. Start a new discussion instead.