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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
| // g++ -g -Wall -o d3d d3d.cpp -ld3d9 -ld3dx9d
// with mpatrol :
// g++ -g -Wall -include /usr/local/include/mpatrol.h -o d3d d3d.cpp -L/usr/local/lib -ld3d9 -ld3dx9d -lmpatrol -lbfd -liberty -limagehlp
#include <cstdlib>
#include <cstdio>
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
typedef struct Data Data;
struct Data
{
LPDIRECT3D9 object;
LPDIRECT3DDEVICE9 device;
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 texture;
int mask_r;
int mask_g;
int mask_b;
};
static LRESULT CALLBACK
MainWndProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
Data *
data_init (HWND window,
int width,
int height)
{
D3DPRESENT_PARAMETERS pp;
D3DDISPLAYMODE dm;
D3DSURFACE_DESC sd;
D3DCAPS9 caps;
Data *d;
DWORD flag;
d = (Data *)malloc (sizeof (Data));
if (!d)
goto no_data;
d->object = Direct3DCreate9 (D3D_SDK_VERSION);
if (!d->object)
goto no_object;
if (FAILED (d->object->GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &dm)))
goto no_device;
if (FAILED (d->object->GetDeviceCaps (D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
&caps)))
goto no_device;
flag = (caps.VertexProcessingCaps != 0)
? D3DCREATE_HARDWARE_VERTEXPROCESSING
: D3DCREATE_SOFTWARE_VERTEXPROCESSING;
ZeroMemory(&pp, sizeof(pp));
pp.BackBufferWidth = width;
pp.BackBufferHeight = height;
pp.BackBufferFormat = dm.Format;
pp.BackBufferCount = 1;
pp.SwapEffect = D3DSWAPEFFECT_FLIP;
pp.hDeviceWindow = window;
pp.Windowed = TRUE;
if (FAILED(d->object->CreateDevice (D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
window,
flag,
&pp,
&d->device)))
goto no_device;
if (FAILED (D3DXCreateSprite (d->device, &d->sprite)))
goto no_sprite;
if (FAILED (d->device->CreateTexture (width, height, 1,
D3DUSAGE_DYNAMIC,
dm.Format,
D3DPOOL_DEFAULT,
&d->texture, NULL)))
goto no_texture;
if (FAILED (d->texture->GetLevelDesc (0, &sd)))
goto no_level_desc;
switch (sd.Format) {
case D3DFMT_A8R8G8B8:
case D3DFMT_X8R8G8B8:
d->mask_r = 0x00ff0000;
d->mask_g = 0x0000ff00;
d->mask_b = 0x000000ff;
break;
case D3DFMT_R5G6B5:
d->mask_r = 0xf800;
d->mask_g = 0x07e0;
d->mask_b = 0x001f;
break;
default:
goto no_level_desc;
}
return d;
no_level_desc:
d->texture->Release ();
no_texture:
d->sprite->Release ();
no_sprite:
d->device->Release ();
no_device:
d->object->Release ();
no_object:
free (d);
no_data:
return NULL;
}
void
data_shutdown (Data *d)
{
if (!d)
return;
d->texture->Release ();
d->sprite->Release ();
d->device->Release ();
d->object->Release ();
free (d);
}
void
data_paint (Data *d)
{
D3DLOCKED_RECT d3d_rect;
RECT rect;
int w;
int h;
int depth;
int pitch;
unsigned char *tmp;
unsigned char *d3d_tmp;
static int foo = 0;
void *surf;
w = 64;
h = 64;
depth = 4;
pitch = depth * w;
surf = calloc (w * h * depth, 1);
if (!surf) {
printf ("pas bon \n");
return;
}
tmp = (unsigned char *)surf;
for (int r = 0; r < w; r++) {
for (int g = 0; g < h; g++, tmp += depth) {
tmp[0] = (r * 2 + foo) % 256;
tmp[1] = (g + foo) % 256;
tmp[2] = (w - 1 - g) * 4;
}
}
foo++;
rect.left = 0;
rect.top = 0;
rect.right = w;
rect.bottom = h;
d->device->Clear (0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_COLORVALUE(0.0f, 1.0f, 0.0f, 1.0f), 1.0f, 0 );
if (FAILED (d->device->BeginScene ()))
goto free_surf;
if (FAILED (d->sprite->Begin (0)))
goto end_scene;
if (FAILED (d->texture->LockRect (0, &d3d_rect, NULL, D3DLOCK_DISCARD)))
goto end;
tmp = (unsigned char *)surf;
d3d_tmp = (unsigned char *)d3d_rect.pBits;
for (int j = 0; j < h; j++, tmp += pitch, d3d_tmp += d3d_rect.Pitch)
memcpy (d3d_tmp, tmp, pitch);
d->texture->UnlockRect(0);
d->sprite->Draw (d->texture, &rect, NULL, NULL,
D3DCOLOR_ARGB (255, 255, 255, 255));
d->sprite->End ();
d->device->EndScene ();
d->device->Present (NULL, NULL, NULL, NULL);
return;
end:
d->sprite->End ();
end_scene:
d->device->EndScene ();
free_surf:
free (surf);
}
#if 1
int WINAPI
WinMain(HINSTANCE hinstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
#else
int main ()
#endif
{
WNDCLASS wc;
RECT rect;
MSG msg;
HINSTANCE instance;
HWND window;
Data *d;
int width;
int height;
instance = GetModuleHandle(0);
if (!instance)
goto no_instance;
ZeroMemory(&wc, sizeof(wc));
wc.lpfnWndProc = MainWndProc;
wc.hInstance = instance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
wc.lpszClassName = "Direct3D_test";
if(!RegisterClass(&wc))
goto no_window_class;
width = 320;
height = 200;
rect.left = 0;
rect.top = 0;
rect.right = width;
rect.bottom = height;
AdjustWindowRect (&rect, WS_OVERLAPPEDWINDOW | WS_SIZEBOX, FALSE);
window = CreateWindow("Direct3D_test",
"Direct3D_test",
WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
rect.right - rect.left, rect.bottom - rect.top,
NULL, NULL, instance, NULL);
if (!window)
goto no_window;
d = data_init (window, width, height);
if (!d)
goto no_data;
ShowWindow(window, SW_SHOWDEFAULT);
UpdateWindow(window);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
data_paint (d);
}
data_shutdown (d);
DestroyWindow (window);
UnregisterClass ("Direct3D_test", instance);
FreeLibrary (instance);
return EXIT_SUCCESS;
no_data:
DestroyWindow (window);
no_window:
UnregisterClass ("Direct3D_test", instance);
no_window_class:
FreeLibrary (instance);
no_instance:
return EXIT_FAILURE;
} |
Partager