From 66acfa15caf852bc476e378d601a469c397be73a Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Sun, 8 Apr 2012 00:51:03 +0100
Subject: [PATCH 1/4] [Add] Added Debug logging.

---
 Bin/Makefile            |   9 ++--
 src/Main/Game.cpp       |   3 +-
 src/Main/LGLXWindow.cpp |  13 ++---
 src/Main/main.cpp       |   5 ++
 src/System/Debug.cpp    | 102 ++++++++++++++++++++++++++++++++++++++++
 src/System/Debug.h      |  23 +++++++++
 src/System/Makefile     |  20 ++++++++
 7 files changed, 165 insertions(+), 10 deletions(-)
 create mode 100644 src/System/Debug.cpp
 create mode 100644 src/System/Debug.h
 create mode 100644 src/System/Makefile

diff --git a/Bin/Makefile b/Bin/Makefile
index 271bb6b..f978e30 100644
--- a/Bin/Makefile
+++ b/Bin/Makefile
@@ -12,22 +12,25 @@ all:
 	$(MAKE) -C ../src/Texture
 	$(MAKE) -C ../src/Actor
 	$(MAKE) -C ../src/Math
+	$(MAKE) -C ../src/System
 	
 	$(CC) $(CFLAGS) -o LibD ../src/Main/main.cpp ../src/Main/*.o ../src/Texture/*.o \
-	                ../src/Actor/*.o  ../src/Math/*.o $(LDADD)
+	                ../src/Actor/*.o  ../src/Math/*.o ../src/System/*.o $(LDADD)
 
 static:
 	@echo -e "\033[1;31mThis is an experimental build, if it does not work, don't complain...\033[0m"
 	@sleep 1
 	$(MAKE) -C ../src/Main/ ../src/Main/*.o ../src/Texture/*.o \
-                  ../src/Actor/*.o ../src/Math/*.o
+                  ../src/Actor/*.o ../src/Math/*.o ../src/System/*.o
 
 	$(CC) $(CFLAGS) -o build/LibD-static ../src/Main/main.cpp ../src/Main/*.o  \
-	                  ../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o $(LDADDSTATIC)
+	                  ../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o \
+	                  ../src/System/*.o$(LDADDSTATIC)
 
 clean:
 	$(MAKE) -C ../src/Main/	       clean
 	$(MAKE) -C ../src/Texture/     clean
 	$(MAKE) -C ../src/Actor/       clean
 	$(MAKE) -C ../src/Math/        clean
+	$(MAKE) -C ../src/System/      clean
 	rm -f LibD
diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp
index 2a82cd7..8d0a1b5 100644
--- a/src/Main/Game.cpp
+++ b/src/Main/Game.cpp
@@ -5,6 +5,7 @@
 #include <GL/gl.h>
 #include <GL/glu.h>
 
+#include "../System/Debug.h"
 #include "Game.h"
 
 Game::Game(void) {
@@ -48,7 +49,7 @@ void Game::Render(void) {
 }
 
 void Game::Shutdown(void) {
-	
+
 }
 
 void Game::OnResize(int width, int height) {
diff --git a/src/Main/LGLXWindow.cpp b/src/Main/LGLXWindow.cpp
index 4037f86..b5b5246 100644
--- a/src/Main/LGLXWindow.cpp
+++ b/src/Main/LGLXWindow.cpp
@@ -5,6 +5,7 @@
 #include <sys/time.h>
 #endif
 
+#include "../System/Debug.h"
 #include "LGLXWindow.h"
 #include "Game.h"
 
@@ -44,7 +45,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) {
 	// Open the default display.
 	_display = XOpenDisplay(0);
 	if(!_display) {
-		std::cerr << "Could not open the display." << std::endl;
+		Debug::logger->message("Could not open the display.");
 		return false;
 	}
 	
@@ -57,7 +58,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) {
 	
 	XF86VidModeModeInfo **modes;
 	if(!XF86VidModeGetAllModeLines(_display, _screenID, &modeNum, &modes)) {
-    std::cerr << "Could not query the video modes." << std::endl;
+    Debug::logger->message("Could not query the video modes.");
     return false;
 	}
 	
@@ -71,7 +72,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) {
 	}
 	
 	if(bestMode == -1) {
-    std::cerr << "Could not find a suitable graphics mode." << std::endl;
+    Debug::logger->message("Could not find a suitable graphics mode.");
     return false;
 	}
 	
@@ -88,13 +89,13 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) {
 	// Attempt to create a double buffered window.
 	vi = glXChooseVisual(_display, _screenID, doubleBufferedAttribList);
 	if(!vi) {
-    std::cerr << "Could not create a double buffere window.. Sux.." <<std::endl;
+    Debug::logger->message("Could not create a double buffere window.. Sux..");
 	}
 	
 	// Time to create a GL 2.1 context.
 	GLXContext gl2Context = glXCreateContext(_display, vi, 0, GL_TRUE);
 	if(!gl2Context) {
-    std::cerr << "Could Not create a GL 2.1 context, check your darn graphics drivers" << std::endl;
+    Debug::logger->message("Could Not create a GL 2.1 context, check your darn graphics drivers");
     return false;
 	}
 	
@@ -102,7 +103,7 @@ bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) {
 	PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs 
         = (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB");
 	if(glXCreateContextAttribs == NULL) {
-    std::cerr << "OpenGL 3.0 is not supported, falling back to 2.1" << std::endl;
+    Debug::logger->message("OpenGL 3.0 is not supported, falling back to 2.1");
     _glContext = gl2Context;
     _GL3Supported = false;
 	} else {
diff --git a/src/Main/main.cpp b/src/Main/main.cpp
index ea4d382..e5b67fa 100644
--- a/src/Main/main.cpp
+++ b/src/Main/main.cpp
@@ -11,12 +11,16 @@
 #endif
 
 #include "Game.h"
+#include "../System/Debug.h"
 
 #ifdef _WIN32
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow) {
 #else
 int main(int argc, char** argv) {
 #endif
+  // Start by opening a debug log.
+  Debug::openLog(true);
+  Debug::logger->message("\n ----- Engine Loading ------");
 	// Get our window settings.
 	const int windowWidth         = 800;
 	const int windowHeight        = 600;
@@ -63,6 +67,7 @@ int main(int argc, char** argv) {
 		programWindow.SwapBuffers();
 	}
 	game.Shutdown();            // Free any resouces.
+	Debug::closeLog();
 	programWindow.Destroy();    // Destroy the program window.
 	
 	return 0;
diff --git a/src/System/Debug.cpp b/src/System/Debug.cpp
new file mode 100644
index 0000000..3cc6cfb
--- /dev/null
+++ b/src/System/Debug.cpp
@@ -0,0 +1,102 @@
+#include <iostream>
+#include <fstream>
+#include <cstdarg>
+#include <ctime>
+#include "Debug.h"
+#include "string"
+
+
+using namespace std;
+
+// ===================================================================
+// The Debug log allows us to display ever piece of data that
+// populates our class components, anything that is loaded, serialized,
+// de-serialized etc will be printed out to a text file.
+// (Running our program in a terminal, this debug log will print to it.)
+// ===================================================================
+
+Debug *Debug::logger = NULL;
+
+Debug::Debug(bool logToFile) {
+	time_t timestamp;
+	if(logToFile) {
+		_logFile.open("../../Bin/Debug.log", ios::out);
+		if(!_logFile.is_open()) {
+			// We can not open our log.
+			cerr << "Warning: Can not open Debug.log to write, continueing without logging\n";
+
+		} else {
+			// Log File is open, let us give it a nice time stamp.
+			timestamp = time(NULL);
+			_logFile << "Log Started: " << ctime(&timestamp) << endl;
+		}
+	}
+}
+
+Debug::~Debug(void) {
+	time_t timestamp;
+
+	// We only need to close the log if it is open.
+	if(_logFile.is_open()) {
+		// Give it a closing timestamp.
+		timestamp = time(NULL);
+		_logFile << endl << "Log Closed: " << ctime(&timestamp) << endl;
+
+		// Close the log file.
+		_logFile.close();
+	}
+}
+
+void Debug::message(std::string msg) {
+	if(_logFile.is_open()) {
+		_logFile << msg << endl;
+	}
+	cerr << msg << endl << endl;
+}
+
+void Debug::message(const char *msg, ...) {
+	va_list vargList; // This is to handlle the variable arguments
+
+	char outBuf[1024];
+	unsigned short outLen;
+
+	// This takes the arguments and puts them into the character array.
+	va_start(vargList, msg);
+
+#if defined WIN32
+	outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
+#else
+	outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
+#endif
+
+	va_end(vargList);
+
+	if(outLen >= sizeof(outBuf)) {
+		outLen = sizeof(outBuf);
+	}
+
+	if(_logFile.is_open()) {
+		_logFile << outBuf << endl;
+	}
+
+	cerr << outBuf << endl;
+}
+
+bool Debug::openLog(bool logToFile) {
+	// Make sure the logger has not already been initialized.
+	if(logger != NULL) {
+		logger->message("Warning: Multiple calls to openLog().");
+		return false;
+	}
+	logger = new Debug(logToFile);
+	return true;
+}
+
+void Debug::closeLog(void) {
+	if(logger == NULL) {
+		cerr << "Warning: Call to closeLog() with NULL logger pointer." << endl;
+		return;
+	}
+	delete logger;
+	logger = NULL;
+}
diff --git a/src/System/Debug.h b/src/System/Debug.h
new file mode 100644
index 0000000..e418cae
--- /dev/null
+++ b/src/System/Debug.h
@@ -0,0 +1,23 @@
+#ifndef _DEBUG_H_
+#define _DEBUG_H_
+#include <fstream>
+#include "string"
+
+class Debug {
+public:
+  Debug(bool logToFile);
+  ~Debug(void);
+
+  // Log an error message.
+  void message(std::string msg);
+  void message(const char *msg, ...);
+  static bool openLog(bool logToFile);
+  static void closeLog(void);
+  
+  static Debug *logger;
+
+private: 
+  std::ofstream _logFile;
+};
+
+#endif // _DEBUG_H_
diff --git a/src/System/Makefile b/src/System/Makefile
new file mode 100644
index 0000000..0928bb6
--- /dev/null
+++ b/src/System/Makefile
@@ -0,0 +1,20 @@
+CC	= g++
+CFLAGS	= -ansi -Wall -g
+LDADD	= -lGL -lGLU -lSDL -lSDL_image
+
+objects = Debug.o \
+
+
+.PHONY: default all clean
+
+default: all
+
+%.cpp: %.h
+
+%.o: %.cpp
+	$(CC) $(CFLAGS) -c -o $@ $<
+
+all: $(objects)
+
+clean:
+	rm -f $(objects)

From 35ec1ed46751ab4eefead19413ef876a0f7845e6 Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Sun, 8 Apr 2012 01:03:52 +0100
Subject: [PATCH 2/4] [Fix] Need to make sure we keep the makefiles up to date
 for us poor unix programmers.

---
 Bin/Makefile        | 20 +++++++++++---------
 src/Sprite/Makefile | 20 ++++++++++++++++++++
 2 files changed, 31 insertions(+), 9 deletions(-)
 create mode 100644 src/Sprite/Makefile

diff --git a/Bin/Makefile b/Bin/Makefile
index f978e30..840424b 100644
--- a/Bin/Makefile
+++ b/Bin/Makefile
@@ -13,24 +13,26 @@ all:
 	$(MAKE) -C ../src/Actor
 	$(MAKE) -C ../src/Math
 	$(MAKE) -C ../src/System
+	$(MAKE) -C ../src/Sprite
 	
 	$(CC) $(CFLAGS) -o LibD ../src/Main/main.cpp ../src/Main/*.o ../src/Texture/*.o \
-	                ../src/Actor/*.o  ../src/Math/*.o ../src/System/*.o $(LDADD)
+	                ../src/Actor/*.o  ../src/Math/*.o ../src/System/*.o ../src/Sprite/*.o  $(LDADD)
 
 static:
 	@echo -e "\033[1;31mThis is an experimental build, if it does not work, don't complain...\033[0m"
 	@sleep 1
 	$(MAKE) -C ../src/Main/ ../src/Main/*.o ../src/Texture/*.o \
-                  ../src/Actor/*.o ../src/Math/*.o ../src/System/*.o
+                  ../src/Actor/*.o ../src/Math/*.o ../src/System/*.o ../src/Sprite/*.o
 
 	$(CC) $(CFLAGS) -o build/LibD-static ../src/Main/main.cpp ../src/Main/*.o  \
-	                  ../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o \
-	                  ../src/System/*.o$(LDADDSTATIC)
+	                  ../src/Texture/*.o ../src/Actor/*.o ../src/Math/*.o  \
+	                  ../src/System/*.o ../src/Sprite/*.o $(LDADDSTATIC)
 
 clean:
-	$(MAKE) -C ../src/Main/	       clean
-	$(MAKE) -C ../src/Texture/     clean
-	$(MAKE) -C ../src/Actor/       clean
-	$(MAKE) -C ../src/Math/        clean
-	$(MAKE) -C ../src/System/      clean
+	$(MAKE) -C ../src/Main/	        clean
+	$(MAKE) -C ../src/Texture/      clean
+	$(MAKE) -C ../src/Actor/        clean
+	$(MAKE) -C ../src/Math/         clean
+	$(MAKE) -C ../src/System/       clean
+	$(MAKE) -C ../src/Sprite/       clean
 	rm -f LibD
diff --git a/src/Sprite/Makefile b/src/Sprite/Makefile
new file mode 100644
index 0000000..a7b8f59
--- /dev/null
+++ b/src/Sprite/Makefile
@@ -0,0 +1,20 @@
+CC	= g++
+CFLAGS	= -ansi -Wall -g
+LDADD	= -lGL -lGLU -lSDL -lSDL_image
+
+objects = Sprite.o \
+
+
+.PHONY: default all clean
+
+default: all
+
+%.cpp: %.h
+
+%.o: %.cpp
+	$(CC) $(CFLAGS) -c -o $@ $<
+
+all: $(objects)
+
+clean:
+	rm -f $(objects)

From 31a9694e743818eef7f09b22395376f83082fce7 Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Sun, 8 Apr 2012 01:13:13 +0100
Subject: [PATCH 3/4] [Change] moved some std::cerr crap to
 Debug::logger->message()

---
 src/Texture/Texture.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/Texture/Texture.cpp b/src/Texture/Texture.cpp
index e4617e5..3e9a871 100644
--- a/src/Texture/Texture.cpp
+++ b/src/Texture/Texture.cpp
@@ -1,3 +1,4 @@
+#include "../System/Debug.h"
 #include "Texture.h"
 #include <iostream>
 using namespace std;
@@ -25,7 +26,7 @@ int BuildTexture(const char* filename, GLuint* texID, GLint param, bool genMips)
   textureImage = IMG_Load(filename);
   
   if(!textureImage) {
-    std::cerr << "Warning: could not load " << filename << std::endl;
+     Debug::logger->message("Warning: could not load %s", filename);
     return false;
   }
 
@@ -55,7 +56,7 @@ int BuildTexture(const char* filename, GLuint* texID, GLint param, bool genMips)
   }
 
   if(internalFormat == GL_NONE || format == GL_NONE) {
-    std::cerr << "Warning: invalid texture format for " << filename << std::endl;
+    Debug::logger->message("Warning: invalid texture format for %s", filename);
     SDL_FreeSurface(textureImage);
     return false;
   }

From 449da95609b88134fd3556412abbb9fe6c11029a Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Sun, 8 Apr 2012 01:18:36 +0100
Subject: [PATCH 4/4] [Fix] Debug logger was not opening file for write as it
 was pointing to the wrong directory.

---
 src/System/Debug.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/System/Debug.cpp b/src/System/Debug.cpp
index 3cc6cfb..bc8e673 100644
--- a/src/System/Debug.cpp
+++ b/src/System/Debug.cpp
@@ -20,7 +20,7 @@ Debug *Debug::logger = NULL;
 Debug::Debug(bool logToFile) {
 	time_t timestamp;
 	if(logToFile) {
-		_logFile.open("../../Bin/Debug.log", ios::out);
+		_logFile.open("../Bin/Debug.log", ios::out);
 		if(!_logFile.is_open()) {
 			// We can not open our log.
 			cerr << "Warning: Can not open Debug.log to write, continueing without logging\n";