Merge branch 'master' of ssh://repo.or.cz/srv/git/tgl
[tgl.git] / src / nglx.c
blob621abab42841783b27a7506134e447baec4c4897
1 /* simple glx like driver for TinyGL and Nano X */
2 #include <GL/gl.h>
3 #include <GL/nglx.h>
4 #include <microwin/nano-X.h>
5 #include "zgl.h"
7 typedef struct {
8 GLContext *gl_context;
9 int xsize,ysize;
10 GR_DRAW_ID drawable;
11 GR_GC_ID gc;
12 int pixtype; /* pixel type in TinyGL */
13 } TinyNGLXContext;
15 NGLXContext nglXCreateContext(NGLXContext shareList, int flags)
17 TinyNGLXContext *ctx;
19 if (shareList != NULL) {
20 gl_fatal_error("No sharing available in TinyGL");
22 ctx=gl_malloc(sizeof(TinyNGLXContext));
23 if (!ctx)
24 return NULL;
25 ctx->gl_context=NULL;
26 return (NGLXContext) ctx;
29 void glXDestroyContext( NGLXContext ctx1 )
31 TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1;
32 if (ctx->gl_context != NULL) {
33 glClose();
35 gl_free(ctx);
39 /* resize the glx viewport : we try to use the xsize and ysize
40 given. We return the effective size which is guaranted to be smaller */
42 static int glX_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr)
44 TinyNGLXContext *ctx;
45 int xsize,ysize;
47 ctx=(TinyNGLXContext *)c->opaque;
49 xsize=*xsize_ptr;
50 ysize=*ysize_ptr;
52 /* we ensure that xsize and ysize are multiples of 2 for the zbuffer.
53 TODO: find a better solution */
54 xsize&=~3;
55 ysize&=~3;
57 if (xsize == 0 || ysize == 0) return -1;
59 *xsize_ptr=xsize;
60 *ysize_ptr=ysize;
62 ctx->xsize=xsize;
63 ctx->ysize=ysize;
65 /* resize the Z buffer */
66 ZB_resize(c->zb,NULL,xsize,ysize);
67 return 0;
70 /* we assume here that drawable is a window */
71 int nglXMakeCurrent( NGLXDrawable drawable,
72 NGLXContext ctx1)
74 TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1;
75 int mode, xsize, ysize;
76 ZBuffer *zb;
77 GR_WINDOW_INFO win_info;
79 if (ctx->gl_context == NULL) {
80 /* create the TinyGL context */
81 GrGetWindowInfo(drawable, &win_info);
83 xsize = win_info.width;
84 ysize = win_info.height;
86 /* currently, we only support 16 bit rendering */
87 mode = ZB_MODE_5R6G5B;
88 zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL);
89 if (zb == NULL) {
90 fprintf(stderr, "Error while initializing Z buffer\n");
91 exit(1);
94 ctx->pixtype = MWPF_TRUECOLOR565;
96 /* create a gc */
97 ctx->gc = GrNewGC();
99 /* initialisation of the TinyGL interpreter */
100 glInit(zb);
101 ctx->gl_context=gl_get_context();
102 ctx->gl_context->opaque=(void *) ctx;
103 ctx->gl_context->gl_resize_viewport=glX_resize_viewport;
105 /* set the viewport : we force a call to glX_resize_viewport */
106 ctx->gl_context->viewport.xsize=-1;
107 ctx->gl_context->viewport.ysize=-1;
109 glViewport(0, 0, xsize, ysize);
112 return 1;
115 void nglXSwapBuffers( NGLXDrawable drawable )
117 GLContext *gl_context;
118 TinyNGLXContext *ctx;
120 /* retrieve the current NGLXContext */
121 gl_context=gl_get_context();
122 ctx=(TinyNGLXContext *)gl_context->opaque;
124 GrArea(drawable, ctx->gc, 0, 0, ctx->xsize,
125 ctx->ysize, ctx->gl_context->zb->pbuf, ctx->pixtype);