Code:
#ifndef PLAYER_H
#define PLAYER_H
#include "sdl.h"
#include "sdlinit.h"
class Player
{
public:
Player(SDL_Graphics* screen,
int imageX, int imageY,
int width, int height,
int transparentR, int transparentG, int transparentB,
char* FileName,
float x, float y,
float maxSpeed);
~Player();
void MoveLeft();
void MoveRight();
void MoveUp();
void MoveDown(); // these 4 adjust the characters position based on the speed in pixels
void StopMoving();
void Update(float deltatime); // this adjusts the speed to make it work to pixels per second.
private:
SDL_Graphics* screen;
SDL_Surface* playerimage;
float maxspeed ; // speed in pixels per second.
float currentspeedx;
float currentspeedy;
float x;
float y; // position on screen
float Width;
float Height;
float imagex;
float imagey;
};
#endif
player.cpp
#include "Player.h"
Player::Player(SDL_Graphics* screen,int imageX, int imageY,int width, int height,
int transparentR, int transparentG, int transparentB,
char* FileName,float x, float y,float maxSpeed)
: screen(screen),
imagex(imageX), imagey(imageY),
Width(width), Height(height),
x(x), y(y),
maxspeed(maxSpeed),
currentspeedx(0.0f)
{
playerimage = screen->Load_Image(FileName, transparentR, transparentG, transparentB);
}
Player::~Player()
{
screen->CloseImage(playerimage);
}
void Player::MoveLeft()
{
}
void Player::MoveRight()
{
}
void Player::MoveUp()
{
}
void Player::MoveDown() // these 4 adjust the characters position based on the speed in pixels
{
}
void Player::StopMoving()
{
}
void Player::Update(float deltatime) // this adjusts the speed to make it work to pixels per second.
{
}
sdlinit.h
#ifndef SDLINIT_H
#define SDLINIT_H
#include "sdl.h"
#include "SDL_TTF.h"
#include "SDL_Image.h"
class SDL_Graphics
{
public:
SDL_Graphics(int width, int height, char* windowtitle, int Red , int Green , int Blue );
~SDL_Graphics();
SDL_Surface* Load_Image(char* filename, int Red, int Green, int Blue);
void CloseImage(SDL_Surface* image);
void BackgroundColour(int Red,int Green,int Blue);
void Drawtext(int xpos, int ypos, char* message, int Red, int Green, int Blue, int fontsize);
void DrawSprite(SDL_Surface* imageSurface, int srcX, int srcY, int dstX, int dstY, int width, int height);
void Flip();
void BeginScene();
void EndScene();
private:
SDL_Surface* screen;
int BackRed;
int BackGreen;
int BackBlue;
};
#endif
sdlinit.cpp
#include "sdlinit.h"
SDL_Graphics::SDL_Graphics(int width, int height, char* windowtitle, int Red, int Green, int Blue)
{
SDL_Init( SDL_INIT_VIDEO );
TTF_Init();
screen = SDL_SetVideoMode( width, height,0,SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption( windowtitle, 0 );
BeginScene();
BackRed = Red; BackGreen = Green; BackBlue = Blue;
}
void SDL_Graphics::BeginScene()
{
SDL_FillRect(screen,
NULL,
SDL_MapRGB(screen->format,BackRed,BackGreen,BackBlue));
}
void SDL_Graphics::EndScene()
{
SDL_Flip(screen);
}
SDL_Surface* Load_Image(char* filename, int red, int green, int blue)
{
SDL_Surface* tempimage = NULL;
tempimage = IMG_Load(filename);
if (tempimage == NULL)
return NULL;
SDL_SetColorKey(tempimage,SDL_SRCCOLORKEY,SDL_MapRGB(tempimage->format, red, green, blue));
return tempimage;
}
void CloseImage(SDL_Surface* image)
{
SDL_FreeSurface(image);
}
void SDL_Graphics::BackgroundColour(int Red = 0 , int Green = 0, int Blue = 0)
{
BackRed = Red; BackGreen = Green; BackBlue = Blue;
}
SDL_Graphics::~SDL_Graphics()
{
SDL_Quit();
TTF_Quit();
}
void SDL_Graphics::Drawtext(int xpos, int ypos, char* message, int Red, int Green, int Blue, int fontsize)
{
TTF_Font* font = TTF_OpenFont("c:\\windows\\fonts\\arial.ttf", fontsize);
SDL_Color Colour = {Red,Green,Blue};
if (font == NULL)
return;
SDL_Surface* textsurface;
textsurface = TTF_RenderText_Blended(font, message, Colour);
SDL_Rect textlocation = {xpos,ypos,0,0};
SDL_BlitSurface(textsurface,NULL,screen,&textlocation);
SDL_FreeSurface(textsurface);
TTF_CloseFont(font);
}
void SDL_Graphics::Flip()
{
SDL_Flip(screen);
}
void SDL_Graphics::DrawSprite(SDL_Surface* imageSurface, int srcX, int srcY, int dstX, int dstY, int width, int height)
{
SDL_Rect srcRect;
srcRect.x = srcX;
srcRect.y = srcY;
srcRect.w = width;
srcRect.h = height;
SDL_Rect dstRect;
dstRect.x = dstX;
dstRect.y = dstY;
dstRect.w = width;
dstRect.h = height;
SDL_BlitSurface(imageSurface, &srcRect, screen, &dstRect);
}
the problems lies in the constructor for the Player class where it calls the SDL_Graphics classes function LoadImage().