Просмотр исходного кода

More work to separate os windows from gl context

Daniele Bartolini 12 лет назад
Родитель
Сommit
6ac97df77a

+ 35 - 9
src/Device.cpp

@@ -46,6 +46,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Mouse.h"
 #include "Touch.h"
 #include "Accelerometer.h"
+#include "OsWindow.h"
 
 #ifdef CROWN_BUILD_OPENGL
 	#include "renderers/gl/GLRenderer.h"
@@ -129,9 +130,12 @@ bool Device::init(int argc, char** argv)
 
 	Log::d("Input manager created.");
 
-	create_renderer();
+	create_window();
 
-	m_renderer->init();
+	m_window->set_title("Crown Game Engine");
+	Log::d("Window created.");
+
+	create_renderer();
 
 	Log::d("Renderer created.");
 
@@ -198,8 +202,13 @@ void Device::shutdown()
 		delete m_input_manager;
 	}
 
-	Log::i("Releasing Renderer...");
+	Log::i("Releasing DebugRenderer...");
+	if (m_debug_renderer)
+	{
+		delete m_debug_renderer;
+	}
 
+	Log::i("Releasing Renderer...");
 	if (m_renderer)
 	{
 		m_renderer->shutdown();
@@ -207,10 +216,10 @@ void Device::shutdown()
 		delete m_renderer;
 	}
 
-	Log::i("Releasing DebugRenderer...");
-	if (m_debug_renderer)
+	Log::i("Releasing Window...");
+	if (m_window)
 	{
-		delete m_debug_renderer;
+		delete m_window;
 	}
 
 	Log::i("Releasing ResourceManager...");
@@ -225,7 +234,6 @@ void Device::shutdown()
 	}
 
 	Log::i("Releasing Filesystem...");
-
 	if (m_filesystem)
 	{
 		delete m_filesystem;
@@ -258,6 +266,12 @@ InputManager* Device::input_manager()
 	return m_input_manager;
 }
 
+//-----------------------------------------------------------------------------
+OsWindow* Device::window()
+{
+	return m_window;
+}
+
 //-----------------------------------------------------------------------------
 Renderer* Device::renderer()
 {
@@ -348,12 +362,12 @@ void Device::frame()
 	m_resource_manager->check_load_queue();
 	m_resource_manager->bring_loaded_online();
 
-	m_input_manager->event_loop();
+	m_window->frame();
+	m_input_manager->frame();
 
 	game_frame(last_delta_time());
 
 	m_debug_renderer->draw_all();
-
 	m_renderer->frame();
 
 	m_frame_count++;
@@ -427,6 +441,16 @@ void Device::create_input_manager()
 	m_input_manager = new InputManager();
 }
 
+//-----------------------------------------------------------------------------
+void Device::create_window()
+{
+	m_window = new OsWindow(m_preferred_window_width, m_preferred_window_height);
+
+	ce_assert(m_window != NULL, "Unable to create the window");
+
+	m_window->show();
+}
+
 //-----------------------------------------------------------------------------
 void Device::create_renderer()
 {
@@ -449,6 +473,8 @@ void Device::create_renderer()
 		exit(EXIT_FAILURE);
 		#endif
 	}
+
+	m_renderer->init();
 }
 
 //-----------------------------------------------------------------------------

+ 7 - 0
src/Device.h

@@ -37,6 +37,7 @@ namespace crown
 class Filesystem;
 class ResourceManager;
 class ResourceArchive;
+class OsWindow;
 class Renderer;
 class DebugRenderer;
 class InputManager;
@@ -98,6 +99,8 @@ public:
 	Filesystem*				filesystem();
 	ResourceManager*		resource_manager();
 	InputManager*			input_manager();
+
+	OsWindow*				window();
 	Renderer*				renderer();
 	DebugRenderer*			debug_renderer();
 
@@ -111,6 +114,8 @@ private:
 	void					create_filesystem();
 	void					create_resource_manager();
 	void					create_input_manager();
+
+	void					create_window();
 	void					create_renderer();
 	void					create_debug_renderer();
 
@@ -140,6 +145,8 @@ private:
 
 	// Public subsystems
 	Filesystem*				m_filesystem;
+
+	OsWindow*				m_window;
 	InputManager*			m_input_manager;
 	Renderer*				m_renderer;
 	DebugRenderer*			m_debug_renderer;

+ 2 - 24
src/input/InputManager.cpp

@@ -31,8 +31,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-InputManager::InputManager() :
-	m_cursor_visible(true)
+InputManager::InputManager()
 {
 }
 
@@ -96,7 +95,7 @@ EventDispatcher* InputManager::get_event_dispatcher()
 }
 
 //-----------------------------------------------------------------------------
-void InputManager::event_loop()
+void InputManager::frame()
 {
 	OsEvent event;
 
@@ -222,26 +221,5 @@ void InputManager::event_loop()
 	}
 }
 
-//-----------------------------------------------------------------------------
-bool InputManager::is_cursor_visible() const
-{
-	return m_cursor_visible;
-}
-
-//-----------------------------------------------------------------------------
-void InputManager::set_cursor_visible(bool visible)
-{
-	if (visible)
-	{
-		//hide_cursor();
-	}
-	else
-	{
-		//show_cursor();
-	}
-
-	m_cursor_visible = visible;
-}
-
 } // namespace crown
 

+ 1 - 9
src/input/InputManager.h

@@ -57,13 +57,7 @@ public:
 
 	EventDispatcher*	get_event_dispatcher();
 
-	/// Returns whether the cursor is visible.
-	bool				is_cursor_visible() const;
-
-	/// Sets whether the cursor is visible.
-	void				set_cursor_visible(bool visible);
-
-	void				event_loop();
+	void				frame();
 
 private:
 
@@ -73,8 +67,6 @@ private:
 	Mouse				m_mouse;
 	Touch				m_touch;
 	Accelerometer		m_accelerometer;
-
-	bool				m_cursor_visible;
 };
 
 } // namespace crown

+ 6 - 5
src/input/Mouse.cpp

@@ -24,7 +24,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Mouse.h"
-#include "OS.h"
+#include "Device.h"
+#include "OsWindow.h"
 
 namespace crown
 {
@@ -54,7 +55,7 @@ Vec2 Mouse::cursor_xy() const
 {
 	int32_t x, y;
 
-	//os::get_cursor_xy(xy.x, xy.y);
+	device()->window()->get_cursor_xy(x, y);
 
 	return Vec2(x, y);
 }
@@ -62,7 +63,7 @@ Vec2 Mouse::cursor_xy() const
 //-----------------------------------------------------------------------------
 void Mouse::set_cursor_xy(const Vec2& position)
 {
-	//os::set_cursor_xy(position.x, position.y);
+	device()->window()->set_cursor_xy((int32_t) position.x, (int32_t) position.y);
 }
 
 //-----------------------------------------------------------------------------
@@ -71,7 +72,7 @@ Vec2 Mouse::cursor_relative_xy() const
 	uint32_t window_width;
 	uint32_t window_height;
 
-	os::get_render_window_metrics(window_width, window_height);
+	device()->window()->get_size(window_width, window_height);
 
 	Vec2 pos = cursor_xy();
 
@@ -87,7 +88,7 @@ void Mouse::set_cursor_relative_xy(const Vec2& position)
 	uint32_t window_width;
 	uint32_t window_height;
 
-	os::get_render_window_metrics(window_width, window_height);
+	device()->window()->get_size(window_width, window_height);
 
 	set_cursor_xy(Vec2(position.x * (float) window_width, position.y * (float) window_height));
 }

+ 0 - 11
src/os/OS.h

@@ -86,20 +86,9 @@ const char*		get_env(const char* env);		//! Returns the content of the 'env' env
 //-----------------------------------------------------------------------------
 void			init_os();
 
-bool			create_render_window(uint32_t x, uint32_t y, uint32_t width, uint32_t height, bool fullscreen);
-bool			destroy_render_window();
-void			get_render_window_metrics(uint32_t& width, uint32_t& height);
-void			swap_buffers();
-
-void			event_loop();
-
-void			init_input();
 void			get_cursor_xy(int32_t& x, int32_t& y);
 void			set_cursor_xy(int32_t x, int32_t y);
 
-void			hide_cursor();
-void			show_cursor();
-
 //-----------------------------------------------------------------------------
 // Timing
 //-----------------------------------------------------------------------------

+ 4 - 1
src/os/linux/CMakeLists.txt

@@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 2.8)
 project(crown-linux)
 
 set (LINUX_SRC
-	GLXRenderWindow.cpp
 	LinuxOS.cpp
 	OsWindow.cpp
 	../posix/TCPSocket.cpp
@@ -12,6 +11,8 @@ set (LINUX_SRC
 	../posix/Thread.cpp
 	../posix/Mutex.cpp
 	../posix/Cond.cpp
+
+	../../renderers/gl/GLXContext.cpp
 )
 
 set (LINUX_HEADERS
@@ -22,6 +23,8 @@ set (LINUX_HEADERS
 	Thread.h
 	Mutex.h
 	Cond.h
+
+	../../renderers/gl/GLXContext.h
 )
 
 link_libraries(X11 Xrandr pthread)

+ 381 - 0
src/os/linux/OsWindow.cpp

@@ -0,0 +1,381 @@
+/*
+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 "OsWindow.h"
+#include "Assert.h"
+#include "Keyboard.h"
+#include "OS.h"
+#include "GLXContext.h"
+#include "String.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+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;
+	}
+}
+
+//-----------------------------------------------------------------------------
+OsWindow::OsWindow(uint32_t width, uint32_t height) :
+	m_x11_display(NULL),
+	m_x11_window(None),
+	m_x(0),
+	m_y(0),
+	m_width(width),
+	m_height(height),
+	m_x11_detectable_autorepeat(false),
+	m_x11_hidden_cursor(None)
+{
+	ce_assert(width != 0 || 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);
+	Window root_window = RootWindow(m_x11_display, screen);
+	int depth = DefaultDepth(m_x11_display, screen);
+	Visual* visual = DefaultVisual(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,
+				   root_window,
+				   0, 0,
+				   width, 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);
+
+	set_x11_display_and_window(m_x11_display, m_x11_window);
+}
+
+//-----------------------------------------------------------------------------
+OsWindow::~OsWindow()
+{
+	if (m_x11_display)
+	{
+		if (m_x11_window != None)
+		{
+			XDestroyWindow(m_x11_display, m_x11_window);
+		}
+
+		XCloseDisplay(m_x11_display);
+	}
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::show()
+{
+	XMapRaised(m_x11_display, m_x11_window);
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::hide()
+{
+	XUnmapWindow(m_x11_display, m_x11_window);
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::get_size(uint32_t& width, uint32_t& height)
+{
+	width = m_width;
+	height = m_height;
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::get_position(uint32_t& x, uint32_t& y)
+{
+	x = m_x;
+	y = m_y;
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::resize(uint32_t width, uint32_t height)
+{
+	XResizeWindow(m_x11_display, m_x11_window, width, height);
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::move(uint32_t x, uint32_t y)
+{
+	XMoveWindow(m_x11_display, m_x11_window, x, y);
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::show_cursor()
+{
+	XDefineCursor(m_x11_display, m_x11_window, None);
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::hide_cursor()
+{
+	XDefineCursor(m_x11_display, m_x11_window, m_x11_hidden_cursor);
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::get_cursor_xy(int32_t& x, int32_t& y)
+{
+	Window unused;
+	int32_t pointer_x, pointer_y, dummy;
+	uint32_t dummy2;
+
+	XQueryPointer(m_x11_display, m_x11_window, &unused, &unused, &dummy, &dummy, &pointer_x, &pointer_y, &dummy2);
+
+	x = pointer_x;
+	y = pointer_y;
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::set_cursor_xy(int32_t x, int32_t y)
+{
+	XWarpPointer(m_x11_display, None, m_x11_window, 0, 0, m_width, m_height, x, y);
+
+	XFlush(m_x11_display);
+}
+
+//-----------------------------------------------------------------------------
+char* OsWindow::title()
+{
+	static char title[1024];
+
+	char* tmp_title;
+	XFetchName(m_x11_display, m_x11_window, &tmp_title);
+
+	string::strncpy(title, tmp_title, 1024);
+	XFree(tmp_title);
+
+	return title;
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::set_title(const char* title)
+{
+	XStoreName(m_x11_display, m_x11_window, title);
+}
+
+//-----------------------------------------------------------------------------
+void OsWindow::frame()
+{
+	XEvent event;
+
+	OsEventParameter data_button[4] = {0, 0, 0, 0};
+	OsEventParameter data_key[4] = {0, 0, 0, 0};
+
+	while (XPending(m_x11_display))
+	{
+		XNextEvent(m_x11_display, &event);
+
+		switch (event.type)
+		{
+			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:
+			{
+				OsEventType oset_type = event.type == ButtonPress ? OSET_BUTTON_PRESS : OSET_BUTTON_RELEASE;
+
+				data_button[0].int_value = event.xbutton.x;
+				data_button[1].int_value = event.xbutton.y;
+
+				switch (event.xbutton.button)
+				{
+					case Button1:
+					{
+						data_button[2].int_value = 0;
+						push_event(oset_type, data_button[0], data_button[1], data_button[2], data_button[3]);
+						break;
+					}
+					case Button2:
+					{
+						data_button[2].int_value = 1;
+						push_event(oset_type, data_button[0], data_button[1], data_button[2], data_button[3]);
+						break;
+					}
+					case Button3:
+					{
+						data_button[2].int_value = 2;
+						push_event(oset_type, data_button[0], data_button[1], data_button[2], data_button[3]);
+						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};
+				int32_t len = -1;
+				KeySym key;
+
+				len = 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;
+
+				data_key[0].int_value = ((int32_t)kc);
+				data_key[1].int_value = modifier_mask;
+
+				push_event(oset_type, data_key[0], data_key[1], data_key[2], data_key[3]);
+
+//				// 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;
+			}
+		}
+	}
+}
+
+} // namespace crown

+ 79 - 0
src/os/linux/OsWindow.h

@@ -0,0 +1,79 @@
+/*
+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 <X11/Xutil.h>
+#include <X11/Xatom.h>
+#include <X11/Xlib.h>
+#include <X11/XKBlib.h>
+
+#include "Types.h"
+
+namespace crown
+{
+
+class OsWindow
+{
+public:
+
+					OsWindow(uint32_t width, uint32_t height);
+					~OsWindow();
+
+	void			show();
+	void			hide();
+
+	void			get_size(uint32_t& width, uint32_t& height);
+	void			get_position(uint32_t& x, uint32_t& y);
+
+	void			resize(uint32_t width, uint32_t height);
+	void			move(uint32_t x, uint32_t y);
+
+	void			show_cursor();
+	void			hide_cursor();
+
+	void			get_cursor_xy(int32_t& x, int32_t& y);
+	void			set_cursor_xy(int32_t x, int32_t y);
+
+	char*			title();
+	void			set_title(const char* title);
+
+	void			frame();
+
+private:
+
+	Display*		m_x11_display;
+	Window			m_x11_window;
+
+	uint32_t		m_x;
+	uint32_t		m_y;
+	uint32_t		m_width;
+	uint32_t		m_height;
+
+	bool			m_x11_detectable_autorepeat;
+	Cursor			m_x11_hidden_cursor;
+};
+
+} // namespace crown

+ 0 - 10
src/os/linux/main.cpp

@@ -40,12 +40,6 @@ int main(int argc, char** argv)
 {
 	crown::os::init_os();
 
-	crown::os::create_render_window(0, 0, 1000, 625, false);
-
-	crown::Context context;
-	context.set_window(crown::os::window);
-	context.create_context();
-
 	crown::Device* engine = crown::device();
 
 	if (!engine->init(argc, argv))
@@ -57,14 +51,10 @@ int main(int argc, char** argv)
 	while (engine->is_running())
 	{
 		engine->frame();
-
-		crown::os::swap_buffers();
 	}
 
 	engine->shutdown();
 
-	crown::os::destroy_render_window();
-
 	return 0;
 }
 

+ 1 - 0
src/renderers/Renderer.h

@@ -25,6 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include "Config.h"
 #include "Types.h"
 #include "Color4.h"
 #include "Mat4.h"

+ 6 - 0
src/renderers/gl/GLRenderer.cpp

@@ -132,6 +132,8 @@ GLRenderer::~GLRenderer()
 //-----------------------------------------------------------------------------
 void GLRenderer::init()
 {
+	m_context.create_context();
+
 	GLenum err = glewInit();
 	ce_assert(err == GLEW_OK, "Failed to initialize GLEW");
 
@@ -200,6 +202,8 @@ void GLRenderer::init()
 void GLRenderer::shutdown()
 {
 	unload_default_shaders();
+
+	m_context.destroy_context();
 }
 
 //-----------------------------------------------------------------------------
@@ -818,6 +822,8 @@ void GLRenderer::frame()
 	set_gpu_program_vec3_uniform(m_default_gpu_program, "color", Vec3(0, 1, 0));
 
 	gl_check(glFinish());
+
+	m_context.swap_buffers();
 }
 
 //-----------------------------------------------------------------------------

+ 4 - 0
src/renderers/gl/GLRenderer.h

@@ -39,6 +39,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "MallocAllocator.h"
 #include "Resource.h"
 
+#include "GLXContext.h"
+
 namespace crown
 {
 
@@ -208,6 +210,8 @@ private:
 
 private:
 
+	GLContext			m_context;
+
 	MallocAllocator		m_allocator;
 
 	// Matrices

+ 102 - 0
src/renderers/gl/GLXContext.cpp

@@ -0,0 +1,102 @@
+/*
+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 "GLXContext.h"
+#include "Assert.h"
+#include "Types.h"
+
+namespace crown
+{
+
+static Display* s_x11_display = NULL;
+static Window s_x11_window = None;
+
+//-----------------------------------------------------------------------------
+void set_x11_display_and_window(Display* dpy, Window win)
+{
+	s_x11_display = dpy;
+	s_x11_window = win;
+}
+
+//-----------------------------------------------------------------------------
+GLContext::GLContext() :
+	m_glx_context(NULL)
+{
+}
+
+//-----------------------------------------------------------------------------
+void GLContext::create_context()
+{
+	// Color index buffer not supported - deprecated
+	static int fb_attribs[] =
+	{
+		GLX_RENDER_TYPE,		static_cast<int>(GLX_RGBA_BIT),
+		GLX_DRAWABLE_TYPE,		static_cast<int>(GLX_WINDOW_BIT),
+		GLX_DOUBLEBUFFER,		static_cast<int>(True),
+		GLX_RED_SIZE,			8,
+		GLX_GREEN_SIZE,			8,
+		GLX_BLUE_SIZE,			8,
+		GLX_DEPTH_SIZE,			24,
+		static_cast<int>(None)
+	};
+
+	int fb_count;
+	GLXFBConfig* fb_config = glXChooseFBConfig(s_x11_display, DefaultScreen(s_x11_display), fb_attribs, &fb_count);
+	ce_assert(fb_config != NULL, "Unable to find a matching frame buffer configuration");
+
+	XVisualInfo* vi = glXGetVisualFromFBConfig(s_x11_display, fb_config[0]);
+	ce_assert(vi != NULL, "Unable to find a matching visual for frame buffer configuration.");
+
+	m_glx_context = glXCreateContext(s_x11_display, vi, 0, True);
+	ce_assert(m_glx_context != NULL, "Unable to create GLX context");
+
+	glXMakeCurrent(s_x11_display, s_x11_window, m_glx_context);
+
+	XFlush(s_x11_display);
+
+	XFree(vi);
+	XFree(fb_config);
+}
+
+//-----------------------------------------------------------------------------
+void GLContext::destroy_context()
+{
+	if (s_x11_display != NULL)
+	{
+		if (m_glx_context != None)
+		{
+			glXMakeCurrent(s_x11_display, None, None);
+			glXDestroyContext(s_x11_display, m_glx_context);
+		}
+	}
+}
+
+//-----------------------------------------------------------------------------
+void GLContext::swap_buffers()
+{
+	glXSwapBuffers(s_x11_display, s_x11_window);
+}
+
+} // namespace crown

+ 56 - 0
src/renderers/gl/GLXContext.h

@@ -0,0 +1,56 @@
+/*
+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 <X11/Xutil.h>
+#include <X11/Xatom.h>
+#include <X11/Xlib.h>
+#include <GL/glx.h>
+
+#include "Config.h"
+
+namespace crown
+{
+
+void set_x11_display_and_window(Display* dpy, Window win);
+
+class GLContext
+{
+public:
+
+					GLContext();
+
+	void			create_context();
+	void			destroy_context();
+
+	void			swap_buffers();
+
+private:
+
+	GLXContext		m_glx_context;
+};
+
+} // namespace crown