Browse Source

Merge branch 'master' of https://github.com/taylor001/crown

Conflicts:
	engine/CMakeLists.txt
	engine/input/Mouse.cpp
mikymod 12 years ago
parent
commit
ccd48e85d8

+ 0 - 13
engine/CMakeLists.txt

@@ -66,9 +66,7 @@ set (CROWN_INCLUDES
 set (SRC
 	Camera.cpp
 	Device.cpp
-	EventBuffer.cpp
 	ConsoleServer.cpp
-	FPSSystem.cpp
 )
 
 set (HEADERS
@@ -78,7 +76,6 @@ set (HEADERS
 	Device.h
 	EventBuffer.h
 	ConsoleServer.h
-	FPSSystem.h
 )
 
 set (CORE_SRC
@@ -229,17 +226,9 @@ set (SETTINGS_HEADERS
 )
 
 set (INPUT_SRC
-	input/EventDispatcher.cpp
-	input/InputManager.cpp
-	input/Keyboard.cpp
-	input/Mouse.cpp
-	input/Touch.cpp
-	input/Accelerometer.cpp
 )
 
 set (INPUT_HEADERS
-	input/EventDispatcher.h
-	input/InputManager.h
 	input/Keyboard.h
 	input/KeyCode.h
 	input/Mouse.h
@@ -298,7 +287,6 @@ set (RESOURCE_HEADERS
 )
 
 set (OS_SRC
-	os/OS.cpp
 )
 
 set (OS_HEADERS
@@ -532,7 +520,6 @@ set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:$ORIGIN")
 add_executable(${CROWN_EXECUTABLE_NAME} ${CROWN_MAIN_SRC})
 target_link_libraries(${CROWN_EXECUTABLE_NAME} crown)
 
-
 if (CROWN_BUILD_TESTS)
 	#add_subdirectory(tests)
 endif (CROWN_BUILD_TESTS)

+ 0 - 2
engine/Crown.h

@@ -104,8 +104,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Device.h"
 
 // Engine/Input
-#include "EventDispatcher.h"
-#include "InputManager.h"
 #include "Keyboard.h"
 #include "KeyCode.h"
 #include "Mouse.h"

+ 0 - 94
engine/EventBuffer.cpp

@@ -1,94 +0,0 @@
-#include "EventBuffer.h"
-#include "Log.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-EventBuffer::EventBuffer() : m_size(0), m_read(0)
-{
-}
-
-//-----------------------------------------------------------------------------
-void EventBuffer::push_event(uint32_t event_type, void* event_data, size_t event_size)
-{
-	if (m_size + sizeof(event_type) + sizeof(event_size) + event_size > MAX_OS_EVENT_BUFFER_SIZE)
-	{
-		flush();
-	}
-
-	char* cur = m_buffer + m_size;
-
-	*(uint32_t*) cur = event_type;
-	*(size_t*) (cur + sizeof(event_type)) = event_size;
-	memcpy(cur + sizeof(event_type) + sizeof(event_size), event_data, event_size);
-
-	m_size += sizeof(event_type) + sizeof(event_size) + event_size;
-}
-
-//-----------------------------------------------------------------------------
-void EventBuffer::push_event_buffer(char* event_buffer, size_t buffer_size)
-{
-	if (m_size + buffer_size > MAX_OS_EVENT_BUFFER_SIZE)
-	{
-		flush();
-	}
-
-	char* cur = m_buffer + m_size;
-
-	memcpy(cur, event_buffer, buffer_size);
-
-	m_size += buffer_size;
-}
-
-
-//-----------------------------------------------------------------------------
-void* EventBuffer::get_next_event(uint32_t& event_type, size_t& event_size)
-{
-	if (m_read < m_size)
-	{
-		char* cur = m_buffer + m_read;
-
-		// Saves type
-		event_type = *(uint32_t*) cur;
-		// Saves size
-		event_size = *(size_t*)(cur + sizeof(uint32_t));
-
-		// Set read to next event
-		m_read += sizeof(size_t) + sizeof(uint32_t) + event_size;
-
-		return cur + sizeof(size_t) + sizeof(uint32_t);
-	}
-
-	m_read = 0;
-
-	return NULL;
-}
-
-//-----------------------------------------------------------------------------
-void EventBuffer::clear()
-{
-	m_size = 0;
-	m_read = 0;
-}
-
-//-----------------------------------------------------------------------------
-void EventBuffer::flush()
-{
-	m_size = 0;
-	m_read = 0;
-}
-
-//-----------------------------------------------------------------------------
-size_t EventBuffer::size() const
-{
-	return m_size;
-}
-
-//-----------------------------------------------------------------------------
-char* EventBuffer::buffer()
-{
-	return m_buffer;
-}
-
-} // namespace crown

+ 0 - 75
engine/EventBuffer.h

@@ -1,75 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include <cstring>
-
-#include "Types.h"
-
-#define MAX_OS_EVENT_BUFFER_SIZE 1024
-
-namespace crown
-{
-
-
-///	__EventBuffer__ is a global buffer used for storing events.
-///	Each subsystem can read its relative events and modifies its behaviour consequently.
-///
-/// [type #0][size #0][data #0] ... [type #n][size #n][data #n]
-class EventBuffer
-{
-
-public:
-	/// Constructor
-				EventBuffer();
-
-	/// Pushes an @a event_data of size @a event_size with type @a event_type 
-	void		push_event(uint32_t event_type, void* event_data, size_t event_size);
-	/// Pushes an entire @a event_buffer of size @a buffer_size
-	void		push_event_buffer(char* event_buffer, size_t buffer_size);
-	/// Retrieves the @a event_type and @a event_size of next os event
-	void*		get_next_event(uint32_t& event_type, size_t& event_size);
-
-	/// Clears entire os buffer
-	void		clear();
-	/// Flushes entire os buffer
-	void		flush();
-
-	/// Returns buffer's size
-	size_t		size() const;
-	/// Return buffer
-	char*		buffer();
-
-public:
-
-	size_t		m_size;
-	char		m_buffer[MAX_OS_EVENT_BUFFER_SIZE];
-
-	uint32_t	m_read;
-};
-
-} // namespace crown

+ 51 - 6
engine/os/OS.cpp → engine/EventQueue.h

@@ -24,16 +24,61 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "OS.h"
+#pragma once
+
+#include <cstring>
+#include <stdio.h>
+#include "Mutex.h"
+#include "Queue.h"
+#include "Log.h"
 
 namespace crown
 {
 
-EventBuffer g_os_event_buffer;
-
-EventBuffer* os_event_buffer()
+/// Represents an abstract queue of events
+class EventQueue
 {
-	return &g_os_event_buffer;
-}
+public:
+
+	//-----------------------------------------------------------------------------
+	EventQueue(Allocator& a)
+		: m_queue(a)
+	{
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_event(void* data)
+	{
+		m_mutex.lock();
+		m_queue.push_back(data);
+		m_mutex.unlock();
+	}
+
+	//-----------------------------------------------------------------------------
+	void* get_event()
+	{
+		m_mutex.lock();
+
+		void* data;
+		if (m_queue.size() > 0)
+		{
+			data = m_queue.front();
+			m_queue.pop_front();
+		}
+		else
+		{
+			data = NULL;
+		}
+
+		m_mutex.unlock();
+
+		return data;
+	}
+
+public:
+
+	Mutex m_mutex;
+	Queue<void*> m_queue;
+};
 
 } // namespace crown

+ 0 - 204
engine/FPSSystem.cpp

@@ -1,204 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "InputManager.h"
-#include "Camera.h"
-#include "FPSSystem.h"
-#include "Vec2.h"
-#include "Vec3.h"
-#include "Mat3.h"
-#include "OS.h"
-#include "Device.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------
-FPSSystem::FPSSystem(Camera* camera, float speed, float sensibility) :
-	m_camera(camera),
-	m_camera_speed(speed),
-	m_camera_sensibility(sensibility),
-
-	m_angle_x(0),
-	m_angle_y(0),
-
-	m_up_pressed(false),
-	m_right_pressed(false),
-	m_down_pressed(false),
-	m_left_pressed(false)
-{
-	device()->input_manager()->register_keyboard_listener(this);
-	device()->input_manager()->register_accelerometer_listener(this);
-	//device()->input_manager()->register_mouse_listener(this);
-	device()->mouse()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
-	
-}
-
-//-----------------------------------------------------------------------
-void FPSSystem::key_pressed(const KeyboardEvent& event)
-{
-	switch (event.key)
-	{
-		case 'w':
-		case 'W':
-		{
-			m_up_pressed = true;
-			break;
-		}
-		case 'a':
-		case 'A':
-		{
-			m_left_pressed = true;
-			break;
-		}
-		case 's':
-		case 'S':
-		{
-			m_down_pressed = true;
-			break;
-		}
-		case 'd':
-		case 'D':
-		{
-			m_right_pressed = true;
-			break;
-		}
-		default:
-		{
-			break;
-		}
-	}
-}
-
-//-----------------------------------------------------------------------
-void FPSSystem::key_released(const KeyboardEvent& event)
-{
-	switch (event.key)
-	{
-		case 'w':
-		case 'W':
-		{
-			m_up_pressed = false;
-			break;
-		}
-		case 'a':
-		case 'A':
-		{
-			m_left_pressed = false;
-			break;
-		}
-		case 's':
-		case 'S':
-		{
-			m_down_pressed = false;
-			break;
-		}
-		case 'd':
-		case 'D':
-		{
-			m_right_pressed = false;
-			break;
-		}
-		default:
-		{
-			break;
-		}
-	}
-}
-
-//-----------------------------------------------------------------------
-void FPSSystem::accelerometer_changed(const AccelerometerEvent& event)
-{
-	(void)event;
-	set_view_by_cursor();
-}
-//-----------------------------------------------------------------------
-void FPSSystem::set_camera(Camera* camera)
-{
-	m_camera = camera;
-}
-
-//-----------------------------------------------------------------------
-Camera* FPSSystem::camera()
-{
-	return m_camera;
-}
-
-//-----------------------------------------------------------------------
-void FPSSystem::update(float dt)
-{
-	if (m_up_pressed)
-	{
-		m_camera->move_forward(m_camera_speed * dt);
-	}
-
-	if (m_left_pressed)
-	{
-		m_camera->strafe_left(m_camera_speed * dt);
-	}		
-
-	if (m_down_pressed)
-	{
-		m_camera->move_backward(m_camera_speed * dt);
-	}
-
-	if (m_right_pressed)
-	{
-		m_camera->strafe_right(m_camera_speed * dt);
-	}
-
-	// device()->renderer()->set_matrix(MT_VIEW, m_camera->view_matrix());
-	// device()->renderer()->set_matrix(MT_PROJECTION, m_camera->projection_matrix());
-}
-
-//-----------------------------------------------------------------------	
-void FPSSystem::set_view_by_cursor()
-{
-	static Vec2 lastPos = device()->mouse()->cursor_relative_xy();
-	Vec2 currentPos = device()->mouse()->cursor_relative_xy();
-	device()->mouse()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
-
-	if (lastPos == currentPos)
-	{
-		return;
-	}
-
-	Vec2 delta = lastPos - currentPos;
-
-	device()->mouse()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
-	lastPos = device()->mouse()->cursor_relative_xy();
-
-	m_angle_x += delta.y * m_camera_sensibility;
-	m_angle_y += delta.x * m_camera_sensibility;
-
-	m_angle_x = math::clamp_to_range(-89.999f * math::DEG_TO_RAD, 89.999f * math::DEG_TO_RAD, m_angle_x);
-	m_angle_y = math::fmod(m_angle_y, math::TWO_PI);
-
-	m_camera->set_rotation(m_angle_x, m_angle_y);
-}
-
-} // namespace crown
-

+ 0 - 74
engine/FPSSystem.h

@@ -1,74 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-#include "Camera.h"
-#include "Vec2.h"
-#include "Mouse.h"
-#include "Keyboard.h"
-#include "Touch.h"
-#include "Accelerometer.h"
-#include "Renderer.h"
-
-namespace crown
-{
-/// TODO: set_view_by_cursor must be implemented through scripting
-class FPSSystem : public MouseListener, public KeyboardListener, public AccelerometerListener
-{
-public:
-
-					/// Constructor
-					FPSSystem(Camera* camera, float speed, float sensibility);
-
-	void 			set_camera(Camera* camera);
-	Camera*			camera();
-
-	void			update(float dt);
-	void			set_view_by_cursor();	
-
-	virtual void 	key_pressed(const KeyboardEvent& event);
-	virtual void 	key_released(const KeyboardEvent& event);
-	virtual void 	accelerometer_changed(const AccelerometerEvent& event);
-
-private:
-
-	Camera*			m_camera;
-
-	float			m_camera_speed;
-	float			m_camera_sensibility;
-
-	float 			m_angle_x;
-	float 			m_angle_y;
-
-	bool			m_up_pressed	: 1;
-	bool			m_right_pressed	: 1;
-	bool			m_down_pressed	: 1;
-	bool			m_left_pressed	: 1;
-};
-
-} // namespace crown

+ 1 - 0
engine/core/Assert.h

@@ -41,3 +41,4 @@ OTHER DEALINGS IN THE SOFTWARE.
 #endif
 
 #define CE_ASSERT_NOT_NULL(x) CE_ASSERT(x != NULL, "Parameter must be not null")
+#define CE_FATAL(msg) CE_ASSERT(false, msg)

+ 47 - 4
engine/core/containers/Queue.h

@@ -75,18 +75,24 @@ public:
 	/// time of call.
 	void			grow(uint32_t min_capacity);
 
-	/// Adds an @a item to the back of the queue
+	/// Appends an @a item to the back of the queue
 	void			push_back(const T& item);
 
 	/// Removes the last item from the queue
 	void			pop_back();
 
-	/// Adds an @a item to the front of the queue
+	/// Appends an @a item to the front of the queue
 	void			push_front(const T& item);
 
 	/// Removes the first item from the queue
 	void			pop_front();
-	
+
+	/// Appends @a n @a items to the back of the queue
+	void			push(const T *items, uint32_t n);
+
+	/// Removes @a n items from the front of the queue
+	void			pop(uint32_t n);
+
 	/// Clears the content of the queue.
 	/// @note
 	/// Does not free memory nor call destructors, it only zeroes
@@ -236,10 +242,47 @@ inline void Queue<T>::pop_front()
 	CE_ASSERT(m_size > 0, "The queue is empty");
 
 	m_read = (m_read + 1) % m_queue.size();
-
 	m_size--;
 }
 
+//-----------------------------------------------------------------------------
+template <typename T>
+inline void Queue<T>::push(const T *items, uint32_t n)
+{
+	if (space() < n)
+	{
+		grow(size() + n);		
+	}
+
+	const uint32_t size = m_queue.size();
+	const uint32_t insert = (m_read + m_size) % size;
+
+	uint32_t to_insert = n;
+	if (insert + to_insert > size)
+	{
+		to_insert = size - insert;
+	}
+
+	memcpy(m_queue.begin() + insert, items, to_insert * sizeof(T));
+
+	m_size += to_insert;
+	items += to_insert;
+	n -= to_insert;
+	memcpy(m_queue.begin(), items, n * sizeof(T));
+
+	m_size += n;
+}
+
+//-----------------------------------------------------------------------------
+template <typename T>
+inline void Queue<T>::pop(uint32_t n)
+{
+	CE_ASSERT(m_size > 0, "The queue is empty");
+
+	m_read = (m_read + n) % m_queue.size();
+	m_size -= n;
+}
+
 //-----------------------------------------------------------------------------
 template <typename T>
 inline void Queue<T>::clear()

+ 0 - 44
engine/input/Accelerometer.cpp

@@ -1,44 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "Accelerometer.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-Accelerometer::Accelerometer() :
-	m_orientation(0.0f, 0.0f, 0.0f)
-{
-}
-
-//-----------------------------------------------------------------------------
-const Vec3& Accelerometer::orientation() const
-{
-	return m_orientation;
-}
-
-} // namespace crown

+ 12 - 23
engine/input/Accelerometer.h

@@ -32,38 +32,27 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-class InputManager;
-
-struct AccelerometerEvent
-{
-	float x;
-	float y;
-	float z;
-};
-
-class AccelerometerListener
-{
-public:
-
-	virtual void accelerometer_changed(const AccelerometerEvent& event) { (void)event; }
-};
-
 /// Interface for accessing accelerometer input device.
-class Accelerometer
+struct Accelerometer
 {
-public:
-	
-					Accelerometer();
+	//-----------------------------------------------------------------------------
+	Accelerometer()
+		: m_orientation(0.0f, 0.0f, 0.0f)
+	{
+	}
 
 	/// Returns the orientation of the accelerometer.
 	/// FIXME NEED MORE DOCUMENTATION
-	const Vec3&		orientation() const;
+	const Vec3& orientation() const
+	{
+		return m_orientation;
+	}
 
-private:
+public:
 
 	Vec3			m_orientation;
 
-	friend class	InputManager;
+	friend class	Device;
 };
 
 } // namespace crown

+ 0 - 178
engine/input/EventDispatcher.cpp

@@ -1,178 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "EventDispatcher.h"
-#include "Allocator.h"
-#include "Log.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-EventDispatcher::EventDispatcher() :
-	m_mouse_listener_list(default_allocator()),
-	m_keyboard_listener_list(default_allocator()),
-	m_touch_listener_list(default_allocator()),
-	m_acc_listener_list(default_allocator())
-{
-}
-
-//-----------------------------------------------------------------------------
-EventDispatcher::~EventDispatcher()
-{
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::add_mouse_listener(MouseListener* listener)
-{
-	CE_ASSERT(listener != NULL, "Listener must be != NULL");
-
-	m_mouse_listener_list.push_back(listener);
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::add_keyboard_listener(KeyboardListener* listener)
-{
-	CE_ASSERT(listener != NULL, "Listener must be != NULL");
-
-	m_keyboard_listener_list.push_back(listener);
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::add_touch_listener(TouchListener* listener)
-{
-	CE_ASSERT(listener != NULL, "Listener must be != NULL");
-
-	m_touch_listener_list.push_back(listener);
-}
-
-void EventDispatcher::add_accelerometer_listener(AccelerometerListener* listener)
-{
-	CE_ASSERT(listener != NULL, "Listener must be != NULL");
-	m_acc_listener_list.push_back(listener);
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::button_pressed(const MouseEvent& event)
-{
-	for (uint32_t i = 0; i < m_mouse_listener_list.size(); i++)
-	{
-		m_mouse_listener_list[i]->button_pressed(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::button_released(const MouseEvent& event)
-{
-	for (uint32_t i = 0; i < m_mouse_listener_list.size(); i++)
-	{
-		m_mouse_listener_list[i]->button_released(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::cursor_moved(const MouseEvent& event)
-{
-	for (uint32_t i = 0; i < m_mouse_listener_list.size(); i++)
-	{
-		m_mouse_listener_list[i]->cursor_moved(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::key_pressed(const KeyboardEvent& event)
-{
-	for (uint32_t i = 0; i < m_keyboard_listener_list.size(); i++)
-	{
-		m_keyboard_listener_list[i]->key_pressed(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::key_released(const KeyboardEvent& event)
-{
-	for (uint32_t i = 0; i < m_keyboard_listener_list.size(); i++)
-	{
-		m_keyboard_listener_list[i]->key_released(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::text_input(const KeyboardEvent& event)
-{
-	for (uint32_t i = 0; i < m_keyboard_listener_list.size(); i++)
-	{
-		m_keyboard_listener_list[i]->text_input(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::touch_down(const TouchEvent& event)
-{
-	for (uint32_t i = 0; i < m_touch_listener_list.size(); i++)
-	{
-		m_touch_listener_list[i]->touch_down(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::touch_up(const TouchEvent& event)
-{
-	for (uint32_t i = 0; i < m_touch_listener_list.size(); i++)
-	{
-		m_touch_listener_list[i]->touch_up(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::touch_move(const TouchEvent& event)
-{
-	for (uint32_t i = 0; i < m_touch_listener_list.size(); i++)
-	{
-		m_touch_listener_list[i]->touch_move(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::touch_cancel(const TouchEvent& event)
-{
-	for (uint32_t i = 0; i < m_touch_listener_list.size(); i++)
-	{
-		m_touch_listener_list[i]->touch_cancel(event);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void EventDispatcher::accelerometer_changed(const AccelerometerEvent& event)
-{
-	for (uint32_t i = 0; i < m_acc_listener_list.size(); i++)
-	{
-		m_acc_listener_list[i]->accelerometer_changed(event);
-	}
-}
-
-} // namespace crown
-

+ 0 - 79
engine/input/EventDispatcher.h

@@ -1,79 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "List.h"
-#include "Mouse.h"
-#include "Keyboard.h"
-#include "Touch.h"
-#include "Accelerometer.h"
-
-namespace crown
-{
-
-class EventDispatcher
-{
-	typedef List<MouseListener*>			MouseListenerList;
-	typedef List<KeyboardListener*>			KeyboardListenerList;
-	typedef List<TouchListener*>			TouchListenerList;
-	typedef List<AccelerometerListener*>	AccelerometerListenerList;
-
-public:
-
-	EventDispatcher();
-	~EventDispatcher();
-
-	void add_mouse_listener(MouseListener* listener);
-	void add_keyboard_listener(KeyboardListener* listener);
-	void add_touch_listener(TouchListener* listener);
-	void add_accelerometer_listener(AccelerometerListener* listener);
-
-	void button_pressed(const MouseEvent&);
-	void button_released(const MouseEvent&);
-	void cursor_moved(const MouseEvent&);
-
-	void key_pressed(const KeyboardEvent&);
-	void key_released(const KeyboardEvent&);
-	void text_input(const KeyboardEvent&);
-
-	void touch_down(const TouchEvent& event);
-	void touch_up(const TouchEvent& event);
-	void touch_move(const TouchEvent& event);
-	void touch_cancel(const TouchEvent& event);
-
-	void accelerometer_changed(const AccelerometerEvent& event);
-
-private:
-
-	MouseListenerList			m_mouse_listener_list;
-	KeyboardListenerList		m_keyboard_listener_list;
-	TouchListenerList			m_touch_listener_list;
-	AccelerometerListenerList	m_acc_listener_list;
-};
-
-} // namespace crown
-

+ 0 - 237
engine/input/InputManager.cpp

@@ -1,237 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "InputManager.h"
-#include "OS.h"
-#include "Log.h"
-#include "EventBuffer.h"
-#include "OsEvents.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-InputManager::InputManager()
-{
-}
-
-//-----------------------------------------------------------------------------
-InputManager::~InputManager()
-{
-}
-
-//-----------------------------------------------------------------------------
-Keyboard* InputManager::keyboard()
-{
-	return &m_keyboard;
-}
-
-//-----------------------------------------------------------------------------
-Mouse* InputManager::mouse()
-{
-	return &m_mouse;
-}
-
-//-----------------------------------------------------------------------------
-Touch* InputManager::touch()
-{
-	return &m_touch;
-}
-
-//-----------------------------------------------------------------------------
-Accelerometer* InputManager::accelerometer()
-{
-	return &m_accelerometer;
-}
-
-//-----------------------------------------------------------------------------
-void InputManager::register_mouse_listener(MouseListener* listener)
-{
-	m_event_dispatcher.add_mouse_listener(listener);
-}
-
-//-----------------------------------------------------------------------------
-void InputManager::register_keyboard_listener(KeyboardListener* listener)
-{
-	m_event_dispatcher.add_keyboard_listener(listener);
-}
-
-//-----------------------------------------------------------------------------
-void InputManager::register_touch_listener(TouchListener* listener)
-{
-	m_event_dispatcher.add_touch_listener(listener);
-}
-
-//-----------------------------------------------------------------------------
-void InputManager::register_accelerometer_listener(AccelerometerListener* listener)
-{
-	m_event_dispatcher.add_accelerometer_listener(listener);
-}
-
-//-----------------------------------------------------------------------------
-EventDispatcher* InputManager::get_event_dispatcher()
-{
-	return &m_event_dispatcher;
-}
-
-//-----------------------------------------------------------------------------
-void InputManager::frame(uint64_t frame_count)
-{
-	void* event;
-	uint32_t event_type;
-	size_t event_size;
-
-	// Update input devices
-	m_keyboard.m_current_frame = frame_count;
-	m_mouse.m_current_frame = frame_count;
-
-	while (1)
-	{
-		if ((event = os_event_buffer()->get_next_event(event_type, event_size)) == NULL)
-		{
-			break;
-		}
-
-		switch (event_type)
-		{
-			case OSET_NONE:
-			{
-				return;
-			}
-			case OSET_BUTTON_PRESS:
-			case OSET_BUTTON_RELEASE:
-			{
-				MouseEvent mouse_event;
-				mouse_event.x = ((OsMouseEvent*)event)->x;
-				mouse_event.y = ((OsMouseEvent*)event)->y;
-				mouse_event.button = ((OsMouseEvent*)event)->button == 0 ? MB_LEFT : ((OsMouseEvent*)event)->button == 1 ? MB_MIDDLE : MB_RIGHT;
-				mouse_event.wheel = 0.0f;
-
-				if (event_type == OSET_BUTTON_PRESS)
-				{
-					m_mouse.update(frame_count, mouse_event.button, true);
-					m_event_dispatcher.button_pressed(mouse_event);
-				}
-				else
-				{
-					m_mouse.update(frame_count, mouse_event.button, false);
-					m_event_dispatcher.button_released(mouse_event);
-				}
-
-				break;
-			}
-			case OSET_KEY_PRESS:
-			case OSET_KEY_RELEASE:
-			{
-				KeyboardEvent keyboard_event;
-				keyboard_event.key = (KeyCode) ((OsKeyboardEvent*)event)->key;
-				keyboard_event.modifier = (uint8_t) ((OsKeyboardEvent*)event)->modifier;
-
-				m_keyboard.m_modifier = keyboard_event.modifier;
-
-				if (event_type == OSET_KEY_PRESS)
-				{
-					m_keyboard.update(frame_count, keyboard_event.key, true);
-					m_event_dispatcher.key_pressed(keyboard_event);
-				}
-				else
-				{
-					m_keyboard.update(frame_count, keyboard_event.key, false);
-					m_event_dispatcher.key_released(keyboard_event);
-				}
-
-				break;
-			}
-			case OSET_TOUCH_DOWN:
-			case OSET_TOUCH_UP:
-			{
-				TouchEvent touch_event;
-				touch_event.pointer_id = ((OsTouchEvent*)event)->pointer_id;
-				touch_event.x = ((OsTouchEvent*)event)->x;
-				touch_event.y = ((OsTouchEvent*)event)->y;
-
-				m_touch.m_pointers[touch_event.pointer_id].x = touch_event.x;
-				m_touch.m_pointers[touch_event.pointer_id].y = touch_event.y;
-
-				// FIXME
-				m_touch.m_pointers[touch_event.pointer_id].relative_x = 0.0f;
-				m_touch.m_pointers[touch_event.pointer_id].relative_y = 0.0f;
-
-				if (event_type == OSET_TOUCH_DOWN)
-				{
-					m_touch.m_pointers[touch_event.pointer_id].up = false;
-					m_event_dispatcher.touch_down(touch_event);
-				}
-				else
-				{
-					m_touch.m_pointers[touch_event.pointer_id].up = true;
-					m_event_dispatcher.touch_up(touch_event);
-				}
-
-				break;
-			}
-			case OSET_TOUCH_MOVE:
-			{
-				TouchEvent touch_event;
-				touch_event.pointer_id = ((OsTouchEvent*)event)->pointer_id;
-				touch_event.x = ((OsTouchEvent*)event)->x;
-				touch_event.y = ((OsTouchEvent*)event)->y;
-
-				m_touch.m_pointers[touch_event.pointer_id].x = touch_event.x;
-				m_touch.m_pointers[touch_event.pointer_id].y = touch_event.y;
-
-				// FIXME
-				m_touch.m_pointers[touch_event.pointer_id].relative_x = 0.0f;
-				m_touch.m_pointers[touch_event.pointer_id].relative_y = 0.0f;
-
-				m_event_dispatcher.touch_move(touch_event);
-
-				break;
-			}
-			// case OSET_ACCELEROMETER:
-			// {
-			// 	AccelerometerEvent sensor_event;
-			// 	sensor_event.x = event.data_a.float_value;
-			// 	sensor_event.y = event.data_b.float_value;
-			// 	sensor_event.z = event.data_c.float_value;
-
-			// 	m_accelerometer.m_orientation.x = sensor_event.x;
-			// 	m_accelerometer.m_orientation.y = sensor_event.y;
-			// 	m_accelerometer.m_orientation.z = sensor_event.z;
-
-			// 	m_event_dispatcher.accelerometer_changed(sensor_event);
-			// 	break;
-			// }
-			default:
-			{
-				break;
-			}
-		}
-	}
-}
-
-} // namespace crown
-

+ 0 - 74
engine/input/InputManager.h

@@ -1,74 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "EventDispatcher.h"
-#include "Mouse.h"
-#include "Keyboard.h"
-#include "Touch.h"
-#include "Accelerometer.h"
-
-namespace crown
-{
-
-class MouseListener;
-class KeyboardListener;
-class TouchListener;
-
-class InputManager
-{
-public:
-
-						InputManager();
-						~InputManager();
-
-	Keyboard*			keyboard();
-	Mouse*				mouse();
-	Touch*				touch();
-	Accelerometer*		accelerometer();
-
-	void				register_mouse_listener(MouseListener* listener);
-	void				register_keyboard_listener(KeyboardListener* listener);
-	void				register_touch_listener(TouchListener* listener);
-	void				register_accelerometer_listener(AccelerometerListener* listener);
-
-	EventDispatcher*	get_event_dispatcher();
-
-	void				frame(uint64_t frame_count);
-
-private:
-
-	EventDispatcher		m_event_dispatcher;
-
-	Keyboard			m_keyboard;
-	Mouse				m_mouse;
-	Touch				m_touch;
-	Accelerometer		m_accelerometer;
-};
-
-} // namespace crown
-

+ 0 - 90
engine/input/Keyboard.cpp

@@ -1,90 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "Keyboard.h"
-#include "Assert.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-Keyboard::Keyboard() :
-	m_modifier(0),
-	m_current_frame(0),
-	m_last_key(KC_NOKEY)
-{
-	for (uint32_t i = 0; i < MAX_KEYCODES; i++)
-	{
-		m_keys[i] = ~0;
-		m_state[i] = false;
-	}
-}
-
-//-----------------------------------------------------------------------------
-bool Keyboard::modifier_pressed(ModifierKey modifier) const
-{
-	return (m_modifier & modifier) == modifier;
-}
-
-//-----------------------------------------------------------------------------
-bool Keyboard::key_pressed(KeyCode key) const
-{
-	CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
-
-	return (m_state[key] == true) && (m_keys[key] == m_current_frame);
-}
-
-//-----------------------------------------------------------------------------
-bool Keyboard::key_released(KeyCode key) const
-{
-	CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
-
-	return (m_state[key] == false) && (m_keys[key] == m_current_frame);
-}
-
-//-----------------------------------------------------------------------------
-bool Keyboard::any_pressed() const
-{
-	return key_pressed(m_last_key);
-}
-
-//-----------------------------------------------------------------------------
-bool Keyboard::any_released() const
-{
-	return key_released(m_last_key);
-}
-
-//-----------------------------------------------------------------------------
-void Keyboard::update(uint64_t frame, KeyCode k, bool state)
-{
-	CE_ASSERT(k >= 0 && k < MAX_KEYCODES, "KeyCode out of range: %d", k);
-
-	m_last_key = k;
-	m_keys[k] = frame;
-	m_state[k] = state;
-}
-
-} // namespace crown

+ 45 - 34
engine/input/Keyboard.h

@@ -28,7 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Types.h"
 #include "KeyCode.h"
-#include "OS.h"
+#include "Assert.h"
 
 #undef MK_SHIFT
 #undef MK_ALT
@@ -36,9 +36,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-class InputManager;
-
-
 /// Enumerates modifier keys.
 enum ModifierKey
 {
@@ -47,29 +44,18 @@ enum ModifierKey
 	MK_ALT		= 4
 };
 
-struct KeyboardEvent
-{
-	KeyCode		key;
-	uint8_t		modifier;
-	char		text[4];
-};
-
-class KeyboardListener
-{
-
-public:
-
-	virtual void key_pressed(const KeyboardEvent& event) { (void)event; }
-	virtual void key_released(const KeyboardEvent& event) { (void)event; }
-	virtual void text_input(const KeyboardEvent& event) { (void)event; }
-};
-
 /// Interface for accessing keyboard input device.
-class Keyboard
+struct Keyboard
 {
-public:
-
-					Keyboard();
+	Keyboard()
+		: m_modifier(0), m_current_frame(0), m_last_key(KC_NOKEY)
+	{
+		for (uint32_t i = 0; i < MAX_KEYCODES; i++)
+		{
+			m_keys[i] = ~0;
+			m_state[i] = false;
+		}
+	}
 
 	/// Returns whether the specified @a modifier is pressed.
 	/// @note
@@ -77,23 +63,49 @@ public:
 	/// of another key when the two are pressed in combination. (Thanks wikipedia.)
 	/// @note
 	/// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
-	bool			modifier_pressed(ModifierKey modifier) const;
+	bool modifier_pressed(ModifierKey modifier) const
+	{
+		return (m_modifier & modifier) == modifier;
+	}
 
 	/// Returns whether the specified @a key is pressed in the current frame.
-	bool			key_pressed(KeyCode key) const;
+	bool key_pressed(KeyCode key) const
+	{
+		CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
+
+		return (m_state[key] == true);
+	}
 
 	/// Returns whether the specified @a key is released in the current frame.
-	bool			key_released(KeyCode key) const;
+	bool key_released(KeyCode key) const
+	{
+		CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
+
+		return (m_state[key] == false) && (m_keys[key] == m_current_frame);
+	}
 
 	/// Returns wheter any key is pressed in the current frame.
-	bool			any_pressed() const;
+	bool any_pressed() const
+	{
+		return key_pressed(m_last_key);
+	}
 
 	/// Returns whether any key is released in the current frame.
-	bool			any_released() const;
+	bool any_released() const
+	{
+		return key_pressed(m_last_key);
+	}
+
+	void update(uint64_t frame, KeyCode k, bool state)
+	{
+		CE_ASSERT(k >= 0 && k < MAX_KEYCODES, "KeyCode out of range: %d", k);
 
-private:
+		m_last_key = k;
+		m_keys[k] = frame;
+		m_state[k] = state;
+	}
 
-	void			update(uint64_t frame, KeyCode k, bool state);
+public:
 
 	uint8_t			m_modifier;
 
@@ -105,8 +117,7 @@ private:
 	uint64_t		m_keys[MAX_KEYCODES];
 	bool			m_state[MAX_KEYCODES];
 
-	friend class	InputManager;
+	friend class	Device;
 };
 
 } // namespace crown
-

+ 0 - 140
engine/input/Mouse.cpp

@@ -1,140 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "Mouse.h"
-#include "Device.h"
-#include "OsWindow.h"
-
-#undef MB_RIGHT
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-Mouse::Mouse() :
-	m_current_frame(0),
-	m_last_button(MB_LEFT)
-{
-	m_buttons[MB_LEFT] = ~0;
-	m_buttons[MB_MIDDLE] = ~0;
-	m_buttons[MB_RIGHT] = ~0;
-
-	m_state[MB_LEFT] = false;
-	m_state[MB_MIDDLE] = false;
-	m_state[MB_RIGHT] = false;
-}
-
-//-----------------------------------------------------------------------------
-bool Mouse::button_pressed(MouseButton button) const
-{
-	// CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
-
-	// return (m_state[button] == true) && (m_buttons[button] == m_current_frame);
-	return false;
-}
-
-//-----------------------------------------------------------------------------
-bool Mouse::button_released(MouseButton button) const
-{
-	// CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
-
-	// return (m_state[button] == false) && (m_buttons[button] == m_current_frame);
-	return false;
-}
-
-//-----------------------------------------------------------------------------
-bool Mouse::any_pressed() const
-{
-	// return button_pressed(m_last_button);
-	return false;
-}
-
-//-----------------------------------------------------------------------------
-bool Mouse::any_released() const
-{
-	// return button_released(m_last_button);
-	return false;
-}
-
-//-----------------------------------------------------------------------------
-Vec2 Mouse::cursor_xy() const
-{
-	// int32_t x, y;
-
-	// device()->window()->get_cursor_xy(x, y);
-
-	// return Vec2(x, y);
-
-	// return Vec2(x, y);
-	return Vec2::ZERO;
-
-}
-
-//-----------------------------------------------------------------------------
-void Mouse::set_cursor_xy(const Vec2& position)
-{
-	// device()->window()->set_cursor_xy((int32_t) position.x, (int32_t) position.y);
-}
-
-//-----------------------------------------------------------------------------
-Vec2 Mouse::cursor_relative_xy() const
-{
-	// uint32_t window_width;
-	// uint32_t window_height;
-
-	// device()->window()->get_size(window_width, window_height);
-
-	// Vec2 pos = cursor_xy();
-
-	// pos.x = pos.x / (float) window_width;
-	// pos.y = pos.y / (float) window_height;
-
-	// return pos;
-	return Vec2::ZERO;
-}
-
-//-----------------------------------------------------------------------------
-void Mouse::set_cursor_relative_xy(const Vec2& position)
-{
-	// uint32_t window_width;
-	// uint32_t window_height;
-
-	// device()->window()->get_size(window_width, window_height);
-
-	// set_cursor_xy(Vec2(position.x * (float) window_width, position.y * (float) window_height));
-}
-
-//-----------------------------------------------------------------------------
-void Mouse::update(uint64_t frame, MouseButton b, bool state)
-{
-	// CE_ASSERT(b >= 0 && b < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", b);
-
-	// m_last_button = b;
-	// m_buttons[b] = frame;
-	// m_state[b] = state;
-}
-
-} // namespace crown

+ 79 - 31
engine/input/Mouse.h

@@ -29,6 +29,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Types.h"
 #include "Vec2.h"
+#include "Device.h"
+#include "OsWindow.h"
 
 namespace crown
 {
@@ -43,58 +45,75 @@ enum MouseButton
 	MB_RIGHT	= 2
 };
 
-struct MouseEvent
-{
-	MouseButton button;
-	int32_t x;
-	int32_t y;
-	float wheel;
-};
-
-/// Interface for managing mouse input.
-class MouseListener
-{
-
-public:
-
-	virtual void button_pressed(const MouseEvent& event) { (void)event; }
-	virtual void button_released(const MouseEvent& event) { (void)event; }
-	virtual void cursor_moved(const MouseEvent& event) { (void)event; }
-};
-
 /// Interface for accessing mouse input device.
 class Mouse
 {
 public:
 
-					Mouse();
+	//-----------------------------------------------------------------------------
+	Mouse()
+		: m_current_frame(0), m_last_button(MB_LEFT)
+	{
+		m_buttons[MB_LEFT] = ~0;
+		m_buttons[MB_MIDDLE] = ~0;
+		m_buttons[MB_RIGHT] = ~0;
 
+		m_state[MB_LEFT] = false;
+		m_state[MB_MIDDLE] = false;
+		m_state[MB_RIGHT] = false;
+	}
 
 	/// Returns whether @a button is pressed in the current frame.
-	bool			button_pressed(MouseButton button) const;
+	bool button_pressed(MouseButton button) const
+	{
+		CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
+
+		return (m_state[button] == true) && (m_buttons[button] == m_current_frame);
+	}
 
 	/// Returns whether @a button is released in the current frame.
-	bool			button_released(MouseButton button) const;
+	bool button_released(MouseButton button) const
+	{
+		CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
+
+		return (m_state[button] == false) && (m_buttons[button] == m_current_frame);
+	}
 
 	/// Returns wheter any button is pressed in the current frame.
-	bool			any_pressed() const;
+	bool any_pressed() const
+	{
+		return button_pressed(m_last_button);
+	}
 
 	/// Returns whether any button is released in the current frame.
-	bool			any_released() const;
+	bool any_released() const
+	{
+		return button_released(m_last_button);
+	}
 
 	/// Returns the position of the cursor in window space.
 	/// @note
 	/// Coordinates in window space have the origin at the
 	/// upper-left corner of the window. +X extends from left
 	/// to right and +Y extends from top to bottom.
-	Vec2			cursor_xy() const;
+	Vec2 cursor_xy() const
+	{
+		int32_t x, y;
+
+		device()->window()->get_cursor_xy(x, y);
+
+		return Vec2(x, y);
+	}
 
 	/// Sets the position of the cursor in window space.
 	/// @note
 	/// Coordinates in window space have the origin at the
 	/// upper-left corner of the window. +X extends from left
 	/// to right and +Y extends from top to bottom.
-	void			set_cursor_xy(const Vec2& position);
+	void set_cursor_xy(const Vec2& position)
+	{
+		device()->window()->set_cursor_xy((int32_t) position.x, (int32_t) position.y);
+	}
 
 	/// Returns the relative position of the cursor in window space.
 	/// @note
@@ -105,7 +124,20 @@ public:
 	/// Relative coordinates are mapped to a float varying
 	/// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
 	/// maximum extent of the cosidered axis.
-	Vec2			cursor_relative_xy() const;
+	Vec2 cursor_relative_xy() const
+	{
+		uint32_t window_width;
+		uint32_t window_height;
+
+		device()->window()->get_size(window_width, window_height);
+
+		Vec2 pos = cursor_xy();
+
+		pos.x = pos.x / (float) window_width;
+		pos.y = pos.y / (float) window_height;
+
+		return pos;
+	}
 
 	/// Sets the relative position of the cursor in window space.
 	/// @note
@@ -116,11 +148,27 @@ public:
 	/// Relative coordinates are mapped to a float varying
 	/// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
 	/// maximum extent of the cosidered axis.
-	void			set_cursor_relative_xy(const Vec2& position);
+	void set_cursor_relative_xy(const Vec2& position)
+	{
+		uint32_t window_width;
+		uint32_t window_height;
+
+		device()->window()->get_size(window_width, window_height);
+
+		set_cursor_xy(Vec2(position.x * (float) window_width, position.y * (float) window_height));
+	}
 
-private:
+	//-----------------------------------------------------------------------------
+	void update(uint64_t frame, MouseButton b, bool state)
+	{
+		CE_ASSERT(b >= 0 && b < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", b);
 
-	void			update(uint64_t frame, MouseButton b, bool state);
+		m_last_button = b;
+		m_buttons[b] = frame;
+		m_state[b] = state;
+	}
+
+public:
 
 	// The current frame number
 	uint64_t		m_current_frame;
@@ -131,7 +179,7 @@ private:
 	uint64_t		m_buttons[MAX_MOUSE_BUTTONS];
 	bool			m_state[MAX_MOUSE_BUTTONS];
 
-	friend class	InputManager;
+	friend class	Device;
 };
 
 } // namespace crown

+ 0 - 66
engine/input/Touch.cpp

@@ -1,66 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "Touch.h"
-
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-Touch::Touch()
-{
-
-}
-
-//-----------------------------------------------------------------------------
-bool Touch::touch_up(uint16_t id) const
-{
-	return m_pointers[id].up == true;
-}
-
-//-----------------------------------------------------------------------------
-bool Touch::touch_down(uint16_t id) const
-{
-	return m_pointers[id].up == false;
-}
-
-//----------------------------------------------------------------------------- 
-Vec2 Touch::touch_xy(uint16_t id) const
-{
-	const PointerData& data = m_pointers[id];
-
-	return Vec2(data.x, data.y);
-}
-
-//-----------------------------------------------------------------------------
-Vec2 Touch::touch_relative_xy(uint16_t id)
-{
-	const PointerData& data = m_pointers[id];
-
-	return Vec2(data.relative_x, data.relative_y);
-}
-
-} // namespace crown

+ 23 - 30
engine/input/Touch.h

@@ -43,44 +43,32 @@ struct PointerData
 	bool		up;
 };
 
-struct TouchEvent
-{
-	uint32_t	pointer_id;
-	uint32_t	x;
-	uint32_t	y;
-};
-
-/// Interface for managing touch input device.
-class TouchListener
-{
-
-public:
-
-	virtual void touch_down(const TouchEvent& event) { (void)event; }
-	virtual void touch_up(const TouchEvent& event) { (void)event; }
-	virtual void touch_move(const TouchEvent& event) { (void)event; }
-	virtual void touch_cancel(const TouchEvent& event) { (void)event; }
-};
-
 /// Interface for accessing touch input device.
-class Touch
+struct Touch
 {
-public:
-
-					Touch();
-
 	/// Returns whether the touch pointer @a id is up.
-	bool			touch_up(uint16_t id) const;
+	bool touch_up(uint16_t id) const
+	{
+		return m_pointers[id].up == true;
+	}
 
 	/// Returns whether the touch pointer @a id is down.
-	bool			touch_down(uint16_t id) const;
+	bool touch_down(uint16_t id) const
+	{
+		return m_pointers[id].up == false;
+	}
 
 	/// Returns the position of the pointer @a id in windows space.
 	/// @note
 	/// Coordinates in window space have the origin at the
 	/// upper-left corner of the window. +X extends from left
 	/// to right and +Y extends from top to bottom.
-	Vec2			touch_xy(uint16_t id) const;
+	Vec2 touch_xy(uint16_t id) const
+	{
+		const PointerData& data = m_pointers[id];
+
+		return Vec2(data.x, data.y);
+	}
 
 	/// Returns the relative position of the pointer @a id in window space.
 	/// @note
@@ -91,13 +79,18 @@ public:
 	/// Relative coordinates are mapped to a float varying
 	/// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
 	/// maximum extent of the cosidered axis.
-	Vec2			touch_relative_xy(uint16_t id);
+	Vec2 touch_relative_xy(uint16_t id)
+	{
+		const PointerData& data = m_pointers[id];
 
-private:
+		return Vec2(data.relative_x, data.relative_y);
+	}
+
+public:
 
 	PointerData		m_pointers[MAX_POINTER_IDS];
 
-	friend class	InputManager;
+	friend class	Device;
 };
 
 }

+ 0 - 3
engine/os/OS.h

@@ -32,7 +32,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Types.h"
 #include "Vector.h"
 #include "DynamicString.h"
-#include "EventBuffer.h"
 
 namespace crown
 {
@@ -158,7 +157,5 @@ void			execute_process(const char* args[]);
 
 } // namespace os
 
-EventBuffer* 	os_event_buffer();
-
 } // namespace crown
 

+ 19 - 10
engine/os/OsEvents.h → engine/os/OsTypes.h

@@ -31,7 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-/// __OsEventType__ represents an event fired by the OS
+/// Represents an event fired by the OS
 enum OsEventType
 {
 	OSET_NONE				= 0,
@@ -47,11 +47,11 @@ enum OsEventType
 	OSET_TOUCH_MOVE			= 7,
 	OSET_TOUCH_UP			= 8,
 	
-	OSET_ACCELEROMETER		= 9
+	OSET_ACCELEROMETER		= 9,
+	OSET_EXIT
 };
 
-/// __OsMouseEvent__ represents an event fired by mouse.
-/// It is processed by InputManager.
+/// Represents an event fired by mouse.
 struct OsMouseEvent
 {
 	uint32_t button;
@@ -59,16 +59,14 @@ struct OsMouseEvent
 	uint32_t y;
 };
 
-/// __OsKeyboardEvent__ represents an event fired by keyboard.
-/// it is processed by InputManager.
+/// Represents an event fired by keyboard.
 struct OsKeyboardEvent
 {
 	uint32_t key;
 	uint32_t modifier;
 };
 
-/// __OsTouchEvent__ represents an event fired by touch screen.
-/// It is processed by InputManager.
+/// Represents an event fired by touch screen.
 struct OsTouchEvent
 {
 	uint32_t pointer_id;
@@ -76,8 +74,7 @@ struct OsTouchEvent
 	uint32_t y;
 };
 
-/// __OsAccelerometerEvent__ represents an event fired by accelerometer.
-/// It is processed by InputManager.
+/// Represents an event fired by accelerometer.
 struct OsAccelerometerEvent
 {
 	float x;
@@ -85,4 +82,16 @@ struct OsAccelerometerEvent
 	float z;	
 };
 
+struct OsEvent
+{
+	OsEventType type;
+	union
+	{
+		OsMouseEvent mouse;
+		OsKeyboardEvent keyboard;
+		OsTouchEvent touch;
+		OsAccelerometerEvent accelerometer;
+	};
+};
+
 } // namespace crown

+ 0 - 2
engine/os/linux/LinuxOS.cpp

@@ -274,8 +274,6 @@ const char* get_env(const char* env)
 //-----------------------------------------------------------------------------
 void init_os()
 {
-	XInitThreads();
-
 	// Initilize the base time
 	clock_gettime(CLOCK_MONOTONIC, &base_time);
 }

+ 410 - 8
engine/os/linux/main.cpp

@@ -24,23 +24,425 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
+#include <X11/Xutil.h>
+#include <X11/Xatom.h>
+#include <X11/Xlib.h>
+#include <X11/XKBlib.h>
 #include "Crown.h"
+#include "Device.h"
+#include "OsTypes.h"
+#include "EventQueue.h"
 
-int main(int argc, char** argv)
+namespace crown
 {
-	crown::init();
 
-	crown::Device* engine = crown::device();
-	if (!engine->init(argc, argv))
+extern void set_x11_display_and_window(Display* dpy, Window win);
+
+//-----------------------------------------------------------------------------
+void init()
+{
+	crown::memory::init();
+	crown::os::init_os();
+}
+
+//-----------------------------------------------------------------------------
+void shutdown()
+{
+	crown::memory::shutdown();
+}
+
+// //-----------------------------------------------------------------------------
+// static Key x11_translate_key(int32_t x11_key)
+// {
+// 	if ((x11_key > 0x40 && x11_key < 0x5B) || (x11_key > 0x60 && x11_key < 0x7B) || (x11_key > 0x2F && x11_key < 0x3A))
+// 	{
+// 		return (Key)x11_key;
+// 	}
+
+// 	switch (x11_key)
+// 	{
+// 		case XK_BackSpace:	return KC_BACKSPACE;
+// 		case XK_Tab:		return KC_TAB;
+// 		case XK_space:		return KC_SPACE;
+// 		case XK_Escape:		return KC_ESCAPE;
+// 		case XK_Return:		return KC_ENTER;
+// 		case XK_F1:			return KC_F1;
+// 		case XK_F2:			return KC_F2;
+// 		case XK_F3:			return KC_F3;
+// 		case XK_F4:			return KC_F4;
+// 		case XK_F5:			return KC_F5;
+// 		case XK_F6:			return KC_F6;
+// 		case XK_F7:			return KC_F7;
+// 		case XK_F8:			return KC_F8;
+// 		case XK_F9:			return KC_F9;
+// 		case XK_F10:		return KC_F10;
+// 		case XK_F11:		return KC_F11;
+// 		case XK_F12:		return KC_F12;
+// 		case XK_Home:		return KC_HOME;
+// 		case XK_Left:		return KC_LEFT;
+// 		case XK_Up:			return KC_UP;
+// 		case XK_Right:		return KC_RIGHT;
+// 		case XK_Down:		return KC_DOWN;
+// 		case XK_Page_Up:	return KC_PAGE_UP;
+// 		case XK_Page_Down:	return KC_PAGE_DOWN;
+// 		case XK_Shift_L:	return KC_LSHIFT;
+// 		case XK_Shift_R:	return KC_RSHIFT;
+// 		case XK_Control_L:	return KC_LCONTROL;
+// 		case XK_Control_R:	return KC_RCONTROL;
+// 		case XK_Caps_Lock:	return KC_CAPS_LOCK;
+// 		case XK_Alt_L:		return KC_LALT;
+// 		case XK_Alt_R:		return KC_RALT;
+// 		case XK_Super_L:	return KC_LSUPER;
+// 		case XK_Super_R:	return KC_RSUPER;
+// 		case XK_KP_0:		return KC_KP_0;
+// 		case XK_KP_1:		return KC_KP_1;
+// 		case XK_KP_2:		return KC_KP_2;
+// 		case XK_KP_3:		return KC_KP_3;
+// 		case XK_KP_4:		return KC_KP_4;
+// 		case XK_KP_5:		return KC_KP_5;
+// 		case XK_KP_6:		return KC_KP_6;
+// 		case XK_KP_7:		return KC_KP_7;
+// 		case XK_KP_8:		return KC_KP_8;
+// 		case XK_KP_9:		return KC_KP_9;
+// 		default:			return KC_NOKEY;
+// 	}
+// }
+
+struct MainArgs
+{
+	int argc;
+	char** argv;
+	class LinuxDevice* device;
+};
+
+class LinuxDevice : public Device
+{
+public:
+
+	//-----------------------------------------------------------------------------
+	LinuxDevice()
+		: m_x11_display(NULL), m_x11_window(None), m_x11_parent_window(None), m_x11_hidden_cursor(None),
+			m_exit(false), m_x(0), m_y(0), m_width(1000), m_height(625), m_alloc(m_event_buffer, 1024 * 4), m_queue(m_alloc)
+	{
+		for (uint32_t i = 0; i < 1024; i++)
+		{
+			m_event_buffer[i] = 'a';
+		}
+	}
+
+	//-----------------------------------------------------------------------------
+	int32_t run(int argc, char** argv)
+	{
+		XInitThreads();
+
+		CE_ASSERT(m_width != 0 || m_height != 0, "Width and height must differ from zero");
+
+		m_x11_display = XOpenDisplay(NULL);
+
+		CE_ASSERT(m_x11_display != NULL, "Unable to open X11 display");
+
+		int screen = DefaultScreen(m_x11_display);
+		int depth = DefaultDepth(m_x11_display, screen);
+		Visual* visual = DefaultVisual(m_x11_display, screen);
+
+		// if (parent != 0)
+		// {
+		// 	m_x11_parent_window = (Window) parent;
+		// }
+		// else
+		{
+			m_x11_parent_window = RootWindow(m_x11_display, screen);
+		}
+
+		// We want to track keyboard and mouse events
+		XSetWindowAttributes win_attribs;
+		win_attribs.background_pixmap = 0;
+		win_attribs.border_pixel = 0;
+		win_attribs.event_mask = FocusChangeMask | StructureNotifyMask | KeyPressMask | 
+			KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
+
+		m_x11_window = XCreateWindow(
+					   m_x11_display,
+					   m_x11_parent_window,
+					   0, 0,
+					   m_width, m_height,
+					   0,
+					   depth,
+					   InputOutput,
+					   visual,
+					   CWBorderPixel | CWEventMask,
+					   &win_attribs
+				   );
+
+		CE_ASSERT(m_x11_window != None, "Unable to create X window");
+
+		// Check presence of detectable autorepeat
+		Bool detectable;
+		m_x11_detectable_autorepeat = (bool) XkbSetDetectableAutoRepeat(m_x11_display, true, &detectable);
+
+		// Build hidden cursor
+		Pixmap bm_no;
+		XColor black, dummy;
+		Colormap colormap;
+		static char no_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
+
+		colormap = XDefaultColormap(m_x11_display, screen);
+		XAllocNamedColor(m_x11_display, colormap, "black", &black, &dummy);
+		bm_no = XCreateBitmapFromData(m_x11_display, m_x11_window, no_data, 8, 8);
+		m_x11_hidden_cursor = XCreatePixmapCursor(m_x11_display, bm_no, bm_no, &black, &black, 0, 0);
+
+		m_wm_delete_message = XInternAtom(m_x11_display, "WM_DELETE_WINDOW", False);
+		XSetWMProtocols(m_x11_display, m_x11_window, &m_wm_delete_message, 1);
+
+		XMapRaised(m_x11_display, m_x11_window);
+
+		set_x11_display_and_window(m_x11_display, m_x11_window);
+
+		MainArgs args;
+		args.argc = argc;
+		args.argv = argv;
+		args.device = this;
+
+		OsThread game_thread("game-thread");
+		game_thread.start(main_loop, (void*)&args);
+
+		while (!m_exit)
+		{
+			while (XPending(m_x11_display))
+			{
+				LinuxDevice::pump_events();
+			}
+		}
+
+		game_thread.stop();
+
+		XDestroyWindow(m_x11_display, m_x11_window);
+		XCloseDisplay(m_x11_display);
+
+		return EXIT_SUCCESS;
+	}
+
+	//-----------------------------------------------------------------------------
+	static int32_t main_loop(void* user_data)
 	{
-		exit(EXIT_FAILURE);
+		MainArgs* args = (MainArgs*)user_data;
+
+		device()->init(args->argc, args->argv);
+
+		while(!args->device->process_events())
+		{
+			device()->frame();
+		}
+
+		device()->shutdown();
+
+		return 0;
 	}
 
-	while (engine->is_running())
+	//-----------------------------------------------------------------------------
+	bool process_events()
 	{
-		engine->frame();
+		OsEvent* event;
+		do
+		{
+			event = (OsEvent*)m_queue.get_event();
+
+			if (event != NULL)
+			{
+				switch (event->type)
+				{
+					case OSET_BUTTON_PRESS:
+					case OSET_BUTTON_RELEASE:
+					{
+						Log::d("Button has been pressed: %d %d", event->mouse.x, event->mouse.y);
+						break;
+					}
+					case OSET_EXIT:
+					{
+						Log::d("Exiting");
+						m_exit = true;
+						return true;
+					}
+					default:
+					{
+						Log::d("Unmanaged");
+						//CE_FATAL("Oops, unknown Os event");
+						break;
+					}
+				}
+			}
+		}
+		while (event != 0);
+
+		return false;
 	}
 
-	engine->shutdown();
+	//-----------------------------------------------------------------------------
+	void pump_events()
+	{
+		XEvent event;
+		XNextEvent(m_x11_display, &event);
+
+		switch (event.type)
+		{
+			case ClientMessage:
+			{
+				if ((Atom)event.xclient.data.l[0] == m_wm_delete_message)
+				{
+					OsEvent* os_event = new OsEvent;
+					os_event->type = OSET_EXIT;
+					m_queue.push_event(os_event);
+				}
+				break;
+			}
+			// case ConfigureNotify:
+			// {
+			// 	m_x = event.xconfigure.x;
+			// 	m_y = event.xconfigure.y;
+			// 	m_width = event.xconfigure.width;
+			// 	m_height = event.xconfigure.height;
+			// 	break;
+			// }
+			case ButtonPress:
+			case ButtonRelease:
+			{
+				OsEvent* os_event = new OsEvent;
+				OsMouseEvent& mouse_event = os_event->mouse;
+				os_event->type = (event.type == ButtonPress) ? OSET_BUTTON_PRESS : OSET_BUTTON_RELEASE;
+
+				mouse_event.x = event.xbutton.x;
+				mouse_event.y = event.xbutton.y;
+
+				switch (event.xbutton.button)
+				{
+					case Button1:
+					{
+						mouse_event.button = 0;
+						m_queue.push_event(os_event);
+						m_queue.push_event(os_event);
+						m_queue.push_event(os_event);
+						m_queue.push_event(os_event);
+						m_queue.push_event(os_event);
+						m_queue.push_event(os_event);
+						break;
+					}
+					case Button2:
+					{
+						mouse_event.button = 1;
+						m_queue.push_event(os_event);
+						break;
+					}
+					case Button3:
+					{
+						mouse_event.button = 2;
+						m_queue.push_event(os_event);
+						break;
+					}
+				}
+
+				break;
+			}
+			// case MotionNotify:
+			// {
+			// 	push_event(OSET_MOTION_NOTIFY, data_button[0], data_button[1], data_button[2], data_button[3]);
+			// 	break;
+			// }
+	// 		case KeyPress:
+	// 		case KeyRelease:
+	// 		{
+	// 			char string[4] = {0, 0, 0, 0};
+	// 			KeySym key;
+
+	// 			XLookupString(&event.xkey, string, 4, &key, NULL);
+
+	// 			Key kc = x11_translate_key(key);
+
+	// 			// Check if any modifier key is pressed or released
+	// 			int32_t modifier_mask = 0;
+
+	// 			if (kc == KC_LSHIFT || kc == KC_RSHIFT)
+	// 			{
+	// 				(event.type == KeyPress) ? modifier_mask |= MK_SHIFT : modifier_mask &= ~MK_SHIFT;
+	// 			}
+	// 			else if (kc == KC_LCONTROL || kc == KC_RCONTROL)
+	// 			{
+	// 				(event.type == KeyPress) ? modifier_mask |= MK_CTRL : modifier_mask &= ~MK_CTRL;
+	// 			}
+	// 			else if (kc == KC_LALT || kc == KC_RALT)
+	// 			{
+	// 				(event.type == KeyPress) ? modifier_mask |= MK_ALT : modifier_mask &= ~MK_ALT;
+	// 			}
+
+	// 			OsEventType oset_type = event.type == KeyPress ? OSET_KEY_PRESS : OSET_KEY_RELEASE;
+
+	// 			keyboard_event.key = ((int32_t)kc);
+	// 			keyboard_event.modifier = modifier_mask;
+
+	// 			m_queue.push_event(oset_type, &keyboard_event, sizeof(OsKeyboardEvent));
+
+	// //				// Text input part
+	// //				if (event.type == KeyPress && len > 0)
+	// //				{
+	// //					//crownEvent.event_type = ET_TEXT;
+	// //					//crownEvent.text.type = TET_TEXT_INPUT;
+	// //					strncpy(keyboardEvent.text, string, 4);
+
+	// //					if (mListener)
+	// //					{
+	// //						mListener->TextInput(keyboardEvent);
+	// //					}
+	// //				}
+
+	// 			break;
+	// 		}
+	// 		case KeymapNotify:
+	// 		{
+	// 			XRefreshKeyboardMapping(&event.xmapping);
+	// 			break;
+	// 		}
+			default:
+			{
+				break;
+			}
+		}
+	}
+
+private:
+
+	Display* m_x11_display;
+	Window m_x11_window;
+	Window m_x11_parent_window;
+	Cursor m_x11_hidden_cursor;
+	Atom m_wm_delete_message;
+
+	bool m_exit;
+	uint32_t m_x;
+	uint32_t m_y;
+	uint32_t m_width;
+	uint32_t m_height;
+	bool m_x11_detectable_autorepeat;
+
+	char m_event_buffer[1024 * 4];
+	LinearAllocator m_alloc;
+	EventQueue m_queue;
+};
+
+crown::LinuxDevice* g_engine = NULL;
+Device* device() { return g_engine; }
+
+} // namespace crown
+
+int main(int argc, char** argv)
+{
+	crown::init();
+
+	crown::g_engine = CE_NEW(crown::default_allocator(), crown::LinuxDevice);
+
+	int32_t ret = crown::device()->run(argc, argv);
+
+	CE_DELETE(crown::default_allocator(), crown::g_engine);
+
 	crown::shutdown();
+
+	return ret;
 }

+ 0 - 320
engine/os/linux/main2.cpp

@@ -1,320 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "Crown.h"
-#include "FPSSystem.h"
-
-using namespace crown;
-
-Renderer* r;
-IndexBufferId monkey_ib;
-VertexBufferId monkey_vb;
-IndexBufferId cube_ib;
-VertexBufferId cube_vb;
-IndexBufferId cone_ib;
-VertexBufferId cone_vb;
-
-GPUProgramId default_program;
-GPUProgramId texture_program;
-Camera* camera;
-FPSSystem* fps;
-
-TextureId grass_texture;
-TextureId lightmap_texture;
-IndexBufferId quad_ib;
-VertexBufferId quad_vb;
-
-UniformId u_albedo_0;
-UniformId u_lightmap_0;
-UniformId u_brightness;
-
-static float quad_vertices[] =
-{
-	-1.0f, -1.0f, 0.0f, 
-	 0.0f, 0.0f, 1.0f,
-	 1.0f, 1.0f, 1.0f, 1.0f,
-	 0.0f, 0.0f,
-
-	 1.0f, -1.0f, 0.0f,
-	 0.0f, 0.0f, 1.0f,
-	 1.0f, 1.0f, 1.0f, 1.0f,
-	 1.0f, 0.0f,
-
-	 1.0f,  1.0f, 0.0f,
-	 0.0f, 0.0f, 1.0f,
-	 1.0f, 1.0f, 1.0f, 1.0f,
-	 1.0f, 1.0f,
-
-	-1.0f,  1.0f, 0.0f,
-	 0.0f, 0.0f, 1.0f,
-	 1.0f, 1.0f, 1.0f, 1.0f,
-	 0.0f, 1.0f
-};
-
-static uint16_t quad_indices[] =
-{
-	0, 1, 3,
-	1, 2, 3
-};
-
-static const char* default_vertex =
-	"in vec4           a_position;"
-	"in vec4           a_normal;"
-	"in vec2           a_tex_coord0;"
-	"in vec4           a_color;"
-
-	"uniform mat4      u_model;"
-	"uniform mat4      u_model_view_projection;"
-
-	"varying out vec2  tex_coord0;"
-	"varying out vec4  color;"
-
-	"void main(void)"
-	"{"
-	"	tex_coord0 = a_tex_coord0;"
-	"   color = a_color;"
-	"	gl_Position = u_model_view_projection * a_position;"
-	"}";
-
-static const char* default_fragment = 
-	"void main(void)"
-	"{"
-	"	gl_FragColor = vec4(1, 0, 0, 0);"
-	"}";
-
-static const char* texture_fragment = 
-	"in vec2            tex_coord0;"
-	"in vec4            color;"
-
-	"uniform sampler2D  u_albedo_0;"
-	"uniform sampler2D  u_lightmap_0;"
-	"uniform float      u_brightness;"
-
-	"void main(void)"
-	"{"
-	"	gl_FragColor = texture(u_albedo_0, tex_coord0);"
-	"}";
-
-void draw(float dt)
-{
-	Mat4 pose; pose.load_identity();
-
-	fps->update(dt);
-	fps->set_view_by_cursor();
-
-	//-----------------------
-	r->set_layer_view(0, camera->view_matrix());
-	r->set_layer_projection(0, camera->projection_matrix());
-	r->set_layer_viewport(0, 0, 0, 1000, 625);
-	r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
-
-	r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CCW);
-	r->commit(0);
-
-	static uint64_t prim = STATE_PRIMITIVE_TRIANGLES;
-	if (device()->keyboard()->key_pressed(KC_z))
-	{
-		prim = STATE_PRIMITIVE_TRIANGLES;
-	}
-	else if (device()->keyboard()->key_pressed(KC_x))
-	{
-		prim = STATE_PRIMITIVE_POINTS;
-	}
-	else if (device()->keyboard()->key_pressed(KC_c))
-	{
-		prim = STATE_PRIMITIVE_LINES;
-	}
-
-	static uint32_t filter = TEXTURE_FILTER_NEAREST;
-	if (device()->keyboard()->key_pressed(KC_1))
-	{
-		filter = TEXTURE_FILTER_NEAREST;
-	}
-	else if (device()->keyboard()->key_pressed(KC_2))
-	{
-		filter = TEXTURE_FILTER_LINEAR;
-	}
-	else if (device()->keyboard()->key_pressed(KC_3))
-	{
-		filter = TEXTURE_FILTER_BILINEAR;
-	}
-	else if (device()->keyboard()->key_pressed(KC_4))
-	{
-		filter = TEXTURE_FILTER_TRILINEAR;
-	}
-	static float brightness = 1.0f;
-	if (device()->keyboard()->key_pressed(KC_UP))
-	{
-		brightness += 0.01f;
-	}
-	else if (device()->keyboard()->key_pressed(KC_DOWN))
-	{
-		brightness += -0.01f;
-	}
-	if (brightness > 1.0f)
-		brightness = 1.0f;
-
-	//-----------------------
-	// r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CW);
-	// r->set_vertex_buffer(monkey_vb);
-	// r->set_index_buffer(monkey_ib);
-	// r->set_program(default_program);
-	// pose.set_translation(Vec3(-3, 0, -3));
-	// r->set_pose(pose);
-	// r->commit(0);
-
-	//-----------------------
-	r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CW);
-	r->set_vertex_buffer(cube_vb);
-	r->set_index_buffer(cube_ib);
-	r->set_program(texture_program);
-	r->set_texture(0, u_albedo_0, grass_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
-	r->set_texture(1, u_lightmap_0, lightmap_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
-	r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);
-
-	pose.set_translation(Vec3(0, 0, -3));
-	r->set_pose(pose);
-	r->commit(0);
-
-	//-----------------------
-	// r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
-	// r->set_vertex_buffer(cone_vb);
-	// r->set_index_buffer(cone_ib);
-	// r->set_program(default_program);
-
-	// pose.set_translation(Vec3(3, 0, -3));
-	// r->set_pose(pose);
-	// r->commit(0);
-
-	//-----------------------
-	// r->set_state(prim | STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
-	// r->set_vertex_buffer(quad_vb);
-	// r->set_index_buffer(quad_ib);
-	// r->set_program(texture_program);
-	// r->set_texture(0, u_albedo_0, grass_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
-	// r->set_texture(1, u_lightmap_0, lightmap_texture, filter | TEXTURE_WRAP_CLAMP_EDGE);
-	// r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);
-
-	// pose.set_translation(Vec3(0, 0, -1));
-	// r->set_pose(pose);
-	// r->commit(0);
-}
-
-int main(int argc, char** argv)
-{
-	crown::init();
-
-	Device* engine = device();
-	engine->init(argc, argv);
-
-	// Load resources
-	ResourceManager* resman = engine->resource_manager();
-	ResourceId texture = resman->load("texture", "ship");
-	ResourceId lightmap = resman->load("texture", "lightmap");
-	ResourceId monkey = resman->load("mesh", "monkey");
-	ResourceId cube = resman->load("mesh", "ship");
-	ResourceId cone = resman->load("mesh", "cone");
-	resman->flush();
-
-	TextureResource* texture_resource = (TextureResource*)resman->data(texture);
-	TextureResource* lightmap_resource = (TextureResource*)resman->data(lightmap);
-	MeshResource* monkey_resource = (MeshResource*)resman->data(monkey);
-	MeshResource* cube_resource = (MeshResource*)resman->data(cube);
-	MeshResource* cone_resource = (MeshResource*)resman->data(cone);
-
-	Log::d("monkey format = %d", monkey_resource->vertex_format());
-	Log::d("monkey vcount = %d", monkey_resource->num_vertices());
-	Log::d("cube format   = %d", cube_resource->vertex_format());
-	Log::d("cube vcount   = %d", cube_resource->num_vertices());
-	Log::d("cone format = %d", cone_resource->vertex_format());
-	Log::d("cone vcount = %d", cone_resource->num_vertices());
-
-	// Create vb/ib
-	r = engine->renderer();
-	monkey_vb = r->create_vertex_buffer(monkey_resource->num_vertices(), monkey_resource->vertex_format(), monkey_resource->vertices());
-	monkey_ib = r->create_index_buffer(monkey_resource->num_indices(), monkey_resource->indices());
-	cube_vb = r->create_vertex_buffer(cube_resource->num_vertices(), cube_resource->vertex_format(), cube_resource->vertices());
-	cube_ib = r->create_index_buffer(cube_resource->num_indices(), cube_resource->indices());
-	cone_vb = r->create_vertex_buffer(cone_resource->num_vertices(), cone_resource->vertex_format(), cone_resource->vertices());
-	cone_ib = r->create_index_buffer(cone_resource->num_indices(), cone_resource->indices());
-
-	// Create texture
-	grass_texture = r->create_texture(texture_resource->width(), texture_resource->height(), texture_resource->format(),
-										texture_resource->data());
-	lightmap_texture = r->create_texture(lightmap_resource->width(), lightmap_resource->height(), lightmap_resource->format(),
-										lightmap_resource->data());
-
-	quad_vb = r->create_vertex_buffer(4, VERTEX_P3_N3_C4_T2, quad_vertices);
-	quad_ib = r->create_index_buffer(6, quad_indices);
-
-	ShaderId default_vs = r->create_shader(SHADER_VERTEX, default_vertex);
-	ShaderId default_fs = r->create_shader(SHADER_FRAGMENT, default_fragment);
-	ShaderId texture_fs = r->create_shader(SHADER_FRAGMENT, texture_fragment);
-
-	u_albedo_0 = r->create_uniform("u_albedo_0", UNIFORM_INTEGER_1, 1);
-	u_lightmap_0 = r->create_uniform("u_lightmap_0", UNIFORM_INTEGER_1, 1);
-	u_brightness = r->create_uniform("u_brightness", UNIFORM_FLOAT_1, 1);
-
-	default_program = r->create_gpu_program(default_vs, default_fs);
-	texture_program = r->create_gpu_program(default_vs, texture_fs);
-
-	// Create camera
-	TempAllocator2048 alloc;
-	camera = CE_NEW(alloc, Camera)(Vec3(0, 0, 3), 90.0f, 16.0f/9.0f);
-	fps = CE_NEW(alloc, FPSSystem)(camera, 3.0f, 2.5f);
-
-	while (engine->is_running())
-	{
-		engine->frame(draw);
-	}
-
-	//resman->unload(mesh);
-	resman->unload(texture);
-	resman->unload(lightmap);
-	resman->unload(monkey);
-	resman->unload(cube);
-	resman->unload(cone);
-	r->destroy_index_buffer(monkey_ib);
-	r->destroy_vertex_buffer(monkey_vb);
-	r->destroy_index_buffer(cube_ib);
-	r->destroy_vertex_buffer(cube_vb);
-	r->destroy_index_buffer(cone_ib);
-	r->destroy_vertex_buffer(cone_vb);
-
-	r->destroy_shader(default_vs);
-	r->destroy_shader(default_fs);
-	r->destroy_shader(texture_fs);
-	r->destroy_gpu_program(default_program);
-	r->destroy_gpu_program(texture_program);
-	r->destroy_vertex_buffer(quad_vb);
-	r->destroy_index_buffer(quad_ib);
-	r->destroy_uniform(u_albedo_0);
-	r->destroy_uniform(u_lightmap_0);
-	r->destroy_uniform(u_brightness);
-
-	engine->shutdown();
-	crown::shutdown();
-}

+ 1 - 0
engine/renderers/gl/glx/GLContext.cpp

@@ -98,6 +98,7 @@ void GLContext::destroy_context()
 //-----------------------------------------------------------------------------
 void GLContext::swap_buffers()
 {
+	glXMakeCurrent(s_x11_display, s_x11_window, m_glx_context);
 	glXSwapBuffers(s_x11_display, s_x11_window);
 }