Here:
Logger.h
#include <string>
#include <iostream>
#include <fstream>
#ifndef LOGGER_H
#define LOGGER_H
#define LOGNAME "game.log"
class Logger
{
private:
struct lg
{
std::ofstream &out;
std::string module;
void operator<< ( const std::string &val);
lg(std::ofstream &out, char *mod);
~lg();
std::string stamp();
};
public:
static lg log( char *modu )
{
static std::ofstream of( LOGNAME, std::ios::app);
return lg( of, modu);
}
};
#endif
Graphics.h
#ifndef ENGINE_H
#define ENGINE_H
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_TTF.h>
#include <string>
class Graphics
{
public:
Graphics( Uint32 Width, Uint32 Height, std::string Title,
Uint8 bgR, Uint8 bgG, Uint8 bgB,
std::string font_Name, Uint8 font_Size);
~Graphics( void );
void ClearScreen();
SDL_Surface *loadimage( std::string filename );
void closeimage( SDL_Surface *Image );
void beginScene( void );
void endScene( void );
void drawText( std::string text, Uint32 x, Uint32 y,
Uint8 fR, Uint8 fG, Uint8 fB,
Uint8 bR, Uint8 bG, Uint8 bB);
SDL_Surface *drawSprite( SDL_Rect *src, SDL_Rect *dst, SDL_Surface *srcImage, SDL_Surface *dstImage );
void drawSprite( Uint32 srcx, Uint32 srcy, Uint32 dstx,
Uint32 dsty, Uint32 width, Uint32 height, SDL_Surface *srcImage );
void drawSprite( SDL_Rect *dst, SDL_Surface *srcImage );
TTF_Font *getFont( void );
private:
void setBackgroundColor( Uint8 r, Uint8 g, Uint8 b);
Uint8 backgroundColorRed;
Uint8 backgroundColorGreen;
Uint8 backgroundColorBlue;
Uint32 windowHeight;
Uint32 windowWidth;
std::string windowTitle;
SDL_Surface *screen;
TTF_Font *font;
};
#endif
Logger.cpp
#include "Logger.h"
#include <fstream>
#include <ctime>
std::string Logger::lg::stamp()
{
struct tm *ti;
time_t epochtime;
char str[32];
time(&epochtime);
ti = localtime(&epochtime);
sprintf(str,
"[%04d%02d%02d %02d:%02d:%02d]",
ti->tm_year + 1900,
ti->tm_mon + 1,
ti->tm_mday,
ti->tm_hour,
ti->tm_min,
ti->tm_sec);
return std::string(str);
}
Logger::lg::~lg()
{
out.flush();
}
Logger::lg::lg(std::ofstream &out, char *mod) : out(out)
{
module = mod;
out << std::endl << stamp() << ": " << module << ": ";
}
void Logger::lg::operator<< ( const std::string &val)
{
out << val;
}
Graphics.cpp
#include <SDL.h>
#include <SDL_TTF.h>
#include <SDL_image.h>
#include <string>
#include <vector>
#include "Graphics.h"
#include "Logger.h"
Graphics::Graphics( Uint32 Width, Uint32 Height, std::string Title,
Uint8 bgR, Uint8 bgG, Uint8 bgB,
std::string font_Name, Uint8 font_Size)
{
SDL_WM_SetCaption( Title.c_str(), NULL );
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1)
{
Logger::log("Graphics" ) << "Failed to Initialize SDL";
}
if( ( screen = SDL_SetVideoMode( Width, Height, NULL, SDL_HWSURFACE | SDL_DOUBLEBUF ) ) == NULL )
{
Logger::log( "Graphics" ) << "Failed to Initialize screen";
}
this->setBackgroundColor( bgR, bgG, bgB );
if( TTF_Init() == -1 )
Logger::log( "Font" ) << "Failed to Initialize SDL_TTF";
this->font = TTF_OpenFont( font_Name.c_str(), font_Size);
}
Graphics::~Graphics( void )
{
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
}
void Graphics::beginScene()
{
SDL_FillRect(screen,
NULL,
SDL_MapRGB(screen->format, backgroundColorRed,
backgroundColorGreen,
backgroundColorBlue));
}
void Graphics::endScene()
{
SDL_Flip(screen);
}
SDL_Surface *Graphics::loadimage( std::string filename )
{
SDL_Surface *loaded = NULL;
SDL_Surface *final = NULL;
loaded = IMG_Load( filename.c_str() );
if( loaded != NULL )
{
final = SDL_DisplayFormat( loaded );
SDL_FreeSurface( loaded );
}
else if (loaded == NULL)
{
Logger::log("Graphics") << "Failed to image: ";
Logger::log("Graphics") << filename.c_str();
}
return final;
}
void Graphics::closeimage( SDL_Surface *Image )
{
SDL_FreeSurface( Image );
}
SDL_Surface *Graphics::drawSprite( SDL_Rect *src, SDL_Rect *dst, SDL_Surface *srcImage, SDL_Surface *dstImage )
{
SDL_BlitSurface( srcImage, src, dstImage, dst );
delete src;
delete dst;
return dstImage;
}
void Graphics::drawSprite( SDL_Rect *dst, SDL_Surface *srcImage )
{
SDL_BlitSurface( srcImage, NULL, this->screen, dst );
delete dst;
}
void Graphics::drawSprite( Uint32 srcx, Uint32 srcy, Uint32 dstx,
Uint32 dsty, Uint32 width, Uint32 height, SDL_Surface *srcImage )
{
SDL_Rect src;
SDL_Rect dst;
dst.h = src.h = height;
dst.w = src.w = width;
src.x = srcx;
src.y = srcy;
dst.x = dstx;
dst.y = dsty;
SDL_BlitSurface( srcImage, &src, screen, &dst );
}
void Graphics::setBackgroundColor( Uint8 r, Uint8 g, Uint8 b)
{
this->backgroundColorBlue = b;
this->backgroundColorGreen = g;
this->backgroundColorRed = r;
}
void Graphics::drawText(std::string text, Uint32 x, Uint32 y,
Uint8 fR, Uint8 fG, Uint8 fB,
Uint8 bR, Uint8 bG, Uint8 bB)
{
SDL_Color foregroundColor = { fR, fG, fB };
SDL_Color backgroundColor = { bR, bG, bB };
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, text.c_str(),
foregroundColor, backgroundColor);
SDL_Rect textLocation = { x, y, 0, 0 };
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_FreeSurface(textSurface);
}
TTF_Font *Graphics::getFont( void )
{
return font;
}
-Matt S.