[Change] Moving from X11 and WinAPI to SDL for window creation.

[Add] Added some player class stuff.
This commit is contained in:
Rtch90 2012-04-08 22:56:16 +01:00
parent ea553a7b62
commit b7949795ae
11 changed files with 113 additions and 111 deletions

View File

@ -1,33 +1,34 @@
CONFIG -= qt CONFIG -= qt
LIBS += -lGL \ LIBS += -lGL \
-lSDL \ -lSDL \
-lSDL_ttf \ -lSDL_ttf \
-lSDL_image \ -lSDL_image \
-lSDL_gfx \ -lSDL_gfx \
-ltinyxml \ -ltinyxml \
-lGLU -lGLU
HEADERS += ../src/Actor/Player.h \ HEADERS += ../src/Actor/Player.h \
../src/Global/Globals.h \ ../src/Global/Globals.h \
../src/IO/Input.h \ ../src/IO/Input.h \
../src/Main/Game.h \ ../src/Main/Game.h \
../src/Main/LGLXWindow.h \ ../src/Main/LGLXWindow.h \
../src/Math/Timer.h \ ../src/Math/Timer.h \
../src/Math/MathBox.h \ ../src/Math/MathBox.h \
../src/Math/FPS.h \ ../src/Math/FPS.h \
../src/Math/Vec2.h \ ../src/Math/Vec2.h \
../src/Sprite/Sprite.h \ ../src/Sprite/Sprite.h \
../src/System/Debug.h \ ../src/System/Debug.h \
../src/Texture/Texture.h ../src/Texture/Texture.h \
../src/Global/Constants.h
SOURCES += ../src/Actor/Player.cpp \ SOURCES += ../src/Actor/Player.cpp \
../src/Global/Globals.cpp \ ../src/Global/Globals.cpp \
../src/IO/Input.cpp \ ../src/IO/Input.cpp \
../src/Main/main.cpp \ ../src/Main/main.cpp \
../src/Main/LGLXWindow.cpp \ ../src/Main/LGLXWindow.cpp \
../src/Main/Game.cpp \ ../src/Main/Game.cpp \
../src/Math/Vec2.cpp \ ../src/Math/Vec2.cpp \
../src/Math/Timer.cpp \ ../src/Math/Timer.cpp \
../src/Math/FPS.cpp \ ../src/Math/FPS.cpp \
../src/Sprite/Sprite.cpp \ ../src/Sprite/Sprite.cpp \
../src/System/Debug.cpp \ ../src/System/Debug.cpp \
../src/Texture/Texture.cpp ../src/Texture/Texture.cpp
OTHER_FILES += OTHER_FILES +=

View File

@ -5,15 +5,22 @@ Player::Player(void) {
} }
Player::~Player(void) { Player::~Player(void) {
delete _player->GetTexture();
delete _player;
} }
void Player::Prepare(void) { 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) { void Player::Render(void) {
_player->SetRotation(_rotationAngle);
_player->Draw();
} }
void Player::ProcessEvents(void) { void Player::ProcessEvents(void) {

View File

@ -1,11 +1,23 @@
#include "../Texture/Texture.h" #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. // We will derive from an Actor class at some point.
class Player { class Player {
public:
Player(void); Player(void);
~Player(void); ~Player(void);
void Prepare(void); void Prepare(void);
void Render(void); void Render(void);
void ProcessEvents(void); void ProcessEvents(void);
private:
Sprite* _player;
Texture* _playerTexture;
float _rotationAngle;
}; };

7
src/Global/Constants.h Normal file
View File

@ -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;

View File

@ -1,3 +1,5 @@
#include "Globals.h" #include "Globals.h"
SDL_Surface* screen = NULL;
SDL_Event event; SDL_Event event;

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <SDL/SDL.h> #include <SDL/SDL.h>
extern SDL_Surface* screen;
extern SDL_Event event; extern SDL_Event event;

View File

@ -11,7 +11,8 @@
#include "Game.h" #include "Game.h"
Game::Game(void) { Game::Game(void) {
_rotationAngle = 0.0f; _player = new Player();
//_rotationAngle = 0.0f;
} }
Game::~Game(void) { Game::~Game(void) {
@ -22,24 +23,13 @@ bool Game::Init(void) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
Texture* testTexture = new Texture(); _player->Prepare();
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));
// Return success. // Return success.
return true; return true;
} }
void Game::Prepare(float dt) { void Game::Prepare(float dt) {
const float SPEED = 15.0f;
_rotationAngle += SPEED*dt;
if(_rotationAngle > 360.0f) {
_rotationAngle -= 360.0f;
}
} }
void Game::Render(void) { void Game::Render(void) {
@ -52,14 +42,13 @@ void Game::Render(void) {
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
_testSprite->SetRotation(_rotationAngle); // Render our shit..
_testSprite->Draw(); _player->Render();
} }
void Game::Shutdown(void) { void Game::Shutdown(void) {
Debug::logger->message("\n ----- Cleaning Engine -----"); Debug::logger->message("\n ----- Cleaning Engine -----");
delete _testSprite->GetTexture(); delete _player;
delete _testSprite;
} }
void Game::OnResize(int width, int height) { void Game::OnResize(int width, int height) {

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "../IO/Input.h" #include "../IO/Input.h"
#include "../Actor/Player.h"
class Sprite; class Sprite;
@ -16,6 +17,5 @@ public:
void OnResize(int width, int height); void OnResize(int width, int height);
private: private:
float _rotationAngle; Player* _player;
Sprite* _testSprite;
}; };

View File

@ -1,75 +1,58 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN #define WIN32_EXTRA_LEAN
#endif
#define GLX_GLXEXT_LEGACY // Use our local glxext.h rather than the system one. #define GLX_GLXEXT_LEGACY // Use our local glxext.h rather than the system one.
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include "GLWindow.h"
#else
#include "LGLXWindow.h"
#endif #endif
#include "Game.h" #include "Game.h"
#include "../Global/Globals.h"
#include "../Global/Constants.h"
#include "../System/Debug.h" #include "../System/Debug.h"
void Destroy(void) {
SDL_FreeSurface(screen);
SDL_Quit();
}
#ifdef _WIN32 #ifdef _WIN32
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow) { int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int cmdShow) {
#else #else
int main(int argc, char** argv) { int main(int argc, char** argv) {
#endif #endif
// Start by opening a debug log. // Start by opening a debug log.
Debug::openLog(true); Debug::openLog(true);
Debug::logger->message("\n ----- Engine Loading ------"); Debug::logger->message("\n ----- Engine Loading -----");
// Get our window settings. // Our game code.
const int windowWidth = 800; Game game;
const int windowHeight = 600;
const int windowBPP = 16;
const int windowFullscreen = false;
#ifdef _WIN32 if(SDL_Init(SDL_INIT_VIDEO == -1)) {
// This is our window. Debug::logger->message("Error: Could not load SDL");
GLWindow programWindow(hInstance); } else {
#else Debug::logger->message("SDL loaded..");
LGLXWindow programWindow; }
#endif
// Our game code.
Game game;
// Attach our game to the window. screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_HWSURFACE);
programWindow.AttachGame(&game); Debug::logger->message("Video mode set..");
// Attempt to create the window. SDL_WM_SetCaption("LibD", NULL);
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();
game.Prepare(elapsedTime); // Do any pre-rendering logic. srand((unsigned int)time(NULL));
game.Render(); // Render the scene.
programWindow.SwapBuffers(); Debug::logger->message("\n ----- Engine Initialization Complete -----");
} Debug::logger->message("\n ----- Logic -----");
game.Shutdown(); // Free any resouces.
programWindow.Destroy(); // Destroy the program window. bool isRunning = true;
Debug::closeLog(); while(isRunning) {
return 0; break;
}
Destroy();
Debug::closeLog();
return 0;
} }

View File

@ -5,7 +5,7 @@ Sprite::Sprite() {
texture = NULL; texture = NULL;
size = Vec2(0.0f, 0.0f); size = Vec2(0.0f, 0.0f);
scale = Vec2(1.0f, 1.0f); scale = Vec2(1.0f, 1.0f);
handle = Vec2(0.0f, 0.0f); position = Vec2(0.0f, 0.0f);
} }
Sprite::~Sprite() { Sprite::~Sprite() {
@ -46,7 +46,7 @@ void Sprite::Draw() const {
// Temporary solution. // Temporary solution.
Vec2 halfScaledSize = scaledSize / 2.0f; 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); glRotatef(rotation, 0.0f, 0.0f, 1.0f);
glTranslatef(-halfScaledSize.x, -halfScaledSize.y, 0.0f); glTranslatef(-halfScaledSize.x, -halfScaledSize.y, 0.0f);

View File

@ -12,20 +12,20 @@ public:
virtual void Update(float dt); virtual void Update(float dt);
virtual void Draw() const; virtual void Draw() const;
const Vec2& GetHandle() const { return handle; } const Vec2& GetPosition() const { return position; }
const Vec2& GetSize() const { return size; } const Vec2& GetSize() const { return size; }
const Vec2& GetScale() const { return scale; } const Vec2& GetScale() const { return scale; }
float GetRotation() const { return rotation; } float GetRotation() const { return rotation; }
Texture* GetTexture() { return texture; } Texture* GetTexture() { return texture; }
const Texture* GetTexture() const { 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 SetScale(const Vec2& scale) { this->scale = scale; }
void SetRotation(float rotation) { this->rotation = rotation; } void SetRotation(float rotation) { this->rotation = rotation; }
void SetTexture(Texture* texture); void SetTexture(Texture* texture);
protected: protected:
Vec2 handle; Vec2 position;
Vec2 size; Vec2 size;
Vec2 scale; Vec2 scale;
float rotation; float rotation;