[1] | 1 | #include <GL/oscontext.h> |
---|
| 2 | #include "zbuffer.h" |
---|
| 3 | #include "zgl.h" |
---|
| 4 | #include <GL/gl.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <assert.h> |
---|
| 7 | |
---|
| 8 | static int buffercnt = 0; |
---|
| 9 | |
---|
| 10 | ostgl_context * |
---|
| 11 | ostgl_create_context(const int xsize, |
---|
| 12 | const int ysize, |
---|
| 13 | const int depth, |
---|
| 14 | void **framebuffers, |
---|
| 15 | const int numbuffers) |
---|
| 16 | { |
---|
| 17 | ostgl_context *context; |
---|
| 18 | int i; |
---|
| 19 | ZBuffer *zb; |
---|
| 20 | |
---|
| 21 | assert(depth == 16); /* support for other depths must include bpp |
---|
| 22 | convertion */ |
---|
| 23 | assert(numbuffers >= 1); |
---|
| 24 | |
---|
| 25 | context = gl_malloc(sizeof(ostgl_context)); |
---|
| 26 | assert(context); |
---|
| 27 | context->zbs = gl_malloc(sizeof(void*)*numbuffers); |
---|
| 28 | context->framebuffers = gl_malloc(sizeof(void*)*numbuffers); |
---|
| 29 | |
---|
| 30 | assert(context->zbs != NULL && context->framebuffers != NULL); |
---|
| 31 | |
---|
| 32 | for (i = 0; i < numbuffers; i++) { |
---|
| 33 | context->framebuffers[i] = framebuffers[i]; |
---|
| 34 | zb = ZB_open(xsize, ysize, ZB_MODE_5R6G5B, 0, NULL, NULL, framebuffers[i]); |
---|
| 35 | if (zb == NULL) { |
---|
| 36 | fprintf(stderr, "Error while initializing Z buffer\n"); |
---|
| 37 | exit(1); |
---|
| 38 | } |
---|
| 39 | context->zbs[i] = zb; |
---|
| 40 | } |
---|
| 41 | if (++buffercnt == 1) { |
---|
| 42 | glInit(context->zbs[0]); |
---|
| 43 | } |
---|
| 44 | context->xsize = xsize; |
---|
| 45 | context->ysize = ysize; |
---|
| 46 | context->numbuffers = numbuffers; |
---|
| 47 | context->depth = 16; |
---|
| 48 | context->bytes_per_line = xsize; |
---|
| 49 | return context; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | void |
---|
| 53 | ostgl_delete_context(ostgl_context *context) |
---|
| 54 | { |
---|
| 55 | int i; |
---|
| 56 | for (i = 0; i < context->numbuffers; i++) { |
---|
| 57 | ZB_close(context->zbs[i]); |
---|
| 58 | } |
---|
| 59 | gl_free(context->zbs); |
---|
| 60 | gl_free(context->framebuffers); |
---|
| 61 | gl_free(context); |
---|
| 62 | |
---|
| 63 | if (--buffercnt == 0) { |
---|
| 64 | glClose(); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void |
---|
| 69 | ostgl_make_current(ostgl_context *oscontext, const int idx) |
---|
| 70 | { |
---|
| 71 | GLContext *context = gl_get_context(); |
---|
| 72 | assert(idx < oscontext->numbuffers); |
---|
| 73 | context->zb = oscontext->zbs[idx]; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void |
---|
| 77 | ostgl_resize(ostgl_context *context, |
---|
| 78 | const int xsize, |
---|
| 79 | const int ysize, |
---|
| 80 | void **framebuffers) |
---|
| 81 | { |
---|
| 82 | int i; |
---|
| 83 | for (i = 0; i < context->numbuffers; i++) { |
---|
| 84 | ZB_resize(context->zbs[i], framebuffers[i], xsize, ysize); |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | void |
---|
| 90 | ostgl_swap_buffers(ostgl_context *context) |
---|
| 91 | { |
---|
| 92 | int i; |
---|
| 93 | |
---|
| 94 | for(i = 0; i < context->numbuffers; i++) { |
---|
| 95 | ZB_copyFrameBuffer(context->zbs[i], context->framebuffers[i], context->bytes_per_line); |
---|
| 96 | } |
---|
| 97 | } |
---|