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

Move InputManager instantiation into Device and rename GetDevice() to device()

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

+ 8 - 8
samples/terrain/terrain.cpp

@@ -10,14 +10,14 @@ public:
 
 	WndCtrl()
 	{
-		get_input_manager()->register_keyboard_listener(this);
+		device()->input_manager()->register_keyboard_listener(this);
 	}
 
 	void KeyReleased(const KeyboardEvent& event)
 	{
 		if (event.key == KC_ESCAPE)
 		{
-			GetDevice()->stop();
+			device()->stop();
 		}
 	}
 };
@@ -34,8 +34,8 @@ public:
 		optShowCrate(true),
 		optShowTerrain(true)
 	{
-		get_input_manager()->register_keyboard_listener(this);
-		get_input_manager()->register_mouse_listener(this);
+		device()->input_manager()->register_keyboard_listener(this);
+		device()->input_manager()->register_mouse_listener(this);
 		mouseRightPressed = false;
 		mouseLeftPressed = false;
 	}
@@ -123,7 +123,7 @@ public:
 		
 	void OnLoad()
 	{
-		crown::Renderer* renderer = crown::GetDevice()->renderer();
+		crown::Renderer* renderer = crown::device()->renderer();
 
 		renderer->set_clear_color(Color4::LIGHTBLUE);
 		
@@ -167,7 +167,7 @@ public:
 
 	void RenderScene()
 	{
-		Renderer* renderer = GetDevice()->renderer();
+		Renderer* renderer = device()->renderer();
 		
 		system->set_view_by_cursor();
 		system->camera_render();
@@ -254,7 +254,7 @@ int main(int argc, char** argv)
 	os::create_render_window(0, 0, 1000, 625, false);
 	os::init_input();
 
-	Device* engine = GetDevice();
+	Device* engine = device();
 
 	if (!engine->init(argc, argv))
 	{
@@ -269,7 +269,7 @@ int main(int argc, char** argv)
 	{
 		os::event_loop();
 
-		get_input_manager()->event_loop();
+		device()->input_manager()->event_loop();
 
 		engine->renderer()->begin_frame();
 			mainScene.RenderScene();

+ 2 - 2
src/Camera.cpp

@@ -154,8 +154,8 @@ const Frustum& Camera::GetFrustum() const
 
 void Camera::Render()
 {
-	GetDevice()->renderer()->set_matrix(MT_PROJECTION, mProjection);
-	GetDevice()->renderer()->set_matrix(MT_VIEW, mView);
+	device()->renderer()->set_matrix(MT_PROJECTION, mProjection);
+	device()->renderer()->set_matrix(MT_VIEW, mView);
 }
 
 void Camera::UpdateProjectionMatrix()

+ 25 - 14
src/Device.cpp

@@ -50,6 +50,7 @@ Device::Device() :
 	m_is_init(false),
 	m_is_running(false),
 
+	m_input_manager(NULL),
 	m_renderer(NULL)
 {
 	string::strcpy(m_preferred_root_path, string::EMPTY);
@@ -81,17 +82,16 @@ bool Device::init(int argc, char** argv)
 	// Set the root path
 	// GetFilesystem()->Init(m_preferred_root_path.c_str(), m_preferred_user_path.c_str());
 
+	m_input_manager = new InputManager();
+
 	// Create the renderer
-	if (m_renderer == NULL)
-	{
-		// FIXME FIXME FIXME
-		// #ifdef CROWN_BUILD_OPENGL
-		 	m_renderer = new GLRenderer();
-			Log::I("Using GLRenderer.");
-		// #elif defined CROWN_BUILD_OPENGLES
-		//	m_renderer = new GLESRenderer();
-		// #endif
-	}
+	// FIXME FIXME FIXME
+	// #ifdef CROWN_BUILD_OPENGL
+	 	m_renderer = new GLRenderer();
+		Log::I("Using GLRenderer.");
+	// #elif defined CROWN_BUILD_OPENGLES
+	//	m_renderer = new GLESRenderer();
+	// #endif
 
 	m_is_init = true;
 
@@ -111,6 +111,11 @@ void Device::shutdown()
 		return;
 	}
 
+	if (m_input_manager)
+	{
+		delete m_input_manager;
+	}
+
 	Log::I("Releasing Renderer...");
 
 	if (m_renderer)
@@ -127,6 +132,12 @@ bool Device::is_init() const
 	return m_is_init;
 }
 
+//-----------------------------------------------------------------------------
+InputManager* Device::input_manager()
+{
+	return m_input_manager;
+}
+
 //-----------------------------------------------------------------------------
 Renderer* Device::renderer()
 {
@@ -166,7 +177,7 @@ bool Device::is_running() const
 //-----------------------------------------------------------------------------
 void Device::frame()
 {
-	get_input_manager()->event_loop();
+	m_input_manager->event_loop();
 
 	m_renderer->begin_frame();
 	m_renderer->end_frame();
@@ -289,10 +300,10 @@ void Device::print_help_message()
 	os::printf("  --fullscreen          Start in fullscreen.\n");
 }
 
-Device device;
-Device* GetDevice()
+Device g_device;
+Device* device()
 {
-	return &device;
+	return &g_device;
 }
 
 } // namespace crown

+ 3 - 1
src/Device.h

@@ -53,6 +53,7 @@ public:
 
 	void					frame();
 
+	InputManager*			input_manager();
 	Renderer*				renderer();
 
 private:
@@ -74,6 +75,7 @@ private:
 	bool					m_is_running	: 1;
 
 	// Subsystems
+	InputManager*			m_input_manager;
 	Renderer*				m_renderer;
 
 private:
@@ -83,7 +85,7 @@ private:
 	Device& operator=(const Device&);
 };
 
-Device* GetDevice();
+Device* device();
 
 } // namespace crown
 

+ 35 - 9
src/FPSSystem.cpp

@@ -1,3 +1,28 @@
+/*
+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 "MovableCamera.h"
 #include "FPSSystem.h"
@@ -5,6 +30,7 @@
 #include "Vec3.h"
 #include "Mat3.h"
 #include "OS.h"
+#include "Device.h"
 
 namespace crown
 {
@@ -19,10 +45,10 @@ FPSSystem::FPSSystem(MovableCamera* camera) :
 	m_down_pressed(false),
 	m_left_pressed(false)
 {
-	get_input_manager()->register_keyboard_listener(this);
-	get_input_manager()->register_accelerometer_listener(this);
-	//get_input_manager()->register_mouse_listener(this);
-	get_input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
+	device()->input_manager()->register_keyboard_listener(this);
+	device()->input_manager()->register_accelerometer_listener(this);
+	//device()->input_manager()->register_mouse_listener(this);
+	device()->input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
 	
 }
 
@@ -140,9 +166,9 @@ void FPSSystem::camera_render()
 //-----------------------------------------------------------------------	
 void FPSSystem::set_view_by_cursor()
 {
-	static Vec2 lastPos = get_input_manager()->get_cursor_relative_xy();
-	Vec2 currentPos = get_input_manager()->get_cursor_relative_xy();
-	get_input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
+	static Vec2 lastPos = device()->input_manager()->get_cursor_relative_xy();
+	Vec2 currentPos = device()->input_manager()->get_cursor_relative_xy();
+	device()->input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
 
 	if (lastPos == currentPos)
 	{
@@ -151,8 +177,8 @@ void FPSSystem::set_view_by_cursor()
 
 	Vec2 delta = lastPos - currentPos;
 
-	get_input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
-	lastPos = get_input_manager()->get_cursor_relative_xy();
+	device()->input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
+	lastPos = device()->input_manager()->get_cursor_relative_xy();
 
 	m_angle_x += delta.y * m_camera->GetSensibility();
 	m_angle_y += delta.x * m_camera->GetSensibility();

+ 25 - 0
src/FPSSystem.h

@@ -1,3 +1,28 @@
+/*
+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"

+ 2 - 1
src/MovableCamera.cpp

@@ -27,6 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Mat3.h"
 #include "Types.h"
 #include "MovableCamera.h"
+#include "Device.h"
 #include "InputManager.h"
 
 namespace crown
@@ -57,7 +58,7 @@ float MovableCamera::GetSensibility() const
 void MovableCamera::SetActive(bool active)
 {
 	Camera::SetActive(active);
-	get_input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
+	device()->input_manager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
 }
 
 //-----------------------------------------------------------------------

+ 1 - 1
src/Terrain.cpp

@@ -280,7 +280,7 @@ uint32_t Terrain::SnapToGrid(const Vec3& vertex)
 
 void Terrain::Render()
 {
-	Renderer* renderer = GetDevice()->renderer();
+	Renderer* renderer = device()->renderer();
 
 	renderer->render_triangles(
 				mVertices[0].to_float_ptr(),

+ 1 - 1
src/TextureResource.cpp

@@ -42,7 +42,7 @@ void TextureResource::online(TextureResource* texture)
 {
 	assert(texture != NULL);
 
-	texture->m_render_texture = GetDevice()->renderer()->load_texture(texture);
+	texture->m_render_texture = device()->renderer()->load_texture(texture);
 }
 
 //-----------------------------------------------------------------------------

+ 0 - 7
src/input/InputManager.cpp

@@ -200,12 +200,5 @@ void InputManager::set_cursor_relative_xy(const Vec2& position)
 	set_cursor_xy(Point2((int32_t)(position.x * (float) window_width), (int32_t)(position.y * (float) window_height)));
 }
 
-//-----------------------------------------------------------------------------
-InputManager inputManager;
-InputManager* get_input_manager()
-{
-	return &inputManager;
-}
-
 } // namespace crown
 

+ 0 - 2
src/input/InputManager.h

@@ -138,7 +138,5 @@ private:
 	bool				m_cursor_visible;
 };
 
-InputManager* get_input_manager();
-
 } // namespace crown
 

+ 1 - 1
src/os/linux/main.cpp

@@ -33,7 +33,7 @@ int main(int argc, char** argv)
 
 	crown::os::init_input();
 
-	crown::Device* engine = crown::GetDevice();
+	crown::Device* engine = crown::device();
 
 	if (!engine->init(argc, argv))
 	{