- Simplify OpenGL integration test structure - Enable CTest registration for OpenGL tests - Refactor test fixtures and device enumeration - Minor code cleanup and improvements
163 lines
4.7 KiB
C++
163 lines
4.7 KiB
C++
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
|
|
#include <glad/glad.h>
|
|
#include <windows.h>
|
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
|
|
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
|
|
#include "XCEngine/Debug/Logger.h"
|
|
#include "XCEngine/Debug/ConsoleLogSink.h"
|
|
#include "XCEngine/Containers/String.h"
|
|
|
|
#pragma comment(lib, "opengl32.lib")
|
|
|
|
using namespace XCEngine::RHI;
|
|
using namespace XCEngine::Debug;
|
|
using namespace XCEngine::Containers;
|
|
|
|
static const int gWidth = 1280;
|
|
static const int gHeight = 720;
|
|
|
|
void Log(const char* format, ...) {
|
|
char buffer[1024];
|
|
va_list args;
|
|
va_start(args, format);
|
|
vsnprintf(buffer, sizeof(buffer), format, args);
|
|
va_end(args);
|
|
Logger::Get().Debug(LogCategory::Rendering, String(buffer));
|
|
}
|
|
|
|
void SaveScreenshotPPM(const char* filename, int width, int height) {
|
|
unsigned char* pixels = (unsigned char*)malloc(width * height * 3);
|
|
if (!pixels) {
|
|
Log("[ERROR] Failed to allocate pixel buffer");
|
|
return;
|
|
}
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
|
|
|
|
FILE* f = fopen(filename, "wb");
|
|
if (!f) {
|
|
Log("[ERROR] Failed to open file for screenshot: %s", filename);
|
|
free(pixels);
|
|
return;
|
|
}
|
|
|
|
fprintf(f, "P6\n%d %d\n255\n", width, height);
|
|
|
|
unsigned char* row = (unsigned char*)malloc(width * 3);
|
|
for (int y = height - 1; y >= 0; y--) {
|
|
memcpy(row, pixels + y * width * 3, width * 3);
|
|
fwrite(row, 1, width * 3, f);
|
|
}
|
|
|
|
free(row);
|
|
free(pixels);
|
|
fclose(f);
|
|
|
|
Log("[INFO] Screenshot saved to %s", filename);
|
|
}
|
|
|
|
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
switch (msg) {
|
|
case WM_CLOSE:
|
|
PostQuitMessage(0);
|
|
break;
|
|
}
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
}
|
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
|
|
Logger::Get().Initialize();
|
|
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
|
|
Logger::Get().SetMinimumLevel(LogLevel::Debug);
|
|
|
|
Log("[INFO] OpenGL Integration Test Starting");
|
|
|
|
WNDCLASSEXW wc = {};
|
|
wc.cbSize = sizeof(WNDCLASSEXW);
|
|
wc.style = CS_HREDRAW | CS_VREDRAW;
|
|
wc.lpfnWndProc = WindowProc;
|
|
wc.hInstance = hInstance;
|
|
wc.lpszClassName = L"XCEngine_OpenGL_Test";
|
|
|
|
if (!RegisterClassExW(&wc)) {
|
|
Log("[ERROR] Failed to register window class");
|
|
return -1;
|
|
}
|
|
|
|
RECT rect = { 0, 0, gWidth, gHeight };
|
|
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
|
|
|
|
HWND hwnd = CreateWindowExW(
|
|
0,
|
|
L"XCEngine_OpenGL_Test",
|
|
L"OpenGL Integration Test",
|
|
WS_OVERLAPPEDWINDOW,
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
|
rect.right - rect.left, rect.bottom - rect.top,
|
|
NULL, NULL, hInstance, NULL
|
|
);
|
|
|
|
RECT clientRect;
|
|
GetClientRect(hwnd, &clientRect);
|
|
fprintf(stderr, "[minimal] Window client area: %ldx%ld\n", clientRect.right, clientRect.bottom);
|
|
|
|
if (!hwnd) {
|
|
Log("[ERROR] Failed to create window");
|
|
return -1;
|
|
}
|
|
|
|
OpenGLDevice device;
|
|
fprintf(stderr, "[minimal] About to call InitializeWithExistingWindow\n");
|
|
if (!device.InitializeWithExistingWindow(hwnd)) {
|
|
fprintf(stderr, "[minimal] InitializeWithExistingWindow returned false\n");
|
|
Log("[ERROR] Failed to initialize OpenGL device");
|
|
return -1;
|
|
}
|
|
fprintf(stderr, "[minimal] InitializeWithExistingWindow succeeded\n");
|
|
|
|
ShowWindow(hwnd, nShowCmd);
|
|
UpdateWindow(hwnd);
|
|
|
|
Log("[INFO] OpenGL Device: %S", device.GetDeviceInfo().renderer.c_str());
|
|
Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str());
|
|
|
|
OpenGLSwapChain swapChain;
|
|
swapChain.Initialize(hwnd, gWidth, gHeight);
|
|
|
|
MSG msg = {};
|
|
int frameCount = 0;
|
|
const int targetFrameCount = 30;
|
|
|
|
while (true) {
|
|
if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
if (msg.message == WM_QUIT) {
|
|
break;
|
|
}
|
|
TranslateMessage(&msg);
|
|
DispatchMessageW(&msg);
|
|
} else {
|
|
glViewport(0, 0, gWidth, gHeight);
|
|
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
swapChain.Present(0, 0);
|
|
frameCount++;
|
|
|
|
if (frameCount >= targetFrameCount) {
|
|
Log("[INFO] Reached target frame count %d - taking screenshot!", targetFrameCount);
|
|
SaveScreenshotPPM("minimal.ppm", gWidth, gHeight);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
swapChain.Shutdown();
|
|
device.Shutdown();
|
|
Logger::Get().Shutdown();
|
|
|
|
Log("[INFO] OpenGL Integration Test Finished");
|
|
return 0;
|
|
} |