I have a few classes, which are all pretty short, but are made to do one thing and only one thing.
First off is my graphics class header then impl.
Code:
class Graphics
{
public:
Graphics(int windowWidth = 800, int windowHeight = 600, bool fullscreen = false,
const char* windowTitle = "",
int bgR = 0, int bgG = 0, int bgB = 0);
~Graphics();
bool CreateSurfaceFromFile(SDL_Surface*& dst, const char* filename, int tran_r = -1, int tran_g = -1, int tran_b = -1);
void CloseSurface(SDL_Surface* src);
void DrawSurface(SDL_Surface* src, SDL_Rect* clip = NULL, int x_pos = 0, int y_pos = 0);
void BeginScene() { SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, r, g, b)); }
void RenderScene(){ SDL_Flip(screen); }
private:
SDL_Surface* screen;
int r;
int g;
int b;
};
Code:
Graphics::Graphics(int windowWidth, int windowHeight, bool fullscreen, const char *windowTitle, int bgR, int bgG, int bgB)
{
if ( SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO) < 0 )
return;
screen = NULL;
if (fullscreen == true)
{
screen = SDL_SetVideoMode(windowWidth,windowHeight, 32, SDL_FULLSCREEN|SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_ANYFORMAT);
}
else
{
screen = SDL_SetVideoMode(windowWidth, windowHeight, 32,SDL_ANYFORMAT|SDL_HWSURFACE|SDL_DOUBLEBUF);
}
SDL_WM_SetCaption(windowTitle,NULL);
r = bgR;
g = bgG;
b = bgB;
}
Graphics::~Graphics()
{
SDL_FreeSurface(screen);
SDL_Quit();
screen = NULL;
}
bool Graphics::CreateSurfaceFromFile(SDL_Surface*& dst, const char *filename, int tran_r, int tran_g, int tran_b)
{
//if file name is not empty then attempt to load the surface.
if (filename!=NULL)
{
SDL_Surface* d = NULL;
d = IMG_Load(filename);
if ( d != NULL )
dst = SDL_DisplayFormat(d);
SDL_FreeSurface(d);
}
else
{
return false;
}
if (dst==NULL)
return false;
if ( tran_r < 0 || tran_g < 0 || tran_b < 0 )
return true;
else
{
SDL_SetColorKey(dst, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(dst->format, tran_r, tran_g, tran_b));
return true;
}
return false;
}
void Graphics::CloseSurface(SDL_Surface *src)
{
SDL_FreeSurface(src);
src = NULL;
}
void Graphics::DrawSurface(SDL_Surface* src, SDL_Rect* clip, int x_pos, int y_pos)
{
SDL_Rect offset;
offset.x = x_pos;
offset.y = y_pos;
SDL_BlitSurface(src,clip,screen,&offset);
}
Second is the input class...
Code:
class Input
{
// Methods
public:
Input();
~Input();
void readInput();
bool* getInput();
bool get(const int& key) { return m_keysHeld[key]; };
// Check this each frame
bool windowClosed();
// Data
private:
SDL_Event m_event;
bool m_keysHeld[324];
bool m_windowClosed;
};
Code:
Input::Input()
{
m_windowClosed = false;
// Make sure all the keys are set to false.
for (int i = 0; i < 323; i++)
{
m_keysHeld[i] = false;
}
}
Input::~Input()
{
}
void Input::readInput()
{
while (SDL_PollEvent(&m_event))
{
if (m_event.type == SDL_QUIT)
{
m_windowClosed = true;
}
if (m_event.type == SDL_KEYDOWN)
{
m_keysHeld[m_event.key.keysym.sym] = true;
}
if (m_event.type == SDL_KEYUP)
{
m_keysHeld[m_event.key.keysym.sym] = false;
}
}
}
bool* Input::getInput()
{
return m_keysHeld;
}
bool Input::windowClosed()
{
return m_windowClosed;
}
And the only other that is questionable to me is my Surface class
Code:
class Surface
{
public:
Surface(const char* FILE):file(FILE) {};
~Surface() { SDL_FreeSurface(surface); };
SDL_Surface*& Get() { return surface; }
const char* GetFile() { return file.c_str(); }
private:
std::string file;
SDL_Surface* surface;
};
They all look good, but i figure someone with more experience will find a flaw, or even a recommendation for better design or functionality. Thanks in advance for any insight.