2 MiniGLUT - minimal GLUT subset without dependencies
3 Copyright (C) 2020 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
18 #ifdef MINIGLUT_USE_LIBC
23 static void mglut_sincosf(float angle, float *sptr, float *cptr);
24 static float mglut_atan(float x);
27 #define PI 3.1415926536f
32 #include <X11/cursorfont.h>
36 #ifndef GLX_SAMPLE_BUFFERS_ARB
37 #define GLX_SAMPLE_BUFFERS_ARB 100000
38 #define GLX_SAMPLES_ARB 100001
40 #ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB
41 #define GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20b2
45 static Window win, root;
47 static GLXContext ctx;
48 static Atom xa_wm_proto, xa_wm_del_win;
49 static unsigned int evmask;
56 static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
58 static HINSTANCE hinst;
64 #error unsupported platform
71 int rsize, gsize, bsize, asize;
79 static void create_window(const char *title);
80 static void get_window_pos(int *x, int *y);
81 static void get_window_size(int *w, int *h);
82 static void get_screen_size(int *scrw, int *scrh);
84 static long get_msec(void);
85 static void panic(const char *msg);
86 static void sys_exit(int status);
87 static int sys_write(int fd, const void *buf, int count);
90 static int init_x, init_y, init_width = 256, init_height = 256;
91 static unsigned int init_mode;
93 static struct ctx_info ctx_info;
94 static int cur_cursor = GLUT_CURSOR_INHERIT;
96 static glut_cb cb_display;
97 static glut_cb cb_idle;
98 static glut_cb_reshape cb_reshape;
99 static glut_cb_state cb_vis, cb_entry;
100 static glut_cb_keyb cb_keydown, cb_keyup;
101 static glut_cb_special cb_skeydown, cb_skeyup;
102 static glut_cb_mouse cb_mouse;
103 static glut_cb_motion cb_motion, cb_passive;
104 static glut_cb_sbmotion cb_sball_motion, cb_sball_rotate;
105 static glut_cb_sbbutton cb_sball_button;
107 static int win_width, win_height;
110 static int upd_pending;
114 void glutInit(int *argc, char **argv)
117 if(!(dpy = XOpenDisplay(0))) {
118 panic("Failed to connect to the X server\n");
120 scr = DefaultScreen(dpy);
121 root = RootWindow(dpy, scr);
122 xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", False);
123 xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
125 evmask = ExposureMask | StructureNotifyMask;
130 hinst = GetModuleHandle(0);
132 wc.cbSize = sizeof wc;
133 wc.hbrBackground = GetStockObject(BLACK_BRUSH);
134 wc.hCursor = LoadCursor(0, IDC_ARROW);
135 wc.hIcon = wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
136 wc.hInstance = hinst;
137 wc.lpfnWndProc = handle_message;
138 wc.lpszClassName = "MiniGLUT";
139 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
140 if(!RegisterClassEx(&wc)) {
141 panic("Failed to register \"MiniGLUT\" window class\n");
146 void glutInitWindowPosition(int x, int y)
152 void glutInitWindowSize(int xsz, int ysz)
158 void glutInitDisplayMode(unsigned int mode)
163 void glutCreateWindow(const char *title)
165 create_window(title);
173 void glutMainLoop(void)
180 void glutPostRedisplay(void)
185 #define UPD_EVMASK(x) \
192 if(win) XSelectInput(dpy, win, evmask); \
196 void glutIdleFunc(glut_cb func)
201 void glutDisplayFunc(glut_cb func)
206 void glutReshapeFunc(glut_cb_reshape func)
211 void glutVisibilityFunc(glut_cb_state func)
215 UPD_EVMASK(VisibilityChangeMask);
219 void glutEntryFunc(glut_cb_state func)
223 UPD_EVMASK(EnterWindowMask | LeaveWindowMask);
227 void glutKeyboardFunc(glut_cb_keyb func)
231 UPD_EVMASK(KeyPressMask);
235 void glutKeyboardUpFunc(glut_cb_keyb func)
239 UPD_EVMASK(KeyReleaseMask);
243 void glutSpecialFunc(glut_cb_special func)
247 UPD_EVMASK(KeyPressMask);
251 void glutSpecialUpFunc(glut_cb_special func)
255 UPD_EVMASK(KeyReleaseMask);
259 void glutMouseFunc(glut_cb_mouse func)
263 UPD_EVMASK(ButtonPressMask | ButtonReleaseMask);
267 void glutMotionFunc(glut_cb_motion func)
271 UPD_EVMASK(ButtonMotionMask);
275 void glutPassiveMotionFunc(glut_cb_motion func)
279 UPD_EVMASK(PointerMotionMask);
283 void glutSpaceballMotionFunc(glut_cb_sbmotion func)
285 cb_sball_motion = func;
288 void glutSpaceballRotateFunc(glut_cb_sbmotion func)
290 cb_sball_rotate = func;
293 void glutSpaceballBittonFunc(glut_cb_sbbutton func)
295 cb_sball_button = func;
298 int glutGet(unsigned int s)
303 get_window_pos(&x, &y);
306 get_window_pos(&x, &y);
308 case GLUT_WINDOW_WIDTH:
309 get_window_size(&x, &y);
311 case GLUT_WINDOW_HEIGHT:
312 get_window_size(&x, &y);
314 case GLUT_WINDOW_BUFFER_SIZE:
315 return ctx_info.rsize + ctx_info.gsize + ctx_info.bsize + ctx_info.asize;
316 case GLUT_WINDOW_STENCIL_SIZE:
317 return ctx_info.ssize;
318 case GLUT_WINDOW_DEPTH_SIZE:
319 return ctx_info.zsize;
320 case GLUT_WINDOW_RED_SIZE:
321 return ctx_info.rsize;
322 case GLUT_WINDOW_GREEN_SIZE:
323 return ctx_info.gsize;
324 case GLUT_WINDOW_BLUE_SIZE:
325 return ctx_info.bsize;
326 case GLUT_WINDOW_ALPHA_SIZE:
327 return ctx_info.asize;
328 case GLUT_WINDOW_DOUBLEBUFFER:
329 return ctx_info.dblbuf;
330 case GLUT_WINDOW_RGBA:
332 case GLUT_WINDOW_NUM_SAMPLES:
333 return ctx_info.samples;
334 case GLUT_WINDOW_STEREO:
335 return ctx_info.stereo;
336 case GLUT_WINDOW_SRGB:
337 return ctx_info.srgb;
338 case GLUT_WINDOW_CURSOR:
340 case GLUT_SCREEN_WIDTH:
341 get_screen_size(&x, &y);
343 case GLUT_SCREEN_HEIGHT:
344 get_screen_size(&x, &y);
346 case GLUT_INIT_DISPLAY_MODE:
348 case GLUT_INIT_WINDOW_X:
350 case GLUT_INIT_WINDOW_Y:
352 case GLUT_INIT_WINDOW_WIDTH:
354 case GLUT_INIT_WINDOW_HEIGHT:
356 case GLUT_ELAPSED_TIME:
364 int glutGetModifiers(void)
369 static int is_space(int c)
371 return c == ' ' || c == '\t' || c == '\v' || c == '\n' || c == '\r';
374 static const char *skip_space(const char *s)
376 while(*s && is_space(*s)) s++;
380 int glutExtensionSupported(char *ext)
382 const char *str, *eptr;
384 if(!(str = (const char*)glGetString(GL_EXTENSIONS))) {
389 str = skip_space(str);
390 eptr = skip_space(ext);
391 while(*str && !is_space(*str) && *eptr && *str == *eptr) {
395 if((!*str || is_space(*str)) && !*eptr) {
398 while(*str && !is_space(*str)) str++;
405 void glutSolidSphere(float rad, int slices, int stacks)
408 float x, y, z, s, t, u, v, phi, theta, sintheta, costheta, sinphi, cosphi;
409 float du = 1.0f / (float)slices;
410 float dv = 1.0f / (float)stacks;
413 for(i=0; i<stacks; i++) {
415 for(j=0; j<slices; j++) {
419 s = gray & 1 ? u + du : u;
420 t = gray & 2 ? v + dv : v;
421 theta = s * PI * 2.0f;
423 mglut_sincosf(theta, &sintheta, &costheta);
424 mglut_sincosf(phi, &sinphi, &cosphi);
425 x = sintheta * sinphi;
426 y = costheta * sinphi;
432 glVertex3f(x * rad, y * rad, z * rad);
439 void glutWireSphere(float rad, int slices, int stacks)
441 glPushAttrib(GL_POLYGON_BIT);
442 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
443 glutSolidSphere(rad, slices, stacks);
447 void glutSolidCube(float sz)
449 int i, j, idx, gray, flip, rotx;
450 float vpos[3], norm[3];
451 float rad = sz * 0.5f;
457 idx = (~i & 2) - rotx;
458 norm[0] = norm[1] = norm[2] = 0.0f;
459 norm[idx] = flip ^ ((i >> 1) & 1) ? -1 : 1;
461 vpos[idx] = norm[idx] * rad;
464 vpos[i & 2] = (gray ^ flip) & 1 ? rad : -rad;
465 vpos[rotx + 1] = (gray ^ (rotx << 1)) & 2 ? rad : -rad;
466 glTexCoord2f(gray & 1, gray >> 1);
473 void glutWireCube(float sz)
475 glPushAttrib(GL_POLYGON_BIT);
476 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
481 static void draw_cylinder(float rbot, float rtop, float height, int slices, int stacks)
484 float x, y, z, s, t, u, v, theta, phi, sintheta, costheta, sinphi, cosphi, rad;
485 float du = 1.0f / (float)slices;
486 float dv = 1.0f / (float)stacks;
489 phi = mglut_atan((rad < 0 ? -rad : rad) / height);
490 mglut_sincosf(phi, &sinphi, &cosphi);
493 for(i=0; i<stacks; i++) {
495 for(j=0; j<slices; j++) {
499 s = gray & 2 ? u + du : u;
500 t = gray & 1 ? v + dv : v;
501 rad = rbot + (rtop - rbot) * t;
502 theta = s * PI * 2.0f;
503 mglut_sincosf(theta, &sintheta, &costheta);
505 x = sintheta * cosphi;
506 y = costheta * cosphi;
512 glVertex3f(sintheta * rad, costheta * rad, t * height);
519 void glutSolidCone(float base, float height, int slices, int stacks)
521 draw_cylinder(base, 0, height, slices, stacks);
524 void glutWireCone(float base, float height, int slices, int stacks)
526 glPushAttrib(GL_POLYGON_BIT);
527 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
528 glutSolidCone(base, height, slices, stacks);
532 void glutSolidCylinder(float rad, float height, int slices, int stacks)
534 draw_cylinder(rad, rad, height, slices, stacks);
537 void glutWireCylinder(float rad, float height, int slices, int stacks)
539 glPushAttrib(GL_POLYGON_BIT);
540 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
541 glutSolidCylinder(rad, height, slices, stacks);
545 void glutSolidTorus(float inner_rad, float outer_rad, int sides, int rings)
548 float x, y, z, s, t, u, v, phi, theta, sintheta, costheta, sinphi, cosphi;
549 float du = 1.0f / (float)rings;
550 float dv = 1.0f / (float)sides;
553 for(i=0; i<rings; i++) {
555 for(j=0; j<sides; j++) {
559 s = gray & 1 ? u + du : u;
560 t = gray & 2 ? v + dv : v;
561 theta = s * PI * 2.0f;
563 mglut_sincosf(theta, &sintheta, &costheta);
564 mglut_sincosf(phi, &sinphi, &cosphi);
565 x = sintheta * sinphi;
566 y = costheta * sinphi;
573 x = x * inner_rad + sintheta * outer_rad;
574 y = y * inner_rad + costheta * outer_rad;
583 void glutWireTorus(float inner_rad, float outer_rad, int sides, int rings)
585 glPushAttrib(GL_POLYGON_BIT);
586 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
587 glutSolidTorus(inner_rad, outer_rad, sides, rings);
591 void glutSolidTeapot(float size)
595 void glutWireTeapot(float size)
601 /* --------------- UNIX/X11 implementation ----------------- */
603 static void handle_event(XEvent *ev);
605 void glutMainLoopEvent(void)
610 panic("display callback not set");
613 if(!upd_pending && !cb_idle) {
614 XNextEvent(dpy, &ev);
618 while(XPending(dpy)) {
619 XNextEvent(dpy, &ev);
628 if(upd_pending && mapped) {
634 static KeySym translate_keysym(KeySym sym)
655 static void handle_event(XEvent *ev)
666 case ConfigureNotify:
667 if(cb_reshape && (ev->xconfigure.width != win_width || ev->xconfigure.height != win_height)) {
668 win_width = ev->xconfigure.width;
669 win_height = ev->xconfigure.height;
670 cb_reshape(ev->xconfigure.width, ev->xconfigure.height);
675 if(ev->xclient.message_type == xa_wm_proto) {
676 if(ev->xclient.data.l[0] == xa_wm_del_win) {
688 modstate = ev->xkey.state & (ShiftMask | ControlMask | Mod1Mask);
689 if(!(sym = XLookupKeysym(&ev->xkey, 0))) {
692 sym = translate_keysym(sym);
694 if(ev->type == KeyPress) {
695 if(cb_keydown) cb_keydown((unsigned char)sym, ev->xkey.x, ev->xkey.y);
697 if(cb_keyup) cb_keyup((unsigned char)sym, ev->xkey.x, ev->xkey.y);
700 if(ev->type == KeyPress) {
701 if(cb_skeydown) cb_skeydown(sym, ev->xkey.x, ev->xkey.y);
703 if(cb_skeyup) cb_skeyup(sym, ev->xkey.x, ev->xkey.y);
710 modstate = ev->xbutton.state & (ShiftMask | ControlMask | Mod1Mask);
712 int bn = ev->xbutton.button - Button1;
713 cb_mouse(bn, ev->type == ButtonPress ? GLUT_DOWN : GLUT_UP,
714 ev->xbutton.x, ev->xbutton.y);
719 if(ev->xmotion.state & (Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask)) {
720 if(cb_motion) cb_motion(ev->xmotion.x, ev->xmotion.y);
722 if(cb_passive) cb_passive(ev->xmotion.x, ev->xmotion.y);
726 case VisibilityNotify:
728 cb_vis(ev->xvisibility.state == VisibilityFullyObscured ? GLUT_NOT_VISIBLE : GLUT_VISIBLE);
732 if(cb_entry) cb_entry(GLUT_ENTERED);
735 if(cb_entry) cb_entry(GLUT_LEFT);
740 void glutSwapBuffers(void)
742 glXSwapBuffers(dpy, win);
745 void glutPositionWindow(int x, int y)
747 XMoveWindow(dpy, win, x, y);
750 void glutReshapeWindow(int xsz, int ysz)
752 XResizeWindow(dpy, win, xsz, ysz);
755 void glutFullScreen(void)
760 void glutSetWindowTitle(const char *title)
763 if(!XStringListToTextProperty((char**)&title, 1, &tprop)) {
766 XSetWMName(dpy, win, &tprop);
770 void glutSetIconTitle(const char *title)
773 if(!XStringListToTextProperty((char**)&title, 1, &tprop)) {
776 XSetWMIconName(dpy, win, &tprop);
780 void glutSetCursor(int cidx)
785 case GLUT_CURSOR_LEFT_ARROW:
786 cur = XCreateFontCursor(dpy, XC_left_ptr);
788 case GLUT_CURSOR_INHERIT:
790 case GLUT_CURSOR_NONE:
796 XDefineCursor(dpy, win, cur);
800 static XVisualInfo *choose_visual(unsigned int mode)
807 if(mode & GLUT_DOUBLE) {
808 *aptr++ = GLX_DOUBLEBUFFER;
811 if(mode & GLUT_INDEX) {
812 *aptr++ = GLX_BUFFER_SIZE;
816 *aptr++ = GLX_RED_SIZE; *aptr++ = 4;
817 *aptr++ = GLX_GREEN_SIZE; *aptr++ = 4;
818 *aptr++ = GLX_BLUE_SIZE; *aptr++ = 4;
820 if(mode & GLUT_ALPHA) {
821 *aptr++ = GLX_ALPHA_SIZE;
824 if(mode & GLUT_DEPTH) {
825 *aptr++ = GLX_DEPTH_SIZE;
828 if(mode & GLUT_STENCIL) {
829 *aptr++ = GLX_STENCIL_SIZE;
832 if(mode & GLUT_ACCUM) {
833 *aptr++ = GLX_ACCUM_RED_SIZE; *aptr++ = 1;
834 *aptr++ = GLX_ACCUM_GREEN_SIZE; *aptr++ = 1;
835 *aptr++ = GLX_ACCUM_BLUE_SIZE; *aptr++ = 1;
837 if(mode & GLUT_STEREO) {
838 *aptr++ = GLX_STEREO;
840 if(mode & GLUT_SRGB) {
841 *aptr++ = GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB;
843 if(mode & GLUT_MULTISAMPLE) {
844 *aptr++ = GLX_SAMPLE_BUFFERS_ARB;
846 *aptr++ = GLX_SAMPLES_ARB;
853 return glXChooseVisual(dpy, scr, attr);
855 while(!(vi = glXChooseVisual(dpy, scr, attr)) && *samples) {
864 static void create_window(const char *title)
866 XSetWindowAttributes xattr;
868 unsigned int xattr_mask;
869 unsigned int mode = init_mode;
871 if(!(vi = choose_visual(mode))) {
873 if(!(vi = choose_visual(mode))) {
874 panic("Failed to find compatible visual\n");
878 if(!(ctx = glXCreateContext(dpy, vi, 0, True))) {
880 panic("Failed to create OpenGL context\n");
883 glXGetConfig(dpy, vi, GLX_RED_SIZE, &ctx_info.rsize);
884 glXGetConfig(dpy, vi, GLX_GREEN_SIZE, &ctx_info.gsize);
885 glXGetConfig(dpy, vi, GLX_BLUE_SIZE, &ctx_info.bsize);
886 glXGetConfig(dpy, vi, GLX_ALPHA_SIZE, &ctx_info.asize);
887 glXGetConfig(dpy, vi, GLX_DEPTH_SIZE, &ctx_info.zsize);
888 glXGetConfig(dpy, vi, GLX_STENCIL_SIZE, &ctx_info.ssize);
889 glXGetConfig(dpy, vi, GLX_DOUBLEBUFFER, &ctx_info.dblbuf);
890 glXGetConfig(dpy, vi, GLX_STEREO, &ctx_info.stereo);
891 glXGetConfig(dpy, vi, GLX_SAMPLES_ARB, &ctx_info.samples);
892 glXGetConfig(dpy, vi, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, &ctx_info.srgb);
894 xattr.background_pixel = BlackPixel(dpy, scr);
895 xattr.colormap = XCreateColormap(dpy, root, vi->visual, AllocNone);
896 xattr_mask = CWBackPixel | CWColormap;
897 if(!(win = XCreateWindow(dpy, root, init_x, init_y, init_width, init_height, 0,
898 vi->depth, InputOutput, vi->visual, xattr_mask, &xattr))) {
900 glXDestroyContext(dpy, ctx);
901 panic("Failed to create window\n");
905 XSelectInput(dpy, win, evmask);
907 glutSetWindowTitle(title);
908 glutSetIconTitle(title);
909 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
910 XMapWindow(dpy, win);
912 glXMakeCurrent(dpy, win, ctx);
915 static void get_window_pos(int *x, int *y)
917 XWindowAttributes wattr;
918 XGetWindowAttributes(dpy, win, &wattr);
923 static void get_window_size(int *w, int *h)
925 XWindowAttributes wattr;
926 XGetWindowAttributes(dpy, win, &wattr);
931 static void get_screen_size(int *scrw, int *scrh)
933 XWindowAttributes wattr;
934 XGetWindowAttributes(dpy, root, &wattr);
936 *scrh = wattr.height;
938 #endif /* BUILD_X11 */
941 /* --------------- windows implementation ----------------- */
943 static int reshape_pending;
945 static void update_modkeys(void);
946 static int translate_vkey(int vkey);
947 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam);
949 #ifdef MINIGLUT_WINMAIN
950 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, char *cmdline, int showcmd)
953 char *argv[] = { "miniglut.exe", 0 };
954 return main(argc, argv);
958 void glutMainLoopEvent(void)
963 panic("display callback not set");
966 if(reshape_pending && cb_reshape) {
968 get_window_size(&win_width, &win_height);
969 cb_reshape(win_width, win_height);
972 if(!upd_pending && !cb_idle) {
973 GetMessage(&msg, 0, 0, 0);
974 TranslateMessage(&msg);
975 DispatchMessage(&msg);
978 while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
979 TranslateMessage(&msg);
980 DispatchMessage(&msg);
988 if(upd_pending && mapped) {
994 void glutSwapBuffers(void)
999 void glutPositionWindow(int x, int y)
1002 GetWindowRect(win, &rect);
1003 MoveWindow(win, x, y, rect.right - rect.left, rect.bottom - rect.top, 1);
1006 void glutReshapeWindow(int xsz, int ysz)
1009 GetWindowRect(win, &rect);
1010 MoveWindow(win, rect.left, rect.top, xsz, ysz, 1);
1013 void glutFullScreen(void)
1018 void glutSetWindowTitle(const char *title)
1020 SetWindowText(win, title);
1023 void glutSetIconTitle(const char *title)
1027 void glutSetCursor(int cidx)
1030 case GLUT_CURSOR_NONE:
1033 case GLUT_CURSOR_INHERIT:
1034 case GLUT_CURSOR_LEFT_ARROW:
1036 SetCursor(LoadCursor(0, IDC_ARROW));
1042 static void create_window(const char *title)
1045 PIXELFORMATDESCRIPTOR pfd = {0};
1047 if(!(win = CreateWindow("MiniGLUT", title, WS_OVERLAPPEDWINDOW, init_x, init_y,
1048 init_width, init_height, 0, 0, hinst, 0))) {
1049 panic("Failed to create window\n");
1053 pfd.nSize = sizeof pfd;
1055 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1056 if(init_mode & GLUT_STEREO) {
1057 pfd.dwFlags |= PFD_STEREO;
1059 pfd.iPixelType = init_mode & GLUT_INDEX ? PFD_TYPE_COLORINDEX : PFD_TYPE_RGBA;
1060 pfd.cColorBits = 24;
1061 if(init_mode & GLUT_ALPHA) {
1064 if(init_mode & GLUT_ACCUM) {
1065 pfd.cAccumBits = 24;
1067 if(init_mode & GLUT_DEPTH) {
1068 pfd.cDepthBits = 24;
1070 if(init_mode & GLUT_STENCIL) {
1071 pfd.cStencilBits = 8;
1073 pfd.iLayerType = PFD_MAIN_PLANE;
1075 if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) {
1076 panic("Failed to find suitable pixel format\n");
1078 if(!SetPixelFormat(dc, pixfmt, &pfd)) {
1079 panic("Failed to set the selected pixel format\n");
1081 if(!(ctx = wglCreateContext(dc))) {
1082 panic("Failed to create the OpenGL context\n");
1084 wglMakeCurrent(dc, ctx);
1086 DescribePixelFormat(dc, pixfmt, sizeof pfd, &pfd);
1087 ctx_info.rsize = pfd.cRedBits;
1088 ctx_info.gsize = pfd.cGreenBits;
1089 ctx_info.bsize = pfd.cBlueBits;
1090 ctx_info.asize = pfd.cAlphaBits;
1091 ctx_info.zsize = pfd.cDepthBits;
1092 ctx_info.ssize = pfd.cStencilBits;
1093 ctx_info.dblbuf = pfd.dwFlags & PFD_DOUBLEBUFFER ? 1 : 0;
1094 ctx_info.samples = 1; /* TODO */
1095 ctx_info.srgb = 0; /* TODO */
1099 reshape_pending = 1;
1102 static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
1104 static int mouse_x, mouse_y;
1109 if(win) DestroyWindow(win);
1113 wglMakeCurrent(dc, 0);
1114 wglDeleteContext(ctx);
1121 ValidateRect(win, 0);
1125 win_width = lparam & 0xffff;
1126 win_height = lparam >> 16;
1128 reshape_pending = 0;
1129 cb_reshape(win_width, win_height);
1135 if(cb_vis) cb_vis(mapped ? GLUT_VISIBLE : GLUT_NOT_VISIBLE);
1140 key = translate_vkey(wparam);
1143 cb_keydown((unsigned char)key, mouse_x, mouse_y);
1147 cb_skeydown(key, mouse_x, mouse_y);
1154 key = translate_vkey(wparam);
1157 cb_keyup((unsigned char)key, mouse_x, mouse_y);
1161 cb_skeyup(key, mouse_x, mouse_y);
1166 case WM_LBUTTONDOWN:
1167 handle_mbutton(0, 1, wparam, lparam);
1169 case WM_MBUTTONDOWN:
1170 handle_mbutton(1, 1, wparam, lparam);
1172 case WM_RBUTTONDOWN:
1173 handle_mbutton(2, 1, wparam, lparam);
1176 handle_mbutton(0, 0, wparam, lparam);
1179 handle_mbutton(1, 0, wparam, lparam);
1182 handle_mbutton(2, 0, wparam, lparam);
1186 if(wparam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)) {
1187 if(cb_motion) cb_motion(lparam & 0xffff, lparam >> 16);
1189 if(cb_passive) cb_passive(lparam & 0xffff, lparam >> 16);
1194 return DefWindowProc(win, msg, wparam, lparam);
1200 static void update_modkeys(void)
1202 if(GetKeyState(VK_SHIFT)) {
1203 modstate |= GLUT_ACTIVE_SHIFT;
1205 modstate &= ~GLUT_ACTIVE_SHIFT;
1207 if(GetKeyState(VK_CONTROL)) {
1208 modstate |= GLUT_ACTIVE_CTRL;
1210 modstate &= ~GLUT_ACTIVE_CTRL;
1212 if(GetKeyState(VK_MENU)) {
1213 modstate |= GLUT_ACTIVE_ALT;
1215 modstate &= ~GLUT_ACTIVE_ALT;
1219 static int translate_vkey(int vkey)
1222 case VK_PRIOR: return GLUT_KEY_PAGE_UP;
1223 case VK_NEXT: return GLUT_KEY_PAGE_DOWN;
1224 case VK_END: return GLUT_KEY_END;
1225 case VK_HOME: return GLUT_KEY_HOME;
1226 case VK_LEFT: return GLUT_KEY_LEFT;
1227 case VK_UP: return GLUT_KEY_UP;
1228 case VK_RIGHT: return GLUT_KEY_RIGHT;
1229 case VK_DOWN: return GLUT_KEY_DOWN;
1231 if(vkey >= VK_F1 && vkey <= VK_F12) {
1232 return vkey - VK_F1 + GLUT_KEY_F1;
1238 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam)
1245 x = lparam & 0xffff;
1247 cb_mouse(bn, st ? GLUT_DOWN : GLUT_UP, x, y);
1251 static void get_window_pos(int *x, int *y)
1254 GetWindowRect(win, &rect);
1259 static void get_window_size(int *w, int *h)
1262 GetClientRect(win, &rect);
1263 *w = rect.right - rect.left;
1264 *h = rect.bottom - rect.top;
1267 static void get_screen_size(int *scrw, int *scrh)
1269 *scrw = GetSystemMetrics(SM_CXSCREEN);
1270 *scrh = GetSystemMetrics(SM_CYSCREEN);
1272 #endif /* BUILD_WIN32 */
1274 #if defined(__unix__) || defined(__APPLE__)
1275 #include <sys/time.h>
1277 #ifdef MINIGLUT_USE_LIBC
1278 #define sys_gettimeofday(tv, tz) gettimeofday(tv, tz)
1280 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz);
1283 static long get_msec(void)
1285 static struct timeval tv0;
1288 sys_gettimeofday(&tv, 0);
1289 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
1293 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
1297 static long get_msec(void)
1302 #ifdef MINIGLUT_NO_WINMM
1303 tm = GetTickCount();
1315 static void panic(const char *msg)
1317 const char *end = msg;
1319 sys_write(2, msg, end - msg);
1324 #ifdef MINIGLUT_USE_LIBC
1325 static void sys_exit(int status)
1330 static int sys_write(int fd, const void *buf, int count)
1332 return write(fd, buf, count);
1335 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1337 return gettimeofday(tv, tz);
1340 #else /* !MINIGLUT_USE_LIBC */
1343 static void mglut_sincosf(float angle, float *sptr, float *cptr)
1350 : "=m"(*sptr), "=m"(*cptr)
1355 static float mglut_atan(float x)
1371 static void mglut_sincosf(float angle, float *sptr, float *cptr)
1384 static float mglut_atan(float x)
1398 #pragma aux mglut_sincosf = \
1400 "fstp dword ptr [edx]" \
1401 "fstp dword ptr [eax]" \
1402 parm[8087][eax][edx] \
1405 #pragma aux mglut_atan = \
1416 static void sys_exit(int status)
1420 :: "a"(60), "D"(status));
1422 static int sys_write(int fd, const void *buf, int count)
1428 : "a"(1), "D"(fd), "S"(buf), "d"(count));
1431 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1437 : "a"(96), "D"(tv), "S"(tz));
1442 static void sys_exit(int status)
1446 :: "a"(1), "b"(status));
1448 static int sys_write(int fd, const void *buf, int count)
1454 : "a"(4), "b"(fd), "c"(buf), "d"(count));
1457 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1463 : "a"(78), "b"(tv), "c"(tz));
1468 #endif /* __linux__ */
1471 static void sys_exit(int status)
1473 ExitProcess(status);
1475 static int sys_write(int fd, const void *buf, int count)
1477 unsigned long wrsz = 0;
1479 HANDLE out = GetStdHandle(fd == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
1480 if(!WriteFile(out, buf, count, &wrsz, 0)) {
1487 #endif /* !MINIGLUT_USE_LIBC */