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;
}
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);
}
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
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;
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