-
Notifications
You must be signed in to change notification settings - Fork 2
/
d3dwindow.c
151 lines (123 loc) · 3.63 KB
/
d3dwindow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <d3d9.h>
#ifdef _DEBUG
#include <stdio.h>
#endif
#include "resource.h"
#include "d3dwindow.h"
static LRESULT CALLBACK window_proc(HWND win,UINT message,WPARAM wparam,LPARAM lparam);
typedef LPDIRECT3D9 (WINAPI * DIRECT3DCREATE9) (unsigned int sdkVersion);
static HMODULE library = 0;
IDirect3D9 *direct3d = NULL;
IDirect3DDevice9 *device = NULL;
HWND win = NULL;
HINSTANCE inst;
unsigned int width;
unsigned int height;
BOOL fullscreen;
IDirect3DDevice9 *d3dwin_open( char *title, unsigned int width_, unsigned int height_, D3DFORMAT format, BOOL fullscreen_ ){
DIRECT3DCREATE9 Direct3DCreate9;
RECT rect;
WNDCLASS wc;
UINT32 style;
D3DDISPLAYMODE displaymode;
D3DPRESENT_PARAMETERS presentparameters;
D3DCAPS9 caps;
width = width_;
height = height_;
fullscreen = fullscreen_;
inst = GetModuleHandle(NULL);
library = (HMODULE) LoadLibrary("d3d9.dll");
if(!library) return NULL;
Direct3DCreate9 = (DIRECT3DCREATE9) GetProcAddress(library,"Direct3DCreate9");
if(!Direct3DCreate9) return NULL;
direct3d=Direct3DCreate9(D3D_SDK_VERSION);
if(!direct3d) return NULL;
if(!fullscreen)
style = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU;
else{
style = WS_POPUP;
ShowCursor(FALSE);
}
rect.left = 0;
rect.top = 0;
rect.right = width;
rect.bottom = height;
if(!fullscreen) AdjustWindowRect( &rect, style, FALSE );
wc.style = CS_VREDRAW|CS_HREDRAW;
wc.lpfnWndProc = window_proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = 0;
wc.hIcon = LoadIcon(inst,MAKEINTRESOURCE(IDI_ICON));
wc.hCursor = LoadCursor(inst,IDC_ARROW);
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = "d3d9";
RegisterClass(&wc);
win = CreateWindowEx(0, "d3d9", title, (style)|WS_VISIBLE, 0, 0, rect.right-rect.left, rect.bottom-rect.top, 0, 0, inst, 0);
if(!win) return NULL;
memset(&presentparameters, 0, sizeof(D3DPRESENT_PARAMETERS));
if(!fullscreen){
if(FAILED(IDirect3D9_GetAdapterDisplayMode( direct3d, D3DADAPTER_DEFAULT, &displaymode ))){
d3dwin_close();
return NULL;
}
}
if(!fullscreen) presentparameters.Windowed = TRUE;
presentparameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentparameters.BackBufferWidth = width;
presentparameters.BackBufferHeight = height;
if( fullscreen ) presentparameters.BackBufferFormat = format;
else presentparameters.BackBufferFormat = displaymode.Format;
presentparameters.AutoDepthStencilFormat = D3DFMT_D24S8;
presentparameters.EnableAutoDepthStencil = TRUE;
IDirect3D9_GetDeviceCaps(direct3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
if(FAILED(IDirect3D9_CreateDevice( direct3d,
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
win,
(caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT) ? D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&presentparameters,
&device))){
d3dwin_close();
return NULL;
}
IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
return device;
}
void d3dwin_close(){
if(fullscreen) ShowCursor(TRUE);
if(device){
IDirect3DDevice9_EvictManagedResources(device);
IDirect3DDevice9_Release(device);
device = NULL;
}
if(direct3d){
IDirect3D9_Release(direct3d);
direct3d = NULL;
}
if(win){
DestroyWindow(win);
win = NULL;
}
UnregisterClass("d3d9", inst);
}
static LRESULT CALLBACK window_proc(HWND win,UINT message,WPARAM wparam,LPARAM lparam){
switch (message){
case WM_CLOSE:
PostQuitMessage(0);
return 0;
break;
case WM_SYSCOMMAND:
switch(wparam){
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
return DefWindowProc(win,message,wparam,lparam);
}