Page 1 of 1

[ SOLVED ]Player wont Move C++/SDL

Posted: Sat Aug 25, 2012 7:03 pm
by werdo659
Hello guys i have a slight problem in my platformer during testing without logic just movement and images.
Well i ran into a problem that my characters X position won't change.
main.cpp

Code: Select all

#include "Variables.h"
	
bool FrameCap = true;
bool Running = true;
const int FPS = 20;
const int PLAYER_WIDTH = 16;
const int PLAYER_HEIGHT = 32;

Uint32 FrameRate;

int Frame = 0;
int PlayerX = 0;
int PlayerY = 0;

SDL_Event Event;

SDL_Surface* Screen = NULL;
SDL_Surface* BG = NULL;
SDL_Surface* Message = NULL;
SDL_Surface* PlayerSurface = NULL;

TTF_Font* Font = NULL;

void Main::Apply_Surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface(source, NULL, destination, &offset);
}

void Main::Init()
{
	TTF_Init();
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_WM_SetCaption("Adrians Quest", NULL);
}

void Main::Load_Images()
{
	Screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
	BG = SDL_LoadBMP("BG.bmp");
	PlayerSurface = SDL_LoadBMP("Player.bmp");
}

void Main::Clean_Up()
{
	SDL_FreeSurface(BG);
	SDL_Quit();
}

int main(int argc, char** args)
{
	Main Main;
	Player Player;
	
	Main.Init();
	Main.Load_Images();
	
	SDL_Color TextColour = {0, 0, 0};
	Font = TTF_OpenFont("Adrians Quest.ttf", 28);
	
	while(Running)
	{
		FrameRate = SDL_GetTicks();
		while(SDL_PollEvent(&Event))
		{
			if(Event.type == SDL_QUIT)
				Running = false;
			Player.Input();
		}
		
		Player.Move();
		
		Main.Apply_Surface(0, 0, BG, Screen);
		Player.Show();
		SDL_Flip(Screen);
		
		Frame++;
		
		if((FrameCap) && (FrameRate < 1000 / FPS))
		{ 
			//Sleep the remaining frame time 
			SDL_Delay((1000 / FPS) - FrameRate); 
		}
	}
	
	Message = TTF_RenderText_Solid(Font, "Thanks for playing!", TextColour);
	Main.Apply_Surface(800/2-140, 600/2-28, Message, Screen);
	
	SDL_Flip(Screen);
	SDL_Delay(2500);
	
	Main.Clean_Up();
	return 0;
}
Player.cpp

Code: Select all

#include "Variables.h"

Main Main;

Player::Player()
{
	PlayerX = 0;
	PlayerY = 0;
	xVel, yVel = 0;
	PlayerBox.w = PLAYER_WIDTH;
	PlayerBox.h = PLAYER_HEIGHT;
}

void Player::Input()
{
	if(Event.type == SDL_KEYDOWN);
	{
		if(Event.key.keysym.sym == SDLK_LEFT)
			xVel -= PLAYER_WIDTH / 2;
		if(Event.key.keysym.sym == SDLK_RIGHT)
			xVel += PLAYER_WIDTH / 2;
	}
	if(Event.type == SDL_KEYUP)
	{
		if(Event.key.keysym.sym == SDLK_LEFT)
			xVel += PLAYER_WIDTH / 2;
		if(Event.key.keysym.sym == SDLK_RIGHT)
			xVel -= PLAYER_WIDTH / 2;
	}
}

void Player::Move()
{
	PlayerX += xVel;
	
	if((PlayerX < 0 ) || (PlayerX + PLAYER_WIDTH > 600 ))
		PlayerX -= xVel;
}

void Player::Show()
{
	Main.Apply_Surface(PlayerX, PlayerY, PlayerSurface, Screen);
}
Player.h

Code: Select all

#ifndef Player_H
#define Player_H

class Player
{
	private:
	int xVel, yVel;
	SDL_Rect PlayerBox;
	
	public:
	Player();
	void Input();
	void Move();
	void Show();
};

#endif
Variables.h

Code: Select all

#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include "Player.h"
#include "main.h"

extern SDL_Surface* Screen;
extern SDL_Surface* PlayerSurface;
extern SDL_Event Event;

extern const int PLAYER_WIDTH;
extern const int PLAYER_HEIGHT;

extern int PlayerX;
extern int PlayerY;
main.h

Code: Select all

#ifndef main_H
#define main_H

class Main
{
	private:
	
	public:
	void Apply_Surface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
	void Init();
	void Load_Images();
	void Clean_Up();
};

#endif

Re: Player wont Move C++/SDL please Help

Posted: Sun Aug 26, 2012 8:46 am
by GroundUpEngine
I'm guessing but try this:

Code: Select all

void Player::Input()
{
   if(Event.type == SDL_KEYDOWN)
   {
      if(Event.key.keysym.sym == SDLK_LEFT)
         xVel -= PLAYER_WIDTH / 2;
      if(Event.key.keysym.sym == SDLK_RIGHT)
         xVel += PLAYER_WIDTH / 2;
   }
}

Re: Player wont Move C++/SDL please Help

Posted: Sun Aug 26, 2012 9:06 am
by wtetzner
Are you trying to make it so when you hold down the key, your character accelerates? If not, try this:

Code: Select all

void Player::Input()
{
	if(Event.type == SDL_KEYDOWN);
	{
		if(Event.key.keysym.sym == SDLK_LEFT)
			xVel = -(PLAYER_WIDTH / 2);
		if(Event.key.keysym.sym == SDLK_RIGHT)
			xVel = PLAYER_WIDTH / 2;
	}
	if(Event.type == SDL_KEYUP)
	{
		if(Event.key.keysym.sym == SDLK_LEFT)
			xVel = 0;
		if(Event.key.keysym.sym == SDLK_RIGHT)
			xVel = 0;
	}
}

Re: Player wont Move C++/SDL please Help

Posted: Sun Aug 26, 2012 11:29 am
by lalacomun
I dont recomend you using a function for handling only your player input, you should check your input during your main function, for everything, and for your movement you should have a boolean, that turns true, while you are pressing some key and turn it false if you are not pressing:

Code: Select all


bool move[2] = {0,0} // 2, representing right, and left 

...

//This is for key down 
case SDLK_LEFT:
    move[0] = true;
    break;
case SDLK_RIGHT:
    move[1] = true;
    break;
...

//This is for key up
case SDLK_LEFT:
    move[0] = false;
    break;
case SDLK_RIGHT:
    move[1] = false;
    break;

...

player.Move(move);

And now for the player class:

Code: Select all


class Player
{
    public: 
          ...
          void Move(bool move);
};

Code: Select all


void Player::Move(bool move)
{
    if(move[0])
    player.x -= xVel;
    if(move[1])
    player.x += xVel; //xVel is a varible you make during your player constructor
}


Re: Player wont Move C++/SDL please Help

Posted: Sun Aug 26, 2012 11:43 am
by werdo659
wtetzner wrote:Are you trying to make it so when you hold down the key, your character accelerates? If not, try this:

Code: Select all

void Player::Input()
{
	if(Event.type == SDL_KEYDOWN);
	{
		if(Event.key.keysym.sym == SDLK_LEFT)
			xVel = -(PLAYER_WIDTH / 2);
		if(Event.key.keysym.sym == SDLK_RIGHT)
			xVel = PLAYER_WIDTH / 2;
	}
	if(Event.type == SDL_KEYUP)
	{
		if(Event.key.keysym.sym == SDLK_LEFT)
			xVel = 0;
		if(Event.key.keysym.sym == SDLK_RIGHT)
			xVel = 0;
	}
}
Thank you so much my guy moves now :):):):)

Re: Player wont Move C++/SDL please Help

Posted: Wed Sep 05, 2012 8:21 am
by YourNerdyJoe
I know this problems been fixed but I'm pretty sure this was the root of the problem:

Code: Select all

if(Event.type == SDL_KEYDOWN);// <--- SEMICOLON!!!!!!!1111!one
{
   if(Event.key.keysym.sym == SDLK_LEFT)
      xVel -= PLAYER_WIDTH / 2;
   if(Event.key.keysym.sym == SDLK_RIGHT)
      xVel += PLAYER_WIDTH / 2;
}
Just thought I put it out there.

Re: Player wont Move C++/SDL please Help

Posted: Wed Sep 05, 2012 8:49 am
by GroundUpEngine
Semicolon strikes again! :lol:

Re: Player wont Move C++/SDL please Help

Posted: Wed Sep 05, 2012 11:54 am
by bbguimaraes
Disabled compiler warnings shoot you in the foot.

Re: Player wont Move C++/SDL please Help

Posted: Mon Feb 18, 2013 7:08 pm
by werdo659
YourNerdyJoe wrote:I know this problems been fixed but I'm pretty sure this was the root of the problem:

Code: Select all

if(Event.type == SDL_KEYDOWN);// <--- SEMICOLON!!!!!!!1111!one
{
   if(Event.key.keysym.sym == SDLK_LEFT)
      xVel -= PLAYER_WIDTH / 2;
   if(Event.key.keysym.sym == SDLK_RIGHT)
      xVel += PLAYER_WIDTH / 2;
}
Just thought I put it out there.

*facepalm*