Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "zgl.h"
  2.  
  3. static char *op_table_str[]=
  4. {
  5. #define ADD_OP(a,b,c) "gl" #a " " #c,
  6.  
  7. #include "opinfo.h"
  8. };
  9.  
  10. static void (*op_table_func[])(GLContext *,GLParam *)=
  11. {
  12. #define ADD_OP(a,b,c) glop ## a ,
  13.  
  14. #include "opinfo.h"
  15. };
  16.  
  17. static int op_table_size[]=
  18. {
  19. #define ADD_OP(a,b,c) b + 1 ,
  20.  
  21. #include "opinfo.h"
  22. };
  23.  
  24.  
  25.  
  26. static GLList *find_list(GLContext *c,unsigned int list)
  27. {
  28.   return c->shared_state.lists[list];
  29. }
  30.  
  31. static void delete_list(GLContext *c,int list)
  32. {
  33.   GLParamBuffer *pb,*pb1;
  34.   GLList *l;
  35.  
  36.   l=find_list(c,list);
  37.   assert(l != NULL);
  38.  
  39.   /* free param buffer */
  40.   pb=l->first_op_buffer;
  41.   while (pb!=NULL) {
  42.     pb1=pb->next;
  43.     gl_free(pb);
  44.     pb=pb1;
  45.   }
  46.  
  47.   gl_free(l);
  48.   c->shared_state.lists[list]=NULL;
  49. }
  50.  
  51. static GLList *alloc_list(GLContext *c,int list)
  52. {
  53.   GLList *l;
  54.   GLParamBuffer *ob;
  55.  
  56.   l=gl_zalloc(sizeof(GLList));
  57.   ob=gl_zalloc(sizeof(GLParamBuffer));
  58.  
  59.   ob->next=NULL;
  60.   l->first_op_buffer=ob;
  61.  
  62.   ob->ops[0].op=OP_EndList;
  63.  
  64.   c->shared_state.lists[list]=l;
  65.   return l;
  66. }
  67.  
  68.  
  69. void gl_print_op(FILE *f,GLParam *p)
  70. {
  71.   int op;
  72.   char *s;
  73.  
  74.   op=p[0].op;
  75.   p++;
  76.   s=op_table_str[op];
  77.   while (*s != 0) {
  78.     if (*s == '%') {
  79.       s++;
  80.       switch (*s++) {
  81.       case 'f':
  82.         fprintf(f,"%g",p[0].f);
  83.         break;
  84.       default:
  85.         fprintf(f,"%d",p[0].i);
  86.         break;
  87.       }
  88.       p++;
  89.     } else {
  90.       fputc(*s,f);
  91.       s++;
  92.     }
  93.   }
  94.   fprintf(f,"\n");
  95. }
  96.  
  97.  
  98. void gl_compile_op(GLContext *c,GLParam *p)
  99. {
  100.   int op,op_size;
  101.   GLParamBuffer *ob,*ob1;
  102.   int index,i;
  103.  
  104.   op=p[0].op;
  105.   op_size=op_table_size[op];
  106.   index=c->current_op_buffer_index;
  107.   ob=c->current_op_buffer;
  108.  
  109.   /* we should be able to add a NextBuffer opcode */
  110.   if ((index + op_size) > (OP_BUFFER_MAX_SIZE-2)) {
  111.  
  112.     ob1=gl_zalloc(sizeof(GLParamBuffer));
  113.     ob1->next=NULL;
  114.  
  115.     ob->next=ob1;
  116.     ob->ops[index].op=OP_NextBuffer;
  117.     ob->ops[index+1].p=(void *)ob1;
  118.  
  119.     c->current_op_buffer=ob1;
  120.     ob=ob1;
  121.     index=0;
  122.   }
  123.  
  124.   for(i=0;i<op_size;i++) {
  125.     ob->ops[index]=p[i];
  126.     index++;
  127.   }
  128.   c->current_op_buffer_index=index;
  129. }
  130.  
  131. void gl_add_op(GLParam *p)
  132. {
  133.   GLContext *c=gl_get_context();
  134.   int op;
  135.  
  136.   op=p[0].op;
  137.   if (c->exec_flag) {
  138.     op_table_func[op](c,p);
  139.   }
  140.   if (c->compile_flag) {
  141.     gl_compile_op(c,p);
  142.   }
  143.   if (c->print_flag) {
  144.     gl_print_op(stderr,p);
  145.   }
  146. }
  147.  
  148. /* this opcode is never called directly */
  149. void glopEndList(GLContext *c,GLParam *p)
  150. {
  151.   assert(0);
  152. }
  153.  
  154. /* this opcode is never called directly */
  155. void glopNextBuffer(GLContext *c,GLParam *p)
  156. {
  157.   assert(0);
  158. }
  159.  
  160.  
  161. void glopCallList(GLContext *c,GLParam *p)
  162. {
  163.   GLList *l;
  164.   int list,op;
  165.  
  166.   list=p[1].ui;
  167.   l=find_list(c,list);
  168.   if (l == NULL) gl_fatal_error("list %d not defined",list);
  169.   p=l->first_op_buffer->ops;
  170.  
  171.   while (1) {
  172.     op=p[0].op;
  173.     if (op == OP_EndList) break;
  174.     if (op == OP_NextBuffer) {
  175.       p=(GLParam *)p[1].p;
  176.     } else {
  177.       op_table_func[op](c,p);
  178.       p+=op_table_size[op];
  179.     }
  180.   }
  181. }
  182.  
  183.  
  184.  
  185. void glNewList(unsigned int list,int mode)
  186. {
  187.   GLList *l;
  188.   GLContext *c=gl_get_context();
  189.  
  190.   assert(mode == GL_COMPILE || mode == GL_COMPILE_AND_EXECUTE);
  191.   assert(c->compile_flag == 0);
  192.  
  193.   l=find_list(c,list);
  194.   if (l!=NULL) delete_list(c,list);
  195.   l=alloc_list(c,list);
  196.  
  197.   c->current_op_buffer=l->first_op_buffer;
  198.   c->current_op_buffer_index=0;
  199.  
  200.   c->compile_flag=1;
  201.   c->exec_flag=(mode == GL_COMPILE_AND_EXECUTE);
  202. }
  203.  
  204. void glEndList(void)
  205. {
  206.   GLContext *c=gl_get_context();
  207.   GLParam p[1];
  208.  
  209.   assert(c->compile_flag == 1);
  210.  
  211.   /* end of list */
  212.   p[0].op=OP_EndList;
  213.   gl_compile_op(c,p);
  214.  
  215.   c->compile_flag=0;
  216.   c->exec_flag=1;
  217. }
  218.  
  219. int glIsList(unsigned int list)
  220. {
  221.   GLContext *c=gl_get_context();
  222.   GLList *l;
  223.   l=find_list(c,list);
  224.   return (l != NULL);
  225. }
  226.  
  227. unsigned int glGenLists(int range)
  228. {
  229.   GLContext *c=gl_get_context();
  230.   int count,i,list;
  231.   GLList **lists;
  232.  
  233.   lists=c->shared_state.lists;
  234.   count=0;
  235.   for(i=0;i<MAX_DISPLAY_LISTS;i++) {
  236.     if (lists[i]==NULL) {
  237.       count++;
  238.       if (count == range) {
  239.         list=i-range+1;
  240.         for(i=0;i<range;i++) {
  241.           alloc_list(c,list+i);
  242.         }
  243.         return list;
  244.       }
  245.     } else {
  246.       count=0;
  247.     }
  248.   }
  249.   return 0;
  250. }
  251.  
  252.