Convert crlf->lf line endings.
[mesa.git] / src / mesa / drivers / allegro / amesa.c
index 518211ccf467431bae4131ea82e39f926436b660..c12d42148b61da707b953894f02eafde7fc27fc9 100644 (file)
-/*\r
- * Mesa 3-D graphics library\r
- * Version:  3.0\r
- * Copyright (C) 1995-1998  Brian Paul\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Library General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2 of the License, or (at your option) any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- * Library General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Library General Public\r
- * License along with this library; if not, write to the Free\r
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
- */\r
-\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <allegro.h>\r
-#include "buffers.h"\r
-#include "context.h"\r
-#include "imports.h"\r
-#include "matrix.h"\r
+/*
+ * Mesa 3-D graphics library
+ * Version:  3.0
+ * Copyright (C) 1995-1998  Brian Paul
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <allegro.h>
+#include "buffers.h"
+#include "context.h"
+#include "imports.h"
+#include "matrix.h"
 #include "mtypes.h"
-#include "GL/amesa.h"\r
-\r
-\r
-struct amesa_visual\r
-    {\r
-    GLvisual   *GLVisual;       /* inherit from GLvisual      */\r
-    GLboolean   DBFlag;         /* double buffered?           */\r
-    GLuint      Depth;          /* bits per pixel ( >= 15 )   */\r
-    };\r
-\r
-\r
-struct amesa_buffer\r
-    {\r
-    GLframebuffer *GLBuffer;    /* inherit from GLframebuffer */\r
-    GLuint         Width, Height;\r
-    BITMAP        *Screen;\r
-       BITMAP        *Background;\r
-       BITMAP        *Active;\r
-    };\r
-\r
-\r
-struct amesa_context\r
-    {\r
-    GLcontext   *GLContext;     /* inherit from GLcontext     */\r
-    AMesaVisual  Visual;\r
-       AMesaBuffer  Buffer;\r
-    GLuint       ClearColor;\r
-    GLuint       CurrentColor;\r
-    };\r
-\r
-\r
-static void setup_dd_pointers(GLcontext *ctx);\r
-\r
-\r
-/**********************************************************************/\r
-/*****                   drawing functions                        *****/\r
-/**********************************************************************/\r
-\r
-#define FLIP(context, y)  (context->Buffer->Height - (y) - 1)\r
-\r
-#include "allegro/generic.h"\r
-#include "allegro/direct.h"\r
-\r
-\r
-/**********************************************************************/\r
-/*****            15-bit accelerated drawing funcs                *****/\r
-/**********************************************************************/\r
-\r
-IMPLEMENT_WRITE_RGBA_SPAN(15, unsigned short)\r
-IMPLEMENT_WRITE_RGB_SPAN(15, unsigned short)\r
-IMPLEMENT_WRITE_MONO_RGBA_SPAN(15, unsigned short)\r
-IMPLEMENT_READ_RGBA_SPAN(15, unsigned short)\r
-IMPLEMENT_WRITE_RGBA_PIXELS(15, unsigned short)\r
-IMPLEMENT_WRITE_MONO_RGBA_PIXELS(15, unsigned short)\r
-IMPLEMENT_READ_RGBA_PIXELS(15, unsigned short)\r
-\r
-\r
-/**********************************************************************/\r
-/*****            16-bit accelerated drawing funcs                *****/\r
-/**********************************************************************/\r
-\r
-IMPLEMENT_WRITE_RGBA_SPAN(16, unsigned short)\r
-IMPLEMENT_WRITE_RGB_SPAN(16, unsigned short)\r
-IMPLEMENT_WRITE_MONO_RGBA_SPAN(16, unsigned short)\r
-IMPLEMENT_READ_RGBA_SPAN(16, unsigned short)\r
-IMPLEMENT_WRITE_RGBA_PIXELS(16, unsigned short)\r
-IMPLEMENT_WRITE_MONO_RGBA_PIXELS(16, unsigned short)\r
-IMPLEMENT_READ_RGBA_PIXELS(16, unsigned short)\r
-\r
-\r
-/**********************************************************************/\r
-/*****            32-bit accelerated drawing funcs                *****/\r
-/**********************************************************************/\r
-\r
-IMPLEMENT_WRITE_RGBA_SPAN(32, unsigned long)\r
-IMPLEMENT_WRITE_RGB_SPAN(32, unsigned long)\r
-IMPLEMENT_WRITE_MONO_RGBA_SPAN(32, unsigned long)\r
-IMPLEMENT_READ_RGBA_SPAN(32, unsigned long)\r
-IMPLEMENT_WRITE_RGBA_PIXELS(32, unsigned long)\r
-IMPLEMENT_WRITE_MONO_RGBA_PIXELS(32, unsigned long)\r
-IMPLEMENT_READ_RGBA_PIXELS(32, unsigned long)\r
-\r
-\r
-/**********************************************************************/\r
-/*****              Miscellaneous device driver funcs             *****/\r
-/**********************************************************************/\r
-\r
-static GLboolean set_buffer(GLcontext *ctx, GLframebuffer *buffer, GLuint bit)\r
-    {\r
-    AMesaContext context = (AMesaContext)(ctx->DriverCtx);\r
-    GLboolean    ok      = GL_TRUE;\r
-\r
-    if (bit == DD_FRONT_LEFT_BIT)\r
-        context->Buffer->Active = context->Buffer->Screen;\r
-\r
-    else if (bit == DD_BACK_LEFT)\r
-        {\r
-        if (context->Buffer->Background)\r
-            context->Buffer->Active = context->Buffer->Background;\r
-        else\r
-            ok = GL_FALSE;\r
-        }\r
-\r
-    else\r
-        ok = GL_FALSE;\r
-\r
-    return ok;\r
-    }\r
-\r
-\r
-static void get_buffer_size(GLcontext *ctx, GLuint *width, GLuint *height)\r
-    {\r
-       AMesaContext context = (AMesaContext)(ctx->DriverCtx);\r
-\r
-    *width  = context->Buffer->Width;\r
-    *height = context->Buffer->Height;\r
-    }\r
-\r
-\r
-/**\r
- * We only implement this function as a mechanism to check if the\r
- * framebuffer size has changed (and update corresponding state).\r
- */\r
-static void viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)\r
-{\r
-   /* poll for window size change and realloc software Z/stencil/etc if needed */\r
-   GLuint newWidth, newHeight;\r
-   GLframebuffer *buffer = ctx->WinSysDrawBuffer;\r
-   get_buffer_size( &newWidth, &newHeight );\r
-   if (buffer->Width != newWidth || buffer->Height != newHeight) {\r
-      _mesa_resize_framebuffer(ctx, buffer, newWidth, newHeight );\r
-   }\r
-\r
-}\r
-\r
-\r
-/**********************************************************************/\r
-/**********************************************************************/\r
-\r
-static void setup_dd_pointers(GLcontext *ctx)\r
-       {\r
-       AMesaContext context = (AMesaContext)(ctx->DriverCtx);\r
-\r
-       /* Initialize all the pointers in the driver struct. Do this whenever */\r
-       /* a new context is made current or we change buffers via set_buffer! */\r
-\r
-    ctx->Driver.UpdateState   = setup_dd_pointers;\r
-    ctx->Driver.SetBuffer     = set_buffer;\r
-    ctx->Driver.GetBufferSize = get_buffer_size;\r
-    ctx->Driver.Viewport      = viewport;\r
-\r
-    ctx->Driver.Color               = set_color_generic;\r
-    ctx->Driver.ClearColor          = clear_color_generic;\r
-    ctx->Driver.Clear               = clear_generic;\r
-    ctx->Driver.WriteRGBASpan       = write_rgba_span_generic;\r
-    ctx->Driver.WriteRGBSpan        = write_rgb_span_generic;\r
-    ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_generic;\r
-    ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_generic;\r
-    ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_generic;\r
-    ctx->Driver.ReadRGBASpan        = read_rgba_span_generic;\r
-    ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_generic;\r
-\r
-    if (context->Buffer->Active != screen)\r
-        {\r
-        switch (context->Visual->Depth)\r
-            {\r
-            case 15:\r
-                ctx->Driver.WriteRGBASpan       = write_rgba_span_15;\r
-                ctx->Driver.WriteRGBSpan        = write_rgb_span_15;\r
-                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_15;\r
-                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_15;\r
-                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_15;\r
-                ctx->Driver.ReadRGBASpan        = read_rgba_span_15;\r
-                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_15;\r
-                break;\r
-\r
-            case 16:\r
-                ctx->Driver.WriteRGBASpan       = write_rgba_span_16;\r
-                ctx->Driver.WriteRGBSpan        = write_rgb_span_16;\r
-                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_16;\r
-                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_16;\r
-                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_16;\r
-                ctx->Driver.ReadRGBASpan        = read_rgba_span_16;\r
-                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_16;\r
-                break;\r
-\r
-            case 32:\r
-                ctx->Driver.WriteRGBASpan       = write_rgba_span_32;\r
-                ctx->Driver.WriteRGBSpan        = write_rgb_span_32;\r
-                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_32;\r
-                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_32;\r
-                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_32;\r
-                ctx->Driver.ReadRGBASpan        = read_rgba_span_32;\r
-                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_32;\r
-                break;\r
-            }\r
-        }\r
-       }\r
-\r
-\r
-/**********************************************************************/\r
-/*****                AMesa Public API Functions                  *****/\r
-/**********************************************************************/\r
-\r
-\r
-AMesaVisual AMesaCreateVisual(GLboolean dbFlag, GLint depth,\r
-                              GLint depthSize, GLint stencilSize, GLint accumSize)\r
-       {\r
-       AMesaVisual visual;\r
-    GLbyte      redBits, greenBits, blueBits;\r
-\r
-    visual = (AMesaVisual)calloc(1, sizeof(struct amesa_visual));\r
-       if (!visual)\r
-       return NULL;\r
-\r
-    switch (depth)\r
-        {\r
-        case 15:\r
-            redBits   = 5;\r
-            greenBits = 5;\r
-            blueBits  = 5;\r
-            break;\r
-\r
-        case 16:\r
-            redBits   = 5;\r
-            greenBits = 6;\r
-            blueBits  = 5;\r
-            break;\r
-\r
-        case 24: case 32:\r
-            redBits   = 8;\r
-            greenBits = 8;\r
-            blueBits  = 8;\r
-            break;\r
-\r
-        default:\r
-            free(visual);\r
-            return NULL;\r
-        }\r
-\r
-    visual->DBFlag   = dbFlag;\r
-    visual->Depth    = depth;\r
-    visual->GLVisual = _mesa_create_visual(GL_TRUE,    /* rgb mode       */\r
-                                           dbFlag,     /* db_flag        */\r
-                                           GL_FALSE,   /* stereo         */\r
-                                           redBits, greenBits, blueBits, 8,\r
-                                           0,          /* index bits     */\r
-                                           depthSize,  /* depth bits     */\r
-                                           stencilSize,/* stencil bits   */\r
-                                           accumSize,  /* accum bits     */\r
-                                           accumSize,  /* accum bits     */\r
-                                           accumSize,  /* accum bits     */\r
-                                           accumSize,  /* accum bits     */\r
-                                           1 );\r
-    if (!visual->GLVisual)\r
-        {\r
-        free(visual);\r
-        return NULL;\r
-        }\r
-\r
-       return visual;\r
-       }\r
-\r
-\r
-void AMesaDestroyVisual(AMesaVisual visual)\r
-       {\r
-           _mesa_destroy_visual(visual->GLVisual);\r
-           free(visual);\r
-       }\r
-\r
-\r
-AMesaBuffer AMesaCreateBuffer(AMesaVisual visual,\r
-                                                         GLint width, GLint height)\r
-       {\r
-       AMesaBuffer buffer;\r
-\r
-    buffer = (AMesaBuffer)calloc(1, sizeof(struct amesa_buffer));\r
-       if (!buffer)\r
-       return NULL;\r
-\r
-       buffer->Screen     = NULL;\r
-       buffer->Background = NULL;\r
-       buffer->Active     = NULL;\r
-       buffer->Width      = width;\r
-       buffer->Height     = height;\r
-\r
-       if (visual->DBFlag)\r
-               {\r
-               buffer->Background = create_bitmap_ex(visual->Depth, width, height);\r
-               if (!buffer->Background)\r
-                       {\r
-                       free(buffer);\r
-                       return NULL;\r
-                       }\r
-               }\r
-\r
-    buffer->GLBuffer = _mesa_create_framebuffer(visual->GLVisual);\r
-    if (!buffer->GLBuffer)\r
-        {\r
-        if (buffer->Background) destroy_bitmap(buffer->Background);\r
-        free(buffer);\r
-        return NULL;\r
-        }\r
-\r
-       return buffer;\r
-       }\r
-\r
-\r
-void AMesaDestroyBuffer(AMesaBuffer buffer)\r
-{\r
-   if (buffer->Screen)     destroy_bitmap(buffer->Screen);\r
-   if (buffer->Background) destroy_bitmap(buffer->Background);\r
-   _mesa_unreference_framebuffer(&buffer->GLBuffer);\r
-   free(buffer);\r
-}\r
-\r
-\r
-AMesaContext AMesaCreateContext(AMesaVisual visual,\r
-                                AMesaContext share)\r
-{\r
-       AMesaContext context;\r
-       GLboolean    direct = GL_FALSE;\r
-\r
-       context = (AMesaContext)calloc(1, sizeof(struct amesa_context));\r
-       if (!context)\r
-               return NULL;\r
-\r
-       context->Visual       = visual;\r
-       context->Buffer           = NULL;\r
-       context->ClearColor   = 0;\r
-       context->CurrentColor = 0;\r
-       context->GLContext    = _mesa_create_context(visual->GLVisual,\r
-                                              share ? share->GLContext : NULL,\r
-                                               (void *) context, GL_FALSE );\r
-       if (!context->GLContext)\r
-        {\r
-               free(context);\r
-               return NULL;\r
-        }\r
-\r
-       return context;\r
-}\r
-\r
-\r
-void AMesaDestroyContext(AMesaContext context)\r
-{\r
-   _mesa_destroy_context(context->GLContext);\r
-   free(context);\r
-}\r
-\r
-\r
-GLboolean AMesaMakeCurrent(AMesaContext context, AMesaBuffer buffer)\r
-{\r
-   if (context && buffer) {\r
-      set_color_depth(context->Visual->Depth);\r
-      if (set_gfx_mode(GFX_AUTODETECT, buffer->Width, buffer->Height, 0, 0) != 0)\r
-         return GL_FALSE;\r
-\r
-      context->Buffer = buffer;\r
-      buffer->Screen  = screen;\r
-      buffer->Active  = buffer->Background ? buffer->Background : screen;\r
-        \r
-      setup_dd_pointers(context->GLContext);\r
-      _mesa_make_current(context->GLContext, buffer->GLBuffer);\r
-   }\r
-   else {\r
+#include "GL/amesa.h"
+
+
+struct amesa_visual
+    {
+    GLvisual   *GLVisual;       /* inherit from GLvisual      */
+    GLboolean   DBFlag;         /* double buffered?           */
+    GLuint      Depth;          /* bits per pixel ( >= 15 )   */
+    };
+
+
+struct amesa_buffer
+    {
+    GLframebuffer *GLBuffer;    /* inherit from GLframebuffer */
+    GLuint         Width, Height;
+    BITMAP        *Screen;
+       BITMAP        *Background;
+       BITMAP        *Active;
+    };
+
+
+struct amesa_context
+    {
+    GLcontext   *GLContext;     /* inherit from GLcontext     */
+    AMesaVisual  Visual;
+       AMesaBuffer  Buffer;
+    GLuint       ClearColor;
+    GLuint       CurrentColor;
+    };
+
+
+static void setup_dd_pointers(GLcontext *ctx);
+
+
+/**********************************************************************/
+/*****                   drawing functions                        *****/
+/**********************************************************************/
+
+#define FLIP(context, y)  (context->Buffer->Height - (y) - 1)
+
+#include "allegro/generic.h"
+#include "allegro/direct.h"
+
+
+/**********************************************************************/
+/*****            15-bit accelerated drawing funcs                *****/
+/**********************************************************************/
+
+IMPLEMENT_WRITE_RGBA_SPAN(15, unsigned short)
+IMPLEMENT_WRITE_RGB_SPAN(15, unsigned short)
+IMPLEMENT_WRITE_MONO_RGBA_SPAN(15, unsigned short)
+IMPLEMENT_READ_RGBA_SPAN(15, unsigned short)
+IMPLEMENT_WRITE_RGBA_PIXELS(15, unsigned short)
+IMPLEMENT_WRITE_MONO_RGBA_PIXELS(15, unsigned short)
+IMPLEMENT_READ_RGBA_PIXELS(15, unsigned short)
+
+
+/**********************************************************************/
+/*****            16-bit accelerated drawing funcs                *****/
+/**********************************************************************/
+
+IMPLEMENT_WRITE_RGBA_SPAN(16, unsigned short)
+IMPLEMENT_WRITE_RGB_SPAN(16, unsigned short)
+IMPLEMENT_WRITE_MONO_RGBA_SPAN(16, unsigned short)
+IMPLEMENT_READ_RGBA_SPAN(16, unsigned short)
+IMPLEMENT_WRITE_RGBA_PIXELS(16, unsigned short)
+IMPLEMENT_WRITE_MONO_RGBA_PIXELS(16, unsigned short)
+IMPLEMENT_READ_RGBA_PIXELS(16, unsigned short)
+
+
+/**********************************************************************/
+/*****            32-bit accelerated drawing funcs                *****/
+/**********************************************************************/
+
+IMPLEMENT_WRITE_RGBA_SPAN(32, unsigned long)
+IMPLEMENT_WRITE_RGB_SPAN(32, unsigned long)
+IMPLEMENT_WRITE_MONO_RGBA_SPAN(32, unsigned long)
+IMPLEMENT_READ_RGBA_SPAN(32, unsigned long)
+IMPLEMENT_WRITE_RGBA_PIXELS(32, unsigned long)
+IMPLEMENT_WRITE_MONO_RGBA_PIXELS(32, unsigned long)
+IMPLEMENT_READ_RGBA_PIXELS(32, unsigned long)
+
+
+/**********************************************************************/
+/*****              Miscellaneous device driver funcs             *****/
+/**********************************************************************/
+
+static GLboolean set_buffer(GLcontext *ctx, GLframebuffer *buffer, GLuint bit)
+    {
+    AMesaContext context = (AMesaContext)(ctx->DriverCtx);
+    GLboolean    ok      = GL_TRUE;
+
+    if (bit == DD_FRONT_LEFT_BIT)
+        context->Buffer->Active = context->Buffer->Screen;
+
+    else if (bit == DD_BACK_LEFT)
+        {
+        if (context->Buffer->Background)
+            context->Buffer->Active = context->Buffer->Background;
+        else
+            ok = GL_FALSE;
+        }
+
+    else
+        ok = GL_FALSE;
+
+    return ok;
+    }
+
+
+static void get_buffer_size(GLcontext *ctx, GLuint *width, GLuint *height)
+    {
+       AMesaContext context = (AMesaContext)(ctx->DriverCtx);
+
+    *width  = context->Buffer->Width;
+    *height = context->Buffer->Height;
+    }
+
+
+/**
+ * We only implement this function as a mechanism to check if the
+ * framebuffer size has changed (and update corresponding state).
+ */
+static void viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
+{
+   /* poll for window size change and realloc software Z/stencil/etc if needed */
+   GLuint newWidth, newHeight;
+   GLframebuffer *buffer = ctx->WinSysDrawBuffer;
+   get_buffer_size( &newWidth, &newHeight );
+   if (buffer->Width != newWidth || buffer->Height != newHeight) {
+      _mesa_resize_framebuffer(ctx, buffer, newWidth, newHeight );
+   }
+
+}
+
+
+/**********************************************************************/
+/**********************************************************************/
+
+static void setup_dd_pointers(GLcontext *ctx)
+       {
+       AMesaContext context = (AMesaContext)(ctx->DriverCtx);
+
+       /* Initialize all the pointers in the driver struct. Do this whenever */
+       /* a new context is made current or we change buffers via set_buffer! */
+
+    ctx->Driver.UpdateState   = setup_dd_pointers;
+    ctx->Driver.SetBuffer     = set_buffer;
+    ctx->Driver.GetBufferSize = get_buffer_size;
+    ctx->Driver.Viewport      = viewport;
+
+    ctx->Driver.Color               = set_color_generic;
+    ctx->Driver.ClearColor          = clear_color_generic;
+    ctx->Driver.Clear               = clear_generic;
+    ctx->Driver.WriteRGBASpan       = write_rgba_span_generic;
+    ctx->Driver.WriteRGBSpan        = write_rgb_span_generic;
+    ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_generic;
+    ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_generic;
+    ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_generic;
+    ctx->Driver.ReadRGBASpan        = read_rgba_span_generic;
+    ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_generic;
+
+    if (context->Buffer->Active != screen)
+        {
+        switch (context->Visual->Depth)
+            {
+            case 15:
+                ctx->Driver.WriteRGBASpan       = write_rgba_span_15;
+                ctx->Driver.WriteRGBSpan        = write_rgb_span_15;
+                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_15;
+                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_15;
+                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_15;
+                ctx->Driver.ReadRGBASpan        = read_rgba_span_15;
+                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_15;
+                break;
+
+            case 16:
+                ctx->Driver.WriteRGBASpan       = write_rgba_span_16;
+                ctx->Driver.WriteRGBSpan        = write_rgb_span_16;
+                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_16;
+                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_16;
+                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_16;
+                ctx->Driver.ReadRGBASpan        = read_rgba_span_16;
+                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_16;
+                break;
+
+            case 32:
+                ctx->Driver.WriteRGBASpan       = write_rgba_span_32;
+                ctx->Driver.WriteRGBSpan        = write_rgb_span_32;
+                ctx->Driver.WriteMonoRGBASpan   = write_mono_rgba_span_32;
+                ctx->Driver.WriteRGBAPixels     = write_rgba_pixels_32;
+                ctx->Driver.WriteMonoRGBAPixels = write_mono_rgba_pixels_32;
+                ctx->Driver.ReadRGBASpan        = read_rgba_span_32;
+                ctx->Driver.ReadRGBAPixels      = read_rgba_pixels_32;
+                break;
+            }
+        }
+       }
+
+
+/**********************************************************************/
+/*****                AMesa Public API Functions                  *****/
+/**********************************************************************/
+
+
+AMesaVisual AMesaCreateVisual(GLboolean dbFlag, GLint depth,
+                              GLint depthSize, GLint stencilSize, GLint accumSize)
+       {
+       AMesaVisual visual;
+    GLbyte      redBits, greenBits, blueBits;
+
+    visual = (AMesaVisual)calloc(1, sizeof(struct amesa_visual));
+       if (!visual)
+       return NULL;
+
+    switch (depth)
+        {
+        case 15:
+            redBits   = 5;
+            greenBits = 5;
+            blueBits  = 5;
+            break;
+
+        case 16:
+            redBits   = 5;
+            greenBits = 6;
+            blueBits  = 5;
+            break;
+
+        case 24: case 32:
+            redBits   = 8;
+            greenBits = 8;
+            blueBits  = 8;
+            break;
+
+        default:
+            free(visual);
+            return NULL;
+        }
+
+    visual->DBFlag   = dbFlag;
+    visual->Depth    = depth;
+    visual->GLVisual = _mesa_create_visual(GL_TRUE,    /* rgb mode       */
+                                           dbFlag,     /* db_flag        */
+                                           GL_FALSE,   /* stereo         */
+                                           redBits, greenBits, blueBits, 8,
+                                           0,          /* index bits     */
+                                           depthSize,  /* depth bits     */
+                                           stencilSize,/* stencil bits   */
+                                           accumSize,  /* accum bits     */
+                                           accumSize,  /* accum bits     */
+                                           accumSize,  /* accum bits     */
+                                           accumSize,  /* accum bits     */
+                                           1 );
+    if (!visual->GLVisual)
+        {
+        free(visual);
+        return NULL;
+        }
+
+       return visual;
+       }
+
+
+void AMesaDestroyVisual(AMesaVisual visual)
+       {
+           _mesa_destroy_visual(visual->GLVisual);
+           free(visual);
+       }
+
+
+AMesaBuffer AMesaCreateBuffer(AMesaVisual visual,
+                                                         GLint width, GLint height)
+       {
+       AMesaBuffer buffer;
+
+    buffer = (AMesaBuffer)calloc(1, sizeof(struct amesa_buffer));
+       if (!buffer)
+       return NULL;
+
+       buffer->Screen     = NULL;
+       buffer->Background = NULL;
+       buffer->Active     = NULL;
+       buffer->Width      = width;
+       buffer->Height     = height;
+
+       if (visual->DBFlag)
+               {
+               buffer->Background = create_bitmap_ex(visual->Depth, width, height);
+               if (!buffer->Background)
+                       {
+                       free(buffer);
+                       return NULL;
+                       }
+               }
+
+    buffer->GLBuffer = _mesa_create_framebuffer(visual->GLVisual);
+    if (!buffer->GLBuffer)
+        {
+        if (buffer->Background) destroy_bitmap(buffer->Background);
+        free(buffer);
+        return NULL;
+        }
+
+       return buffer;
+       }
+
+
+void AMesaDestroyBuffer(AMesaBuffer buffer)
+{
+   if (buffer->Screen)     destroy_bitmap(buffer->Screen);
+   if (buffer->Background) destroy_bitmap(buffer->Background);
+   _mesa_unreference_framebuffer(&buffer->GLBuffer);
+   free(buffer);
+}
+
+
+AMesaContext AMesaCreateContext(AMesaVisual visual,
+                                AMesaContext share)
+{
+       AMesaContext context;
+       GLboolean    direct = GL_FALSE;
+
+       context = (AMesaContext)calloc(1, sizeof(struct amesa_context));
+       if (!context)
+               return NULL;
+
+       context->Visual       = visual;
+       context->Buffer           = NULL;
+       context->ClearColor   = 0;
+       context->CurrentColor = 0;
+       context->GLContext    = _mesa_create_context(visual->GLVisual,
+                                              share ? share->GLContext : NULL,
+                                               (void *) context, GL_FALSE );
+       if (!context->GLContext)
+        {
+               free(context);
+               return NULL;
+        }
+
+       return context;
+}
+
+
+void AMesaDestroyContext(AMesaContext context)
+{
+   _mesa_destroy_context(context->GLContext);
+   free(context);
+}
+
+
+GLboolean AMesaMakeCurrent(AMesaContext context, AMesaBuffer buffer)
+{
+   if (context && buffer) {
+      set_color_depth(context->Visual->Depth);
+      if (set_gfx_mode(GFX_AUTODETECT, buffer->Width, buffer->Height, 0, 0) != 0)
+         return GL_FALSE;
+
+      context->Buffer = buffer;
+      buffer->Screen  = screen;
+      buffer->Active  = buffer->Background ? buffer->Background : screen;
+        
+      setup_dd_pointers(context->GLContext);
+      _mesa_make_current(context->GLContext, buffer->GLBuffer);
+   }
+   else {
       /* XXX I don't think you want to destroy anything here! */
-      destroy_bitmap(context->Buffer->Screen);\r
-      context->Buffer->Screen = NULL;\r
-      context->Buffer->Active = NULL;\r
-      context->Buffer         = NULL;\r
-      _mesa_make_current(NULL, NULL);\r
-   }\r
-\r
-   return GL_TRUE;\r
-}\r
-\r
-\r
-void AMesaSwapBuffers(AMesaBuffer buffer)\r
-{\r
-   if (buffer->Background) {\r
-      blit(buffer->Background, buffer->Screen,\r
-           0, 0, 0, 0,\r
-           buffer->Width, buffer->Height);\r
-   }\r
-}\r
+      destroy_bitmap(context->Buffer->Screen);
+      context->Buffer->Screen = NULL;
+      context->Buffer->Active = NULL;
+      context->Buffer         = NULL;
+      _mesa_make_current(NULL, NULL);
+   }
+
+   return GL_TRUE;
+}
+
+
+void AMesaSwapBuffers(AMesaBuffer buffer)
+{
+   if (buffer->Background) {
+      blit(buffer->Background, buffer->Screen,
+           0, 0, 0, 0,
+           buffer->Width, buffer->Height);
+   }
+}