Add she::EventQueue to she::Display to get events from she library

This commit is contained in:
David Capello 2014-03-20 00:01:00 -03:00
parent aa27c0d68a
commit be6b98995e
8 changed files with 152 additions and 24 deletions

View File

@ -9,8 +9,9 @@
namespace she {
class Surface;
class EventQueue;
class NotDisposableSurface;
class Surface;
// A display or window to show graphics.
class Display {
@ -43,6 +44,8 @@ namespace she {
virtual void maximize() = 0;
virtual bool isMaximized() const = 0;
virtual EventQueue* getEventQueue() = 0;
// Returns the HWND on Windows.
virtual void* nativeHandle() = 0;
};

29
src/she/event.h Normal file
View File

@ -0,0 +1,29 @@
// SHE library
// Copyright (C) 2012-2014 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef SHE_EVENT_H_INCLUDED
#define SHE_EVENT_H_INCLUDED
namespace she {
class Event {
public:
enum Type {
None,
};
Event() : m_type(None) { }
int type() const { return m_type; }
void setType(Type type) { m_type = type; }
private:
Type m_type;
};
} // namespace she
#endif

View File

@ -4,15 +4,18 @@
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef SHE_EVENT_LOOP_H_INCLUDED
#define SHE_EVENT_LOOP_H_INCLUDED
#ifndef SHE_EVENT_QUEUE_H_INCLUDED
#define SHE_EVENT_QUEUE_H_INCLUDED
namespace she {
class EventLoop {
class Event;
class EventQueue {
public:
virtual ~EventLoop() { }
virtual ~EventQueue() { }
virtual void dispose() = 0;
virtual void getEvent(Event& ev) = 0;
};
} // namespace she

View File

@ -8,7 +8,8 @@
#define SHE_H_INCLUDED
#include "she/display.h"
#include "she/event_loop.h"
#include "she/event.h"
#include "she/event_queue.h"
#include "she/locked_surface.h"
#include "she/scoped_handle.h"
#include "she/scoped_surface_lock.h"

View File

@ -12,12 +12,21 @@
#include <allegro.h>
#include <allegro/internal/aintern.h>
#ifdef ALLEGRO_WINDOWS
#ifdef WIN32
#include <winalleg.h>
#if defined STRICT || defined __GNUC__
typedef WNDPROC wndproc_t;
#else
typedef FARPROC wndproc_t;
#endif
#endif
#include "loadpng.h"
#include <cassert>
#include <vector>
#define DISPLAY_FLAG_FULL_REFRESH 1
#define DISPLAY_FLAG_WINDOW_RESIZE 2
@ -125,6 +134,60 @@ private:
DestroyFlag m_destroy;
};
class Alleg4EventQueue : public EventQueue {
public:
Alleg4EventQueue() {
}
void dispose() {
delete this;
}
void getEvent(Event& event) {
if (m_events.size() > 0) {
event = m_events[0];
m_events.erase(m_events.begin());
}
else
event.setType(Event::None);
}
void queueEvent(const Event& event) {
m_events.push_back(event);
}
private:
std::vector<Event> m_events;
};
#if WIN32
namespace {
Display* g_display = NULL;
wndproc_t base_wndproc = NULL;
static LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
// TODO
// switch (msg) {
// }
return ::CallWindowProc(base_wndproc, hwnd, msg, wparam, lparam);
}
void subclass_hwnd(HWND hwnd)
{
base_wndproc = (wndproc_t)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)wndproc);
}
void unsubclass_hwnd(HWND hwnd)
{
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)base_wndproc);
base_wndproc = NULL;
}
}
#endif
class Alleg4Display : public Display {
public:
Alleg4Display(int width, int height, int scale)
@ -150,6 +213,8 @@ public:
setScale(scale);
m_queue = new Alleg4EventQueue();
// Add a hook to display-switch so when the user returns to the
// screen it's completelly refreshed/redrawn.
LOCK_VARIABLE(display_flags);
@ -160,9 +225,19 @@ public:
// Setup the handler for window-resize events
set_resize_callback(resize_callback);
#endif
#if WIN32
subclass_hwnd((HWND)nativeHandle());
#endif WIN32
}
~Alleg4Display() {
delete m_queue;
#if WIN32
unsubclass_hwnd((HWND)nativeHandle());
#endif WIN32
m_surface->dispose();
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
}
@ -246,6 +321,10 @@ public:
#endif
}
EventQueue* getEventQueue() {
return m_queue;
}
void* nativeHandle() {
#ifdef WIN32
return reinterpret_cast<void*>(win_get_window());
@ -257,16 +336,7 @@ public:
private:
Surface* m_surface;
int m_scale;
};
class Alleg4EventLoop : public EventLoop {
public:
Alleg4EventLoop() {
}
void dispose() {
delete this;
}
Alleg4EventQueue* m_queue;
};
class Alleg4System : public System {
@ -307,10 +377,6 @@ public:
Alleg4Surface::AutoDestroy);
}
EventLoop* createEventLoop() {
return new Alleg4EventLoop();
}
};
static System* g_instance;

View File

@ -30,7 +30,6 @@ namespace she {
virtual Display* createDisplay(int width, int height, int scale) = 0;
virtual Surface* createSurface(int width, int height) = 0;
virtual Surface* createSurfaceFromNativeHandle(void* nativeHandle) = 0;
virtual EventLoop* createEventLoop() = 0;
};
System* CreateSystem();

View File

@ -13,6 +13,9 @@
#include "ui/manager.h"
#include "she/display.h"
#include "she/event.h"
#include "she/event_queue.h"
#include "ui/intern.h"
#include "ui/ui.h"
@ -106,6 +109,8 @@ static void allegro_window_close_hook()
Manager::Manager()
: Widget(kManagerWidget)
, m_display(NULL)
, m_eventQueue(NULL)
{
if (!m_defaultManager) {
// Hook the window close message
@ -173,6 +178,12 @@ Manager::~Manager()
}
}
void Manager::setDisplay(she::Display* display)
{
m_display = display;
m_eventQueue = m_display->getEventQueue();
}
void Manager::run()
{
MessageLoop loop(this);
@ -442,6 +453,18 @@ bool Manager::generateMessages()
}
}
// Events from "she" layer.
she::Event sheEvent;
for (;;) {
m_eventQueue->getEvent(sheEvent);
if (sheEvent.type() == she::Event::None)
break;
// TODO
// switch (sheEvent.type()) {
// }
}
// Generate messages for timers
Timer::pollTimers();

View File

@ -12,7 +12,10 @@
#include "ui/mouse_buttons.h"
#include "ui/widget.h"
namespace she { class Display; }
namespace she {
class Display;
class EventQueue;
}
namespace ui {
@ -31,7 +34,7 @@ namespace ui {
~Manager();
she::Display* getDisplay() { return m_display; }
void setDisplay(she::Display* display) { m_display = display; }
void setDisplay(she::Display* display);
void run();
@ -99,6 +102,7 @@ namespace ui {
WidgetsList m_garbage;
she::Display* m_display;
she::EventQueue* m_eventQueue;
};
} // namespace ui