[ SOLVED ]Player wont Move C++/SDL

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
werdo659
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri Aug 19, 2011 11:37 am
Current Project: A top-down 2D RPG game using C++, and lua
Favorite Gaming Platforms: N64, and PC
Programming Language of Choice: C++, Lua

[ SOLVED ]Player wont Move C++/SDL

Post 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
Last edited by werdo659 on Mon Feb 18, 2013 8:35 pm, edited 1 time in total.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post 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;
   }
}
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

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

Post 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;
	}
}
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
lalacomun
VS Setup Wizard
VS Setup Wizard
Posts: 114
Joined: Wed Dec 28, 2011 10:18 pm
Favorite Gaming Platforms: psx, nintendo ds, gameboy advance, xbox 360, ps2
Programming Language of Choice: C++
Location: Argentina
Contact:

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

Post 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
}

Image
werdo659
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri Aug 19, 2011 11:37 am
Current Project: A top-down 2D RPG game using C++, and lua
Favorite Gaming Platforms: N64, and PC
Programming Language of Choice: C++, Lua

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

Post 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 :):):):)
User avatar
YourNerdyJoe
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 79
Joined: Sun Oct 02, 2011 3:28 pm
Current Project: Top secret (not really) Top-Down Shooter for GBA
Favorite Gaming Platforms: GBA, Gamecube, PC, 3DS
Programming Language of Choice: C/C++
Contact:

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

Post 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.
See that?.....
Exactly
https://yournerdyjoe.github.io/
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post by GroundUpEngine »

Semicolon strikes again! :lol:
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
Posts: 294
Joined: Wed Apr 11, 2012 4:34 pm
Programming Language of Choice: c++
Location: Brazil
Contact:

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

Post by bbguimaraes »

Disabled compiler warnings shoot you in the foot.
werdo659
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Fri Aug 19, 2011 11:37 am
Current Project: A top-down 2D RPG game using C++, and lua
Favorite Gaming Platforms: N64, and PC
Programming Language of Choice: C++, Lua

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

Post 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*
Post Reply