diff --git a/meson.build b/meson.build index a37701f..cda8d77 100644 --- a/meson.build +++ b/meson.build @@ -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 ) diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..7fe6a81 --- /dev/null +++ b/src/log.c @@ -0,0 +1,17 @@ +#include +#include +#include + +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"); +} + diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..29ba6a6 --- /dev/null +++ b/src/log.h @@ -0,0 +1,12 @@ +#ifndef LOG_H_ +#define LOG_H_ + +#include + +#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_ diff --git a/src/main.c b/src/main.c index 5d06504..a090bfb 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,8 @@ #include #include +#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); diff --git a/vendor/wine/opengl32.dll b/vendor/wine/opengl32.dll new file mode 100644 index 0000000..225c95d Binary files /dev/null and b/vendor/wine/opengl32.dll differ