Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// \file main.cpp
- /// \brief Main file for Demo 0.
- /// \mainpage Demo 0: The Black Screen of Death
- ///
- /// This is our first Windows application, Hello World for Windows in a sense.
- /// It fires up a black window with a text message on it and waits for user
- /// to hit the ESC key to exit. You will learn how to register a window,
- /// create it, draw graphics on it using the Windows GDI, respond to user
- /// keyboard input, and shut down the application gracefully. There is no
- /// gameplay yet and no DirectX graphics, but hey, you've gotta
- /// learn to crawl before you can walk, right? You can do this either windowed
- /// or fullscreen by changing the initial value of the global variable
- /// g_bWindowed, and the resolution of the window by changing g_nScreenWidth
- /// and g_nScreenHeight.
- #include <windows.h>
- #include <windowsx.h>
- //globals
- BOOL g_bActiveApp; ///< TRUE if this is the active application
- HWND g_HwndApp; ///< Application window handle.
- HINSTANCE g_hInstance; ///< Application instance handle.
- //graphics settings
- int g_nScreenWidth = 1024; ///< Screen width.
- int g_nScreenHeight = 768; ///< Screen height.
- BOOL g_bWindowed = TRUE; ///< TRUE to run in a window, FALSE for fullscreen.
- /// \brief Keyboard handler.
- ///
- /// Handler for keyboard messages from the Windows API. Takes the appropriate
- /// action when the user presses a key on the keyboard.
- /// \param keystroke Virtual key code for the key pressed
- /// \return TRUE if the game is to exit
- BOOL KeyboardHandler(WPARAM keystroke){
- switch(keystroke){
- case VK_ESCAPE: //exit game
- return TRUE; //exit keyboard handler
- break;
- } //switch
- return FALSE; //normal exit
- } //KeyboardHandler
- /// \brief Window procedure.
- ///
- /// Handler for messages from the Windows API.
- /// \param hwnd Window handle
- /// \param message Message code
- /// \param wParam Parameter for message
- /// \param lParam Second parameter for message
- /// \return 0 if message is handled
- LRESULT CALLBACK WindowProc(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam){
- switch(message){ //handle message
- case WM_ACTIVATEAPP: g_bActiveApp = (BOOL)wParam; break; //iconize
- case WM_KEYDOWN: //keyboard hit
- if(KeyboardHandler(wParam))DestroyWindow(hwnd);
- break;
- case WM_DESTROY: //on exit
- ShowCursor(TRUE); //show mouse cursor
- MessageBox(nullptr, "The Black Screen of Death is Watching You.",
- "Fooled you", MB_OK | MB_ICONEXCLAMATION);
- PostQuitMessage(0); //this is the last thing to do on exit
- break;
- default: //default window procedure
- return DefWindowProc(hwnd, message, wParam, lParam);
- } //switch(message)
- return 0;
- } //WindowProc
- /// \brief Create a default window.
- ///
- /// Register and create a window, either full screen or a window on the
- /// desktop depending on the value of g_bWindowed. It takes some shenanigans
- /// to make sure that the client area of the window is exactly g_nScreenWidth
- /// by g_nScreenHeight since Windows usually includes the banner and border
- /// when specifying window size. The window is centered on the desktop too.
- /// \param name The name of this application.
- /// \param hInstance Handle to the current instance of this application.
- /// \param nCmdShow Specifies how the window is to be shown.
- /// \return Handle to the application window.
- HWND CreateDefaultWindow(char* name, HINSTANCE hInstance, int nCmdShow){
- WNDCLASS wc; //window registration info
- //fill in registration information wc
- wc.style = CS_HREDRAW | CS_VREDRAW; //style
- wc.lpfnWndProc = WindowProc; //window message handler
- wc.cbClsExtra = wc.cbWndExtra = 0;
- wc.hInstance = hInstance; //instance handle
- wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
- wc.hCursor = nullptr; //no cursor
- wc.hbrBackground = nullptr; //we will draw background
- wc.lpszMenuName = nullptr; //no menu
- wc.lpszClassName = name; //app name provided as parameter
- RegisterClass(&wc); //register window
- //create and set up window
- HWND hwnd; //window handle
- //get resolution that user's monitor is currently set at
- int nDevScreenWidth = GetSystemMetrics(SM_CXSCREEN);
- int nDevScreenHeight = GetSystemMetrics(SM_CYSCREEN);
- if(g_bWindowed){ //in a window
- RECT r; //desired window rectangle
- //initialize r to size of client area in window
- r.left = 0; r.right = g_nScreenWidth;
- r.top = 0; r.bottom = g_nScreenHeight;
- //adjust r to include Windows border and stuff
- AdjustWindowRectEx(&r, WS_POPUP | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW,
- TRUE, WS_EX_APPWINDOW | WS_EX_DLGMODALFRAME);
- //create window
- hwnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_DLGMODALFRAME, name, name,
- WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX, 0, 0,
- r.right - r.left, r.bottom - r.top, nullptr, nullptr, hInstance, nullptr);
- //center window on screen
- int x = (nDevScreenWidth - g_nScreenWidth)/2;
- int y = (nDevScreenHeight - g_nScreenHeight)/2;
- ::SetWindowPos(hwnd, nullptr, x, y, g_nScreenWidth, g_nScreenHeight,
- SWP_NOZORDER | SWP_SHOWWINDOW);
- } //if
- else //full screen
- hwnd = CreateWindowEx(WS_EX_TOPMOST, name, name, WS_POPUP, 0, 0,
- nDevScreenWidth, nDevScreenHeight, nullptr, nullptr, hInstance, nullptr);
- if(hwnd){ //if successfully created window
- ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); //show and update
- SetFocus(hwnd); //get input from keyboard
- } //if
- return hwnd; //return window handle
- } //CreateDefaultWindow
- /// \brief Initialize graphics.
- ///
- /// Initialize the graphics using Windows API functions.
- /// Draws a black rectangle with some white text on it.
- void InitGraphics(){ //initialize graphics
- HDC hdc = GetDC(g_HwndApp); //device context for screen
- RECT rc; //screen rectangle
- //draw black rectangle to screen
- GetClientRect(g_HwndApp, &rc); //get screen rectangle into rc
- FillRect(hdc, &rc ,(HBRUSH)GetStockObject(BLACK_BRUSH)); //fill rc black
- //draw white text
- SetTextColor(hdc, RGB(255, 255, 255)); //white text
- SetBkMode(hdc, TRANSPARENT); //transparent background
- HFONT hFont = CreateFont(-MulDiv(30, GetDeviceCaps(hdc, LOGPIXELSY), 72),
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Old English Text MT"); //truly a scary number of zeros;
- if(hFont)SelectObject(hdc, hFont);
- DrawText(hdc, "Black Screen of Death: Hit Esc to Exit", -1, &rc, //draw text...
- DT_CENTER | DT_VCENTER | DT_SINGLELINE); //...centered on screen
- //clean up and exit
- if(hFont)DeleteObject(hFont);
- ReleaseDC(g_HwndApp, hdc); //release device context
- } //InitGraphics
- /// \brief Winmain.
- ///
- /// Main entry point for this application.
- /// \param hInst Handle to the current instance of this application.
- /// \param hPrevInst Handle to previous instance, deprecated.
- /// \param lpCmdLine Command line string, unused.
- /// \param nShow Specifies how the window is to be shown.
- /// \return TRUE if application terminates correctly.
- int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShow){
- MSG msg; //current message
- HWND hwnd; //handle to fullscreen window
- g_hInstance = hInst;
- //create fullscreen window
- hwnd = CreateDefaultWindow("Ned Demo 0: The Black Screen of Death", hInst, nShow);
- if(!hwnd)return FALSE; //bail if problem creating window
- g_HwndApp = hwnd; //save window handle
- InitGraphics(); //initialize graphics
- //message loop
- while(TRUE)
- if(PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)){ //if message waiting
- if(!GetMessage(&msg, nullptr, 0, 0))return (int)msg.wParam; //get it
- TranslateMessage(&msg); DispatchMessage(&msg); //translate it
- } //if
- else if(!g_bActiveApp)WaitMessage(); //else if not active, wait for msg
- } //WinMain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement