made the header compatible with C++
[miniglut] / miniglut.c
index 788d8a1..c9cc1f0 100644 (file)
@@ -16,9 +16,16 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #ifdef MINIGLUT_USE_LIBC
+#define _GNU_SOURCE
 #include <stdlib.h>
+#include <math.h>
+#else
+static void mglut_sincosf(float angle, float *sptr, float *cptr);
+static float mglut_atan(float x);
 #endif
 
+#define PI     3.1415926536f
+
 #if defined(__unix__)
 
 #include <X11/Xlib.h>
@@ -46,6 +53,9 @@ static unsigned int evmask;
 #include <windows.h>
 #define BUILD_WIN32
 
+static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
+
+static HINSTANCE hinst;
 static HWND win;
 static HDC dc;
 static HGLRC ctx;
@@ -67,9 +77,9 @@ struct ctx_info {
 };
 
 static void create_window(const char *title);
-static void get_window_pos(Window win, int *x, int *y);
-static void get_window_size(Window win, int *w, int *h);
-static void get_screen_size(Window win, int *scrw, int *scrh);
+static void get_window_pos(int *x, int *y);
+static void get_window_size(int *w, int *h);
+static void get_screen_size(int *scrw, int *scrh);
 
 static long get_msec(void);
 static void panic(const char *msg);
@@ -97,7 +107,7 @@ static glut_cb_sbbutton cb_sball_button;
 static int win_width, win_height;
 static int mapped;
 static int quit;
-static int upd_pending, reshape_pending;
+static int upd_pending;
 static int modstate;
 
 
@@ -114,6 +124,23 @@ void glutInit(int *argc, char **argv)
 
        evmask = ExposureMask | StructureNotifyMask;
 #endif
+#ifdef BUILD_WIN32
+       WNDCLASSEX wc = {0};
+
+       hinst = GetModuleHandle(0);
+
+       wc.cbSize = sizeof wc;
+       wc.hbrBackground = GetStockObject(BLACK_BRUSH);
+       wc.hCursor = LoadCursor(0, IDC_ARROW);
+       wc.hIcon = wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
+       wc.hInstance = hinst;
+       wc.lpfnWndProc = handle_message;
+       wc.lpszClassName = "MiniGLUT";
+       wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
+       if(!RegisterClassEx(&wc)) {
+               panic("Failed to register \"MiniGLUT\" window class\n");
+       }
+#endif
 }
 
 void glutInitWindowPosition(int x, int y)
@@ -273,16 +300,16 @@ int glutGet(unsigned int s)
        int x, y;
        switch(s) {
        case GLUT_WINDOW_X:
-               get_window_pos(win, &x, &y);
+               get_window_pos(&x, &y);
                return x;
        case GLUT_WINDOW_Y:
-               get_window_pos(win, &x, &y);
+               get_window_pos(&x, &y);
                return y;
        case GLUT_WINDOW_WIDTH:
-               get_window_size(win, &x, &y);
+               get_window_size(&x, &y);
                return x;
        case GLUT_WINDOW_HEIGHT:
-               get_window_size(win, &x, &y);
+               get_window_size(&x, &y);
                return y;
        case GLUT_WINDOW_BUFFER_SIZE:
                return ctx_info.rsize + ctx_info.gsize + ctx_info.bsize + ctx_info.asize;
@@ -311,10 +338,10 @@ int glutGet(unsigned int s)
        case GLUT_WINDOW_CURSOR:
                return cur_cursor;
        case GLUT_SCREEN_WIDTH:
-               get_screen_size(win, &x, &y);
+               get_screen_size(&x, &y);
                return x;
        case GLUT_SCREEN_HEIGHT:
-               get_screen_size(win, &x, &y);
+               get_screen_size(&x, &y);
                return y;
        case GLUT_INIT_DISPLAY_MODE:
                return init_mode;
@@ -375,28 +402,190 @@ int glutExtensionSupported(char *ext)
 }
 
 /* TODO */
-void glutSolidSphere(float rad)
+void glutSolidSphere(float rad, int slices, int stacks)
 {
+       int i, j, k, gray;
+       float x, y, z, s, t, u, v, phi, theta, sintheta, costheta, sinphi, cosphi;
+       float du = 1.0f / (float)slices;
+       float dv = 1.0f / (float)stacks;
+
+       glBegin(GL_QUADS);
+       for(i=0; i<stacks; i++) {
+               v = i * dv;
+               for(j=0; j<slices; j++) {
+                       u = j * du;
+                       for(k=0; k<4; k++) {
+                               gray = k ^ (k >> 1);
+                               s = gray & 1 ? u + du : u;
+                               t = gray & 2 ? v + dv : v;
+                               theta = s * PI * 2.0f;
+                               phi = t * PI;
+                               mglut_sincosf(theta, &sintheta, &costheta);
+                               mglut_sincosf(phi, &sinphi, &cosphi);
+                               x = sintheta * sinphi;
+                               y = costheta * sinphi;
+                               z = cosphi;
+
+                               glColor3f(s, t, 1);
+                               glTexCoord2f(s, t);
+                               glNormal3f(x, y, z);
+                               glVertex3f(x * rad, y * rad, z * rad);
+                       }
+               }
+       }
+       glEnd();
 }
 
-void glutWireSphere(float rad)
+void glutWireSphere(float rad, int slices, int stacks)
 {
+       glPushAttrib(GL_POLYGON_BIT);
+       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+       glutSolidSphere(rad, slices, stacks);
+       glPopAttrib();
 }
 
 void glutSolidCube(float sz)
 {
+       int i, j, idx, gray, flip, rotx;
+       float vpos[3], norm[3];
+       float rad = sz * 0.5f;
+
+       glBegin(GL_QUADS);
+       for(i=0; i<6; i++) {
+               flip = i & 1;
+               rotx = i >> 2;
+               idx = (~i & 2) - rotx;
+               norm[0] = norm[1] = norm[2] = 0.0f;
+               norm[idx] = flip ^ ((i >> 1) & 1) ? -1 : 1;
+               glNormal3fv(norm);
+               vpos[idx] = norm[idx] * rad;
+               for(j=0; j<4; j++) {
+                       gray = j ^ (j >> 1);
+                       vpos[i & 2] = (gray ^ flip) & 1 ? rad : -rad;
+                       vpos[rotx + 1] = (gray ^ (rotx << 1)) & 2 ? rad : -rad;
+                       glTexCoord2f(gray & 1, gray >> 1);
+                       glVertex3fv(vpos);
+               }
+       }
+       glEnd();
 }
 
 void glutWireCube(float sz)
 {
+       glPushAttrib(GL_POLYGON_BIT);
+       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+       glutSolidCube(sz);
+       glPopAttrib();
+}
+
+static void draw_cylinder(float rbot, float rtop, float height, int slices, int stacks)
+{
+       int i, j, k, gray;
+       float x, y, z, s, t, u, v, theta, phi, sintheta, costheta, sinphi, cosphi, rad;
+       float du = 1.0f / (float)slices;
+       float dv = 1.0f / (float)stacks;
+
+       rad = rbot - rtop;
+       phi = mglut_atan((rad < 0 ? -rad : rad) / height);
+       mglut_sincosf(phi, &sinphi, &cosphi);
+
+       glBegin(GL_QUADS);
+       for(i=0; i<stacks; i++) {
+               v = i * dv;
+               for(j=0; j<slices; j++) {
+                       u = j * du;
+                       for(k=0; k<4; k++) {
+                               gray = k ^ (k >> 1);
+                               s = gray & 2 ? u + du : u;
+                               t = gray & 1 ? v + dv : v;
+                               rad = rbot + (rtop - rbot) * t;
+                               theta = s * PI * 2.0f;
+                               mglut_sincosf(theta, &sintheta, &costheta);
+
+                               x = sintheta * cosphi;
+                               y = costheta * cosphi;
+                               z = sinphi;
+
+                               glColor3f(s, t, 1);
+                               glTexCoord2f(s, t);
+                               glNormal3f(x, y, z);
+                               glVertex3f(sintheta * rad, costheta * rad, t * height);
+                       }
+               }
+       }
+       glEnd();
+}
+
+void glutSolidCone(float base, float height, int slices, int stacks)
+{
+       draw_cylinder(base, 0, height, slices, stacks);
 }
 
-void glutSolidTorus(float inner_rad, float outer_rad, float sides, float rings)
+void glutWireCone(float base, float height, int slices, int stacks)
 {
+       glPushAttrib(GL_POLYGON_BIT);
+       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+       glutSolidCone(base, height, slices, stacks);
+       glPopAttrib();
+}
+
+void glutSolidCylinder(float rad, float height, int slices, int stacks)
+{
+       draw_cylinder(rad, rad, height, slices, stacks);
+}
+
+void glutWireCylinder(float rad, float height, int slices, int stacks)
+{
+       glPushAttrib(GL_POLYGON_BIT);
+       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+       glutSolidCylinder(rad, height, slices, stacks);
+       glPopAttrib();
+}
+
+void glutSolidTorus(float inner_rad, float outer_rad, int sides, int rings)
+{
+       int i, j, k, gray;
+       float x, y, z, s, t, u, v, phi, theta, sintheta, costheta, sinphi, cosphi;
+       float du = 1.0f / (float)rings;
+       float dv = 1.0f / (float)sides;
+
+       glBegin(GL_QUADS);
+       for(i=0; i<rings; i++) {
+               u = i * du;
+               for(j=0; j<sides; j++) {
+                       v = j * dv;
+                       for(k=0; k<4; k++) {
+                               gray = k ^ (k >> 1);
+                               s = gray & 1 ? u + du : u;
+                               t = gray & 2 ? v + dv : v;
+                               theta = s * PI * 2.0f;
+                               phi = t * PI * 2.0f;
+                               mglut_sincosf(theta, &sintheta, &costheta);
+                               mglut_sincosf(phi, &sinphi, &cosphi);
+                               x = sintheta * sinphi;
+                               y = costheta * sinphi;
+                               z = cosphi;
+
+                               glColor3f(s, t, 1);
+                               glTexCoord2f(s, t);
+                               glNormal3f(x, y, z);
+
+                               x = x * inner_rad + sintheta * outer_rad;
+                               y = y * inner_rad + costheta * outer_rad;
+                               z *= inner_rad;
+                               glVertex3f(x, y, z);
+                       }
+               }
+       }
+       glEnd();
 }
 
-void glutWireTorus(float inner_rad, float outer_rad, float sides, float rings)
+void glutWireTorus(float inner_rad, float outer_rad, int sides, int rings)
 {
+       glPushAttrib(GL_POLYGON_BIT);
+       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+       glutSolidTorus(inner_rad, outer_rad, sides, rings);
+       glPopAttrib();
 }
 
 void glutSolidTeapot(float size)
@@ -408,6 +597,8 @@ void glutWireTeapot(float size)
 }
 
 
+
+/* --------------- UNIX/X11 implementation ----------------- */
 #ifdef BUILD_X11
 static void handle_event(XEvent *ev);
 
@@ -419,26 +610,24 @@ void glutMainLoopEvent(void)
                panic("display callback not set");
        }
 
-       for(;;) {
-               if(!upd_pending && !cb_idle) {
-                       XNextEvent(dpy, &ev);
-                       handle_event(&ev);
-                       if(quit) return;
-               }
-               while(XPending(dpy)) {
-                       XNextEvent(dpy, &ev);
-                       handle_event(&ev);
-                       if(quit) return;
-               }
+       if(!upd_pending && !cb_idle) {
+               XNextEvent(dpy, &ev);
+               handle_event(&ev);
+               if(quit) return;
+       }
+       while(XPending(dpy)) {
+               XNextEvent(dpy, &ev);
+               handle_event(&ev);
+               if(quit) return;
+       }
 
-               if(cb_idle) {
-                       cb_idle();
-               }
+       if(cb_idle) {
+               cb_idle();
+       }
 
-               if(upd_pending && mapped) {
-                       upd_pending = 0;
-                       cb_display();
-               }
+       if(upd_pending && mapped) {
+               upd_pending = 0;
+               cb_display();
        }
 }
 
@@ -611,16 +800,23 @@ void glutSetCursor(int cidx)
 static XVisualInfo *choose_visual(unsigned int mode)
 {
        XVisualInfo *vi;
-       int attr[32] = {
-               GLX_RGBA,
-               GLX_DOUBLEBUFFER,
-               GLX_RED_SIZE, 4,
-               GLX_GREEN_SIZE, 4,
-               GLX_BLUE_SIZE, 4
-       };
-       int *aptr = attr + 8;
+       int attr[32];
+       int *aptr = attr;
        int *samples = 0;
 
+       if(mode & GLUT_DOUBLE) {
+               *aptr++ = GLX_DOUBLEBUFFER;
+       }
+
+       if(mode & GLUT_INDEX) {
+               *aptr++ = GLX_BUFFER_SIZE;
+               *aptr++ = 1;
+       } else {
+               *aptr++ = GLX_RGBA;
+               *aptr++ = GLX_RED_SIZE; *aptr++ = 4;
+               *aptr++ = GLX_GREEN_SIZE; *aptr++ = 4;
+               *aptr++ = GLX_BLUE_SIZE; *aptr++ = 4;
+       }
        if(mode & GLUT_ALPHA) {
                *aptr++ = GLX_ALPHA_SIZE;
                *aptr++ = 4;
@@ -716,7 +912,7 @@ static void create_window(const char *title)
        glXMakeCurrent(dpy, win, ctx);
 }
 
-static void get_window_pos(Window win, int *x, int *y)
+static void get_window_pos(int *x, int *y)
 {
        XWindowAttributes wattr;
        XGetWindowAttributes(dpy, win, &wattr);
@@ -724,7 +920,7 @@ static void get_window_pos(Window win, int *x, int *y)
        *y = wattr.y;
 }
 
-static void get_window_size(Window win, int *w, int *h)
+static void get_window_size(int *w, int *h)
 {
        XWindowAttributes wattr;
        XGetWindowAttributes(dpy, win, &wattr);
@@ -732,24 +928,341 @@ static void get_window_size(Window win, int *w, int *h)
        *h = wattr.height;
 }
 
-static void get_screen_size(Window win, int *scrw, int *scrh)
+static void get_screen_size(int *scrw, int *scrh)
 {
        XWindowAttributes wattr;
        XGetWindowAttributes(dpy, root, &wattr);
        *scrw = wattr.width;
        *scrh = wattr.height;
 }
-#endif
+#endif /* BUILD_X11 */
+
+
+/* --------------- windows implementation ----------------- */
+#ifdef BUILD_WIN32
+static int reshape_pending;
+
+static void update_modkeys(void);
+static int translate_vkey(int vkey);
+static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam);
+
+void glutMainLoopEvent(void)
+{
+       MSG msg;
+
+       if(!cb_display) {
+               panic("display callback not set");
+       }
+
+       if(reshape_pending && cb_reshape) {
+               reshape_pending = 0;
+               get_window_size(&win_width, &win_height);
+               cb_reshape(win_width, win_height);
+       }
+
+       if(!upd_pending && !cb_idle) {
+               GetMessage(&msg, 0, 0, 0);
+               TranslateMessage(&msg);
+               DispatchMessage(&msg);
+               if(quit) return;
+       }
+       while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
+               TranslateMessage(&msg);
+               DispatchMessage(&msg);
+               if(quit) return;
+       }
+
+       if(cb_idle) {
+               cb_idle();
+       }
+
+       if(upd_pending && mapped) {
+               upd_pending = 0;
+               cb_display();
+       }
+}
+
+void glutSwapBuffers(void)
+{
+       SwapBuffers(dc);
+}
+
+void glutPositionWindow(int x, int y)
+{
+       RECT rect;
+       GetWindowRect(win, &rect);
+       MoveWindow(win, x, y, rect.right - rect.left, rect.bottom - rect.top, 1);
+}
+
+void glutReshapeWindow(int xsz, int ysz)
+{
+       RECT rect;
+       GetWindowRect(win, &rect);
+       MoveWindow(win, rect.left, rect.top, xsz, ysz, 1);
+}
+
+void glutFullScreen(void)
+{
+       /* TODO */
+}
+
+void glutSetWindowTitle(const char *title)
+{
+       SetWindowText(win, title);
+}
+
+void glutSetIconTitle(const char *title)
+{
+}
+
+void glutSetCursor(int cidx)
+{
+       switch(cidx) {
+       case GLUT_CURSOR_NONE:
+               ShowCursor(0);
+               break;
+       case GLUT_CURSOR_INHERIT:
+       case GLUT_CURSOR_LEFT_ARROW:
+       default:
+               SetCursor(LoadCursor(0, IDC_ARROW));
+               ShowCursor(1);
+       }
+}
+
+
+static void create_window(const char *title)
+{
+       int pixfmt;
+       PIXELFORMATDESCRIPTOR pfd = {0};
+
+       if(!(win = CreateWindow("MiniGLUT", title, WS_OVERLAPPEDWINDOW, init_x, init_y,
+                               init_width, init_height, 0, 0, hinst, 0))) {
+               panic("Failed to create window\n");
+       }
+       dc = GetDC(win);
+
+       pfd.nSize = sizeof pfd;
+       pfd.nVersion = 1;
+       pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
+       if(init_mode & GLUT_STEREO) {
+               pfd.dwFlags |= PFD_STEREO;
+       }
+       pfd.iPixelType = init_mode & GLUT_INDEX ? PFD_TYPE_COLORINDEX : PFD_TYPE_RGBA;
+       pfd.cColorBits = 24;
+       if(init_mode & GLUT_ALPHA) {
+               pfd.cAlphaBits = 8;
+       }
+       if(init_mode & GLUT_ACCUM) {
+               pfd.cAccumBits = 24;
+       }
+       if(init_mode & GLUT_DEPTH) {
+               pfd.cDepthBits = 24;
+       }
+       if(init_mode & GLUT_STENCIL) {
+               pfd.cStencilBits = 8;
+       }
+       pfd.iLayerType = PFD_MAIN_PLANE;
+
+       if(!(pixfmt = ChoosePixelFormat(dc, &pfd))) {
+               panic("Failed to find suitable pixel format\n");
+       }
+       if(!SetPixelFormat(dc, pixfmt, &pfd)) {
+               panic("Failed to set the selected pixel format\n");
+       }
+       if(!(ctx = wglCreateContext(dc))) {
+               panic("Failed to create the OpenGL context\n");
+       }
+       wglMakeCurrent(dc, ctx);
+
+       DescribePixelFormat(dc, pixfmt, sizeof pfd, &pfd);
+       ctx_info.rsize = pfd.cRedBits;
+       ctx_info.gsize = pfd.cGreenBits;
+       ctx_info.bsize = pfd.cBlueBits;
+       ctx_info.asize = pfd.cAlphaBits;
+       ctx_info.zsize = pfd.cDepthBits;
+       ctx_info.ssize = pfd.cStencilBits;
+       ctx_info.dblbuf = pfd.dwFlags & PFD_DOUBLEBUFFER ? 1 : 0;
+       ctx_info.samples = 1;   /* TODO */
+       ctx_info.srgb = 0;              /* TODO */
+
+       ShowWindow(win, 1);
+       upd_pending = 1;
+       reshape_pending = 1;
+}
+
+static HRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam)
+{
+       static int mouse_x, mouse_y;
+       int key;
+
+       switch(msg) {
+       case WM_CLOSE:
+               if(win) DestroyWindow(win);
+               break;
+
+       case WM_DESTROY:
+               wglMakeCurrent(dc, 0);
+               wglDeleteContext(ctx);
+               quit = 1;
+               PostQuitMessage(0);
+               break;
+
+       case WM_PAINT:
+               upd_pending = 1;
+               ValidateRect(win, 0);
+               break;
+
+       case WM_SIZE:
+               win_width = lparam & 0xffff;
+               win_height = lparam >> 16;
+               if(cb_reshape) {
+                       reshape_pending = 0;
+                       cb_reshape(win_width, win_height);
+               }
+               break;
+
+       case WM_SHOWWINDOW:
+               mapped = wparam;
+               if(cb_vis) cb_vis(mapped ? GLUT_VISIBLE : GLUT_NOT_VISIBLE);
+               break;
+
+       case WM_KEYDOWN:
+               update_modkeys();
+               key = translate_vkey(wparam);
+               if(key < 256) {
+                       if(cb_keydown) {
+                               cb_keydown((unsigned char)key, mouse_x, mouse_y);
+                       }
+               } else {
+                       if(cb_skeydown) {
+                               cb_skeydown(key, mouse_x, mouse_y);
+                       }
+               }
+               break;
+
+       case WM_KEYUP:
+               update_modkeys();
+               key = translate_vkey(wparam);
+               if(key < 256) {
+                       if(cb_keyup) {
+                               cb_keyup((unsigned char)key, mouse_x, mouse_y);
+                       }
+               } else {
+                       if(cb_skeyup) {
+                               cb_skeyup(key, mouse_x, mouse_y);
+                       }
+               }
+               break;
+
+       case WM_LBUTTONDOWN:
+               handle_mbutton(0, 1, wparam, lparam);
+               break;
+       case WM_MBUTTONDOWN:
+               handle_mbutton(1, 1, wparam, lparam);
+               break;
+       case WM_RBUTTONDOWN:
+               handle_mbutton(2, 1, wparam, lparam);
+               break;
+       case WM_LBUTTONUP:
+               handle_mbutton(0, 0, wparam, lparam);
+               break;
+       case WM_MBUTTONUP:
+               handle_mbutton(1, 0, wparam, lparam);
+               break;
+       case WM_RBUTTONUP:
+               handle_mbutton(2, 0, wparam, lparam);
+               break;
+
+       case WM_MOUSEMOVE:
+               if(wparam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)) {
+                       if(cb_motion) cb_motion(lparam & 0xffff, lparam >> 16);
+               } else {
+                       if(cb_passive) cb_passive(lparam & 0xffff, lparam >> 16);
+               }
+               break;
+
+       default:
+               return DefWindowProc(win, msg, wparam, lparam);
+       }
+
+       return 0;
+}
+
+static void update_modkeys(void)
+{
+       if(GetKeyState(VK_SHIFT)) {
+               modstate |= GLUT_ACTIVE_SHIFT;
+       } else {
+               modstate &= ~GLUT_ACTIVE_SHIFT;
+       }
+       if(GetKeyState(VK_CONTROL)) {
+               modstate |= GLUT_ACTIVE_CTRL;
+       } else {
+               modstate &= ~GLUT_ACTIVE_CTRL;
+       }
+       if(GetKeyState(VK_MENU)) {
+               modstate |= GLUT_ACTIVE_ALT;
+       } else {
+               modstate &= ~GLUT_ACTIVE_ALT;
+       }
+}
+
+static int translate_vkey(int vkey)
+{
+       return vkey;    /* TODO */
+}
+
+static void handle_mbutton(int bn, int st, WPARAM wparam, LPARAM lparam)
+{
+       int x, y;
+
+       update_modkeys();
+
+       if(cb_mouse) {
+               x = lparam & 0xffff;
+               y = lparam >> 16;
+               cb_mouse(bn, st ? GLUT_DOWN : GLUT_UP, x, y);
+       }
+}
+
+static void get_window_pos(int *x, int *y)
+{
+       RECT rect;
+       GetWindowRect(win, &rect);
+       *x = rect.left;
+       *y = rect.top;
+}
+
+static void get_window_size(int *w, int *h)
+{
+       RECT rect;
+       GetClientRect(win, &rect);
+       *w = rect.right - rect.left;
+       *h = rect.bottom - rect.top;
+}
+
+static void get_screen_size(int *scrw, int *scrh)
+{
+       *scrw = GetSystemMetrics(SM_CXSCREEN);
+       *scrh = GetSystemMetrics(SM_CYSCREEN);
+}
+#endif /* BUILD_WIN32 */
 
 #if defined(__unix__) || defined(__APPLE__)
 #include <sys/time.h>
 
+#ifdef MINIGLUT_USE_LIBC
+#define sys_gettimeofday(tv, tz)       gettimeofday(tv, tz)
+#else
+static int sys_gettimeofday(struct timeval *tv, struct timezone *tz);
+#endif
+
 static long get_msec(void)
 {
        static struct timeval tv0;
        struct timeval tv;
 
-       gettimeofday(&tv, 0);
+       sys_gettimeofday(&tv, 0);
        if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
                tv0 = tv;
                return 0;
@@ -761,12 +1274,18 @@ static long get_msec(void)
 static long get_msec(void)
 {
        static long t0;
+       long tm;
 
+#ifdef MINIGLUT_NO_WINMM
+       tm = GetTickCount();
+#else
+       tm = timeGetTime();
+#endif
        if(!t0) {
-               t0 = timeGetTime();
+               t0 = tm;
                return 0;
        }
-       return timeGetTime() - t0;
+       return tm - t0;
 }
 #endif
 
@@ -789,8 +1308,85 @@ static int sys_write(int fd, const void *buf, int count)
 {
        return write(fd, buf, count);
 }
+
+static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+       return gettimeofday(tv, tz);
+}
+
 #else  /* !MINIGLUT_USE_LIBC */
 
+#ifdef __GNUC__
+static void mglut_sincosf(float angle, float *sptr, float *cptr)
+{
+       asm volatile(
+               "flds %2\n\t"
+               "fsincos\n\t"
+               "fstps %1\n\t"
+               "fstps %0\n\t"
+               : "=m"(*sptr), "=m"(*cptr)
+               : "m"(angle)
+       );
+}
+
+static float mglut_atan(float x)
+{
+       float res;
+       asm volatile(
+               "flds %1\n\t"
+               "fld1\n\t"
+               "fpatan\n\t"
+               "fstps %0\n\t"
+               : "=m"(res)
+               : "m"(x)
+       );
+       return res;
+}
+#endif
+
+#ifdef _MSC_VER
+static void mglut_sincosf(float angle, float *sptr, float *cptr)
+{
+       float s, c;
+       __asm {
+               fld angle
+               fsincos
+               fstp c
+               fstp s
+       }
+       *sptr = s;
+       *cptr = c;
+}
+
+static float mglut_atan(float x)
+{
+       float res;
+       __asm {
+               fld x
+               fld1
+               fpatan
+               fstp res
+       }
+       return res;
+}
+#endif
+
+#ifdef __WATCOMC__
+#pragma aux mglut_sincosf = \
+       "fsincos" \
+       "fstp dword ptr [edx]" \
+       "fstp dword ptr [eax]" \
+       parm[8087][eax][edx]    \
+       modify[8087];
+
+#pragma aux mglut_atan = \
+       "fld1" \
+       "fpatan" \
+       parm[8087] \
+       value[8087] \
+       modify [8087];
+#endif
+
 #ifdef __linux__
 
 #ifdef __x86_64__
@@ -798,8 +1394,7 @@ static void sys_exit(int status)
 {
        asm volatile(
                "syscall\n\t"
-               :: "a"(60), "D"(status)
-       );
+               :: "a"(60), "D"(status));
 }
 static int sys_write(int fd, const void *buf, int count)
 {
@@ -807,8 +1402,16 @@ static int sys_write(int fd, const void *buf, int count)
        asm volatile(
                "syscall\n\t"
                : "=a"(res)
-               : "a"(1), "D"(fd), "S"(buf), "d"(count)
-       );
+               : "a"(1), "D"(fd), "S"(buf), "d"(count));
+       return res;
+}
+static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+       int res;
+       asm volatile(
+               "syscall\n\t"
+               : "=a"(res)
+               : "a"(96), "D"(tv), "S"(tz));
        return res;
 }
 #endif
@@ -816,19 +1419,25 @@ static int sys_write(int fd, const void *buf, int count)
 static void sys_exit(int status)
 {
        asm volatile(
-               "mov $1, %%eax\n\t"
                "int $0x80\n\t"
-               :: "b"(status)
-               : "eax"
-       );
+               :: "a"(1), "b"(status));
 }
 static int sys_write(int fd, const void *buf, int count)
 {
        int res;
        asm volatile(
-               "mov $4, %%eax\n\t"
                "int $0x80\n\t"
-               :: "b"(fd), "c"(buf), "d"(count));
+               : "=a"(res)
+               : "a"(4), "b"(fd), "c"(buf), "d"(count));
+       return res;
+}
+static int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+       int res;
+       asm volatile(
+               "int $0x80\n\t"
+               : "=a"(res)
+               : "a"(78), "b"(tv), "c"(tz));
        return res;
 }
 #endif
@@ -836,13 +1445,13 @@ static int sys_write(int fd, const void *buf, int count)
 #endif /* __linux__ */
 
 #ifdef _WIN32
-static int sys_exit(int status)
+static void sys_exit(int status)
 {
        ExitProcess(status);
 }
 static int sys_write(int fd, const void *buf, int count)
 {
-       int wrsz;
+       unsigned long wrsz = 0;
 
        HANDLE out = GetStdHandle(fd == 1 ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
        if(!WriteFile(out, buf, count, &wrsz, 0)) {