[Add] Implemented Input decices.

This commit is contained in:
Rtch90 2012-04-08 18:16:35 +01:00
parent a277545963
commit 60366c7ad0
7 changed files with 247 additions and 236 deletions

View File

@ -31,6 +31,9 @@ bool CreateInput(void) {
memcpy(keyboard.keys, tempKeys, sizeof(char) * keyboard.keycount); memcpy(keyboard.keys, tempKeys, sizeof(char) * keyboard.keycount);
mouse.buttons = SDL_GetMouseState(&mouse.dx, &mouse.dy); mouse.buttons = SDL_GetMouseState(&mouse.dx, &mouse.dy);
if(&keyboard > 0 || &mouse > 0){
Debug::logger->message("Input device registered");
}
return true; return true;
} }
@ -79,4 +82,5 @@ bool MouseStillUp(int button) { return(!_curr_mouse(button) && !_old_mouse(b
void DestroyInput(void) { void DestroyInput(void) {
free(keyboard.keys); free(keyboard.keys);
free(keyboard.oldKeys); free(keyboard.oldKeys);
Debug::logger->message("Input device destroyed");
} }

View File

@ -1,43 +1,45 @@
#pragma once #pragma once
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include "../System/Debug.h"
typedef struct mouse_s { typedef struct mouse_s {
int dx, dy; int dx, dy;
int oldx, oldy; int oldx, oldy;
unsigned int buttons; unsigned int buttons;
unsigned int oldButtons; unsigned int oldButtons;
} mouse_t; } mouse_t;
typedef struct keyboard_s { typedef struct keyboard_s {
unsigned char *keys; unsigned char *keys;
unsigned char *oldKeys; unsigned char *oldKeys;
int keycount; int keycount;
int lastChar; int lastChar;
unsigned int mods; unsigned int mods;
} keyboard_t; } keyboard_t;
typedef struct input_s { typedef struct input_s {
mouse_t mouse; mouse_t mouse;
keyboard_t keyboard; keyboard_t keyboard;
} input_t; } input_t;
bool CreateInput(void); bool CreateInput(void);
void UpdateInput(void); void UpdateInput(void);
char GetKey(void); char GetKey(void);
unsigned int GetX(void); unsigned int GetX(void);
unsigned int GetY(void); unsigned int GetY(void);
unsigned int GetOldX(void); unsigned int GetOldX(void);
unsigned int GetOldY(void); unsigned int GetOldY(void);
unsigned int GetMods(void); unsigned int GetMods(void);
bool KeyDown(int index); bool KeyDown(int index);
bool KeyStillDown(int index); bool KeyStillDown(int index);
bool KeyUp(int index); bool KeyUp(int index);
bool KeyStillUp(int index); bool KeyStillUp(int index);
bool MouseDown(int button); bool MouseDown(int button);
bool MouseStillDown(int button); bool MouseStillDown(int button);
bool MouseUp(int button); bool MouseUp(int button);
bool MouseStillUp(int button); bool MouseStillUp(int button);
void DestroyInput(void); void DestroyInput(void);

View File

@ -6,12 +6,12 @@
#include <GL/glu.h> #include <GL/glu.h>
#include "../System/Debug.h" #include "../System/Debug.h"
#include "Game.h"
#include "../Sprite/Sprite.h" #include "../Sprite/Sprite.h"
#include "../Texture/Texture.h" #include "../Texture/Texture.h"
#include "Game.h"
Game::Game(void) { Game::Game(void) {
_rotationAngle = 0.0f; _rotationAngle = 0.0f;
} }
Game::~Game(void) { Game::~Game(void) {
@ -19,56 +19,57 @@ Game::~Game(void) {
} }
bool Game::Init(void) { bool Game::Init(void) {
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
Texture* testTexture = new Texture(); Texture* testTexture = new Texture();
testTexture->Load("../Data/Img/test.png"); testTexture->Load("../Data/Img/test.png");
_testSprite = new Sprite(); _testSprite = new Sprite();
_testSprite->SetTexture(testTexture); _testSprite->SetTexture(testTexture);
_testSprite->SetHandle(Vec2(800/2, 600/2)); _testSprite->SetHandle(Vec2(800/2, 600/2));
_testSprite->SetScale(Vec2(5.0f, 5.0f)); _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; const float SPEED = 15.0f;
_rotationAngle += SPEED*dt; _rotationAngle += SPEED*dt;
if(_rotationAngle > 360.0f) { if(_rotationAngle > 360.0f) {
_rotationAngle -= 360.0f; _rotationAngle -= 360.0f;
} }
} }
void Game::Render(void) { void Game::Render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
glOrtho(0.0, 800.0, 600.0, 0.0, 0.0, 1.0); glOrtho(0.0, 800.0, 600.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
_testSprite->SetRotation(_rotationAngle); _testSprite->SetRotation(_rotationAngle);
_testSprite->Draw(); _testSprite->Draw();
} }
void Game::Shutdown(void) { void Game::Shutdown(void) {
delete _testSprite->GetTexture(); Debug::logger->message("\n ----- Cleaning Engine -----");
delete _testSprite->GetTexture();
delete _testSprite; delete _testSprite;
} }
void Game::OnResize(int width, int height) { void Game::OnResize(int width, int height) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
gluPerspective(45.0f, float(width) / float(height), 1.0f, 100.0f); gluPerspective(45.0f, float(width) / float(height), 1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
} }

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "../IO/Input.h"
class Sprite; class Sprite;
@ -6,15 +7,15 @@ class Game {
public: public:
Game(void); Game(void);
~Game(void); ~Game(void);
bool Init(void); bool Init(void);
void Prepare(float dt); void Prepare(float dt);
void Render(void); void Render(void);
void Shutdown(void); void Shutdown(void);
void OnResize(int width, int height); void OnResize(int width, int height);
private: private:
float _rotationAngle; float _rotationAngle;
Sprite* _testSprite; Sprite* _testSprite;
}; };

View File

@ -17,13 +17,13 @@ using std::string;
unsigned int GetTickCount(void) { unsigned int GetTickCount(void) {
struct timeval t; struct timeval t;
gettimeofday(&t, NULL); gettimeofday(&t, NULL);
unsigned long secs = t.tv_sec * 1000; unsigned long secs = t.tv_sec * 1000;
secs += (t.tv_usec / 1000); secs += (t.tv_usec / 1000);
return secs; return secs;
} }
LGLXWindow::LGLXWindow(void) : LGLXWindow::LGLXWindow(void) :
_game(NULL), _game(NULL),
_isRunning(true), _isRunning(true),
_lastTime(0), _lastTime(0),
@ -35,208 +35,210 @@ LGLXWindow::LGLXWindow(void) :
_width(0), _width(0),
_height(0), _height(0),
_bpp(0), _bpp(0),
_GL3Supported(false) _GL3Supported(false)
{} { CreateInput(); }
LGLXWindow::~LGLXWindow(void) { LGLXWindow::~LGLXWindow(void) {
} }
bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) { bool LGLXWindow::Create(int width, int height, int bpp, bool fullscreen) {
// Open the default display. // Open the default display.
_display = XOpenDisplay(0); _display = XOpenDisplay(0);
if(!_display) { if(!_display) {
Debug::logger->message("Could not open the display."); Debug::logger->message("Could not open the display.");
return false; return false;
} }
// Get the default screen ID. // Get the default screen ID.
_screenID = DefaultScreen(_display); _screenID = DefaultScreen(_display);
int n = 0, modeNum = 0; int n = 0, modeNum = 0;
// Get a frambuffer config useing the default attributes. // Get a frambuffer config useing the default attributes.
GLXFBConfig framebufferConfig = (*glXChooseFBConfig(_display, DefaultScreen(_display), 0, &n)); GLXFBConfig framebufferConfig = (*glXChooseFBConfig(_display, DefaultScreen(_display), 0, &n));
XF86VidModeModeInfo **modes; XF86VidModeModeInfo **modes;
if(!XF86VidModeGetAllModeLines(_display, _screenID, &modeNum, &modes)) { if(!XF86VidModeGetAllModeLines(_display, _screenID, &modeNum, &modes)) {
Debug::logger->message("Could not query the video modes."); Debug::logger->message("Could not query the video modes.");
return false; return false;
} }
_XF86DeskMode = *modes[0]; _XF86DeskMode = *modes[0];
int bestMode = -1; int bestMode = -1;
for(int i = 0; i < modeNum; i++) { for(int i = 0; i < modeNum; i++) {
if((modes[i]->hdisplay == width) && (modes[i]->vdisplay == height)) { if((modes[i]->hdisplay == width) && (modes[i]->vdisplay == height)) {
bestMode = i; bestMode = i;
} }
} }
if(bestMode == -1) { if(bestMode == -1) {
Debug::logger->message("Could not find a suitable graphics mode."); Debug::logger->message("Could not find a suitable graphics mode.");
return false; return false;
} }
int doubleBufferedAttribList[] = { int doubleBufferedAttribList[] = {
GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RGBA, GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 4, GLX_RED_SIZE, 4,
GLX_GREEN_SIZE, 4, GLX_GREEN_SIZE, 4,
GLX_BLUE_SIZE, 4, GLX_BLUE_SIZE, 4,
GLX_DEPTH_SIZE, 16, GLX_DEPTH_SIZE, 16,
None None
}; };
XVisualInfo* vi = NULL; XVisualInfo* vi = NULL;
// Attempt to create a double buffered window. // Attempt to create a double buffered window.
vi = glXChooseVisual(_display, _screenID, doubleBufferedAttribList); vi = glXChooseVisual(_display, _screenID, doubleBufferedAttribList);
if(!vi) { if(!vi) {
Debug::logger->message("Could not create a double buffere window.. Sux.."); Debug::logger->message("Could not create a double buffere window.. Sux..");
} }
// Time to create a GL 2.1 context. // Time to create a GL 2.1 context.
GLXContext gl2Context = glXCreateContext(_display, vi, 0, GL_TRUE); GLXContext gl2Context = glXCreateContext(_display, vi, 0, GL_TRUE);
if(!gl2Context) { if(!gl2Context) {
Debug::logger->message("Could Not create a GL 2.1 context, check your darn graphics drivers"); Debug::logger->message("Could Not create a GL 2.1 context, check your darn graphics drivers");
return false; return false;
} }
// Get a pointer to the GL 3.0 context creation. // Get a pointer to the GL 3.0 context creation.
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribs
= (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB"); = (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((GLubyte*)"glXCreateContextAttribsARB");
if(glXCreateContextAttribs == NULL) { if(glXCreateContextAttribs == NULL) {
Debug::logger->message("OpenGL 3.0 is not supported, falling back to 2.1"); Debug::logger->message("OpenGL 3.0 is not supported, falling back to 2.1");
_glContext = gl2Context; _glContext = gl2Context;
_GL3Supported = false; _GL3Supported = false;
} else { } else {
// We create a GL 3.0 context.. // We create a GL 3.0 context..
int attribs[] = { int attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 3, // We want a 3.0 context. GLX_CONTEXT_MAJOR_VERSION_ARB, 3, // We want a 3.0 context.
GLX_CONTEXT_MINOR_VERSION_ARB, 0, GLX_CONTEXT_MINOR_VERSION_ARB, 0,
0 // Zero indicates the end of the array. 0 // Zero indicates the end of the array.
}; };
_glContext = glXCreateContextAttribs(_display, framebufferConfig, 0, true, &attribs[0]); _glContext = glXCreateContextAttribs(_display, framebufferConfig, 0, true, &attribs[0]);
// We can destroy thr GL 2.0 context once the 3.0 one has been checked. // We can destroy thr GL 2.0 context once the 3.0 one has been checked.
glXDestroyContext(_display, gl2Context); glXDestroyContext(_display, gl2Context);
_GL3Supported = true; _GL3Supported = true;
} }
Colormap cmap = XCreateColormap(_display, RootWindow(_display, vi->screen), vi->visual, AllocNone); Colormap cmap = XCreateColormap(_display, RootWindow(_display, vi->screen), vi->visual, AllocNone);
_XSetAttr.colormap = cmap; _XSetAttr.colormap = cmap;
_XSetAttr.border_pixel = 0; _XSetAttr.border_pixel = 0;
_XSetAttr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | _XSetAttr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask |
StructureNotifyMask; StructureNotifyMask;
_XSetAttr.override_redirect = False; _XSetAttr.override_redirect = False;
//unsigned long windowAttributes = CWBorderPixel | CWColormap | CWEventMask; //unsigned long windowAttributes = CWBorderPixel | CWColormap | CWEventMask;
if(fullscreen) { if(fullscreen) {
//windowAttributes = CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect; //windowAttributes = CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect;
XF86VidModeSwitchToMode(_display, _screenID, modes[bestMode]); XF86VidModeSwitchToMode(_display, _screenID, modes[bestMode]);
XF86VidModeSetViewPort(_display, _screenID, 0, 0); XF86VidModeSetViewPort(_display, _screenID, 0, 0);
_XSetAttr.override_redirect = True; _XSetAttr.override_redirect = True;
} }
_xWindow = XCreateWindow(_display, RootWindow(_display, vi->screen), _xWindow = XCreateWindow(_display, RootWindow(_display, vi->screen),
0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &_XSetAttr); CWBorderPixel | CWColormap | CWEventMask, &_XSetAttr);
string title = "LibD"; string title = "LibD";
if(fullscreen) { if(fullscreen) {
XWarpPointer(_display, None, _xWindow, 0, 0, 0, 0, 0, 0); XWarpPointer(_display, None, _xWindow, 0, 0, 0, 0, 0, 0);
XMapRaised(_display, _xWindow); XMapRaised(_display, _xWindow);
XGrabKeyboard(_display, _xWindow, True, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabKeyboard(_display, _xWindow, True, GrabModeAsync, GrabModeAsync, CurrentTime);
XGrabPointer(_display, _xWindow, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, XGrabPointer(_display, _xWindow, True, ButtonPressMask, GrabModeAsync, GrabModeAsync,
_xWindow, None, CurrentTime); _xWindow, None, CurrentTime);
} else { } else {
Atom wmDelete = XInternAtom(_display, "WM_DELETE_WINDOW", True); Atom wmDelete = XInternAtom(_display, "WM_DELETE_WINDOW", True);
XSetWMProtocols(_display, _xWindow, &wmDelete, 1); XSetWMProtocols(_display, _xWindow, &wmDelete, 1);
XSetStandardProperties(_display, _xWindow, title.c_str(), None, 0, NULL, 0, NULL); XSetStandardProperties(_display, _xWindow, title.c_str(), None, 0, NULL, 0, NULL);
XMapRaised(_display, _xWindow); XMapRaised(_display, _xWindow);
} }
XFree(modes); XFree(modes);
glXMakeCurrent(_display, _xWindow, _glContext); glXMakeCurrent(_display, _xWindow, _glContext);
int posX = 0; int posX = 0;
int posY = 0; int posY = 0;
Window winDummy; Window winDummy;
unsigned int borderDummy; unsigned int borderDummy;
_width = (unsigned) width; _width = (unsigned) width;
_height = (unsigned) height; _height = (unsigned) height;
_bpp = (unsigned) bpp; _bpp = (unsigned) bpp;
XGetGeometry(_display, _xWindow, &winDummy, &posX, &posY, XGetGeometry(_display, _xWindow, &winDummy, &posX, &posY,
&_width, &_height, &borderDummy, &_bpp); &_width, &_height, &borderDummy, &_bpp);
// Init the time. // Init the time.
_lastTime = GetTickCount(); _lastTime = GetTickCount();
return true; return true;
} }
void LGLXWindow::Destroy(void) { void LGLXWindow::Destroy(void) {
if(_glContext) { if(_glContext) {
glXMakeCurrent(_display, None, NULL); glXMakeCurrent(_display, None, NULL);
glXDestroyContext(_display, _glContext); glXDestroyContext(_display, _glContext);
_glContext = NULL; _glContext = NULL;
} }
if(_isFullscreen) { if(_isFullscreen) {
XF86VidModeSwitchToMode(_display, _screenID, &_XF86DeskMode); XF86VidModeSwitchToMode(_display, _screenID, &_XF86DeskMode);
XF86VidModeSetViewPort(_display, _screenID, 0, 0); XF86VidModeSetViewPort(_display, _screenID, 0, 0);
} }
DestroyInput();
XCloseDisplay(_display); XCloseDisplay(_display);
} }
void LGLXWindow::ProcessEvents(void) { void LGLXWindow::ProcessEvents(void) {
XEvent event; XEvent event;
while(XPending(_display) > 0) { while(XPending(_display) > 0) {
XNextEvent(_display, &event); XNextEvent(_display, &event);
switch(event.type) { switch(event.type) {
case Expose: case Expose:
if(event.xexpose.count != 0) if(event.xexpose.count != 0)
break; break;
break; break;
case ConfigureNotify: case ConfigureNotify:
{ {
int width = event.xconfigure.width; int width = event.xconfigure.width;
int height = event.xconfigure.height; int height = event.xconfigure.height;
GetAttachedGame()->OnResize(width, height); GetAttachedGame()->OnResize(width, height);
} }
break; break;
case KeyPress: case KeyPress:
{ {
if(XLookupKeysym(&event.xkey, 0) == XK_Escape) { if(XLookupKeysym(&event.xkey, 0) == XK_Escape) {
_isRunning = false; _isRunning = false;
} }
// Register the key press with keyboard interface. // Register the key press with keyboard interface.
} }
break; break;
case KeyRelease: case KeyRelease:
{ {
// Code here NAW! if(KeyStillUp(SDLK_SPACE)) {
} Debug::logger->message("Testing Input!");
break; }
case ClientMessage: // Code here NAW!
if(string(XGetAtomName(_display, event.xclient.message_type)) == string("WM_PROTOCOLS")) { }
_isRunning = true; break;
} case ClientMessage:
break; if(string(XGetAtomName(_display, event.xclient.message_type)) == string("WM_PROTOCOLS")) {
default: _isRunning = true;
break; }
} break;
} default:
break;
}
}
} }
float LGLXWindow::GetElapsedSeconds(void) { float LGLXWindow::GetElapsedSeconds(void) {
unsigned int currentTime = GetTickCount(); unsigned int currentTime = GetTickCount();
unsigned int diff = currentTime - _lastTime; unsigned int diff = currentTime - _lastTime;
_lastTime = currentTime; _lastTime = currentTime;
return float(diff) / 1000.0f; return float(diff) / 1000.0f;
} }

View File

@ -18,39 +18,41 @@ class LGLXWindow {
public: public:
LGLXWindow(void); LGLXWindow(void);
virtual ~LGLXWindow(void); virtual ~LGLXWindow(void);
bool Create(int width, int hight, int bpp, bool fullscreen); bool Create(int width, int hight, int bpp, bool fullscreen);
void Destroy(void); void Destroy(void);
void ProcessEvents(void); void ProcessEvents(void);
void AttachGame(Game* game) { _game = game; } void AttachGame(Game* game) { _game = game; }
bool IsRunning(void) { return _isRunning; } bool IsRunning(void) { return _isRunning; }
void SwapBuffers(void) { glXSwapBuffers(_display, _xWindow); } void SwapBuffers(void) { glXSwapBuffers(_display, _xWindow); }
float GetElapsedSeconds(void); float GetElapsedSeconds(void);
private: private:
// A pointer to our game thingy.. // A pointer to our game thingy..
Game* _game; Game* _game;
// Check to see if the window is still running FFS. // Check to see if the window is still running FFS.
bool _isRunning; bool _isRunning;
keyboard_t keyboard;
Game* GetAttachedGame(void) { return _game; } Game* GetAttachedGame(void) { return _game; }
unsigned int _lastTime; unsigned int _lastTime;
Display* _display; Display* _display;
Window _xWindow; Window _xWindow;
GLXContext _glContext; GLXContext _glContext;
XF86VidModeModeInfo _XF86DeskMode; XF86VidModeModeInfo _XF86DeskMode;
XSetWindowAttributes _XSetAttr; XSetWindowAttributes _XSetAttr;
int _screenID; int _screenID;
bool _isFullscreen; bool _isFullscreen;
unsigned int _width; unsigned int _width;
unsigned int _height; unsigned int _height;
unsigned int _bpp; unsigned int _bpp;
bool _GL3Supported; bool _GL3Supported;
// I think that's about all I need for now.. FOR NOW!!! // I think that's about all I need for now.. FOR NOW!!!
}; };

View File

@ -69,8 +69,7 @@ int main(int argc, char** argv) {
programWindow.SwapBuffers(); programWindow.SwapBuffers();
} }
game.Shutdown(); // Free any resouces. game.Shutdown(); // Free any resouces.
Debug::closeLog();
programWindow.Destroy(); // Destroy the program window. programWindow.Destroy(); // Destroy the program window.
Debug::closeLog();
return 0; return 0;
} }