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