OpenGL实现3D魔方游戏源代码

【转】http://blog.csdn.net/hackbuteer1/article/details/6679557

首先这个程序是建立的是Windows应用程序,建立控制台程序是不能运行的,另外,项目——项目属性——配置属性——常规-----使用多字节字符集,这样编译才能够通过的,否则如果选择使用 Unicode 字符集,编译会有错误提示:error C2440: “初始化”: 无法从“const char [8]”转换为“LPCTSTR”,另外,链接器----输入----附加依赖项要加入:“opengl32.lib glu32.lib”的lib库。。

cubemanage.h文件为:

[cpp]  view plain copy
  1. #ifndef CUBEMANAGE_H  
  2. #define CUBEMANAGE_H  
  3.   
  4. #include <windows.h>  
  5. #include <gl/gl.h>  
  6. #include <gl/glu.h>  
  7. #include <math.h>  
  8.   
  9. #include "wcgcube.h"  
  10.   
  11. #define CUBE_SIZE  3  
  12. #define ORIENTX 0  
  13. #define ORIENTY 0  
  14. #define ORIENTZ 0  
  15.   
  16. class CubeManage {  
  17. public:   
  18.     CubeManage();  
  19.     ~CubeManage();  
  20.       
  21.     void turn(int rotateType);  
  22.     void turnByXShun(int x);  
  23.     void turnByXNi(int x);  
  24.     void turnByYShun(int y);  
  25.     void turnByYNi(int y);  
  26.     void turnByZShun(int z);  
  27.     void turnByZNi(int z);  
  28.     void output(int scr,int site);  
  29.     void output();  
  30.     void draw(int rotateType,GLfloat rotate);  
  31.       
  32. private:  
  33.     WcgCube *cubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];  
  34.       
  35.     void goStep(int *leftLeg,int *rightLeg,int *goDirection,int step,int leftEdge,int rightEdge);  
  36. };  
  37.   
  38. #endif  

wcgcube.h文件为:

[cpp]  view plain copy
  1. #ifndef WCGCUBE_H  
  2. #define WCGCUBE_H  
  3.   
  4. #include <windows.h>  
  5. #include <gl/gl.h>  
  6. #include <gl/glu.h>  
  7. #include <math.h>  
  8.   
  9. #include "iostream"  
  10. using namespace std;  
  11.   
  12. #define X 1  
  13. #define Y 2  
  14. #define Z 3  
  15.   
  16. class WcgCube {  
  17. public:  
  18.     WcgCube();    
  19.     ~WcgCube();  
  20.       
  21.     void turnByXShun(int x);  
  22.     void turnByXNi(int x);  
  23.     void turnByYShun(int y);  
  24.     void turnByYNi(int y);  
  25.     void turnByZShun(int z);  
  26.     void turnByZNi(int z);  
  27.     void output(int sign);  
  28.     void output();  
  29.     void draw(GLfloat x0,GLfloat y0,GLfloat z0);  
  30.       
  31. private:  
  32.     int direct[6];  
  33.     GLfloat sideColor[6][3];  
  34.       
  35.     void turnByX(int x,int sign);  
  36.     void turnByY(int y,int sign);  
  37.     void turnByZ(int z,int sign);  
  38. };  
  39.   
  40. #endif  

CubeGame.cpp文件为:

[cpp]  view plain copy
  1. #include <windows.h>  
  2. #include <winuser.h>  
  3. #include <gl/gl.h>  
  4. #include <gl/glu.h>  
  5. #include <math.h>  
  6.   
  7. #include "iostream"  
  8. using namespace std;  
  9.   
  10. #include "cubemanage.h"  
  11. #include "wcgcube.h"  
  12.   
  13. static GLfloat PI=3.1415f;  
  14. // Rotation amounts  
  15. static GLfloat xRot = 0.0f;  
  16. static GLfloat yRot = 0.0f;  
  17.   
  18. static GLfloat rotate=0.0f;  
  19. static int rotateType=0;  
  20. static int rotateOK=0;  
  21. static int rotateRate=100;  
  22. static GLfloat rotateStep=5*PI/180;  
  23.   
  24. CubeManage cm;  
  25.   
  26.   
  27. HPALETTE hPalette = NULL;  
  28.   
  29. // Keep track of windows changing width and height  
  30. GLfloat windowWidth;  
  31. GLfloat windowHeight;  
  32.   
  33.   
  34. static LPCTSTR lpszAppName = "WcgCube";  
  35.   
  36. void exitGame(HWND hWnd,HDC hDC,HGLRC hRC);  
  37. // Declaration for Window procedure  
  38. LRESULT CALLBACK WndProc(   HWND    hWnd,  
  39.                             UINT    message,  
  40.                             WPARAM  wParam,  
  41.                             LPARAM  lParam);  
  42.   
  43. // Set Pixel Format function - forward declaration  
  44. void SetDCPixelFormat(HDC hDC);  
  45.   
  46.   
  47. void ChangeSize(GLsizei w, GLsizei h)  
  48.     {  
  49.     GLfloat nRange = 350.0f;  
  50.   
  51.     // Prevent a divide by zero  
  52.     if(h == 0)  
  53.         h = 1;  
  54.   
  55.     // Set Viewport to window dimensions  
  56.     glViewport(0, 0, w, h);  
  57.   
  58.     // Reset coordinate system  
  59.     glMatrixMode(GL_PROJECTION);  
  60.     glLoadIdentity();  
  61.   
  62.     // Establish clipping volume (left, right, bottom, top, near, far)  
  63.     if (w <= h)   
  64.         glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);  
  65.     else   
  66.         glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);  
  67.   
  68.     glMatrixMode(GL_MODELVIEW);  
  69.     glLoadIdentity();  
  70.     }  
  71.   
  72.   
  73. // Called by timer routine to effect movement of the rectangle.  
  74. void IdleFunction(void)  
  75.     {  
  76.     if (rotate>=PI/2) {  
  77.         cm.turn(rotateType);  
  78.         rotateType=0;  
  79.         rotateOK=0;  
  80.         rotate=0.0f;  
  81.         // Refresh the Window  
  82. //      glutPostRedisplay();  
  83.         return;  
  84.     }  
  85.     rotate+=rotateStep;  
  86.       
  87.     // Refresh the Window  
  88. //  glutPostRedisplay();  
  89.     }  
  90.   
  91.   
  92.   
  93. // Called by AUX library to draw scene  
  94. void RenderScene(void)  
  95.     {  
  96.     // Clear the window with current clearing color  
  97.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  98.       
  99.   glPushMatrix();  
  100.   
  101.   glRotatef(xRot, 1.0f, 0.0f, 0.0f);  
  102.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);  
  103.   
  104.     cm.draw(rotateType,rotate);  
  105.   
  106.   glPopMatrix();  
  107.   
  108.     // Show the graphics  
  109. //  glutSwapBuffers();  
  110.     }  
  111.   
  112.   
  113.   
  114. // If necessary, creates a 3-3-2 palette for the device context listed.  
  115. HPALETTE GetOpenGLPalette(HDC hDC)  
  116.     {  
  117.     HPALETTE hRetPal = NULL;    // Handle to palette to be created  
  118.     PIXELFORMATDESCRIPTOR pfd;  // Pixel Format Descriptor  
  119.     LOGPALETTE *pPal;           // Pointer to memory for logical palette  
  120.     int nPixelFormat;           // Pixel format index  
  121.     int nColors;                // Number of entries in palette  
  122.     int i;                      // Counting variable  
  123.     BYTE RedRange,GreenRange,BlueRange;  
  124.                                 // Range for each color entry (7,7,and 3)  
  125.   
  126.   
  127.     // Get the pixel format index and retrieve the pixel format description  
  128.     nPixelFormat = GetPixelFormat(hDC);  
  129.     DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);  
  130.   
  131.     // Does this pixel format require a palette?  If not, do not create a  
  132.     // palette and just return NULL  
  133.     if(!(pfd.dwFlags & PFD_NEED_PALETTE))  
  134.         return NULL;  
  135.   
  136.     // Number of entries in palette.  8 bits yeilds 256 entries  
  137.     nColors = 1 << pfd.cColorBits;      
  138.   
  139.     // Allocate space for a logical palette structure plus all the palette entries  
  140.     pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY));  
  141.   
  142.     // Fill in palette header   
  143.     pPal->palVersion = 0x300;        // Windows 3.0  
  144.     pPal->palNumEntries = nColors; // table size  
  145.   
  146.     // Build mask of all 1's.  This creates a number represented by having  
  147.     // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and  
  148.     // pfd.cBlueBits.    
  149.     RedRange = (1 << pfd.cRedBits) -1;  
  150.     GreenRange = (1 << pfd.cGreenBits) - 1;  
  151.     BlueRange = (1 << pfd.cBlueBits) -1;  
  152.   
  153.     // Loop through all the palette entries  
  154.     for(i = 0; i < nColors; i++)  
  155.         {  
  156.         // Fill in the 8-bit equivalents for each component  
  157.         pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;  
  158.         pPal->palPalEntry[i].peRed = (unsigned char)(  
  159.             (double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);  
  160.   
  161.         pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;  
  162.         pPal->palPalEntry[i].peGreen = (unsigned char)(  
  163.             (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);  
  164.   
  165.         pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;  
  166.         pPal->palPalEntry[i].peBlue = (unsigned char)(  
  167.             (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);  
  168.   
  169.         pPal->palPalEntry[i].peFlags = (unsigned char) NULL;  
  170.         }  
  171.           
  172.   
  173.     // Create the palette  
  174.     hRetPal = CreatePalette(pPal);  
  175.   
  176.     // Go ahead and select and realize the palette for this device context  
  177.     SelectPalette(hDC,hRetPal,FALSE);  
  178.     RealizePalette(hDC);  
  179.   
  180.     // Free the memory used for the logical palette structure  
  181.     free(pPal);  
  182.   
  183.     // Return the handle to the new palette  
  184.     return hRetPal;  
  185.     }  
  186.   
  187.   
  188. // Select the pixel format for a given device context  
  189. void SetDCPixelFormat(HDC hDC)  
  190.     {  
  191.     int nPixelFormat;  
  192.   
  193.     static PIXELFORMATDESCRIPTOR pfd = {  
  194.         sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure  
  195.         1,                              // Version of this structure      
  196.         PFD_DRAW_TO_WINDOW |            // Draw to Window (not to bitmap)  
  197.         PFD_SUPPORT_OPENGL |            // Support OpenGL calls in window  
  198.         PFD_DOUBLEBUFFER,               // Double buffered mode  
  199.         PFD_TYPE_RGBA,                  // RGBA Color mode  
  200.         32,                             // Want 32 bit color   
  201.         0,0,0,0,0,0,                    // Not used to select mode  
  202.         0,0,                            // Not used to select mode  
  203.         0,0,0,0,0,                      // Not used to select mode  
  204.         16,                             // Size of depth buffer  
  205.         0,                              // Not used to select mode  
  206.         0,                              // Not used to select mode  
  207.         0,                              // Not used to select mode  
  208.         0,                              // Not used to select mode  
  209.         0,0,0 };                        // Not used to select mode  
  210.   
  211.     // Choose a pixel format that best matches that described in pfd  
  212.     nPixelFormat = ChoosePixelFormat(hDC, &pfd);  
  213.   
  214.     // Set the pixel format for the device context  
  215.     SetPixelFormat(hDC, nPixelFormat, &pfd);  
  216.     }  
  217.   
  218. void dealKey(HWND hWnd,HDC hDC,HGLRC hRC,int wParam)  
  219. {  
  220.     switch (wParam)  
  221.     {  
  222.     case 27:  
  223.         exitGame(hWnd,hDC,hRC);  
  224.         break;  
  225.     case 113:                       //q  
  226.         if (rotateOK==1)  
  227.             return;  
  228.         rotateType=1;  
  229.         rotateOK=1;  
  230.         rotate=0.0f;  
  231.         break;  
  232.     case 119:                       //w  
  233.         if (rotateOK==1)  
  234.             return;  
  235.         rotateType=2;  
  236.         rotateOK=1;  
  237.         rotate=0.0f;  
  238.         break;  
  239.     case 101:                       //e  
  240.         if (rotateOK==1)  
  241.             return;  
  242.         rotateType=3;  
  243.         rotateOK=1;  
  244.         rotate=0.0f;  
  245.         break;  
  246.     case 114:                       //r  
  247.         if (rotateOK==1)  
  248.             return;  
  249.         rotateType=4;  
  250.         rotateOK=1;  
  251.         rotate=0.0f;  
  252.         break;  
  253.     case 116:                       //t  
  254.         if (rotateOK==1)  
  255.             return;  
  256.         rotateType=5;  
  257.         rotateOK=1;  
  258.         rotate=0.0f;  
  259.         break;  
  260.     case 121:                       //y  
  261.         if (rotateOK==1)  
  262.             return;  
  263.         rotateType=6;  
  264.         rotateOK=1;  
  265.         rotate=0.0f;  
  266.         break;  
  267.     case 97:                        //a  
  268.         if (rotateOK==1)  
  269.             return;  
  270.         rotateType=7;  
  271.         rotateOK=1;  
  272.         rotate=0.0f;  
  273.         break;  
  274.     case 115:                       //s  
  275.         if (rotateOK==1)  
  276.             return;  
  277.         rotateType=8;  
  278.         rotateOK=1;  
  279.         rotate=0.0f;  
  280.         break;  
  281.     case 100:                       //d  
  282.         if (rotateOK==1)  
  283.             return;  
  284.         rotateType=9;  
  285.         rotateOK=1;  
  286.         rotate=0.0f;  
  287.         break;  
  288.     case 102:                       //f  
  289.         if (rotateOK==1)  
  290.             return;  
  291.         rotateType=10;  
  292.         rotateOK=1;  
  293.         rotate=0.0f;  
  294.         break;  
  295.     case 103:                       //g  
  296.         if (rotateOK==1)  
  297.             return;  
  298.         rotateType=11;  
  299.         rotateOK=1;  
  300.         rotate=0.0f;  
  301.         break;  
  302.     case 104:                       //h  
  303.         if (rotateOK==1)  
  304.             return;  
  305.         rotateType=12;  
  306.         rotateOK=1;  
  307.         rotate=0.0f;  
  308.         break;  
  309.     case VK_UP:   
  310.         xRot-= 5.0f;  
  311.         break;  
  312.     case VK_DOWN:  
  313.         xRot += 5.0f;  
  314.         break;  
  315.     case VK_LEFT:  
  316.         yRot -= 5.0f;  
  317.         break;  
  318.     case VK_RIGHT:  
  319.         yRot += 5.0f;  
  320.         break;  
  321.     }  
  322.     if(xRot > 356.0f)  
  323.         xRot = 0.0f;  
  324.   
  325.     if(xRot < -1.0f)  
  326.         xRot = 355.0f;  
  327.   
  328.     if(yRot > 356.0f)  
  329.         yRot = 0.0f;  
  330.   
  331.     if(yRot < -1.0f)  
  332.         yRot = 355.0f;  
  333. }  
  334.   
  335. void exitGame(HWND hWnd,HDC hDC,HGLRC hRC)  
  336. {  
  337.             // Kill the timer that we created  
  338.             KillTimer(hWnd,101);  
  339.   
  340.             // Deselect the current rendering context and delete it  
  341.             wglMakeCurrent(hDC,NULL);  
  342.             wglDeleteContext(hRC);  
  343.   
  344.             // Delete the palette  
  345.             if(hPalette != NULL)  
  346.                 DeleteObject(hPalette);  
  347.   
  348.             // Tell the application to terminate after the window  
  349.             // is gone.  
  350.             PostQuitMessage(0);  
  351. }  
  352.   
  353. // Entry point of all Windows programs  
  354. int APIENTRY WinMain(   HINSTANCE   hInstance,  
  355.                         HINSTANCE   hPrevInstance,  
  356.                         LPSTR       lpCmdLine,  
  357.                         int         nCmdShow)  
  358.     {  
  359.     MSG         msg;        // Windows message structure  
  360.     WNDCLASS    wc;         // Windows class structure  
  361.     HWND        hWnd;       // Storeage for window handle  
  362.     HWND        hDesktopWnd;// Storeage for desktop window handle  
  363.     HDC         hDesktopDC; // Storeage for desktop window device context  
  364.     int         nScreenX, nScreenY; // Screen Dimensions  
  365.   
  366.     // Register Window style  
  367.     wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;  
  368.     wc.lpfnWndProc      = (WNDPROC) WndProc;  
  369.     wc.cbClsExtra       = 0;  
  370.     wc.cbWndExtra       = 0;  
  371.     wc.hInstance        = hInstance;  
  372.     wc.hIcon            = NULL;  
  373.     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);  
  374.       
  375.     // No need for background brush for OpenGL window  
  376.     wc.hbrBackground    = NULL;       
  377.       
  378.     wc.lpszMenuName     = NULL;  
  379.     wc.lpszClassName    = lpszAppName;  
  380.   
  381.     // Register the window class  
  382.     if(RegisterClass(&wc) == 0)  
  383.         return FALSE;  
  384.   
  385.     // Get he Window handle and Device context to the desktop  
  386.     hDesktopWnd = GetDesktopWindow();  
  387.     hDesktopDC = GetDC(hDesktopWnd);  
  388.   
  389.     // Get the screen size  
  390.     nScreenX = GetDeviceCaps(hDesktopDC, HORZRES);  
  391.     nScreenY = GetDeviceCaps(hDesktopDC, VERTRES);  
  392.   
  393.     // Release the desktop device context  
  394.     ReleaseDC(hDesktopWnd, hDesktopDC);  
  395.   
  396.     // Create the main application window  
  397.     hWnd = CreateWindow(  
  398.                 lpszAppName,  
  399.                 lpszAppName,  
  400.                   
  401.                 // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS  
  402.                 WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,  
  403.       
  404.                 // Window position and size  
  405.                 0, 0,  
  406.                 nScreenX, nScreenY,  
  407.                 NULL,  
  408.                 NULL,  
  409.                 hInstance,  
  410.                 NULL);  
  411.   
  412.   
  413.     // If window was not created, quit  
  414.     if(hWnd == NULL)  
  415.         return FALSE;  
  416.   
  417.   
  418.     // Display the window  
  419.     ShowWindow(hWnd,SW_SHOW);  
  420.     UpdateWindow(hWnd);  
  421.   
  422.     // Process application messages until the application closes  
  423.     while( GetMessage(&msg, NULL, 0, 0))  
  424.         {  
  425.         TranslateMessage(&msg);  
  426.         DispatchMessage(&msg);  
  427.         }  
  428.   
  429.     return msg.wParam;  
  430.     }  
  431.   
  432.   
  433.   
  434. // Window procedure, handles all messages for this program  
  435. LRESULT CALLBACK WndProc(   HWND    hWnd,  
  436.                             UINT    message,  
  437.                             WPARAM  wParam,  
  438.                             LPARAM  lParam)  
  439.     {  
  440.     static HGLRC hRC;       // Permenant Rendering context  
  441.     static HDC hDC;         // Private GDI Device context  
  442.   
  443.     switch (message)  
  444.         {  
  445.         // Window creation, setup for OpenGL  
  446.         case WM_CREATE:  
  447.             // Store the device context  
  448.             hDC = GetDC(hWnd);        
  449.   
  450.             // Select the pixel format  
  451.             SetDCPixelFormat(hDC);        
  452.   
  453.             // Create the rendering context and make it current  
  454.             hRC = wglCreateContext(hDC);  
  455.             wglMakeCurrent(hDC, hRC);  
  456.   
  457.             // Create the palette  
  458.             hPalette = GetOpenGLPalette(hDC);  
  459.     // Black background  
  460.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f );  
  461.   
  462.     glEnable(GL_DEPTH_TEST);      
  463. //  glEnable(GL_DITHER);  
  464.     glShadeModel(GL_SMOOTH);  
  465.   
  466.             // Create a timer that fires 30 times a second  
  467.             SetTimer(hWnd,33,1,NULL);  
  468.             break;  
  469.   
  470.         // Window is being destroyed, cleanup  
  471.         case WM_DESTROY:  
  472.             exitGame(hWnd,hDC,hRC);  
  473.             break;  
  474.           
  475.         case WM_KEYDOWN:  
  476.             dealKey(hWnd,hDC,hRC,wParam);  
  477.             InvalidateRect(hWnd,NULL,FALSE);  
  478.             break;  
  479.   
  480.         case WM_CHAR:  
  481.             dealKey(hWnd,hDC,hRC,wParam);  
  482.             InvalidateRect(hWnd,NULL,FALSE);  
  483.             break;  
  484.           
  485.         // Window is resized.  
  486.         case WM_SIZE:  
  487.             // Call our function which modifies the clipping  
  488.             // volume and viewport  
  489.             ChangeSize(LOWORD(lParam), HIWORD(lParam));  
  490.             break;  
  491.   
  492.         // Timer, moves and bounces the rectangle, simply calls  
  493.         // our previous OnIdle function, then invalidates the   
  494.         // window so it will be redrawn.  
  495.         case WM_TIMER:  
  496.             {  
  497.             IdleFunction();  
  498.           
  499.             InvalidateRect(hWnd,NULL,FALSE);  
  500.             }  
  501.             break;  
  502.   
  503.         // The painting function.  This message sent by Windows   
  504.         // whenever the screen needs updating.  
  505.         case WM_PAINT:  
  506.             {  
  507.             // Call OpenGL drawing code  
  508.             RenderScene();  
  509.   
  510.             // Call function to swap the buffers  
  511.             SwapBuffers(hDC);  
  512.   
  513.             // Validate the newly painted client area  
  514.             ValidateRect(hWnd,NULL);  
  515.             }  
  516.             break;  
  517.   
  518.   
  519.         // Windows is telling the application that it may modify  
  520.         // the system palette.  This message in essance asks the   
  521.         // application for a new palette.  
  522.         case WM_QUERYNEWPALETTE:  
  523.             // If the palette was created.  
  524.             if(hPalette)  
  525.                 {  
  526.                 int nRet;  
  527.   
  528.                 // Selects the palette into the current device context  
  529.                 SelectPalette(hDC, hPalette, FALSE);  
  530.   
  531.                 // Map entries from the currently selected palette to  
  532.                 // the system palette.  The return value is the number   
  533.                 // of palette entries modified.  
  534.                 nRet = RealizePalette(hDC);  
  535.   
  536.                 // Repaint, forces remap of palette in current window  
  537.                 InvalidateRect(hWnd,NULL,FALSE);  
  538.   
  539.                 return nRet;  
  540.                 }  
  541.             break;  
  542.   
  543.       
  544.         // This window may set the palette, even though it is not the   
  545.         // currently active window.  
  546.         case WM_PALETTECHANGED:  
  547.             // Don't do anything if the palette does not exist, or if  
  548.             // this is the window that changed the palette.  
  549.             if((hPalette != NULL) && ((HWND)wParam != hWnd))  
  550.                 {  
  551.                 // Select the palette into the device context  
  552.                 SelectPalette(hDC,hPalette,FALSE);  
  553.   
  554.                 // Map entries to system palette  
  555.                 RealizePalette(hDC);  
  556.                   
  557.                 // Remap the current colors to the newly realized palette  
  558.                 UpdateColors(hDC);  
  559.                 return 0;  
  560.                 }  
  561.             break;  
  562.   
  563.   
  564.         default:   // Passes it on if unproccessed  
  565.             return (DefWindowProc(hWnd, message, wParam, lParam));  
  566.   
  567.         }  
  568.   
  569.     return (0L);  
  570.     }  

cubemanage.cpp文件为:

[cpp]  view plain copy
  1. #include "iostream"  
  2. using namespace std;  
  3.   
  4. #include "cubemanage.h"  
  5.   
  6. CubeManage::CubeManage(){  
  7.     for (int i=0;i<CUBE_SIZE;i++) {  
  8.         for (int j=0;j<CUBE_SIZE;j++) {  
  9.             for (int k=0;k<CUBE_SIZE;k++) {  
  10.                 cubes[i][j][k]=new WcgCube();  
  11.             }  
  12.         }  
  13.     }  
  14. }  
  15.   
  16. CubeManage::~CubeManage(){  
  17.     for (int i=0;i<CUBE_SIZE;i++) {  
  18.         for (int j=0;j<CUBE_SIZE;j++) {  
  19.             for (int k=0;k<CUBE_SIZE;k++) {  
  20.                 delete cubes[i][j][k];  
  21.             }  
  22.         }  
  23.     }  
  24. }  
  25.   
  26. void CubeManage::turn(int rotateType) {   
  27.     if (rotateType==1) {  
  28.         turnByZShun(2);  
  29.     }  
  30.     else  
  31.     if (rotateType==2) {  
  32.         turnByXShun(2);  
  33.     }  
  34.     else  
  35.     if (rotateType==3) {  
  36.         turnByZNi(0);  
  37.     }  
  38.     else  
  39.     if (rotateType==4) {  
  40.         turnByXNi(0);  
  41.     }  
  42.     else  
  43.     if (rotateType==5) {  
  44.         turnByYShun(2);  
  45.     }  
  46.     else  
  47.     if (rotateType==6) {  
  48.         turnByYNi(0);  
  49.     }  
  50.     else  
  51.     if (rotateType==7) {  
  52.         turnByZNi(2);  
  53.     }  
  54.     else  
  55.     if (rotateType==8) {  
  56.         turnByXNi(2);  
  57.     }  
  58.     else  
  59.     if (rotateType==9) {  
  60.         turnByZShun(0);  
  61.     }  
  62.     else  
  63.     if (rotateType==10) {  
  64.         turnByXShun(0);  
  65.     }  
  66.     else  
  67.     if (rotateType==11) {  
  68.         turnByYNi(2);  
  69.     }  
  70.     else  
  71.     if (rotateType==12) {  
  72.         turnByYShun(0);  
  73.     }  
  74. }  
  75.   
  76. void CubeManage::draw(int rotateType,GLfloat rotate) {  
  77.     GLfloat PI=3.1415f;  
  78.     GLfloat cubeRadium=10.0f;  
  79.     GLfloat cubeSpace=2.0f;  
  80.     GLfloat x,y,z;  
  81.     int i,j,k;  
  82.   
  83.     x=ORIENTX-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);  
  84.     y=ORIENTZ-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);  
  85.     z=ORIENTZ-(CUBE_SIZE/2)*(cubeRadium*2+cubeSpace);  
  86.       
  87.     if (rotateType==0) {  
  88.         for (i=0;i<CUBE_SIZE;i++) {  
  89.             for (j=0;j<CUBE_SIZE;j++) {  
  90.                 for (k=0;k<CUBE_SIZE;k++) {  
  91.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  92.                 }  
  93.             }  
  94.         }  
  95.     }  
  96.     else  
  97.     if (rotateType==1) {  
  98.     glPushMatrix();  
  99.     glRotatef(360-180*rotate/PI,0.0f,0.0f,1.0f);  
  100.         for (i=0;i<CUBE_SIZE;i++) {  
  101.             for (j=0;j<CUBE_SIZE;j++) {  
  102.                 (cubes[i][j][CUBE_SIZE-1])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1));  
  103.             }  
  104.         }  
  105.     glPopMatrix();  
  106.         for (i=0;i<CUBE_SIZE;i++) {  
  107.             for (j=0;j<CUBE_SIZE;j++) {  
  108.                 for (k=0;k<CUBE_SIZE-1;k++) {  
  109.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  110.                 }  
  111.             }  
  112.         }  
  113.     }  
  114.     else  
  115.     if (rotateType==2) {  
  116.     glPushMatrix();  
  117.     glRotatef(360-180*rotate/PI,1.0f,0.0f,0.0f);  
  118.         for (i=0;i<CUBE_SIZE;i++) {  
  119.             for (j=0;j<CUBE_SIZE;j++) {  
  120.                 (cubes[CUBE_SIZE-1][i][j])->draw(x+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);  
  121.             }  
  122.         }  
  123.     glPopMatrix();  
  124.         for (i=0;i<CUBE_SIZE-1;i++) {  
  125.             for (j=0;j<CUBE_SIZE;j++) {  
  126.                 for (k=0;k<CUBE_SIZE;k++) {  
  127.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  128.                 }  
  129.             }  
  130.         }  
  131.     }  
  132.     else  
  133.     if (rotateType==3) {  
  134.     glPushMatrix();  
  135.     glRotatef(360-180*rotate/PI,0.0f,0.0f,-1.0f);  
  136.         for (i=0;i<CUBE_SIZE;i++) {  
  137.             for (j=0;j<CUBE_SIZE;j++) {  
  138.                 (cubes[i][j][0])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z);  
  139.             }  
  140.         }  
  141.     glPopMatrix();  
  142.         for (i=0;i<CUBE_SIZE;i++) {  
  143.             for (j=0;j<CUBE_SIZE;j++) {  
  144.                 for (k=1;k<CUBE_SIZE;k++) {  
  145.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  146.                 }  
  147.             }  
  148.         }  
  149.     }  
  150.     else  
  151.     if (rotateType==4) {  
  152.     glPushMatrix();  
  153.     glRotatef(360-180*rotate/PI,-1.0f,0.0f,0.0f);  
  154.         for (i=0;i<CUBE_SIZE;i++) {  
  155.             for (j=0;j<CUBE_SIZE;j++) {  
  156.                 (cubes[0][i][j])->draw(x,y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);  
  157.             }  
  158.         }  
  159.     glPopMatrix();  
  160.         for (i=1;i<CUBE_SIZE;i++) {  
  161.             for (j=0;j<CUBE_SIZE;j++) {  
  162.                 for (k=0;k<CUBE_SIZE;k++) {  
  163.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  164.                 }  
  165.             }  
  166.         }  
  167.     }  
  168.     else  
  169.     if (rotateType==5) {  
  170.     glPushMatrix();  
  171.     glRotatef(360-180*rotate/PI,0.0f,1.0f,0.0f);  
  172.         for (i=0;i<CUBE_SIZE;i++) {  
  173.             for (j=0;j<CUBE_SIZE;j++) {  
  174.                 (cubes[i][CUBE_SIZE-1][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),z+(cubeRadium*2+cubeSpace)*j);  
  175.             }  
  176.         }  
  177.     glPopMatrix();  
  178.         for (i=0;i<CUBE_SIZE;i++) {  
  179.             for (j=0;j<CUBE_SIZE-1;j++) {  
  180.                 for (k=0;k<CUBE_SIZE;k++) {  
  181.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  182.                 }  
  183.             }  
  184.         }  
  185.     }  
  186.     else  
  187.     if (rotateType==6) {  
  188.     glPushMatrix();  
  189.     glRotatef(360-180*rotate/PI,0.0f,-1.0f,0.0f);  
  190.         for (i=0;i<CUBE_SIZE;i++) {  
  191.             for (j=0;j<CUBE_SIZE;j++) {  
  192.                 (cubes[i][0][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y,z+(cubeRadium*2+cubeSpace)*j);  
  193.             }  
  194.         }  
  195.     glPopMatrix();  
  196.         for (i=0;i<CUBE_SIZE;i++) {  
  197.             for (j=1;j<CUBE_SIZE;j++) {  
  198.                 for (k=0;k<CUBE_SIZE;k++) {  
  199.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  200.                 }  
  201.             }  
  202.         }  
  203.     }  
  204.     else  
  205.     if (rotateType==7) {  
  206.     glPushMatrix();  
  207.     glRotatef(180*rotate/PI,0.0f,0.0f,1.0f);  
  208.         for (i=0;i<CUBE_SIZE;i++) {  
  209.             for (j=0;j<CUBE_SIZE;j++) {  
  210.                 (cubes[i][j][CUBE_SIZE-1])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1));  
  211.             }  
  212.         }  
  213.     glPopMatrix();  
  214.         for (i=0;i<CUBE_SIZE;i++) {  
  215.             for (j=0;j<CUBE_SIZE;j++) {  
  216.                 for (k=0;k<CUBE_SIZE-1;k++) {  
  217.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  218.                 }  
  219.             }  
  220.         }  
  221.     }  
  222.     else  
  223.     if (rotateType==8) {  
  224.     glPushMatrix();  
  225.     glRotatef(180*rotate/PI,1.0f,0.0f,0.0f);  
  226.         for (i=0;i<CUBE_SIZE;i++) {  
  227.             for (j=0;j<CUBE_SIZE;j++) {  
  228.                 (cubes[CUBE_SIZE-1][i][j])->draw(x+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);  
  229.             }  
  230.         }  
  231.     glPopMatrix();  
  232.         for (i=0;i<CUBE_SIZE-1;i++) {  
  233.             for (j=0;j<CUBE_SIZE;j++) {  
  234.                 for (k=0;k<CUBE_SIZE;k++) {  
  235.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  236.                 }  
  237.             }  
  238.         }  
  239.     }  
  240.     else  
  241.     if (rotateType==9) {  
  242.     glPushMatrix();  
  243.     glRotatef(180*rotate/PI,0.0f,0.0f,-1.0f);  
  244.         for (i=0;i<CUBE_SIZE;i++) {  
  245.             for (j=0;j<CUBE_SIZE;j++) {  
  246.                 (cubes[i][j][0])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z);  
  247.             }  
  248.         }  
  249.     glPopMatrix();  
  250.         for (i=0;i<CUBE_SIZE;i++) {  
  251.             for (j=0;j<CUBE_SIZE;j++) {  
  252.                 for (k=1;k<CUBE_SIZE;k++) {  
  253.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  254.                 }  
  255.             }  
  256.         }  
  257.     }  
  258.     else  
  259.     if (rotateType==10) {  
  260.     glPushMatrix();  
  261.     glRotatef(180*rotate/PI,-1.0f,0.0f,0.0f);  
  262.         for (i=0;i<CUBE_SIZE;i++) {  
  263.             for (j=0;j<CUBE_SIZE;j++) {  
  264.                 (cubes[0][i][j])->draw(x,y+(cubeRadium*2+cubeSpace)*i,z+(cubeRadium*2+cubeSpace)*j);  
  265.             }  
  266.         }  
  267.     glPopMatrix();  
  268.         for (i=1;i<CUBE_SIZE;i++) {  
  269.             for (j=0;j<CUBE_SIZE;j++) {  
  270.                 for (k=0;k<CUBE_SIZE;k++) {  
  271.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  272.                 }  
  273.             }  
  274.         }  
  275.     }  
  276.     else  
  277.     if (rotateType==11) {  
  278.     glPushMatrix();  
  279.     glRotatef(180*rotate/PI,0.0f,1.0f,0.0f);  
  280.         for (i=0;i<CUBE_SIZE;i++) {  
  281.             for (j=0;j<CUBE_SIZE;j++) {  
  282.                 (cubes[i][CUBE_SIZE-1][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*(CUBE_SIZE-1),z+(cubeRadium*2+cubeSpace)*j);  
  283.             }  
  284.         }  
  285.     glPopMatrix();  
  286.         for (i=0;i<CUBE_SIZE;i++) {  
  287.             for (j=0;j<CUBE_SIZE-1;j++) {  
  288.                 for (k=0;k<CUBE_SIZE;k++) {  
  289.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  290.                 }  
  291.             }  
  292.         }  
  293.     }  
  294.     else  
  295.     if (rotateType==12) {  
  296.     glPushMatrix();  
  297.     glRotatef(180*rotate/PI,0.0f,-1.0f,0.0f);  
  298.         for (i=0;i<CUBE_SIZE;i++) {  
  299.             for (j=0;j<CUBE_SIZE;j++) {  
  300.                 (cubes[i][0][j])->draw(x+(cubeRadium*2+cubeSpace)*i,y,z+(cubeRadium*2+cubeSpace)*j);  
  301.             }  
  302.         }  
  303.     glPopMatrix();  
  304.         for (i=0;i<CUBE_SIZE;i++) {  
  305.             for (j=1;j<CUBE_SIZE;j++) {  
  306.                 for (k=0;k<CUBE_SIZE;k++) {  
  307.                     (cubes[i][j][k])->draw(x+(cubeRadium*2+cubeSpace)*i,y+(cubeRadium*2+cubeSpace)*j,z+(cubeRadium*2+cubeSpace)*k);  
  308.                 }  
  309.             }  
  310.         }  
  311.     }  
  312. }  
  313.   
  314. void CubeManage::output() {  
  315.     for (int i=0;i<CUBE_SIZE;i++) {  
  316.         for (int j=0;j<CUBE_SIZE;j++) {  
  317.             cubes[0][i][j]->output();  
  318.         }  
  319.     }  
  320. }  
  321.   
  322. void CubeManage::output(int scr,int site){  
  323.     int sign;  
  324.     int i,j;  
  325.   
  326.     if (site==1) {  
  327.         cout << "site=1,nonsense!" << endl;  
  328.         return;  
  329.     }  
  330.       
  331.     switch (scr) {  
  332.         case 1:  
  333.             if (site==0)  
  334.                 sign=-X;  
  335.             else  
  336.                 sign=X;  
  337.             cout << "scr=" << scr << " sign=" << sign << endl;  
  338.             for (i=0;i<CUBE_SIZE;i++) {  
  339.                 for (j=0;j<CUBE_SIZE;j++) {  
  340.                     cout << i << "," << j << "=";  
  341.                     cubes[site][i][j]->output(sign);  
  342.                     cout << endl;  
  343.                 }  
  344.             }  
  345.             break;  
  346.         case 2:  
  347.             if (site==0)  
  348.                 sign=-Y;  
  349.             else  
  350.                 sign=Y;  
  351.             for (i=0;i<CUBE_SIZE;i++) {  
  352.                 for (j=0;j<CUBE_SIZE;j++) {  
  353.                     cout << i << "," << j << "=";  
  354.                     cubes[i][site][j]->output(sign);  
  355.                     cout << endl;  
  356.                 }  
  357.             }  
  358.             break;  
  359.         case 3:  
  360.             if (site==0)  
  361.                 sign=-Z;  
  362.             else  
  363.                 sign=Z;  
  364.             for (i=0;i<CUBE_SIZE;i++) {  
  365.                 for (j=0;j<CUBE_SIZE;j++) {  
  366.                     cout << i << "," << j << "=";  
  367.                     cubes[i][j][site]->output(sign);  
  368.                     cout << endl;  
  369.                 }  
  370.             }  
  371.             break;  
  372.     }  
  373. }  
  374.   
  375. void CubeManage::goStep(int *leftLeg,int *rightLeg,int *goDirection,int step,int leftEdge,int rightEdge) {  
  376.     for (int i=0;i<step;i++) {  
  377.         switch (*goDirection) {  
  378.             case 0:  
  379.                 *leftLeg=*leftLeg-1;  
  380.                 if (*leftLeg<leftEdge) {  
  381.                     *leftLeg=*leftLeg+1;  
  382.                     *goDirection=3;  
  383.                     *rightLeg=*rightLeg+1;  
  384.                 }  
  385.                 break;  
  386.             case 1:  
  387.                 *rightLeg=*rightLeg-1;  
  388.                 if (*rightLeg<leftEdge) {  
  389.                     *rightLeg=*rightLeg+1;  
  390.                     *goDirection=0;  
  391.                     *leftLeg=*leftLeg-1;  
  392.                 }  
  393.                 break;  
  394.             case 2:  
  395.                 *leftLeg=*leftLeg+1;  
  396.                 if (*leftLeg>=rightEdge) {  
  397.                     *leftLeg=*leftLeg-1;  
  398.                     *goDirection=1;  
  399.                     *rightLeg=*rightLeg-1;  
  400.                 }  
  401.                 break;  
  402.             case 3:  
  403.                 *rightLeg=*rightLeg+1;  
  404.                 if (*rightLeg>=rightEdge) {  
  405.                     *rightLeg=*rightLeg-1;  
  406.                     *goDirection=2;  
  407.                     *leftLeg=*leftLeg+1;  
  408.                 }  
  409.                 break;  
  410.         }  
  411.     }  
  412. }  
  413.   
  414. void CubeManage::turnByXShun(int x) {  
  415.     int step=CUBE_SIZE-1;  
  416.     int leftEdge=0;  
  417.     int rightEdge=CUBE_SIZE;  
  418.     int goDirection0=3;  
  419.     int goDirection1=3;  
  420.     int y0=0;  
  421.     int z0=0;  
  422.     int y1=0;  
  423.     int z1=0;  
  424.     WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];  
  425.       
  426.     tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[x][CUBE_SIZE/2][CUBE_SIZE/2];  
  427.     cubes[x][CUBE_SIZE/2][CUBE_SIZE/2]->turnByXShun(x);  
  428.     for (int i=0;i<CUBE_SIZE/2;i++) {  
  429.         step=CUBE_SIZE-i*2-1;  
  430.         goDirection0=3;  
  431.         goDirection1=3;  
  432.         leftEdge=i;  
  433.         rightEdge=CUBE_SIZE-i;  
  434.         y0=leftEdge;  
  435.         z0=leftEdge;  
  436.         y1=leftEdge;  
  437.         z1=leftEdge;  
  438.         goStep(&y1,&z1,&goDirection1,step,leftEdge,rightEdge);  
  439.         for (int j=0;j<step*4;j++) {  
  440.             tempcubes[y1][z1]=cubes[x][y0][z0];  
  441.             cubes[x][y0][z0]->turnByXShun(x);  
  442.             goStep(&y0,&z0,&goDirection0,1,leftEdge,rightEdge);  
  443.             goStep(&y1,&z1,&goDirection1,1,leftEdge,rightEdge);  
  444.         }  
  445.         for (int m=0;m<CUBE_SIZE;m++) {  
  446.             for (int n=0;n<CUBE_SIZE;n++) {  
  447.                 cubes[x][m][n]=tempcubes[m][n];  
  448.             }  
  449.         }  
  450.     }  
  451. }  
  452.   
  453. void CubeManage::turnByXNi(int x) {  
  454.     turnByXShun(x);  
  455.     turnByXShun(x);  
  456.     turnByXShun(x);  
  457. }  
  458.   
  459. void CubeManage::turnByYShun(int y) {  
  460.     int step=CUBE_SIZE-1;  
  461.     int leftEdge=0;  
  462.     int rightEdge=CUBE_SIZE;  
  463.     int goDirection0=3;  
  464.     int goDirection1=3;  
  465.     int x0=0;  
  466.     int z0=0;  
  467.     int x1=0;  
  468.     int z1=0;  
  469.     WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];  
  470.       
  471.     tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[CUBE_SIZE/2][y][CUBE_SIZE/2];  
  472.     cubes[CUBE_SIZE/2][y][CUBE_SIZE/2]->turnByYShun(y);  
  473.     for (int i=0;i<CUBE_SIZE/2;i++) {  
  474.         step=CUBE_SIZE-i*2-1;  
  475.         goDirection0=3;  
  476.         goDirection1=3;  
  477.         leftEdge=i;  
  478.         rightEdge=CUBE_SIZE-i;  
  479.         x0=leftEdge;  
  480.         z0=leftEdge;  
  481.         x1=leftEdge;  
  482.         z1=leftEdge;  
  483.         goStep(&z1,&x1,&goDirection1,step,leftEdge,rightEdge);  
  484.         for (int j=0;j<step*4;j++) {  
  485.             tempcubes[x1][z1]=cubes[x0][y][z0];  
  486.             cubes[x0][y][z0]->turnByYShun(y);  
  487.             goStep(&z0,&x0,&goDirection0,1,leftEdge,rightEdge);  
  488.             goStep(&z1,&x1,&goDirection1,1,leftEdge,rightEdge);  
  489.         }  
  490.         for (int m=0;m<CUBE_SIZE;m++) {  
  491.             for (int n=0;n<CUBE_SIZE;n++) {  
  492.                 cubes[m][y][n]=tempcubes[m][n];  
  493.             }  
  494.         }  
  495.     }  
  496. }  
  497.   
  498. void CubeManage::turnByYNi(int y) {  
  499.     turnByYShun(y);  
  500.     turnByYShun(y);  
  501.     turnByYShun(y);  
  502. }  
  503.   
  504. void CubeManage::turnByZShun(int z) {  
  505.     int step=CUBE_SIZE-1;  
  506.     int leftEdge=0;  
  507.     int rightEdge=CUBE_SIZE;  
  508.     int goDirection0=3;  
  509.     int goDirection1=3;  
  510.     int x0=0;  
  511.     int y0=0;  
  512.     int x1=0;  
  513.     int y1=0;  
  514.     WcgCube *tempcubes[CUBE_SIZE][CUBE_SIZE];  
  515.       
  516.     tempcubes[CUBE_SIZE/2][CUBE_SIZE/2]=cubes[CUBE_SIZE/2][CUBE_SIZE/2][z];  
  517.     cubes[CUBE_SIZE/2][CUBE_SIZE/2][z]->turnByZShun(z);  
  518.     for (int i=0;i<CUBE_SIZE/2;i++) {  
  519.         step=CUBE_SIZE-i*2-1;  
  520.         goDirection0=3;  
  521.         goDirection1=3;  
  522.         leftEdge=i;  
  523.         rightEdge=CUBE_SIZE-i;  
  524.         x0=leftEdge;  
  525.         y0=leftEdge;  
  526.         x1=leftEdge;  
  527.         y1=leftEdge;  
  528.         goStep(&x1,&y1,&goDirection1,step,leftEdge,rightEdge);  
  529.         for (int j=0;j<step*4;j++) {  
  530.             tempcubes[x1][y1]=cubes[x0][y0][z];  
  531.             cubes[x0][y0][z]->turnByZShun(z);  
  532.             goStep(&x0,&y0,&goDirection0,1,leftEdge,rightEdge);  
  533.             goStep(&x1,&y1,&goDirection1,1,leftEdge,rightEdge);  
  534.         }  
  535.         for (int m=0;m<CUBE_SIZE;m++) {  
  536.             for (int n=0;n<CUBE_SIZE;n++) {  
  537.                 cubes[m][n][z]=tempcubes[m][n];  
  538.             }  
  539.         }  
  540.     }  
  541. }  
  542.   
  543. void CubeManage::turnByZNi(int z) {  
  544.     turnByZShun(z);  
  545.     turnByZShun(z);  
  546.     turnByZShun(z);  
  547. }  

wcgcube.cpp文件为:

[cpp]  view plain copy
  1. #include "iostream"  
  2. using namespace std;  
  3.   
  4. #include "wcgcube.h"  
  5.   
  6. WcgCube::WcgCube(){  
  7.     direct[0]=Z;  
  8.     direct[1]=X;  
  9.     direct[2]=-Z;  
  10.     direct[3]=-X;  
  11.     direct[4]=Y;  
  12.     direct[5]=-Y;  
  13.       
  14.     sideColor[0][0]=1.0f;  
  15.     sideColor[0][1]=1.0f;  
  16.     sideColor[0][2]=1.0f;  
  17.       
  18.     sideColor[1][0]=1.0f;  
  19.     sideColor[1][1]=1.0f;  
  20.     sideColor[1][2]=0.0f;  
  21.       
  22.     sideColor[2][0]=1.0f;  
  23.     sideColor[2][1]=0.0f;  
  24.     sideColor[2][2]=0.0f;  
  25.       
  26.     sideColor[3][0]=1.0f;  
  27.     sideColor[3][1]=0.0f;  
  28.     sideColor[3][2]=1.0f;  
  29.       
  30.     sideColor[4][0]=0.0f;  
  31.     sideColor[4][1]=1.0f;  
  32.     sideColor[4][2]=1.0f;  
  33.       
  34.     sideColor[5][0]=0.0f;  
  35.     sideColor[5][1]=1.0f;  
  36.     sideColor[5][2]=0.0f;  
  37. }  
  38.   
  39. WcgCube::~WcgCube(){  
  40. }  
  41.   
  42. void WcgCube::draw(GLfloat orientX,GLfloat orientY,GLfloat orientZ) {  
  43.     GLfloat cubeRadium=10.0f;  
  44.     GLfloat cubeSpace=2.0f;  
  45.     for (int i=0;i<6;i++) {  
  46.         glColor3f(sideColor[i][0],sideColor[i][1],sideColor[i][2]);  
  47.         if (direct[i]==Z) {  
  48.             // Front face  
  49.             glBegin(GL_POLYGON);  
  50.                 glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);  
  51.   
  52.                 glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);  
  53.   
  54.                 glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);  
  55.   
  56.                 glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);  
  57.             glEnd();  
  58.         }  
  59.         else  
  60.         if (direct[i]==-Z) {  
  61.             // Back Face  
  62.             glBegin(GL_POLYGON);  
  63.                 glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);  
  64.   
  65.                 glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);  
  66.   
  67.                 glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);  
  68.   
  69.                 glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);  
  70.             glEnd();  
  71.         }  
  72.         else  
  73.         if (direct[i]==Y) {  
  74.             // Top Face  
  75.             glBegin(GL_POLYGON);  
  76.                 glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);  
  77.   
  78.                 glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);  
  79.   
  80.                 glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);  
  81.   
  82.                 glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);  
  83.             glEnd();  
  84.         }  
  85.         else  
  86.         if (direct[i]==-Y) {  
  87.             // Bottom Face  
  88.             glBegin(GL_POLYGON);  
  89.                 glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);  
  90.   
  91.                 glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);  
  92.   
  93.                 glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);  
  94.   
  95.                 glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);  
  96.             glEnd();  
  97.         }  
  98.         else  
  99.         if (direct[i]==X) {  
  100.             // Left face  
  101.             glBegin(GL_POLYGON);  
  102.                 glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);  
  103.   
  104.                 glVertex3f(orientX+cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);  
  105.   
  106.                 glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);  
  107.   
  108.                 glVertex3f(orientX+cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);  
  109.             glEnd();  
  110.         }  
  111.         else  
  112.         if (direct[i]==-X) {  
  113.             // Right face  
  114.             glBegin(GL_POLYGON);  
  115.                 glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ+cubeRadium);  
  116.   
  117.                 glVertex3f(orientX-cubeRadium,orientY+cubeRadium,orientZ-cubeRadium);  
  118.   
  119.                 glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ-cubeRadium);  
  120.   
  121.                 glVertex3f(orientX-cubeRadium,orientY-cubeRadium,orientZ+cubeRadium);  
  122.             glEnd();  
  123.         }  
  124.     }  
  125. }  
  126.   
  127. void WcgCube::output() {  
  128.     for (int i=0;i<6;i++) {  
  129.         cout << "direct[" << i << "]=" << direct[i] << endl;  
  130.     }  
  131. }  
  132.   
  133. void WcgCube::output(int sign) {  
  134.     for (int i=0;i<6;i++) {  
  135.         if (direct[i]==sign)  
  136.             cout <<  i;  
  137.     }  
  138. }  
  139.   
  140. void WcgCube::turnByXShun(int x) {  
  141.     turnByX(x,-1);  
  142. }  
  143.   
  144. void WcgCube::turnByXNi(int x) {  
  145.     turnByX(x,1);  
  146. }  
  147.   
  148. void WcgCube::turnByX(int x,int sign) {  
  149.     for (int i=0;i<6;i++) {  
  150.         switch (direct[i]) {  
  151.             case Z:  
  152.                 direct[i]=(-1)*sign*Y;  
  153.                 break;  
  154.             case -Z:  
  155.                 direct[i]=sign*Y;  
  156.                 break;  
  157.             case Y:  
  158.                 direct[i]=sign*Z;  
  159.                 break;  
  160.             case -Y:  
  161.                 direct[i]=(-1)*sign*Z;  
  162.                 break;  
  163.         }  
  164.     }  
  165. }  
  166.   
  167. void WcgCube::turnByYShun(int y) {  
  168.     turnByY(y,-1);  
  169. }  
  170.   
  171. void WcgCube::turnByYNi(int y) {  
  172.     turnByY(y,1);  
  173. }  
  174.   
  175. void WcgCube::turnByY(int y,int sign) {  
  176.     for (int i=0;i<6;i++) {  
  177.         switch (direct[i]) {  
  178.             case Z:  
  179.                 direct[i]=sign*X;  
  180.                 break;  
  181.             case -Z:  
  182.                 direct[i]=(-1)*sign*X;  
  183.                 break;  
  184.             case X:  
  185.                 direct[i]=(-1)*sign*Z;  
  186.                 break;  
  187.             case -X:  
  188.                 direct[i]=sign*Z;  
  189.                 break;  
  190.         }  
  191.     }  
  192. }  
  193.   
  194. void WcgCube::turnByZShun(int z) {  
  195.     turnByZ(z,-1);  
  196. }  
  197.   
  198. void WcgCube::turnByZNi(int z) {  
  199.     turnByZ(z,1);  
  200. }  
  201.   
  202. void WcgCube::turnByZ(int z,int sign) {  
  203.     for (int i=0;i<6;i++) {  
  204.         switch (direct[i]) {  
  205.             case Y:  
  206.                 direct[i]=(-1)*sign*X;  
  207.                 break;  
  208.             case -Y:  
  209.                 direct[i]=sign*X;  
  210.                 break;  
  211.             case X:  
  212.                 direct[i]=sign*Y;  
  213.                 break;  
  214.             case -X:  
  215.                 direct[i]=(-1)*sign*Y;  
  216.                 break;  
  217.         }  
  218.     }  
  219. }  

        通过键盘上的按键q、w、e、r、t、a、s、d、f、g、h来旋转改变魔方的各种组合。

        最终效果图如下所示:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
作者对游戏的说明: 首先,您应当以一种批判的眼光来看待本程序。这个游戏是我制作 的第一部RPG游戏,无任何经验可谈,完全按照自己对游戏的理解进 行设计的。当我参照了《圣剑英雄2》的源码之后,才体会到专业游 戏引擎的博大精深。 该程序的内核大约有2000余行,能够处理人物的行走、对话、战斗, 等等。由于该程序的结构并不适于这种规模的程序,故不推荐您详 细研究该程序。所附地图编辑器的源程序我已经添加了详细的注释, 其程序结构也比较合理,可以作为初学VC的例子。 该程序在VC的程序向导所生成的SDI框架的基础上修改而成。它没有 使用任何关于VC底层的东西。程序的绝大部分都是在CgameView类中 制作的,只有修改窗口特征的一段代码在CMainFrm类中。其他的类 统统没有用到。另外添加的一个类是CEnemy类。 整个游戏的故事情节分成8段,分别由Para1.h ~ Para8.h八个文件 实现。由于程序仅仅能够被动的处理各种各样的消息,所以情节的 实现也只能根据系统的一些参数来判断当前应当做什么。在程序中 使用了冗长的if……else if……结构来实现这种判断。 当然,在我的记录本上,详细的记录了每个事件的判断条件。这种 笨拙的设计当然是不可取的。成都金点所作《圣剑英雄II》采用了 剧本解读的方式,这才是正统的做法。但这也需要更多的编程经验 和熟练的code功夫。 下面列举的是程序编制过程中总结出来的经验和教训。 第一,对话方式应该采用《圣剑英雄II》的剧本方式。 现在的方式把一个段落中所有的对话都混在一个文件中,然后给每 句话一个号码相对应。这样做虽然降低了引擎的难度,却导致剧情的 编写极其繁琐。 第二,运动和显示应当完全分开。 现在的程序中,运动和显示是完全同步的。即:在定时器中调用所有 敌人的运动函数,然后将主角的动画向前推一帧,接着绘制地图,调 用所有敌人的显示函数、重绘主角。这样的好处是不会掉帧,但带来 的问题是,如果要提高敌人的运动速度,那么帧数也跟着上去了。所 以当DEMO版反馈说速度太慢的时候,我修改起来非常困难。而这个问 题到最后也仅仅是将4步一格该成了2步一格。 第三,VC中数组存在上限。如果用“int aaa[1000000000]”定义一个 数组,编译器肯定不会给分配那么大的内存空间。而在这个程序中, 地图矩阵、NPC矩阵都超过了VC中数组的上限。但这一点知道的太晚了。 在1.0版本中已经发现地图最右端缺少了几行,但不知道是什么原因 造成的。(地图编辑器中未出现此问题,因为地图编辑器是用“序列 化”的方式存盘读盘的。)解决这个问题的方法是用“new”来分配 内存空间。 第四,由于不知道应该如何使用“new”和“delete”,几乎所有的DC 都使用了全局变量。这是完全没有必要的。程序运行期大约会耗用20 多M的内存空间,相当于一个大型游戏所使用的内存空间了。 另外,在游戏的剧情、美工方面也有许多问题,总之一个词“业余”。 我就不总结了。下一部作品,我将争取在程序上有一个质的飞跃。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值