WPARAM MainLoop() { MSG msg; while(1) // Do our infinate loop { // Check if there was a message if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) // If the message wasnt to quit break; TranslateMessage(&msg); // Find out what the message does DispatchMessage(&msg); // Execute the message } else if(LockFrameRate(60)) // if it is time to render { // Render the scene every frame RenderScene(); } } DeInit(); // Free everything after the program finishes return(msg.wParam); // Return from the program }
// Standard WinMain int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, // Window extended style kClassName, "www.GameTutorials.com -- Rotating Cube", winStyle, // Window style CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // Set up our projection matrix once because it will not change g3D->setProjMatrix(DEG2RAD(60), (float)kWinWid / (float)kWinHgt, 1.0f, 8192.0f); // We set up our view matrix once because it will never change g3D->setViewMatrix(CPos(0.0f, 0.0f, -5.0f), CPos(0.0f, 0.0f, 0.0f)); ShowWindow(hwnd, ishow); UpdateWindow(hwnd); while(1) // While the app is running... { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Handle messages from the OS { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // If it's time to render, do so { DrawAndRotateCube(); } else Sleep(1); // Otherwise give the OS some time to process other things } // end of while(1) g3D->deinit(); // Free up the D3D object UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }
// Our Win32 main int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; // Handle to our window MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, kClassName, "www.GameTutorials.com -- Shadow Mapping (HLSL)", winStyle, CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint if(!CreateSphere(0.05f, D3DCOLOR_XRGB(255,255,0), &gLight)) return false; // Couldn't create the sphere... Something very bad happened... if(!CreateSphere(0.75f, D3DCOLOR_XRGB(0,128,64), &gSphere)) return false; // Couldn't create the sphere... Something very bad happened... if(!CreateBox(1.0f, 2.0f, 1.0f, D3DCOLOR_XRGB(0, 128, 64), &gBox)) return false; // Couldn't create the box... This isn't good! if(!CreateBox(16.0f, 0.05f, 16.0f, D3DCOLOR_XRGB(225, 225, 255), &gGround)) return false; // Couldn't create the ground... Time to bail if(!gShadowMap.createRenderTexture(512, 512, D3DFMT_R32F)) return false; // Couldn't create a shadow map texture. Your video card // probably doesn't support the D3DFMT_R32F format. You will need a beefier card to // run this tutorial if(!gRenderTarget.init(512, 512, D3DFMT_R32F, D3DFMT_D24S8)) return false; // Couldn't create a shadow map texture. Your video card doesn't support render // targets or it probably doesn't support the D3DFMT_R32F format. You will need // a beefier card to run this tutorial // Set up our projection matrix once because it will not change g3D->setProjMatrix(DEG2RAD(60), 1.0f, 2048.0f); // We set up our view matrix once because it will never change g3D->setViewMatrix(kEyePos, CPos(0.0f, 0.0f, 0.0f)); D3DXMATRIXA16 projMat; // Create texture projection matrix D3DXMatrixPerspectiveFovLH(&projMat, DEG2RAD(60), 1.0f, 0.1f, 100.0f); // Set data in our shader that will never change g3D->mShader->SetFloatArray("gLightProjMat", &projMat._11, 16); g3D->mShader->SetFloatArray("gEyePos", &kEyePos.x, 3); // Set the camera's eye position g3D->mShader->SetFloatArray("gDiffuse", &kDiffuse.r, 3); // Lights diffuse color g3D->mShader->SetFloatArray("gSpecular", &kSpecular.r, 3); // Lights specular color g3D->mShader->SetFloatArray("gAmbient", &kAmbient.r, 3); // Lights ambient color // Show and update the window ShowWindow(hwnd, ishow); UpdateWindow(hwnd); while(1) // While the app is alive { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // If the OS has a message for us { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // Else, if it's time to draw { static int angle = 0; // Stores the amount rotated in degrees D3DXMATRIX tMat, rMat; // This will hold translation and rotation matrices D3DXMATRIX wMat; // This will hold the world matrix of objects we want rendered D3DXMATRIX shadowMat; // The matrix for projecting the shadow to a plane // The light is going to potentially move, so we need to // set it's position every frame g3D->mShader->SetFloatArray("gLightPos", &gLightPos.x, 3); // Render the shadow map RenderShadowMap(); // Render the ground, box and sphere with shadows RenderScene(); } else Sleep(1); // Otherwise, give the OS some time to process other programs } // end of while(1) gLight->Release(); // Free up the light gSphere->Release(); // Free up the sphere gBox->Release(); // Free up the box gGround->Release(); // Free up the ground g3D->deinit(); // Free up the D3D object UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }
// Main program int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, // Window extended style kClassName, "www.GameTutorials.com -- DirectX Mesh", winStyle, // Window style CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint // Create the sphere if(!CreateSphere(0.5f, D3DCOLOR_XRGB(0,25,225), &gSphere)) return false; // Couldn't create the sphere... Something very bad happened... // Create the torus if(!CreateTorus(0.5f, 1.0f, D3DCOLOR_XRGB(225,25,25), &gTorus)) return false; // Couldn't create the sphere... Something very bad happened... // Set up our projection matrix once because it will not change g3D->setProjMatrix(DEG2RAD(60), (float)kWinWid / (float)kWinHgt, 1.0f, 8192.0f); // We set up our view matrix once because it will never change g3D->setViewMatrix(CPos(0.0f, 0.0f, -5.0f), CPos(0.0f, 0.0f, 0.0f)); ShowWindow(hwnd, ishow); UpdateWindow(hwnd); // While the app is running... while(1) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // If the OS has a message for us { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // Else, if it's time to draw { g3D->begin(); // Begin the frame g3D->clear(); // Clear the back buffer static float angle = 0; D3DXMATRIX wMat; // Set "wMat" to the identity matrix, then set the world matrix // to "wMat" so the sphere is centered in the world D3DXMatrixIdentity(&wMat); g3D->setWorldMatrix(&wMat); g3D->render(gSphere); // Draw the sphere // Set "wMat" to a rotation around the X, Y, and Z axis, then // set the world matrix to "wMat" so the torus rotates around all // three axises D3DXMatrixRotationYawPitchRoll(&wMat, DEG2RAD(angle), DEG2RAD(angle), DEG2RAD(angle)); g3D->setWorldMatrix(&wMat); g3D->render(gTorus); // Draw the torus ++angle; // Update the amount to rotate the torus by g3D->end(); // End the frame } else Sleep(1); // Otherwise, give the OS some time to process other programs } // end of while(1) gSphere->Release(); // Free up the sphere gTorus->Release(); // Free up the torus g3D->deinit(); // Free up the D3D object UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }
// WinMain int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, // Window extended style kClassName, "www.GameTutorials.com -- Ray and Plane Collision", winStyle, // Window style CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, // Window width rect.bottom - rect.top, // Window height NULL, NULL, hinstance, NULL); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // We set up our projection matrix once because it will never change g3D->setProjMatrix(DEG2RAD(60), (float)rect.right / (float)rect.bottom, 1.0f, 8192.0f); // We set our view matrix once because it will never change g3D->setViewMatrix(CPos(0,1,-3.0f), CPos(0,0,0)); ShowWindow(hwnd, ishow); UpdateWindow(hwnd); // While the app is running while(1) { // If the OS has messages for us, handle them if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // If it is time to draw, do so { g3D->begin(); // Begin drawing g3D->clear(); // Clear the screen // Draw our ray gRay.render(D3DCOLOR_ARGB(255,225,225,0)); // Draw our plane in GREEN if our ray intersects with it if(gPlane.intersect(gRay)) gPlane.render(D3DCOLOR_ARGB(255,25,225,25)); else // Otherwise draw it in RED gPlane.render(D3DCOLOR_ARGB(255,225,25,25)); g3D->end(); // Finish drawing } else Sleep(1); // Give the OS a little bit of time to process other things } // end of while(1) g3D->deinit(); // Free up CD3DObj UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd; MSG msg; WNDCLASSEX wndclassex = {0}; // Init the fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndclassex.lpszClassName = class_name; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Create the window hwnd = CreateWindowEx(WS_EX_APPWINDOW, class_name, "www.GameTutorials.com -- Shortest Path", WS_OVERLAPPED | WS_SYSMENU, // Window won't be resizable CW_USEDEFAULT, CW_USEDEFAULT, WIN_WID, WIN_HGT, NULL, NULL, hinstance, NULL); // Error Check if(!hwnd) return EXIT_FAILURE; // Init our CWinObj (sets up double buffering as well) if(gWinObj.init(hwnd) == false) return EXIT_FAILURE; // Show and update window ShowWindow(hwnd, ishow); UpdateWindow(hwnd); while(1) { // Checking for window messages -- If we get one we'll deal with it if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate(FRAME_RATE)) // Otherwise we'll redraw the screen if we have to { // Fill the window to black FillRect(gWinObj.getBackHDC(),&(gWinObj.getClientRect()), (HBRUSH)GetStockObject(BLACK_BRUSH)); gGrid.draw(gWinObj.getBackHDC(),XPOS,YPOS); // Blit the back buffer to the front buffer BitBlt(gWinObj.getHDC(),0,0,gWinObj.getClientWid(),gWinObj.getClientHgt(), gWinObj.getBackHDC(),0,0,SRCCOPY); // If done drawing, prompt the user to see if they want to do it again if(gGrid.doneDrawing()) { if(MessageBox(hwnd,"Do again?","Shortest path completed!!!", MB_YESNO | MB_ICONQUESTION) == IDYES) { gGrid.init(); gRbTimes = 0; } else break; // We're done with the app } } // end of else if(LockFrameRate(FRAME_RATE)) } // end of while(1) UnregisterClass(class_name,hinstance); // Unregister the WNDCLASSEX return msg.wParam; }
// Our Win32 main int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; // Handle to our window MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, kClassName, "www.GameTutorials.com -- Render Targets", winStyle, CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint if(!gBlurTexture1.createRenderTexture(256, 256, D3DFMT_A8R8G8B8)) return false; // Couldn't create a off screen texture to render to if(!gBlurTexture2.createRenderTexture(256, 256, D3DFMT_A8R8G8B8)) return false; // Couldn't create a off screen texture to render to if(!gDiffTex.load("texture.jpg")) return false; if(!gRenderTarget.init(256, 256, D3DFMT_A8R8G8B8, D3DFMT_D24S8)) return false; // Couldn't create a render texture... Does your video card support this? Make3x3GuassianDistribution(); // Set up our projection matrix once because it will not change g3D->setProjMatrix(DEG2RAD(60), 1.0f, 2048.0f); // We set up our view matrix once because it will never change g3D->setViewMatrix(kEyePos, CPos(0.0f, 0.0f, 0.0f)); g3D->mShader->SetFloatArray("gEyePos", &kEyePos.x, 3); // Set the camera's eye position g3D->mShader->SetFloatArray("gBlurWeights", gBlurWeights, 9); // Gaussian blur weights // Show and update the window ShowWindow(hwnd, ishow); UpdateWindow(hwnd); while(1) // While the app is alive { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // If the OS has a message for us { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // Else, if it's time to draw { // Render the off screen texture (created directly above) to another // off screen texture using a Gaussian blur if(gToggle) Combined(); else RenderSobelFilter(); // Render the two textures to the screen RenderScene(); } else Sleep(1); // Otherwise, give the OS some time to process other programs } // end of while(1) g3D->deinit(); // Free up the D3D object UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }
// Main window program int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, // Window extended style kClassName, "www.GameTutorials.com -- D3D Vertex Buffers", winStyle, // Window style CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint if(gTexture.load("Texture.jpg") == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint // Create and set the vertex buffer in our CD3DMesh object if(gQuad.setVertexData(gQuadVertexData, kMaxVerts) == false) return EXIT_FAILURE; // This is bad! The tutorial is officially broken // Set the perspective matrix once because it will never change g3D->setProjMatrix(DEG2RAD(60), (float)rect.right / (float)rect.bottom, 1.0f, 8192.0f); // We set up our view matrix once because it will never change g3D->setViewMatrix(CPos(0.0f, 0.0f, -5.0f), CPos(0.0f, 0.0f, 0.0f)); ShowWindow(hwnd, ishow); UpdateWindow(hwnd); while(1) // While the app is running { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Handle messages from the OS { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // If it is time to draw, do so { g3D->begin(); // Begin the scene g3D->clear(); // Clear the Z-buffer gTexture.select(); // Select the texture to be used // Render the textured quad gQuad.render(); g3D->end(); // End the scene } else Sleep(1); // Give the OS a slice of time to process other things } // end of while(1) g3D->deinit(); // Free up CD3DObj UnregisterClass(kClassName, hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd; MSG msg; WNDCLASSEX wndclassex = {0}; SDBuffer doubleBuff = {0}; // This is our "double buffer" struct // Fill the fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclassex.lpszClassName = class_name; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); // Load the default arrow cursor RegisterClassEx(&wndclassex); // Register the window hwnd = CreateWindowEx(NULL, // No extra window attributes class_name, "www.GameTutorials.com -- Double Buffering", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, // Window will receive a default x pos on screen CW_USEDEFAULT, // Window will receive a default y pos on screen WIN_WID, WIN_HGT, NULL, NULL, hinstance, NULL); // Error check if(!hwnd) return EXIT_FAILURE; // Something really bad happened! doubleBuff.win_hwnd = hwnd; // Set the HWND of our double buffer struct // Attempt to initialize our double buffering if(!InitDoubleBuffer(doubleBuff)) return EXIT_FAILURE; // Couldn't set up double buffering // Here we'll load up our bitmap HBITMAP img_bmp = (HBITMAP)LoadImage(hinstance,"AnImage.bmp",IMAGE_BITMAP, 0,0,LR_LOADFROMFILE); // Error Check if(!img_bmp) return EXIT_FAILURE; // Couldn't load our image // Create a compatible HDC so that we can draw our "img_bmp" HDC img_dc = CreateCompatibleDC(doubleBuff.win_dc); if(!img_dc) return EXIT_FAILURE; // Select our "img_bmp" into the "img_dc" HBITMAP old_bmp = (HBITMAP)SelectObject(img_dc,img_bmp); ShowWindow(hwnd, ishow); UpdateWindow(hwnd); while(1) { // Check message(s) if there are any if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) { #if DOUBLE_BUFFER // If we are using double buffering // First we fill our back buffer to solid white (the same color as the // background color of our window) FillRect(doubleBuff.back_dc,&doubleBuff.rect,(HBRUSH)GetStockObject(WHITE_BRUSH)); // Next we'll draw the bitmap to our back buffer BitBlt(doubleBuff.back_dc,gXPos,gYPos,gXPos + IMG_WID, gYPos + IMG_HGT,img_dc,0,0,SRCCOPY); // Then we draw the back buffer to the front buffer (our window) BitBlt(doubleBuff.win_dc,0,0,doubleBuff.rect.right, doubleBuff.rect.bottom,doubleBuff.back_dc,0,0,SRCCOPY); #else // No double buffering in use // We fill our window with solid white so we can clear away the "old" // position of the image FillRect(doubleBuff.win_dc,&doubleBuff.rect,(HBRUSH)GetStockObject(WHITE_BRUSH)); // Blit the image to the window BitBlt(doubleBuff.win_dc,gXPos,gYPos,gXPos + IMG_WID, gYPos + IMG_HGT,img_dc,0,0,SRCCOPY); // **NOTE** Do not be mislead by the use of the "doubleBuff" variable. // We are ONLY using this to access the window's HDC. Absolutely // no double buffering goes on in the between the #else and the // #endif. Be sure to look at how worse it looks without double // buffering. #endif } } // Free up our image memory SelectObject(img_dc,old_bmp); DeleteDC(img_dc); // Free up all the memory associated with our back buffer FreeDoubleBuffer(doubleBuff); UnregisterClass(class_name,hinstance); // Free up WNDCLASSEX return msg.wParam; }
// Main window program int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd = NULL; MSG msg = {0}; WNDCLASSEX wndclassex = {0}; // Init fields we care about wndclassex.cbSize = sizeof(WNDCLASSEX); // Always set to size of WNDCLASSEX wndclassex.style = CS_HREDRAW | CS_VREDRAW; wndclassex.lpfnWndProc = WinProc; wndclassex.hInstance = hinstance; wndclassex.lpszClassName = kClassName; wndclassex.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED); RegisterClassEx(&wndclassex); // Register the WNDCLASSEX RECT rect = { 0, 0, kWinWid, kWinHgt }; // Desired window client rect DWORD winStyleEx = WS_EX_CLIENTEDGE; DWORD winStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN | WS_CLIPSIBLINGS; // Adjust window rect so it gives us our desired client rect when we // create the window AdjustWindowRectEx(&rect, winStyle, false, winStyleEx); // Create the window hwnd = CreateWindowEx(winStyleEx, // Window extended style kClassName, "www.GameTutorials.com -- D3D Fog", winStyle, // Window style CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, hinstance, NULL); // Init our global 3D object if(g3D->init(hwnd) == false) return EXIT_FAILURE; // There's been an error, lets get out of this joint // Get the client rect and make sure our client is the size we want GetClientRect(hwnd, &rect); assert(rect.right == kWinWid && rect.bottom == kWinHgt); // We set up our projection matrix once because it will never change g3D->setProjMatrix(DEG2RAD(60), (float)rect.right / (float)rect.bottom, 1.0f, 8192.0f); // If we can initialize the fog, exit the application if(!InitFog()) return EXIT_FAILURE; ShowCursor(FALSE); // Hide cursor ShowWindow(hwnd, ishow); UpdateWindow(hwnd); // Slam the cursor to the middle of the screen SetCursorPos(GetSystemMetrics(SM_CXSCREEN) >> 1, GetSystemMetrics(SM_CYSCREEN) >> 1); // While the app is running... while(1) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Handle messages from the OS { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else if(LockFrameRate()) // Otherwise if it's time to draw { static float angle = 0; // Angle of rotation D3DXMATRIX wMat; // World matrix CameraMouseInput(); // Move camera via the mouse // Set the world view to the current camera view g3D->setViewMatrix(gCamera); g3D->begin(); // Begin drawing g3D->clear(kFogColor); // Clear the screen to the fog color // Rotate around the Y-axis g3D->setWorldMatrix(D3DXMatrixRotationY(&wMat, DEG2RAD(++angle))); // Draw 4 cubes DrawCube(CPos(2,0,0), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); DrawCube(CPos(-2,0,0), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); DrawCube(CPos(0,0,2), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); DrawCube(CPos(0,0,-2), 0.5f, D3DCOLOR_ARGB(255, 255, 0, 0)); // Draw the grid g3D->setWorldMatrix(D3DXMatrixIdentity(&wMat)); DrawGrid(CPos(0,-2,0), 32, D3DCOLOR_ARGB(255, 0, 200, 0)); g3D->end(); // Finish drawing } else Sleep(1); // Give the OS a tiny bit of time to process other things } // end of while(1) ShowCursor(TRUE); // Reshow cursor g3D->deinit(); // Free up CD3DObj UnregisterClass(kClassName,hinstance); // Free up WNDCLASSEX return EXIT_SUCCESS; // Application was a success }