log functions and working opengl context on windows

This commit is contained in:
2026-06-26 23:38:56 +03:00
parent 4b1ade6464
commit cf4a087dc3
5 changed files with 145 additions and 41 deletions

View File

@@ -5,16 +5,38 @@ cargs = [
'-Og',
'-g',
]
if host_machine.system() == 'windows'
dependencies = []
link_args = []
if host_machine.system() == 'linux'
dependencies += [
dependency('libglvnd')
]
elif host_machine.system() == 'windows'
link_args += [
'-lopengl32'
]
cargs += [
'-DUNICODE',
'-D__UNICODE',
'-municode'
]
if build_machine.system() == 'linux'
link_args += [
'-Lvendor/wine/',
]
endif
endif
executable(
'scuffedcraft',
'./src/main.c',
'src/main.c',
'src/log.c',
c_args: cargs,
dependencies: dependencies,
link_args: link_args
)

17
src/log.c Normal file
View File

@@ -0,0 +1,17 @@
#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>
void log_error(char* file, size_t line, char* str) {
fprintf(stderr, "[ERROR] %s:%zu: %s\n", file, line, str);
}
void log_errorf(char* file, size_t line, char* fmt, ...) {
fprintf(stderr, "[ERROR] %s:%zu: ", file, line);
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}

12
src/log.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef LOG_H_
#define LOG_H_
#include <stddef.h>
#define LOG_ERROR(str) log_error(__FILE__, __LINE__, (str))
#define LOG_ERRORF(fmt, ...) log_errorf(__FILE__, __LINE__, (fmt), __VA_ARGS__)
void log_error(char* file, size_t line, char* str);
void log_errorf(char* file, size_t line, char* fmt, ...);
#endif // LOG_H_

View File

@@ -5,6 +5,8 @@
#include <windowsx.h>
#include <winuser.h>
#include "log.h"
// int _main() {
// return 0;
// }
@@ -21,27 +23,79 @@ void OnSize(HWND hwnd, UINT flag, int width, int height) {
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
State* pState = NULL;
if (uMsg == WM_CREATE) {
CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam;
pState = (State*) pCreate->lpCreateParams;
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState);
} else {
LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
pState = (State*) ptr;
}
// State* pState = NULL;
// if (uMsg == WM_CREATE) {
// CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam;
// pState = (State*) pCreate->lpCreateParams;
// SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState);
// } else {
// LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
// pState = (State*) ptr;
// }
switch (uMsg) {
case WM_LBUTTONDOWN:
{
printf("State: msg: %s, num: %d\n", pState->msg, pState->num);
// case WM_LBUTTONDOWN: {
// printf("State: msg: %s, num: %d\n", pState->msg, pState->num);
//
// printf("lParam: %llx\n", lParam);
// int mouse_x = GET_X_LPARAM(lParam);
// printf("mouse_x: %x\n", mouse_x);
// int mouse_y = GET_Y_LPARAM(lParam);
// printf("mouse_y: %x\n", mouse_y);
// printf("x: %d y: %d\n", mouse_x, mouse_y);
//
// return 0;
// }
printf("lParam: %llx\n", lParam);
int mouse_x = GET_X_LPARAM(lParam);
printf("mouse_x: %x\n", mouse_x);
int mouse_y = GET_Y_LPARAM(lParam);
printf("mouse_y: %x\n", mouse_y);
printf("x: %d y: %d\n", mouse_x, mouse_y);
case WM_CREATE: {
const PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // Color depth
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
24, // Depth buffer bits
8, // Stencil buffer bits
0,
};
HDC hdc = GetWindowDC(hwnd);
if (hdc == NULL) {
LOG_ERROR("Failed to get window DC");
return 1;
}
const int pixel_format = ChoosePixelFormat(hdc, &pfd);
if (SetPixelFormat(hdc, pixel_format, &pfd) == FALSE) {
LOG_ERRORF("Failed to set pixel format. Error: %d", GetLastError());
return 1;
}
HGLRC gl_ctx = wglCreateContext(hdc);
if (gl_ctx == NULL) {
LOG_ERRORF("Failed to create OpenGL context. Error: %d", GetLastError());
return 1;
}
if (wglMakeCurrent(hdc, gl_ctx) == FALSE) {
LOG_ERRORF("Failed to make OpenGL context current. Error: %d", GetLastError());
return 1;
}
// glClearColor(1.0, 1.0, 0.0, 1.0);
// glClear(GL_COLOR_BUFFER_BIT);
return 0;
}
@@ -62,31 +116,29 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
}
return 0;
case WM_SIZE:
{
case WM_SIZE: {
int width = LOWORD(lParam);
int height = HIWORD(lParam);
OnSize(hwnd, (UINT) wParam, width, height);
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));
RECT blue = {
.bottom = 100,
.left = 0,
.right = 100,
.top = 0
};
FillRect(hdc, &blue, (HBRUSH) (COLOR_BACKGROUND + 1));
EndPaint(hwnd, &ps);
return 0;
}
// case WM_PAINT: {
// PAINTSTRUCT ps;
// HDC hdc = BeginPaint(hwnd, &ps);
//
// FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));
// RECT blue = {
// .bottom = 100,
// .left = 0,
// .right = 100,
// .top = 0
// };
// FillRect(hdc, &blue, (HBRUSH) (COLOR_BACKGROUND + 1));
//
// EndPaint(hwnd, &ps);
// return 0;
// }
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
@@ -99,6 +151,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.style = CS_OWNDC;
RegisterClass(&wc);
@@ -124,8 +177,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
);
if (hwnd == NULL) {
printf("ERROR: hwnd was NULL\n");
return 0;
LOG_ERROR("ERROR: hwnd was NULL\n");
return 1;
}
ShowWindow(hwnd, nCmdShow);

BIN
vendor/wine/opengl32.dll vendored Normal file

Binary file not shown.