#ifndef _GAME_H_
#define _GAME_H_
#include "State.h"
#include "CEvent.h"
#include "GSurface.h"
#include <iostream>
class Game:public CEvent
{
private:
bool Running;
SDL_Surface* SurfDisplay;
State* CurrentState;
GSurface* temp;
SDL_Surface* temp2;
public:
Game();
~Game();
int onExecute();
bool onInit(); //inits display surface
void onEvent(SDL_Event* event); //Handles button presses
void onLoop(); //Game Loop
void onRender(); //Graphics
void onCleanup(); //Cleans up entire game
void onExit(); //Sets Running to false
void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode);
};
Game::Game()
{
SDL_Init(SDL_INIT_EVERYTHING);
Running=true;
SurfDisplay=0;
CurrentState=0;
temp=0;
//temp=new GSurface("image0","./image.png");
temp2=0;
}
bool Game::onInit()
{
//if(SDL_Init(SDL_INIT_EVERYTHING)<0){return false;}
SurfDisplay=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if(SurfDisplay==NULL)
{
return false;
}
temp=new GSurface("image0","./image.png");
temp2=CSurface::OnLoad("./image.png");
return true;
}
void Game::onRender()
{
if(temp->drawSurface(SurfDisplay,32,32)==false)
{
Running=false;
}
//CSurface::OnDraw(SurfDisplay,temp2,0,0);
SDL_Flip(SurfDisplay);
}
The previous problem I was having has been fixed, but now I have a new problem, though it still concerns strings. The function OnLoad() takes in a const* char and returns a pointer to an SDL_Surface. The variable File is a std::string equal to "./image.png". I put in File.c_str() but it fails to load. When I put in "./image.png" instead it works. I wish to know what I am doing wrong and what I can do to solve this.
#ifndef _GSURFACE_H_
#define _GSURFACE_H_
#include "CSurface.h"
#include <iostream>
class GSurface
{
public:
std::string File;
private:
SDL_Surface* Surface;
std::string Name;
//std::string File;
bool Loaded;
Uint8 R,G,B,A;
int Width,Height;
int iWidth,iHeight;
int ImageMax;
public:
GSurface(std::string n,std::string location);
GSurface(std::string n,std::string location,int iw,int ih);
GSurface(std::string n,std::string location,int iw,int ih,Uint8 red,Uint8
green,Uint8 blue,Uint8 alpha);
//GSurface(char* n,char* location,Uint32 RGBA);
~GSurface();
bool drawSurface(SDL_Surface* surfDest,int x,int y,int ix,int iy,int w,int h);
bool drawSurface(SDL_Surface* surfDest,int x,int y,int image);
bool drawSurface(SDL_Surface* surfDest,int x,int y);
bool setAlpha(Uint8 a); //Sets alpha of surface
void setColorKey(Uint8 r,Uint8 g,Uint8 b,Uint8 a); //Sets color key and alpha
void setImage(int w,int h);
//bool setColorKey(Uint32 rgba);
std::string getName();
void setName(std::string n);
void free(); //calls unload()
private:
bool load();
void unload();
void checkLoaded();
};
GSurface::GSurface(std::string n,std::string location)
{
GSurface(n,location,NULL,NULL,NULL,NULL,NULL,NULL);
}
GSurface::GSurface(std::string n,std::string location,int iw,int ih)
{
GSurface(n,location,iw,ih,NULL,NULL,NULL,NULL);
}
GSurface::GSurface(std::string n,std::string location,int iw,int ih,Uint8
red,Uint8 green,Uint8 blue,Uint8 alpha)
{
Name=n;
File=location;
R=red;
G=green;
B=blue;
A=alpha;
iWidth=iw;
iHeight=ih;
Surface=NULL;
Loaded=false;
Width=Height=ImageMax=0;
}
GSurface::~GSurface()
{
unload();
Surface=NULL;
}
bool GSurface::load()
{
if(Loaded==true)
{
return true;
}
//Surface=CSurface::OnLoad(File.c_str()); NOTE: Does Not Work
Surface=CSurface::OnLoad("./image.png"); //But if replaced with this it does
work
if(Surface==NULL)
{
return false;
}
Loaded=true;
if(Width==0)
{
Width=Surface->w;
Height=Surface->h;
}
if(R!=NULL&&G!=NULL&&B!=NULL&&A!=NULL)
{
CSurface::OnColorKey(Surface,R,G,B);
}
return true;
}
void GSurface::unload()
{
SDL_FreeSurface(Surface);
Surface=NULL;
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y)
{
checkLoaded();if(!Loaded){return false;}
return CSurface::OnDraw(surfDest,Surface,x,y);
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y,int ix,int iy,int
w,int h)
{
checkLoaded();if(!Loaded){return false;}
return CSurface::OnDraw(surfDest,Surface,x,y,ix,iy,w,h);
}
bool GSurface::drawSurface(SDL_Surface* surfDest,int x,int y,int image)
{
checkLoaded();if(!Loaded){return false;}
int ix,iy,fw;
while(image>ImageMax)
{
image-=ImageMax;
}
fw=(int)(Width/iWidth);
ix=(image%fw)*iWidth;
iy=(image/fw)*iHeight;
return CSurface::OnDraw(surfDest,Surface,x,y,ix,iy,iWidth,iHeight);
}
void GSurface::setColorKey(Uint8 r,Uint8 g,Uint8 b,Uint8 a)
{
R=r;
G=g;
B=b;
A=a;
if(Loaded==true)
{
CSurface::OnColorKey(Surface,R,G,B);
}
}
void GSurface::setImage(int w,int h)
{
iWidth=w;
iHeight=h;
}
void GSurface::checkLoaded()
{
if(!Loaded&&load())
{
Loaded=true;
}
}
std::string GSurface::getName()
{
return Name;
}
void GSurface::setName(std::string n)
{
Name=n;
}
void GSurface::free()
{
unload();
}
#endif
SDL_Surface* CSurface::OnLoad(const char* File)
{
SDL_Surface* Surf_Temp=NULL;
SDL_Surface* Surf_Return=NULL;
if((Surf_Temp=IMG_Load(File))==NULL)
{
return NULL;
}
Surf_Return=SDL_DisplayFormat(Surf_Temp);
SDL_FreeSurface(Surf_Temp);
return Surf_Return;
}