[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
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 +=

View File

@ -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) {

View File

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

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"
SDL_Surface* screen = NULL;
SDL_Event event;

View File

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

View File

@ -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) {

View File

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

View File

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

View File

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

View File

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