Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:53:42+00:00 2026-05-29T08:53:42+00:00

I’ve been making a Window class for a game, and I’m having trouble with

  • 0

I’ve been making a Window class for a game, and I’m having trouble with the message pump.

I pull events off the Windows-provided message queue and send them to the windows they pertain to. Here’s the function that Translates and Dispatches.

From what I remember of Win32 programming, Translating and Dispatching a message calls the specified WindowProc with the message’s contents for parameters. So here’s the WindowProc I specified…

currWin, currhwnd and winMap are defined as variables local to Window.cpp, at the top…

Anyway, calling distributeSystemMessages() seems to cause an infinite loop.

IMPORTANT NOTE: THE GAME LOOP IS NOT INSIDE THE MESSAGE HANDLING LOOP, AND NEITHER IS THE MESSAGE HANDLING CODE. THE MESSAGE HANDLING LOOP SHOULD EMPTY THE MESSAGE QUEUE ONCE PER FRAME, SENDING EACH MESSAGE TO THE WINDOW IT PERTAINS TO.

Here’s Window.h…

#ifndef WINDOW_H_INCLUDED
#define WINDOW_H_INCLUDED

#include "SystemMessage.h"
#include <queue>
#include <windows.h>
using namespace std;

///THIS FUNCTION MUST BE CALLED TO GET MESSAGES INTO WINDOW QUEUES
void distributeSystemMessages();

class Window
{
protected:
    HDC hDC;
    HWND hWnd;
    HINSTANCE hInst;
    HGLRC hRC;

public:
    queue<SystemMessage> messages;

    Window(unsigned int width, unsigned int height, const char* name, unsigned int colorBits = 24, unsigned int depthBits = 24, unsigned int stencilBits = 0);
    ~Window();

    void makeContextCurrent();
    void swapBuffers();

    unsigned int getHeight();
    unsigned int getWidth();

    int getMouseX();
    int getMouseY();

    void setSize(unsigned int width, unsigned int height);
};

#endif // WINDOW_H_INCLUDED

Here’s Window.cpp…

#include "Window.h"
#include <map>
#include <cstdio>
#include <GLee.h>
#include <GL/gl.h>
#include <GL/glu.h>
using namespace std;

HWND currhwnd;
Window* currWin;
map<HWND, Window*> winMap = map<HWND, Window*>();

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    if(currhwnd != hwnd)
    {
        map<HWND, Window*>::iterator i = winMap.find(hwnd);
        if(i != winMap.end())
        {
            currWin = (*i).second;
            currhwnd = hwnd;
        }
        else return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    SystemMessage msg(hwnd, uMsg, wParam, lParam);

    currWin->messages.push(msg);

    return 0;
}

Window::Window(unsigned int width, unsigned int height, const char* name, unsigned int colorBits, unsigned int depthBits, unsigned int stencilBits)
{
    //TODO: ADD TIME FUNCTIONS TO A TIMER CLASS
    //QueryPerformanceCounter(&startTime);
    //lastTime = startTime;
    messages = queue<SystemMessage>();


    hInst = GetModuleHandle(NULL);

    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hInst;
    wincl.lpszClassName = "Squirrel Engine Window";
    wincl.lpfnWndProc = MainWndProc;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
    {
        printf("Could not register window class.\n");
        return;
    }

    /* The class is registered, let's create the program*/
    hWnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           "Squirrel Engine Window",/* Classname */
           name,       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends after on the screen */
           10,                 /* The programs width */
           10,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hInst,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    RECT rcWindow;
    RECT rcClient;

    GetWindowRect(hWnd, &rcWindow);
    GetClientRect(hWnd, &rcClient);

    POINT ptDiff;
    ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
    ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;

    MoveWindow(hWnd,rcWindow.left, rcWindow.top, width + ptDiff.x, height + ptDiff.y, TRUE);

    ShowWindow (hWnd, SW_SHOW);

    hDC = GetDC( hWnd );

    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory( &pfd, sizeof( pfd ) );
    pfd.nSize = sizeof( pfd );
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
                  PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = colorBits;
    pfd.cDepthBits = depthBits;
    pfd.cStencilBits = stencilBits;
    pfd.iLayerType = PFD_MAIN_PLANE;
    int iFormat = ChoosePixelFormat( hDC, &pfd );
    SetPixelFormat( hDC, iFormat, &pfd );

    hRC = wglCreateContext(hDC);
    hDC = hDC;

    //makeContextCurrent();

    winMap[hWnd] = this;
    //TODO: Find out what this function does
    wglSwapIntervalEXT(0);
}

Window::~Window()
{
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);
    winMap.erase(hWnd);
    PostQuitMessage(0);
}

unsigned int Window::getWidth()
{
    RECT rcClient;
    GetClientRect(hWnd, &rcClient);
    return rcClient.right;
}
unsigned int Window::getHeight()
{
    RECT rcClient;
    GetClientRect(hWnd, &rcClient);
    return rcClient.bottom;
}

void Window::setSize(unsigned int width, unsigned int height)
{
    RECT rcWindow;
    RECT rcClient;

    GetWindowRect(hWnd, &rcWindow);
    GetClientRect(hWnd, &rcClient);

    POINT ptDiff;
    ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
    ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;

    MoveWindow(hWnd,rcWindow.left, rcWindow.top, width + ptDiff.x, height + ptDiff.y, TRUE);
}

void Window::makeContextCurrent()
{
    wglMakeCurrent( hDC, hRC );
}

void Window::swapBuffers()
{
    SwapBuffers( hDC );
}

int Window::getMouseX()
{
    POINT p;
    GetCursorPos(&p);

    RECT rcWindow;
    RECT rcClient;

    GetWindowRect(hWnd, &rcWindow);
    GetClientRect(hWnd, &rcClient);

    POINT ptDiff;
    ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
    ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;


    return p.x - (rcWindow.left + ptDiff.x);
}
int Window::getMouseY()
{
    POINT p;
    GetCursorPos(&p);

    RECT rcWindow;
    RECT rcClient;

    GetWindowRect(hWnd, &rcWindow);
    GetClientRect(hWnd, &rcClient);

    POINT ptDiff;
    ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
    ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;

    return p.y - (rcWindow.top + ptDiff.y);
}

void distributeSystemMessages()
{
    MSG messages;
    while(PeekMessage (&messages, NULL, 0, 0, PM_REMOVE))
    {
        printf("MessageLoop\n");
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:53:42+00:00Added an answer on May 29, 2026 at 8:53 am

    Typically, you use an object architecture. Windows provides access to space local to each hWnd that is explicitly reserved for your use for basically this exact purpose. You don’t need any global variables to make this work.

    class Window {
        // Can be virtual if necessary
        LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
            // Process the message here
        }
        static LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
            if (Window* ptr = reinterpret_cast<Window*>(GetWindowLongPtr(hwnd, GWLP_USERDATA))) {
                return ptr->WindowProc(hwnd, uMsg, wParam, lParam);
            }
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        HWND hwnd;
    public:
        Window() {
             hwnd = CreateWindowEx(....);
             SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
             ShowWindow(...); // update the ptr
        }
        void ProcessMessages() {
            MSG messages;
            while(PeekMessage (&messages, hwnd, 0, 0, PM_REMOVE))
            {
                printf("MessageLoop\n");
                TranslateMessage(&messages);
                DispatchMessage(&messages);
            }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.