// Create window //----------------------------------------------------------------------------- CPUTResult CPUTWindowWin::Create(CPUT* cput, const cString WindowTitle, const int windowWidth, const int windowHeight, int windowX, int windowY) { if(mhWnd) { return CPUT_ERROR_WINDOW_ALREADY_EXISTS; } ASSERT( (windowX < GetSystemMetrics(SM_CXFULLSCREEN) && (windowX>=-1)), _L("You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams")); ASSERT( (windowY < GetSystemMetrics(SM_CYFULLSCREEN) && (windowY>=-1)), _L("You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams")); // get the hInstance of this executable mhInst = GetModuleHandle(NULL); if(NULL==mhInst) { return CPUT_ERROR_CANNOT_GET_WINDOW_INSTANCE; } // set up app title (if not specified) mAppTitle = WindowTitle; if(0==mAppTitle.compare(_L(""))) { mAppTitle = _L("CPUT Sample"); } // Register the Win32 class for this app WNDCLASS wc; if(TRUE == GetClassInfo(mhInst, mAppTitle.c_str(), &wc)) { // point to the existing one mhInst = wc.hInstance; } else { // register a new windows class ATOM classID; classID = MyRegisterClass(mhInst); if(0==classID) { HandleWin32Error(); return CPUT_ERROR_WINDOW_CANNOT_REGISTER_APP; } } // Perform Win32 instance initialization const int nCmdShow = SW_SHOWNORMAL; if (false == InitInstance(nCmdShow, windowWidth, windowHeight, windowX, windowY)) { return CPUT_ERROR_CANNOT_GET_WINDOW_INSTANCE; } // store the CPUT pointer mCPUT = (CPUT*) cput; return CPUT_SUCCESS; }
// Main message pump //----------------------------------------------------------------------------- int CPUTWindowWin::StartMessageLoop() { // // Message pump // MSG msg = { 0 }; bool fRunning = true; while(fRunning) { // PeekMessage() is a passthru on no events // so it allows us to render while no events are present if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) { if (msg.message == WM_QUIT) { PostQuitMessage(0); fRunning = false; } TranslateMessage( &msg ); DispatchMessage( &msg ); } else { // trigger render and other calls mCPUT->InnerExecutionLoop(); } } // // Drain out the rest of the message queue. // while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } if (UnregisterClass(mAppTitle.c_str(), mhInst) == 0) { HandleWin32Error(); } // // Set the window handle to NULL to indicate window shutdown is complete // mhWnd = NULL; // return code mAppClosedReturnCode = (int) msg.wParam; return mAppClosedReturnCode; }
// Main message pump //----------------------------------------------------------------------------- int CPUTWindowWin::StartMessageLoop() { // Clear message queue MSG msg; //for(;;) //{ // while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) // { // if (msg.message == WM_DESTROY) // return CPUT_ERROR; // TranslateMessage( &msg ); // DispatchMessage( &msg ); // // } //} // // Message pump // bool fRunning = true; while(fRunning) { // PeekMessage() is a passthru on no events // so it allows us to render while no events are present if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) { if (msg.message == WM_QUIT) { PostQuitMessage(0); fRunning = false; } TranslateMessage( &msg ); DispatchMessage( &msg ); } else if (IsWindowMinimized()) { Sleep(100); } else { // trigger render and other calls for (const auto &callBack : mLoopEventCallbacks) { callBack(); } } } Destroy(); // // Drain out the rest of the message queue. // while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } uint32_t numWChars = MultiByteToWideChar(CP_UTF8, 0, mAppTitle.c_str(), -1, NULL, 0); wchar_t* wstr = new wchar_t[numWChars]; MultiByteToWideChar(CP_UTF8, 0, mAppTitle.c_str(), -1, wstr, numWChars); if (UnregisterClass(wstr, mhInstance) == 0) { HandleWin32Error(); } delete[] wstr; // // Set the window handle to NULL to indicate window shutdown is complete // mhWnd = NULL; // return code mAppClosedReturnCode = (int) msg.wParam; return mAppClosedReturnCode; }
// Create window //----------------------------------------------------------------------------- CPUTResult CPUTWindowWin::Create(const std::string WindowTitle, CPUTWindowCreationParams windowParams) { // // Validate that the window starting position is within the virtual desktop // ASSERT((windowParams.windowPositionX == -1) || (windowParams.windowPositionX >= GetSystemMetrics(SM_XVIRTUALSCREEN)), "You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams"); ASSERT((windowParams.windowPositionX <= GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN)), "You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams"); ASSERT((windowParams.windowPositionY == -1) || (windowParams.windowPositionY >= GetSystemMetrics(SM_YVIRTUALSCREEN)), "You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams"); ASSERT((windowParams.windowPositionY <= GetSystemMetrics(SM_YVIRTUALSCREEN) + GetSystemMetrics(SM_CYVIRTUALSCREEN)), "You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams"); // // Width or height of zero means to use a default size // if( (0 == windowParams.windowWidth) || (0 == windowParams.windowHeight) ) { windowParams.windowWidth = 1280; windowParams.windowHeight = 720; } // // If the requested with or height is greater than the size of the desktop, then CW_USEDDEFAULT // is used. This will size the window to the desktop extents based on the window starting position. // int desktopX, desktopY; int desktopWidth, desktopHeight; GetDesktopDimensions(&desktopX, &desktopY, &desktopWidth, &desktopHeight); if (desktopWidth < windowParams.windowWidth || desktopHeight < windowParams.windowHeight) { windowParams.windowWidth = CW_USEDEFAULT; windowParams.windowHeight = CW_USEDEFAULT; } // // A position of -1 means to use the system default value for window placement // if(-1 == windowParams.windowPositionX) { windowParams.windowPositionX = CW_USEDEFAULT; } if (-1 == windowParams.windowPositionY) { windowParams.windowPositionY = CW_USEDEFAULT; } mhInstance = GetModuleHandle(NULL); if( mhInstance == NULL ) { HandleWin32Error(); return CPUT_ERROR_CANNOT_GET_WINDOW_INSTANCE; } mAppTitle = WindowTitle; if (0 == mAppTitle.compare("")) { mAppTitle = "CPUT Sample"; } LPCTSTR iconPathName= TEXT("CPUT.ico"); UINT icon_flags = LR_LOADFROMFILE | LR_DEFAULTSIZE; HANDLE hIcon = LoadImage(mhInstance, iconPathName, IMAGE_ICON, 0, 0, icon_flags); // Clear message queue MSG msg = { 0 }; while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } // // Register the Win32 class for this app // WNDCLASSEX wc; uint32_t numWChars = MultiByteToWideChar(CP_UTF8, 0, mAppTitle.c_str(), -1, NULL, 0); wchar_t* wstr = new wchar_t[numWChars]; MultiByteToWideChar(CP_UTF8, 0, mAppTitle.c_str(), -1, wstr, numWChars); wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = mhInstance; wc.hIcon = (HICON)hIcon; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = wstr; wc.hIconSm = NULL; ATOM registeredClass = RegisterClassEx(&wc); if(0 == registeredClass) { HandleWin32Error(); delete[] wstr; return CPUT_ERROR_WINDOW_CANNOT_REGISTER_APP; } mhWnd = CreateWindowEx( 0, wstr, wstr, WS_OVERLAPPEDWINDOW, windowParams.windowPositionX, windowParams.windowPositionY, windowParams.windowWidth, windowParams.windowHeight, NULL, NULL, mhInstance, NULL ); delete[] wstr; if (!mhWnd) { HandleWin32Error(); return CPUT_ERROR_CANNOT_GET_WINDOW_INSTANCE; } ShowWindow(mhWnd, SW_SHOWNORMAL); UpdateWindow(mhWnd); return CPUT_SUCCESS; }
CPUTResult CPUTWindowWin::Create(CPUT* cput, const cString WindowTitle, CPUTWindowCreationParams windowParams) { // If were here, we are sure that window is not created. // Take into notice this whole framework is not Thread-Safe. ASSERT( (windowParams.windowPositionX < GetSystemMetrics(SM_CXFULLSCREEN) && (windowParams.windowPositionX>=-1)), _L("You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams")); ASSERT( (windowParams.windowPositionY < GetSystemMetrics(SM_CYFULLSCREEN) && (windowParams.windowPositionY>=-1)), _L("You are attempting to create a window outside the desktop coordinates. Check your CPUTWindowCreationParams")); // Get the hInstance of this executable mhInst = GetModuleHandle(NULL); if( !mhInst ) { return CPUT_ERROR_CANNOT_GET_WINDOW_INSTANCE; } // Clear message queue MSG msg = { 0 }; while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } WNDCLASS Window; // Window class DEVMODE DispDevice; // Display Device settings DWORD Style; // Window style DWORD ExStyle; // Window extended style RECT WindowRect; // Window rectangle // Load icon HANDLE hIcon = LoadImage(mhInst, L"CPUT.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE ); // set up app title (if not specified) mAppTitle = WindowTitle; if(0==mAppTitle.compare(_L(""))) { mAppTitle = _L("CPUT Sample"); } // Register the Win32 class for this app WNDCLASS wc; if(TRUE == GetClassInfo(mhInst, mAppTitle.c_str(), &wc)) { // point to the existing one mhInst = wc.hInstance; } else { // register a new windows class ATOM classID; classID = MyRegisterClass(mhInst); if(0==classID) { HandleWin32Error(); return CPUT_ERROR_WINDOW_CANNOT_REGISTER_APP; } } // Perform Win32 instance initialization const int nCmdShow = SW_SHOWNORMAL; if (false == InitInstance(nCmdShow, windowParams.windowWidth, windowParams.windowHeight, windowParams.windowPositionX, windowParams.windowPositionY)) { return CPUT_ERROR_CANNOT_GET_WINDOW_INSTANCE; } // store the CPUT pointer mCPUT = (CPUT*) cput; return CPUT_SUCCESS; }