I'm trying to build an OpenGL application that is responsive even while the main window is being resized or moved. The most logical solution that I have found is to create a child window and a Message Pump in separate thread which renders the OpenGL. It can resize itself in between frames as necessary. The primary message pump and window frame runs in the main process.
It works great to a point. The window can be moved, menus used and resized without affecting the frame rate of the child window. SwapBuffers() is where it all falls apart.
SwapBuffers() seems to be running in software mode when it is run in this manner. It no longer holds at 60 FPS to match my monitor's VSync, it jumps into the hundreds when the window is around 100x100 and drops to 20 FPS when maximized to 1920x1080. When running in a single thread, everything seems normal.
There were a few issues that I resolved. Like when messages need to pass between parent and child it stalls the entire application. Overriding WM_SETCURSOR and setting WS_EX_NOPARENTNOTIFY resolved those. It still occasionally stutters when I click.
I'm hoping that I'm just not doing something properly and that someone experienced with OpenGL can help me out. Something to do with my initialization or cleanup may be off since this interferes with other OpenGL applications running on my PC even after I close it.
Here is a simplified test case that exhibits the issues that I'm experiencing. It should compile in about any modern Visual Studio.
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <wchar.h>
#pragma comment(lib, "opengl32.lib")
typedef signed int s32;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef float f32;
typedef double f64;
bool run = true;
// Window procedure for the main application window
LRESULT CALLBACK AppWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_DESTROY && (GetWindowLong(hWnd, GWL_EXSTYLE) & WS_EX_APPWINDOW))
PostQuitMessage(0);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// Window procedure for the OpenGL rendering window
LRESULT CALLBACK RenderWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (msg == WM_SETCURSOR)
{
SetCursor(LoadCursor(NULL, IDC_CROSS));
return TRUE;
}
if (msg == WM_SIZE)
glViewport(0, 0, LOWORD(lParam)-2, HIWORD(lParam)-2);
return AppWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI ThreadMain(HWND parent)
{
HINSTANCE hInstance = GetModuleHandle(0);
// Depending on if this is running as a child or a overlap window, set up the window styles
UINT ClassStyle, Style, ExStyle;
if (parent)
{
ClassStyle = 0;
Style = WS_CHILD;
ExStyle = WS_EX_NOPARENTNOTIFY;
} else {
ClassStyle = 0 | CS_VREDRAW | CS_HREDRAW;
Style = WS_OVERLAPPEDWINDOW;
ExStyle = WS_EX_APPWINDOW;
}
// Create the child window class
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = ClassStyle;
wc.hInstance = hInstance;
wc.lpfnWndProc = RenderWindowProc;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.lpszClassName = L"OGLChild";
ATOM ClassAtom = RegisterClassEx(&wc);
// Create the child window
RECT r = {0, 0, 640, 480};
if (parent)
GetClientRect(parent, &r);
HWND WindowHandle = CreateWindowExW(ExStyle, (LPCTSTR)MAKELONG(ClassAtom, 0), 0, Style,
0, 0, r.right, r.bottom, parent, 0, hInstance, 0);
// Initialize OpenGL render context
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
HDC DeviceContext = GetDC(WindowHandle);
int format = ChoosePixelFormat(DeviceContext, &pfd);
SetPixelFormat(DeviceContext, format, &pfd);
HGLRC RenderContext = wglCreateContext(DeviceContext);
wglMakeCurrent(DeviceContext, RenderContext);
ShowWindow(WindowHandle, SW_SHOW);
GetClientRect(WindowHandle, &r);
glViewport(0, 0, r.right, r.bottom);
// Set up an accurate clock
u64 start, now, last, frequency;
QueryPerformanceFrequency((LARGE_INTEGER*)&frequency);
QueryPerformanceCounter((LARGE_INTEGER*)&now);
start = last = now;
u32 frames = 0; // total frames this second
f64 nextFrameCount = 0; // next FPS update
f32 left = 0; // line position
MSG msg;
while (run)
{
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT || msg.message == WM_DESTROY)
run = false;
}
// Update the clock
QueryPerformanceCounter((LARGE_INTEGER*)&now);
f64 clock = (f64)(now - start) / frequency;
f64 delta = (f64)(now - last) / frequency;
last = now;
// Render a line moving
glOrtho(0, 640, 480, 0, -1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(1.0f, 1.0f, 0.0f);
left += (f32)(delta * 320.0f);
if (left > 640.0f)
left = 0;
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(left, 480.0f);
glEnd();
SwapBuffers(DeviceContext);
// Resize as necessary
if (parent)
{
RECT pr, cr;
GetClientRect(parent, &pr);
GetClientRect(WindowHandle, &cr);
if (pr.right != cr.right || pr.bottom != cr.bottom)
MoveWindow(WindowHandle, 0, 0, pr.right, pr.bottom, FALSE);
}
// Update FPS counter
frames++;
if (clock > nextFrameCount)
{
WCHAR title[16] = {0};
_snwprintf_s(title, 16, 16, L"FPS: %u", frames);
SetWindowText(parent ? parent : WindowHandle, title);
nextFrameCount = clock + 1;
frames = 0;
}
Sleep(1);
}
// Cleanup OpenGL context
wglDeleteContext(RenderContext);
wglMakeCurrent(0,0);
ReleaseDC(WindowHandle, DeviceContext);
DestroyWindow(WindowHandle);
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
int result = MessageBox(0, L"Would you like to run in threaded child mode?",
L"Threaded OpenGL Demo", MB_YESNOCANCEL | MB_ICONQUESTION);
if (result == IDNO)
return ThreadMain(0);
else if (result != IDYES)
return 0;
// Create the parent window class
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.hInstance = hInstance;
wc.lpfnWndProc = (WNDPROC)AppWindowProc;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.lpszClassName = L"OGLFrame";
ATOM ClassAtom = RegisterClassEx(&wc);
// Create the parent window
HWND WindowHandle = CreateWindowExW(WS_EX_APPWINDOW, (LPCTSTR)MAKELONG(ClassAtom, 0),
0, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
0, 0, 640, 480, 0, 0, hInstance, 0);
ShowWindow(WindowHandle, SW_SHOW);
// Start the child thread
HANDLE thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadMain, (LPVOID)WindowHandle, 0, 0);
MSG msg;
while (run)
{
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
run = false;
}
Sleep(100);
}
DestroyWindow(WindowHandle);
// Wait for the child thread to finish
WaitForSingleObject(thread, INFINITE);
ExitProcess(0);
return 0;
}
I've been running this on a NVIDIA GeForce 8800 GTX, but I'd hope that any solution will work equally on any modern card.
I have tried other methods, such as a thread rendering into a window in the main process but during resizing I've gotten artifacting and even a couple blue screens. My application will be cross platform so DirectX isn't an option.
The issue turned out to be that prolonged debugging of OpenGL applications in Visual Studio can cause OpenGL to start failing to create contexts. Since I didn't trap for any errors, I never realized that it was running without hardware acceleration. It required a full reboot to recover and now works fine.
Also, replacing the pump WinMain with this fixes any issues with resizing:
MSG msg;
while (GetMessage(&msg, 0, 0, 0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
run = false;
Related
I'm working with the win32 API, and am using it to make a window. This window works, but when I open it, the cursor is a loading cursor, and every time I bring my cursor to the edge to resize it, the cursor gets 'stuck' as that resizing cursor, it doesn't go back to normal. Here's a video to explain what I'm talking about:
Here's the reproducible example (compile with g++ reproducible_example.cpp -mwindows -O3 -o reproducible_example.exe):
#undef UNICODE
#undef _UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
bool isRunning = true;
void *buffer; // buffer memory
BITMAPINFO bmi; // bit map information, needed for rendering
int width, height; // main window's width and height
LRESULT __stdcall WindowProc(HWND, UINT, WPARAM, LPARAM);
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR pCmdLine, int nCmdShow) {
LPCSTR CLASS_NAME = "Class Name";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowExA(0, // Optional window styles.
"Class Name", // Window class
"Window", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
return 0;
ShowWindow(hwnd, nCmdShow);
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
// Run the message loop.
HDC hdc = GetDC(hwnd);
while (isRunning) {
MSG msg;
if (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
StretchDIBits(hdc, 0, 0, width, height, 0, 0, width, height, buffer, &bmi,
DIB_RGB_COLORS, SRCCOPY);
}
ReleaseDC(hwnd, hdc);
return 0;
}
LRESULT __stdcall WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
isRunning = false;
return 0;
case WM_SIZE: {
// Calculate window height and width
width = LOWORD(lParam);
height = HIWORD(lParam);
if (buffer) // If memory already exists
// free it
VirtualFree(buffer, 0, MEM_RELEASE);
// Allocate buffer memory
buffer = VirtualAlloc(0, width * height * sizeof(unsigned int),
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Now, I know this isn't some weird windows bug and is definitely something with my code, because it doesn't happen in other apps which I open, and it also didn't happen when I made an equivalent window with SFML (probably because the windows api and SFML are entirely different things). Code for that window:
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(1280, 720), "Window");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
}
window.clear();
window.display();
}
}
I want the first window to act like the second window, but the cursor is the one discrepancy I can find. I've tried googling this, but to no avail. I followed a youtube tutorial series for the window I'm having problems with. Also, on downloading the tutorial's source code and removing some code which makes it full screen and hides the cursor, it has the same problem. How do I fix this? Thank you in advance.
Set wc.hCursor to something other than null. If it's null, the operating system will leave the cursor alone when it enters your window, and you're supposed to set it to the cursor you want.
Most likely you want the boring old arrow cursor. You can get that cursor by calling LoadCursor(NULL, IDC_ARROW).
I'm trying to learn how to create a window in win 32. This is as far as I have got with it. The problem I'm facing is I'm unable to create a window that can be resized by the user. I'm hoping that someone could help me solve this newbie problem.
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
std::string msg = "";
UINT width = 0;
UINT height = 0;
switch(uMsg)
{
case WM_SIZE:
width = LOWORD(lParam);
height = HIWORD(lParam);
if(width<(height*600)/800) SetWindowPos(hWnd, NULL, 0, 0, width, height, SWP_NOMOVE|SWPNOZORDER);
return true;
case WM_SIZING:
width = LOWORD(lParam);
height = HIWORD(lParam);
if(width<(height*600)/800) SetWindowPos(hWnd, NULL, 0, 0, width, height, SWP_NOMOVE|SWPNOZORDER);
return true;
case WM_DESTROY:
PostQuitMessage(0);
return true;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, INT nCmdShow)
{
WNDCLASSEX wnd = {0};
wnd.lpszClassName = "WindowLearn";
wnd.hInstance = hInstance;
wnd.lpfnWndProc = windowProc;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wnd);
HWND hWnd = CreateWindowEx(NULL, "WindowLearn", "WindowLearnChild", WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg = {0};
float pTime = 0.0f;
BOOL result;
while(msg.message != WM_QUIT)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Window gets created OK, but with this when I try to resize the window the window gets stuck to the mouse.
It appears that you want to to idle processing, meaning having some tasks done for your directX application when no event are present in the event loop.
There are 2 different ways of doing that:
dedicate a separate thread for background processing. It adds the complexity of multiprocessing, but allows to do all the processing as a single piece of code, and let the system affect time slices to the event loop and the background processing.
use a modified event loop that does chunks of background processing when no event are present. PeekMessage is the key here:
...
MSG msg = {0};
for (;;)
{
if (PeekMessage(&msg, NULL, 0, 0, 0, 0) {
if (! GetMessage(&msg, NULL, 0, 0, 0)) break; // msg is WM_QUIT
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
// do some idle processing for a short time
// no event will be processing during that
...
}
}
The nice point is that you do not need any multi-threading, but you have to explicitely split you backgroung processing in short time slices.
Having such a simple DirectX application:
#include <d3dx9.h>
#include <tchar.h>
#include <chrono>
#include "Utils.h"
//#pragma comment (lib, "d3d9.lib")
//#pragma comment (lib, "d3dx9.lib")
using namespace std::chrono_literals;
using namespace std::chrono;
const wchar_t g_szClassName[] = _T("myWindowClass");
// we use a fixed timestep of 1 / (60 fps) = 16 milliseconds
//constexpr std::chrono::nanoseconds timestep(16ms);
constexpr std::chrono::nanoseconds timestep(1000ms);
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
wchar_t msgbuf[100];
int loop_cnt = 0;
bool handle_events() {
// poll for events
return false; // true if the user wants to quit the game
}
void update(long long lag) {
// update game logic here
swprintf_s(msgbuf, _T("update - loop_cnt: %d\n"), loop_cnt++);
OutputDebugString(msgbuf);
}
void render() {
// render stuff here
}
VOID OnPaint(HDC hdc) {
}
// ------------ D3D ------------
IDirect3D9* pD3D;
IDirect3DDevice9* pDev;
IDirect3DVertexBuffer9* pVB;
const DWORD OURVERT_FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;
struct OurVertex {
float x, y, z; // pozycja
float rhw; // komponent rhw
D3DCOLOR color; // kolor
};
// -----------------------------
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
using clock = std::chrono::high_resolution_clock;
WNDCLASSEX wc;
HWND hWnd;
MSG Msg = {0};
std::chrono::nanoseconds lag(0ns);
auto start_time = clock::now();
bool quit_game = false;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
_T("The title of my window"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
480, // initial x size
640, // initial y size
NULL,
NULL,
hInstance,
NULL
);
OutputDebugString(_T("--> WinMain <-- \n"));
if (hWnd == NULL) {
MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// ------------ D3D ------------
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = true;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
OurVertex verts[] = {
{ 20.0f, 20.0f, 0.5f, 1.0f, 0xffff0000, },
{ 100.0f, 20.0f, 0.5f, 1.0f, 0xff00ff00, },
{ 20.0f, 100.0f, 0.5f, 1.0f, 0xff00ff55, },
{ 100.0f, 100.0f, 0.5f, 1.0f, 0xff0000ff},
};
pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pDev);
HRESULT hr = pDev->CreateVertexBuffer(4*sizeof(OurVertex), D3DUSAGE_DYNAMIC, OURVERT_FVF, D3DPOOL_DEFAULT, &pVB, 0);
check_HRESULT(_T("CreateVertexBuffer"), hr);
void* data;
pVB->Lock(0, 4*sizeof(OurVertex), &data, D3DLOCK_DISCARD);
memcpy(data, (void*)verts, sizeof(verts));
pVB->Unlock();
// -----------------------------
// Step 3: The Message Loop
while (TRUE) {
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if (Msg.message == WM_QUIT)
break;
// Run game logic
auto current_time = clock::now();
auto frame_time = current_time - start_time;
start_time = current_time;
lag += duration_cast<nanoseconds>(frame_time);
// update game logic as lag permits
while (lag >= timestep) {
lag -= timestep;
pDev->Clear(0, 0, D3DCLEAR_TARGET, 0xff000000, 1, 0);
pDev->BeginScene();
pDev->SetFVF(OURVERT_FVF);
pDev->SetStreamSource(0, pVB, 0, sizeof(OurVertex));
pDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
pDev->EndScene();
pDev->Present(0, 0, 0, 0);
update(lag.count()); // update at a fixed rate each time
}
}
OutputDebugString(_T("--> AFTER MSG LOOP END <-- \n"));
return Msg.wParam;
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
When i run debug (without any brakpoints defined) i get the output:
--> WinMain <--
update - loop_cnt: 0
update - loop_cnt: 1
update - loop_cnt: 2
update - loop_cnt: 3
update - loop_cnt: 4
update - loop_cnt: 5
...
The program successfully displays a colored rectangle as expected. But, something is going wrong here, because when I click the standard "X" close button in the right upper corner of the app window, the window closes but the program seems not to end properly. I still receive update - loop_cnt: # messages in the Output window in my Visual Studio, and there is no --> AFTER MSG LOOP END <-- message suggesting proper program termination.
What's more, when I try to run debug again, I get an error:
LINK : fatal error LNK1168: cannot open ...Game_Loop_Windows_1.exe for writing
...
MSB6006: "link.exe" exited with code 1168.
1>Done building project "Game_Loop_Windows_1.vcxproj" -- FAILED
What can be wrong with the code?
PS. I'm using Microsoft Visual Studio Community 2019 on Windows 10.
The message loop likely never terminates.
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
if (Msg.message == WM_QUIT)
break;
You are handling all messages, but only check that last one whether it was a WM_QUIT message. Instead, you would need to check all messages and properly exit the loop, something like this:
bool running{true};
while (running) {
while (running && PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
running = Msg.message != WM_QUIT;
}
// Not strictly required; you can run another render loop and have
// the outermost loop exit when running is false
if (!running)
break;
The link error simply means that the process is still running. An executable image that's mapped into a process cannot be written to. That's what the linker is complaining about.
Desktop Window Manager Uses A Ton Of CPU When Drawing DX9 Window.
I'm Not Sure Why It Is Using So Much CPU
https://imgur.com/a/Bz7AVro
LRESULT CALLBACK WinProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam){
Sleep(12);
switch (Message){
case WM_PAINT:
Render();
break;
case WM_CREATE:
DwmExtendFrameIntoClientArea(hWnd, &Margin);
break;
case WM_DESTROY:
PostQuitMessage(1);
return 0;
default:
return DefWindowProc(hWnd, Message, wParam, lParam);
break;
}
return 0;
}
ref class CMAIN {
public:
void StartIt() { Main(); }
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hSecInstance, LPSTR nCmdLine, INT nCmdShow){
Thread^ main;
CMAIN^ cMain = gcnew CMAIN();
main = gcnew Thread(gcnew ThreadStart(cMain, &CMAIN::StartIt));
main->Name = "main";
main->Start();
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SetWindowToTarget, 0, 0, 0);
WNDCLASSEX wClass;
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = NULL;
wClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));
wClass.hCursor = LoadCursor(0, IDC_ARROW);
wClass.hIcon = LoadIcon(0, IDI_APPLICATION);
wClass.hIconSm = LoadIcon(0, IDI_APPLICATION);
wClass.hInstance = hInstance;
wClass.lpfnWndProc = WinProc;
wClass.lpszClassName = lWindowName;
wClass.lpszMenuName = lWindowName;
wClass.style = CS_VREDRAW | CS_HREDRAW;
if(!RegisterClassEx(&wClass))
exit(1);
tWnd = FindWindow(0, tWindowName);
if (tWnd){
GetWindowRect(tWnd, &tSize);
Width = tSize.right - tSize.left;
Height = tSize.bottom - tSize.top;
hWnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED, lWindowName, lWindowName, WS_POPUP, 1, 1, Width, Height, 0, 0, 0, 0);
SetLayeredWindowAttributes(hWnd, 0, 1.0f, LWA_ALPHA);
SetLayeredWindowAttributes(hWnd, 0, RGB(0, 0, 0), LWA_COLORKEY);
ShowWindow( hWnd, SW_SHOW);
}
DirectXInit(hWnd);
while (!directXExit){
Sleep(12);
if(PeekMessage(&Message, hWnd, 0, 0, PM_REMOVE)){
DispatchMessage(&Message);
TranslateMessage(&Message);
}
}
return 0;
}
void SetWindowToTarget(){
while(true){
tWnd = FindWindow(0, tWindowName);
if (tWnd){
GetWindowRect(tWnd, &tSize);
Width = tSize.right - tSize.left;
Height = tSize.bottom - tSize.top;
DWORD dwStyle = GetWindowLong(tWnd, GWL_STYLE);
if(dwStyle & WS_BORDER){
tSize.top += 23;
Height -= 23;
}
MoveWindow(hWnd, tSize.left, tSize.top, Width, Height, true);
}
Sleep(1500);
}
}
It would be useful to get some more information around what you're trying to do, and it's also hard to see exactly what could be going on with the WM_PAINT handler in your code (since you call a function called Render but don't provide that code). But at first glance I think there's at least one thing that's going to force high CPU usage, and that's your message loop. You're basically spinning in that loop due to calling PeekMessage, with a call to Sleep(12) which will basically give you a ~80fps update on that window; since you're using the DWM composition code (DwmExtendFrameIntoClient) my guess is that you're triggering a 80Hz update in DWM because of that. If you're rendering something in realtime in that loop, then that's all to be expected and not avoidable (unless you put your rendering loop on a separate thread). But if you're just responding to WM_PAINT messages then you'd be better served using a message pump that's using GetMessage rather than PeekMessage, since GetMessage will suspend the thread when the Windows message queue is empty, and you'll only trigger CPU work when something needs to be updated.
We are learning about windows raw touch events in class using Visual Studios 2012 in C++. I got my demo working and it does exactly what it is supposed to, which is drawing circles beneath your fingertips to detect that a touch event has been raised. But after a certain time has passed and if you are still pressing on the screen with the circles drawn the screen turns blue! The circles still show but they are blue as well with black contours and I can still move them around. I showed the professor and he can't seem to figure it out which is why I came here. Can anyone take a look at my code to let me know what seems to be the cause of it?
// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
#ifndef WINVER // Specifies that the minimum required platform is Windows 7.
#define WINVER 0x0601 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows 7.
#define _WIN32_WINNT 0x0601 // Change this to the appropriate value to target other versions of Windows.
#endif
#include <windows.h> // for windows touch
#include <windowsx.h> // included for point conversion
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include "wtypes.h"
#include <iostream>
using namespace std;
// The main window class name.
TCHAR szWindowClass[] = _T("win32app");
// The string that appears in the application's title bar.
TCHAR szTitle[] = _T("Hello World!");
//Instancing the handler
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//Maximum ammount of touches allowed
#define MAXPOINTS 10
// You will use this array to track touch points
int points[MAXPOINTS][2];
// You will use this array to switch the color / track ids
int idLookup[MAXPOINTS];
// You can make the touch points larger
// by changing this radius value
static int radius = 30;
// There should be at least as many colors
// as there can be touch points so that you
// can have different colors for each point
COLORREF colors[] = { RGB(153,255,51),
RGB(153,0,0),
RGB(0,153,0),
RGB(255,255,0),
RGB(255,51,204),
RGB(0,0,0),
RGB(0,153,0),
RGB(153, 255, 255),
RGB(153,153,255),
RGB(0,51,153)
};
int wmId, wmEvent, i, x, y, index;
UINT cInputs;
PTOUCHINPUT pInputs;
POINT ptInput;
// This function is used to return an index given an ID
int GetContactIndex(int dwID){
for (int i=0; i < MAXPOINTS; i++){
if (idLookup[i] == -1){
idLookup[i] = dwID;
return i;
}else{
if (idLookup[i] == dwID){
return i;
}
}
}
// Out of contacts
return -1;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
int width = 0, height = 0; // Screen resolution
GetScreenResolution(width, height);
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
hInst = hInstance; // Store instance handle in our global variable
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application does not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd) {
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
// register the window for touch instead of gestures
RegisterTouchWindow(hWnd, 0);
// the following code initializes the points
for (int i=0; i< MAXPOINTS; i++){
points[i][0] = -1;
points[i][1] = -1;
idLookup[i] = -1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes touch messages for the main window.
//
// WM_TOUCH - handles WM_TOUCH messages in the application
// WM_DESTROY - post a quit message and return
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// For double buffering
static HDC memDC = 0;
static HBITMAP hMemBmp = 0;
HBITMAP hOldBmp = 0;
//For drawing / fills
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_TOUCH:
//LOWORD(wParam) = number of touch points in this message
//HIWORD(wParam) = reserved for future use
//lParam = handle for use with GetTouchInputInfo
cInputs = LOWORD(wParam);
pInputs = new TOUCHINPUT[ cInputs ];
if(pInputs)
{
if( GetTouchInputInfo((HTOUCHINPUT)lParam, cInputs, pInputs, sizeof(TOUCHINPUT)) )
{
for (int i=0; i < static_cast<INT>(cInputs); i++)
{
TOUCHINPUT ti = pInputs[i];
index = GetContactIndex(ti.dwID);
if(ti.dwID != 0 && index < MAXPOINTS )
{
//get screen corrdinates of touch
ptInput.x = TOUCH_COORD_TO_PIXEL(ti.x);
ptInput.y = TOUCH_COORD_TO_PIXEL(ti.y);
//get coordinates relative to the top left of the application window
ScreenToClient(hWnd, &ptInput);
if(ti.dwFlags & TOUCHEVENTF_UP)
{
points[index][0] = -1;
points[index][1] = -1;
}
else
{
points[index][0] = ptInput.x;
points[index][1] = ptInput.y;
}
}
}
}
CloseTouchInputHandle((HTOUCHINPUT)lParam);
delete [] pInputs;
}
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT client;
GetClientRect(hWnd, &client);
//START DOUBLE BUFFERING
if (!memDC)
{
memDC = CreateCompatibleDC(hdc);
}
hMemBmp = CreateCompatibleBitmap(hdc, client.right, client.bottom);
hOldBmp = (HBITMAP)SelectObject(memDC, hMemBmp);
FillRect(memDC, &client, CreateSolidBrush(RGB(255,255,255)));
//Draw Touched Points
for (i=0; i < MAXPOINTS; i++)
{
SelectObject( memDC, CreateSolidBrush(colors[i]));
x = points[i][0];
y = points[i][1];
if (x >0 && y>0)
{
Ellipse(memDC, x - radius, y - radius, x + radius, y + radius);
}
}
BitBlt(hdc, 0,0, client.right, client.bottom, memDC, 0,0, SRCCOPY);
EndPaint(hWnd, &ps);
ReleaseDC(hWnd, hdc);
//DeleteObject(hMemBmp);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
I see the problem now. You have a GDI resource leak.
In two places you call CreateSolidBrush but you never delete the brushes you created. You do it here
FillRect(memDC, &client, CreateSolidBrush(RGB(255,255,255)));
and here
SelectObject( memDC, CreateSolidBrush(colors[i]));
Really, you should assign the result of eachCreateSolidBrush function to a HBRUSH and then call DeleteObject on it when you've finished with it.
You also need to release your bitmap: DeleteObject(hMemBmp) which you have commented out and also remove the call to ReleaseDC as I said in the comments.
Generally, you should keep a careful track of all of the GDI objects you have created and make sure you delete them when you've finished with them.