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 = -1, 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");
145 get_screen_size(&init_x, &init_y);
152 void glutInitWindowPosition(int x, int y)
158 void glutInitWindowSize(int xsz, int ysz)
164 void glutInitDisplayMode(unsigned int mode)
169 void glutCreateWindow(const char *title)
171 create_window(title);
179 void glutMainLoop(void)
186 void glutPostRedisplay(void)
191 #define UPD_EVMASK(x) \
198 if(win) XSelectInput(dpy, win, evmask); \
202 void glutIdleFunc(glut_cb func)
207 void glutDisplayFunc(glut_cb func)
212 void glutReshapeFunc(glut_cb_reshape func)
217 void glutVisibilityFunc(glut_cb_state func)
221 UPD_EVMASK(VisibilityChangeMask);
225 void glutEntryFunc(glut_cb_state func)
229 UPD_EVMASK(EnterWindowMask | LeaveWindowMask);
233 void glutKeyboardFunc(glut_cb_keyb func)
237 UPD_EVMASK(KeyPressMask);
241 void glutKeyboardUpFunc(glut_cb_keyb func)
245 UPD_EVMASK(KeyReleaseMask);
249 void glutSpecialFunc(glut_cb_special func)
253 UPD_EVMASK(KeyPressMask);
257 void glutSpecialUpFunc(glut_cb_special func)
261 UPD_EVMASK(KeyReleaseMask);
265 void glutMouseFunc(glut_cb_mouse func)
269 UPD_EVMASK(ButtonPressMask | ButtonReleaseMask);
273 void glutMotionFunc(glut_cb_motion func)
277 UPD_EVMASK(ButtonMotionMask);
281 void glutPassiveMotionFunc(glut_cb_motion func)
285 UPD_EVMASK(PointerMotionMask);
289 void glutSpaceballMotionFunc(glut_cb_sbmotion func)
291 cb_sball_motion = func;
294 void glutSpaceballRotateFunc(glut_cb_sbmotion func)
296 cb_sball_rotate = func;
299 void glutSpaceballBittonFunc(glut_cb_sbbutton func)
301 cb_sball_button = func;
304 int glutGet(unsigned int s)
309 get_window_pos(&x, &y);
312 get_window_pos(&x, &y);
314 case GLUT_WINDOW_WIDTH:
315 get_window_size(&x, &y);
317 case GLUT_WINDOW_HEIGHT:
318 get_window_size(&x, &y);
320 case GLUT_WINDOW_BUFFER_SIZE:
321 return ctx_info.rsize + ctx_info.gsize + ctx_info.bsize + ctx_info.asize;
322 case GLUT_WINDOW_STENCIL_SIZE:
323 return ctx_info.ssize;
324 case GLUT_WINDOW_DEPTH_SIZE:
325 return ctx_info.zsize;
326 case GLUT_WINDOW_RED_SIZE:
327 return ctx_info.rsize;
328 case GLUT_WINDOW_GREEN_SIZE:
329 return ctx_info.gsize;
330 case GLUT_WINDOW_BLUE_SIZE:
331 return ctx_info.bsize;
332 case GLUT_WINDOW_ALPHA_SIZE:
333 return ctx_info.asize;
334 case GLUT_WINDOW_DOUBLEBUFFER:
335 return ctx_info.dblbuf;
336 case GLUT_WINDOW_RGBA:
338 case GLUT_WINDOW_NUM_SAMPLES:
339 return ctx_info.samples;
340 case GLUT_WINDOW_STEREO:
341 return ctx_info.stereo;
342 case GLUT_WINDOW_SRGB:
343 return ctx_info.srgb;
344 case GLUT_WINDOW_CURSOR:
346 case GLUT_SCREEN_WIDTH:
347 get_screen_size(&x, &y);
349 case GLUT_SCREEN_HEIGHT:
350 get_screen_size(&x, &y);
352 case GLUT_INIT_DISPLAY_MODE:
354 case GLUT_INIT_WINDOW_X:
356 case GLUT_INIT_WINDOW_Y:
358 case GLUT_INIT_WINDOW_WIDTH:
360 case GLUT_INIT_WINDOW_HEIGHT:
362 case GLUT_ELAPSED_TIME:
370 int glutGetModifiers(void)
375 static int is_space(int c)
377 return c == ' ' || c == '\t' || c == '\v' || c == '\n' || c == '\r';
380 static const char *skip_space(const char *s)
382 while(*s && is_space(*s)) s++;
386 int glutExtensionSupported(char *ext)
388 const char *str, *eptr;
390 if(!(str = (const char*)glGetString(GL_EXTENSIONS))) {
395 str = skip_space(str);
396 eptr = skip_space(ext);
397 while(*str && !is_space(*str) && *eptr && *str == *eptr) {
401 if((!*str || is_space(*str)) && !*eptr) {
404 while(*str && !is_space(*str)) str++;
411 void glutSolidSphere(float rad, int slices, int stacks)
414 float x, y, z, s, t, u, v, phi, theta, sintheta, costheta, sinphi, cosphi;
415 float du = 1.0f / (float)slices;
416 float dv = 1.0f / (float)stacks;
419 for(i=0; i<stacks; i++) {
421 for(j=0; j<slices; j++) {
425 s = gray & 1 ? u + du : u;
426 t = gray & 2 ? v + dv : v;
427 theta = s * PI * 2.0f;
429 mglut_sincosf(theta, &sintheta, &costheta);
430 mglut_sincosf(phi, &sinphi, &cosphi);
431 x = sintheta * sinphi;
432 y = costheta * sinphi;
438 glVertex3f(x * rad, y * rad, z * rad);
445 void glutWireSphere(float rad, int slices, int stacks)
447 glPushAttrib(GL_POLYGON_BIT);
448 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
449 glutSolidSphere(rad, slices, stacks);
453 void glutSolidCube(float sz)
455 int i, j, idx, gray, flip, rotx;
456 float vpos[3], norm[3];
457 float rad = sz * 0.5f;
463 idx = (~i & 2) - rotx;
464 norm[0] = norm[1] = norm[2] = 0.0f;
465 norm[idx] = flip ^ ((i >> 1) & 1) ? -1 : 1;
467 vpos[idx] = norm[idx] * rad;
470 vpos[i & 2] = (gray ^ flip) & 1 ? rad : -rad;
471 vpos[rotx + 1] = (gray ^ (rotx << 1)) & 2 ? rad : -rad;
472 glTexCoord2f(gray & 1, gray >> 1);
479 void glutWireCube(float sz)
481 glPushAttrib(GL_POLYGON_BIT);
482 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
487 static void draw_cylinder(float rbot, float rtop, float height, int slices, int stacks)
490 float x, y, z, s, t, u, v, theta, phi, sintheta, costheta, sinphi, cosphi, rad;
491 float du = 1.0f / (float)slices;
492 float dv = 1.0f / (float)stacks;
495 phi = mglut_atan((rad < 0 ? -rad : rad) / height);
496 mglut_sincosf(phi, &sinphi, &cosphi);
499 for(i=0; i<stacks; i++) {
501 for(j=0; j<slices; j++) {
505 s = gray & 2 ? u + du : u;
506 t = gray & 1 ? v + dv : v;
507 rad = rbot + (rtop - rbot) * t;
508 theta = s * PI * 2.0f;
509 mglut_sincosf(theta, &sintheta, &costheta);
511 x = sintheta * cosphi;
512 y = costheta * cosphi;
518 glVertex3f(sintheta * rad, costheta * rad, t * height);
525 void glutSolidCone(float base, float height, int slices, int stacks)
527 draw_cylinder(base, 0, height, slices, stacks);
530 void glutWireCone(float base, float height, int slices, int stacks)
532 glPushAttrib(GL_POLYGON_BIT);
533 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
534 glutSolidCone(base, height, slices, stacks);
538 void glutSolidCylinder(float rad, float height, int slices, int stacks)
540 draw_cylinder(rad, rad, height, slices, stacks);
543 void glutWireCylinder(float rad, float height, int slices, int stacks)
545 glPushAttrib(GL_POLYGON_BIT);
546 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
547 glutSolidCylinder(rad, height, slices, stacks);
551 void glutSolidTorus(float inner_rad, float outer_rad, int sides, int rings)
554 float x, y, z, s, t, u, v, phi, theta, sintheta, costheta, sinphi, cosphi;
555 float du = 1.0f / (float)rings;
556 float dv = 1.0f / (float)sides;
559 for(i=0; i<rings; i++) {
561 for(j=0; j<sides; j++) {
565 s = gray & 1 ? u + du : u;
566 t = gray & 2 ? v + dv : v;
567 theta = s * PI * 2.0f;
569 mglut_sincosf(theta, &sintheta, &costheta);
570 mglut_sincosf(phi, &sinphi, &cosphi);
571 x = sintheta * sinphi;
572 y = costheta * sinphi;
579 x = x * inner_rad + sintheta * outer_rad;
580 y = y * inner_rad + costheta * outer_rad;
589 void glutWireTorus(float inner_rad, float outer_rad, int sides, int rings)
591 glPushAttrib(GL_POLYGON_BIT);
592 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
593 glutSolidTorus(inner_rad, outer_rad, sides, rings);
597 void glutSolidTeapot(float size)
601 void glutWireTeapot(float size)
607 /* --------------- UNIX/X11 implementation ----------------- */
609 static void handle_event(XEvent *ev);
611 void glutMainLoopEvent(void)
616 panic("display callback not set");
619 if(!upd_pending && !cb_idle) {
620 XNextEvent(dpy, &ev);
624 while(XPending(dpy)) {
625 XNextEvent(dpy, &ev);
634 if(upd_pending && mapped) {
640 static KeySym translate_keysym(KeySym sym)
661 static void handle_event(XEvent *ev)
672 case ConfigureNotify:
673 if(cb_reshape && (ev->xconfigure.width != win_width || ev->xconfigure.height != win_height)) {
674 win_width = ev->xconfigure.width;
675 win_height = ev->xconfigure.height;
676 cb_reshape(ev->xconfigure.width, ev->xconfigure.height);
681 if(ev->xclient.message_type == xa_wm_proto) {
682 if(ev->xclient.data.l[0] == xa_wm_del_win) {
694 modstate = ev->xkey.state & (ShiftMask | ControlMask | Mod1Mask);
695 if(!(sym = XLookupKeysym(&ev->xkey, 0))) {
698 sym = translate_keysym(sym);
700 if(ev->type == KeyPress) {
701 if(cb_keydown) cb_keydown((unsigned char)sym, ev->xkey.x, ev->xkey.y);
703 if(cb_keyup) cb_keyup((unsigned char)sym, ev->xkey.x, ev->xkey.y);
706 if(ev->type == KeyPress) {
707 if(cb_skeydown) cb_skeydown(sym, ev->xkey.x, ev->xkey.y);
709 if(cb_skeyup) cb_skeyup(sym, ev->xkey.x, ev->xkey.y);
716 modstate = ev->xbutton.state & (ShiftMask | ControlMask | Mod1Mask);
718 int bn = ev->xbutton.button - Button1;
719 cb_mouse(bn, ev->type == ButtonPress ? GLUT_DOWN : GLUT_UP,
720 ev->xbutton.x, ev->xbutton.y);
725 if(ev->xmotion.state & (Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask)) {
726 if(cb_motion) cb_motion(ev->xmotion.x, ev->xmotion.y);
728 if(cb_passive) cb_passive(ev->xmotion.x, ev->xmotion.y);
732 case VisibilityNotify:
734 cb_vis(ev->xvisibility.state == VisibilityFullyObscured ? GLUT_NOT_VISIBLE : GLUT_VISIBLE);
738 if(cb_entry) cb_entry(GLUT_ENTERED);
741 if(cb_entry) cb_entry(GLUT_LEFT);
746 void glutSwapBuffers(void)
748 glXSwapBuffers(dpy, win);
751 void glutPositionWindow(int x, int y)
753 XMoveWindow(dpy, win, x, y);
756 void glutReshapeWindow(int xsz, int ysz)
758 XResizeWindow(dpy, win, xsz, ysz);
761 void glutFullScreen(void)
766 void glutSetWindowTitle(const char *title)
769 if(!XStringListToTextProperty((char**)&title, 1, &tprop)) {
772 XSetWMName(dpy, win, &tprop);
776 void glutSetIconTitle(const char *title)
779 if(!XStringListToTextProperty((char**)&title, 1, &tprop)) {
782 XSetWMIconName(dpy, win, &tprop);
786 void glutSetCursor(int cidx)
791 case GLUT_CURSOR_LEFT_ARROW:
792 cur = XCreateFontCursor(dpy, XC_left_ptr);
794 case GLUT_CURSOR_INHERIT:
796 case GLUT_CURSOR_NONE:
802 XDefineCursor(dpy, win, cur);
806 static XVisualInfo *choose_visual(unsigned int mode)
813 if(mode & GLUT_DOUBLE) {
814 *aptr++ = GLX_DOUBLEBUFFER;
817 if(mode & GLUT_INDEX) {
818 *aptr++ = GLX_BUFFER_SIZE;
822 *aptr++ = GLX_RED_SIZE; *aptr++ = 4;
823 *aptr++ = GLX_GREEN_SIZE; *aptr++ = 4;
824 *aptr++ = GLX_BLUE_SIZE; *aptr++ = 4;
826 if(mode & GLUT_ALPHA) {
827 *aptr++ = GLX_ALPHA_SIZE;
830 if(mode & GLUT_DEPTH) {
831 *aptr++ = GLX_DEPTH_SIZE;
834 if(mode & GLUT_STENCIL) {
835 *aptr++ = GLX_STENCIL_SIZE;
838 if(mode & GLUT_ACCUM) {
839 *aptr++ = GLX_ACCUM_RED_SIZE; *aptr++ = 1;
840 *aptr++ = GLX_ACCUM_GREEN_SIZE; *aptr++ = 1;
841 *aptr++ = GLX_ACCUM_BLUE_SIZE; *aptr++ = 1;
843 if(mode & GLUT_STEREO) {
844 *aptr++ = GLX_STEREO;
846 if(mode & GLUT_SRGB) {
847 *aptr++ = GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB;
849 if(mode & GLUT_MULTISAMPLE) {
850 *aptr++ = GLX_SAMPLE_BUFFERS_ARB;
852 *aptr++ = GLX_SAMPLES_ARB;
859 return glXChooseVisual(dpy, scr, attr);
861 while(!(vi = glXChooseVisual(dpy, scr, attr)) && *samples) {
870 static void create_window(const char *title)
872 XSetWindowAttributes xattr;
874 unsigned int xattr_mask;
875 unsigned int mode = init_mode;
877 if(!(vi = choose_visual(mode))) {
879 if(!(vi = choose_visual(mode))) {
880 panic("Failed to find compatible visual\n");
884 if(!(ctx = glXCreateContext(dpy, vi, 0, True))) {
886 panic("Failed to create OpenGL context\n");
889 glXGetConfig(dpy, vi, GLX_RED_SIZE, &ctx_info.rsize);
890 glXGetConfig(dpy, vi, GLX_GREEN_SIZE, &ctx_info.gsize);
891 glXGetConfig(dpy, vi, GLX_BLUE_SIZE, &ctx_info.bsize);
892 glXGetConfig(dpy, vi, GLX_ALPHA_SIZE, &ctx_info.asize);
893 glXGetConfig(dpy, vi, GLX_DEPTH_SIZE, &ctx_info.zsize);
894 glXGetConfig(dpy, vi, GLX_STENCIL_SIZE, &ctx_info.ssize);
895 glXGetConfig(dpy, vi, GLX_DOUBLEBUFFER, &ctx_info.dblbuf);
896 glXGetConfig(dpy, vi, GLX_STEREO, &ctx_info.stereo);
897 glXGetConfig(dpy, vi, GLX_SAMPLES_ARB, &ctx_info.samples);
898 glXGetConfig(dpy, vi, GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB, &ctx_info.srgb);
900 xattr.background_pixel = BlackPixel(dpy, scr);
901 xattr.colormap = XCreateColormap(dpy, root, vi->visual, AllocNone);
902 xattr_mask = CWBackPixel | CWColormap;
903 if(!(win = XCreateWindow(dpy, root, init_x, init_y, init_width, init_height, 0,
904 vi->depth, InputOutput, vi->visual, xattr_mask, &xattr))) {
906 glXDestroyContext(dpy, ctx);
907 panic("Failed to create window\n");
911 XSelectInput(dpy, win, evmask);
913 glutSetWindowTitle(title);
914 glutSetIconTitle(title);
915 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
916 XMapWindow(dpy, win);
918 glXMakeCurrent(dpy, win, ctx);
921 static void get_window_pos(int *x, int *y)
923 XWindowAttributes wattr;
924 XGetWindowAttributes(dpy, win, &wattr);
929 static void get_window_size(int *w, int *h)
931 XWindowAttributes wattr;
932 XGetWindowAttributes(dpy, win, &wattr);
937 static void get_screen_size(int *scrw, int *scrh)
939 XWindowAttributes wattr;
940 XGetWindowAttributes(dpy, root, &wattr);
942 *scrh = wattr.height;
944 #endif /* BUILD_X11 */
947 /* --------------- windows implementation ----------------- */
949 static int reshape_pending;
951 static void update_modkeys(void);
952 static int translate_vkey(int vkey);
953 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam);
955 #ifdef MINIGLUT_WINMAIN
956 int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, char *cmdline, int showcmd)
959 char *argv[] = { "miniglut.exe", 0 };
960 return main(argc, argv);
964 void glutMainLoopEvent(void)
969 panic("display callback not set");
972 if(reshape_pending && cb_reshape) {
974 get_window_size(&win_width, &win_height);
975 cb_reshape(win_width, win_height);
978 if(!upd_pending && !cb_idle) {
979 GetMessage(&msg, 0, 0, 0);
980 TranslateMessage(&msg);
981 DispatchMessage(&msg);
984 while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
985 TranslateMessage(&msg);
986 DispatchMessage(&msg);
994 if(upd_pending && mapped) {
1000 void glutSwapBuffers(void)
1005 void glutPositionWindow(int x, int y)
1008 GetWindowRect(win, &rect);
1009 MoveWindow(win, x, y, rect.right - rect.left, rect.bottom - rect.top, 1);
1012 void glutReshapeWindow(int xsz, int ysz)
1015 GetWindowRect(win, &rect);
1016 MoveWindow(win, rect.left, rect.top, xsz, ysz, 1);
1019 void glutFullScreen(void)
1024 void glutSetWindowTitle(const char *title)
1026 SetWindowText(win, title);
1029 void glutSetIconTitle(const char *title)
1033 void glutSetCursor(int cidx)
1036 case GLUT_CURSOR_NONE:
1039 case GLUT_CURSOR_INHERIT:
1040 case GLUT_CURSOR_LEFT_ARROW:
1042 SetCursor(LoadCursor(0, IDC_ARROW));
1048 static void create_window(const char *title)
1051 PIXELFORMATDESCRIPTOR pfd = {0};
1056 rect.right = init_x + init_width;
1057 rect.bottom = init_y + init_height;
1058 AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, 0);
1060 if(!(win = CreateWindow("MiniGLUT", title, WS_OVERLAPPEDWINDOW, rect.left, rect.top,
1061 rect.right - rect.left, rect.bottom - rect.top, 0, 0, hinst, 0))) {
1062 panic("Failed to create window\n");
1066 pfd.nSize = sizeof pfd;
1068 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
1069 if(init_mode & GLUT_STEREO) {
1070 pfd.dwFlags |= PFD_STEREO;
1072 pfd.iPixelType = init_mode & GLUT_INDEX ? PFD_TYPE_COLORINDEX : PFD_TYPE_RGBA;
1073 pfd.cColorBits = 24;
1074 if(init_mode & GLUT_ALPHA) {
1077 if(init_mode & GLUT_ACCUM) {
1078 pfd.cAccumBits = 24;
1080 if(init_mode & GLUT_DEPTH) {
1081 pfd.cDepthBits = 24;
1083 if(init_mode & GLUT_STENCIL) {
1084 pfd.cStencilBits = 8;
1086 pfd.iLayerType = PFD_MAIN_PLANE;
1088 if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) {
1089 panic("Failed to find suitable pixel format\n");
1091 if(!SetPixelFormat(dc, pixfmt, &pfd)) {
1092 panic("Failed to set the selected pixel format\n");
1094 if(!(ctx = wglCreateContext(dc))) {
1095 panic("Failed to create the OpenGL context\n");
1097 wglMakeCurrent(dc, ctx);
1099 DescribePixelFormat(dc, pixfmt, sizeof pfd, &pfd);
1100 ctx_info.rsize = pfd.cRedBits;
1101 ctx_info.gsize = pfd.cGreenBits;
1102 ctx_info.bsize = pfd.cBlueBits;
1103 ctx_info.asize = pfd.cAlphaBits;
1104 ctx_info.zsize = pfd.cDepthBits;
1105 ctx_info.ssize = pfd.cStencilBits;
1106 ctx_info.dblbuf = pfd.dwFlags & PFD_DOUBLEBUFFER ? 1 : 0;
1107 ctx_info.samples = 1; /* TODO */
1108 ctx_info.srgb = 0; /* TODO */
1112 reshape_pending = 1;
1115 static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
1117 static int mouse_x, mouse_y;
1122 if(win) DestroyWindow(win);
1126 wglMakeCurrent(dc, 0);
1127 wglDeleteContext(ctx);
1134 ValidateRect(win, 0);
1138 win_width = lparam & 0xffff;
1139 win_height = lparam >> 16;
1141 reshape_pending = 0;
1142 cb_reshape(win_width, win_height);
1148 if(cb_vis) cb_vis(mapped ? GLUT_VISIBLE : GLUT_NOT_VISIBLE);
1153 key = translate_vkey(wparam);
1156 cb_keydown((unsigned char)key, mouse_x, mouse_y);
1160 cb_skeydown(key, mouse_x, mouse_y);
1167 key = translate_vkey(wparam);
1170 cb_keyup((unsigned char)key, mouse_x, mouse_y);
1174 cb_skeyup(key, mouse_x, mouse_y);
1179 case WM_LBUTTONDOWN:
1180 handle_mbutton(0, 1, wparam, lparam);
1182 case WM_MBUTTONDOWN:
1183 handle_mbutton(1, 1, wparam, lparam);
1185 case WM_RBUTTONDOWN:
1186 handle_mbutton(2, 1, wparam, lparam);
1189 handle_mbutton(0, 0, wparam, lparam);
1192 handle_mbutton(1, 0, wparam, lparam);
1195 handle_mbutton(2, 0, wparam, lparam);
1199 if(wparam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)) {
1200 if(cb_motion) cb_motion(lparam & 0xffff, lparam >> 16);
1202 if(cb_passive) cb_passive(lparam & 0xffff, lparam >> 16);
1207 return DefWindowProc(win, msg, wparam, lparam);
1213 static void update_modkeys(void)
1215 if(GetKeyState(VK_SHIFT)) {
1216 modstate |= GLUT_ACTIVE_SHIFT;
1218 modstate &= ~GLUT_ACTIVE_SHIFT;
1220 if(GetKeyState(VK_CONTROL)) {
1221 modstate |= GLUT_ACTIVE_CTRL;
1223 modstate &= ~GLUT_ACTIVE_CTRL;
1225 if(GetKeyState(VK_MENU)) {
1226 modstate |= GLUT_ACTIVE_ALT;
1228 modstate &= ~GLUT_ACTIVE_ALT;
1232 static int translate_vkey(int vkey)
1235 case VK_PRIOR: return GLUT_KEY_PAGE_UP;
1236 case VK_NEXT: return GLUT_KEY_PAGE_DOWN;
1237 case VK_END: return GLUT_KEY_END;
1238 case VK_HOME: return GLUT_KEY_HOME;
1239 case VK_LEFT: return GLUT_KEY_LEFT;
1240 case VK_UP: return GLUT_KEY_UP;
1241 case VK_RIGHT: return GLUT_KEY_RIGHT;
1242 case VK_DOWN: return GLUT_KEY_DOWN;
1244 if(vkey >= VK_F1 && vkey <= VK_F12) {
1245 return vkey - VK_F1 + GLUT_KEY_F1;
1251 static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam)
1258 x = lparam & 0xffff;
1260 cb_mouse(bn, st ? GLUT_DOWN : GLUT_UP, x, y);
1264 static void get_window_pos(int *x, int *y)
1267 GetWindowRect(win, &rect);
1272 static void get_window_size(int *w, int *h)
1275 GetClientRect(win, &rect);
1276 *w = rect.right - rect.left;
1277 *h = rect.bottom - rect.top;
1280 static void get_screen_size(int *scrw, int *scrh)
1282 *scrw = GetSystemMetrics(SM_CXSCREEN);
1283 *scrh = GetSystemMetrics(SM_CYSCREEN);
1285 #endif /* BUILD_WIN32 */
1287 #if defined(__unix__) || defined(__APPLE__)
1288 #include <sys/time.h>
1290 #ifdef MINIGLUT_USE_LIBC
1291 #define sys_gettimeofday(tv, tz) gettimeofday(tv, tz)
1293 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz);
1296 static long get_msec(void)
1298 static struct timeval tv0;
1301 sys_gettimeofday(&tv, 0);
1302 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
1306 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
1310 static long get_msec(void)
1315 #ifdef MINIGLUT_NO_WINMM
1316 tm = GetTickCount();
1328 static void panic(const char *msg)
1330 const char *end = msg;
1332 sys_write(2, msg, end - msg);
1337 #ifdef MINIGLUT_USE_LIBC
1338 static void sys_exit(int status)
1343 static int sys_write(int fd, const void *buf, int count)
1345 return write(fd, buf, count);
1348 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1350 return gettimeofday(tv, tz);
1353 #else /* !MINIGLUT_USE_LIBC */
1356 static void mglut_sincosf(float angle, float *sptr, float *cptr)
1363 : "=m"(*sptr), "=m"(*cptr)
1368 static float mglut_atan(float x)
1384 static void mglut_sincosf(float angle, float *sptr, float *cptr)
1397 static float mglut_atan(float x)
1411 #pragma aux mglut_sincosf = \
1413 "fstp dword ptr [edx]" \
1414 "fstp dword ptr [eax]" \
1415 parm[8087][eax][edx] \
1418 #pragma aux mglut_atan = \
1429 static void sys_exit(int status)
1433 :: "a"(60), "D"(status));
1435 static int sys_write(int fd, const void *buf, int count)
1441 : "a"(1), "D"(fd), "S"(buf), "d"(count));
1444 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1450 : "a"(96), "D"(tv), "S"(tz));
1455 static void sys_exit(int status)
1459 :: "a"(1), "b"(status));
1461 static int sys_write(int fd, const void *buf, int count)
1467 : "a"(4), "b"(fd), "c"(buf), "d"(count));
1470 static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
1476 : "a"(78), "b"(tv), "c"(tz));
1481 #endif /* __linux__ */
1484 static void sys_exit(int status)
1486 ExitProcess(status);
1488 static int sys_write(int fd, const void *buf, int count)
1490 unsigned long wrsz = 0;
1492 HANDLE out = GetStdHandle(fd == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
1493 if(!WriteFile(out, buf, count, &wrsz, 0)) {
1500 #endif /* !MINIGLUT_USE_LIBC */