Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
PoroCYon
Bonzomatic
Commits
f89d0d38
Commit
f89d0d38
authored
Jan 24, 2015
by
Gargaj
Browse files
basic dx9 init
parent
60878646
Changes
1
Hide whitespace changes
Inline
Side-by-side
platform_w32_dx9/Renderer.cpp
View file @
f89d0d38
#ifdef _WIN32
#include <windows.h>
#
endif
#
include <tchar.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "../Renderer.h"
...
...
@@ -9,6 +8,8 @@
#include <stb_image.c>
#include "../external/scintilla/include/Scintilla.h"
#define DEVTYPE D3DDEVTYPE_HAL
namespace
Renderer
{
char
defaultShader
[
65536
]
=
...
...
@@ -44,25 +45,244 @@ namespace Renderer
int
nWidth
=
0
;
int
nHeight
=
0
;
bool
Open
(
RENDERER_SETTINGS
*
settings
)
HWND
hWnd
=
NULL
;
KeyEvent
keyEventBuffer
[
512
];
int
keyEventBufferCount
=
0
;
MouseEvent
mouseEventBuffer
[
512
];
int
mouseEventBufferCount
=
0
;
LRESULT
CALLBACK
WndProc
(
HWND
hWnd
,
UINT
uMsg
,
WPARAM
wParam
,
LPARAM
lParam
)
{
switch
(
uMsg
)
{
case
WM_KEYDOWN
:
{
switch
(
wParam
)
{
case
VK_ESCAPE
:
{
run
=
false
;
}
break
;
}
//pKeys[wParam] = 1;
}
break
;
case
WM_KEYUP
:
{
//pKeys[wParam] = 0;
}
break
;
case
WM_LBUTTONDOWN
:
{
//nLeftMouse = 1;
}
break
;
case
WM_LBUTTONUP
:
{
//nLeftMouse = 0;
}
break
;
case
WM_ACTIVATE
:
{
if
((
LOWORD
(
wParam
)
&
WA_ACTIVE
)
||
(
LOWORD
(
wParam
)
&
WA_CLICKACTIVE
))
{
//nGotFocus = 1;
//bActive = true;
}
else
{
//nLostFocus = 1;
//bActive = false;
}
}
break
;
case
WM_SIZE
:
{
switch
(
wParam
)
{
case
SIZE_MINIMIZED
:
{
//nLostFocus = 1;
}
break
;
case
SIZE_MAXIMIZED
:
case
SIZE_RESTORED
:
{
//nGotFocus = 1;
}
break
;
}
}
break
;
case
WM_SYSCOMMAND
:
{
switch
(
wParam
)
{
case
SC_SCREENSAVE
:
case
SC_MONITORPOWER
:
{
return
0
;
}
}
}
break
;
case
WM_CLOSE
:
{
run
=
false
;
}
break
;
}
return
DefWindowProc
(
hWnd
,
uMsg
,
wParam
,
lParam
);
}
bool
InitWindow
(
RENDERER_SETTINGS
*
pSetup
)
{
// TODO: create window
WNDCLASS
WC
;
WC
.
style
=
CS_HREDRAW
|
CS_VREDRAW
|
CS_OWNDC
;
WC
.
lpfnWndProc
=
&
WndProc
;
WC
.
cbClsExtra
=
0
;
WC
.
cbWndExtra
=
0
;
WC
.
hInstance
=
GetModuleHandle
(
NULL
);
//WC.hIcon = LoadIcon(setup->hInstance,MAKEINTRESOURCE(IDI_ICON1));
WC
.
hIcon
=
NULL
;
WC
.
hCursor
=
LoadCursor
(
NULL
,
IDC_ARROW
);
WC
.
hbrBackground
=
NULL
;
WC
.
lpszMenuName
=
NULL
;
WC
.
lpszClassName
=
_T
(
"fwzwnd"
);
if
(
!
RegisterClass
(
&
WC
))
return
0
;
DWORD
wExStyle
=
WS_EX_APPWINDOW
;
DWORD
wStyle
=
WS_POPUP
|
WS_CLIPSIBLINGS
|
WS_CLIPCHILDREN
;
if
(
pSetup
->
windowMode
==
RENDERER_WINDOWMODE_WINDOWED
)
wStyle
|=
WS_OVERLAPPED
|
WS_CAPTION
;
RECT
wr
=
{
0
,
0
,
pSetup
->
nWidth
,
pSetup
->
nHeight
};
AdjustWindowRectEx
(
&
wr
,
wStyle
,
FALSE
,
wExStyle
);
pD3D
=
Direct3DCreate9
(
D3D9b_SDK_VERSION
);
if
(
!
pD3D
)
hWnd
=
CreateWindowEx
(
wExStyle
,
_T
(
"fwzwnd"
),
_T
(
"mlat design"
),
wStyle
,
(
GetSystemMetrics
(
SM_CXSCREEN
)
-
pSetup
->
nWidth
)
/
2
,
(
GetSystemMetrics
(
SM_CYSCREEN
)
-
pSetup
->
nHeight
)
/
2
,
wr
.
right
-
wr
.
left
,
wr
.
bottom
-
wr
.
top
,
NULL
,
NULL
,
WC
.
hInstance
,
NULL
);
if
(
!
hWnd
)
return
false
;
// TODO: create device
ShowWindow
(
hWnd
,
SW_SHOW
);
SetForegroundWindow
(
hWnd
);
SetFocus
(
hWnd
);
return
true
;
}
bool
InitDirect3D
(
RENDERER_SETTINGS
*
pSetup
)
{
pD3D
=
Direct3DCreate9
(
D3D9b_SDK_VERSION
);
if
(
!
pD3D
)
return
false
;
nWidth
=
pSetup
->
nWidth
;
nHeight
=
pSetup
->
nHeight
;
D3DPRESENT_PARAMETERS
d3dpp
;
ZeroMemory
(
&
d3dpp
,
sizeof
(
d3dpp
));
d3dpp
.
SwapEffect
=
D3DSWAPEFFECT_DISCARD
;
d3dpp
.
hDeviceWindow
=
hWnd
;
d3dpp
.
Windowed
=
pSetup
->
windowMode
!=
RENDERER_WINDOWMODE_FULLSCREEN
;
d3dpp
.
PresentationInterval
=
D3DPRESENT_INTERVAL_IMMEDIATE
;
d3dpp
.
BackBufferCount
=
1
;
d3dpp
.
BackBufferWidth
=
pSetup
->
nWidth
;
d3dpp
.
BackBufferHeight
=
pSetup
->
nHeight
;
D3DDISPLAYMODE
d3ddm
;
if
(
FAILED
(
pD3D
->
GetAdapterDisplayMode
(
D3DADAPTER_DEFAULT
,
&
d3ddm
)
)
)
return
false
;
static
D3DFORMAT
pBackbufferFormats32
[]
=
{
D3DFMT_X8R8G8B8
,
D3DFMT_A8R8G8B8
,
D3DFMT_UNKNOWN
};
D3DFORMAT
*
pFormats
=
pBackbufferFormats32
;
for
(
int
i
=
0
;
pFormats
[
i
]
!=
D3DFMT_UNKNOWN
;
i
++
)
if
(
SUCCEEDED
(
pD3D
->
CheckDeviceType
(
D3DADAPTER_DEFAULT
,
DEVTYPE
,
d3ddm
.
Format
,
pFormats
[
i
],
d3dpp
.
Windowed
))
)
d3dpp
.
BackBufferFormat
=
pFormats
[
i
];
if
(
d3dpp
.
BackBufferFormat
==
D3DFMT_UNKNOWN
)
return
false
;
d3dpp
.
EnableAutoDepthStencil
=
TRUE
;
d3dpp
.
AutoDepthStencilFormat
=
D3DFMT_UNKNOWN
;
static
D3DFORMAT
pDepthFormats
[]
=
{
D3DFMT_D16
,
D3DFMT_D24X8
,
D3DFMT_D32
,
D3DFMT_UNKNOWN
};
for
(
int
i
=
0
;
pDepthFormats
[
i
]
!=
D3DFMT_UNKNOWN
;
i
++
)
if
(
SUCCEEDED
(
pD3D
->
CheckDeviceFormat
(
D3DADAPTER_DEFAULT
,
DEVTYPE
,
d3ddm
.
Format
,
D3DUSAGE_DEPTHSTENCIL
,
D3DRTYPE_SURFACE
,
pDepthFormats
[
i
]
))
)
d3dpp
.
AutoDepthStencilFormat
=
pDepthFormats
[
i
];
if
(
d3dpp
.
AutoDepthStencilFormat
==
D3DFMT_UNKNOWN
)
return
false
;
static
D3DMULTISAMPLE_TYPE
pMultisampleTypes
[]
=
{
D3DMULTISAMPLE_2_SAMPLES
,
D3DMULTISAMPLE_4_SAMPLES
,
D3DMULTISAMPLE_6_SAMPLES
,
D3DMULTISAMPLE_8_SAMPLES
,
D3DMULTISAMPLE_NONE
};
d3dpp
.
MultiSampleType
=
D3DMULTISAMPLE_NONE
;
HRESULT
h
;
h
=
pD3D
->
CreateDevice
(
D3DADAPTER_DEFAULT
,
DEVTYPE
,
hWnd
,
D3DCREATE_HARDWARE_VERTEXPROCESSING
,
&
d3dpp
,
&
pDevice
);
if
(
h
!=
D3D_OK
)
{
h
=
pD3D
->
CreateDevice
(
D3DADAPTER_DEFAULT
,
DEVTYPE
,
hWnd
,
D3DCREATE_SOFTWARE_VERTEXPROCESSING
,
&
d3dpp
,
&
pDevice
);
if
(
h
!=
D3D_OK
)
{
return
false
;
}
}
pDevice
->
SetRenderState
(
D3DRS_ZENABLE
,
D3DZB_TRUE
);
pDevice
->
SetRenderState
(
D3DRS_CULLMODE
,
D3DCULL_NONE
);
pDevice
->
SetRenderState
(
D3DRS_LIGHTING
,
FALSE
);
for
(
int
x
=
0
;
x
<
4
;
x
++
)
{
pDevice
->
SetSamplerState
(
x
,
D3DSAMP_MINFILTER
,
D3DTEXF_LINEAR
);
pDevice
->
SetSamplerState
(
x
,
D3DSAMP_MAGFILTER
,
D3DTEXF_LINEAR
);
pDevice
->
SetSamplerState
(
x
,
D3DSAMP_MIPFILTER
,
D3DTEXF_LINEAR
);
}
return
1
;
}
bool
Open
(
RENDERER_SETTINGS
*
settings
)
{
if
(
!
InitWindow
(
settings
))
return
false
;
if
(
!
InitDirect3D
(
settings
))
return
false
;
return
true
;
}
KeyEvent
keyEventBuffer
[
512
];
int
keyEventBufferCount
=
0
;
MouseEvent
mouseEventBuffer
[
512
];
int
mouseEventBufferCount
=
0
;
void
StartFrame
()
{
MSG
msg
;
if
(
PeekMessage
(
&
msg
,
NULL
,
0U
,
0U
,
PM_REMOVE
)
)
{
TranslateMessage
(
&
msg
);
DispatchMessage
(
&
msg
);
}
pDevice
->
Clear
(
0
,
NULL
,
D3DCLEAR_TARGET
|
D3DCLEAR_ZBUFFER
,
0xFF808080
,
1.0
f
,
0
);
pDevice
->
BeginScene
();
}
...
...
@@ -79,6 +299,11 @@ namespace Renderer
{
if
(
pDevice
)
pDevice
->
Release
();
if
(
pD3D
)
pD3D
->
Release
();
if
(
!
hWnd
)
{
DestroyWindow
(
hWnd
);
UnregisterClass
(
_T
(
"fwzwnd"
),
GetModuleHandle
(
NULL
));
}
}
void
RenderFullscreenQuad
()
...
...
@@ -133,6 +358,8 @@ namespace Renderer
void
StartTextRendering
()
{
pDevice
->
SetVertexShader
(
NULL
);
pDevice
->
SetPixelShader
(
NULL
);
}
void
SetTextRenderingViewport
(
Scintilla
::
PRectangle
rect
)
{
...
...
@@ -152,10 +379,13 @@ namespace Renderer
void
ReleaseTexture
(
Texture
*
tex
)
{
// ((DX9Texture *)tex)->pTexture->Release();
// delete tex;
}
void
BindTexture
(
Texture
*
tex
)
{
// pDevice->SetTexture( 0, ((DX9Texture *)tex)->pTexture );
}
void
RenderQuad
(
Vertex
&
a
,
Vertex
&
b
,
Vertex
&
c
,
Vertex
&
d
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment