home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / FLIP2D / FLIPCUBE.CPP < prev    next >
C/C++ Source or Header  |  1996-08-28  |  34KB  |  1,145 lines

  1. /**************************************************************************
  2.  
  3.     FLIPCUBE.CPP - A spinning cube demo for DirectDraw
  4.  
  5.     basic page fliping app, just render to the back buffer and flip
  6.     that is all I do.
  7.  
  8.  **************************************************************************/
  9. /**************************************************************************
  10.  
  11.     (C) Copyright 1995-1996 Microsoft Corp.  All rights reserved.
  12.  
  13.     You have a royalty-free right to use, modify, reproduce and
  14.     distribute the Sample Files (and/or any modified version) in
  15.     any way you find useful, provided that you agree that
  16.     Microsoft has no warranty obligations or liability for any
  17.     Sample Application Files which are modified.
  18.  
  19.  **************************************************************************/
  20.  
  21. #define INITGUID
  22.  
  23. #include <windows.h>
  24. #include <windowsx.h>
  25. #include <mmsystem.h>
  26. #include <ddraw.h>
  27. #include <dinput.h>
  28. #include <math.h>
  29.  
  30. #include "flipcube.h"
  31. #include "dumb3d.h"
  32.  
  33. // code is in tri.cpp
  34. extern void Triangle8(BYTE *p, int next_scan, POINT P0, POINT P1, POINT P2, DWORD c);
  35.  
  36. /**************************************************************************
  37.   Global Variables
  38.  **************************************************************************/
  39.  
  40. static char szAppName[]="DirectDraw Spinning Cube";
  41.  
  42. static HINSTANCE  hInstApp;
  43. static BOOL       fAppActive;
  44. static BOOL       fAppPaused;
  45. static HWND       hwndApp;
  46. static HACCEL     hAccelApp;
  47. static HFONT      AppFont;
  48. static SIZE       ScreenSize;
  49. static BOOL       fDrawWithGDI;
  50.  
  51. /**************************************************************************
  52.   DirectDraw Globals
  53.  **************************************************************************/
  54.  
  55. IDirectDraw            *dd;
  56. IDirectDrawSurface     *FrontBuffer;
  57. IDirectDrawSurface     *BackBuffer;
  58. IDirectDrawPalette     *Palette;
  59.  
  60. /**************************************************************************
  61.   DirectInput Globals
  62.  **************************************************************************/
  63. LPDIRECTINPUT                   lpdi;
  64. LPDIRECTINPUTDEVICE             lpdiZoom;       // Used for zooming
  65. LPDIRECTINPUTDEVICE             lpdiRot;        // Use for rotation
  66. BOOL                            fMouseAcquired = FALSE; // Acquired for rot'n
  67.  
  68. /**************************************************************************
  69.   dumb 3D Globals
  70.  **************************************************************************/
  71.  
  72. //*** Cube vertices, normals, shades, and modeling transform
  73. static point_4 CubeVertices[8] =
  74. {
  75.   point_4( -10,  10, -10 ),
  76.   point_4( -10,  10,  10 ),
  77.   point_4(  10,  10,  10 ),
  78.   point_4(  10,  10, -10 ),
  79.   point_4(  10, -10, -10 ),
  80.   point_4(  10, -10,  10 ),
  81.   point_4( -10, -10,  10 ),
  82.   point_4( -10, -10, -10 )
  83. };
  84. static vector_4   CubeSurfaceNormals[6];
  85. static real       CubeSurfaceShades[6];
  86. static matrix_4x4 CubeTransform;
  87.  
  88. //*** Cube edges - ordered indices into the vertex array
  89. const int CubeFaces[6][4] =
  90. {
  91.   0, 1, 2, 3,
  92.   2, 1, 6, 5,
  93.   3, 2, 5, 4,
  94.   0, 3, 4, 7,
  95.   1, 0, 7, 6,
  96.   4, 5, 6, 7
  97. };
  98.  
  99. //*** Cube colors - one RGB color per surface
  100. const unsigned char CubeColors[6][3] =
  101. {
  102.   240,  20,  20,    // Unsaturated Red
  103.    20, 240,  20,    // Unsaturated Green
  104.    20,  20, 240,    // Unsaturated Blue
  105.   128,  64,   0,    // Brown
  106.   240,  20, 240,    // Unsaturated Magenta
  107.   240, 240,  20     // Unsaturated Yellow
  108. };
  109.  
  110. //*** Lighting
  111. vector_4   LightSourceDirection;
  112. const real AmbientLight = 0.2;
  113.  
  114. //*** Viewing and perspective
  115. static matrix_4x4  ViewPerspective;
  116. static point_4     Viewpoint(60, 60, 60);
  117. static vector_4    Up(0, 1, 0);
  118. static point_4     Origin;
  119.  
  120. /**************************************************************************
  121.    Internal function declarations
  122.  **************************************************************************/
  123.  
  124. LONG  CALLBACK AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
  125. BOOL  InitDInput(void);
  126. BOOL  AppIdle(void);
  127. void  RenderFrame(void);
  128.  
  129. void  TransformCube(matrix_4x4 const &Transform);
  130. BOOL  ProjectAndDrawCube(HDC hdc, int XOffset, int YOffset);
  131. BOOL  ProjectAndDrawCube(IDirectDrawSurface *pdds, int XOffset, int YOffset);
  132.  
  133. /**************************************************************************
  134.   AppAbout
  135.  
  136.   Description:
  137.     This function handles messages belonging to the "About" dialog box.
  138.   The only message that it looks for is WM_COMMAND, indicating the user
  139.   has pressed the "OK" button.
  140.  **************************************************************************/
  141.  
  142. BOOL FAR PASCAL AppAbout(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  143. {
  144.   switch (msg)
  145.   {
  146.     case WM_COMMAND:
  147.       if (LOWORD(wParam) == IDOK)
  148.         EndDialog(hwnd, TRUE);
  149.       break;
  150.  
  151.     case WM_INITDIALOG:
  152.       return TRUE;
  153.   }
  154.   return FALSE;
  155. }
  156.  
  157. /**************************************************************************
  158.   DDInit
  159.  
  160.   Description:
  161.     initialize all the DirectDraw specific stuff
  162.  **************************************************************************/
  163.  
  164. BOOL DDInit()
  165. {
  166.     HRESULT err;
  167.  
  168.     err = DirectDrawCreate(NULL, &dd, NULL);
  169.  
  170.     if (err != DD_OK)
  171.         return FALSE;
  172.  
  173.     err = dd->SetCooperativeLevel(hwndApp,
  174.         DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX);
  175.  
  176.     if (err != DD_OK)
  177.         return FALSE;
  178.  
  179.         // NEWNEW init DirectInput iff DDraw inits ok
  180.         if(!InitDInput())
  181.                 return FALSE;
  182.  
  183.     return TRUE;
  184. }
  185.  
  186. /**************************************************************************
  187.   CreateMouse
  188.  **************************************************************************/
  189.  
  190. BOOL CreateMouse(GUID &guid, LPDIRECTINPUTDEVICE& lpdiMouse, DWORD dwAccess)
  191. {
  192.     HRESULT err;
  193.  
  194.     err = lpdi->CreateDevice(guid, &lpdiMouse, NULL);
  195.  
  196.     if(err != DI_OK)
  197.     {
  198.             MessageBox(NULL, "Unable to Create DirectInput Mouse Device",
  199.                     "DirectDraw Spinning Cube", MB_OK);
  200.             goto fail;
  201.     }
  202.  
  203.     // Tell DirectInput that we want to receive data in mouse format
  204.     err = lpdiMouse->SetDataFormat(&c_dfDIMouse);
  205.  
  206.     if(err != DI_OK)
  207.     {
  208.             MessageBox(NULL, "Unable to Access DirectInput Device as a mouse",
  209.                     "DirectDraw Spinning Cube", MB_OK);
  210.             goto fail;
  211.     }
  212.  
  213.     // set desired access mode
  214.     err = lpdiMouse->SetCooperativeLevel(hwndApp, dwAccess);
  215.     if(err != DI_OK)
  216.     {
  217.             MessageBox(NULL, "Unable to set cooperativity level",
  218.                     "DirectDraw Spinning Cube", MB_OK);
  219.             goto fail;
  220.     }
  221.  
  222.     return TRUE;
  223.  
  224. fail:;
  225.     if (lpdiMouse)      lpdiMouse->Release(),       lpdiMouse = 0;
  226.     return FALSE;
  227. }
  228.  
  229. /**************************************************************************
  230.   InitDInput
  231.  **************************************************************************/
  232. BOOL InitDInput(void)
  233. {
  234.         HRESULT         err;
  235.         GUID     guid = GUID_SysMouse;
  236.  
  237.         err = DirectInputCreate(hInstApp, DIRECTINPUT_VERSION, &lpdi, NULL);
  238.  
  239.         if(err != DI_OK)
  240.         {
  241.                 MessageBox(NULL, "Unable to Create DirectInput Object",
  242.                         "DirectDraw Spinning Cube", MB_OK);
  243.                 return FALSE;
  244.         }
  245.  
  246.         // Create a mouse for zooming.  Zooming is done non-exclusively.
  247.         if (!CreateMouse(guid, lpdiZoom, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND))
  248.         {
  249.                 goto fail;
  250.         }
  251.  
  252.         if (fAppActive) lpdiZoom->Acquire();
  253.  
  254.         // Create a mouse for rotation.  Rotation is done exclusively.
  255.         if (!CreateMouse(guid, lpdiRot, DISCL_EXCLUSIVE | DISCL_FOREGROUND))
  256.         {
  257.                 goto fail;
  258.         }
  259.  
  260.         // if we get here, all DirectInput objects were created ok
  261.         return TRUE;
  262.  
  263. fail:
  264.         if (lpdiZoom)  lpdiZoom->Release(), lpdiZoom = NULL;
  265.         if (lpdiRot)   lpdiRot ->Release(), lpdiRot  = NULL;
  266.         if (lpdi)      lpdi    ->Release(), lpdi     = NULL;
  267.         return FALSE;
  268.  
  269. }
  270.  
  271.  
  272. /**************************************************************************
  273.   DDSetMode
  274.  **************************************************************************/
  275.  
  276. BOOL DDSetMode(int width, int height, int bpp)
  277. {
  278.     HRESULT err;
  279.  
  280.     err = dd->SetDisplayMode(width, height, bpp);
  281.  
  282.     if (err != DD_OK)
  283.         return FALSE;
  284.  
  285.     ScreenSize.cx = width;
  286.     ScreenSize.cy = height;
  287.  
  288.     // get rid of any previous surfaces.
  289.     if (BackBuffer)  BackBuffer->Release(),     BackBuffer = NULL;
  290.     if (FrontBuffer) FrontBuffer->Release(),    FrontBuffer = NULL;
  291.     if (Palette)     Palette->Release(),        Palette = NULL;
  292.  
  293.     //
  294.     // Create surfaces
  295.     //
  296.     // what we want is a tripple buffered surface in video memory
  297.     // so we try to create this first.
  298.     //
  299.     // if we cant get a triple buffered surface, we try again
  300.     // for a double buffered surface (still must be in video memory)
  301.     //
  302.     // if we cant get a double buffered surface, we try for a double
  303.     // buffered surface not being specific about video memory, we will
  304.     // get back a main-memory surface, that work use HW page flipping
  305.     // but at least we run.
  306.     //
  307.     // NOTE you need to recreate the surfaces for a new display mode
  308.     // they wont work when/if the mode is changed.
  309.     //
  310.     DDSURFACEDESC ddsd;
  311.  
  312.     ZeroMemory(&ddsd, sizeof(ddsd));
  313.     ddsd.dwSize = sizeof(ddsd);
  314.     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  315.     ddsd.dwBackBufferCount = 2;
  316.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
  317.                           DDSCAPS_FLIP |
  318.                           DDSCAPS_COMPLEX |
  319.                           DDSCAPS_VIDEOMEMORY;
  320.  
  321.     // try to get a triple buffered video memory surface.
  322.     err = dd->CreateSurface(&ddsd, &FrontBuffer, NULL);
  323.  
  324.     if (err != DD_OK)
  325.     {
  326.         // try to get a double buffered video memory surface.
  327.         ddsd.dwBackBufferCount = 1;
  328.         err = dd->CreateSurface(&ddsd, &FrontBuffer, NULL);
  329.     }
  330.  
  331.     if (err != DD_OK)
  332.     {
  333.         // settle for a main memory surface.
  334.         ddsd.ddsCaps.dwCaps &= ~DDSCAPS_VIDEOMEMORY;
  335.         err = dd->CreateSurface(&ddsd, &FrontBuffer, NULL);
  336.     }
  337.  
  338.     if (err != DD_OK)
  339.         return FALSE;
  340.  
  341.     // get a pointer to the back buffer
  342.     DDSCAPS caps;
  343.     caps.dwCaps = DDSCAPS_BACKBUFFER;
  344.     err = FrontBuffer->GetAttachedSurface(&caps, &BackBuffer);
  345.  
  346.     if (err != DD_OK)
  347.         return FALSE;
  348.  
  349.     // create a palette if we are in a paletized display mode.
  350.     //
  351.     // NOTE because we want to be able to show dialog boxs and
  352.     // use our menu, we leave the windows reserved colors as is
  353.     // so things dont look ugly.
  354.     //
  355.     // palette is setup like so:
  356.     //
  357.     //      10      windows system colors
  358.     //      64      red wash
  359.     //      64      grn wash
  360.     //      64      blu wash
  361.     //
  362.     PALETTEENTRY ape[256];
  363.     HDC hdc = GetDC(NULL);
  364.     if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
  365.     {
  366.         // get the current windows colors.
  367.         GetSystemPaletteEntries(hdc, 0, 256, ape);
  368.  
  369.         // make a red, grn, and blu wash for our cube.
  370.         for (int i=0; i<64; i++)
  371.         {
  372.             ape[10+64*0+i].peRed   = i * 255/63;
  373.             ape[10+64*0+i].peGreen = 0;
  374.             ape[10+64*0+i].peBlue  = 0;
  375.  
  376.             ape[10+64*1+i].peRed   = 0;
  377.             ape[10+64*1+i].peGreen = i * 255/63;
  378.             ape[10+64*1+i].peBlue  = 0;
  379.  
  380.             ape[10+64*2+i].peRed   = 0;
  381.             ape[10+64*2+i].peGreen = 0;
  382.             ape[10+64*2+i].peBlue  = i * 255/63;
  383.         }
  384.  
  385.         // create the palette.
  386.         err = dd->CreatePalette(DDPCAPS_8BIT, ape, &Palette, NULL);
  387.  
  388.         if (err == DD_OK)
  389.         {
  390.             FrontBuffer->SetPalette(Palette);
  391.         }
  392.     }
  393.     ReleaseDC(NULL, hdc);
  394.  
  395.     if (AppFont)
  396.         DeleteObject(AppFont);
  397.  
  398.     AppFont = CreateFont(width < 640 ? 24 : 48,
  399.         0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  400.         ANSI_CHARSET,
  401.         OUT_DEFAULT_PRECIS,
  402.         CLIP_DEFAULT_PRECIS,
  403.         NONANTIALIASED_QUALITY,
  404.         VARIABLE_PITCH,
  405.         "Comic Sans MS");
  406.  
  407.     return TRUE;
  408. }
  409.  
  410.  
  411. /**************************************************************************
  412.   DDTerm
  413.  **************************************************************************/
  414.  
  415. void DDTerm()
  416. {
  417.     if (BackBuffer)  BackBuffer->Release(),     BackBuffer = NULL;
  418.     if (FrontBuffer) FrontBuffer->Release(),    FrontBuffer = NULL;
  419.     if (Palette)     Palette->Release(),        Palette = NULL;
  420.     if (dd)          dd->Release(),             dd = NULL;
  421.     if (lpdi)        lpdi->Release(),           lpdi = NULL;
  422. }
  423.  
  424. /**************************************************************************
  425.   ModeCallback
  426.  **************************************************************************/
  427.  
  428. HRESULT CALLBACK ModeCallback(LPDDSURFACEDESC pdds, LPVOID lParam)
  429. {
  430.     HMENU hmenu = (HMENU)lParam;
  431.     char ach[80];
  432.     int n;
  433.     int width  = pdds->dwWidth;
  434.     int height = pdds->dwHeight;
  435.     int bpp    = pdds->ddpfPixelFormat.dwRGBBitCount;
  436.  
  437.     n = GetMenuItemCount(hmenu);
  438.     wsprintf(ach,"%dx%dx%d",width,height,bpp);
  439.     AppendMenu(hmenu,MF_STRING,MENU_MODE+n,ach);
  440.  
  441.     MENUITEMINFO mii;
  442.  
  443.     // pack the mode info into a DWORD and set the extra item data.
  444.     mii.cbSize = sizeof(mii);
  445.     mii.fMask = MIIM_DATA;
  446.     mii.dwItemData = width | (height << 12) | (bpp << 24);
  447.     SetMenuItemInfo(hmenu, MENU_MODE+n, MF_BYCOMMAND, &mii);
  448.  
  449.     //return S_TRUE to stop enuming modes, S_FALSE to continue
  450.     return S_FALSE;
  451. }
  452.  
  453. /**************************************************************************
  454.   AppInit
  455.  
  456.   Description:
  457.     This is called when the application is first loaded. It initializes
  458.   all variables, registers the window class, and creates the main app
  459.   window.
  460.  **************************************************************************/
  461.  
  462. BOOL AppInit(HINSTANCE hInst,HINSTANCE hPrev,int sw,LPSTR szCmdLine)
  463. {
  464.   WNDCLASS cls;
  465.  
  466.   /* Save instance handle for DialogBoxes */
  467.   hInstApp = hInst;
  468.  
  469.   if (!hPrev)
  470.   {
  471.     //***  Register a class for the main application window
  472.     cls.hCursor        = LoadCursor(0,IDC_ARROW);
  473.  
  474.     //*** Just for fun, we'll draw our own spinning cube icon.
  475.     cls.hIcon          = LoadIcon(hInst, "AppIcon");
  476.     cls.lpszMenuName   = "AppMenu";
  477.     cls.lpszClassName  = szAppName;
  478.     cls.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  479.     cls.hInstance      = hInst;
  480.     cls.style          = CS_VREDRAW | CS_HREDRAW;
  481.     cls.lpfnWndProc    = (WNDPROC)AppWndProc;
  482.     cls.cbClsExtra     = 0;
  483.     cls.cbWndExtra     = 0;
  484.  
  485.     if (!RegisterClass(&cls))
  486.       return FALSE;
  487.   }
  488.  
  489.   hAccelApp = LoadAccelerators(hInst, "AppAccel");
  490.  
  491.   //*** Set and normalize the light source
  492.   LightSourceDirection = vector_4(50, 30, -15);
  493.   LightSourceDirection.Normalize();
  494.  
  495.   //*** Distance to view plane:
  496.   ViewPerspective.SetElement(3, 2, 1/300.0);
  497.   ViewPerspective.SetElement(3, 3, 0);
  498.  
  499.   //*** Viewport scaling - some arbitrary number like 3.5 will do
  500.   ViewPerspective.SetElement(0, 0, 3.5);
  501.   ViewPerspective.SetElement(1, 1, 3.5);
  502.  
  503.   //*** Calculate the initial normals and shades
  504.   TransformCube(CubeTransform);
  505.  
  506.   //*** Then generate an interesting rotation for the spin
  507.   CubeTransform.ConcatenateYRotation(6.0);
  508.   CubeTransform.ConcatenateXRotation(3.5);
  509.   CubeTransform.ConcatenateZRotation(2.0);
  510.  
  511.   hwndApp = CreateWindowEx(
  512.                   WS_EX_APPWINDOW,
  513.                   szAppName,           // Class name
  514.                   szAppName,           // Caption
  515.                   WS_POPUP |
  516.                   WS_SYSMENU |
  517.                   WS_CAPTION,
  518.                   0, 0,                // Position
  519.                   640,480,             // Size
  520.                   0,                   // Parent window (no parent)
  521.                   0,                   // use class menu
  522.                   hInst,               // handle to window instance
  523.                   0                    // no params to pass on
  524.                   );
  525.   ShowWindow(hwndApp,sw);
  526.   UpdateWindow(hwndApp);
  527.  
  528.   if (!DDInit())
  529.     return FALSE;
  530.  
  531.   // Enumerate all posible display modes, and stick them in our menu.
  532.   // we use the extra item DWORD of a menu item to store the mode info
  533.   HMENU hmenu = CreatePopupMenu();
  534.   dd->EnumDisplayModes(0,NULL,(LPVOID)hmenu,ModeCallback);
  535.   AppendMenu(GetMenu(hwndApp),MF_POPUP,(UINT)hmenu,"Modes");
  536.  
  537.   if (!DDSetMode(640,480,8) &&
  538.       !DDSetMode(640,480,16))
  539.     return FALSE;
  540.  
  541.   return TRUE;
  542. }
  543.  
  544. /**************************************************************************
  545.   WinMain
  546.  
  547.   Description:
  548.     The main procedure for the App.  After initializing, it just goes
  549.   into a message-processing loop until it gets a WM_QUIT message.
  550.  **************************************************************************/
  551.  
  552. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  553. {
  554.   MSG     msg;
  555.  
  556.   //*** Call initialization procedure
  557.   if (!AppInit(hInst,hPrev,sw,szCmdLine))
  558.     return FALSE;
  559.  
  560.     //*** Polling messages from event queue until quit
  561.   for (;;)
  562.   {
  563.     if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  564.     {
  565.       if (msg.message == WM_QUIT)
  566.         break;
  567.  
  568.       if (!hwndApp || !TranslateAccelerator(hwndApp, hAccelApp, &msg))
  569.       {
  570.         TranslateMessage(&msg);
  571.         DispatchMessage(&msg);
  572.       }
  573.     }
  574.     else
  575.     {
  576.       if (AppIdle())
  577.         WaitMessage();
  578.     }
  579.   }
  580.  
  581.   DDTerm();
  582.   return msg.wParam;
  583. }
  584.  
  585. /**************************************************************************
  586.   AppPause
  587.  
  588.  **************************************************************************/
  589.  
  590. void AppPause(BOOL f)
  591. {
  592.     if (f)
  593.     {
  594.         DDSCAPS caps;
  595.         FrontBuffer->GetCaps(&caps);
  596.  
  597.         // if we are in ModeX go back to a windows mode
  598.         // so we can see the menu or dialog box.
  599.  
  600.         if (caps.dwCaps & DDSCAPS_MODEX)
  601.         {
  602.             DDSetMode(640,480,8);
  603.         }
  604.  
  605.         // turn off rotation while paused
  606.         if (lpdiRot) lpdiRot->Unacquire();
  607.         fMouseAcquired = FALSE;
  608.  
  609.         fAppPaused = TRUE;
  610.         dd->FlipToGDISurface();
  611.         DrawMenuBar(hwndApp);
  612.         RedrawWindow(hwndApp, NULL, NULL, RDW_FRAME);
  613.     }
  614.     else
  615.     {
  616.         fAppPaused = FALSE;
  617.  
  618.     }
  619. }
  620.  
  621. /**************************************************************************
  622.   MagnifyCube
  623.  
  624.   Description:
  625.     Magnify the cube the indicated number of times.  A negative number
  626.     makes it smaller.
  627.  **************************************************************************/
  628.  
  629. void MagnifyCube(double times)
  630. {
  631.     matrix_4x4 m;
  632.     double factor = pow(1.5, times);
  633.     m.SetElement(0,0,factor);
  634.     m.SetElement(1,1,factor);
  635.     m.SetElement(2,2,factor);
  636.     TransformCube(m);
  637. }
  638.  
  639. /**************************************************************************
  640.   AppIdle
  641.  
  642.   return TRUE if the app is idle
  643.   return FALSE if the app is not idle.
  644.  
  645.   Description:
  646.  **************************************************************************/
  647.  
  648. BOOL AppIdle()
  649. {
  650.   DIMOUSESTATE dims;
  651.  
  652.   //*** Spin while the app is active, lbutton is up, and spinning is on.
  653.  
  654.   //*** Spin while the app is iconized.
  655.   if (fAppActive && !fAppPaused)
  656.   {
  657.     //*** If the app is active, spin the cube and redraw
  658.  
  659.     // See if any zooming needs to be done.
  660.     if(lpdiZoom->GetDeviceState(sizeof(DIMOUSESTATE), &dims) == DI_OK) {
  661.         // 240 units of motion in the Z-axis equals one unit of
  662.         // magnification / shrinkage.
  663.         if(dims.lZ) {
  664.            MagnifyCube(dims.lZ / 240.0);
  665.         }
  666.     }
  667.  
  668.     if(fMouseAcquired)
  669.          {
  670.                 //** If we have the mouse acquired...
  671.  
  672.                 // user spins cube if GetDeviceState succeeds and if the left button (button 0) is held
  673.                 if(lpdiRot->GetDeviceState(sizeof(DIMOUSESTATE), &dims) == DI_OK)
  674.                 {
  675.          if(dims.rgbButtons[0] & 0x80)
  676.          {
  677.             if(dims.lX || dims.lY)
  678.             {
  679.                matrix_4x4 Movement;
  680.                Movement.ConcatenateYRotation(dims.lX);
  681.                Movement.ConcatenateXRotation(dims.lY);
  682.                TransformCube(Movement);
  683.             }
  684.  
  685.          }
  686.          else
  687.                    {
  688.                            // unacquire the mouse
  689.                            lpdiRot->Unacquire();
  690.                            fMouseAcquired = FALSE;
  691.                    }
  692.       }
  693.     }
  694.     else
  695.     {
  696.          TransformCube(CubeTransform);
  697.     }
  698.     RenderFrame();
  699.     return FALSE;
  700.   }
  701.   else
  702.   {
  703.     //*** Don't do anything when not the active app
  704.     return TRUE;
  705.   }
  706. }
  707.  
  708. /**************************************************************************
  709.   RenderFrame
  710.  
  711.   render the frame into the back buffer and do a page flip.
  712.  
  713.   things to NOTE:
  714.  
  715.     we use the blter to clear the backbuffer, this usualy is a big
  716.     win blters are real fast.
  717.  
  718.     we use GDI to draw the frame rate, and info text
  719.  
  720.     we either use GDI to draw the faces of the cube, or our own code
  721.     based on the fDrawWithGDI global variable.
  722.  
  723.  **************************************************************************/
  724.  
  725. int FrameRate;
  726. int FrameCount;
  727. int FrameCount0;
  728. DWORD FrameTime;
  729. DWORD FrameTime0;
  730.  
  731. void RenderFrame()
  732. {
  733.   HDC hdc;
  734.  
  735.   //*** always need to handle DDERR_SURFACELOST, this will happen
  736.   //*** when we get switched away from.
  737.  
  738.   if (FrontBuffer->IsLost() == DDERR_SURFACELOST)
  739.     FrontBuffer->Restore();
  740.  
  741.   //*** use the blter to do a color fill to clear the back buffer
  742.  
  743.   DDBLTFX ddbltfx;
  744.   ddbltfx.dwSize = sizeof(ddbltfx);
  745.   ddbltfx.dwFillColor = 0;
  746.   BackBuffer->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT,&ddbltfx);
  747.  
  748.   //*** based on the fDrawWithGDI global variable, we either
  749.   //*** render the polygons ourself or let GDI do it
  750.  
  751.   BOOL fGDI = fDrawWithGDI;
  752.  
  753.   //*** render the cube with our own code.
  754.   //*** we need to do this outside of the GetDC, because we cant
  755.   //*** lock a buffer while we have a DC on it.
  756.   //*** if ProjectAndDrawCube returns FALSE it did not want to
  757.   //*** draw for some reason, so go ahead and use GDI
  758.  
  759.   if (!fGDI)
  760.      fGDI = !ProjectAndDrawCube(BackBuffer, ScreenSize.cx/2, ScreenSize.cy/2);
  761.  
  762.   if (BackBuffer->GetDC(&hdc) == DD_OK)
  763.   {
  764.     //*** use GDI to draw the cube.
  765.     if (fGDI)
  766.       ProjectAndDrawCube(hdc, ScreenSize.cx/2, ScreenSize.cy/2);
  767.  
  768.     //*** draw stats, like frame number and frame rate
  769.  
  770.     char ach[128];
  771.     int len;
  772.     static char szHelp[] = "F10=Menu F7=Smaller F8=Larger";
  773.  
  774.     SetBkMode(hdc, TRANSPARENT);
  775.     SelectObject(hdc, AppFont);
  776.  
  777.     len = wsprintf(ach, "FPS %02d Frame %05d", FrameRate, FrameCount);
  778.  
  779.     SetTextColor(hdc, RGB(255, 255, 0));
  780.     TextOut(hdc, 0, 0, ach, len);
  781.     TextOut(hdc, 0, ScreenSize.cy-(ScreenSize.cx<640 ? 24:48),szHelp,sizeof(szHelp)-1);
  782.  
  783.     BackBuffer->ReleaseDC(hdc);
  784.   }
  785.  
  786.   //*** we have rendered the backbuffer, call flip so we can see it
  787.   FrontBuffer->Flip(NULL, DDFLIP_WAIT);
  788.  
  789.   FrameCount++;
  790.   FrameTime = timeGetTime();
  791.  
  792.   if (FrameTime - FrameTime0 > 1000)
  793.   {
  794.     FrameRate = (FrameCount - FrameCount0) * 1000 / (FrameTime - FrameTime0);
  795.     FrameTime0 = FrameTime;
  796.     FrameCount0 = FrameCount;
  797.   }
  798. }
  799.  
  800. /**************************************************************************
  801.   AppWndProc
  802.  
  803.   Description:
  804.     Main window proc. Standard Windows fare.
  805.  **************************************************************************/
  806.  
  807. LONG CALLBACK AppWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  808. {
  809.   RECT Rect;
  810.  
  811.   switch (msg)
  812.   {
  813.     case WM_CREATE:
  814.       break;
  815.  
  816.     case WM_ACTIVATEAPP:
  817.       //*** Keep track of whether or not the app is in the foreground
  818.       fAppActive = (BOOL)wParam;
  819.           // re-acquire the zooming controller when we are activated
  820.           if (fAppActive) {
  821.                 if (lpdiZoom) lpdiZoom->Acquire();
  822.           } else{           // unacquire everything if app is not active
  823.                 if (lpdiZoom) lpdiZoom->Unacquire();
  824.                 if (lpdiRot) lpdiRot->Unacquire();
  825.                 fMouseAcquired = FALSE;
  826.           }
  827.       break;
  828.  
  829.     case WM_SETCURSOR:
  830.       if (fAppActive && !fAppPaused)
  831.       {
  832.         SetCursor(NULL);
  833.         return 1;
  834.       }
  835.       break;
  836.  
  837.     case WM_ENTERMENULOOP:
  838.       AppPause(TRUE);
  839.       break;
  840.  
  841.     case WM_EXITMENULOOP:
  842.       AppPause(FALSE);
  843.       break;
  844.  
  845.     case WM_INITMENUPOPUP:
  846.         CheckMenuItem((HMENU)wParam, MENU_GDI, fDrawWithGDI ? MF_CHECKED : MF_UNCHECKED);
  847.         break;
  848.  
  849.     case WM_COMMAND:
  850.       switch(LOWORD(wParam))
  851.       {
  852.         case MENU_ABOUT:
  853.           AppPause(TRUE);
  854.           DialogBox(hInstApp, "AppAbout", hwnd, (DLGPROC)AppAbout);
  855.           AppPause(FALSE);
  856.           break;
  857.  
  858.         case MENU_EXIT:
  859.           PostMessage(hwnd, WM_CLOSE, 0, 0L);
  860.           break;
  861.  
  862.         case MENU_LARGER:
  863.           MagnifyCube(+1.0);
  864.           break;
  865.  
  866.         case MENU_SMALLER:
  867.           MagnifyCube(-1.0);
  868.           break;
  869.  
  870.         case MENU_GDI:
  871.           fDrawWithGDI = !fDrawWithGDI;
  872.           break;
  873.       }
  874.       if (LOWORD(wParam) >= MENU_MODE && LOWORD(wParam) < MENU_MODE+100)
  875.       {
  876.         MENUITEMINFO mii;
  877.  
  878.         mii.cbSize = sizeof(mii);
  879.         mii.fMask = MIIM_DATA;
  880.         GetMenuItemInfo(GetMenu(hwnd), LOWORD(wParam), MF_BYCOMMAND, &mii);
  881.  
  882.         DDSetMode(
  883.             (mii.dwItemData >> 0)  & 0xFFF,
  884.             (mii.dwItemData >> 12) & 0xFFF,
  885.             (mii.dwItemData >> 24) & 0x0FF);
  886.       }
  887.       return 0L;
  888.  
  889.     case WM_DESTROY:
  890.           // clean up DirectInput objects
  891.           if (fMouseAcquired) lpdiRot->Unacquire();
  892.           if (lpdiZoom)  lpdiZoom->Release(), lpdiZoom = NULL;
  893.           if (lpdiRot)   lpdiRot ->Release(), lpdiRot  = NULL;
  894.           if (lpdi)      lpdi    ->Release(), lpdi     = NULL;
  895.  
  896.       hwndApp = NULL;
  897.       PostQuitMessage(0);
  898.       break;
  899.  
  900.     case WM_PAINT:
  901.       break;
  902.  
  903.     case WM_MOVE:
  904.     case WM_SIZE:
  905.     case WM_DISPLAYCHANGE:
  906.       if (fAppActive && !IsIconic(hwnd))
  907.       {
  908.         SetRect(&Rect, 0, GetSystemMetrics(SM_CYCAPTION), GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
  909.         AdjustWindowRectEx(&Rect, WS_POPUP | WS_CAPTION, FALSE, 0);
  910.         SetWindowPos(hwnd, NULL, Rect.left, Rect.top, Rect.right-Rect.left, Rect.bottom-Rect.top, SWP_NOACTIVATE | SWP_NOZORDER);
  911.       }
  912.       break;
  913.  
  914.     case WM_LBUTTONDOWN:
  915.     case WM_RBUTTONDOWN:
  916.     case WM_MBUTTONDOWN:
  917.           if(lpdiRot->Acquire() == DI_OK)
  918.      {
  919.         fMouseAcquired = TRUE;
  920.      }
  921.           else
  922.           {
  923.                 // not acquired, mouse will not do anything
  924.           }
  925.       break;
  926.  
  927.   }
  928.  
  929.   return DefWindowProc(hwnd,msg,wParam,lParam);
  930. }
  931.  
  932. /**************************************************************************
  933.   TransformCube
  934.  
  935.   Description:
  936.     Transforms the cube vertices by the current rotation matrix.
  937.     Recalculates normals and flat shade values for the
  938.   directional light source.
  939.  **************************************************************************/
  940.  
  941. void TransformCube(matrix_4x4 const &Transform)
  942. {
  943.   int i;
  944.  
  945.   //*** Transform the cube by the matrix
  946.   for (i = 0; i < 8; ++i)
  947.     CubeVertices[i] = Transform * CubeVertices[i];
  948.  
  949.   //*** Recalculate normals and shades
  950.   for (i = 0; i < 6; ++i)
  951.   {
  952.     //*** Normals are perpendicular to two edges of the cube
  953.     vector_4 Edge1, Edge2;
  954.     Edge1 = CubeVertices[CubeFaces[i][1]] - CubeVertices[CubeFaces[i][0]];
  955.     Edge2 = CubeVertices[CubeFaces[i][3]] - CubeVertices[CubeFaces[i][0]];
  956.     CubeSurfaceNormals[i] = CrossProduct(Edge1, Edge2);
  957.     CubeSurfaceNormals[i].Normalize();
  958.  
  959.     //*** Cosine shading based on the surface normal, clamped to [0, 1]
  960.     real Shade = DotProduct(CubeSurfaceNormals[i], LightSourceDirection);
  961.     Shade = Shade + AmbientLight;
  962.     if (Shade < 0) Shade = 0;
  963.     else if (Shade > 1.0) Shade = 1.0;
  964.     CubeSurfaceShades[i] = Shade;
  965.   }
  966. }
  967.  
  968. /**************************************************************************
  969.   ProjectAndDrawCube
  970.  
  971.   Description:
  972.     Projects the cube vertices for the current viewpoint then culls
  973.   in screen space and draws into the DC via GDI.
  974.  **************************************************************************/
  975.  
  976. BOOL ProjectAndDrawCube(HDC hdc, int XOffset, int YOffset)
  977. {
  978.   //*** Create a viewing transform for the current eye position
  979.   vector_4 ViewDirection = Origin - Viewpoint;
  980.   ViewDirection.Normalize();
  981.   view_transform View(Viewpoint, ViewDirection, Up);
  982.  
  983.   //*** Transform and project the vertices into screen space
  984.   int i;
  985.   POINT aScreenVertices[8];
  986.   for (i = 0; i < 8; ++i)
  987.   {
  988.     point_4 Temp = View * CubeVertices[i];
  989.     Temp = ViewPerspective * Temp;
  990.     Temp.Homogenize();
  991.  
  992.     aScreenVertices[i].x = (int)Temp.GetX() + XOffset;
  993.     aScreenVertices[i].y = (int)Temp.GetY() + YOffset;
  994.   }
  995.  
  996.   SelectPen(hdc, GetStockPen(NULL_PEN));
  997.  
  998.   for (i = 0; i < 6; ++i)
  999.   {
  1000.     //*** Standard culling operation based on the z value of the
  1001.     //*** cross product of the edges: are the vertices oriented in the
  1002.     //*** counterclockwise or clockwise direction?
  1003.     real v1 = aScreenVertices[ CubeFaces[i][2] ].x -
  1004.       aScreenVertices[ CubeFaces[i][1] ].x;
  1005.     real w1 = aScreenVertices[ CubeFaces[i][0] ].x -
  1006.       aScreenVertices[ CubeFaces[i][1] ].x;
  1007.     real v2 = aScreenVertices[ CubeFaces[i][2] ].y -
  1008.       aScreenVertices[ CubeFaces[i][1] ].y;
  1009.     real w2 = aScreenVertices[ CubeFaces[i][0] ].y -
  1010.       aScreenVertices[ CubeFaces[i][1] ].y;
  1011.     if ((v1*w2 - v2*w1) <= 0)
  1012.       continue;
  1013.  
  1014.     //*** Create a brush for the shaded face color using the selected dither
  1015.  
  1016.     HBRUSH hbr;
  1017.  
  1018.     //*** Get the shading colors
  1019.  
  1020.     int Red, Green, Blue;
  1021.  
  1022.     Red   = (int)(CubeColors[i][0] * CubeSurfaceShades[i]);
  1023.     Green = (int)(CubeColors[i][1] * CubeSurfaceShades[i]);
  1024.     Blue  = (int)(CubeColors[i][2] * CubeSurfaceShades[i]);
  1025.  
  1026.     //*** Create the dithered or PALETTERGB brush
  1027.  
  1028.     COLORREF cr;
  1029.  
  1030.     cr = RGB(Red, Green, Blue);
  1031.     hbr = CreateSolidBrush(cr);
  1032.  
  1033.     //*** Collect the correct points in an array
  1034.     POINT aQuadVertices[4];
  1035.     for (int j = 0; j < 4; ++j)
  1036.       aQuadVertices[j] = aScreenVertices[ CubeFaces[i][j] ];
  1037.  
  1038.     //*** Use GDI to draw the face
  1039.     hbr = SelectBrush(hdc, hbr);
  1040.     Polygon(hdc, aQuadVertices, 4);
  1041.     hbr = SelectBrush(hdc, hbr);
  1042.     DeleteObject(hbr);
  1043.   }
  1044.  
  1045.   return TRUE;
  1046. }
  1047.  
  1048. /**************************************************************************
  1049.   ProjectAndDrawCube
  1050.  
  1051.   Description:
  1052.     Projects the cube vertices for the current viewpoint then culls
  1053.   in screen space and draws them into a DirectDrawSurface via custom code
  1054.  **************************************************************************/
  1055.  
  1056. BOOL ProjectAndDrawCube(IDirectDrawSurface *pdds, int XOffset, int YOffset)
  1057. {
  1058.   //*** Lock the DirectDraw surface
  1059.   DDSURFACEDESC ddsd;
  1060.   ddsd.dwSize = sizeof(ddsd);
  1061.  
  1062.   if (pdds->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL) != DD_OK)
  1063.     return FALSE;
  1064.  
  1065.   //*** This code only works for 8bpp
  1066.   if (ddsd.ddpfPixelFormat.dwRGBBitCount != 8)
  1067.   {
  1068.     pdds->Unlock(NULL);
  1069.     return FALSE;
  1070.   }
  1071.  
  1072.   //*** Create a viewing transform for the current eye position
  1073.   vector_4 ViewDirection = Origin - Viewpoint;
  1074.   ViewDirection.Normalize();
  1075.   view_transform View(Viewpoint, ViewDirection, Up);
  1076.  
  1077.   //*** Transform and project the vertices into screen space
  1078.   int i;
  1079.   POINT aScreenVertices[8];
  1080.   for (i = 0; i < 8; ++i)
  1081.   {
  1082.     point_4 Temp = View * CubeVertices[i];
  1083.     Temp = ViewPerspective * Temp;
  1084.     Temp.Homogenize();
  1085.  
  1086.     aScreenVertices[i].x = (int)Temp.GetX() + XOffset;
  1087.     aScreenVertices[i].y = (int)Temp.GetY() + YOffset;
  1088.  
  1089.     //*** !!! OUR CODE DOES NOT CLIP, SO FAIL IF WE NEED CLIPPING
  1090.     if (aScreenVertices[i].x < 0 || aScreenVertices[i].x >= ScreenSize.cx ||
  1091.         aScreenVertices[i].y < 0 || aScreenVertices[i].y >= ScreenSize.cy)
  1092.     {
  1093.       pdds->Unlock(NULL);
  1094.       return FALSE;
  1095.     }
  1096.   }
  1097.  
  1098.   for (i = 0; i < 6; ++i)
  1099.   {
  1100.     //*** Standard culling operation based on the z value of the
  1101.     //*** cross product of the edges: are the vertices oriented in the
  1102.     //*** counterclockwise or clockwise direction?
  1103.     real v1 = aScreenVertices[ CubeFaces[i][2] ].x -
  1104.       aScreenVertices[ CubeFaces[i][1] ].x;
  1105.     real w1 = aScreenVertices[ CubeFaces[i][0] ].x -
  1106.       aScreenVertices[ CubeFaces[i][1] ].x;
  1107.     real v2 = aScreenVertices[ CubeFaces[i][2] ].y -
  1108.       aScreenVertices[ CubeFaces[i][1] ].y;
  1109.     real w2 = aScreenVertices[ CubeFaces[i][0] ].y -
  1110.       aScreenVertices[ CubeFaces[i][1] ].y;
  1111.     if ((v1*w2 - v2*w1) <= 0)
  1112.       continue;
  1113.  
  1114.     //*** Get the shading color, palette is setup like so:
  1115.     //*** 10 system, 64 red, 64 green, 64 blue
  1116.  
  1117.     BYTE color;
  1118.  
  1119.     if (CubeColors[i][0] >= 128)
  1120.         color = (BYTE)(10 + 0*64 + (63 * CubeSurfaceShades[i]));
  1121.     else if (CubeColors[i][1] >= 128)
  1122.         color = (BYTE)(10 + 1*64 + (63 * CubeSurfaceShades[i]));
  1123.     else
  1124.         color = (BYTE)(10 + 2*64 + (63 * CubeSurfaceShades[i]));
  1125.  
  1126.     //*** Use code in tri.cpp draw the face
  1127.  
  1128.     Triangle8((BYTE*)ddsd.lpSurface, ddsd.lPitch,
  1129.         aScreenVertices[CubeFaces[i][0]],
  1130.         aScreenVertices[CubeFaces[i][1]],
  1131.         aScreenVertices[CubeFaces[i][2]],
  1132.         color);
  1133.  
  1134.     Triangle8((BYTE*)ddsd.lpSurface, ddsd.lPitch,
  1135.         aScreenVertices[CubeFaces[i][2]],
  1136.         aScreenVertices[CubeFaces[i][3]],
  1137.         aScreenVertices[CubeFaces[i][0]],
  1138.         color);
  1139.   }
  1140.  
  1141.   //*** Never ever forget to unlock!
  1142.   pdds->Unlock(NULL);
  1143.   return TRUE;
  1144. }
  1145.