我是一个正在努力学习的新手程序员。我正在尝试创建一个简单的视频游戏,因此我正在遵循 YouTube 上一个人的指南。
但是,我遇到了一个问题,即使我编译代码并创建
.exe
文件,当我运行该 .exe
文件时什么也没有发生。我尝试操纵代码来让某些事情发生,但什么也没发生。
我尝试更改
CreateWindowExW()
函数并操纵值以显示某些内容,但没有任何进展。没有语法错误,一切都编译干净,我可以制作可执行文件。但是,当我编译它时,没有任何效果。我假设我使用了错误的函数,但我不确定,因为 ChatGPT 告诉我我的代码应该可以工作。
#include <windows.h>
bool running = true;
void* buffer_memory;
int buffer_width;
int buffer_height;
BITMAPINFO buffer_bitmap_info;
// this controls the memory assigned and allows for good memory allocation
LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
LRESULT result =0;
switch (uMsg) {
case WM_CLOSE:
case WM_DESTROY:
running = false;
break;
case WM_SIZE: {
RECT rect;
GetClientRect(hwnd, &rect);
buffer_width = rect.right - rect.left;
buffer_height = rect.bottom - rect.top;
int buffer_size = buffer_width*buffer_height *sizeof(unsigned int);
if(buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
buffer_memory = VirtualAlloc(0, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
buffer_bitmap_info.bmiHeader.biSize = sizeof(buffer_bitmap_info.bmiHeader);
buffer_bitmap_info.bmiHeader.biWidth = buffer_width;
buffer_bitmap_info.bmiHeader.biHeight = buffer_height;
buffer_bitmap_info.bmiHeader.biPlanes = 1;
buffer_bitmap_info.bmiHeader.biBitCount = 32;
buffer_bitmap_info.bmiHeader.biCompression = BI_RGB;
}
break;
default: {
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return result;
}
// Creating a typedef function so I can compile without having to link to a library
typedef int (WINAPI* StretchDIBitsFunc)(HDC, int, int, int, int, int, int, int, int, const VOID*, const BITMAPINFO*, UINT, DWORD);
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//Create Window Class
WNDCLASS window_class ={};
window_class.style = CS_HREDRAW | CS_VREDRAW;
const wchar_t* WindowName = L"Game Window Class";
LPCWSTR GameName = L"Ping Pong";
//window_class.lpszClassName = GameName;
window_class.lpfnWndProc = window_callback;
// Register Class
RegisterClass(&window_class);
// Create Window
HWND window = CreateWindowExW(0,WindowName, GameName, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);
HDC hdc = GetDC(window);
StretchDIBitsFunc StretchDIBits = reinterpret_cast<StretchDIBitsFunc>(GetProcAddress(GetModuleHandle(NULL), "StretchDIBits"));
// Load StretchDIBits function dynamically(so I dont have to link it to a library and it compiles)
while(running) {
// Input
MSG message;
while(PeekMessage(&message, window, 0, 0, PM_REMOVE)){
TranslateMessage(&message);
DispatchMessage(&message);
}
// Simulate
unsigned int* pixel = (unsigned int*)buffer_memory;
for (int i = 0; i < buffer_height; i++) {
for (int z = 0; z < buffer_width; z++) {
*pixel++ = 0xff5500;
}
}
// Render
if(StretchDIBits){
StretchDIBits(hdc,0,0, buffer_width, buffer_height, 0, 0, buffer_width, buffer_height, buffer_memory, &buffer_bitmap_info, DIB_RGB_COLORS, SRCCOPY);
}
}
return 0;
}
我对发布的代码做了一些更改。尝试一下 -
前两点正如@HansPassant所说。
第三点是记忆
buffer_size
错了,应该是:
stride = ((((biWidth * biBitCount) + 31) & ~31) >> 3);
biSizeImage = abs(biHeight) * stride;
请参阅计算表面步幅了解更多信息。
StretchDIBits
。
#include <windows.h>
bool running = true;
void* buffer_memory;
int buffer_width;
int buffer_height;
BITMAPINFO buffer_bitmap_info;
// this controls the memory assigned and allows for good memory allocation
LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
LRESULT result = 0;
switch (uMsg) {
case WM_CLOSE:
case WM_DESTROY:
running = false;
break;
case WM_SIZE: {
RECT rect;
GetClientRect(hwnd, &rect);
buffer_width = rect.right - rect.left;
buffer_height = rect.bottom - rect.top;
//int buffer_size = buffer_width * buffer_height * sizeof(unsigned int); 3
int buffer_size = ((buffer_width * 32 + 31) / 32) * 4 * buffer_height;
if (buffer_memory) VirtualFree(buffer_memory, 0, MEM_RELEASE);
buffer_memory = VirtualAlloc(0, buffer_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
buffer_bitmap_info.bmiHeader.biSize = sizeof(buffer_bitmap_info.bmiHeader);
buffer_bitmap_info.bmiHeader.biWidth = buffer_width;
buffer_bitmap_info.bmiHeader.biHeight = buffer_height;
buffer_bitmap_info.bmiHeader.biPlanes = 1;
buffer_bitmap_info.bmiHeader.biBitCount = 32;
buffer_bitmap_info.bmiHeader.biCompression = BI_RGB;
}
break;
default: {
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return result;
}
// Creating a typedef function so I can compile without having to link to a library
typedef int (WINAPI* StretchDIBitsFunc)(HDC, int, int, int, int, int, int, int, int, const VOID*, const BITMAPINFO*, UINT, DWORD);
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//Create Window Class
WNDCLASS window_class = {};
window_class.style = CS_HREDRAW | CS_VREDRAW;
const wchar_t* WindowName = L"Game Window Class";
LPCWSTR GameName = L"Ping Pong";
window_class.lpszClassName = GameName;//1
window_class.lpfnWndProc = window_callback;
// Register Class
RegisterClass(&window_class);
// Create Window
//HWND window = CreateWindowExW(0, WindowName, GameName, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);2
HWND window = CreateWindowExW(0, GameName, WindowName, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);
HDC hdc = GetDC(window);
//StretchDIBitsFunc StretchDIBits = reinterpret_cast<StretchDIBitsFunc>(GetProcAddress(GetModuleHandle(NULL), "StretchDIBits"));
// Load StretchDIBits function dynamically(so I dont have to link it to a library and it compiles)
while (running) {
// Input
MSG message;
while (PeekMessage(&message, window, 0, 0, PM_REMOVE)) {
TranslateMessage(&message);
DispatchMessage(&message);
}
// Simulate
unsigned int* pixel = (unsigned int*)buffer_memory;
for (int i = 0; i < buffer_height; i++) {
for (int z = 0; z < buffer_width; z++) {
*pixel++ = 0xff5500;
}
}
// Render 4
//if (StretchDIBits) {
StretchDIBits(hdc, 0, 0, buffer_width, buffer_height, 0, 0, buffer_width, buffer_height, buffer_memory, &buffer_bitmap_info, DIB_RGB_COLORS, SRCCOPY);
//}
}
return 0;
}