Here:
Logger.h
#include <string>#include <iostream>#include <fstream>#ifndef LOGGER_H#define LOGGER_H#define LOGNAME "game.log"class Logger{private: struct lg { // Member variables filled by the constructor std::ofstream &out; std::string module; // Overload << to write to the ostream void operator<< ( const std::string &val); // Construct by timestamping this line // (and filling in the member variables while we're here) lg(std::ofstream &out, char *mod); // Destruct by flushing out the line ~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); // -- Holds the Backgroung Color of the screen for beginScene() Uint8 backgroundColorRed; Uint8 backgroundColorGreen; Uint8 backgroundColorBlue; // -- Holds the Basic data for the window for quick reference Uint32 windowHeight; Uint32 windowWidth; std::string windowTitle; SDL_Surface *screen; // -- Holds the main window surface TTF_Font *font; // -- Holds the font for the game to use};#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); // Get the number of seconds since 1970 ti = localtime(&epochtime); // and convert to a date/time structure 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();}// -- Constructer, outputs timeLogger::lg::lg(std::ofstream &out, char *mod) : out(out){ module = mod; out << std::endl << stamp() << ": " << module << ": ";}// Overload << to write to the ofstreamvoid 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; // -- Temp Space to hold image SDL_Surface *final = NULL; // -- The Final Surface that is returned // -- Load the image loaded = IMG_Load( filename.c_str() ); // -- Error checking to see if it loaded if( loaded != NULL ) { // -- Convert final and Copy Image final = SDL_DisplayFormat( loaded ); // -- Free memory SDL_FreeSurface( loaded ); } else if (loaded == NULL) { Logger::log("Graphics") << "Failed to image: "; Logger::log("Graphics") << filename.c_str(); } // -- return image 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 ){ // -- Holds source and destination locations for image transfer 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;}