Guest User

window.cpp

a guest
Aug 19th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #pragma once
  2. #include "window.h"
  3. #include "dxmanager.h"
  4. #include <d3d10.h>
  5.  
  6. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  7. //Window Initialization Function
  8. bool Window::InitializeWindow(HINSTANCE* hInstance, HWND* hwnd, LPCTSTR WndClassName, LPCSTR WndTitle, int showWnd, int* width, int* height, bool* windowed)
  9. {
  10.     //extended styles of WNDCLASSESEX
  11.     typedef struct _WNDCLASS
  12.     {
  13.         UINT cbSize;
  14.         UINT style;
  15.         WNDPROC lpfnWndProc;
  16.         int cbClsExtra;
  17.         int cbWndExtra;
  18.         HANDLE hInstance;
  19.         HICON hIcon;
  20.         HCURSOR hCursor;
  21.         HBRUSH hbrBackground;
  22.         LPCTSTR lpszMenuName;
  23.         LPCTSTR lpszClassName;
  24.     } WNDCLASS;
  25.     WNDCLASSEX wc;
  26.  
  27.     //initializes wc to be the same sizes as WNDCLASSEX
  28.     wc.cbSize = sizeof(WNDCLASSEX);
  29.  
  30.     //enabled styles for our window
  31.     wc.style = CS_HREDRAW | CS_VREDRAW;
  32.  
  33.     //pointer to windows messages function
  34.     wc.lpfnWndProc = WndProc;
  35.  
  36.     //#extra bytes allocated after WNDCLASSEX
  37.     wc.cbClsExtra = NULL;
  38.  
  39.     //#extra bytes allocated after the windows instance
  40.     wc.cbWndExtra = NULL;
  41.  
  42.     //makes wc's instance handler the same as the one we are using
  43.     wc.hInstance = *hInstance;
  44.  
  45.     //sets the icon at the top left corner of the window
  46.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  47.  
  48.     //sets the cursor's icon
  49.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  50.  
  51.     //sets background color (black)
  52.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
  53.  
  54.     //name to the menu attached to the window. (We don't have one)
  55.     wc.lpszMenuName = NULL;
  56.  
  57.     //Name of Class
  58.     wc.lpszClassName = WndClassName;
  59.  
  60.     //Taskbar Icon
  61.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  62.  
  63.     //Register's class, ends if failed (would not create window)
  64.     if (!RegisterClassEx(&wc))
  65.     {
  66.         MessageBox(NULL, "Error registering class", "Error", MB_OK | MB_ICONERROR);
  67.         return 1;
  68.     }
  69.  
  70.     //Creation of window
  71.     *hwnd = CreateWindowEx(NULL, //WNDCLASSEX Styles to be applied to this specific window
  72.         WndClassName, //name of registered class that we use
  73.         WndTitle, //name of the window
  74.         WS_OVERLAPPEDWINDOW, //window styles (seperate by '|')
  75.         CW_USEDEFAULT, CW_USEDEFAULT, //Starting x&y positions
  76.         *width, *height, //size of window
  77.         NULL, //handle to parent window (we don't have a parent)
  78.         NULL, //handle to menu attached to window (no menu)
  79.         *hInstance, //instance of current program
  80.         NULL
  81.         ); //if use of MDI client (we don't)
  82.     //if the window fails to initialize
  83.     if (!*hwnd)
  84.     {
  85.         MessageBox(NULL, "Error creating window", "Error", MB_OK | MB_ICONERROR);
  86.         return 1;
  87.     }
  88.     ShowWindow(*hwnd, showWnd);
  89.     UpdateWindow(*hwnd);
  90.  
  91.     return true;
  92. }
  93.  
  94. //Message Loop until program is closed
  95. int Window::messageLoop(DXManager* DXM,ID3D10Device* d3dDevice, IDXGISwapChain* swapChain, ID3D10RenderTargetView* renderTargetView)
  96. {
  97.     MSG msg;
  98.     ZeroMemory(&msg, sizeof(MSG));
  99.     while (true)
  100.     {
  101.         if (PeekMessage(&msg, //LPMSG lpMsg, pointer to message structure
  102.             NULL, //HWND hWndParam, hande to the window that is sending the message (NULL=takes message from any window in the program
  103.             0, //UINT wMsgFilterMin, max value of the message range to be examined (MIN & MAX = 0, all messages are examined)
  104.             0, //wMsgFilterMax, max value of the message range to be examined (MIN & MAX = 0, all messages are examined)
  105.             PM_REMOVE)) //UINT wRemoveMsg, how the message will be handled
  106.         {
  107.             //if the message was a quit message, close the program
  108.             if (msg.message == WM_QUIT)
  109.                 break;
  110.             //if the message was a windows message, translate and dispatch.
  111.             TranslateMessage(&msg);
  112.             DispatchMessage(&msg);
  113.         }
  114.         //if it was not a windows message, run the game
  115.         else
  116.         {
  117.             float red = 0.0f;
  118.             float green = 0.0f;
  119.             float blue = 0.0f;
  120.             int colormodr = 1;
  121.             int colormodg = 1;
  122.             int colormodb = 1;
  123.             red += colormodr * 0.00005f;
  124.             green += colormodg * 0.00002f;
  125.             blue += colormodb * 0.00001f;
  126.  
  127.             if (red >= 1.0f || red <= 0.0f)
  128.                 colormodr *= -1;
  129.             if (green >= 1.0f || green <= 0.0f)
  130.                 colormodg *= -1;
  131.             if (blue >= 1.0f || blue <= 0.0f)
  132.                 colormodb *= -1;
  133.  
  134.             const FLOAT bgColor[4] = { red, green, blue, 1.0f };
  135.                 d3dDevice->ClearRenderTargetView(renderTargetView, bgColor);
  136.                 swapChain->Present(0, 0);
  137.         }
  138.     }
  139.  
  140.     return msg.wParam;
  141. }
  142.  
  143. //message processing
  144. LRESULT CALLBACK WndProc(HWND hwnd, //window handler
  145.     UINT msg, //message handler
  146.     WPARAM wParam, //extra message information(keyboard imput detection)
  147.     LPARAM lParam) //extra message information(keyboard imput detection)
  148. {
  149.     switch (msg)
  150.     {
  151.         case WM_KEYDOWN:
  152.             if (wParam == VK_ESCAPE)
  153.             {
  154.                 if (MessageBox(0, "Are you sure you want to quit?", "Really?", MB_YESNO | MB_ICONQUESTION) == IDYES)
  155.                     DestroyWindow(hwnd);
  156.             }
  157.             return 0;
  158.         case WM_DESTROY:
  159.             PostQuitMessage(0);
  160.             return 0;
  161.     }
  162.     return DefWindowProc(hwnd, msg, wParam, lParam);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment