1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2015-12-22 17:43:51 +01:00
commit edfc5298e1
252 changed files with 93965 additions and 0 deletions

View file

@ -0,0 +1,25 @@
#ifndef SPECTRE_PLATFORM_H
#define SPECTRE_PLATFORM_H
class PlatformInput;
class PlatformDisplay;
class MessageQueue;
class PlatformApplication
{
public :
virtual void init() = 0;
virtual void shutdown() = 0;
//virtual PlatformDisplay& getDisplay() = 0;
virtual PlatformInput& getInput() = 0;
virtual MessageQueue& getMessageQueue() = 0;
virtual void update() = 0;
};
#endif /* SPECTRE_PLATFORM_H */

View file

@ -0,0 +1,32 @@
#include <Spectre/Display/Display.h>
#include "PlatformDisplay.h"
#ifdef _WIN32
#include <Platform/Win32/Win32Display.h>
typedef Win32Display DisplayType;
#else
#error "No Display implementation exists"
#endif
PlatformDisplay* PlatformDisplay::make(Display* parent)
{
DisplayType* disp = new DisplayType();
disp->m_parent = parent;
return disp;
}
PlatformDisplay::PlatformDisplay()
{
}
PlatformDisplay::~PlatformDisplay()
{
// Nothing to do.
}
void PlatformDisplay::onReshape(int width, int height)
{
// Forward to parent.
m_parent->onReshape(width, height);
}

View file

@ -0,0 +1,52 @@
#ifndef SPECTRE_PLATFORM_DISPLAY_H
#define SPECTRE_PLATFORM_DISPLAY_H
#include <Spectre/Math/Vector2.h>
#include <Spectre/Display/DisplayDescription.h>
// Low-level platform dependant API.
#include <string>
class Display;
class PlatformDisplay
{
public :
// Factory method.
static PlatformDisplay* make(Display* parent);
virtual ~PlatformDisplay();
virtual bool create(DisplayDescription description) = 0;
virtual void destroy() = 0;
virtual void* getHandle() const = 0;
virtual bool isValid() = 0;
virtual void setSize(unsigned int width, unsigned int height) = 0;
virtual Vector2u getSize() const = 0;
virtual void setPosition(unsigned int x, unsigned int y) = 0;
virtual void setCaption(const std::string& caption) = 0;
virtual void setIcon(const std::string& icon) = 0;
virtual void showCursor(bool value) = 0;
protected :
PlatformDisplay();
void onReshape(int width, int height);
private :
Display* m_parent;
};
#endif /* SPECTRE_PLATFORM_DISPLAY_H */

View file

@ -0,0 +1,18 @@
#ifndef PLATFORM_INPUT_H
#define PLATFORM_INPUT_H
class Keyboard;
class Mouse;
class PlatformInput
{
public :
virtual Keyboard* createKeyboard() = 0;
virtual Mouse* createMouse() = 0;
virtual void update() = 0;
};
#endif /* PLATFORM_INPUT_H */

View file

@ -0,0 +1,19 @@
#ifndef PLATFORM_MISC_H
#define PLATFORM_MISC_H
#include <Spectre/Display/DisplayMode.h>
#include <vector>
class DisplayMode;
class PlatformMisc
{
public:
static void GetDisplayModes(std::vector<DisplayMode>& modes);
static DisplayMode GetDesktopMode();
};
#endif /* PLATFORM_MISC_H */

View file

@ -0,0 +1,96 @@
#include <windows.h>
#include "Win32Keyboard.h"
#include "Win32Mouse.h"
#include "Win32Application.h"
void Win32Application::init()
{
}
void Win32Application::shutdown()
{
}
/*
PlatformDisplay& Win32Application::getDisplay()
{
return m_display;
} */
PlatformInput& Win32Application::getInput()
{
return m_input;
}
MessageQueue& Win32Application::getMessageQueue()
{
return m_messageQueue;
}
void Win32Application::update()
{
MSG msg;
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
processMessage(msg);
}
}
LRESULT Win32Application::processMessage(MSG msg)
{
switch(msg.message) {
case WM_QUIT :
m_messageQueue.postEvent(SysEvent(SysEvent::Quit));
return 0;
// Input, Forward to devices.
case WM_KEYDOWN :
case WM_KEYUP :
OutputDebugString("WM_KEYDOWN\n");
//SetCapture(msg.hwnd);
if (Win32Keyboard::handleMessage(msg)) {
// Keyboard did handle the message.
return 0;
}
break;
case WM_MOUSEMOVE :
case WM_MOUSELEAVE :
if (Win32Mouse::handleMessage(msg)) {
// Mouse did handle the message.
return 0;
}
break;
case WM_LBUTTONDOWN :
case WM_RBUTTONDOWN :
case WM_MBUTTONDOWN :
case WM_XBUTTONDOWN :
SetCapture(msg.hwnd);
if (Win32Mouse::handleMessage(msg)) {
// Mouse did handle the message.
return 0;
}
break;
case WM_LBUTTONUP :
case WM_RBUTTONUP :
case WM_MBUTTONUP :
case WM_XBUTTONUP :
ReleaseCapture();
if (Win32Mouse::handleMessage(msg)) {
// Mouse did handle the message.
return 0;
}
break;
default :
break;
}
// Message was not intercepted. Pass down to window.
return DispatchMessage(&msg);
}

View file

@ -0,0 +1,38 @@
#ifndef WIN32_PLATFORM_H
#define WIN32_PLATFORM_H
#include <Windows.h>
#include "Win32Display.h"
#include "Win32Input.h"
#include <Spectre/System/MessageQueue.h>
#include <Platform/PlatformApplication.h>
class Win32Application : public PlatformApplication
{
public :
virtual void init();
virtual void shutdown();
//virtual PlatformDisplay& getDisplay();
virtual PlatformInput& getInput();
virtual MessageQueue& getMessageQueue();
virtual void update();
protected :
LRESULT processMessage(MSG msg);
protected :
//Win32Display m_display;
Win32Input m_input;
MessageQueue m_messageQueue;
};
#endif /* WIN32_PLATFORM_H */

View file

@ -0,0 +1,260 @@
#include <Windows.h>
#include <Spectre/Display/Display.h>
#include <Spectre/System/SystemEvent.h>
#include <Spectre/System/Log.h>
#include "Win32Application.h"
#include "Win32Internal.h"
#include "Win32Display.h"
#define WND_CLASSNAME "SPECTRE_WIN32_WNDCLASS"
static bool firstTime = true;
Win32Display::Win32Display() :
m_handle (NULL),
m_icon (0),
m_cursor (0),
m_inResizeModalLoop (false),
m_minSize (200, 200)
{
}
Win32Display::~Win32Display()
{
if (m_icon) {
DestroyIcon(m_icon);
}
if (m_handle) {
DestroyWindow(m_handle);
}
}
bool Win32Display::create(DisplayDescription description)
{
DWORD flags = getWin32Flags(description.decoration);
int x, y;
if (firstTime) {
registerClass();
firstTime = false;
}
centerWindow(x, y, description.mode.width, description.mode.height);
// Create window.
m_handle = CreateWindowExA(0, WND_CLASSNAME, "", flags,
x, y, description.mode.width, description.mode.height,
NULL, NULL, GetModuleHandle(NULL), (LPVOID) this);
if (!m_handle) {
log("Win32 - Could not create window: %s", Win32GetMessage(GetLastError()));
return false;
}
setSize(description.mode.width, description.mode.height);
// Store the size for use later.
m_size = getSize();
return true;
}
void Win32Display::destroy()
{
if (m_handle) {
DestroyWindow(m_handle);
m_handle = NULL;
}
}
void* Win32Display::getHandle() const
{
return m_handle;
}
bool Win32Display::isValid()
{
return m_handle != NULL;
}
void Win32Display::setSize(unsigned int width, unsigned int height)
{
int w, h;
RECT rect = {0, 0, width, height};
AdjustWindowRect(&rect, GetWindowLong(m_handle, GWL_STYLE), false);
w = rect.right - rect.left;
h = rect.bottom - rect.top;
::SetWindowPos(m_handle, NULL, 0, 0, w, h, SWP_NOMOVE | SWP_NOZORDER);
}
Vector2u Win32Display::getSize() const
{
RECT rect;
Vector2u size;
if (GetClientRect(m_handle, &rect)) {
size.x = rect.right - rect.left;
size.y = rect.bottom - rect.top;
}
return size;
}
void Win32Display::setPosition(unsigned int x, unsigned int y)
{
::SetWindowPos(m_handle, NULL, x, y, 0, 0, SWP_NOSIZE);
}
void Win32Display::setCaption(const std::string& caption)
{
::SetWindowText(m_handle, caption.c_str());
}
void Win32Display::showCursor(bool value)
{
if (value) {
m_cursor = ::LoadCursor(NULL, IDC_ARROW);
} else {
m_cursor = 0;
}
::SetCursor(m_cursor);
}
void Win32Display::setIcon(const std::string& icon)
{
HICON hIcon = (HICON) ::LoadImage(0, icon.c_str(), IMAGE_ICON,
0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (hIcon) {
if (m_icon) {
DestroyIcon(m_icon);
}
m_icon = hIcon;
::SendMessage(m_handle, WM_SETICON, ICON_SMALL, (LPARAM) hIcon);
::SendMessage(m_handle, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
}
}
void Win32Display::registerClass()
{
WNDCLASSA wndcl;
ZeroMemory(&wndcl, sizeof(wndcl));
wndcl.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wndcl.lpfnWndProc = Win32Display::staticWndProc;
wndcl.hInstance = ::GetModuleHandle(NULL);
wndcl.lpszClassName = WND_CLASSNAME;
::RegisterClass(&wndcl);
}
DWORD Win32Display::getWin32Flags(unsigned int flags)
{
DWORD win32_flags = WS_VISIBLE;
if (flags == DisplayDecorate::None) {
win32_flags |= WS_POPUP;
} else {
if (flags & DisplayDecorate::Menu) {
win32_flags |= WS_CAPTION | WS_MINIMIZEBOX;
}
if (flags & DisplayDecorate::Resize) {
win32_flags |= WS_THICKFRAME | WS_MAXIMIZEBOX;
}
if (flags & DisplayDecorate::Close) {
win32_flags |= WS_SYSMENU;
}
}
return win32_flags;
}
void Win32Display::centerWindow(int &x, int &y, int width, int height)
{
x = (::GetSystemMetrics(SM_CXSCREEN) - width) / 2;
y = (::GetSystemMetrics(SM_CYSCREEN) - height) / 2;
}
void Win32Display::processResizeMessage(const Vector2u& new_size)
{
// Check if the size has actually changed.
if (m_size != new_size) {
// Update the size and notify the higher layer.
m_size = new_size;
onReshape(m_size.x, m_size.y);
}
}
void Win32Display::processMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) {
case WM_SETCURSOR :
if (LOWORD(lParam) == HTCLIENT) {
::SetCursor(m_cursor);
}
break;
case WM_DESTROY :
destroy();
PostQuitMessage(0);
break;
case WM_SETFOCUS :
OutputDebugString("WM_SETFOCUS\n");
break;
case WM_KILLFOCUS :
OutputDebugString("WM_KILLFOCUS\n");
break;
case WM_SIZE :
if (!m_inResizeModalLoop && (wParam == SIZE_MAXIMIZED || wParam == SIZE_RESTORED)) {
processResizeMessage(getSize());
}
break;
case WM_GETMINMAXINFO :
{
MINMAXINFO* info = (MINMAXINFO*)lParam;
info->ptMinTrackSize.x = m_minSize.x;
info->ptMinTrackSize.y = m_minSize.y;
info->ptMaxTrackSize.x = 99999;
info->ptMaxTrackSize.y = 99999;
break;
}
case WM_ENTERSIZEMOVE :
m_inResizeModalLoop = true;
break;
case WM_EXITSIZEMOVE :
m_inResizeModalLoop = false;
processResizeMessage(getSize());
break;
}
}
LRESULT CALLBACK Win32Display::staticWndProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
Win32Display *display;
if (message == WM_NCCREATE) {
LONG_PTR ptr = (LONG_PTR) ((LPCREATESTRUCT)lParam)->lpCreateParams;
::SetWindowLong(handle, GWL_USERDATA, ptr);
display = (Win32Display*) ptr;
} else {
display = (Win32Display*) ::GetWindowLong(handle, GWL_USERDATA);
}
if (display) {
display->processMessage(message, wParam, lParam);
}
return DefWindowProc(handle, message, wParam, lParam);
}

View file

@ -0,0 +1,66 @@
#ifndef PLATFORM_WIN32_DISPLAY_H
#define PLATFORM_WIN32_DISPLAY_H
#include "Win32GLContext.h"
#include <Platform/PlatformDisplay.h>
#include <Windows.h>
class Win32Display : public PlatformDisplay
{
public :
Win32Display();
virtual ~Win32Display();
virtual bool create(DisplayDescription description);
virtual void destroy();
virtual bool isValid();
virtual void* getHandle() const;
virtual void setSize(unsigned int width, unsigned int height);
virtual Vector2u getSize() const;
virtual void setPosition(unsigned int x, unsigned int y);
virtual void setCaption(const std::string& caption);
virtual void setIcon(const std::string& icon);
virtual void showCursor(bool value);
protected :
void registerClass();
DWORD getWin32Flags(unsigned int flags);
void centerWindow(int &x, int &y, int width, int height);
void processMessage(UINT message, WPARAM wParam, LPARAM lParam);
void processResizeMessage(const Vector2u& new_size);
static LRESULT CALLBACK staticWndProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam);
protected :
HWND m_handle;
HCURSOR m_cursor;
HICON m_icon;
// True if this window is in a resize loop.
bool m_inResizeModalLoop;
// Client area size. cached here to check if a resize has been made.
Vector2u m_size;
Vector2u m_minSize;
};
#endif /* PLATFORM_WIN32_DISPLAY_H */

View file

@ -0,0 +1,156 @@
#include <GL/glew.h>
#include <GL/wglew.h>
#include <Platform/PlatformDisplay.h>
#include <Spectre/System/Log.h>
#include "Win32Internal.h"
#include "Win32GLContext.h"
// Ensure that OpenGL extensions are loaded.
static void ensureExtensionsLoaded()
{
static bool glewInitOK = false;
if (!glewInitOK) {
GLenum ret = glewInit();
if (ret != GLEW_OK) {
log("Win32: Could not initialize GLEW %s\n",
glewGetErrorString(ret));
return;
}
glewInitOK = true;
}
}
Win32GLContext::Win32GLContext() :
m_wnd (NULL),
m_deviceContext (NULL),
m_renderContext (NULL)
{
}
Win32GLContext::~Win32GLContext()
{
destroy();
}
bool Win32GLContext::create(const PlatformDisplay* display, unsigned int bpp)
{
// If created. destroy old handles.
destroy();
m_wnd = (HWND) display->getHandle();
// Should have a valid handle here. trigger error.
if (!m_wnd) {
log("Win32 - Could not create GL context: Invalid display handle\n");
return false;
}
m_deviceContext = ::GetDC(m_wnd);
createGLContext(bpp);
if (!m_deviceContext || !m_renderContext) {
// Make sure we release all handles.
destroy();
log("Win32 - Could not create GL context: %s", Win32GetMessage(GetLastError()));
return false;
}
return true;
}
void Win32GLContext::destroy()
{
if (m_wnd && m_deviceContext) {
::ReleaseDC(m_wnd, m_deviceContext);
m_deviceContext = NULL;
m_wnd = NULL;
}
if (m_renderContext) {
::wglDeleteContext(m_renderContext);
m_renderContext = NULL;
}
}
void Win32GLContext::createGLContext(unsigned int bpp)
{
// First set pixel format.
if (!setPixelFormat(bpp)) {
return;
}
m_renderContext = ::wglCreateContext(m_deviceContext);
}
bool Win32GLContext::activate()
{
// Make sure we have dc and rc before calling MakeCurrent.
if (m_deviceContext && m_renderContext) {
return ::wglMakeCurrent(m_deviceContext, m_renderContext);
}
return false;
}
bool Win32GLContext::deactivate()
{
return ::wglMakeCurrent(NULL, NULL);
}
bool Win32GLContext::isActive() const
{
return ::wglGetCurrentContext() == m_renderContext;
}
bool Win32GLContext::setSwapInterval(int interval)
{
ensureExtensionsLoaded();
if (WGLEW_EXT_swap_control) {
return wglSwapIntervalEXT(interval);
}
log("wglSwapInterval: function is not supported\n");
return false;
}
void Win32GLContext::setSize(unsigned int width, unsigned int height)
{
if (activate()) {
glViewport(0, 0, width, height);
}
}
void Win32GLContext::swapBuffers()
{
if (m_deviceContext) {
::SwapBuffers(m_deviceContext);
}
}
bool Win32GLContext::setPixelFormat(unsigned int bpp)
{
PIXELFORMATDESCRIPTOR pfd;
int format;
// For now, always create a RGBA pixel format with double buffer.
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = bpp;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ::ChoosePixelFormat(m_deviceContext, &pfd);
if (format) {
return ::SetPixelFormat(m_deviceContext, format, &pfd);
}
return false;
}

View file

@ -0,0 +1,48 @@
#ifndef PLATFORM_WIN32_GLCONTEXT_H
#define PLATFORM_WIN32_GLCONTEXT_H
// Win32 OpenGL Context (wgl)
#include <Windows.h>
#include <Spectre/Display/GLContext.h>
class Win32GLContext : public GLContext
{
public :
Win32GLContext();
~Win32GLContext();
// Create a context associated with a display.
bool create(const PlatformDisplay* display, unsigned int bpp);
void destroy();
bool activate();
bool deactivate();
bool isActive() const;
bool setSwapInterval(int interval);
void setSize(unsigned int width, unsigned int height);
void setSize(const Vector2u size);
void swapBuffers();
private :
void createGLContext(unsigned int bpp);
bool setPixelFormat(unsigned int bpp);
private :
HWND m_wnd;
HDC m_deviceContext;
HGLRC m_renderContext;
};
#endif /* PLATFORM_WIN32_GLCONTEXT_H */

View file

@ -0,0 +1,27 @@
#include "Win32Keyboard.h"
#include "Win32Mouse.h"
#include "Win32Input.h"
Win32InputMsgBuffer Win32Input::inputMsgBuffer;
Win32InputMsgBuffer::Win32InputMsgBuffer() :
index (0),
enabled (false)
{
}
Keyboard* Win32Input::createKeyboard()
{
return new Win32Keyboard();
}
Mouse* Win32Input::createMouse()
{
return new Win32Mouse();
}
void Win32Input::update()
{
}

View file

@ -0,0 +1,32 @@
#ifndef PLATFORM_WIN32_INPUT_H
#define PLATFORM_WIN32_INPUT_H
#include <Windows.h>
#include <Platform/PlatformInput.h>
#define WIN32_INPUT_BUFFER_QUEUE_MAX_SIZE 64
struct Win32InputMsgBuffer {
int index;
MSG messages[WIN32_INPUT_BUFFER_QUEUE_MAX_SIZE];
bool enabled;
Win32InputMsgBuffer();
bool postMessage(MSG msg);
};
class Win32Input : public PlatformInput
{
public :
virtual Keyboard* createKeyboard();
virtual Mouse* createMouse();
virtual void update();
static Win32InputMsgBuffer inputMsgBuffer;
};
#endif /* PLATFORM_WIN32_INPUT_H */

View file

@ -0,0 +1,20 @@
#include <string.h>
#include "Win32Internal.h"
const char* Win32GetMessage(DWORD messageId) {
static char buf[1024] = { '\0' };
DWORD rc = FormatMessage(
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, messageId,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, 1024, NULL);
if (!rc) {
strcpy_s(buf, "Unkown error!\n");
}
return buf;
}

View file

@ -0,0 +1,10 @@
#ifndef PLATFORM_WIN32_INTERNAL_H
#define PLATFORM_WIN32_INTERNAL_H
#include <Windows.h>
#include <string>
const char* Win32GetMessage(DWORD messageId);
#endif /* PLATFORM_WIN32_INTERNAL_H */

View file

@ -0,0 +1,119 @@
#include <Windows.h>
#include <Spectre/Input/InputEvent.h>
#include <Spectre/Input/InputModule.h>
#include "Win32Input.h"
#include "Win32Keyboard.h"
#include "Win32MsgBuffer.h"
static Win32MsgBuffer msg_buf;
static const Key::Type deviceToKey[256] = {
/* 0 1 2 3 4 5 6 7 */
/* 8 9 A B C D E F */
// 00-0F
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Backspace, Key::Tab, Key::Unknown, Key::Unknown, Key::Unknown, Key::Enter, Key::Unknown, Key::Unknown,
// 10-1F
Key::Unknown, Key::Unknown, Key::Unknown, Key::Pause, Key::Capslock, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Escape, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// 20-2F
Key::Space, Key::PageUp, Key::PageDown, Key::End, Key::Home, Key::Left, Key::Up, Key::Right,
Key::Down, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Insert, Key::Delete, Key::Unknown,
// 30-3F
Key::Zero, Key::One, Key::Two, Key::Three, Key::Four, Key::Five, Key::Six, Key::Seven,
Key::Eight, Key::Nine, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// 40-4F
Key::Unknown, Key::A, Key::B, Key::C, Key::D, Key::E, Key::F, Key::G,
Key::H, Key::I, Key::J, Key::K, Key::L, Key::M, Key::N, Key::O,
// 50-5F
Key::P, Key::Q, Key::R, Key::S, Key::T, Key::U, Key::V, Key::W,
Key::X, Key::Y, Key::Z, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// 60-6F
Key::NUMPAD_0, Key::NUMPAD_1, Key::NUMPAD_2, Key::NUMPAD_3, Key::NUMPAD_4, Key::NUMPAD_5, Key::NUMPAD_6, Key::NUMPAD_7,
Key::NUMPAD_8, Key::NUMPAD_9, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// 70-7F
Key::F1, Key::F2, Key::F3, Key::F4, Key::F5, Key::F6, Key::F7, Key::F8,
Key::F9, Key::F10, Key::F11, Key::F12, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// 80-8F
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// 90-9F
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// A0-AF
Key::LShift, Key::RShift, Key::LCtrl, Key::RCtrl, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// B0-BF
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Comma, Key::Unknown, Key::Period, Key::Unknown,
// C0-CF
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// E0-EF
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
// F0-FF
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown, Key::Unknown,
};
void Win32Keyboard::init()
{
Win32Input::inputMsgBuffer.enabled = true;
memset(m_btnState, 0, sizeof(m_btnState) / sizeof(m_btnState[0]));
}
bool Win32Keyboard::isKeyDown(Key::Type key)
{
return m_btnState[key];
}
void Win32Keyboard::update(InputModule *input)
{
for(int i = 0; i < msg_buf.index; i++) {
MSG msg = msg_buf.messages[i];
if (msg.message == WM_KILLFOCUS) {
for(int i = 0; i < Key::NUM_KEYS; i++) {
if (m_btnState[i]) {
InputEvent event(InputEvent::Key);
event.key.code = (Key::Type) i;
event.key.pressed = msg.message == WM_KEYDOWN;
m_btnState[i] = false;
input->postInputEvent(event);
}
}
continue;
}
if (msg.message != WM_KEYDOWN && msg.message != WM_KEYUP) {
continue;
}
Key::Type key = deviceToKey[msg.wParam % 0xFF];
if (key != Key::Unknown) {
InputEvent event(InputEvent::Key);
event.key.code = key;
event.key.pressed = msg.message == WM_KEYDOWN;
m_btnState[key] = event.key.pressed;
input->postInputEvent(event);
}
}
msg_buf.index = 0;
}
bool Win32Keyboard::handleMessage(MSG message)
{
return msg_buf.postMessage(message);
}

View file

@ -0,0 +1,28 @@
#ifndef PLATFORM_WIN32_KEYBOARD_H
#define PLATFORM_WIN32_KEYBOARD_H
#include <windows.h>
#include <Spectre/Input/Keyboard.h>
class Win32Keyboard : public Keyboard
{
public :
void init();
bool isKeyDown(Key::Type key);
static bool handleMessage(MSG message);
protected :
void update(InputModule *input);
protected :
bool m_btnState[Key::NUM_KEYS];
bool m_hasFocus;
};
#endif /* PLATFORM_WIN32_KEYBOARD_H */

View file

@ -0,0 +1,22 @@
#include <Windows.h>
#include <Platform/PlatformMisc.h>
void PlatformMisc::GetDisplayModes(std::vector<DisplayMode>& modes)
{
DEVMODE dev;
for(int i = 0; EnumDisplaySettings(NULL, i, &dev); i++) {
DisplayMode mode(dev.dmPelsWidth, dev.dmPelsHeight, dev.dmBitsPerPel);
modes.push_back(mode);
}
}
DisplayMode PlatformMisc::GetDesktopMode()
{
DEVMODE dev;
if (EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dev)) {
return DisplayMode(dev.dmPelsWidth, dev.dmPelsHeight, dev.dmBitsPerPel);
}
return DisplayMode();
}

View file

@ -0,0 +1,115 @@
#include <Windows.h>
#include <Spectre/System/Log.h>
#include <Spectre/Input/InputModule.h>
#include "Win32MsgBuffer.h"
#include "Win32Input.h"
#include "Win32Mouse.h"
static Win32MsgBuffer msg_buf;
static Vector2f _normalizePos(int x, int y, RECT area)
{
Vector2f out;
out.x = x / ((float) (area.right - area.left));
out.y = y / ((float) (area.bottom - area.top));
return out;
}
static RECT GetClientArea(HWND hwnd) {
RECT rect;
GetClientRect(hwnd, &rect);
return rect;
}
void Win32Mouse::init()
{
memset(m_state, 0, MouseButton::NUM_MBUTTONS);
m_tracked = false;
}
Vector2f Win32Mouse::getPosition() const
{
return m_position;
}
bool Win32Mouse::isButtonDown(MouseButton::Type button) const
{
return m_state[button];
}
void Win32Mouse::update(InputModule *input)
{
// TODO: Clean this.
for(int i = 0; i < msg_buf.index; i++) {
MSG msg = msg_buf.messages[i];
if (msg.message == WM_LBUTTONDOWN || msg.message == WM_LBUTTONUP) {
InputEvent event(InputEvent::MouseButton);
event.mouseButton.button = MouseButton::Left;
event.mouseButton.pressed = msg.message == WM_LBUTTONDOWN;
m_state[MouseButton::Left] = event.mouseButton.pressed;
input->postInputEvent(event);
} else if (msg.message == WM_RBUTTONDOWN || msg.message == WM_RBUTTONUP) {
InputEvent event(InputEvent::MouseButton);
event.mouseButton.button = MouseButton::Right;
event.mouseButton.pressed = msg.message == WM_RBUTTONDOWN;
m_state[MouseButton::Right] = event.mouseButton.pressed;
input->postInputEvent(event);
} else if (msg.message == WM_MBUTTONDOWN || msg.message == WM_MBUTTONUP) {
InputEvent event(InputEvent::MouseButton);
event.mouseButton.button = MouseButton::Middle;
event.mouseButton.pressed = msg.message == WM_MBUTTONDOWN;
m_state[MouseButton::Middle] = event.mouseButton.pressed;
input->postInputEvent(event);
} else if (msg.message == WM_XBUTTONDOWN || msg.message == WM_XBUTTONUP) {
int btn = GET_XBUTTON_WPARAM(msg.wParam);
InputEvent event(InputEvent::MouseButton);
event.mouseButton.button = btn == XBUTTON1 ? MouseButton::Button1 : MouseButton::Button2;
event.mouseButton.pressed = msg.message == WM_XBUTTONDOWN;
m_state[MouseButton::Button1] = event.mouseButton.pressed;
input->postInputEvent(event);
} else if (msg.message == WM_MOUSEMOVE) {
InputEvent event(InputEvent::MousePosition);
RECT area = GetClientArea(msg.hwnd);
int x = LOWORD(msg.lParam);
int y = HIWORD(msg.lParam);
// Do not forward the message if mouse is outside client area.
if ((x < area.left) || (x > area.right) || (y < area.top) || (y > area.bottom)) {
continue;
}
event.mouse.x = x;
event.mouse.y = y;
input->postInputEvent(event);
m_position = _normalizePos(x, y, area);
}
}
msg_buf.index = 0;
}
bool Win32Mouse::handleMessage(MSG message)
{
return msg_buf.postMessage(message);
}

View file

@ -0,0 +1,34 @@
#ifndef PLATFORM_WIN32_MOUSE_H
#define PLATFORM_WIN32_MOUSE_H
#include <Windows.h>
#include <Spectre/Input/Mouse.h>
class Win32Mouse : public Mouse
{
public :
~Win32Mouse() {}
virtual void init();
// Get mouse position
virtual Vector2f getPosition() const;
virtual bool isButtonDown(MouseButton::Type button) const;
static bool handleMessage(MSG message);
protected :
virtual void update(InputModule *input);
protected :
Vector2f m_position;
bool m_state[MouseButton::NUM_MBUTTONS];
bool m_tracked;
};
#endif /* PLATFORM_WIN32_MOUSE_H */

View file

@ -0,0 +1,18 @@
#include <Spectre/System/Log.h>
#include "Win32MsgBuffer.h"
Win32MsgBuffer::Win32MsgBuffer() :
index (0)
{
}
bool Win32MsgBuffer::postMessage(MSG msg)
{
if (index < WIN32_MSG_BUFFER_MAX_SIZE) {
messages[index++] = msg;
return true;
}
log("Win32MsgBuffer: Queue overflow\n");
return false;
}

View file

@ -0,0 +1,18 @@
#ifndef PLATFORM_WIN32_MSG_BUFFER_H
#define PLATFORM_WIN32_MSG_BUFFER_H
#include <Windows.h>
#define WIN32_MSG_BUFFER_MAX_SIZE 64
struct Win32MsgBuffer {
int index;
MSG messages[WIN32_MSG_BUFFER_MAX_SIZE];
Win32MsgBuffer();
bool postMessage(MSG msg);
};
#endif /* PLATFORM_WIN32_MSG_BUFFER_H */

View file

@ -0,0 +1,28 @@
#include <Windows.h>
#include <Spectre/System/System.h>
static LARGE_INTEGER getFrequency() {
LARGE_INTEGER freq;
::QueryPerformanceFrequency(&freq);
return freq;
}
unsigned long System::getMilliseconds()
{
static LARGE_INTEGER freq = getFrequency();
LARGE_INTEGER cnt;
::QueryPerformanceCounter(&cnt);
cnt.QuadPart *= 1000;
cnt.QuadPart /= freq.QuadPart;
return (unsigned long) cnt.QuadPart;
}
void System::sleep(int milliseconds)
{
::Sleep(milliseconds);
}