Browse Source

Updated DirectX 9 sample to work with new shell library layout

David Wimsey 11 years ago
parent
commit
05aa98b0ee

+ 34 - 3
Samples/basic/directx/src/RenderInterfaceDirectX.cpp

@@ -53,10 +53,11 @@ struct RocketD3D9Vertex
 
 DWORD vertex_fvf = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1;
 
-RenderInterfaceDirectX::RenderInterfaceDirectX(LPDIRECT3D9 _g_pD3D, LPDIRECT3DDEVICE9 _g_pd3dDevice)
+RenderInterfaceDirectX::RenderInterfaceDirectX()
 {
-	g_pD3D = _g_pD3D;
-	g_pd3dDevice = _g_pd3dDevice;
+	g_pD3D = NULL;
+	g_pd3dDevice = NULL;
+	m_rocket_context = NULL;
 }
 
 RenderInterfaceDirectX::~RenderInterfaceDirectX()
@@ -81,6 +82,11 @@ void RenderInterfaceDirectX::RenderGeometry(Rocket::Core::Vertex* ROCKET_UNUSED_
 // Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
 Rocket::Core::CompiledGeometryHandle RenderInterfaceDirectX::CompileGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rocket::Core::TextureHandle texture)
 {
+	if(g_pd3dDevice == NULL)
+	{
+		return false;
+	}
+
 	// Construct a new RocketD3D9CompiledGeometry structure, which will be returned as the handle, and the buffers to
 	// store the geometry.
 	RocketD3D9CompiledGeometry* geometry = new RocketD3D9CompiledGeometry();
@@ -120,6 +126,11 @@ Rocket::Core::CompiledGeometryHandle RenderInterfaceDirectX::CompileGeometry(Roc
 // Called by Rocket when it wants to render application-compiled geometry.
 void RenderInterfaceDirectX::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
 {
+	if(g_pd3dDevice == NULL)
+	{
+		return;
+	}
+
 	// Build and set the transform matrix.
 	D3DXMATRIX world_transform;
 	D3DXMatrixTranslation(&world_transform, translation.x, translation.y, 0);
@@ -156,12 +167,22 @@ void RenderInterfaceDirectX::ReleaseCompiledGeometry(Rocket::Core::CompiledGeome
 // Called by Rocket when it wants to enable or disable scissoring to clip content.
 void RenderInterfaceDirectX::EnableScissorRegion(bool enable)
 {
+	if(g_pd3dDevice == NULL)
+	{
+		return;
+	}
+
 	g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enable);
 }
 
 // Called by Rocket when it wants to change the scissor region.
 void RenderInterfaceDirectX::SetScissorRegion(int x, int y, int width, int height)
 {
+	if(g_pd3dDevice == NULL)
+	{
+		return;
+	}
+
 	RECT scissor_rect;
 	scissor_rect.left = x;
 	scissor_rect.right = x + width;
@@ -194,6 +215,11 @@ struct TGAHeader
 // Called by Rocket when a texture is required by the library.
 bool RenderInterfaceDirectX::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
 {
+	if(g_pd3dDevice == NULL)
+	{
+		return false;
+	}
+
 	Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
 	Rocket::Core::FileHandle file_handle = file_interface->Open(source);
 	if (file_handle == NULL)
@@ -263,6 +289,11 @@ bool RenderInterfaceDirectX::LoadTexture(Rocket::Core::TextureHandle& texture_ha
 // Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
 bool RenderInterfaceDirectX::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const byte* source, const Rocket::Core::Vector2i& source_dimensions)
 {
+	if(g_pd3dDevice == NULL)
+	{
+		return false;
+	}
+
 	// Create a Direct3DTexture9, which will be set as the texture handle. Note that we only create one surface for
 	// this texture; because we're rendering in a 2D context, mip-maps are not required.
 	LPDIRECT3DTEXTURE9 d3d9_texture;

+ 12 - 2
Samples/basic/directx/src/RenderInterfaceDirectX.h

@@ -29,6 +29,7 @@
 #define RENDERINTERFACEDIRECTX_H
 
 #include <Rocket/Core/RenderInterface.h>
+#include "../../../shell/include/ShellRenderInterfaceExtensions.h"
 #include <d3d9.h>
 
 /**
@@ -37,10 +38,10 @@
 	@author Peter Curry
  */
 
-class RenderInterfaceDirectX : public Rocket::Core::RenderInterface
+class RenderInterfaceDirectX : public Rocket::Core::RenderInterface, public ShellRenderInterfaceExtensions
 {
 public:
-	RenderInterfaceDirectX(LPDIRECT3D9 g_pD3D, LPDIRECT3DDEVICE9 g_pd3dDevice);
+	RenderInterfaceDirectX(void);
 	virtual ~RenderInterfaceDirectX();
 
 	/// Called by Rocket when it wants to render geometry that it does not wish to optimise.
@@ -71,9 +72,18 @@ public:
 	/// Returns the native vertical texel offset for the renderer.
 	float GetVerticalTexelOffset();
 
+// ShellRenderInterfaceExtensions
+	virtual void SetViewport(int width, int height);
+	virtual void SetContext(void *context);
+	virtual bool AttachToNative(void *nativeWindow);
+	virtual void DetachFromNative(void);
+	virtual void PrepareRenderBuffer(void);
+	virtual void PresentRenderBuffer(void);
+
 private:
 	LPDIRECT3D9 g_pD3D;
 	LPDIRECT3DDEVICE9 g_pd3dDevice;
+	void *m_rocket_context;
 };
 
 #endif

+ 152 - 0
Samples/basic/directx/src/ShellRenderInterfaceExtensionsDirectX_Win32.cpp

@@ -0,0 +1,152 @@
+/*
+ * This source file is part of libRocket, the HTML/CSS Interface Middleware
+ *
+ * For the latest information, see http://www.librocket.com
+ *
+ * Copyright (c) 2014 CodePoint David Wimsey
+ *
+ * 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 "RenderInterfaceDirectX.h"
+#include <Rocket/Core.h>
+#include <d3dx9.h>
+
+void RenderInterfaceDirectX::SetContext(void *context)
+{
+	m_rocket_context = context;
+}
+
+void RenderInterfaceDirectX::SetViewport(int width, int height)
+{
+	if(g_pd3dDevice != NULL)
+	{
+		D3DXMATRIX projection;
+		D3DXMatrixOrthoOffCenterLH(&projection, 0, (float)width, (float)height, 0, -1, 1);
+		g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
+	}
+
+	if(m_rocket_context != NULL)
+	{
+		((Rocket::Core::Context*)m_rocket_context)->SetDimensions(Rocket::Core::Vector2i(width, height));
+	}
+}
+
+bool RenderInterfaceDirectX::AttachToNative(void *nativeWindow)
+{
+	RECT clientRect;
+	if(!GetClientRect((HWND) nativeWindow, &clientRect))
+	{
+		// if we can't lookup the client rect, abort, something is seriously wrong
+		return false;
+	}
+
+	g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
+	if (g_pD3D == NULL)
+		return false;
+
+	D3DPRESENT_PARAMETERS d3dpp;
+	ZeroMemory(&d3dpp, sizeof(d3dpp));
+	d3dpp.Windowed = TRUE;
+	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
+	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
+	g_pd3dDevice = NULL;
+
+	if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
+									D3DDEVTYPE_HAL,
+									(HWND) nativeWindow,
+									D3DCREATE_SOFTWARE_VERTEXPROCESSING,
+									&d3dpp,
+									&g_pd3dDevice)))
+	{
+
+		this->DetachFromNative();
+		return false;
+	}
+
+	// Set up an orthographic projection.
+	D3DXMATRIX projection;
+	D3DXMatrixOrthoOffCenterLH(&projection, 0, (FLOAT)clientRect.right, (FLOAT)clientRect.bottom, 0, -1, 1);
+
+	g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
+
+	// Switch to clockwise culling instead of counter-clockwise culling; Rocket generates counter-clockwise geometry,
+	// so you can either reverse the culling mode when Rocket is rendering, or reverse the indices in the render
+	// interface.
+	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
+
+	// Enable alpha-blending for Rocket.
+	g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
+	g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
+	g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
+
+	// Set up the texture stage states for the diffuse texture.
+	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
+	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
+	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
+	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
+	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
+
+	g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
+	g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
+
+	// Disable lighting for Rocket.
+	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
+
+	return true;
+}
+
+void RenderInterfaceDirectX::DetachFromNative()
+{
+	if (g_pd3dDevice != NULL)
+	{
+		// Release the last resources we bound to the device.
+		g_pd3dDevice->SetTexture(0, NULL);
+		g_pd3dDevice->SetStreamSource(0, NULL, 0, 0);
+		g_pd3dDevice->SetIndices(NULL);
+
+		g_pd3dDevice->Release();
+		g_pd3dDevice = NULL;
+	}
+
+	if (g_pD3D != NULL)
+	{
+		g_pD3D->Release();
+		g_pD3D = NULL;
+	}
+}
+
+void RenderInterfaceDirectX::PrepareRenderBuffer()
+{
+	if(g_pd3dDevice != NULL)
+	{
+		g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
+		g_pd3dDevice->BeginScene();
+	}
+}
+
+void RenderInterfaceDirectX::PresentRenderBuffer()
+{
+	if(g_pd3dDevice != NULL)
+	{
+		g_pd3dDevice->EndScene();
+		g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
+	}
+}

+ 13 - 98
Samples/basic/directx/src/main.cpp

@@ -30,97 +30,18 @@
 #include <Input.h>
 #include <Shell.h>
 #include "RenderInterfaceDirectX.h"
-#include <d3d9.h>
-#include <d3dx9.h>
-
-static LPDIRECT3D9 g_pD3D = NULL;
-static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
 
 static Rocket::Core::Context* context = NULL;
 
-bool InitialiseDirectX()
-{
-	g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
-	if (g_pD3D == NULL)
-		return false;
-
-	D3DPRESENT_PARAMETERS d3dpp;
-	ZeroMemory(&d3dpp, sizeof(d3dpp));
-	d3dpp.Windowed = TRUE;
-	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
-	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
-
-	if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
-									D3DDEVTYPE_HAL,
-									(HWND) Shell::GetWindowHandle(),
-									D3DCREATE_SOFTWARE_VERTEXPROCESSING,
-									&d3dpp,
-									&g_pd3dDevice)))
-	{
-		return false;
-	}
-
-	// Set up an orthographic projection.
-	D3DXMATRIX projection;
-	D3DXMatrixOrthoOffCenterLH(&projection, 0, 1024, 768, 0, -1, 1);
-	g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
-
-	// Switch to clockwise culling instead of counter-clockwise culling; Rocket generates counter-clockwise geometry,
-	// so you can either reverse the culling mode when Rocket is rendering, or reverse the indices in the render
-	// interface.
-	g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
-
-	// Enable alpha-blending for Rocket.
-	g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
-	g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
-	g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
-
-	// Set up the texture stage states for the diffuse texture.
-	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
-	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
-	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
-	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
-	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
-
-	g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
-	g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
-
-	// Disable lighting for Rocket.
-	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
-
-	return true;
-}
-
-void ShutdownDirectX()
-{
-	if (g_pd3dDevice != NULL)
-	{
-		// Release the last resources we bound to the device.
-		g_pd3dDevice->SetTexture(0, NULL);
-		g_pd3dDevice->SetStreamSource(0, NULL, 0, 0);
-		g_pd3dDevice->SetIndices(NULL);
-
-		g_pd3dDevice->Release();
-		g_pd3dDevice = NULL;
-	}
-
-	if (g_pD3D != NULL)
-	{
-		g_pD3D->Release();
-		g_pD3D = NULL;
-	}
-}
+ShellRenderInterfaceExtensions *shell_renderer;
 
 void GameLoop()
 {
-	g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
-	g_pd3dDevice->BeginScene();
-
 	context->Update();
-	context->Render();
 
-	g_pd3dDevice->EndScene();
-	g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
+	shell_renderer->PrepareRenderBuffer();
+	context->Render();
+	shell_renderer->PresentRenderBuffer();
 }
 
 #if defined ROCKET_PLATFORM_WIN32
@@ -140,25 +61,21 @@ int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv)
 	ROCKET_UNUSED(argv);
 #endif
 
-	// Generic OS initialisation, creates a window and does not attach OpenGL.
-	if (!Shell::Initialise("../Samples/basic/directx/") ||
-		!Shell::OpenWindow("DirectX Sample", false))
-	{
-		Shell::Shutdown();
-		return -1;
-	}
+	int window_width = 1024;
+	int window_height = 768;
 
-	// DirectX initialisation.
-	if (!InitialiseDirectX())
+	RenderInterfaceDirectX directx_renderer;
+	shell_renderer = &directx_renderer;
+
+	// Generic OS initialisation, creates a window
+	if (!Shell::Initialise("../Samples/basic/directx/") ||
+		!Shell::OpenWindow("DirectX Sample", shell_renderer, window_width, window_height, true))
 	{
-		Shell::CloseWindow();
 		Shell::Shutdown();
-
 		return -1;
 	}
 
 	// Install our DirectX render interface into Rocket.
-	RenderInterfaceDirectX directx_renderer(g_pD3D, g_pd3dDevice);
 	Rocket::Core::SetRenderInterface(&directx_renderer);
 
 	ShellSystemInterface system_interface;
@@ -167,7 +84,7 @@ int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv)
 	Rocket::Core::Initialise();
 
 	// Create the main Rocket context and set it on the shell's input layer.
-	context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(1024, 768));
+	context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(window_width, window_height));
 	if (context == NULL)
 	{
 		Rocket::Core::Shutdown();
@@ -194,8 +111,6 @@ int main(int ROCKET_UNUSED_PARAMETER(argc), char** ROCKET_UNUSED_PARAMETER(argv)
 	context->RemoveReference();
 	Rocket::Core::Shutdown();
 
-	ShutdownDirectX();
-
 	Shell::CloseWindow();
 	Shell::Shutdown();
 

+ 5 - 1
Samples/shell/src/win32/ShellWin32.cpp

@@ -148,7 +148,11 @@ bool Shell::OpenWindow(const char* name, ShellRenderInterfaceExtensions *_shell_
 	if (_shell_renderer != NULL)
 	{
 		shell_renderer = _shell_renderer;
-		return(shell_renderer->AttachToNative(window_handle));
+		if(!shell_renderer->AttachToNative(window_handle))
+		{
+			CloseWindow();
+			return false;
+		}
 	}
 
     return true;