ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

graphics/tinygl/list.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 /*
00024  * This file is based on, or a modified version of code from TinyGL (C) 1997-1998 Fabrice Bellard,
00025  * which is licensed under the zlib-license (see LICENSE).
00026  * It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
00027  */
00028 
00029 #define FORBIDDEN_SYMBOL_EXCEPTION_FILE
00030 #define FORBIDDEN_SYMBOL_EXCEPTION_fprintf
00031 #define FORBIDDEN_SYMBOL_EXCEPTION_fputc
00032 #define FORBIDDEN_SYMBOL_EXCEPTION_stderr
00033 
00034 #include "graphics/tinygl/zgl.h"
00035 
00036 namespace TinyGL {
00037 
00038 static const char *op_table_str[] = {
00039 #define ADD_OP(a, b, c) "gl" #a " " #c,
00040 
00041 #include "graphics/tinygl/opinfo.h"
00042 };
00043 
00044 static void (*op_table_func[])(GLContext *, GLParam *) = {
00045 #define ADD_OP(a, b, c) glop ## a ,
00046 
00047 #include "graphics/tinygl/opinfo.h"
00048 };
00049 
00050 static int op_table_size[] = {
00051 #define ADD_OP(a, b, c) b + 1 ,
00052 
00053 #include "graphics/tinygl/opinfo.h"
00054 };
00055 
00056 GLContext *gl_get_context() {
00057     return gl_ctx;
00058 }
00059 
00060 static GLList *find_list(GLContext *c, unsigned int list) {
00061     return c->shared_state.lists[list];
00062 }
00063 
00064 static void delete_list(GLContext *c, int list) {
00065     GLParamBuffer *pb, *pb1;
00066     GLList *l;
00067 
00068     l = find_list(c, list);
00069     assert(l);
00070 
00071     // free param buffer
00072     pb = l->first_op_buffer;
00073     while (pb) {
00074         pb1 = pb->next;
00075         gl_free(pb);
00076         pb = pb1;
00077     }
00078 
00079     gl_free(l);
00080     c->shared_state.lists[list] = NULL;
00081 }
00082 
00083 static GLList *alloc_list(GLContext *c, int list) {
00084     GLList *l;
00085     GLParamBuffer *ob;
00086 
00087     l = (GLList *)gl_zalloc(sizeof(GLList));
00088     ob = (GLParamBuffer *)gl_zalloc(sizeof(GLParamBuffer));
00089 
00090     ob->next = NULL;
00091     l->first_op_buffer = ob;
00092 
00093     ob->ops[0].op = OP_EndList;
00094 
00095     c->shared_state.lists[list] = l;
00096     return l;
00097 }
00098 
00099 void gl_print_op(FILE *f, GLParam *p) {
00100     int op;
00101     const char *s;
00102 
00103     op = p[0].op;
00104     p++;
00105     s = op_table_str[op];
00106     while (*s != 0) {
00107         if (*s == '%') {
00108             s++;
00109             switch (*s++) {
00110             case 'f':
00111                 fprintf(f, "%g", p[0].f);
00112                 break;
00113             default:
00114                 fprintf(f, "%d", p[0].i);
00115                 break;
00116             }
00117             p++;
00118         } else {
00119             fputc(*s, f);
00120             s++;
00121         }
00122     }
00123     fprintf(f, "\n");
00124 }
00125 
00126 
00127 void gl_compile_op(GLContext *c, GLParam *p) {
00128     int op, op_size;
00129     GLParamBuffer *ob, *ob1;
00130     int index;
00131 
00132     op = p[0].op;
00133     op_size = op_table_size[op];
00134     index = c->current_op_buffer_index;
00135     ob = c->current_op_buffer;
00136 
00137     // we should be able to add a NextBuffer opcode
00138     if ((index + op_size) > (OP_BUFFER_MAX_SIZE - 2)) {
00139 
00140         ob1 = (GLParamBuffer *)gl_zalloc(sizeof(GLParamBuffer));
00141         ob1->next = NULL;
00142 
00143         ob->next = ob1;
00144         ob->ops[index].op = OP_NextBuffer;
00145         ob->ops[index + 1].p = (void *)ob1;
00146 
00147         c->current_op_buffer = ob1;
00148         ob = ob1;
00149         index = 0;
00150     }
00151 
00152     for (int i = 0; i < op_size; i++) {
00153         ob->ops[index] = p[i];
00154         index++;
00155     }
00156     c->current_op_buffer_index = index;
00157 }
00158 
00159 void gl_add_op(GLParam *p) {
00160     GLContext *c = gl_get_context();
00161     int op;
00162 
00163     op = p[0].op;
00164     if (c->exec_flag) {
00165         op_table_func[op](c, p);
00166     }
00167     if (c->compile_flag) {
00168         gl_compile_op(c, p);
00169     }
00170     if (c->print_flag) {
00171         gl_print_op(stderr, p);
00172     }
00173 }
00174 
00175 // this opcode is never called directly
00176 void glopEndList(GLContext *, GLParam *) {
00177     assert(0);
00178 }
00179 
00180 // this opcode is never called directly
00181 void glopNextBuffer(GLContext *, GLParam *) {
00182     assert(0);
00183 }
00184 
00185 void glopCallList(GLContext *c, GLParam *p) {
00186     GLList *l;
00187     int list, op;
00188 
00189     list = p[1].ui;
00190     l = find_list(c, list);
00191     if (!l)
00192         error("list %d not defined", list);
00193     p = l->first_op_buffer->ops;
00194 
00195     while (1) {
00196         op = p[0].op;
00197         if (op == OP_EndList)
00198             break;
00199         if (op == OP_NextBuffer) {
00200             p = (GLParam *)p[1].p;
00201         } else {
00202             op_table_func[op](c, p);
00203             p += op_table_size[op];
00204         }
00205     }
00206 }
00207 
00208 void glNewList(unsigned int list, int mode) {
00209     GLList *l;
00210     GLContext *c = gl_get_context();
00211 
00212     assert(mode == TGL_COMPILE || mode == TGL_COMPILE_AND_EXECUTE);
00213     assert(c->compile_flag == 0);
00214 
00215     l = find_list(c, list);
00216     if (l)
00217         delete_list(c, list);
00218     l = alloc_list(c, list);
00219 
00220     c->current_op_buffer = l->first_op_buffer;
00221     c->current_op_buffer_index = 0;
00222 
00223     c->compile_flag = 1;
00224     c->exec_flag = (mode == TGL_COMPILE_AND_EXECUTE);
00225 }
00226 
00227 void glEndList() {
00228     GLContext *c = gl_get_context();
00229     GLParam p[1];
00230 
00231     assert(c->compile_flag == 1);
00232 
00233     // end of list
00234     p[0].op = OP_EndList;
00235     gl_compile_op(c, p);
00236 
00237     c->compile_flag = 0;
00238     c->exec_flag = 1;
00239 }
00240 
00241 int glIsList(unsigned int list) {
00242     GLContext *c = gl_get_context();
00243     GLList *l = find_list(c, list);
00244 
00245     return (l != NULL);
00246 }
00247 
00248 unsigned int glGenLists(int range) {
00249     GLContext *c = gl_get_context();
00250     int count, list;
00251     GLList **lists;
00252 
00253     lists = c->shared_state.lists;
00254     count = 0;
00255     for (int i = 0; i < MAX_DISPLAY_LISTS; i++) {
00256         if (!lists[i]) {
00257             count++;
00258             if (count == range) {
00259                 list = i - range + 1;
00260                 for (int j = 0; j < range; j++) {
00261                     alloc_list(c, list + j);
00262                 }
00263                 return list;
00264             }
00265         } else {
00266             count = 0;
00267         }
00268     }
00269     return 0;
00270 }
00271 
00272 } // end of namespace TinyGL


Generated on Sat Feb 23 2019 05:01:07 for ResidualVM by doxygen 1.7.1
curved edge   curved edge