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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
| // g++ -g -Wall -o ddraw ddraw.cpp -lddraw
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
typedef struct _DDraw DDraw;
struct _DDraw
{
HWND window;
int width;
int height;
int x;
int y;
LPDIRECTDRAW object;
LPDIRECTDRAWSURFACE surface_primary;
LPDIRECTDRAWSURFACE surface_back;
LPDIRECTDRAWCLIPPER clipper;
int depth;
int fullscreen;
};
DDraw my_ddraw;
static int result = 1;
static LRESULT CALLBACK window_procedure(HWND hWindow,
UINT uMessage,
WPARAM wparam,
LPARAM lparam);
static void
ddraw_error_display (HRESULT res, char *str)
{
switch (res) {
case DDERR_DIRECTDRAWALREADYCREATED:
printf ("[error] [ddraw] %s %s\n", str, " ddraw already created");
break;
case DDERR_GENERIC:
printf ("[error] [ddraw] %s %s\n", str, " generic");
break;
case DDERR_INVALIDDIRECTDRAWGUID:
printf ("[error] [ddraw] %s %s\n", str, " invalid ddraw guid");
break;
case DDERR_INVALIDPARAMS:
printf ("[error] [ddraw] %s %s\n", str, " invalid params");
break;
case DDERR_NODIRECTDRAWHW:
printf ("[error] [ddraw] %s %s\n", str, " not ddraw hw");
break;
case DDERR_OUTOFMEMORY:
printf ("[error] [ddraw] %s %s\n", str, " out of memory");
break;
case DDERR_INVALIDOBJECT:
printf ("[error] [ddraw] %s %s\n", str, " invalid object");
break;
case DDERR_SURFACEBUSY:
printf ("[error] [ddraw] %s %s\n", str, " surface busy");
break;
case DDERR_SURFACELOST:
// printf ("[error] [ddraw] %s %s\n", str, " surface lost");
break;
case DDERR_WASSTILLDRAWING:
printf ("[error] [ddraw] %s %s\n", str, " still drawing");
break;
case DDERR_INVALIDRECT:
printf ("[error] [ddraw] %s %s\n", str, " invalid rect");
break;
case DDERR_NOTLOCKED:
printf ("[error] [ddraw] %s %s\n", str, " not locked");
break;
}
}
static int
_directdraw_init (DDraw *ddraw)
{
DDSURFACEDESC surface_desc;
HRESULT res;
res = DirectDrawCreate (NULL, &ddraw->object, NULL);
if (FAILED(res)) {
ddraw_error_display (res, "DirectDrawCreate");
return 0;
}
if (ddraw->fullscreen) {
DDSCAPS caps;
res = ddraw->object->SetCooperativeLevel (ddraw->window,
DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
if (FAILED(res)) {
ddraw_error_display (res, "SetCooperativeLevel");
ddraw->object->Release ();
return 0;
}
res = ddraw->object->SetDisplayMode (ddraw->width, ddraw->height, ddraw->depth);
if (FAILED(res)) {
ddraw_error_display (res, "SetDisplayMode");
ddraw->object->Release ();
return 0;
}
memset(&surface_desc, 0, sizeof(surface_desc));
surface_desc.dwSize = sizeof(surface_desc);
surface_desc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
surface_desc.dwBackBufferCount = 1;
res = ddraw->object->CreateSurface (&surface_desc,
&ddraw->surface_primary, NULL);
if (FAILED(res)) {
ddraw_error_display (res, "CreateSurface");
ddraw->object->Release ();
return 0;
}
caps.dwCaps = DDSCAPS_BACKBUFFER;
res = ddraw->surface_primary->GetAttachedSurface (&caps, &ddraw->surface_back);
if (FAILED(res)) {
ddraw_error_display (res, "GetAttachedSurface back");
ddraw->surface_primary->Release ();
ddraw->object->Release ();
return 0;
}
}
else {
DDPIXELFORMAT pixel_format;
res = ddraw->object->SetCooperativeLevel (ddraw->window, DDSCL_NORMAL);
if (FAILED(res)) {
ddraw_error_display (res, "SetCooperativeLevel");
ddraw->object->Release ();
return 0;
}
res = ddraw->object->CreateClipper (0, &ddraw->clipper, NULL);
if (FAILED(res)) {
ddraw_error_display (res, "CreateClipper");
ddraw->object->Release ();
return 0;
}
res = ddraw->clipper->SetHWnd (0, ddraw->window);
if (FAILED(res)) {
ddraw_error_display (res, "SetHWnd");
ddraw->clipper->Release ();
ddraw->object->Release ();
return 0;
}
memset(&surface_desc, 0, sizeof(surface_desc));
surface_desc.dwSize = sizeof(surface_desc);
surface_desc.dwFlags = DDSD_CAPS;
surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
res = ddraw->object->CreateSurface (&surface_desc,
&ddraw->surface_primary, NULL);
if (FAILED(res)) {
ddraw_error_display (res, "CreateSurface");
ddraw->clipper->Release ();
ddraw->object->Release ();
return 0;
}
res = ddraw->surface_primary->SetClipper (ddraw->clipper);
if (FAILED(res)) {
ddraw_error_display (res, "SetClipper");
ddraw->surface_primary->Release ();
ddraw->clipper->Release ();
ddraw->object->Release ();
return 0;
}
memset (&surface_desc, 0, sizeof(surface_desc));
surface_desc.dwSize = sizeof(surface_desc);
surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
surface_desc.dwWidth = ddraw->width;
surface_desc.dwHeight = ddraw->height;
res = ddraw->object->CreateSurface (&surface_desc,
&ddraw->surface_back, NULL);
if (FAILED(res)) {
ddraw_error_display (res, "CreateSurface back");
ddraw->surface_primary->Release ();
ddraw->clipper->Release ();
ddraw->object->Release ();
return 0;
}
ZeroMemory(&pixel_format, sizeof(pixel_format));
pixel_format.dwSize = sizeof(pixel_format);
ddraw->surface_primary->GetPixelFormat(&pixel_format);
ddraw->depth = pixel_format.dwRGBBitCount;
}
return 1;
}
static void
_directdraw_shutdown (DDraw *ddraw)
{
ddraw->surface_primary->Release ();
if (ddraw->clipper)
ddraw->clipper->Release ();
ddraw->object->Release ();
}
static HWND
create_window (DDraw *ddraw)
{
WNDCLASS wc;
RECT rect;
HWND window;
DWORD style;
int x;
int y;
memset (&wc, 0, sizeof (wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = window_procedure;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wc.lpszMenuName = NULL;
wc.lpszClassName = "class name";
RegisterClass(&wc);
rect.left = 0;
rect.top = 0;
rect.right = ddraw->width;
rect.bottom = ddraw->height;
if (ddraw->fullscreen) {
style = WS_VISIBLE | WS_POPUP;
ddraw->x = -1;
ddraw->y = -1;
x = 0;
y = 0;
}
else {
style = WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
ddraw->x = x = 100;
ddraw->y = y = 100;
}
if (!AdjustWindowRect(&rect, style, FALSE))
return NULL;
window = CreateWindowEx (WS_EX_TOPMOST,
"class name", "",
style,
x, y,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
GetModuleHandle(NULL),
NULL);
if (!window) {
DWORD err = GetLastError ();
TCHAR *str;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&str, 0, NULL);
std::cout << "[error] create_window() : " << err << " " << str << std::endl;
LocalFree(str);
}
return window;
}
static void
destroy_window (HWND window)
{
DestroyWindow (window);
}
static void
change_window_style (DDraw *ddraw)
{
int x;
int y;
if (ddraw->fullscreen) {
SetWindowLong(ddraw->window, GWL_STYLE, WS_POPUP);
}
else {
RECT rect;
SetWindowLong(ddraw->window, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SIZEBOX);
GetWindowRect (ddraw->window, &rect);
if (ddraw->x < 0)
x = 100;
else
x = ddraw->x;
if (ddraw->y < 0)
y = 100;
else
y = ddraw->y;
printf ("move window : %d %d\n", x, y);
MoveWindow(ddraw->window, x, y,
rect.right - rect.left,
rect.bottom - rect.top,
TRUE);
}
SetFocus (ddraw->window);
ShowWindow(ddraw->window, SW_SHOWNORMAL);
UpdateWindow(ddraw->window);
}
static LRESULT CALLBACK
window_procedure(HWND window,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch (msg) {
case WM_LBUTTONDOWN:
{
int x;
int y;
x = GET_X_LPARAM(lparam);
y = GET_Y_LPARAM(lparam);
if ((x >= 10) && (x <= (10 + 30 - 1)) &&
(x >= 10) && (x <= (10 + 30 - 1))) {
printf ("dedans ! %d %d\n", x, y);
if (!my_ddraw.fullscreen) {
RECT rect;
GetWindowRect (my_ddraw.window, &rect);
my_ddraw.x = rect.left;
my_ddraw.y = rect.top;
}
my_ddraw.fullscreen = !my_ddraw.fullscreen;
_directdraw_shutdown (&my_ddraw);
change_window_style(&my_ddraw);
_directdraw_init(&my_ddraw);
}
else
printf (" pas dedans ! %d %d\n", x, y);
return 0;
}
case WM_KEYDOWN:
{
switch (wparam) {
case VK_ESCAPE:
result = 0;
break;
}
return 0;
}
case WM_DESTROY:
result = 0;
break;
}
return DefWindowProc(window, msg, wparam, lparam);
}
static int
draw_rect_16 (DDraw *ddraw, int x, int y, int w, int h)
{
DDSURFACEDESC surface_desc;
unsigned short *data;
unsigned short *tmp;
int stride;
HRESULT res;
ZeroMemory(&surface_desc, sizeof(surface_desc));
surface_desc.dwSize = sizeof(surface_desc);
res = ddraw->surface_back->Lock(NULL,
&surface_desc,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL);
if (FAILED (res)) {
ddraw_error_display (res, "Lock");
return 0;
}
stride = surface_desc.lPitch / 2;
data = (unsigned short *)surface_desc.lpSurface;
memset (data, 0, surface_desc.lPitch * surface_desc.dwHeight);
tmp = data + y * (surface_desc.lPitch / 2) + x;
for (int j = 0; j < h; ++j, tmp += (surface_desc.lPitch / 2)) {
for (int i = 0; i < w; i++) {
tmp[i] = (31 << 11) + (0 << 5) + (0 << 0);
}
}
res = ddraw->surface_back->Unlock(NULL);
if (FAILED (res)) {
ddraw_error_display (res, "Unlock");
return 0;
}
return 1;
}
static int
draw_rect_32 (DDraw *ddraw, int x, int y, int w, int h)
{
HRESULT res;
DDSURFACEDESC surface_desc;
unsigned int *data;
unsigned int *tmp;
ZeroMemory(&surface_desc, sizeof(surface_desc));
surface_desc.dwSize = sizeof(surface_desc);
res = ddraw->surface_back->Lock(NULL,
&surface_desc,
DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,
NULL);
if (FAILED (res)) {
ddraw_error_display (res, "Lock");
return 0;
}
data = (unsigned int *)surface_desc.lpSurface;
memset (data, 0, surface_desc.lPitch * surface_desc.dwHeight);
tmp = data + y * (surface_desc.lPitch / 4) + x;
for (int j = 0; j < h; ++j, tmp += (surface_desc.lPitch / 4)) {
for (int i = 0; i < w; i++) {
tmp[i] = (0 << 24) + (255 << 16) + (0 << 8) + (0 << 0);
}
}
res = ddraw->surface_back->Unlock(NULL);
if (FAILED (res)) {
ddraw_error_display (res, "Unlock");
return 0;
}
return 1;
}
static int
draw_rect (DDraw *ddraw, int x, int y, int w, int h)
{
switch (ddraw->depth) {
case 16:
return draw_rect_16 (ddraw, x, y, w, h);
case 32:
return draw_rect_32 (ddraw, x, y, w, h);
default:
return 0;
}
}
static void
ddraw_flip (DDraw *ddraw)
{
RECT dst_rect;
RECT src_rect;
POINT p;
HRESULT res;
if (ddraw->fullscreen) {
res = ddraw->surface_primary->Flip (NULL, DDFLIP_WAIT);
}
else {
/* we figure out where on the primary surface our window lives */
p.x = 0;
p.y = 0;
ClientToScreen(ddraw->window, &p);
GetClientRect(ddraw->window, &dst_rect);
OffsetRect(&dst_rect, p.x, p.y);
SetRect(&src_rect, 0, 0, ddraw->width, ddraw->height);
/* nothing to do if the function fails, so we don't check the result */
res = ddraw->surface_primary->Blt(&dst_rect, ddraw->surface_back, &src_rect, DDBLT_WAIT, NULL);
}
if (res == DDERR_SURFACELOST) {
printf ("surface lost \n");
res = ddraw->surface_primary->Restore ();
if (res == DD_OK) {
// on fait des trucs comme recharger les images, etc...
}
}
}
int main ()
{
/* when fullscreen != 0, width and height must be a screen size and depth the bpp of the screen */
/* when fullscreen == 0, width and height are the size of the window. bpp is computed */
my_ddraw.fullscreen = 1;
my_ddraw.width = 640;
my_ddraw.height = 480;
my_ddraw.depth = 32;
printf ("window init\n");
my_ddraw.window = create_window (&my_ddraw);
if (!my_ddraw.window) {
printf ("window init failed\n");
return EXIT_FAILURE;
}
printf ("directdraw init\n");
if (!_directdraw_init(&my_ddraw)) {
printf ("directdraw init failed\n");
destroy_window (my_ddraw.window);
return EXIT_FAILURE;
}
SetFocus (my_ddraw.window);
ShowWindow(my_ddraw.window, SW_SHOWNORMAL);
UpdateWindow(my_ddraw.window);
while (result) {
MSG msg;
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
draw_rect (&my_ddraw, 10, 10, 30, 30);
ddraw_flip (&my_ddraw);
}
_directdraw_shutdown (&my_ddraw);
printf ("destroy window\n");
destroy_window (my_ddraw.window);
return EXIT_SUCCESS;
} |
Partager