Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5098 | clevermous | 1 | /* simple gl like driver for TinyGL and KolibriOS - porting iadn */ |
2 | #include |
||
3 | #include "kosgl.h" |
||
4 | #include "zgl.h" |
||
5 | |||
6 | |||
7 | |||
8 | typedef struct { |
||
9 | GLContext *gl_context; |
||
10 | int xsize,ysize; |
||
11 | int dx,dy; |
||
12 | int x,y; |
||
13 | } TinyGLContext; |
||
14 | |||
15 | |||
16 | KOSGLContext kosglCreateContext(KOSGLContext shareList, int flags) |
||
17 | { |
||
18 | TinyGLContext *ctx; |
||
19 | |||
20 | if (shareList != NULL) { |
||
21 | gl_fatal_error("No sharing available in TinyGL"); |
||
22 | } |
||
23 | |||
24 | ctx=gl_malloc(sizeof(TinyGLContext)); |
||
25 | if (!ctx) |
||
26 | return NULL; |
||
27 | ctx->gl_context=NULL; |
||
28 | return (KOSGLContext) ctx; |
||
29 | } |
||
30 | |||
31 | void kosglDestroyContext( KOSGLContext ctx1 ) |
||
32 | { |
||
33 | TinyGLContext *ctx = (TinyGLContext *) ctx1; |
||
34 | if (ctx->gl_context != NULL) { |
||
35 | glClose(); |
||
36 | } |
||
37 | gl_free(ctx); |
||
38 | } |
||
39 | |||
40 | /* resize the glx viewport : we try to use the xsize and ysize |
||
41 | given. We return the effective size which is guaranted to be smaller */ |
||
42 | |||
43 | static int gl_resize_viewport(GLContext *c,int *xsize_ptr,int *ysize_ptr) |
||
44 | { |
||
45 | TinyGLContext *ctx; |
||
46 | int xsize,ysize; |
||
47 | |||
48 | ctx=(TinyGLContext *)c->opaque; |
||
49 | |||
50 | xsize=*xsize_ptr; |
||
51 | ysize=*ysize_ptr; |
||
52 | |||
53 | /* we ensure that xsize and ysize are multiples of 2 for the zbuffer. |
||
54 | TODO: find a better solution */ |
||
55 | xsize&=~3; |
||
56 | ysize&=~3; |
||
57 | |||
58 | if (xsize == 0 || ysize == 0) return -1; |
||
59 | |||
60 | *xsize_ptr=xsize-1; |
||
61 | *ysize_ptr=ysize-1; |
||
62 | ctx->dx = xsize; |
||
63 | ctx->dy = ysize; |
||
64 | |||
65 | ctx->xsize=xsize; |
||
66 | ctx->ysize=ysize; |
||
67 | |||
68 | /* resize the Z buffer */ |
||
69 | ZB_resize(c->zb,NULL,xsize,ysize); |
||
70 | return 0; |
||
71 | } |
||
72 | |||
73 | /* we assume here that drawable is a window */ |
||
74 | int kosglMakeCurrent( int win_x0, int win_y0,int win_x, int win_y, KOSGLContext ctx1) |
||
75 | { |
||
76 | TinyGLContext *ctx = (TinyGLContext *) ctx1; |
||
77 | int mode; |
||
78 | ZBuffer *zb; |
||
79 | |||
80 | if (ctx->gl_context == NULL) { |
||
81 | /* create the TinyGL context */ |
||
82 | ctx->x = win_x0; |
||
83 | ctx->y = win_y0; |
||
84 | ctx->dx = win_x; |
||
85 | ctx->dy = win_y; |
||
86 | |||
87 | /* currently, we only support 16 bit rendering */ |
||
88 | mode = ZB_MODE_RGB24; |
||
89 | zb=ZB_open(win_x,win_y,mode,0,NULL,NULL,NULL); |
||
90 | |||
91 | if (zb == NULL) { |
||
92 | fprintf(stderr, "Error while initializing Z buffer\n"); |
||
93 | exit(1); |
||
94 | } |
||
95 | |||
96 | /* initialisation of the TinyGL interpreter */ |
||
97 | glInit(zb); |
||
98 | |||
99 | ctx->gl_context=gl_get_context(); |
||
100 | |||
101 | ctx->gl_context->opaque=(void *) ctx; |
||
102 | ctx->gl_context->gl_resize_viewport=gl_resize_viewport; |
||
103 | |||
104 | /* set the viewport : we force a call to gl_resize_viewport */ |
||
105 | ctx->gl_context->viewport.xsize=-1; |
||
106 | ctx->gl_context->viewport.ysize=-1; |
||
107 | |||
108 | glViewport(0, 0, win_x, win_y); |
||
109 | |||
110 | } |
||
111 | return 1; |
||
112 | } |
||
113 | |||
114 | void kosglSwapBuffers( ) |
||
115 | { |
||
116 | GLContext *gl_context; |
||
117 | TinyGLContext *ctx; |
||
118 | |||
119 | /* retrieve the current TinyGLContext */ |
||
120 | gl_context=gl_get_context(); |
||
121 | ctx=(TinyGLContext *)gl_context->opaque; |
||
122 | |||
123 | __asm__ __volatile__("int $0x40"::"a"(7), |
||
124 | "b"((char *)gl_context->zb->pbuf), |
||
125 | "c"((ctx->dx<<16)|ctx->dy), |
||
126 | "d"((ctx->x<<16)|ctx->y)); |
||
127 | }16)|ctx-><16)|ctx->16)|ctx-><16)|ctx-> |