Hello, I have been programming for a little while, but I wouldn't consider myself very novice yet. Yesterday I started a project but ran into difficulties with getting the position of the object (apply_surface(0,0,Ss,screen,&clip[0]); to change based on the key input because they are in different headers and stuff, so I'd like a bit of help finding out how I can have the player actually moving from the key input. This might be a bit big of a question, or maybe a stupidly simple one (most likely the former), but maybe someone can help me. Thanks in advance.

main.cpp:

#include <iostream>
#include <string>
#include "SDL/SDL.h"
#include "SDL/SDL_Image.h"
#include "main.h"
#include "player.h"
#include "villager.h"

int main(int argc, char* args [])
{
    player plr;

    if (init() == false)
    {
        return -1;
    }

    if (load_files() == false)
    {
        return -1;
    }

    clip[0].x = 0;
    clip[0].y = 64;
    clip[0].w = 64;
    clip[0].h = 64;

    clip[1].x = 256;
    clip[1].y = 320;
    clip[1].w = 64;
    clip[1].h = 64;

    while (quit == false)
    {
        SDL_Event event;

        if (SDL_PollEvent(&event))
        {
            HandleEvent(event);
            plr.handle_input();
        }

        plr.move();

        for (int Tilex = 0; Tilex < TOWN_WIDTH / 60; Tilex++)
        {
        for (int Tiley = 0; Tiley < TOWN_HEIGHT / 60; Tiley++)
        {
            tileP.x = Tilex * 64;
            tileP.y = Tiley * 64;

            apply_surface(tileP.x, tileP.y, Ss, screen, &clip[1]);

            }
        }

        apply_surface(0,0,Ss,screen,&clip[0]);

        SDL_UpdateRect(screen, 0, 0, 0, 0);
    }

    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }

    SDL_FreeSurface(Ss);

    SDL_Quit;

    return 0;
}

main.h:

#ifndef MAIN_H
#define MAIN_H
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define TOWN_WIDTH 800
#define TOWN_HEIGHT 600
#define PLAYER_WIDTH 800
#define PLAYER_HEIGHT 600

#include "player.h"
#include "villager.h"

SDL_Surface *screen;
SDL_Surface *Ss;
SDL_Rect tileP;
SDL_Rect clip[0];
bool quit;

bool init()
{
    SDL_Init(SDL_INIT_EVERYTHING);

    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,8,SDL_SWSURFACE);
    SDL_WM_SetCaption("Village Game", NULL);
    return true;
}

SDL_Surface *load_image(std::string filename)
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load(filename.c_str());

    if(loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);

        if(optimizedImage != NULL)
        {
            SDL_SetColorKey(optimizedImage,SDL_SRCCOLORKEY,SDL_MapRGB(optimizedImage->format,255,0,255));
        }
    }


    return optimizedImage;
}

bool load_files()
{
   Ss = load_image("bin/Image/spritesheet.bmp");

    if((Ss == NULL))
    {
        return false;
    }

    return true;
}

void HandleEvent(SDL_Event event)
{

  switch (event.type) {
    case SDL_QUIT:
      quit = true;
      break;

    case SDL_KEYDOWN:
      switch (event.key.keysym.sym) {
        case SDLK_ESCAPE:
          quit = true;
          break;
      }
      break;
  }

}

#endif

player.h:

#ifndef PLAYER_H
#define PLAYER_H

#include "main.h"
#include "villager.h"


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

};

player::player()
{

    x = 0;
    y = 0;

    xVel = 0;
    yVel = 0;

}

void player::handle_input()
{
    SDL_Event event;

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

void player::move()
{

    x += xVel;

    if( ( x < 0 ) || ( x + PLAYER_WIDTH > SCREEN_WIDTH ) )
    {
        x -= xVel;
    }

    y += yVel;

    if( ( y < 0 ) || ( y + PLAYER_HEIGHT > SCREEN_HEIGHT ) )
    {
        y -= yVel;
    }
}


void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface( source, clip, destination, &offset );
}

#endif

villager.h:

#ifndef VILLAGER_H
#define VILLAGER_H

#include "main.h"
#include "player.h"

class villager
{
    public:
        villager();
    protected:
    private:
};


#endif

Here is the issue, at least what I understand of it based on a preliminary review of your code. Basically, in the handle_input function, you declare a new event, but you do not set it to anything. It is going to be filled with whatever is left on the stack, or 0 depending on your compiler. What you need to do is somehow send it the same event object that recieved the call to SDLPollEvents. Judging by your style of code, I would suggest making a global sdl event.

While I am on your style of code, I feel inclined to tell you that your extensive use of globals is not generally considered good practice. Instead of a bunch of globals, maybe create a control object like this:

class Control
{
    private:
    //your globals here
    int exampleInt;
    SDL_Event exampleEvent;
    public:
    Control();//here you could set up SDL?
    ~Control();//here you could clean up your mess (close SDL, free surfaces, delete memory, whatever)
    int getInt(){return exampleInt;}
    void setInt(int val){exampleInt=val;}
    SDL_Event getEvent(){return exampleEvent;}
    void pollEvent(){SDL_PollEvent(&exampleEvent);}
};

Hope this helps!

commented: Thanks! This was very helpful! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.