Browse Source

Add initial Window

Daniele Bartolini 10 năm trước cách đây
mục cha
commit
1f7eb71a62

+ 16 - 0
src/device.cpp

@@ -132,6 +132,7 @@ Device::Device(const DeviceOptions& opts)
 	, _input_manager(NULL)
 	, _unit_manager(NULL)
 	, _lua_environment(NULL)
+	, _window(NULL)
 	, _boot_package_id(uint64_t(0))
 	, _boot_script_id(uint64_t(0))
 	, _boot_package(NULL)
@@ -174,6 +175,15 @@ void Device::init()
 	_bgfx_allocator = CE_NEW(_allocator, BgfxAllocator)(default_allocator());
 	_bgfx_callback  = CE_NEW(_allocator, BgfxCallback)();
 
+	_window = Window::create(_allocator);
+	_window->open(_device_options.window_x()
+		, _device_options.window_y()
+		, _device_options.window_width()
+		, _device_options.window_height()
+		, _device_options.parent_window()
+		);
+	_window->bgfx_setup();
+
 	bgfx::init(bgfx::RendererType::Count
 		, BGFX_PCI_ID_NONE
 		, 0
@@ -230,6 +240,7 @@ void Device::shutdown()
 	CE_DELETE(_allocator, _bundle_filesystem);
 
 	bgfx::shutdown();
+	Window::destroy(_allocator, *_window);
 	CE_DELETE(_allocator, _bgfx_callback);
 	CE_DELETE(_allocator, _bgfx_allocator);
 
@@ -406,6 +417,11 @@ UnitManager* Device::unit_manager()
 	return _unit_manager;
 }
 
+Window* Device::window()
+{
+	return _window;
+}
+
 void Device::read_config()
 {
 	TempAllocator4096 ta;

+ 5 - 0
src/device.h

@@ -18,6 +18,7 @@
 #include "resource_types.h"
 #include "string_id.h"
 #include "types.h"
+#include "window.h"
 #include "world_types.h"
 
 namespace crown
@@ -47,6 +48,7 @@ class Device
 	InputManager* _input_manager;
 	UnitManager* _unit_manager;
 	LuaEnvironment* _lua_environment;
+	Window* _window;
 
 	StringId64 _boot_package_id;
 	StringId64 _boot_script_id;
@@ -165,6 +167,9 @@ public:
 	/// Returns the unit manager.
 	UnitManager* unit_manager();
 
+	/// Returns the main window.
+	Window* window();
+
 private:
 
 	// Disable copying

+ 16 - 33
src/lua/lua_api.cpp

@@ -2617,69 +2617,54 @@ static int gui_draw_text(lua_State* L)
 static int window_show(lua_State* L)
 {
 	LuaStack stack(L);
-	// window()->show();
+	device()->window()->show();
 	return 0;
 }
 
 static int window_hide(lua_State* L)
 {
 	LuaStack stack(L);
-	// window()->hide();
+	device()->window()->hide();
 	return 0;
 }
 
 static int window_resize(lua_State* L)
 {
 	LuaStack stack(L);
-	// window()->resize(stack.get_int(1), stack.get_int(2));
+	device()->window()->resize(stack.get_int(1), stack.get_int(2));
 	return 0;
 }
 
 static int window_move(lua_State* L)
 {
 	LuaStack stack(L);
-	// window()->move(stack.get_int(1), stack.get_int(2));
+	device()->window()->move(stack.get_int(1), stack.get_int(2));
 	return 0;
 }
 
 static int window_minimize(lua_State* /*L*/)
 {
-	// window()->minimize();
+	device()->window()->minimize();
 	return 0;
 }
 
 static int window_restore(lua_State* /*L*/)
 {
-	// window()->restore();
-	return 0;
-}
-
-static int window_is_resizable(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_bool(/*window()->is_resizable()*/ false);
-	return 1;
-}
-
-static int window_set_resizable(lua_State* L)
-{
-	LuaStack stack(L);
-	// window()->set_resizable(stack.get_bool(1));
+	device()->window()->restore();
 	return 0;
 }
 
 static int window_title(lua_State* L)
 {
 	LuaStack stack(L);
-	// stack.push_string(window()->title());
-	stack.push_string("");
+	stack.push_string(device()->window()->title());
 	return 1;
 }
 
 static int window_set_title(lua_State* L)
 {
 	LuaStack stack(L);
-	// window()->set_title(stack.get_string(1));
+	device()->window()->set_title(stack.get_string(1));
 	return 0;
 }
 
@@ -3095,16 +3080,14 @@ void load_api(LuaEnvironment& env)
 	env.load_module_function("Gui", "draw_image_uv",  gui_draw_image_uv);
 	env.load_module_function("Gui", "draw_text",      gui_draw_text);
 
-	env.load_module_function("Window", "show",          window_show);
-	env.load_module_function("Window", "hide",          window_hide);
-	env.load_module_function("Window", "resize",        window_resize);
-	env.load_module_function("Window", "move",          window_move);
-	env.load_module_function("Window", "minimize",      window_minimize);
-	env.load_module_function("Window", "restore",       window_restore);
-	env.load_module_function("Window", "is_resizable",  window_is_resizable);
-	env.load_module_function("Window", "set_resizable", window_set_resizable);
-	env.load_module_function("Window", "title",         window_title);
-	env.load_module_function("Window", "set_title",     window_set_title);
+	env.load_module_function("Window", "show",      window_show);
+	env.load_module_function("Window", "hide",      window_hide);
+	env.load_module_function("Window", "resize",    window_resize);
+	env.load_module_function("Window", "move",      window_move);
+	env.load_module_function("Window", "minimize",  window_minimize);
+	env.load_module_function("Window", "restore",   window_restore);
+	env.load_module_function("Window", "title",     window_title);
+	env.load_module_function("Window", "set_title", window_set_title);
 }
 
 } // namespace crown

+ 14 - 70
src/main/main_linux.cpp

@@ -7,19 +7,18 @@
 
 #if CROWN_PLATFORM_LINUX
 
-#include "device.h"
-#include "os_event_queue.h"
-#include "thread.h"
-#include "command_line.h"
 #include "bundle_compiler.h"
+#include "command_line.h"
 #include "console_server.h"
 #include "device.h"
+#include "os_event_queue.h"
+#include "thread.h"
 #include <stdlib.h>
-#include <X11/Xutil.h>
+#include <X11/extensions/Xrandr.h>
 #include <X11/Xatom.h>
-#include <X11/Xlib.h>
 #include <X11/XKBlib.h>
-#include <X11/extensions/Xrandr.h>
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
 #include <bgfx/bgfxplatform.h>
 
 namespace crown
@@ -345,12 +344,12 @@ int32_t func(void* data)
 	return EXIT_SUCCESS;
 }
 
+extern void set_x11_display(Display*);
+
 struct LinuxDevice
 {
 	LinuxDevice()
 		: _x11_display(NULL)
-		, _x11_window(None)
-		, _x11_hidden_cursor(None)
 		, _screen_config(NULL)
 		, _x11_detectable_autorepeat(false)
 	{
@@ -366,71 +365,18 @@ struct LinuxDevice
 		_x11_display = XOpenDisplay(NULL);
 		CE_ASSERT(_x11_display != NULL, "XOpenDisplay: error");
 
-		int screen = DefaultScreen(_x11_display);
-		int depth = DefaultDepth(_x11_display, screen);
-		Visual* visual = DefaultVisual(_x11_display, screen);
+		crown::set_x11_display(_x11_display);
 
-		Window root_window = RootWindow(_x11_display, screen);
-		uint32_t pw = opts->parent_window();
-		Window parent_window = (pw == 0) ? root_window : (Window)pw;
-
-		// Create main window
-		XSetWindowAttributes win_attribs;
-		win_attribs.background_pixmap = 0;
-		win_attribs.border_pixel = 0;
-		win_attribs.event_mask = FocusChangeMask
-			| StructureNotifyMask
-			;
-
-		if (!opts->parent_window())
-		{
-			win_attribs.event_mask |= KeyPressMask
-				| KeyReleaseMask
-				| ButtonPressMask
-				| ButtonReleaseMask
-				| PointerMotionMask
-				| EnterWindowMask
-				;
-		}
-
-		_x11_window = XCreateWindow(_x11_display
-			, parent_window
-			, opts->window_x()
-			, opts->window_y()
-			, opts->window_width()
-			, opts->window_height()
-			, 0
-			, depth
-			, InputOutput
-			, visual
-			, CWBorderPixel | CWEventMask
-			, &win_attribs
-			);
-		CE_ASSERT(_x11_window != None, "XCreateWindow: error");
-
-		_wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
-		XSetWMProtocols(_x11_display, _x11_window, &_wm_delete_message, 1);
+		::Window root_window = RootWindow(_x11_display, DefaultScreen(_x11_display));
 
 		// Do we have detectable autorepeat?
 		Bool detectable;
 		_x11_detectable_autorepeat = (bool)XkbSetDetectableAutoRepeat(_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(_x11_display, screen);
-		XAllocNamedColor(_x11_display, colormap, "black", &black, &dummy);
-		bm_no = XCreateBitmapFromData(_x11_display, _x11_window, no_data, 8, 8);
-		_x11_hidden_cursor = XCreatePixmapCursor(_x11_display, bm_no, bm_no, &black, &black, 0, 0);
-
-		bgfx::x11SetDisplayWindow(_x11_display, _x11_window);
-		XMapRaised(_x11_display, _x11_window);
+		_wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
 
 		// Save screen configuration
-		_screen_config = XRRGetScreenInfo(_x11_display, parent_window);
+		_screen_config = XRRGetScreenInfo(_x11_display, root_window);
 
 		Rotation rr_old_rot;
 		const SizeID rr_old_sizeid = XRRConfigCurrentConfiguration(_screen_config, &rr_old_rot);
@@ -469,7 +415,6 @@ struct LinuxDevice
 		}
 		XRRFreeScreenConfigInfo(_screen_config);
 
-		XDestroyWindow(_x11_display, _x11_window);
 		XCloseDisplay(_x11_display);
 		return EXIT_SUCCESS;
 	}
@@ -493,8 +438,9 @@ struct LinuxDevice
 				case ClientMessage:
 				{
 					if ((Atom)event.xclient.data.l[0] == _wm_delete_message)
+					{
 						_queue.push_exit_event(0);
-
+					}
 					break;
 				}
 				case ConfigureNotify:
@@ -569,8 +515,6 @@ struct LinuxDevice
 public:
 
 	Display* _x11_display;
-	Window _x11_window;
-	Cursor _x11_hidden_cursor;
 	Atom _wm_delete_message;
 	XRRScreenConfiguration* _screen_config;
 	bool _x11_detectable_autorepeat;

+ 60 - 0
src/main/window.h

@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#pragma once
+
+#include "memory_types.h"
+#include "types.h"
+
+namespace crown
+{
+
+/// Window
+///
+/// @ingroup Device
+class Window
+{
+public:
+
+	/// Opens the window.
+	virtual void open(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t parent) = 0;
+
+	/// Closes the window.
+	virtual void close() = 0;
+
+	/// Shows the window.
+	virtual void show() = 0;
+
+	/// Hides the window.
+	virtual void hide() = 0;
+
+	/// Resizes the window to @a width and @a height.
+	virtual void resize(uint16_t width, uint16_t height) = 0;
+
+	/// Moves the window to @a x and @a y.
+	virtual void move(uint16_t x, uint16_t y) = 0;
+
+	/// Minimizes the window.
+	virtual void minimize() = 0;
+
+	/// Restores the window.
+	virtual void restore() = 0;
+
+	/// Returns the title of the window.
+	virtual const char* title() = 0;
+
+	/// Sets the title of the window.
+	virtual void set_title (const char* title) = 0;
+
+	/// Returns the native window handle.
+	virtual void* handle() = 0;
+
+	virtual void bgfx_setup() = 0;
+
+	static Window* create(Allocator& a);
+	static void destroy(Allocator& a, Window& w);
+};
+
+} // namespace crown

+ 85 - 0
src/main/window_android.cpp

@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "platform.h"
+
+#if CROWN_PLATFORM_ANDROID
+
+#include "memory.h"
+#include "window.h"
+
+namespace crown
+{
+
+class WindowAndroid : public Window
+{
+public:
+
+	WindowAndroid()
+	{
+	}
+
+	void open(uint16_t /*x*/, uint16_t /*y*/, uint16_t /*width*/, uint16_t /*height*/, uint32_t /*parent*/)
+	{
+	}
+
+	void close()
+	{
+	}
+
+	void bgfx_setup()
+	{
+	}
+
+	void show()
+	{
+	}
+
+	void hide()
+	{
+	}
+
+	void resize(uint16_t /*width*/, uint16_t /*height*/)
+	{
+	}
+
+	void move(uint16_t /*x*/, uint16_t /*y*/)
+	{
+	}
+
+	void minimize()
+	{
+	}
+
+	void restore()
+	{
+	}
+
+	const char* title()
+	{
+	}
+
+	void set_title (const char* /*title*/)
+	{
+	}
+
+	void* handle()
+	{
+	}
+};
+
+Window* Window::create(Allocator& a)
+{
+	return CE_NEW(a, WindowAndroid)();
+}
+
+void Window::destroy(Allocator& a, Window& w)
+{
+	CE_DELETE(a, &w);
+}
+
+} // namespace crown
+
+#endif // CROWN_PLATFORM_ANDROID

+ 168 - 0
src/main/window_linux.cpp

@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "platform.h"
+
+#if CROWN_PLATFORM_LINUX
+
+#include "memory.h"
+#include "window.h"
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <bgfx/bgfxplatform.h>
+
+namespace crown
+{
+
+Display* _x11_display = NULL;
+
+void set_x11_display(Display* dpy)
+{
+	_x11_display = dpy;
+}
+
+class WindowX11 : public Window
+{
+	::Window _x11_window;
+	Cursor _x11_hidden_cursor;
+	Atom _wm_delete_message;
+
+public:
+
+	WindowX11()
+		: _x11_window(None)
+		, _x11_hidden_cursor(None)
+	{
+	}
+
+	void open(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t parent)
+	{
+		int screen = DefaultScreen(_x11_display);
+		int depth = DefaultDepth(_x11_display, screen);
+		Visual* visual = DefaultVisual(_x11_display, screen);
+
+		::Window root_window = RootWindow(_x11_display, screen);
+		::Window parent_window = (parent == 0) ? root_window : (::Window)parent;
+
+		// Create main window
+		XSetWindowAttributes win_attribs;
+		win_attribs.background_pixmap = 0;
+		win_attribs.border_pixel = 0;
+		win_attribs.event_mask = FocusChangeMask
+			| StructureNotifyMask
+			;
+
+		if (!parent)
+		{
+			win_attribs.event_mask |= KeyPressMask
+				| KeyReleaseMask
+				| ButtonPressMask
+				| ButtonReleaseMask
+				| PointerMotionMask
+				| EnterWindowMask
+				;
+		}
+
+		_x11_window = XCreateWindow(_x11_display
+			, parent_window
+			, x
+			, y
+			, width
+			, height
+			, 0
+			, depth
+			, InputOutput
+			, visual
+			, CWBorderPixel | CWEventMask
+			, &win_attribs
+			);
+		CE_ASSERT(_x11_window != None, "XCreateWindow: error");
+
+		// 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(_x11_display, screen);
+		XAllocNamedColor(_x11_display, colormap, "black", &black, &dummy);
+		bm_no = XCreateBitmapFromData(_x11_display, _x11_window, no_data, 8, 8);
+		_x11_hidden_cursor = XCreatePixmapCursor(_x11_display, bm_no, bm_no, &black, &black, 0, 0);
+
+		_wm_delete_message = XInternAtom(_x11_display, "WM_DELETE_WINDOW", False);
+		XSetWMProtocols(_x11_display, _x11_window, &_wm_delete_message, 1);
+
+		XMapRaised(_x11_display, _x11_window);
+	}
+
+	void close()
+	{
+		XDestroyWindow(_x11_display, _x11_window);
+	}
+
+	void bgfx_setup()
+	{
+		bgfx::x11SetDisplayWindow(_x11_display, _x11_window);
+	}
+
+	void show()
+	{
+
+	}
+
+	void hide()
+	{
+
+	}
+
+	void resize(uint16_t width, uint16_t height)
+	{
+
+	}
+
+	void move(uint16_t x, uint16_t y)
+	{
+
+	}
+
+	void minimize()
+	{
+
+	}
+
+	void restore()
+	{
+
+	}
+
+	const char* title()
+	{
+
+	}
+
+	void set_title (const char* /*title*/)
+	{
+
+	}
+
+	void* handle()
+	{
+		return (void*)(uintptr_t)_x11_window;
+	}
+};
+
+Window* Window::create(Allocator& a)
+{
+	return CE_NEW(a, WindowX11)();
+}
+
+void Window::destroy(Allocator& a, Window& w)
+{
+	CE_DELETE(a, &w);
+}
+
+} // namespace crown
+
+#endif // CROWN_PLATFORM_LINUX

+ 94 - 0
src/main/window_windows.cpp

@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "platform.h"
+
+#if CROWN_PLATFORM_WINDOWS
+
+#include "memory.h"
+#include "window.h"
+#include <bgfx/bgfxplatform.h>
+
+namespace crown
+{
+
+class WindowWin : public Window
+{
+public:
+
+	WindowWin()
+	{
+	}
+
+	void open(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t parent)
+	{
+	}
+
+	void close()
+	{
+	}
+
+	void bgfx_setup()
+	{
+	}
+
+	void show()
+	{
+
+	}
+
+	void hide()
+	{
+
+	}
+
+	void resize(uint16_t width, uint16_t height)
+	{
+
+	}
+
+	void move(uint16_t x, uint16_t y)
+	{
+
+	}
+
+	void minimize()
+	{
+
+	}
+
+	void restore()
+	{
+
+	}
+
+	const char* title()
+	{
+
+	}
+
+	void set_title (const char* /*title*/)
+	{
+
+	}
+
+	void* handle()
+	{
+	}
+};
+
+Window* Window::create(Allocator& a)
+{
+	return CE_NEW(a, WindowWin)();
+}
+
+void Window::destroy(Allocator& a, Window& w)
+{
+	CE_DELETE(a, &w);
+}
+
+} // namespace crown
+
+#endif // CROWN_PLATFORM_WINDOWS