added context pointer to window userdata (Win32);

Canoi 0a90a068 f08faec2

Changed files
+89 -45
+57 -27
bite.c
··· 5 5 #include <tchar.h> 6 6 #include <gl/GL.h> 7 7 #include <gl/GLU.h> 8 - #elif defined(__EMSCRIPTEN__) 9 - 10 - #include <GL/gl.h> 11 - // #include <GLES2/gl2.h> 12 8 #else 13 9 #if defined(__EMSCRIPTEN__) 14 10 #include <emscripten.h> ··· 53 49 54 50 static be_Context _context; 55 51 56 - be_Context* _create_context(const be_Config* conf); 52 + int _init_context(be_Context* ctx, const be_Config* conf); 57 53 void _poll_events(be_Context* ctx); 58 54 59 55 #if defined(_WIN32) 60 56 LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { 57 + be_Context* ctx = (be_Context*)GetWindowLongPtr(hwnd, GWLP_USERDATA); 61 58 be_Event e; 62 59 e.type = 0; 63 60 switch (msg) { 61 + case WM_CREATE: return 0; 64 62 case WM_CLOSE: { 65 63 e.type = BITE_WINDOW_CLOSE; 66 64 } ··· 72 70 e.key.keycode = (int)wParam; 73 71 } 74 72 break; 73 + case WM_SYSKEYUP: 74 + case WM_KEYUP: { 75 + e.type = BITE_KEY_RELEASED; 76 + e.key.keycode = (int)wParam; 77 + } 78 + break; 75 79 } 76 - be_EventCallback fn = _callbacks[e.type]; 77 - if (fn) fn(&e); 80 + be_EventCallback fn = NULL; 81 + if (ctx) fn = ctx->callbacks[e.type]; 82 + if (fn) fn(ctx, &e); 78 83 return DefWindowProc(hwnd, msg, wParam, lParam); 79 84 } 80 85 #endif 81 86 87 + void bite_simple_triangle(void) { 88 + glClearColor(0.3f, 0.4f, 0.4f, 1.f); 89 + glClear(GL_COLOR_BUFFER_BIT); 90 + glBegin(GL_TRIANGLES); 91 + glColor3f(1.f, 0.f, 0.f); 92 + glVertex2f(0.f, 0.5f); 93 + glColor3f(0.f, 1.f, 0.f); 94 + glVertex2f(-0.5f, -0.5f); 95 + glColor3f(0.f, 0.f, 1.f); 96 + glVertex2f(0.5f, -0.5f); 97 + glEnd(); 98 + } 99 + 82 100 be_Config bite_init_config(const char* title, int w, int h) { 83 101 be_Config c = {0}; 84 102 title = title != NULL ? title : "bitEngine"; ··· 89 107 } 90 108 91 109 be_Context* bite_create(const be_Config* conf) { 92 - be_Context* ctx = _create_context(conf); 93 - if (!ctx) return ctx; 110 + be_Context* ctx = malloc(sizeof(*ctx)); 111 + if (!ctx) { 112 + fprintf(stderr, "Failed to alloc memory for context\n"); 113 + return NULL; 114 + } 94 115 ctx->running = 1; 95 - ctx->window.width = conf->window.width; 96 - ctx->window.height = conf->window.height; 97 116 for (int i = 0; i < BITE_EVENTS; i++) { 98 117 ctx->callbacks[i] = NULL; 99 118 } 119 + if (_init_context(ctx, conf) < 0) { 120 + free(ctx); 121 + return NULL; 122 + } 100 123 return ctx; 101 124 } 102 125 ··· 154 177 /********************* 155 178 * Window 156 179 *********************/ 180 + void bite_set_window_width(be_Context* ctx, int width) { 181 + 182 + } 183 + 184 + 157 185 int bite_get_window_width(be_Context* ctx) { 158 186 if (!ctx) return -1; 159 187 return ctx->window.width; ··· 191 219 /********************* 192 220 * Internal 193 221 *********************/ 194 - be_Context* _create_context(const be_Config* conf) { 195 - be_Context* ctx = NULL; 222 + int _init_context(be_Context* ctx, const be_Config* conf) { 196 223 #if defined(_WIN32) 197 224 HWND handle; 198 225 HMODULE hInstance = GetModuleHandle(0); ··· 219 246 _T("bit Engine"), 220 247 NULL); 221 248 fprintf(stderr, "Error code: %d\n", GetLastError()); 222 - return window; 249 + return -1; 223 250 } 224 251 225 252 handle = CreateWindow( 226 253 windowClass, 227 - title, 254 + conf->window.title, 228 255 WS_OVERLAPPEDWINDOW, 229 256 CW_USEDEFAULT, CW_USEDEFAULT, 230 - w, h, 231 - NULL, NULL, hInstance, NULL 257 + conf->window.width, conf->window.height, 258 + NULL, NULL, hInstance, ctx 232 259 ); 233 260 if (!handle) { 234 261 MessageBox(NULL, ··· 236 263 _T("bit Engine"), 237 264 NULL); 238 265 fprintf(stderr, "Error code: %d\n", GetLastError()); 239 - return window; 266 + return -1; 240 267 } 268 + SetWindowLongPtr(handle, GWLP_USERDATA, (LONG_PTR)ctx); 241 269 242 270 HDC hdc = GetDC(handle); 243 271 ··· 255 283 HGLRC hglrc = wglCreateContext(hdc); 256 284 wglMakeCurrent(hdc, hglrc); 257 285 258 - ctx = malloc(sizeof(*ctx)); 286 + // ctx = malloc(sizeof(*ctx)); 259 287 be_Window* window = &(ctx->window); 260 288 window->handle = handle; 261 289 window->hInstance = hInstance; ··· 265 293 ShowWindow(handle, SW_SHOWDEFAULT); 266 294 UpdateWindow(handle); 267 295 #elif defined(__EMSCRIPTEN__) 296 + be_Window* window = &(ctx->window); 268 297 EmscriptenWebGLContextAttributes attr; 269 298 emscripten_webgl_init_context_attributes(&attr); 270 299 attr.alpha = 0; 271 - ctx = malloc(sizeof(*ctx)); 272 300 ctx->window.handle = emscripten_webgl_create_context("#canvas", &attr); 273 301 emscripten_webgl_make_context_current(window->handle); 274 302 #else ··· 279 307 dpy = XOpenDisplay(NULL); 280 308 if (dpy == NULL) { 281 309 fprintf(stderr, "Could not open X11 display\n"); 282 - return ctx; 310 + return -1; 283 311 } 284 312 scr = DefaultScreenOfDisplay(dpy); 285 313 scrId = DefaultScreen(dpy); ··· 289 317 if (majorGLX <= 1 && minorGLX < 2) { 290 318 fprintf(stderr, "GLX 1.2 or greater is required\n"); 291 319 XCloseDisplay(dpy); 292 - return ctx; 320 + return -1; 293 321 } else { 294 322 fprintf(stdout, "GLX version: %d.%d\n", majorGLX, minorGLX); 295 323 } ··· 310 338 if (!visual) { 311 339 fprintf(stderr, "Failed to create correct visual window\n"); 312 340 XCloseDisplay(dpy); 313 - return ctx; 341 + return -1; 314 342 } 315 343 316 344 XSetWindowAttributes windowAttribs; ··· 324 352 if (!handle) { 325 353 fprintf(stderr, "Failed to create X11 window\n"); 326 354 XCloseDisplay(dpy); 327 - return ctx; 355 + return -1; 328 356 } 329 357 GLXContext context = glXCreateContext(dpy, visual, NULL, GL_TRUE); 330 358 if (!context) { 331 359 fprintf(stderr, "Failed to create GLX context\n"); 332 360 XDestroyWindow(dpy, handle); 333 361 XCloseDisplay(dpy); 334 - return ctx; 362 + return -1; 335 363 } 336 364 glXMakeCurrent(dpy, handle, context); 337 - ctx = malloc(sizeof(*ctx)); 338 365 be_Window* window = &(ctx->window); 339 366 window->display = dpy; 340 367 window->handle = handle; ··· 344 371 XMapRaised(dpy, handle); 345 372 XStoreName(dpy, handle, conf->window.title); 346 373 #endif 347 - return ctx; 374 + ctx->window.width = conf->window.width; 375 + ctx->window.height = conf->window.height; 376 + return 0; 348 377 } 349 378 350 379 void _poll_events(be_Context* ctx) { ··· 354 383 TranslateMessage(&message); 355 384 DispatchMessage(&message); 356 385 } 386 + #elif defined(__EMSCRIPTEN__) 357 387 #else 358 388 XEvent ev; 359 389 be_Window* window = &(ctx->window);
+6
bite.h
··· 79 79 #if defined(__cplusplus) 80 80 extern "C" { 81 81 #endif 82 + 83 + BITE_API void bite_simple_triangle(void); 82 84 83 85 /********************* 84 86 * Core ··· 100 102 /********************* 101 103 * Window 102 104 *********************/ 105 + BITE_API void bite_set_window_width(be_Context* ctx, int width); 106 + BITE_API void bite_set_window_height(be_Context* ctx, int height); 107 + BITE_API void bite_set_window_size(be_Context* ctx, int width, int height); 108 + 103 109 BITE_API int bite_get_window_width(be_Context* ctx); 104 110 BITE_API int bite_get_window_height(be_Context* ctx); 105 111 BITE_API void bite_get_window_size(int* w, int* h);
+8 -1
index.html
··· 4 4 <meta charset="UTF-8"> 5 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 - <title>BitEngine</title> 7 + <title>bitEngine</title> 8 + <style> 9 + #canvas { 10 + border: 1px solid black; 11 + display: block; 12 + margin: 0 auto; 13 + } 14 + </style> 8 15 </head> 9 16 <body> 10 17 <canvas id="canvas" width="640px" height="380px"></canvas>
+18 -17
main.c
··· 1 1 #include "bite.h" 2 2 3 - #include <GL/gl.h> 3 + #if defined(__EMSCRIPTEN__) 4 + #include <emscripten.h> 5 + #endif 6 + 7 + // #include <GL/gl.h> 4 8 5 9 void key_pressed(be_Context* ctx, be_Event* ev) { 6 10 printf("Pressed: %x\n", ev->key.keycode); 7 - #if defined(_WIN32) 8 - if (ev->key.keycode == VK_ESCAPE) bite_set_should_close(ctx, 1); 9 - #else 10 - if (ev->key.keycode == 0x09) bite_set_should_close(ctx, 1); 11 - #endif 11 + if (ev->key.keycode == BITEK_ESCAPE) bite_set_should_close(ctx, 1); 12 12 } 13 13 14 14 void quit_callback(be_Context* ctx, be_Event* ev) { ··· 19 19 // printf("Entering render function\n"); 20 20 be_Context* ctx = (be_Context*)data; 21 21 bite_poll_events(ctx); 22 - glClearColor(0.3f, 0.4f, 0.4f, 1.f); 23 - glClear(GL_COLOR_BUFFER_BIT); 24 - glBegin(GL_TRIANGLES); 25 - glColor3f(1.f, 0.f, 0.f); 26 - glVertex2f(0.f, 0.5f); 27 - glColor3f(0.f, 1.f, 0.f); 28 - glVertex2f(-0.5f, -0.5f); 29 - glColor3f(0.f, 0.f, 1.f); 30 - glVertex2f(0.5f, -0.5f); 31 - glEnd(); 22 + // glClearColor(0.3f, 0.4f, 0.4f, 1.f); 23 + // glClear(GL_COLOR_BUFFER_BIT); 24 + // glBegin(GL_TRIANGLES); 25 + // glColor3f(1.f, 0.f, 0.f); 26 + // glVertex2f(0.f, 0.5f); 27 + // glColor3f(0.f, 1.f, 0.f); 28 + // glVertex2f(-0.5f, -0.5f); 29 + // glColor3f(0.f, 0.f, 1.f); 30 + // glVertex2f(0.5f, -0.5f); 31 + // glEnd(); 32 + bite_simple_triangle(); 32 33 bite_swap(ctx); 33 34 } 34 35 ··· 36 37 be_Config c = bite_init_config("bitEngine", 640, 380); 37 38 be_Context* ctx = bite_create(&c); 38 39 #if defined(__EMSCRIPTEN__) 39 - emscripten_set_main_loop_arg(main_loop, 0, 1, ctx); 40 + emscripten_set_main_loop_arg(main_loop, ctx, 0, 1); 40 41 #else 41 42 bite_register_callback(ctx, BITE_WINDOW_CLOSE, quit_callback); 42 43 bite_register_callback(ctx, BITE_KEY_PRESSED, key_pressed);