aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/tinygl-0.4-ugfx/src/nglx.c
blob: 26f356c2ed4a22ea8a34940c52e9ce620f14e7d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* simple glx like driver for TinyGL and Nano X */
#include <GL/gl.h>
#include <GL/nglx.h>
#include <microwin/nano-X.h>
#include "zgl.h"

typedef struct {
    GLContext *gl_context;
    int xsize,ysize;
    GR_DRAW_ID drawable;
    GR_GC_ID gc;
    int pixtype; /* pixel type in TinyGL */
} TinyNGLXContext;

NGLXContext nglXCreateContext(NGLXContext shareList, int flags)
{
  TinyNGLXContext *ctx;

  if (shareList != NULL) {
    gl_fatal_error("No sharing available in TinyGL");
  }
  ctx=gl_malloc(sizeof(TinyNGLXContext));
  if (!ctx)
      return NULL;
  ctx->gl_context=NULL;
  return (NGLXContext) ctx;
}

void glXDestroyContext( NGLXContext ctx1 )
{
  TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1;
  if (ctx->gl_context != NULL) {
    glClose();
  }
  gl_free(ctx);
}


/* resize the glx viewport : we try to use the xsize and ysize
   given. We return the effective size which is guaranted to be smaller */

static int glX_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr)
{
  TinyNGLXContext *ctx;
  int xsize,ysize;
  
  ctx=(TinyNGLXContext *)c->opaque;

  xsize=*xsize_ptr;
  ysize=*ysize_ptr;

  /* we ensure that xsize and ysize are multiples of 2 for the zbuffer. 
     TODO: find a better solution */
  xsize&=~3;
  ysize&=~3;

  if (xsize == 0 || ysize == 0) return -1;

  *xsize_ptr=xsize;
  *ysize_ptr=ysize;

  ctx->xsize=xsize;
  ctx->ysize=ysize;

  /* resize the Z buffer */
  ZB_resize(c->zb,NULL,xsize,ysize);
  return 0;
}

/* we assume here that drawable is a window */
int nglXMakeCurrent( NGLXDrawable drawable,
                     NGLXContext ctx1)
{
  TinyNGLXContext *ctx = (TinyNGLXContext *) ctx1;
  int mode, xsize, ysize;
  ZBuffer *zb;
  GR_WINDOW_INFO win_info;

  if (ctx->gl_context == NULL) {
      /* create the TinyGL context */
      GrGetWindowInfo(drawable, &win_info);

      xsize = win_info.width;
      ysize = win_info.height;

      /* currently, we only support 16 bit rendering */
      mode = ZB_MODE_5R6G5B;
      zb=ZB_open(xsize,ysize,mode,0,NULL,NULL,NULL);
      if (zb == NULL) {
          fprintf(stderr, "Error while initializing Z buffer\n");
          exit(1);
      }

      ctx->pixtype = MWPF_TRUECOLOR565;

      /* create a gc */
      ctx->gc = GrNewGC();
      
      /* initialisation of the TinyGL interpreter */
      glInit(zb);
      ctx->gl_context=gl_get_context();
      ctx->gl_context->opaque=(void *) ctx;
      ctx->gl_context->gl_resize_viewport=glX_resize_viewport;

      /* set the viewport : we force a call to glX_resize_viewport */
      ctx->gl_context->viewport.xsize=-1;
      ctx->gl_context->viewport.ysize=-1;
      
      glViewport(0, 0, xsize, ysize);
  }
  
  return 1;
}

void nglXSwapBuffers( NGLXDrawable drawable )
{
    GLContext *gl_context;
    TinyNGLXContext *ctx;
    
    /* retrieve the current NGLXContext */
    gl_context=gl_get_context();
    ctx=(TinyNGLXContext *)gl_context->opaque;
    
    GrArea(drawable, ctx->gc, 0, 0, ctx->xsize, 
           ctx->ysize, ctx->gl_context->zb->pbuf, ctx->pixtype);
}