win32_compat.h (6302B)
1 /* Windows-only glue for main.c (included under #ifdef _WIN32). 2 * 3 * The VM (cf.s) calls C functions with the 32-bit stack-based cdecl convention: the 4 * caller pushes the arguments and cleans the stack. Windows' OpenGL entry points are 5 * __stdcall (callee cleans), so calling them straight from Forth would unbalance the 6 * stack. Every GL function the VM calls is therefore wrapped below by a plain cdecl 7 * function with the same arguments. GLFW's own functions are cdecl and need no wrapper. 8 */ 9 #ifndef WIN32_COMPAT_H 10 #define WIN32_COMPAT_H 11 12 #include <windows.h> 13 #include <malloc.h> 14 #include <fcntl.h> 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include "debuglog.h" 19 20 /* Windows' gl.h is OpenGL 1.1; these 1.2 constants are only mentioned by main.c (the Forth 21 * side passes its own numeric values), so just provide their standard values */ 22 #ifndef GL_UNSIGNED_SHORT_5_6_5 23 #define GL_UNSIGNED_SHORT_5_6_5 0x8363 24 #endif 25 #ifndef GL_UNSIGNED_SHORT_5_5_5_1 26 #define GL_UNSIGNED_SHORT_5_5_5_1 0x8034 27 #endif 28 29 #define usleep(us) Sleep((DWORD)((us) / 1000)) 30 31 /* ---- cdecl wrappers for the stdcall GL functions the VM calls ---- */ 32 #define GLF(x) (&cd_##x) 33 static void cd_glClear(GLbitfield m) { glClear(m); } 34 static void cd_glDrawPixels(GLsizei w, GLsizei h, GLenum f, GLenum t, const GLvoid* p) { 35 static int n = 0; 36 if(n < 4) { n++; dbg("glDrawPixels #%d: %dx%d format %04x type %04x\n", n, w, h, f, t); } 37 glDrawPixels(w, h, f, t, p); 38 } 39 static void cd_glEnable(GLenum c) { glEnable(c); } 40 static void cd_glDisable(GLenum c) { glDisable(c); } 41 static void cd_glBlendFunc(GLenum s, GLenum d) { glBlendFunc(s, d); } 42 static void cd_glBindTexture(GLenum t, GLuint n) { glBindTexture(t, n); } 43 static void cd_glBegin(GLenum m) { glBegin(m); } 44 static void cd_glEnd(void) { glEnd(); } 45 static void cd_glGenTextures(GLsizei n, GLuint* t) { glGenTextures(n, t); } 46 static void cd_glTexParameteri(GLenum t, GLenum p, GLint v) { glTexParameteri(t, p, v); } 47 static void cd_glMatrixMode(GLenum m) { glMatrixMode(m); } 48 static void cd_glPushMatrix(void) { glPushMatrix(); } 49 static void cd_glPopMatrix(void) { glPopMatrix(); } 50 static void cd_glLoadIdentity(void) { glLoadIdentity(); } 51 static void cd_glVertex3i(GLint x, GLint y, GLint z) { glVertex3i(x, y, z); } 52 static void cd_glTexCoord2i(GLint s, GLint t) { glTexCoord2i(s, t); } 53 54 /* ---- no libmikmod on Windows: the sound functions in main.c are already stubs; 55 * provide just the types/entry points it still references ---- */ 56 typedef struct { int volume, wrap, loop, fadeout; } MODULE; 57 typedef struct { int panning, speed, volume; } SAMPLE; 58 static void Player_ToggleMute(int a, ...) { } 59 static void Player_TogglePause(void) { } 60 static void Player_SetVolume(short v) { } 61 static void Player_Stop(void) { } 62 static void Player_Free(MODULE* m) { } 63 64 /* ---- crash report: same PC32_crash.log as the Linux build ---- */ 65 static int g_argc; static char** g_argv; 66 static LONG WINAPI win_crash_filter(EXCEPTION_POINTERS* ep) { 67 CONTEXT* c = ep->ContextRecord; 68 EXCEPTION_RECORD* e = ep->ExceptionRecord; 69 FILE* out[2] = { stderr, fopen("PC32_crash.log", "w") }; 70 for(int k = 0; k < 2; k++) { 71 FILE* o = out[k]; 72 if(!o) continue; 73 fprintf(o, "\nPC32 CRASH: exception %08lx, addr %p, eip %08lx, esp %08lx (esp&15 = %lu)\n", 74 e->ExceptionCode, e->ExceptionAddress, c->Eip, c->Esp, c->Esp & 15); 75 if(e->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && e->NumberParameters >= 2) 76 fprintf(o, "access violation: %s address %p\n", 77 e->ExceptionInformation[0] ? "writing" : "reading", (void*)e->ExceptionInformation[1]); 78 fprintf(o, "regs: eax %08lx ebx %08lx ecx %08lx edx %08lx esi %08lx edi %08lx ebp %08lx\n", 79 c->Eax, c->Ebx, c->Ecx, c->Edx, c->Esi, c->Edi, c->Ebp); 80 HMODULE mod = NULL; char name[MAX_PATH] = "?"; 81 if(GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, 82 (LPCSTR)c->Eip, &mod)) GetModuleFileNameA(mod, name, sizeof name); 83 fprintf(o, "in module: %s (base %p)\n", name, (void*)mod); 84 if(!IsBadReadPtr((void*)c->Eip, 8)) { 85 unsigned char* p = (unsigned char*)c->Eip; 86 fprintf(o, "bytes at eip: %02x %02x %02x %02x %02x %02x %02x %02x\n", 87 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 88 } 89 fprintf(o, "argc %d:", g_argc); 90 for(int i = 0; i < g_argc; i++) fprintf(o, " [%s]", g_argv[i]); 91 fprintf(o, "\nstack at esp:"); 92 if(!IsBadReadPtr((void*)c->Esp, 128)) 93 for(int i = 0; i < 32; i++) fprintf(o, "%s%08lx", i % 8 ? " " : "\n ", ((unsigned long*)c->Esp)[i]); 94 fprintf(o, "\n"); 95 fflush(o); 96 } 97 if(out[1]) { fclose(out[1]); fprintf(stderr, "PC32: crash report written to PC32_crash.log\n"); } 98 return EXCEPTION_EXECUTE_HANDLER; 99 } 100 static void install_crash_handler(void) { SetUnhandledExceptionFilter(win_crash_filter); } 101 102 /* run from the exe's folder (data.blk, data/, fnt.bin ... are looked up relative to it), 103 * and make fopen binary by default (Windows text mode would translate \n and stop at ^Z) */ 104 static void win_startup(void) { 105 _fmode = _O_BINARY; 106 char path[MAX_PATH]; 107 DWORD n = GetModuleFileNameA(NULL, path, sizeof path); 108 if(n > 0 && n < sizeof path) { 109 char* slash = strrchr(path, '\\'); 110 if(slash) { *slash = 0; SetCurrentDirectoryA(path); } 111 } 112 } 113 114 /* An undecorated GLFW window on Windows is WS_POPUP without a maximize box, so GLFW_MAXIMIZED and 115 * ShowWindow(SW_MAXIMIZE) don't work (and WS_MAXIMIZE would even block resizing it). Fit it to 116 * the monitor's work area (screen minus taskbar) by hand instead. */ 117 static void win_fit_window(GLFWwindow* w) { 118 int x = 0, y = 0, cx = 0, cy = 0; 119 GLFWmonitor* mon = glfwGetPrimaryMonitor(); 120 if(mon) glfwGetMonitorWorkarea(mon, &x, &y, &cx, &cy); 121 dbg("work area %d,%d %dx%d\n", x, y, cx, cy); 122 if(cx > 0 && cy > 0) { glfwSetWindowPos(w, x, y); glfwSetWindowSize(w, cx, cy); } 123 } 124 125 #endif