104 lines
2.1 KiB
C++
104 lines
2.1 KiB
C++
|
|
#include "main.h"
|
|
#include <iostream>
|
|
HGLRC hRC;
|
|
|
|
// Declare a global callback variable
|
|
ErrorCallback glErrorCallback = nullptr;
|
|
|
|
bool _HAS_VIEWPORT_BEEN_SETUP = false;
|
|
unsigned int VIEWPORT_WIDTH = 0;
|
|
unsigned int VIEWPORT_HEIGHT = 0;
|
|
|
|
// Function to set the error callback
|
|
DLL_API void SetGLErrorCallback(ErrorCallback callback)
|
|
{
|
|
glErrorCallback = callback;
|
|
}
|
|
|
|
DLL_API void InitializeOpenGL(HWND hWnd)
|
|
{
|
|
HDC hdc = GetDC(hWnd);
|
|
|
|
// Set pixel format
|
|
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 = 32;
|
|
pfd.cDepthBits = 24;
|
|
pfd.cStencilBits = 8;
|
|
|
|
int pixelFormat = ChoosePixelFormat(hdc, &pfd);
|
|
SetPixelFormat(hdc, pixelFormat, &pfd);
|
|
|
|
hRC = wglCreateContext(hdc);
|
|
wglMakeCurrent(hdc, hRC);
|
|
|
|
GLenum err = glewInit();
|
|
if (err != GLEW_OK) {
|
|
// Handle GLEW initialization error
|
|
|
|
if (glErrorCallback)
|
|
{
|
|
const char* errorMessage = reinterpret_cast<const char*>(glewGetErrorString(err));
|
|
glErrorCallback(err, errorMessage);
|
|
}
|
|
|
|
wglMakeCurrent(NULL, NULL);
|
|
wglDeleteContext(hRC);
|
|
ReleaseDC(hWnd, hdc);
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
DLL_API void RenderFrame()
|
|
{
|
|
if (!_HAS_VIEWPORT_BEEN_SETUP)
|
|
{
|
|
return;
|
|
}
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
glViewport(0, 0, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
|
|
|
|
// Set up vertex data
|
|
float vertices[] = {
|
|
0.0f, 0.5f, 0.0f,
|
|
0.5f, -0.5f, 0.0f,
|
|
-0.5f, -0.5f, 0.0f
|
|
};
|
|
|
|
unsigned int vao;
|
|
glGenVertexArrays(1, &vao);
|
|
glBindVertexArray(vao);
|
|
|
|
unsigned int vbo;
|
|
glGenBuffers(1, &vbo);
|
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
glDeleteBuffers(1, &vbo);
|
|
glDeleteVertexArrays(1, &vao);
|
|
|
|
SwapBuffers(wglGetCurrentDC());
|
|
}
|
|
|
|
DLL_API void SetupViewport(unsigned int width, unsigned int height)
|
|
{
|
|
VIEWPORT_WIDTH = width;
|
|
VIEWPORT_HEIGHT = height;
|
|
|
|
_HAS_VIEWPORT_BEEN_SETUP = true;
|
|
} |