瀏覽代碼

Use namespaces instead of static methods

Daniele Bartolini 10 年之前
父節點
當前提交
7c5eefca72

+ 4 - 4
src/device/device.cpp

@@ -182,8 +182,8 @@ void Device::init()
 	_bgfx_allocator = CE_NEW(_allocator, BgfxAllocator)(default_allocator());
 	_bgfx_callback  = CE_NEW(_allocator, BgfxCallback)();
 
-	_display = Display::create(_allocator);
-	_window = Window::create(_allocator);
+	_display = display::create(_allocator);
+	_window = window::create(_allocator);
 	_window->open(_device_options.window_x()
 		, _device_options.window_y()
 		, _config_window_w
@@ -248,8 +248,8 @@ void Device::shutdown()
 	CE_DELETE(_allocator, _bundle_filesystem);
 
 	bgfx::shutdown();
-	Window::destroy(_allocator, *_window);
-	Display::destroy(_allocator, *_display);
+	window::destroy(_allocator, *_window);
+	display::destroy(_allocator, *_display);
 	CE_DELETE(_allocator, _bgfx_callback);
 	CE_DELETE(_allocator, _bgfx_allocator);
 

+ 13 - 6
src/device/display.h

@@ -23,10 +23,8 @@ struct DisplayMode
 /// Display interface.
 ///
 /// @ingroup Device
-class Display
+struct Display
 {
-public:
-
 	/// Fills @a modes with all available display modes.
 	virtual void modes(Array<DisplayMode>& modes) = 0;
 
@@ -35,9 +33,18 @@ public:
 	/// @note
 	/// The initial display mode is automatically reset when the program terminates.
 	virtual void set_mode(u32 id) = 0;
-
-	static Display* create(Allocator& a);
-	static void destroy(Allocator& a, Display& d);
 };
 
+/// Functions to manipulate Display.
+///
+/// @ingroup Device
+namespace display
+{
+	/// Creates a new display.
+	Display* create(Allocator& a);
+
+	/// Destroys the display @a d.
+	void destroy(Allocator& a, Display& d);
+} // namespace display
+
 } // namespace crown

+ 51 - 48
src/device/input_device.cpp

@@ -119,55 +119,58 @@ void InputDevice::update()
 	memcpy(_last_state, _state, sizeof(u8)*_num_buttons);
 }
 
-InputDevice* InputDevice::create(Allocator& a, const char* name, u8 num_buttons, u8 num_axes, const char** button_names, const char** axis_names)
+namespace input_device
 {
-	const u32 size = 0
-		+ sizeof(InputDevice)
-		+ sizeof(u8)*num_buttons*2
-		+ sizeof(Vector3)*num_axes
-		+ sizeof(char*)*num_buttons
-		+ sizeof(char*)*num_axes
-		+ sizeof(StringId32)*num_buttons
-		+ sizeof(StringId32)*num_axes
-		+ strlen32(name) + 1
-		;
-
-	InputDevice* id = (InputDevice*)a.allocate(size);
-
-	id->_connected = false;
-	id->_num_buttons = num_buttons;
-	id->_num_axes = num_axes;
-	id->_last_button = 0;
-
-	id->_last_state = (u8*)&id[1];
-	id->_state = (u8*)(id->_last_state + num_buttons);
-	id->_axis = (Vector3*)(id->_state + num_buttons);
-	id->_button_name = (const char**)(id->_axis + num_axes);
-	id->_axis_name = (const char**)(id->_button_name + num_buttons);
-	id->_button_hash = (StringId32*)(id->_axis_name + num_axes);
-	id->_axis_hash = (StringId32*)(id->_button_hash + num_buttons);
-	id->_name = (char*)(id->_axis_hash + num_axes);
-
-	memset(id->_last_state, 0, sizeof(u8)*num_buttons);
-	memset(id->_state, 0, sizeof(u8)*num_buttons);
-	memset(id->_axis, 0, sizeof(Vector3)*num_axes);
-	memcpy(id->_button_name, button_names, sizeof(const char*)*num_buttons);
-	memcpy(id->_axis_name, axis_names, sizeof(const char*)*num_axes);
-
-	for (u32 i = 0; i < num_buttons; ++i)
-		id->_button_hash[i] = StringId32(button_names[i]);
-
-	for (u32 i = 0; i < num_axes; ++i)
-		id->_axis_hash[i] = StringId32(axis_names[i]);
-
-	strcpy(id->_name, name);
-
-	return id;
-}
+	InputDevice* create(Allocator& a, const char* name, u8 num_buttons, u8 num_axes, const char** button_names, const char** axis_names)
+	{
+		const u32 size = 0
+			+ sizeof(InputDevice)
+			+ sizeof(u8)*num_buttons*2
+			+ sizeof(Vector3)*num_axes
+			+ sizeof(char*)*num_buttons
+			+ sizeof(char*)*num_axes
+			+ sizeof(StringId32)*num_buttons
+			+ sizeof(StringId32)*num_axes
+			+ strlen32(name) + 1
+			;
+
+		InputDevice* id = (InputDevice*)a.allocate(size);
+
+		id->_connected = false;
+		id->_num_buttons = num_buttons;
+		id->_num_axes = num_axes;
+		id->_last_button = 0;
+
+		id->_last_state = (u8*)&id[1];
+		id->_state = (u8*)(id->_last_state + num_buttons);
+		id->_axis = (Vector3*)(id->_state + num_buttons);
+		id->_button_name = (const char**)(id->_axis + num_axes);
+		id->_axis_name = (const char**)(id->_button_name + num_buttons);
+		id->_button_hash = (StringId32*)(id->_axis_name + num_axes);
+		id->_axis_hash = (StringId32*)(id->_button_hash + num_buttons);
+		id->_name = (char*)(id->_axis_hash + num_axes);
+
+		memset(id->_last_state, 0, sizeof(u8)*num_buttons);
+		memset(id->_state, 0, sizeof(u8)*num_buttons);
+		memset(id->_axis, 0, sizeof(Vector3)*num_axes);
+		memcpy(id->_button_name, button_names, sizeof(const char*)*num_buttons);
+		memcpy(id->_axis_name, axis_names, sizeof(const char*)*num_axes);
+
+		for (u32 i = 0; i < num_buttons; ++i)
+			id->_button_hash[i] = StringId32(button_names[i]);
+
+		for (u32 i = 0; i < num_axes; ++i)
+			id->_axis_hash[i] = StringId32(axis_names[i]);
+
+		strcpy(id->_name, name);
+
+		return id;
+	}
 
-void InputDevice::destroy(Allocator& a, InputDevice& id)
-{
-	a.deallocate(&id);
-}
+	void destroy(Allocator& a, InputDevice& id)
+	{
+		a.deallocate(&id);
+	}
+} // namespace input_device
 
 } // namespace crown

+ 13 - 6
src/device/input_device.h

@@ -15,7 +15,7 @@ namespace crown
 /// Represents a generic input device.
 ///
 /// @ingroup Input
-class InputDevice
+struct InputDevice
 {
 	bool _connected;
 	u8 _num_buttons;
@@ -31,8 +31,6 @@ class InputDevice
 	StringId32* _axis_hash;    // num_axes
 	char* _name;               // strlen32(name) + 1
 
-public:
-
 	/// Returns the name of the input device.
 	const char* name() const;
 
@@ -79,9 +77,18 @@ public:
 	void set_axis(u8 i, const Vector3& value);
 
 	void update();
-
-	static InputDevice* create(Allocator& a, const char* name, u8 num_buttons, u8 num_axes, const char** button_names, const char** axis_names);
-	static void destroy(Allocator& a, InputDevice& id);
 };
 
+/// Functions to manipulate InputDevice.
+///
+/// @ingroup Input
+namespace input_device
+{
+	/// Creates a new input device.
+	InputDevice* create(Allocator& a, const char* name, u8 num_buttons, u8 num_axes, const char** button_names, const char** axis_names);
+
+	/// Destroys the input device @a id.
+	void destroy(Allocator& a, InputDevice& id);
+} // namespace input_device
+
 } // namespace crown

+ 8 - 8
src/device/input_manager.cpp

@@ -164,21 +164,21 @@ InputManager::InputManager(Allocator& a)
 	, _mouse(NULL)
 	, _touch(NULL)
 {
-	_keyboard = InputDevice::create(*_allocator
+	_keyboard = input_device::create(*_allocator
 		, "Keyboard"
 		, KeyboardButton::COUNT
 		, 0
 		, s_keyboard_button_names
 		, NULL
 		);
-	_mouse = InputDevice::create(*_allocator
+	_mouse = input_device::create(*_allocator
 		, "Mouse"
 		, MouseButton::COUNT
 		, MouseAxis::COUNT
 		, s_mouse_button_names
 		, s_mouse_axis_names
 		);
-	_touch = InputDevice::create(*_allocator
+	_touch = input_device::create(*_allocator
 		, "Touch"
 		, TouchButton::COUNT
 		, 0
@@ -188,7 +188,7 @@ InputManager::InputManager(Allocator& a)
 
 	for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
 	{
-		_joypad[i] = InputDevice::create(*_allocator
+		_joypad[i] = input_device::create(*_allocator
 			, "Joypad"
 			, JoypadButton::COUNT
 			, JoypadAxis::COUNT
@@ -205,11 +205,11 @@ InputManager::InputManager(Allocator& a)
 InputManager::~InputManager()
 {
 	for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
-		InputDevice::destroy(*_allocator, *_joypad[i]);
+		input_device::destroy(*_allocator, *_joypad[i]);
 
-	InputDevice::destroy(*_allocator, *_touch);
-	InputDevice::destroy(*_allocator, *_mouse);
-	InputDevice::destroy(*_allocator, *_keyboard);
+	input_device::destroy(*_allocator, *_touch);
+	input_device::destroy(*_allocator, *_mouse);
+	input_device::destroy(*_allocator, *_keyboard);
 }
 
 InputDevice* InputManager::keyboard()

+ 1 - 1
src/device/input_types.h

@@ -9,7 +9,7 @@
 namespace crown
 {
 class InputManager;
-class InputDevice;
+struct InputDevice;
 
 /// Enumerates keyboard buttons.
 ///

+ 13 - 6
src/device/window.h

@@ -13,10 +13,8 @@ namespace crown
 /// Window interface.
 ///
 /// @ingroup Device
-class Window
+struct Window
 {
-public:
-
 	/// Opens the window.
 	virtual void open(u16 x, u16 y, u16 width, u16 height, u32 parent) = 0;
 
@@ -54,9 +52,18 @@ public:
 	virtual void show_cursor(bool show) = 0;
 
 	virtual void bgfx_setup() = 0;
-
-	static Window* create(Allocator& a);
-	static void destroy(Allocator& a, Window& w);
 };
 
+/// Functions to manipulate Window
+///
+/// @ingroup Device
+namespace window
+{
+	/// Creates a new window.
+	Window* create(Allocator& a);
+
+	/// Destroys the window @a w.
+	void destroy(Allocator& a, Window& w);
+} // namespace window
+
 } // namespace crown

+ 22 - 18
src/main/main_android.cpp

@@ -196,10 +196,8 @@ public:
 	MainThreadArgs _margs;
 };
 
-class WindowAndroid : public Window
+struct WindowAndroid : public Window
 {
-public:
-
 	WindowAndroid()
 	{
 	}
@@ -259,17 +257,20 @@ public:
 	}
 };
 
-Window* Window::create(Allocator& a)
+namespace window
 {
-	return CE_NEW(a, WindowAndroid)();
-}
+	Window* create(Allocator& a)
+	{
+		return CE_NEW(a, WindowAndroid)();
+	}
 
-void Window::destroy(Allocator& a, Window& w)
-{
-	CE_DELETE(a, &w);
-}
+	void destroy(Allocator& a, Window& w)
+	{
+		CE_DELETE(a, &w);
+	}
+} // namespace window
 
-class DisplayAndroid : public Display
+struct DisplayAndroid : public Display
 {
 	void modes(Array<DisplayMode>& /*modes*/)
 	{
@@ -280,15 +281,18 @@ class DisplayAndroid : public Display
 	}
 };
 
-Display* Display::create(Allocator& a)
+namespace display
 {
-	return CE_NEW(a, DisplayAndroid)();
-}
+	Display* create(Allocator& a)
+	{
+		return CE_NEW(a, DisplayAndroid)();
+	}
 
-void Display::destroy(Allocator& a, Display& d)
-{
-	CE_DELETE(a, &d);
-}
+	void destroy(Allocator& a, Display& d)
+	{
+		CE_DELETE(a, &d);
+	}
+} // namespace display
 
 static AndroidDevice s_advc;
 

+ 22 - 18
src/main/main_linux.cpp

@@ -477,7 +477,7 @@ public:
 
 static LinuxDevice s_ldvc;
 
-class WindowX11 : public Window
+struct WindowX11 : public Window
 {
 	::Display* _x11_display;
 	::Window _x11_window;
@@ -622,23 +622,24 @@ public:
 	}
 };
 
-Window* Window::create(Allocator& a)
+namespace window
 {
-	return CE_NEW(a, WindowX11)();
-}
+	Window* create(Allocator& a)
+	{
+		return CE_NEW(a, WindowX11)();
+	}
 
-void Window::destroy(Allocator& a, Window& w)
-{
-	CE_DELETE(a, &w);
-}
+	void destroy(Allocator& a, Window& w)
+	{
+		CE_DELETE(a, &w);
+	}
+} // namespace window
 
-class DisplayXRandr : public Display
+struct DisplayXRandr : public Display
 {
 	::Display* _x11_display;
 	XRRScreenConfiguration* _screen_config;
 
-public:
-
 	DisplayXRandr()
 		: _x11_display(NULL)
 		, _screen_config(NULL)
@@ -695,15 +696,18 @@ public:
 	// }
 };
 
-Display* Display::create(Allocator& a)
+namespace display
 {
-	return CE_NEW(a, DisplayXRandr)();
-}
+	Display* create(Allocator& a)
+	{
+		return CE_NEW(a, DisplayXRandr)();
+	}
 
-void Display::destroy(Allocator& a, Display& d)
-{
-	CE_DELETE(a, &d);
-}
+	void destroy(Allocator& a, Display& d)
+	{
+		CE_DELETE(a, &d);
+	}
+} // namespace display
 
 bool next_event(OsEvent& ev)
 {

+ 22 - 18
src/main/main_windows.cpp

@@ -473,7 +473,7 @@ LRESULT CALLBACK WindowsDevice::window_proc(HWND hwnd, UINT id, WPARAM wparam, L
 	return s_wdvc.pump_events(hwnd, id, wparam, lparam);
 }
 
-class WindowWin : public Window
+struct WindowWin : public Window
 {
 	HWND _hwnd;
 	u16 _x;
@@ -481,8 +481,6 @@ class WindowWin : public Window
 	u16 _width;
 	u16 _height;
 
-public:
-
 	WindowWin()
 		: _hwnd(NULL)
 		, _x(0)
@@ -569,17 +567,20 @@ public:
 	}
 };
 
-Window* Window::create(Allocator& a)
+namespace window
 {
-	return CE_NEW(a, WindowWin)();
-}
+	Window* create(Allocator& a)
+	{
+		return CE_NEW(a, WindowWin)();
+	}
 
-void Window::destroy(Allocator& a, Window& w)
-{
-	CE_DELETE(a, &w);
-}
+	void destroy(Allocator& a, Window& w)
+	{
+		CE_DELETE(a, &w);
+	}
+} // namespace window
 
-class DisplayWin : public Display
+struct DisplayWin : public Display
 {
 	void modes(Array<DisplayMode>& /*modes*/)
 	{
@@ -590,15 +591,18 @@ class DisplayWin : public Display
 	}
 };
 
-Display* Display::create(Allocator& a)
+namespace display
 {
-	return CE_NEW(a, DisplayWin)();
-}
+	Display* create(Allocator& a)
+	{
+		return CE_NEW(a, DisplayWin)();
+	}
 
-void Display::destroy(Allocator& a, Display& d)
-{
-	CE_DELETE(a, &d);
-}
+	void destroy(Allocator& a, Display& d)
+	{
+		CE_DELETE(a, &d);
+	}
+} // namespace display
 
 bool next_event(OsEvent& ev)
 {