Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.72 KB | None | 0 0
  1. /// \file main.cpp
  2. /// \brief Main file for Demo 0.
  3.  
  4. /// \mainpage Demo 0: The Black Screen of Death
  5. ///
  6. /// This is our first Windows application, Hello World for Windows in a sense.
  7. /// It fires up a black window with a text message on it and waits for user
  8. /// to hit the ESC key to exit. You will learn how to register a window,
  9. /// create it, draw graphics on it using the Windows GDI, respond to user
  10. /// keyboard input, and shut down the application gracefully. There is no
  11. /// gameplay yet and no DirectX graphics, but hey, you've gotta
  12. /// learn to crawl before you can walk, right? You can do this either windowed
  13. /// or fullscreen by changing the initial value of the global variable
  14. /// g_bWindowed, and the resolution of the window by changing g_nScreenWidth
  15. /// and g_nScreenHeight.
  16.  
  17. #include <windows.h>
  18. #include <windowsx.h>
  19.  
  20. //globals
  21. BOOL g_bActiveApp; ///< TRUE if this is the active application
  22. HWND g_HwndApp; ///< Application window handle.
  23. HINSTANCE g_hInstance; ///< Application instance handle.
  24.  
  25. //graphics settings
  26. int g_nScreenWidth = 1024; ///< Screen width.
  27. int g_nScreenHeight = 768; ///< Screen height.
  28. BOOL g_bWindowed = TRUE; ///< TRUE to run in a window, FALSE for fullscreen.
  29.  
  30. /// \brief Keyboard handler.
  31. ///
  32. /// Handler for keyboard messages from the Windows API. Takes the appropriate
  33. /// action when the user presses a key on the keyboard.
  34. /// \param keystroke Virtual key code for the key pressed
  35. /// \return TRUE if the game is to exit
  36.  
  37. BOOL KeyboardHandler(WPARAM keystroke){
  38.   switch(keystroke){
  39.     case VK_ESCAPE: //exit game
  40.       return TRUE; //exit keyboard handler
  41.       break;
  42.   } //switch
  43.   return FALSE; //normal exit
  44. } //KeyboardHandler
  45.  
  46. /// \brief Window procedure.
  47. ///
  48. /// Handler for messages from the Windows API.
  49. /// \param hwnd Window handle
  50. /// \param message Message code
  51. /// \param wParam Parameter for message
  52. /// \param lParam Second parameter for message
  53. /// \return 0 if message is handled
  54.  
  55. LRESULT CALLBACK WindowProc(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam){
  56.   switch(message){ //handle message
  57.     case WM_ACTIVATEAPP: g_bActiveApp = (BOOL)wParam; break; //iconize
  58.  
  59.     case WM_KEYDOWN: //keyboard hit
  60.       if(KeyboardHandler(wParam))DestroyWindow(hwnd);
  61.       break;
  62.  
  63.     case WM_DESTROY:  //on exit
  64.       ShowCursor(TRUE); //show mouse cursor
  65.       MessageBox(nullptr, "The Black Screen of Death is Watching You.",
  66.         "Fooled you", MB_OK | MB_ICONEXCLAMATION);
  67.       PostQuitMessage(0); //this is the last thing to do on exit
  68.       break;
  69.  
  70.     default: //default window procedure
  71.       return DefWindowProc(hwnd, message, wParam, lParam);
  72.   } //switch(message)
  73.   return 0;
  74. } //WindowProc
  75.  
  76. /// \brief Create a default window.
  77. ///
  78. /// Register and create a window, either full screen or a window on the
  79. /// desktop depending on the value of g_bWindowed. It takes some shenanigans
  80. /// to make sure that the client area of the window is exactly g_nScreenWidth
  81. /// by g_nScreenHeight since Windows usually includes the banner and border
  82. /// when specifying window size. The window is centered on the desktop too.
  83. /// \param name The name of this application.
  84. /// \param hInstance Handle to the current instance of this application.
  85. /// \param nCmdShow Specifies how the window is to be shown.
  86. /// \return Handle to the application window.
  87.  
  88. HWND CreateDefaultWindow(char* name, HINSTANCE hInstance, int nCmdShow){
  89.  
  90.   WNDCLASS wc; //window registration info
  91.   //fill in registration information wc
  92.   wc.style = CS_HREDRAW | CS_VREDRAW; //style
  93.   wc.lpfnWndProc = WindowProc; //window message handler
  94.   wc.cbClsExtra = wc.cbWndExtra = 0;
  95.   wc.hInstance = hInstance; //instance handle
  96.   wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  97.   wc.hCursor = nullptr; //no cursor
  98.   wc.hbrBackground = nullptr; //we will draw background
  99.   wc.lpszMenuName = nullptr; //no menu
  100.   wc.lpszClassName = name; //app name provided as parameter
  101.  
  102.   RegisterClass(&wc); //register window
  103.  
  104.   //create and set up window
  105.   HWND hwnd; //window handle
  106.  
  107.   //get resolution that user's monitor is currently set at
  108.   int nDevScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  109.   int nDevScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  110.  
  111.   if(g_bWindowed){ //in a window
  112.     RECT r; //desired window rectangle
  113.  
  114.     //initialize r to size of client area in window
  115.     r.left = 0; r.right = g_nScreenWidth;
  116.     r.top = 0; r.bottom = g_nScreenHeight;
  117.  
  118.     //adjust r to include Windows border and stuff
  119.     AdjustWindowRectEx(&r, WS_POPUP | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
  120.       TRUE, WS_EX_APPWINDOW | WS_EX_DLGMODALFRAME);
  121.  
  122.     //create window
  123.     hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_DLGMODALFRAME, name, name,
  124.       WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX, 0, 0,
  125.       r.right - r.left, r.bottom - r.top, nullptr, nullptr, hInstance, nullptr);
  126.  
  127.     //center window on screen
  128.     int x = (nDevScreenWidth - g_nScreenWidth)/2;
  129.     int y = (nDevScreenHeight - g_nScreenHeight)/2;
  130.     ::SetWindowPos(hwnd, nullptr, x, y, g_nScreenWidth, g_nScreenHeight,
  131.       SWP_NOZORDER | SWP_SHOWWINDOW);
  132.   } //if
  133.   else //full screen
  134.     hwnd = CreateWindowEx(WS_EX_TOPMOST, name, name, WS_POPUP, 0, 0,
  135.       nDevScreenWidth, nDevScreenHeight, nullptr, nullptr, hInstance, nullptr);  
  136.  
  137.   if(hwnd){ //if successfully created window
  138.     ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); //show and update
  139.     SetFocus(hwnd); //get input from keyboard
  140.   } //if
  141.  
  142.   return hwnd; //return window handle
  143. } //CreateDefaultWindow
  144.  
  145. /// \brief Initialize graphics.
  146. ///
  147. /// Initialize the graphics using Windows API functions.
  148. /// Draws a black rectangle with some white text on it.
  149.  
  150. void InitGraphics(){ //initialize graphics
  151.   HDC hdc = GetDC(g_HwndApp); //device context for screen
  152.   RECT rc; //screen rectangle
  153.  
  154.   //draw black rectangle to screen
  155.   GetClientRect(g_HwndApp, &rc); //get screen rectangle into rc
  156.   FillRect(hdc, &rc ,(HBRUSH)GetStockObject(BLACK_BRUSH)); //fill rc black
  157.  
  158.   //draw white text
  159.   SetTextColor(hdc, RGB(255, 255, 255)); //white text
  160.   SetBkMode(hdc, TRANSPARENT); //transparent background
  161.   HFONT hFont = CreateFont(-MulDiv(30, GetDeviceCaps(hdc, LOGPIXELSY), 72),
  162.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Old English Text MT"); //truly a scary number of zeros;
  163.   if(hFont)SelectObject(hdc, hFont);
  164.   DrawText(hdc, "Black Screen of Death: Hit Esc to Exit", -1, &rc, //draw text...
  165.     DT_CENTER | DT_VCENTER | DT_SINGLELINE); //...centered on screen
  166.  
  167.   //clean up and exit
  168.   if(hFont)DeleteObject(hFont);
  169.   ReleaseDC(g_HwndApp, hdc); //release device context
  170. } //InitGraphics
  171.  
  172. /// \brief Winmain.
  173. ///
  174. /// Main entry point for this application.
  175. /// \param hInst Handle to the current instance of this application.
  176. /// \param hPrevInst Handle to previous instance, deprecated.
  177. /// \param lpCmdLine Command line string, unused.
  178. /// \param nShow Specifies how the window is to be shown.
  179. /// \return TRUE if application terminates correctly.
  180.  
  181. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShow){
  182.   MSG msg; //current message
  183.   HWND hwnd; //handle to fullscreen window
  184.  
  185.   g_hInstance = hInst;
  186.  
  187.   //create fullscreen window
  188.   hwnd = CreateDefaultWindow("Ned Demo 0: The Black Screen of Death", hInst, nShow);
  189.   if(!hwnd)return FALSE; //bail if problem creating window
  190.   g_HwndApp = hwnd; //save window handle
  191.  
  192.   InitGraphics(); //initialize graphics
  193.  
  194.   //message loop
  195.   while(TRUE)
  196.     if(PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)){ //if message waiting
  197.       if(!GetMessage(&msg, nullptr, 0, 0))return (int)msg.wParam; //get it
  198.       TranslateMessage(&msg); DispatchMessage(&msg); //translate it
  199.     } //if
  200.     else if(!g_bActiveApp)WaitMessage(); //else if not active, wait for msg
  201. } //WinMain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement