Files
unease/engine/render/render.cpp
2025-02-28 04:43:17 +03:00

133 lines
2.7 KiB
C++

#include "render/render.h"
#include "render/renderdevice.h"
#include "render/gl_shared.h"
#include "utils/logger.h"
#define RENDER_CLASS_NAME "RenderClassName"
#define RENDER_WINDOW_NAME "UNEASE Alpha"
HWND hWnd = NULL;
HDC hDC = NULL;
HGLRC hRC = NULL;
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
//input->OnKeyAction((int)wParam, true);
return 0;
case WM_KEYUP:
//input->OnKeyAction((int)wParam, false);
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
void R_CreateWindow(int width, int height)
{
static bool wndclassRegistered = false;
if (wndclassRegistered == false)
{
WNDCLASS wc;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(0);
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = RENDER_CLASS_NAME;
RegisterClass (&wc);
wndclassRegistered=true;
}
/* create main window */
hWnd = CreateWindow (
RENDER_CLASS_NAME, RENDER_WINDOW_NAME,
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, width, height,
NULL, NULL, GetModuleHandle(0), NULL);
LogMsg("Created window %ix%i", width, height);
// create opengl context
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
hDC = GetDC (hWnd);
/* set the pixel format for the DC */
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 = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (hDC, &pfd);
SetPixelFormat (hDC, iFormat, &pfd);
LogMsg("SetPixelFormat successful");
/* create and enable the render context (RC) */
hRC = wglCreateContext( hDC );
LogMsg("wglCreateContext successful");
wglMakeCurrent( hDC, hRC );
}
void R_Init()
{
LogMsg("--- R_Init ---");
// Create window
R_CreateWindow(1024, 768);
// Load OpenGL
GL_Load();
// Create render device
g_renderDevice = new RenderDevice();
}
void R_Shutdown()
{
if (g_renderDevice != NULL)
{
delete g_renderDevice;
g_renderDevice = NULL;
}
}
void R_Present()
{
SwapBuffers(hDC);
}