home *** CD-ROM | disk | FTP | other *** search
/ cyber.net interactivo 13 / cybernetinteractivo13.ISO / wing / cube.cp_ / cube.cp
Text File  |  1994-08-23  |  19KB  |  679 lines

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