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

fix
win main and try a new implementation of EventBuffer

mikymod 12 лет назад
Родитель
Сommit
1e5cc51aa2
3 измененных файлов с 222 добавлено и 22 удалено
  1. 1 0
      engine/CMakeLists.txt
  2. 117 0
      engine/EventBuffer.h
  3. 104 22
      engine/os/win/main.cpp

+ 1 - 0
engine/CMakeLists.txt

@@ -76,6 +76,7 @@ set (HEADERS
 	Device.h
 	EventQueue.h
 	ConsoleServer.h
+	EventBuffer.h
 )
 
 set (CORE_SRC

+ 117 - 0
engine/EventBuffer.h

@@ -0,0 +1,117 @@
+/*
+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>
+
+#define EVENT_BUFFER_MAX_SIZE 1024 * 4
+
+namespace crown
+{
+
+struct EventBuffer
+{
+	uint8_t m_buffer[EVENT_BUFFER_MAX_SIZE];
+	size_t m_size;
+	size_t m_read;
+
+	//-----------------------------------------------------------------------------
+	EventBuffer::EventBuffer() 
+		: m_size(0), m_read(0)
+	{
+	}
+
+	//-----------------------------------------------------------------------------
+	void 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;
+	}
+
+	//-----------------------------------------------------------------------------
+	uint32_t get_next_event_type()
+	{
+		if (m_read < m_size)
+		{
+			char* cur = m_buffer + m_read;
+
+			// Saves type
+			event_type = *(uint32_t*) cur;			
+		}
+
+		return -1;		
+	}
+
+	//-----------------------------------------------------------------------------
+	void* 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;
+	}
+};
+
+} // namespace crown

+ 104 - 22
engine/os/win/main.cpp

@@ -115,22 +115,23 @@ class WindowsDevice : public Device
 {
 public:
 
-	WindowsDevice() :
-		m_hwnd(0),
-		m_style(0),
-		m_width(0),
-		m_height(0),
-		m_old_width(0),
-		m_old_height(0),
-		m_frame_width(0),
-		m_frame_height(0),
-		m_aspect_ratio(0.0f),
-		m_x(0),
-		m_y(0),
-		m_frame(false),
-		m_mouse_lock(false),
-		m_started(false),
-		m_exit(false)
+	WindowsDevice() 
+		: m_hwnd(0)
+		, m_rect()
+		, m_style(0)
+		, m_width(0)
+		, m_height(0)
+		, m_old_width(0)
+		, m_old_height(0)
+		, m_frame_width(0)
+		, m_frame_height(0)
+		, m_aspect_ratio(0.0f)
+		, m_x(0)
+		, m_y(0)
+		, m_frame(true)
+		, m_mouse_lock(false)
+		, m_started(false)
+		, m_exit(false)
 	{
 	}
 
@@ -140,7 +141,7 @@ public:
 		HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
 
 		WNDCLASSEX wnd;
-		memset(&wnd, 0, sizeof(wnd) );
+		memset(&wnd, 0, sizeof(wnd));
 		wnd.cbSize = sizeof(wnd);
 		wnd.lpfnWndProc = DefWindowProc;
 		wnd.hInstance = instance;
@@ -210,7 +211,88 @@ public:
 	//-----------------------------------------------------------------------------
 	void adjust(uint32_t width, uint32_t height, bool window_frame)
 	{
-		// Nothing right now
+			m_width = width;
+			m_height = height;
+			m_aspect_ratio = float(width) / float(height);
+
+			ShowWindow(m_hwnd, SW_SHOWNORMAL);
+			RECT rect;
+			RECT newrect = {0, 0, (LONG)width, (LONG)height};
+			DWORD style = WS_POPUP|WS_SYSMENU;
+
+			if (m_frame)
+			{
+				GetWindowRect(m_hwnd, &m_rect);
+				m_style = GetWindowLong(m_hwnd, GWL_STYLE);
+			}
+
+			if (window_frame)
+			{
+				rect = m_rect;
+				style = m_style;
+			}
+			else
+			{
+				HMONITOR monitor = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTONEAREST);
+				MONITORINFO mi;
+				mi.cbSize = sizeof(mi);
+				GetMonitorInfo(monitor, &mi);
+				newrect = mi.rcMonitor;
+				rect = mi.rcMonitor;
+			}
+
+			SetWindowLong(m_hwnd, GWL_STYLE, style);
+			uint32_t prewidth = newrect.right - newrect.left;
+			uint32_t preheight = newrect.bottom - newrect.top;
+			AdjustWindowRect(&newrect, style, FALSE);
+			m_frame_width = (newrect.right - newrect.left) - prewidth;
+			m_frame_height = (newrect.bottom - newrect.top) - preheight;
+			UpdateWindow(m_hwnd);
+
+			if (rect.left < 0 || rect.top < 0)
+			{
+				rect.left = 0;
+				rect.top = 0;
+			}
+
+			int32_t left_t = rect.left;
+			int32_t top_t = rect.top;
+			int32_t width_t = (newrect.right-newrect.left);
+			int32_t height_t = (newrect.bottom-newrect.top);
+
+			if (!window_frame)
+			{
+				float aspect_ratio = 1.0f / m_aspect_ratio;
+				width_t = math::max(uint32_t(ENTRY_DEFAULT_WIDTH / 4), width);
+				height_t = uint32_t(float(width) * aspect_ratio);
+
+				left_t = newrect.left+(newrect.right-newrect.left-width) / 2;
+				top_t = newrect.top+(newrect.bottom-newrect.top-height) / 2;
+			}
+
+			HWND parent = GetWindow(m_hwnd, GW_OWNER);
+			if (NULL != parent)
+			{
+				if (window_frame)
+				{
+					SetWindowPos(parent, HWND_TOP, -32000, -32000, 0, 0, SWP_SHOWWINDOW);
+				}
+				else
+				{
+					SetWindowPos(parent, HWND_TOP, newrect.left, newrect.top, newrect.right-newrect.left, newrect.bottom-newrect.top, SWP_SHOWWINDOW);
+				}
+			}
+
+			Log::i("left: %d", left_t);
+			Log::i("top: %d", top_t);
+			Log::i("width: %d", width_t);
+			Log::i("height: %d", height_t);
+
+			SetWindowPos(m_hwnd, HWND_TOP, left_t, top_t, width_t, height_t, SWP_SHOWWINDOW);
+
+			ShowWindow(m_hwnd, SW_RESTORE);
+
+			m_frame = window_frame;
 	}
 
 	//-----------------------------------------------------------------------------
@@ -222,7 +304,7 @@ public:
 	}
 
 	//-----------------------------------------------------------------------------
-	void setMouseLock(bool lock)
+	void set_mouse_lock(bool lock)
 	{
 		if (lock != m_mouse_lock)
 		{
@@ -272,6 +354,7 @@ public:
 			{
 			case WM_USER_SET_WINDOW_SIZE:
 			{
+				Log::i("WM_USER_SET_WINDOW_SIZE");
 				uint32_t width = GET_X_LPARAM(lparam);
 				uint32_t height = GET_Y_LPARAM(lparam);
 				adjust(width, height, true);
@@ -279,6 +362,7 @@ public:
 			}
 			case WM_USER_TOGGLE_WINDOW_FRAME:
 			{
+				Log::i("WM_USER_TOGGLE_WINDOW_FRAME");
 				if (m_frame)
 				{
 					m_old_width = m_width;
@@ -289,7 +373,7 @@ public:
 			}
 			case WM_USER_MOUSE_LOCK:
 			{
-				// setMouseLock(!!lparam);
+				set_mouse_lock(!!lparam);
 				break;
 			}
 			case WM_DESTROY:
@@ -299,7 +383,6 @@ public:
 			case WM_QUIT:
 			case WM_CLOSE:
 			{
-				Log::i("EXIT");
 				m_exit = true;
 				// m_eventQueue.postExitEvent();
 				break;
@@ -425,7 +508,6 @@ public:
 				int32_t my = GET_Y_LPARAM(lparam);
 				// m_eventQueue.postMouseEvent(mx, my, MouseButton::Middle, id == WM_MBUTTONDOWN);
 				Log::i("Middle Click! '%d' '%d'", mx, my);
-
 				break;
 			}
 			case WM_KEYDOWN: