简体   繁体   中英

How do I initialize hwnd

I'm trying to make a 32 bit game for experience but I have run into a problem an error keeps popping up saying "Uninitialized variable hwnd used" I understand what it means but I don't know what to initialize hwnd as. I have tried setting hwnd to render_state.memory but it doesn't like that ether.

( if you try to compile these make sure to set thier configurations in property's to all configurations,and exclude from build to true on the fist one (the one just below). Also the.cpp file below is the one called render.cpp)

here is the code with the error:

#include <Windows.h>

struct Render_State
 {
int 
width, 
hight;

void* memory;
BITMAPINFO bitmap_info;

 };

Render_State render_state;

void render_backround()
{
if (WM_PAINT)
{
    PAINTSTRUCT ps;

    HWND hwnd;
    HDC hdc = BeginPaint(hwnd, &ps);

    unsigned int* pixel = (unsigned int*)render_state.memory;

    for (int y = 0; y < render_state.hight; y++)
       {
    for (int x = 0; x < render_state.width; x++)
        {
            *pixel++ = x * y + x;
        }
       }
    // render
    StretchDIBits(
    hdc,
    0, 
    0, 
    render_state.width,
    render_state.hight, 
    0, 
    0, 
    render_state.width, 
    render_state.hight,
    render_state.memory, 
    &render_state.bitmap_info,
    DIB_RGB_COLORS,SRCCOPY
     ); 
    {
    
    }

    EndPaint(hwnd, &ps);
}
}

here is the second.cpp file (this is the main program):

#include "renderer.cpp"
#include <Windows.h>



bool running = true;




LRESULT CALLBACK windows_callback(
HWND hwnd, 
UINT uMsg, 
WPARAM wParam, 
LPARAM lParam
)
{
LRESULT result = 0;
switch (uMsg)
{
case WM_CLOSE:
case WM_DESTROY:
{
    PostQuitMessage(0);
}
break;
render_backround();
case WM_SIZE:
{
    RECT rect;
    GetClientRect(hwnd, &rect);
   render_state.width = rect.right - rect.left;
   render_state.hight = rect.bottom - rect.top;
    int size = render_state.width*render_state.hight*sizeof(unsigned int);
    if(render_state.memory)VirtualFree(render_state.memory,0,MEM_RELEASE);     
    render_state.memory=VirtualAlloc(
    0,size,
    MEM_COMMIT|MEM_RESERVE,
    PAGE_READWRITE);
   render_state.bitmap_info.bmiHeader.biSize = 
   sizeof(render_state.bitmap_info.bmiHeader);
   render_state.bitmap_info.bmiHeader.biWidth =render_state.width;
   render_state.bitmap_info.bmiHeader.biHeight =render_state.hight;
   render_state.bitmap_info.bmiHeader.biPlanes = 1;
   render_state.bitmap_info.bmiHeader.biBitCount = 32;
   render_state.bitmap_info.bmiHeader.biCompression = BI_RGB;
}
break;

default:
{
    result = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return result;
}

  int WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPSTR lpCmdLine, 
  int nShowCmd
  )
{
//compile window
CHAR clsName[] = "test";
WNDCLASSA window_class = {};
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpszClassName = clsName;
window_class.lpfnWndProc = windows_callback;
//register clases
ATOM atom = RegisterClassA(&window_class);
if (0 == atom)
{
    DWORD err = GetLastError();
    return 1;
}

// create window
HWND window = CreateWindow(
   clsName,
   "game", 
    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    CW_USEDEFAULT, 
    CW_USEDEFAULT, 
    720, 
    360,
    0, 
    0,
    hInstance,
    0
    );
if (NULL == window)
{
    DWORD err = GetLastError();
    return 1;
}
MSG msg;

// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
}

all help is appreciated.

Based on the information you provide it's impossible to say for sure, but here is my best guess:

  1. Change the signature of your render_backround() to this: void render_backround(HWND hwnd)

  2. Remove the line HWND hwnd; from that function

  3. In your second.cpp file replace this line render_backround(); with this: render_backround(hwnd);

  4. As written, the render_backround(hwnd); is unreachable. You are missing a case WM_PAINT: line in front of it.

I think this is the way to go, because your function windows_callback is later on registered as a callback (as the naming suggests). That means Windows will be responsible for passing it sane parameters. Among those parameters there happens to be a HWND , just what you need. So, if the set up callback mechanism works at all, we can expect that this HWND instance inside of the callback will be the one to work with. So you only need to use that very same instance in your render_backround function, hence you have to pass it there rather than have it declare its own one (which leeds to your error).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM