From b7949795ae9188308d7819ca21eef9f493d40bd9 Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Sun, 8 Apr 2012 22:56:16 +0100
Subject: [PATCH] [Change] Moving from X11 and WinAPI to SDL for window
 creation. [Add] Added some player class stuff.

---
 LibDQt/LibDQt.pro      | 59 ++++++++++++++--------------
 src/Actor/Player.cpp   | 13 +++++--
 src/Actor/Player.h     | 16 +++++++-
 src/Global/Constants.h |  7 ++++
 src/Global/Globals.cpp |  2 +
 src/Global/Globals.h   |  1 +
 src/Main/Game.cpp      | 25 ++++--------
 src/Main/Game.h        |  4 +-
 src/Main/main.cpp      | 87 +++++++++++++++++-------------------------
 src/Sprite/Sprite.cpp  |  4 +-
 src/Sprite/Sprite.h    |  6 +--
 11 files changed, 113 insertions(+), 111 deletions(-)
 create mode 100644 src/Global/Constants.h

diff --git a/LibDQt/LibDQt.pro b/LibDQt/LibDQt.pro
index 289e866..5dcb4b4 100644
--- a/LibDQt/LibDQt.pro
+++ b/LibDQt/LibDQt.pro
@@ -1,33 +1,34 @@
 CONFIG -= qt
 LIBS += -lGL \
-		-lSDL \
-		-lSDL_ttf \
-		-lSDL_image \
-		-lSDL_gfx \
-		-ltinyxml \
-		-lGLU
+    -lSDL \
+    -lSDL_ttf \
+    -lSDL_image \
+    -lSDL_gfx \
+    -ltinyxml \
+    -lGLU
 HEADERS += ../src/Actor/Player.h \
-		../src/Global/Globals.h \
-		../src/IO/Input.h \
-		../src/Main/Game.h \
-		../src/Main/LGLXWindow.h \
-		../src/Math/Timer.h \
-		../src/Math/MathBox.h \
-		../src/Math/FPS.h \
-		../src/Math/Vec2.h \
-		../src/Sprite/Sprite.h \
-		../src/System/Debug.h \
-		../src/Texture/Texture.h
+    ../src/Global/Globals.h \
+    ../src/IO/Input.h \
+    ../src/Main/Game.h \
+    ../src/Main/LGLXWindow.h \
+    ../src/Math/Timer.h \
+    ../src/Math/MathBox.h \
+    ../src/Math/FPS.h \
+    ../src/Math/Vec2.h \
+    ../src/Sprite/Sprite.h \
+    ../src/System/Debug.h \
+    ../src/Texture/Texture.h \
+    ../src/Global/Constants.h
 SOURCES += ../src/Actor/Player.cpp \
-		../src/Global/Globals.cpp \
-		../src/IO/Input.cpp \
-		../src/Main/main.cpp \
-		../src/Main/LGLXWindow.cpp \
-		../src/Main/Game.cpp \
-		../src/Math/Vec2.cpp \
-		../src/Math/Timer.cpp \
-		../src/Math/FPS.cpp \
-		../src/Sprite/Sprite.cpp \
-		../src/System/Debug.cpp \
-		../src/Texture/Texture.cpp
-OTHER_FILES +=
+    ../src/Global/Globals.cpp \
+    ../src/IO/Input.cpp \
+    ../src/Main/main.cpp \
+    ../src/Main/LGLXWindow.cpp \
+    ../src/Main/Game.cpp \
+    ../src/Math/Vec2.cpp \
+    ../src/Math/Timer.cpp \
+    ../src/Math/FPS.cpp \
+    ../src/Sprite/Sprite.cpp \
+    ../src/System/Debug.cpp \
+    ../src/Texture/Texture.cpp
+OTHER_FILES += 
diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp
index 3055523..c37f7ac 100644
--- a/src/Actor/Player.cpp
+++ b/src/Actor/Player.cpp
@@ -5,15 +5,22 @@ Player::Player(void) {
 }
 
 Player::~Player(void) {
-
+  delete _player->GetTexture();
+  delete _player;
 }
 
 void Player::Prepare(void) {
-
+  _player = new Sprite();
+  _playerTexture = new Texture();
+  _playerTexture->Load("../Data/Img/test.png");
+  _player->SetTexture(_playerTexture);
+  _player->SetPosition(Vec2(800/2, 600/2));
+  _player->SetScale(Vec2(4.5f, 4.5f));
 }
 
 void Player::Render(void) {
-
+  _player->SetRotation(_rotationAngle);
+  _player->Draw();
 }
 
 void Player::ProcessEvents(void) {
diff --git a/src/Actor/Player.h b/src/Actor/Player.h
index 2ba4323..a3670b9 100644
--- a/src/Actor/Player.h
+++ b/src/Actor/Player.h
@@ -1,11 +1,23 @@
 #include "../Texture/Texture.h"
+#include "../Sprite/Sprite.h"
+#include "../Global/Globals.h"
+#include "../System/Debug.h"
+#include "../IO/Input.h"
+
+class Sprite;
 
 // We will derive from an Actor class at some point.
 class Player {
+public:
   Player(void);
   ~Player(void);
-  
+
   void Prepare(void);
   void Render(void);
-  void ProcessEvents(void);  
+  void ProcessEvents(void);
+
+private:
+  Sprite*   _player;
+  Texture*  _playerTexture;
+  float     _rotationAngle;
 };
diff --git a/src/Global/Constants.h b/src/Global/Constants.h
new file mode 100644
index 0000000..38ec842
--- /dev/null
+++ b/src/Global/Constants.h
@@ -0,0 +1,7 @@
+#pragma once
+#include <SDL/SDL.h>
+
+const int WINDOW_WIDTH        = 800;
+const int WINDOW_HEIGHT       = 600;
+const int WINDOW_BPP          = 16;
+const int WINDOW_FULLSCREEN   = false;
diff --git a/src/Global/Globals.cpp b/src/Global/Globals.cpp
index 109a1ab..5ddada9 100644
--- a/src/Global/Globals.cpp
+++ b/src/Global/Globals.cpp
@@ -1,3 +1,5 @@
 #include "Globals.h"
 
+SDL_Surface* screen   = NULL;
 SDL_Event event;
+
diff --git a/src/Global/Globals.h b/src/Global/Globals.h
index 22174ec..b035443 100644
--- a/src/Global/Globals.h
+++ b/src/Global/Globals.h
@@ -1,4 +1,5 @@
 #pragma once
 #include <SDL/SDL.h>
 
+extern SDL_Surface* screen;
 extern SDL_Event event;
diff --git a/src/Main/Game.cpp b/src/Main/Game.cpp
index 0b40ed6..13547b7 100644
--- a/src/Main/Game.cpp
+++ b/src/Main/Game.cpp
@@ -11,7 +11,8 @@
 #include "Game.h"
 
 Game::Game(void) {
-  _rotationAngle = 0.0f;
+  _player = new Player();
+  //_rotationAngle = 0.0f;
 }
 
 Game::~Game(void) {
@@ -22,24 +23,13 @@ bool Game::Init(void) {
   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LEQUAL);
 
-  Texture* testTexture = new Texture();
-  testTexture->Load("../Data/Img/test.png");
-
-  _testSprite = new Sprite();
-  _testSprite->SetTexture(testTexture);
-  _testSprite->SetHandle(Vec2(800/2, 600/2));
-  _testSprite->SetScale(Vec2(5.0f, 5.0f));
-
+  _player->Prepare();
   // Return success.
   return true;
 }
 
 void Game::Prepare(float dt) {
-  const float SPEED = 15.0f;
-  _rotationAngle += SPEED*dt;
-  if(_rotationAngle > 360.0f) {
-    _rotationAngle -= 360.0f;
-  }
+
 }
 
 void Game::Render(void) {
@@ -52,14 +42,13 @@ void Game::Render(void) {
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
 
-  _testSprite->SetRotation(_rotationAngle);
-  _testSprite->Draw();
+  // Render our shit..
+  _player->Render();
 }
 
 void Game::Shutdown(void) {
   Debug::logger->message("\n ----- Cleaning Engine -----");
-  delete _testSprite->GetTexture();
-  delete _testSprite;
+  delete _player;
 }
 
 void Game::OnResize(int width, int height) {
diff --git a/src/Main/Game.h b/src/Main/Game.h
index a99e890..7a4c09e 100644
--- a/src/Main/Game.h
+++ b/src/Main/Game.h
@@ -1,5 +1,6 @@
 #pragma once
 #include "../IO/Input.h"
+#include "../Actor/Player.h"
 
 class Sprite;
 
@@ -16,6 +17,5 @@ public:
 	void OnResize(int width, int height);
 
 private:
-	float _rotationAngle;
-	Sprite* _testSprite;
+	Player* _player;
 };
diff --git a/src/Main/main.cpp b/src/Main/main.cpp
index 18d7234..4e8d0ae 100644
--- a/src/Main/main.cpp
+++ b/src/Main/main.cpp
@@ -1,75 +1,58 @@
+#ifdef _WIN32
 #define WIN32_LEAN_AND_MEAN
 #define WIN32_EXTRA_LEAN
+#endif
 
 #define GLX_GLXEXT_LEGACY		//  Use our local glxext.h rather than the system one.
 
 #ifdef _WIN32
 #include <windows.h>
-#include "GLWindow.h"
-#else
-#include "LGLXWindow.h"
 #endif
 
 #include "Game.h"
+#include "../Global/Globals.h"
+#include "../Global/Constants.h"
 #include "../System/Debug.h"
 
+void Destroy(void) {
+  SDL_FreeSurface(screen);
+  SDL_Quit();
+}
+
 #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;
-	const int windowBPP           = 16;
-	const int windowFullscreen    = false;
+  // Start by opening a debug log.
+  Debug::openLog(true);
+  Debug::logger->message("\n ----- Engine Loading -----");
+  // Our game code.
+  Game game;
 
-#ifdef _WIN32
-	// This is our window.
-	GLWindow programWindow(hInstance);
-#else
-	LGLXWindow programWindow;
-#endif
-	// Our game code.
-	Game game;
+  if(SDL_Init(SDL_INIT_VIDEO == -1)) {
+    Debug::logger->message("Error: Could not load SDL");
+  } else {
+    Debug::logger->message("SDL loaded..");
+  }
 
-	// Attach our game to the window.
-	programWindow.AttachGame(&game);
+  screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_HWSURFACE);
+  Debug::logger->message("Video mode set..");
 
-	// Attempt to create the window.
-	if(!programWindow.Create(windowWidth, windowHeight, windowBPP, windowFullscreen)) {
-		// If it fails.
-#ifdef _WIN32
-		Debug::logger->message("Unable to create the OpenGL Window");
-		MessageBox(NULL, TEXT("Unable to create the OpenGL Window"), TEXT("An error occured"), MB_ICONERROR | MB_OK);
-#endif
-		programWindow.Destroy();    // Reset the display and exit.
-		return 1;
-	}
-	if(!game.Init()) {            // Initialize our game.
-#ifdef _WIN32
-		Debug::logger->message("Could not initialize the application");
-		MessageBox(NULL, TEXT("Could not initialize the application"), TEXT("An error occured"), MB_ICONERROR | MB_OK);
-#endif
-		programWindow.Destroy();    // Reset the display and exit.
-		return 1;
-	}
-	// This is the main loop, we render frames until IsRunning returns false.
-	while(programWindow.IsRunning()) {
-		programWindow.ProcessEvents();    // Process any window events.
-		// We get the time that passed since the last frame.
-		float elapsedTime = programWindow.GetElapsedSeconds();
+  SDL_WM_SetCaption("LibD", NULL);
 
-		game.Prepare(elapsedTime);    // Do any pre-rendering logic.
-		game.Render();                // Render the scene.
+  srand((unsigned int)time(NULL));
 
-		programWindow.SwapBuffers();
-	}
-	game.Shutdown();            // Free any resouces.
-	programWindow.Destroy();    // Destroy the program window.
-	Debug::closeLog();
-	return 0;
+  Debug::logger->message("\n ----- Engine Initialization Complete -----");
+  Debug::logger->message("\n ----- Logic -----");
+
+  bool isRunning = true;
+  while(isRunning) {
+    break;
+  }
+
+  Destroy();
+
+  Debug::closeLog();
+  return 0;
 }
diff --git a/src/Sprite/Sprite.cpp b/src/Sprite/Sprite.cpp
index 66fc349..3da7e26 100644
--- a/src/Sprite/Sprite.cpp
+++ b/src/Sprite/Sprite.cpp
@@ -5,7 +5,7 @@ Sprite::Sprite() {
   texture = NULL;
   size = Vec2(0.0f, 0.0f);
   scale = Vec2(1.0f, 1.0f);
-  handle = Vec2(0.0f, 0.0f);
+  position = Vec2(0.0f, 0.0f);
 }
 
 Sprite::~Sprite() {
@@ -46,7 +46,7 @@ void Sprite::Draw() const {
 
   // Temporary solution.
   Vec2 halfScaledSize = scaledSize / 2.0f;
-  glTranslatef(handle.x + halfScaledSize.x, handle.y + halfScaledSize.y, 0.0f);
+  glTranslatef(position.x + halfScaledSize.x, position.y + halfScaledSize.y, 0.0f);
   glRotatef(rotation, 0.0f, 0.0f, 1.0f);
   glTranslatef(-halfScaledSize.x, -halfScaledSize.y, 0.0f);
 
diff --git a/src/Sprite/Sprite.h b/src/Sprite/Sprite.h
index ab4ecf8..52e8d2c 100644
--- a/src/Sprite/Sprite.h
+++ b/src/Sprite/Sprite.h
@@ -12,20 +12,20 @@ public:
   virtual void Update(float dt);
   virtual void Draw() const;
 
-  const Vec2&     GetHandle() const { return handle; }
+  const Vec2&     GetPosition() const { return position; }
   const Vec2&     GetSize() const { return size; }
   const Vec2&     GetScale() const { return scale; }
   float           GetRotation() const { return rotation; }
   Texture*        GetTexture() { return texture; }
   const Texture*  GetTexture() const { return texture; }
 
-  void SetHandle(const Vec2& handle) { this->handle = handle; }
+  void SetPosition(const Vec2& position) { this->position = position; }
   void SetScale(const Vec2& scale) { this->scale = scale; }
   void SetRotation(float rotation) { this->rotation = rotation; }
   void SetTexture(Texture* texture);
 
 protected:
-  Vec2 handle;
+  Vec2 position;
   Vec2 size;
   Vec2 scale;
   float rotation;