|
27 | 27 |
|
28 | 28 | static int (*test_callback)(HDC hdc); |
29 | 29 |
|
| 30 | +static int |
| 31 | +find_pixel_format(HDC hdc) |
| 32 | +{ |
| 33 | + DWORD required_flags = PFD_DRAW_TO_WINDOW | |
| 34 | + PFD_SUPPORT_OPENGL; |
| 35 | + DWORD unwanted_flags = PFD_NEED_PALETTE | |
| 36 | + PFD_NEED_SYSTEM_PALETTE; |
| 37 | + PIXELFORMATDESCRIPTOR pfd; |
| 38 | + int best_index = 0; |
| 39 | + int best_distance = 1; |
| 40 | + |
| 41 | + for (int index = 1; |
| 42 | + DescribePixelFormat(hdc, index, sizeof (pfd), &pfd) && best_distance != 0; |
| 43 | + index++) |
| 44 | + { |
| 45 | + int distance; |
| 46 | + |
| 47 | + if (pfd.nSize < sizeof (pfd) || pfd.nVersion != 1) |
| 48 | + continue; |
| 49 | + |
| 50 | + if (pfd.iPixelType != PFD_TYPE_RGBA) |
| 51 | + continue; |
| 52 | + |
| 53 | + if (pfd.iLayerType != PFD_MAIN_PLANE) |
| 54 | + continue; |
| 55 | + |
| 56 | + if ((pfd.dwFlags & required_flags) != required_flags) |
| 57 | + continue; |
| 58 | + |
| 59 | + if ((pfd.dwFlags & unwanted_flags) != 0) |
| 60 | + continue; |
| 61 | + |
| 62 | + distance = 1000 * !!(pfd.dwFlags & PFD_GENERIC_FORMAT) + |
| 63 | + 100 * !!(pfd.dwFlags & PFD_GENERIC_ACCELERATED) + |
| 64 | + 10 * !!(pfd.cColorBits != 24 && pfd.cColorBits != 32) + |
| 65 | + 10 * !!(pfd.cRedBits != 8 || pfd.cGreenBits != 8 || pfd.cBlueBits != 8) + |
| 66 | + !(pfd.dwFlags & PFD_DOUBLEBUFFER); |
| 67 | + if (best_index == 0 || distance < best_distance) { |
| 68 | + best_index = index; |
| 69 | + best_distance = distance; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return best_index; |
| 74 | +} |
| 75 | + |
30 | 76 | static void |
31 | 77 | setup_pixel_format(HDC hdc) |
32 | 78 | { |
33 | | - PIXELFORMATDESCRIPTOR pfd = { |
34 | | - sizeof(PIXELFORMATDESCRIPTOR), |
35 | | - 1, |
36 | | - PFD_SUPPORT_OPENGL | |
37 | | - PFD_DRAW_TO_WINDOW | |
38 | | - PFD_DOUBLEBUFFER, |
39 | | - PFD_TYPE_RGBA, |
40 | | - 32, |
41 | | - 0, 0, 0, 0, 0, 0, |
42 | | - 0, |
43 | | - 0, |
44 | | - 0, |
45 | | - 0, 0, 0, 0, |
46 | | - 16, |
47 | | - 0, |
48 | | - 0, |
49 | | - PFD_MAIN_PLANE, |
50 | | - 0, |
51 | | - 0, 0, 0, |
52 | | - }; |
| 79 | + PIXELFORMATDESCRIPTOR pfd; |
53 | 80 | int pixel_format; |
| 81 | + int ret; |
54 | 82 |
|
55 | | - pixel_format = ChoosePixelFormat(hdc, &pfd); |
| 83 | + pixel_format = find_pixel_format(hdc); |
56 | 84 | if (!pixel_format) { |
57 | | - fputs("ChoosePixelFormat failed.\n", stderr); |
58 | | - exit(1); |
| 85 | + fputs("Could not find suitable pixel format.\n", stderr); |
| 86 | + exit(77); /* skip */ |
59 | 87 | } |
60 | 88 |
|
| 89 | + DescribePixelFormat(hdc, pixel_format, sizeof (pfd), &pfd); |
| 90 | + |
61 | 91 | if (SetPixelFormat(hdc, pixel_format, &pfd) != TRUE) { |
62 | 92 | fputs("SetPixelFormat() failed.\n", stderr); |
63 | 93 | exit(1); |
64 | 94 | } |
| 95 | + |
| 96 | + SetLastError(0); |
| 97 | + ret = GetPixelFormat(hdc); |
| 98 | + if (ret == 0) { |
| 99 | + unsigned int code = GetLastError(); |
| 100 | + fprintf(stderr, "GetPixelFormat failed with error code %u\n", code); |
| 101 | + exit(1); |
| 102 | + } |
65 | 103 | } |
66 | 104 |
|
67 | 105 | static LRESULT CALLBACK |
|
0 commit comments