home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / DEMO / init_win.h < prev    next >
C/C++ Source or Header  |  2000-08-20  |  23KB  |  468 lines

  1.  
  2. /*
  3.  * main window & setup dialog specific code
  4.  */
  5.  
  6. // setup dialog callback function 
  7.  
  8. #include "dialog.h"
  9.  
  10. #define MESS(id,m,w,l) SendDlgItemMessage(h,id,m,(WPARAM)w,(LPARAM)l)
  11. #define ER(m,w,l) MESS(ID_RESOLUTION,m,w,l)
  12. #define EB(m,w,l) MESS(ID_BPP,m,w,l)
  13.  
  14. BOOL CALLBACK dialogproc(HWND h,UINT m,WPARAM w,LPARAM l)
  15. {
  16.         switch (m) {
  17.  
  18.                 case WM_COMMAND:
  19.                         switch (LOWORD(w)) {
  20.                                 case IDCANCEL:
  21.                                 case ID_STOP:
  22.                                         EndDialog(h,0);
  23.                                         return 1;
  24.                                 case ID_GO:
  25.                                         EndDialog(h,1);
  26.                                         return 1;
  27.                                 case ID_FULLSCREEN:
  28.                                         fullscreen=SendDlgItemMessage(h,ID_FULLSCREEN,BM_GETCHECK,0,0);
  29.                                         return 1;
  30.                                 case ID_VOODOO:
  31.                                         Texture3DS::voodoo=SendDlgItemMessage(h,ID_VOODOO,BM_GETCHECK,0,0);
  32.                                         return 1;
  33.                                 case ID_LOWTEXTURES:
  34.                                         Texture3DS::lowtextures=SendDlgItemMessage(h,ID_LOWTEXTURES,BM_GETCHECK,0,0);
  35.                                         return 1;
  36.                                 case ID_NOFOG:
  37.                                         usefog=!SendDlgItemMessage(h,ID_NOFOG,BM_GETCHECK,0,0);
  38.                                         return 1;
  39.                                 case ID_RESOLUTION:
  40.                                         if (HIWORD(w)==CBN_SELCHANGE) {
  41.                                         int s=ER(CB_GETCURSEL,0,0);
  42.                                         switch (s) {
  43.                                                 case 0:
  44.                                                         resx=320; resy=200;
  45.                                                         break;
  46.                                                 case 1:
  47.                                                         resx=320; resy=240;
  48.                                                         break;
  49.                                                 case 2:
  50.                                                         resx=400; resy=300;
  51.                                                         break;
  52.                                                 case 3:
  53.                                                         resx=512; resy=384;
  54.                                                         break;
  55.                                                 case 4:
  56.                                                         resx=640; resy=400;
  57.                                                         break;
  58.                                                 case 5:
  59.                                                         resx=640; resy=480;
  60.                                                         break;
  61.                                                 case 6:
  62.                                                         resx=720; resy=480;
  63.                                                         break;
  64.                                                 case 7:
  65.                                                         resx=800; resy=600;
  66.                                                         break;
  67.                                                 case 8:
  68.                                                         resx=848; resy=480;
  69.                                                         break;
  70.                                                 case 9:
  71.                                                         resx=1024; resy=768;
  72.                                                         break;
  73.                                                 case 10:
  74.                                                         resx=1280; resy=1024;
  75.                                                         break;
  76.                                                 case 11:
  77.                                                         resx=1600; resy=1200;
  78.                                                         break;
  79.                                                 }
  80.                                         }
  81.                                         return 1;
  82.                                 case ID_BPP:
  83.                                         if (HIWORD(w)==CBN_SELCHANGE) {
  84.                                         int s=EB(CB_GETCURSEL,0,0);
  85.                                         switch (s) {
  86.                                                 case 0:
  87.                                                         bpp=16;
  88.                                                         break;
  89.                                                 case 1:
  90.                                                         bpp=24;
  91.                                                         break;
  92.                                                 case 2:
  93.                                                         bpp=32;
  94.                                                         break;
  95.                                                 }
  96.                                         }
  97.                                         return 1;
  98.                                 }
  99.                         break;
  100.  
  101.                 case WM_INITDIALOG:
  102.  
  103.                         resx=640;resy=480;bpp=32;
  104.                         fullscreen=TRUE;
  105.                         Texture3DS::voodoo=FALSE;
  106.                         Texture3DS::lowtextures=FALSE;
  107.                         usefog=TRUE;
  108.  
  109.                         MESS(ID_FULLSCREEN,BM_SETCHECK,1,0);
  110.  
  111.                         ER(CB_ADDSTRING,0,"320 x 200");
  112.                         ER(CB_ADDSTRING,0,"320 x 240");
  113.                         ER(CB_ADDSTRING,0,"400 x 300");
  114.                         ER(CB_ADDSTRING,0,"512 x 384");
  115.                         ER(CB_ADDSTRING,0,"640 x 400");
  116.                         ER(CB_ADDSTRING,0,"640 x 480");
  117.                         ER(CB_ADDSTRING,0,"720 x 480");
  118.                         ER(CB_ADDSTRING,0,"800 x 600");
  119.                         ER(CB_ADDSTRING,0,"848 x 480");
  120.                         ER(CB_ADDSTRING,0,"1024 x 768");
  121.                         ER(CB_ADDSTRING,0,"1280 x 1024");
  122.                         ER(CB_ADDSTRING,0,"1600 x 1200");
  123.                         ER(CB_SETCURSEL,5,0);
  124.  
  125.                         EB(CB_ADDSTRING,0,"16");
  126.                         EB(CB_ADDSTRING,0,"24");
  127.                         EB(CB_ADDSTRING,0,"32");
  128.                         EB(CB_SETCURSEL,2,0);
  129.  
  130.                         return 1;
  131.  
  132.         }
  133.         return 0;
  134. }
  135.  
  136.  
  137. /*okno initialization*/
  138.  
  139. #ifndef CDS_FULLSCREEN
  140. #define CDS_FULLSCREEN 4
  141. #endif
  142.  
  143. bool      active=TRUE;
  144.  
  145.  
  146. #define   VK_MOUSEL 256
  147. #define   VK_MOUSER 257
  148. #define   VK_PLUS VK_ADD
  149. #define   VK_MINUS VK_SUBTRACT
  150. #define   VK_A 65
  151. #define   VK_Z 90
  152. #define   VK_X 88
  153. #define   VK_S 83
  154. #define   VK_D 68
  155. #define   VK_Q 81
  156. #define   VK_E 69
  157. bool      key[258];
  158. int       mouse_x=320,mouse_y=240;
  159.  
  160. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  161.  
  162. GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
  163. {
  164.         if (height==0)
  165.         {
  166.                 height=1;
  167.         }
  168.         glViewport(0,0,width,height);
  169. }
  170.  
  171. GLvoid KillGLWindow(GLvoid)                                                   
  172. {
  173.         if (fullscreen)                                                       
  174.         {
  175.                 ChangeDisplaySettings(NULL,0);                                
  176.                 ShowCursor(TRUE);                                             
  177.         }
  178.  
  179.         if (hRC)                                                                
  180.         {
  181.                 if (!wglMakeCurrent(NULL,NULL))                                 
  182.                 {
  183.                         MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  184.                 }
  185.  
  186.                 if (!wglDeleteContext(hRC))                                             
  187.                 {
  188.                         MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  189.                 }
  190.                 hRC=NULL;                                                                               
  191.         }
  192.  
  193.         if (hDC && !ReleaseDC(hWnd,hDC))                                        
  194.         {
  195.                 MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  196.                 hDC=NULL;                                                                             
  197.         }
  198.  
  199.         if (hWnd && !DestroyWindow(hWnd))                               
  200.         {
  201.                 MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  202.                 hWnd=NULL;                                              
  203.         }
  204.  
  205.         if (!UnregisterClass("OpenGL",hInstance))                       
  206.         {
  207.                 MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
  208.                 hInstance=NULL;                                                                
  209.         }
  210. }
  211.  
  212. void* memset (void* p, int a, size_t size)
  213. {
  214.   char b = a;
  215.   char* ptr = (char*)p;
  216.   for(int i=size; i; i--) {
  217.     *ptr = b;
  218.     ptr++;
  219.   }
  220. }
  221.  
  222. BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
  223. {
  224.         GLuint          PixelFormat;        
  225.         WNDCLASS        wc;                 
  226.         DWORD           dwExStyle;          
  227.         DWORD           dwStyle;            
  228.         RECT            WindowRect;         
  229.         WindowRect.left=(long)0;            
  230.         WindowRect.right=(long)width;       
  231.         WindowRect.top=(long)0;             
  232.         WindowRect.bottom=(long)height;     
  233.  
  234.         fullscreen=fullscreenflag;          
  235.  
  236.         hInstance                       = GetModuleHandle(NULL);              
  237.         wc.style                        = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 
  238.         wc.lpfnWndProc          = (WNDPROC) WndProc;                          
  239.         wc.cbClsExtra           = 0;                                          
  240.         wc.cbWndExtra           = 0;                                          
  241.         wc.hInstance            = hInstance;                                  
  242.         wc.hIcon                = LoadIcon(hInstance, MAKEINTRESOURCE(ID_ICON));
  243. //        wc.hIcon                        = LoadIcon(NULL, IDI_WINLOGO);        
  244.         wc.hCursor                      = LoadCursor(NULL, IDC_ARROW);        
  245.         wc.hbrBackground        = NULL;                                       
  246.         wc.lpszMenuName         = NULL;                                       
  247.         wc.lpszClassName        = "OpenGL";                                   
  248.  
  249.         if (!RegisterClass(&wc))                                                                      
  250.         {
  251.                 MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  252.                 return FALSE;                                                                           
  253.         }
  254.  
  255.         if (fullscreen)                                                                                 
  256.         {
  257.                 DEVMODE dmScreenSettings;                                                               
  258.                 memset(&dmScreenSettings,0,sizeof(dmScreenSettings));   
  259.                 dmScreenSettings.dmSize=sizeof(dmScreenSettings);       
  260.                 dmScreenSettings.dmPelsWidth    = width;                
  261.                 dmScreenSettings.dmPelsHeight   = height;               
  262.                 dmScreenSettings.dmBitsPerPel   = bits;                 
  263.                 dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
  264.  
  265.                   if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
  266.                 {
  267.                         if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
  268.                         {
  269.                                 fullscreen=FALSE;               
  270.                         }
  271.                         else
  272.                         {
  273.                                 MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
  274.                                 return FALSE;                                                                   // Return FALSE
  275.                         }
  276.                 }
  277.         }
  278.  
  279.         if (fullscreen)                                        
  280.         {
  281.                 dwExStyle=WS_EX_APPWINDOW;                     
  282.                 dwStyle=WS_POPUP;                              
  283.                 ShowCursor(FALSE);                             
  284.         }
  285.         else
  286.         {
  287.                 dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;  
  288.                 dwStyle=WS_OVERLAPPEDWINDOW;                   
  289.         }
  290.  
  291.         AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);             
  292.  
  293.         // Create The Window
  294.         if (!(hWnd=CreateWindowEx(      dwExStyle,                                                      
  295.                                                                 "OpenGL",                               
  296.                                                                 title,                                  
  297.                                                                 dwStyle |                               
  298.                                                                 WS_CLIPSIBLINGS |                       
  299.                                                                 WS_CLIPCHILDREN,                        
  300.  
  301.                                                                 CW_USEDEFAULT, CW_USEDEFAULT,
  302. //                                                                0, 0,                                   
  303.                                                                 WindowRect.right-WindowRect.left,       
  304.                                                                 WindowRect.bottom-WindowRect.top,       
  305.                                                                 NULL,                                   
  306.                                                                 NULL,                                   
  307.                                                                 hInstance,                              
  308.                                                                 NULL)))                                 
  309.         {
  310.                 KillGLWindow();          
  311.                 MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  312.                 return FALSE;            
  313.         }
  314.  
  315.         static  PIXELFORMATDESCRIPTOR pfd=                              // pfd Tells Windows How We Want Things To Be
  316.         {
  317.                 sizeof(PIXELFORMATDESCRIPTOR),                          // Size Of This Pixel Format Descriptor
  318.                 1,                                                                                      // Version Number
  319.                 PFD_DRAW_TO_WINDOW |                                            // Format Must Support Window
  320.                 PFD_SUPPORT_OPENGL |                                            // Format Must Support OpenGL
  321.                 PFD_DOUBLEBUFFER,                                                       // Must Support Double Buffering
  322.                 PFD_TYPE_RGBA,                                                          // Request An RGBA Format
  323.                 bits,                                                                           // Select Our Color Depth
  324.                 0, 0, 0, 0, 0, 0,                                                       // Color Bits Ignored
  325.                 0,                                                                                      // No Alpha Buffer
  326.                 0,                                                                                      // Shift Bit Ignored
  327.                 0,                                                                                      // No Accumulation Buffer
  328.                 0, 0, 0, 0,                                                                     // Accumulation Bits Ignored
  329.                 16,                                                                                     // 16Bit Z-Buffer (Depth Buffer)
  330.                 0,                                                                                      // No Stencil Buffer
  331.                 0,                                                                                      // No Auxiliary Buffer
  332.                 PFD_MAIN_PLANE,                                                         // Main Drawing Layer
  333.                 0,                                                                                      // Reserved
  334.                 0, 0, 0                                                                         // Layer Masks Ignored
  335.         };
  336.  
  337.         if (!(hDC=GetDC(hWnd)))                         
  338.         {
  339.                 KillGLWindow();                                                        
  340.                 MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  341.                 return FALSE;                                                          
  342.         }
  343.  
  344.         if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) 
  345.         {
  346.                 KillGLWindow();                                                        
  347.                 MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  348.                 return FALSE;                                                          
  349.         }
  350.  
  351.         if(!SetPixelFormat(hDC,PixelFormat,&pfd))       
  352.         {
  353.                 KillGLWindow();                                                        
  354.                 MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  355.                 return FALSE;                                                          
  356.         }
  357.  
  358.         if (!(hRC=wglCreateContext(hDC)))               
  359.         {
  360.                 KillGLWindow();                                                        
  361.                 MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  362.                 return FALSE;                                                           
  363.         }
  364.  
  365.         if(!wglMakeCurrent(hDC,hRC))                    
  366.         {
  367.                 KillGLWindow();                                                        
  368.                 MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  369.                 return FALSE;                                                           
  370.         }
  371.  
  372.         ShowWindow(hWnd,SW_SHOW);                                   
  373.         SetForegroundWindow(hWnd);                                  
  374.         SetFocus(hWnd);                                             
  375.         ReSizeGLScene(width, height);                               
  376.  
  377.         if (!InitGL())                                              
  378.         {
  379.                 KillGLWindow();                                                        
  380.                 MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
  381.                 return FALSE;                                                        
  382.         }
  383.  
  384.         return TRUE;                                                                 
  385. }
  386.  
  387. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
  388. {
  389.         switch (uMsg)                                                          
  390.         {
  391.                 case WM_ACTIVATE:                                              
  392.                 {
  393.                         if (!HIWORD(wParam))                                   
  394.                         {
  395.                                 active=TRUE;                                   
  396.                         }
  397.                         else
  398.                         {
  399.                                 active=FALSE;                                  
  400.                         }
  401.  
  402.                         return 0;                                              
  403.                 }
  404.  
  405.                 case WM_SYSCOMMAND:                                            
  406.                 {
  407.                         switch (wParam)                                        
  408.                         {
  409.                                 case SC_SCREENSAVE:                            
  410.                                 case SC_MONITORPOWER:                          
  411.                                 return 0;                                      
  412.                         }
  413.                         break;                                                 
  414.                 }
  415.  
  416.                 case WM_CLOSE:                                                 
  417.                 {
  418.                         PostQuitMessage(0);                                    
  419.                         return 0;                                              
  420.                 }
  421.  
  422.                 case WM_KEYDOWN:                                               
  423.                 {
  424.                         key[wParam] = TRUE;                                    
  425.                         return 0;                                              
  426.                 }
  427.  
  428.                 case WM_KEYUP:                                                 
  429.                 {
  430.                         key[wParam] = FALSE;                                   
  431.                         return 0;                                              
  432.                 }
  433.                 case WM_MOUSEMOVE:
  434.                   {
  435.                   mouse_x = ((int) LOWORD(lParam) << 16) >> 16;
  436.                   mouse_y = ((int) HIWORD(lParam) << 16) >> 16;
  437.                   break;
  438.                   }
  439.                 case WM_LBUTTONDOWN:
  440.                   {
  441.                   key[VK_MOUSEL]=1;
  442.                   break;
  443.                   }
  444.                 case WM_LBUTTONUP:
  445.                   {
  446.                   key[VK_MOUSEL]=0;
  447.                   break;
  448.                   }
  449.                 case WM_RBUTTONDOWN:
  450.                   {
  451.                   key[VK_MOUSER]=1;
  452.                   break;
  453.                   }
  454.                 case WM_RBUTTONUP:
  455.                   {
  456.                   key[VK_MOUSER]=0;
  457.                   break;
  458.                   }
  459.                 case WM_SIZE:                                          
  460.                 {
  461.                         ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  
  462.                         return 0;                                      
  463.                 }
  464.         }
  465.  
  466.         // Pass All Unhandled Messages To DefWindowProc
  467.         return DefWindowProc(hWnd,uMsg,wParam,lParam);
  468. }