updated Win32 to use Modern OpenGL;

Canoi fd09c00f 6fa5ff5c

Changed files
+669 -56
+668 -55
bite.c
··· 2 2 3 3 #if defined(_WIN32) 4 4 #include <windows.h> 5 + #ifndef WINDOWS_LEAN_AND_MEAN 6 + #define WINDOWS_LEAN_AND_MEAN 1 7 + #endif 5 8 #include <tchar.h> 6 9 #include <gl/GL.h> 7 10 #include <gl/GLU.h> ··· 14 17 #include <X11/Xlib.h> 15 18 #include <X11/Xutil.h> 16 19 #include <GL/glx.h> 20 + #include <dlfcn.h> 17 21 #endif 18 22 #include <GL/gl.h> 19 23 #include <time.h> ··· 24 28 int width, height; 25 29 #if defined(_WIN32) 26 30 HWND handle; 27 - HINSTANCE hInstance; 28 - HDC devContext; 29 - HGLRC glContext; 31 + HDC dev_context; 30 32 #elif defined(__EMSCRIPTEN__) 31 - EMSCRIPTEN_WEBGL_CONTEXT_HANDLE handle; 32 33 #else 33 34 Display* display; 34 35 Window handle; 35 - GLXContext glContext; 36 - XVisualInfo* visual; 37 36 #endif 38 37 }; 39 38 39 + typedef struct be_Render be_Render; 40 40 struct be_Render { 41 + #if defined(_WIN32) 42 + HGLRC gl_context; 43 + #elif defined(__EMSCRIPTEN__) 44 + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE gl_context; 45 + #else 46 + GLXContext gl_context; 47 + #endif 41 48 int mode; 42 49 }; 43 50 44 51 struct be_Context { 45 52 int running; 46 53 be_Window window; 54 + be_Render render; 47 55 be_EventCallback callbacks[BITE_EVENTS]; 48 56 }; 49 57 ··· 57 65 } 58 66 }; 59 67 60 - int _init_context(be_Context* ctx, const be_Config* conf); 68 + #if defined(_WIN32) 69 + typedef HGLRC WINAPI wglCreateContextAttribsARBProc(HDC hdc, HGLRC hShareContext, const int* attribList); 70 + wglCreateContextAttribsARBProc* wglCreateContextAttribsARB; 71 + #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091 72 + #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092 73 + #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126 74 + #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001 75 + #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002 76 + 77 + typedef BOOL WINAPI wglChoosePixelFormatARBProc(HDC hdc, const int *piAttribIList, 78 + const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats); 79 + wglChoosePixelFormatARBProc *wglChoosePixelFormatARB; 80 + 81 + // See https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_pixel_format.txt for all values 82 + #define WGL_DRAW_TO_WINDOW_ARB 0x2001 83 + #define WGL_ACCELERATION_ARB 0x2003 84 + #define WGL_SUPPORT_OPENGL_ARB 0x2010 85 + #define WGL_DOUBLE_BUFFER_ARB 0x2011 86 + #define WGL_PIXEL_TYPE_ARB 0x2013 87 + #define WGL_COLOR_BITS_ARB 0x2014 88 + #define WGL_DEPTH_BITS_ARB 0x2022 89 + #define WGL_STENCIL_BITS_ARB 0x2023 90 + 91 + #define WGL_FULL_ACCELERATION_ARB 0x2027 92 + #define WGL_TYPE_RGBA_ARB 0x202B 93 + #else 94 + #endif 95 + 96 + #define DEFINE_GL(ret, name, ...)\ 97 + typedef ret name##Proc(__VA_ARGS__);\ 98 + static name##Proc* name = 0 99 + 100 + #if !defined(__EMSCRIPTEN__) 101 + #define GL_VERSION 0x1F02 102 + #define GL_SHADING_LANGUAGE_VERSION 0x8B8C 103 + 104 + /* data types */ 105 + #define GL_BYTE 0x1400 106 + #define GL_UNSIGNED_BYTE 0x1401 107 + #define GL_SHORT 0x1402 108 + #define GL_UNSIGNED_SHORT 0x1403 109 + #define GL_INT 0x1404 110 + #define GL_UNSIGNED_INT 0x1405 111 + #define GL_FLOAT 0x1406 112 + #define GL_2_BYTES 0x1407 113 + #define GL_3_BYTES 0x1408 114 + #define GL_4_BYTES 0x1409 115 + #define GL_DOUBLE 0x140A 116 + 117 + /* Clear buffer bits */ 118 + #define GL_DEPTH_BUFFER_BIT 0x00000100 119 + #define GL_ACCUM_BUFFER_BIT 0x00000200 120 + #define GL_STENCIL_BUFFER_BIT 0x00000400 121 + #define GL_COLOR_BUFFER_BIT 0x00004000 122 + 123 + #define GL_RGB 0x1907 124 + #define GL_RGBA 0x1908 125 + 126 + /* bgra */ 127 + #define GL_BGR 0x80E0 128 + #define GL_BGRA 0x80E1 129 + 130 + /* Primitives */ 131 + #define GL_POINTS 0x0000 132 + #define GL_LINES 0x0001 133 + #define GL_TRIANGLES 0x0004 134 + 135 + #define GL_ARRAY_BUFFER 0x8892 136 + #define GL_ELEMENT_ARRAY_BUFFER 0x8893 137 + 138 + #define GL_STREAM_DRAW 0x88E0 139 + #define GL_STREAM_READ 0x88E1 140 + #define GL_STREAM_COPY 0x88E2 141 + #define GL_STATIC_DRAW 0x88E4 142 + #define GL_STATIC_READ 0x88E5 143 + #define GL_STATIC_COPY 0x88E6 144 + #define GL_DYNAMIC_DRAW 0x88E8 145 + #define GL_DYNAMIC_READ 0x88E9 146 + #define GL_DYNAMIC_COPY 0x88EA 147 + 148 + #define GL_TEXTURE_2D 0x0DE1 149 + // #define GL_TEXTURE_MIN_FILTER 0x2800 150 + // #define GL_TEXTURE_MAG_FILTER 0x2801 151 + #define GL_TEXTURE_WRAP_S 0x2802 152 + #define GL_TEXTURE_WRAP_T 0x2803 153 + 154 + #define GL_NEAREST 0x2600 155 + #define GL_REPEAT 0x2901 156 + #define GL_CLAMP 0x2900 157 + 158 + #define GL_CLAMP_TO_EDGE 0x812F /* 1.2 */ 159 + #define GL_CLAMP_TO_BORDER 0x812D /* 1.3 */ 160 + 161 + // Core 162 + // DEFINE_GL(void, glClearColor, float, float, float, float); 163 + // DEFINE_GL(void, glClear, GLenum); 164 + 165 + // Transformation 166 + // DEFINE_GL(void, glViewport, GLint, GLint, GLint, GLint); 167 + 168 + // VAO 169 + DEFINE_GL(void, glGenVertexArrays, GLsizei, GLuint*); // glGenVertexArrays 170 + DEFINE_GL(void, glBindVertexArray, GLuint); // glBindVertexArray 171 + DEFINE_GL(void, glDeleteVertexArrays, GLsizei, GLuint*); // glDeleteVertexArrays 172 + DEFINE_GL(void, glVertexAttribPointer, GLuint, GLint, GLuint, GLint, GLint, const void*); 173 + DEFINE_GL(void, glEnableVertexAttribArray, GLuint); 174 + DEFINE_GL(void, glDisableVertexAttribArray, GLuint); 175 + 176 + // Buffers 177 + DEFINE_GL(void, glGenBuffers, GLsizei, GLuint*); // glGenBuffers 178 + DEFINE_GL(void, glBindBuffer, GLenum, GLuint); // glBindBuffer 179 + DEFINE_GL(void, glDeleteBuffers, GLsizei, GLuint*); // glDeleteBuffers 180 + DEFINE_GL(void, glBufferData, GLenum, GLsizei, const void*, GLenum); // glBufferData(GLenum target, GLsizeptr size, const void* data, GLenum usage) 181 + DEFINE_GL(void, glBufferSubData, GLenum, GLsizei, GLsizei, const void*); // glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr, size, const void* data) 182 + 183 + // DEFINE_GL(void, glDrawArrays, GLenum, GLint, GLsizei); // glDrawArrays 184 + 185 + // SHADERS 186 + #define GL_FRAGMENT_SHADER 0x8B30 187 + #define GL_VERTEX_SHADER 0x8B31 188 + 189 + #define GL_COMPILE_STATUS 0x8B81 190 + #define GL_LINK_STATUS 0x8B82 191 + 192 + DEFINE_GL(GLuint, glCreateShader, GLenum); // glCreateShader 193 + DEFINE_GL(void, glShaderSource, GLuint, GLsizei, const char**, const GLint*); // glShaderSource 194 + DEFINE_GL(void, glCompileShader, GLuint); // glCompileShader 195 + 196 + #define GL_LINK_STATUS 0x8B82 197 + DEFINE_GL(void, glGetShaderiv, GLuint, GLenum, GLint*); // glGetShaderiv 198 + const int t = GL_LINK_STATUS; 199 + DEFINE_GL(void, glGetShaderInfoLog, GLuint, GLsizei, GLsizei*, char*); // glGetShaderInfoLog 200 + DEFINE_GL(void, glDeleteShader, GLuint); // glDeleteShader 201 + DEFINE_GL(GLuint, glCreateProgram, void); // glCreateProgram 202 + DEFINE_GL(void, glAttachShader, GLuint, GLuint); // glAttachShader 203 + DEFINE_GL(void, glLinkProgram, GLuint); // glLinkProgram 204 + DEFINE_GL(void, glGetProgramiv, GLuint, GLenum, GLint*); // glGetProgramiv 205 + DEFINE_GL(void, glGetProgramInfoLog, GLuint, GLsizei, GLsizei*, char*); // glGetProgramInfoLog 206 + DEFINE_GL(void, glUseProgram, GLuint); // glUseProgram 207 + 208 + DEFINE_GL(void, glGetActiveUniform, GLuint, GLuint, GLint, GLint*, GLint*, GLint*, char*); 209 + DEFINE_GL(GLint, glGetUniformLocation, GLuint, const char*); 210 + 211 + #define DEFINE_GL_UNIFORM(X, T)\ 212 + DEFINE_GL(void, glUniform1##X, GLuint, T);\ 213 + DEFINE_GL(void, glUniform2##X, GLint, T, T);\ 214 + DEFINE_GL(void, glUniform3##X, GLint, T, T, T);\ 215 + DEFINE_GL(void, glUniform4##X, GLint, T, T, T, T);\ 216 + DEFINE_GL(void, glUniform1##X##v, GLint, GLint, const T*);\ 217 + DEFINE_GL(void, glUniform2##X##v, GLint, GLint, const T*);\ 218 + DEFINE_GL(void, glUniform3##X##v, GLint, GLint, const T*);\ 219 + DEFINE_GL(void, glUniform4##X##v, GLint, GLint, const T*) 220 + 221 + // DEFINE_GL_UNIFORM(f, float); 222 + // DEFINE_GL_UNIFORM(i, GLint); 223 + 224 + DEFINE_GL(void, glUniformMatrix2fv, GLint, GLint, GLint, const float*); 225 + DEFINE_GL(void, glUniformMatrix3fv, GLint, GLint, GLint, const float*); 226 + DEFINE_GL(void, glUniformMatrix4fv, GLint, GLint, GLint, const float*); 227 + 228 + DEFINE_GL(GLint, glGetAttribLocation, GLuint, const char*); 229 + DEFINE_GL(void, glGetActiveAttribe, GLuint, GLuint, GLint, GLint*, GLint*, GLuint*, char*); 230 + DEFINE_GL(void, glBindAttribLocation, GLuint, GLuint, const char*); 231 + 232 + #if defined(_WIN32) 233 + static HMODULE _gl_sym; 234 + #elif defined(__EMSCRIPTEN__) 235 + #else 236 + static void* _gl_sym; 237 + #ifndef RTLD_LAZY 238 + #define RTLD_LAZY 0x00001 239 + #endif 240 + #ifndef RTLD_GLOBAL 241 + #define RTLD_GLOBAL 0x00100 242 + #endif 243 + #endif 244 + 245 + static void* _get_proc(const char* name); 246 + BITE_RESULT _load_gl(void); 247 + void _setup_gl(void); 248 + void _close_gl(void); 249 + #endif 250 + 251 + BITE_RESULT _init_context(be_Context* ctx, const be_Config* conf); 252 + BITE_RESULT _init_window(be_Window* window, const be_Config* conf); 253 + BITE_RESULT _init_render(be_Render* render, const be_Window* window, const be_Config* conf); 61 254 void _poll_events(be_Context* ctx); 255 + 62 256 63 257 #if defined(_WIN32) 64 258 LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { 65 259 be_Context* ctx = (be_Context*)GetWindowLongPtr(hwnd, GWLP_USERDATA); 66 260 be_Event e; 67 261 e.type = 0; 262 + be_EventCallback fn = NULL; 68 263 switch (msg) { 69 264 case WM_CREATE: return 0; 70 265 case WM_CLOSE: { ··· 84 279 e.key.keycode = (int)wParam; 85 280 } 86 281 break; 282 + default: 283 + return DefWindowProc(hwnd, msg, wParam, lParam); 87 284 } 88 - be_EventCallback fn = NULL; 285 + 89 286 if (ctx) fn = ctx->callbacks[e.type]; 90 287 if (fn) fn(ctx, &e); 91 - return DefWindowProc(hwnd, msg, wParam, lParam); 288 + printf("Teste\n"); 289 + return 0; 92 290 } 93 291 #endif 94 292 ··· 108 306 be_Config bite_init_config(const char* title, int w, int h) { 109 307 be_Config c = {0}; 110 308 title = title != NULL ? title : "bitEngine "BITE_VERSION; 111 - int len = strlen(title); 112 - strcpy(c.window.title, title); 309 + size_t len = strlen(title); 310 + memcpy(c.window.title, title, len); 113 311 c.window.width = w; 114 312 c.window.height = h; 115 313 return c; ··· 122 320 fprintf(stderr, "Failed to alloc memory for context\n"); 123 321 return NULL; 124 322 } 323 + int mag, min; 324 + // glGetIntegerv(GL_MAJOR_VERSION, &mag); 325 + // glGetIntegerv(GL_MINOR_VERSION, &min); 326 + // printf("OpenGL loaded: %d.%d\n", mag, min); 125 327 ctx->running = 1; 126 328 for (int i = 0; i < BITE_EVENTS; i++) { 127 329 ctx->callbacks[i] = NULL; ··· 130 332 free(ctx); 131 333 return NULL; 132 334 } 335 + const char* ver = glGetString(GL_VERSION); 336 + printf("OpenGL loaded: %s\n", ver); 133 337 return ctx; 134 338 } 135 339 136 340 void bite_destroy(be_Context* ctx) { 137 341 if (!ctx) return; 138 342 be_Window* window = &(ctx->window); 343 + be_Render* render = &(ctx->render); 139 344 #if defined(_WIN32) 140 345 wglMakeCurrent(NULL, NULL); 141 - wglDeleteContext(window->glContext); 142 - ReleaseDC(window->handle, window->devContext); 346 + wglDeleteContext(render->gl_context); 347 + ReleaseDC(window->handle, window->dev_context); 143 348 DestroyWindow(window->handle); 144 349 #elif defined(__EMSCRIPTEN__) 145 350 emscripten_webgl_destroy_context(window->handle); 146 351 #else 147 - glXDestroyContext(window->display, window->glContext); 352 + glXDestroyContext(window->display, render->gl_context); 148 353 XDestroyWindow(window->display, window->handle); 149 354 XCloseDisplay(window->display); 150 355 #endif ··· 176 381 if (!ctx) return; 177 382 be_Window* window = &(ctx->window); 178 383 #if defined(_WIN32) 179 - SwapBuffers(window->devContext); 384 + SwapBuffers(window->dev_context); 180 385 #elif defined(__EMSCRIPTEN__) 181 386 emscripten_webgl_commit_frame(); 182 387 #else ··· 202 407 *********************/ 203 408 204 409 static GLuint _create_program(GLuint vert, GLuint frag) { 410 + #if 0 205 411 GLuint program = glCreateProgram(); 206 412 glAttachShader(program, vert); 207 413 glAttachShader(program, frag); ··· 212 418 if (!success) {} 213 419 214 420 return program; 421 + #endif 422 + return 0; 215 423 } 216 424 217 425 be_Shader* bite_create_shader(const char* vert, const char* frag) { 218 426 be_Shader* shader = NULL; 219 - GLuint program; 220 - GLuint vert, frag; 221 - 222 427 return shader; 223 428 } 224 429 225 430 void bite_render_clear_color(float r, float g, float b, float a) { 226 - glClearColor(r, g, b, a); 431 + glClearColor(r, g, b, a); 227 432 } 228 433 229 434 void bite_render_clear(void) { 230 - glClear(GL_COLOR_BUFFER_BIT); 435 + glClear(GL_COLOR_BUFFER_BIT); 231 436 } 232 437 233 438 struct be_Texture { 234 - GLuint handle; 235 - int width, height; 236 - int filter[2]; 237 - int wrap[2]; 439 + GLuint handle; 440 + int width, height; 441 + int filter[2]; 442 + int wrap[2]; 238 443 }; 239 - 240 - struct be_Framebuffer { 444 + 445 + struct be_Framebuffer { 241 446 GLuint handle; 242 447 be_Texture* texture; 243 - }; 244 - 245 - struct be_Shader { 448 + }; 449 + 450 + struct be_Shader { 246 451 GLuint handle; 247 452 int world_uniform; 248 453 int modelview_uniform; 249 - }; 250 - 251 - void bite_bind_framebuffer(be_Framebuffer* fbo) { 454 + }; 455 + 456 + void bite_bind_framebuffer(be_Framebuffer* fbo) { 457 + #if 0 252 458 if (!fbo) glBindFramebuffer(GL_FRAMEBUFFER, 0); 253 459 else glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle); 254 - } 255 - 256 - void bite_bind_texture(be_Texture* tex) { 460 + #endif 461 + } 462 + 463 + void bite_bind_texture(be_Texture* tex) { 257 464 if (!tex) glBindTexture(GL_TEXTURE_2D, 0); 258 465 else glBindTexture(GL_TEXTURE_2D, tex->handle); 259 - } 260 - 261 - void bite_use_shader(be_Shader* shader) { 466 + } 467 + 468 + void bite_use_shader(be_Shader* shader) { 262 469 if (!shader) glUseProgram(0); 263 470 else glUseProgram(shader->handle); 264 - } 471 + } 265 472 266 473 /********************* 267 474 * Timer ··· 294 501 /********************* 295 502 * Internal 296 503 *********************/ 504 + 505 + // static void init_opengl_extensions(be_Context* ctx); 506 + 507 + #if defined(_WIN32) 508 + static void init_opengl_extensions(void) { 509 + WNDCLASSA window_class = { 510 + .style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC, 511 + .lpfnWndProc = DefWindowProcA, 512 + .hInstance = GetModuleHandle(0), 513 + .lpszClassName = "Dummy_WGL_window" 514 + }; 515 + 516 + if (!RegisterClassA(&window_class)) { 517 + fprintf(stderr, "Failed to register dummy OpenGL window\n"); 518 + exit(EXIT_FAILURE); 519 + } 520 + 521 + HWND dummy_window = CreateWindowExA( 522 + 0, 523 + window_class.lpszClassName, 524 + "Dummy OpenGL Window", 525 + 0, 526 + CW_USEDEFAULT, CW_USEDEFAULT, 527 + CW_USEDEFAULT, CW_USEDEFAULT, 528 + 0, 0, 529 + window_class.hInstance, 530 + 0 531 + ); 532 + 533 + if (!dummy_window) { 534 + fprintf(stderr, "Failed to create dummy OpenGL window\n"); 535 + exit(EXIT_FAILURE); 536 + } 537 + 538 + HDC dummy_dc = GetDC(dummy_window); 539 + 540 + PIXELFORMATDESCRIPTOR pfd = { 541 + .nSize = sizeof(pfd), 542 + .nVersion = 1, 543 + .iPixelType = PFD_TYPE_RGBA, 544 + .dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, 545 + .cColorBits = 32, 546 + .cAlphaBits = 8, 547 + .iLayerType = PFD_MAIN_PLANE, 548 + .cDepthBits = 24, 549 + .cStencilBits = 8 550 + }; 551 + 552 + int pixel_format = ChoosePixelFormat(dummy_dc, &pfd); 553 + if (!pixel_format) { 554 + fprintf(stderr, "Failed to find a suitable format\n"); 555 + exit(EXIT_FAILURE); 556 + } 557 + if (!SetPixelFormat(dummy_dc, pixel_format, &pfd)) { 558 + fprintf(stderr, "Failed to set the pixel format\n"); 559 + exit(EXIT_FAILURE); 560 + } 561 + 562 + HGLRC dummy_context = wglCreateContext(dummy_dc); 563 + if (!dummy_context) { 564 + fprintf(stderr, "Failed to create dummy OpenGL context\n"); 565 + exit(EXIT_FAILURE); 566 + } 567 + 568 + if (!wglMakeCurrent(dummy_dc, dummy_context)) { 569 + fprintf(stderr, "Failed to activate dummy OpenGL context\n"); 570 + exit(EXIT_FAILURE); 571 + } 572 + wglCreateContextAttribsARB = (wglCreateContextAttribsARBProc*)wglGetProcAddress("wglCreateContextAttribsARB"); 573 + wglChoosePixelFormatARB = (wglChoosePixelFormatARBProc*)wglGetProcAddress("wglChoosePixelFormatARB"); 574 + 575 + printf("functions: %p %p\n", wglCreateContextAttribsARB, wglChoosePixelFormatARB); 576 + 577 + wglMakeCurrent(dummy_dc, 0); 578 + wglDeleteContext(dummy_context); 579 + ReleaseDC(dummy_window, dummy_dc); 580 + DestroyWindow(dummy_window); 581 + } 582 + #endif 583 + 584 + BITE_RESULT _init_context(be_Context* ctx, const be_Config* conf) { 585 + if (_init_window(&(ctx->window), conf) != BITE_OK) return BITE_ERROR; 586 + if (_init_render(&(ctx->render), &(ctx->window), conf) != BITE_OK) return BITE_ERROR; 587 + return BITE_OK; 588 + } 589 + 590 + BITE_RESULT _init_window(be_Window* window, const be_Config* conf) { 591 + #if defined(_WIN32) 592 + HWND handle; 593 + HMODULE hInstance = GetModuleHandle(0); 594 + 595 + const TCHAR windowClass[] = _T("bitEngine"); 596 + 597 + WNDCLASSEX wc; 598 + wc.cbSize = sizeof(WNDCLASSEX); 599 + wc.style = CS_HREDRAW | CS_VREDRAW; 600 + wc.lpfnWndProc = WindowProc; 601 + wc.cbClsExtra = 0; 602 + wc.cbWndExtra = 0; 603 + wc.hInstance = hInstance; 604 + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 605 + wc.hCursor = LoadCursor(NULL, IDC_ARROW); 606 + wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 607 + wc.lpszMenuName = NULL; 608 + wc.lpszClassName = windowClass; 609 + wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 610 + 611 + if (!RegisterClassEx(&wc)) { 612 + MessageBox(NULL, 613 + _T("Call to RegisterClassEx failed"), 614 + _T("bitEngine"), 615 + NULL); 616 + return BITE_ERROR; 617 + } 618 + 619 + handle = CreateWindow( 620 + windowClass, 621 + conf->window.title, 622 + WS_OVERLAPPEDWINDOW, 623 + CW_USEDEFAULT, CW_USEDEFAULT, 624 + conf->window.width, conf->window.height, 625 + NULL, NULL, hInstance, NULL 626 + ); 627 + if (!handle) { 628 + MessageBox(NULL, 629 + _T("Class to CreateWindow failed"), 630 + _T("bitEngine"), 631 + NULL 632 + ); 633 + return BITE_ERROR; 634 + } 635 + SetWindowLongPtr(handle, GWLP_USERDATA, (LONG_PTR)window); 636 + window->handle = handle; 637 + window->dev_context = GetDC(handle); 638 + 639 + ShowWindow(handle, SW_SHOWDEFAULT); 640 + UpdateWindow(handle); 641 + #endif 642 + window->width = conf->window.width; 643 + window->height = conf->window.height; 644 + return BITE_OK; 645 + } 646 + 647 + BITE_RESULT _init_render(be_Render* render, const be_Window* window, const be_Config* conf) { 648 + #if defined(_WIN32) 649 + init_opengl_extensions(); 650 + HDC hdc = window->dev_context; 651 + int pixel_format_attribs[] = { 652 + WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, 653 + WGL_SUPPORT_OPENGL_ARB, GL_TRUE, 654 + WGL_DOUBLE_BUFFER_ARB, GL_TRUE, 655 + WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, 656 + WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, 657 + WGL_COLOR_BITS_ARB, 32, 658 + WGL_DEPTH_BITS_ARB, 24, 659 + WGL_STENCIL_BITS_ARB, 8, 660 + 0, 0 661 + }; 662 + 663 + int pixel_format; 664 + UINT num_formats; 665 + wglChoosePixelFormatARB(hdc, pixel_format_attribs, 0, 1, &pixel_format, &num_formats); 666 + if (!num_formats) { 667 + MessageBox( 668 + NULL, 669 + _T("Failed to set Modern OpenGL pixel format"), 670 + _T("bitEngine"), 671 + NULL 672 + ); 673 + DestroyWindow(window->handle); 674 + return BITE_ERROR; 675 + } 676 + 677 + PIXELFORMATDESCRIPTOR pfd; 678 + DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd); 679 + if (!SetPixelFormat(hdc, pixel_format, &pfd)) { 680 + MessageBox( 681 + NULL, 682 + _T("Failed to set Modern OpenGL pixel format\n"), 683 + _T("bitEngine"), 684 + NULL 685 + ); 686 + DestroyWindow(window->handle); 687 + return BITE_ERROR; 688 + } 689 + 690 + int gl_attribs[] = { 691 + WGL_CONTEXT_MAJOR_VERSION_ARB, 2, 692 + WGL_CONTEXT_MINOR_VERSION_ARB, 1, 693 + WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 694 + 0, 0 695 + }; 696 + 697 + HGLRC hglrc = wglCreateContextAttribsARB(hdc, 0, gl_attribs); 698 + if (!hglrc) { 699 + MessageBox( 700 + NULL, 701 + _T("Failed to create Modern OpenGL context"), 702 + _T("bitEngine"), 703 + NULL 704 + ); 705 + DestroyWindow(window->handle); 706 + return BITE_ERROR; 707 + } 708 + wglMakeCurrent(hdc, hglrc); 709 + 710 + if (_load_gl() != BITE_OK) { 711 + MessageBox( 712 + NULL, 713 + _T("Failed to init OpenGL loader"), 714 + _T("bitEngine"), 715 + NULL 716 + ); 717 + DestroyWindow(window->handle); 718 + return BITE_ERROR; 719 + } 720 + _setup_gl(); 721 + _close_gl(); 722 + #elif defined(__EMSCRIPTEN__) 723 + #else 724 + #endif 725 + return BITE_OK; 726 + } 727 + 728 + #if 0 297 729 int _init_context(be_Context* ctx, const be_Config* conf) { 298 730 #if defined(_WIN32) 299 731 HWND handle; 300 732 HMODULE hInstance = GetModuleHandle(0); 301 733 302 - const TCHAR windowClass[] = _T("BitEngine"); 734 + const TCHAR windowClass[] = _T("bitEngine"); 303 735 304 736 WNDCLASSEX wc; 305 737 wc.cbSize = sizeof(WNDCLASSEX); ··· 344 776 345 777 HDC hdc = GetDC(handle); 346 778 779 + init_opengl_extensions(); 780 + 781 + int pixel_format_attribs[] = { 782 + WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, 783 + WGL_SUPPORT_OPENGL_ARB, GL_TRUE, 784 + WGL_DOUBLE_BUFFER_ARB, GL_TRUE, 785 + WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, 786 + WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, 787 + WGL_COLOR_BITS_ARB, 32, 788 + WGL_DEPTH_BITS_ARB, 24, 789 + WGL_STENCIL_BITS_ARB, 8, 790 + 0 791 + }; 792 + 793 + int pixel_format; 794 + UINT num_formats; 795 + wglChoosePixelFormatARB(hdc, pixel_format_attribs, 0, 1, &pixel_format, &num_formats); 796 + if (!num_formats) { 797 + fprintf(stderr, "Failed to set Modern OpenGL pixel format\n"); 798 + DestroyWindow(handle); 799 + return -1; 800 + } 801 + 347 802 PIXELFORMATDESCRIPTOR pfd; 348 - pfd.nSize = sizeof(pfd); 349 - pfd.nVersion = 1; 350 - pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 351 - pfd.iPixelType = PFD_TYPE_RGBA; 352 - pfd.cColorBits = 32; 353 - pfd.cDepthBits = 24; 354 - pfd.cStencilBits = 8; 355 - int pixelFormat = ChoosePixelFormat(hdc, &pfd); 356 - SetPixelFormat(hdc, pixelFormat, &pfd); 803 + DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd); 804 + if (!SetPixelFormat(hdc, pixel_format, &pfd)) { 805 + fprintf(stderr, "Failed to set Modern OpenGL pixel format\n"); 806 + DestroyWindow(handle); 807 + return -1; 808 + } 809 + 810 + int gl_attribs[] = { 811 + WGL_CONTEXT_MAJOR_VERSION_ARB, 2, 812 + WGL_CONTEXT_MINOR_VERSION_ARB, 1, 813 + WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 814 + 0 815 + }; 357 816 358 - HGLRC hglrc = wglCreateContext(hdc); 817 + HGLRC hglrc = wglCreateContextAttribsARB(hdc, 0, gl_attribs); 818 + if (!hglrc) { 819 + fprintf(stderr, "Failed to create Modern OpenGL context\n"); 820 + DestroyWindow(handle); 821 + return -1; 822 + } 359 823 wglMakeCurrent(hdc, hglrc); 360 824 361 825 // ctx = malloc(sizeof(*ctx)); 362 826 be_Window* window = &(ctx->window); 363 827 window->handle = handle; 364 - window->hInstance = hInstance; 365 - window->devContext = hdc; 366 - window->glContext = hglrc; 828 + window->dev_context = hdc; 367 829 368 830 ShowWindow(handle, SW_SHOWDEFAULT); 369 831 UpdateWindow(handle); ··· 446 908 XMapRaised(dpy, handle); 447 909 XStoreName(dpy, handle, conf->window.title); 448 910 #endif 911 + 912 + if (_init_gl(ctx) != BITE_OK) { 913 + fprintf(stderr, "Failed to init OpenGL loader\n"); 914 + return NULL; 915 + } 916 + _setup_gl(); 917 + _close_gl(); 918 + 919 + 449 920 ctx->window.width = conf->window.width; 450 921 ctx->window.height = conf->window.height; 451 - return 0; 922 + return BITE_OK; 452 923 } 453 - 924 + #endif 454 925 void _poll_events(be_Context* ctx) { 455 926 #if defined(_WIN32) 456 927 MSG message; ··· 513 984 if (fn) fn(ctx, &e); 514 985 } 515 986 #endif 987 + } 988 + 989 + // OpenGL loader 990 + 991 + #if !defined(__APPLE__) && !defined(__HAIKU__) 992 + void* (*_proc_address)(const char*); 993 + #endif 994 + 995 + #define GET_GL_PROC(name)\ 996 + name = (name##Proc*)_get_proc(#name) 997 + 998 + void* _get_proc(const char*); 999 + 1000 + BITE_RESULT _load_gl(void) { 1001 + #if defined(_WIN32) 1002 + _gl_sym = LoadLibrary("opengl32.dll"); 1003 + if (_gl_sym == NULL) { 1004 + fprintf(stderr, "Failed to load OpenGL32.dll\n"); 1005 + return BITE_ERROR; 1006 + } 1007 + #else 1008 + #if defined(__APPLE__) 1009 + const char *names[] = { 1010 + "../Frameworks/OpenGL.framework/OpenGL", 1011 + "/Library/Frameworks/OpenGL.framework/Opengl", 1012 + "/System/Library/Frameworks/OpenGL.framework/OpenGL", 1013 + "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", 1014 + NULL, 1015 + }; 1016 + #else 1017 + const char *names[] = { 1018 + "libGL.so.1", 1019 + "libGL.so", 1020 + NULL, 1021 + }; 1022 + #endif 1023 + int index; 1024 + for (index = 0; names[index] != NULL; ++index) { 1025 + _gl_sym = dlopen(names[index], RTLD_LAZY | RTLD_GLOBAL); 1026 + if (_gl_sym != NULL) { 1027 + #if defined(__APPLE__) || defined(__HAIKU__) 1028 + return BITE_OK; 1029 + #else 1030 + _proc_address = (void*(*)(const i8*))dlsym(_gl_sym, "glXGetProcAddress"); 1031 + return _proc_address != NULL ? BITE_OK : BITE_ERROR; 1032 + #endif 1033 + break; 1034 + } 1035 + } 1036 + #endif 1037 + return BITE_OK; 1038 + } 1039 + 1040 + void _close_gl(void) { 1041 + if (_gl_sym != NULL) { 1042 + #if defined(_WIN32) 1043 + FreeLibrary(_gl_sym); 1044 + #elif defined(__EMSCRIPTEN_) 1045 + #else 1046 + dlclose(_gl_sym); 1047 + #endif 1048 + _gl_sym = NULL; 1049 + } 1050 + } 1051 + 1052 + void _setup_gl(void) { 1053 + const char* version = glGetString(GL_VERSION); 1054 + const char* glsl = glGetString(GL_SHADING_LANGUAGE_VERSION); 1055 + if (!version) { 1056 + fprintf(stderr, "Failed to get OpenGL version\n"); 1057 + } 1058 + const char* prefixes[] = { 1059 + "OpenGL ES-CM ", 1060 + "OpenGL ES-CL ", 1061 + "OpenGL ES ", 1062 + NULL 1063 + }; 1064 + 1065 + if (version) { 1066 + const char* ver = version; 1067 + for (int i = 0; prefixes[i] != NULL; i++) { 1068 + if (strncmp(ver, prefixes[i], strlen(prefixes[i])) == 0) { 1069 + ver += strlen(prefixes[i]); 1070 + fprintf(stdout, "Using OpenGL ES\n"); 1071 + } 1072 + } 1073 + } 1074 + 1075 + fprintf(stdout, "OpenGL: %s\n", version); 1076 + 1077 + GET_GL_PROC(glGenVertexArrays); 1078 + GET_GL_PROC(glBindVertexArray); 1079 + GET_GL_PROC(glDeleteVertexArrays); 1080 + 1081 + GET_GL_PROC(glVertexAttribPointer); 1082 + GET_GL_PROC(glEnableVertexAttribArray); 1083 + GET_GL_PROC(glDisableVertexAttribArray); 1084 + 1085 + GET_GL_PROC(glGenBuffers); 1086 + GET_GL_PROC(glBindBuffer); 1087 + GET_GL_PROC(glDeleteBuffers); 1088 + GET_GL_PROC(glBufferData); 1089 + GET_GL_PROC(glBufferSubData); 1090 + 1091 + GET_GL_PROC(glCreateShader); 1092 + GET_GL_PROC(glShaderSource); 1093 + GET_GL_PROC(glCompileShader); 1094 + GET_GL_PROC(glGetShaderiv); 1095 + GET_GL_PROC(glGetShaderInfoLog); 1096 + GET_GL_PROC(glCreateProgram); 1097 + GET_GL_PROC(glAttachShader); 1098 + GET_GL_PROC(glLinkProgram); 1099 + GET_GL_PROC(glGetProgramiv); 1100 + GET_GL_PROC(glGetProgramInfoLog); 1101 + } 1102 + 1103 + BITE_BOOL _load_procs(void) { 1104 + // GET_GL_PROC(glClear); 1105 + // GET_GL_PROC(glClearColor); 1106 + // GET_GL_PROC(glViewport); 1107 + 1108 + 1109 + 1110 + // GET_GL_PROC(glDrawArrays); 1111 + 1112 + return BITE_TRUE; 1113 + } 1114 + 1115 + static inline void* _get_proc(const char* name) { 1116 + void* sym = NULL; 1117 + if (_gl_sym == NULL) return sym; 1118 + #if !defined(__APPLE__) && !defined(__HAIKU__) 1119 + if (_proc_address != NULL) sym = _proc_address(name); 1120 + #endif 1121 + if (sym == NULL) { 1122 + #if defined(_WIN32) || defined(__CYGWWIN__) 1123 + sym = (void*)GetProcAddress(_gl_sym, name); 1124 + #else 1125 + sym = (void*)dlsym(_gl_sym, name); 1126 + #endif 1127 + } 1128 + return sym; 516 1129 }
+1 -1
bite.h
··· 109 109 110 110 BITE_API void bite_register_callback(be_Context* ctx, int type, be_EventCallback callback); 111 111 112 - BITE_API BITE_BOOL bite_should_close(be_Context* ctx); 112 + BITE_API int bite_should_close(be_Context* ctx); 113 113 BITE_API void bite_set_should_close(be_Context* ctx, int should_close); 114 114 115 115 BITE_API void bite_poll_events(be_Context* ctx);