home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM User 1995 January / CDuser6Jan95.iso / WING / CUBE.CP_ / CUBE.CP
Text File  |  1994-06-25  |  17KB  |  632 lines

  1. /**************************************************************************
  2.     cube.cpp - A spinning cube demo for WinG
  3.  
  4.     History:
  5.         11/18/93 JonBl    Created
  6.  **************************************************************************/
  7. /**************************************************************************
  8.  
  9.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  10.    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  11.    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  12.    PURPOSE.
  13.  
  14.    Copyright (c) 1993  Microsoft Corporation.  All Rights Reserved.
  15.  
  16.  **************************************************************************/
  17.  
  18. #include <windows.h>
  19. #include <windowsx.h>
  20. #include <wing.h>
  21.  
  22. #include "cube.hpp"
  23. #include "dumb3d.hpp"
  24.  
  25. #if defined(WIN32)
  26. #define _export
  27. #endif
  28.  
  29. /**************************************************************************
  30.     Global Variables
  31.  **************************************************************************/
  32.  
  33. static char    szAppName[]="Spinning Cube";
  34.  
  35. //*** Global Windows needs
  36. static HINSTANCE    hInstApp;
  37. static BOOL            fAppActive;
  38. static HWND            hwndApp;
  39. static HPALETTE    hpalApp = 0;
  40. static HDC        hdcWinG;
  41. static HBITMAP OldBitmap;
  42. static HBITMAP WinGBitmap;
  43.  
  44. struct
  45. {
  46.     BITMAPINFOHEADER Header;
  47.     RGBQUAD aColorTable[256];
  48.  
  49. } HeaderAndPalette =
  50. {
  51.     sizeof(BITMAPINFOHEADER),
  52.     50, 50,
  53.     1, 8,
  54.     BI_RGB,
  55.     0, 0, 0, 0, 0
  56. };
  57.  
  58.  
  59. static int        DibWidth, DibHeight;
  60.  
  61. //*** Cube vertices, normals, shades, and modelling transform
  62. static point_4 CubeVertices[8] =
  63. {
  64.     point_4( -10,  10, -10 ),
  65.     point_4( -10,  10,  10 ),
  66.     point_4(  10,  10,  10 ),
  67.     point_4(  10,  10, -10 ),
  68.     point_4(  10, -10, -10 ),
  69.     point_4(  10, -10,  10 ),
  70.     point_4( -10, -10,  10 ),
  71.     point_4( -10, -10, -10 )
  72. };
  73. static vector_4   CubeSurfaceNormals[6];
  74. static real       CubeSurfaceShades[6];
  75. static matrix_4x4 CubeTransform;
  76.  
  77. //*** Cube edges - ordered indices into the vertex array
  78. const int CubeFaces[6][4] =
  79. {
  80.     0, 1, 2, 3,
  81.     2, 1, 6, 5,
  82.     3, 2, 5, 4,
  83.     0, 3, 4, 7,
  84.     1, 0, 7, 6,
  85.     4, 5, 6, 7
  86. };
  87.  
  88. //*** Cube colors - one RGB color per surface
  89. const unsigned char CubeColors[6][3] =
  90. {
  91.     240,  20,  20,        // Unsaturated Red
  92.      20, 240,  20,        // Unsaturated Green
  93.      20,  20, 240,        // Unsaturated Blue
  94.     128,  64,   0,        // Brown
  95.     240,  20, 240,        // Unsaturated Magenta
  96.     240, 240,  20        // Unsaturated Yellow
  97. };
  98.  
  99. //*** Lighting
  100. vector_4   LightSourceDirection;
  101. const real AmbientLight = 0.2;
  102.  
  103. //*** Viewing and perspective
  104. static matrix_4x4 ViewPerspective;
  105. static point_4    Viewpoint(60, 60, 60);
  106. static vector_4   Up(0, 1, 0);
  107. static point_4    Origin;
  108.  
  109. //*** Interaction
  110. static real  XMove,YMove;
  111. static short gSpinFlag = 1;
  112.  
  113. //*** Dithering
  114. static int DitherType = 0;
  115.  
  116. /**************************************************************************
  117.    Internal function declarations
  118.  **************************************************************************/
  119.  
  120. LONG FAR PASCAL _export AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  121. void AppExit(void);
  122. BOOL AppIdle(void);
  123. void AppPaint(HWND hwnd, HDC hdc);
  124.  
  125. void TransformCube(matrix_4x4 const &Transform);
  126. void ProjectAndDrawCube(HDC hdc, int XOffset, int YOffset);
  127.  
  128. /**************************************************************************
  129.     AppAbout
  130.  
  131.     Description:
  132.         This function handles messages belonging to the "About" dialog box.
  133.     The only message that it looks for is WM_COMMAND, indicating the user
  134.     has pressed the "OK" button.
  135.  **************************************************************************/
  136.  
  137. BOOL FAR PASCAL _export AppAbout(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  138. {
  139.     switch (msg)
  140.     {
  141.         case WM_COMMAND:
  142.             if (LOWORD(wParam) == IDOK)
  143.                 EndDialog(hwnd, TRUE);
  144.             break;
  145.  
  146.         case WM_INITDIALOG:
  147.             return TRUE;
  148.     }
  149.     return FALSE;
  150. }
  151.  
  152. /**************************************************************************
  153.     AppInit
  154.  
  155.     Description:
  156.         This is called when the application is first loaded. It initializes
  157.     all variables, registers the window class, and creates the main app
  158.     window.
  159.  **************************************************************************/
  160.  
  161. BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
  162. {
  163.     WNDCLASS cls;
  164.  
  165.     /* Save instance handle for DialogBoxs */
  166.     hInstApp = hInst;
  167.  
  168.     if (!hPrev)
  169.     {
  170.         //***  Register a class for the main application window
  171.         cls.hCursor        = LoadCursor(0,IDC_ARROW);
  172.  
  173.         //*** Just for fun, we'll draw our own spinning cube icon.
  174.         cls.hIcon          = 0;
  175.         cls.lpszMenuName   = "AppMenu";
  176.         cls.lpszClassName  = szAppName;
  177.         cls.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  178.         cls.hInstance      = hInst;
  179.         cls.style          = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW;
  180.         cls.lpfnWndProc    = (WNDPROC)AppWndProc;
  181.         cls.cbClsExtra     = 0;
  182.         cls.cbWndExtra     = 0;
  183.  
  184.         if (!RegisterClass(&cls))
  185.             return FALSE;
  186.     }
  187.  
  188.     //*** Set and normalize the light source
  189.     LightSourceDirection = vector_4(50, 30, -15);
  190.     LightSourceDirection.Normalize();
  191.  
  192.     //*** Distance to view plane:
  193.     ViewPerspective.SetElement(3, 2, 1/300.0);
  194.     ViewPerspective.SetElement(3, 3, 0);
  195.  
  196.     //*** Viewport scaling - some arbitrary number like 3.5 will do
  197.     ViewPerspective.SetElement(0, 0, 3.5);
  198.     ViewPerspective.SetElement(1, 1, 3.5);
  199.  
  200.     //*** Calculate the initial normals and shades
  201.     TransformCube(CubeTransform);
  202.  
  203.     //*** Then generate an interesting rotation for the spin
  204.     CubeTransform.ConcatenateYRotation(6.0);
  205.     CubeTransform.ConcatenateXRotation(3.5);
  206.     CubeTransform.ConcatenateZRotation(2.0);
  207.  
  208.     hwndApp = CreateWindow (szAppName,           // Class name
  209.                                     szAppName,           // Caption
  210.                                     WS_OVERLAPPED |
  211.                                     WS_CAPTION |
  212.                                     WS_SYSMENU |
  213.                                     WS_MINIMIZEBOX,      // Style bits
  214.                                     CW_USEDEFAULT, 0,    // Position
  215.                                     350,350,                 // Size
  216.                                     0,                   // Parent window (no parent)
  217.                                     0,                   // use class menu
  218.                                     hInst,               // handle to window instance
  219.                                     0                    // no params to pass on
  220.                                     );
  221.     hdcWinG = WinGCreateDC();
  222.  
  223.     ShowWindow(hwndApp,sw);
  224.  
  225.     //*** Check the default dither selection
  226.     HMENU hMenu = GetMenu(hwndApp);
  227.     CheckMenuItem(hMenu, MENU_DISPERSED8x8, MF_CHECKED);
  228.     CheckMenuItem(hMenu, MENU_SPIN, MF_CHECKED);
  229.  
  230.     return TRUE;
  231. }
  232.  
  233. /**************************************************************************
  234.     WinMain
  235.  
  236.     Description:
  237.         The main procedure for the App.  After initializing, it just goes
  238.     into a message-processing loop until it gets a WM_QUIT message.
  239.  **************************************************************************/
  240.  
  241. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  242. {
  243.     MSG     msg;
  244.  
  245.     //*** Call initialization procedure
  246.     if (!AppInit(hInst,hPrev,sw,szCmdLine))
  247.         return FALSE;
  248.  
  249.       //*** Polling messages from event queue until quit
  250.     for (;;)
  251.     {
  252.         if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  253.         {
  254.             if (msg.message == WM_QUIT)
  255.                 break;
  256.             TranslateMessage(&msg);
  257.             DispatchMessage(&msg);
  258.         }
  259.         else
  260.         {
  261.             if (AppIdle())
  262.                 WaitMessage();
  263.         }
  264.     }
  265.  
  266.     return msg.wParam;
  267. }
  268.  
  269. /**************************************************************************
  270.     AppIdle
  271.  
  272.     Description:
  273.  **************************************************************************/
  274.  
  275. BOOL AppIdle()
  276. {
  277.     //*** Spin while the app is active, lbutton is up, and spinning is on.
  278.     //*** Spin while the app is iconized.
  279.     if ( (gSpinFlag && fAppActive && GetKeyState(VK_LBUTTON) >= 0)
  280.             || IsIconic(hwndApp))
  281.     {
  282.         //*** If the app is active, spin the cube and redraw
  283.         TransformCube(CubeTransform);
  284.         HDC hdc = GetDC(hwndApp);
  285.         if (hpalApp)
  286.         {
  287.             SelectPalette(hdc, hpalApp, FALSE);
  288.             RealizePalette(hdc);
  289.         }
  290.         AppPaint(hwndApp, hdc);
  291.         ReleaseDC(hwndApp, hdc);
  292.         return FALSE;
  293.     }
  294.     else
  295.     {
  296.         //*** Don't do anything when not the active app
  297.         return TRUE;
  298.     }
  299. }
  300.  
  301. /**************************************************************************
  302.     AppPain