Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.3 KiB

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <GL/gl.h>
  4. #include "stonehen.h"
  5. #include "setpixel.h"
  6. HPALETTE ghpalOld, ghPalette = (HPALETTE) 0;
  7. unsigned char threeto8[8] = {
  8. 0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
  9. };
  10. unsigned char twoto8[4] = {
  11. 0, 0x55, 0xaa, 0xff
  12. };
  13. unsigned char oneto8[2] = {
  14. 0, 255
  15. };
  16. unsigned char
  17. ComponentFromIndex(UCHAR i, UINT nbits, UINT shift)
  18. {
  19. unsigned char val;
  20. val = i >> shift;
  21. switch (nbits) {
  22. case 1:
  23. val &= 0x1;
  24. return oneto8[val];
  25. case 2:
  26. val &= 0x3;
  27. return twoto8[val];
  28. case 3:
  29. val &= 0x7;
  30. return threeto8[val];
  31. default:
  32. return 0;
  33. }
  34. }
  35. void
  36. CreateRGBPalette(HDC hdc)
  37. {
  38. PIXELFORMATDESCRIPTOR pfd, *ppfd;
  39. LOGPALETTE *pPal;
  40. int n, i;
  41. ppfd = &pfd;
  42. n = GetPixelFormat(hdc);
  43. DescribePixelFormat(hdc, n, sizeof(PIXELFORMATDESCRIPTOR), ppfd);
  44. if (ppfd->dwFlags & PFD_NEED_PALETTE) {
  45. n = 1 << ppfd->cColorBits;
  46. pPal = (PLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
  47. n * sizeof(PALETTEENTRY));
  48. pPal->palVersion = 0x300;
  49. pPal->palNumEntries = n;
  50. for (i=0; i<n; i++) {
  51. pPal->palPalEntry[i].peRed =
  52. ComponentFromIndex(i, ppfd->cRedBits, ppfd->cRedShift);
  53. pPal->palPalEntry[i].peGreen =
  54. ComponentFromIndex(i, ppfd->cGreenBits, ppfd->cGreenShift);
  55. pPal->palPalEntry[i].peBlue =
  56. ComponentFromIndex(i, ppfd->cBlueBits, ppfd->cBlueShift);
  57. pPal->palPalEntry[i].peFlags = 0;
  58. }
  59. ghPalette = CreatePalette(pPal);
  60. LocalFree(pPal);
  61. ghpalOld = SelectPalette(hdc, ghPalette, FALSE);
  62. n = RealizePalette(hdc);
  63. }
  64. }
  65. BOOL bSetupPixelFormat(HDC hdc)
  66. {
  67. PIXELFORMATDESCRIPTOR pfd, *ppfd;
  68. int pixelformat;
  69. ppfd = &pfd;
  70. ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
  71. ppfd->nVersion = 1;
  72. ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  73. ppfd->dwLayerMask = PFD_MAIN_PLANE;
  74. ppfd->iPixelType = PFD_TYPE_RGBA;
  75. ppfd->cColorBits = 24;
  76. ppfd->cDepthBits = 16; //GLX_DEPTH_SIZE
  77. ppfd->cRedBits = 8; //GLX_RED_SIZE
  78. ppfd->cRedShift = 0;
  79. ppfd->cGreenBits = 8; //GLX_GREEN_SIZE
  80. ppfd->cGreenShift = 8;
  81. ppfd->cBlueBits = 8; //GLX_BLUE_SIZE
  82. ppfd->cBlueShift = 16;
  83. ppfd->cAlphaBits = 0;
  84. ppfd->cAlphaShift = 0;
  85. ppfd->cAccumBits = 0; //ACCUM NOT SUPPORTED
  86. ppfd->cAccumRedBits = 0; //GLX_ACCUM_RED_SIZE
  87. ppfd->cAccumGreenBits = 0; //GLX_ACCUM_GREEN_SIZE
  88. ppfd->cAccumBlueBits = 0; //GLX_ACCUM_BLUE_SIZE
  89. ppfd->cAccumAlphaBits = 0; //GLX_ACCUM_ALPHA_SIZE
  90. ppfd->cStencilBits = 24; //GLX_STENCIL_SIZE
  91. ppfd->cAuxBuffers = 0;
  92. ppfd->bReserved = 0;
  93. ppfd->dwVisibleMask =
  94. ppfd->dwDamageMask = 0;
  95. pixelformat = ChoosePixelFormat(hdc, ppfd);
  96. if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
  97. {
  98. MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
  99. return FALSE;
  100. }
  101. if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
  102. {
  103. MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
  104. return FALSE;
  105. }
  106. CreateRGBPalette(hdc);
  107. return TRUE;
  108. }