scummvm/graphics/tinygl/list.cpp

245 lines
5.1 KiB
C++
Raw Permalink Normal View History

2021-12-26 21:19:38 +01:00
/* ScummVM - Graphic Adventure Engine
2014-08-13 18:52:52 +02:00
*
2021-12-26 21:19:38 +01:00
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
2014-08-13 18:52:52 +02:00
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2014-08-13 18:52:52 +02:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2014-08-13 18:52:52 +02:00
*
*/
/*
* This file is based on, or a modified version of code from TinyGL (C) 1997-2022 Fabrice Bellard,
* which is licensed under the MIT license (see LICENSE).
2014-08-13 18:52:52 +02:00
* It also has modifications by the ResidualVM-team, which are covered under the GPLv2 (or later).
*/
#include "common/streamdebug.h"
2006-05-16 14:52:36 +00:00
2009-05-08 07:32:33 +00:00
#include "graphics/tinygl/zgl.h"
namespace TinyGL {
#define ADD_OP(aa, bb, ff) \
2022-01-01 12:00:38 +01:00
static void glop ## aa (GLContext *c, GLParam *p) { \
c->glop ## aa (p); \
}
#include "graphics/tinygl/opinfo.h"
static const char *op_table_str[] = {
2006-05-16 14:52:36 +00:00
#define ADD_OP(a, b, c) "gl" #a " " #c,
2009-05-08 07:32:33 +00:00
#include "graphics/tinygl/opinfo.h"
};
static void (*op_table_func[])(GLContext *, GLParam *) = {
2006-05-16 14:52:36 +00:00
#define ADD_OP(a, b, c) glop ## a ,
2009-05-08 07:32:33 +00:00
#include "graphics/tinygl/opinfo.h"
};
2006-05-16 14:52:36 +00:00
static int op_table_size[] = {
#define ADD_OP(a, b, c) b + 1 ,
2009-05-08 07:32:33 +00:00
#include "graphics/tinygl/opinfo.h"
};
GLList *GLContext::find_list(uint list) {
return shared_state.lists[list];
}
void GLContext::delete_list(int list) {
2022-01-02 09:22:49 +01:00
GLList *l = find_list(list);
2008-09-10 11:16:57 +00:00
assert(l);
2006-05-16 14:52:36 +00:00
// free param buffer
2022-01-02 09:22:49 +01:00
GLParamBuffer *pb = l->first_op_buffer;
2008-09-10 11:16:57 +00:00
while (pb) {
2022-01-02 09:22:49 +01:00
GLParamBuffer *pb1 = pb->next;
2006-05-16 14:52:36 +00:00
gl_free(pb);
pb = pb1;
}
2006-05-16 14:52:36 +00:00
gl_free(l);
shared_state.lists[list] = nullptr;
}
GLList *GLContext::alloc_list(int list) {
2022-01-02 09:22:49 +01:00
GLList *l = (GLList *)gl_zalloc(sizeof(GLList));
GLParamBuffer *ob = (GLParamBuffer *)gl_zalloc(sizeof(GLParamBuffer));
2022-01-01 12:00:38 +01:00
ob->next = nullptr;
2006-05-16 14:52:36 +00:00
l->first_op_buffer = ob;
2006-05-16 14:52:36 +00:00
ob->ops[0].op = OP_EndList;
shared_state.lists[list] = l;
2006-05-16 14:52:36 +00:00
return l;
}
static void gl_print_op(GLParam *p) {
Common::StreamDebug debug = streamDbg();
2022-01-02 09:22:49 +01:00
int op = p[0].op;
2006-05-16 14:52:36 +00:00
p++;
2022-01-02 09:22:49 +01:00
const char *s = op_table_str[op];
2006-05-16 14:52:36 +00:00
while (*s != 0) {
if (*s == '%') {
s++;
switch (*s++) {
case 'f':
debug << p[0].f;
2006-05-16 14:52:36 +00:00
break;
default:
debug << p[0].i;
2006-05-16 14:52:36 +00:00
break;
}
p++;
} else {
debug << *s;
2006-05-16 14:52:36 +00:00
s++;
}
}
debug << "\n";
}
void GLContext::gl_compile_op(GLParam *p) {
2022-01-02 09:22:49 +01:00
int op = p[0].op;
int op_size = op_table_size[op];
int index = current_op_buffer_index;
GLParamBuffer *ob = current_op_buffer;
2006-05-16 14:52:36 +00:00
// we should be able to add a NextBuffer opcode
if ((index + op_size) > (OP_BUFFER_MAX_SIZE - 2)) {
2022-01-02 09:22:49 +01:00
GLParamBuffer *ob1 = (GLParamBuffer *)gl_zalloc(sizeof(GLParamBuffer));
2022-01-01 12:00:38 +01:00
ob1->next = nullptr;
2006-05-16 14:52:36 +00:00
ob->next = ob1;
ob->ops[index].op = OP_NextBuffer;
ob->ops[index + 1].p = (void *)ob1;
current_op_buffer = ob1;
2006-05-16 14:52:36 +00:00
ob = ob1;
index = 0;
}
for (int i = 0; i < op_size; i++) {
2006-05-16 14:52:36 +00:00
ob->ops[index] = p[i];
index++;
}
current_op_buffer_index = index;
}
void GLContext::gl_add_op(GLParam *p) {
GLContext *c = gl_get_context();
2022-01-02 09:22:49 +01:00
int op = p[0].op;
if (exec_flag) {
2006-05-16 14:52:36 +00:00
op_table_func[op](c, p);
}
if (compile_flag) {
gl_compile_op(p);
2006-05-16 14:52:36 +00:00
}
if (print_flag) {
gl_print_op(p);
2006-05-16 14:52:36 +00:00
}
}
2006-05-16 14:52:36 +00:00
// this opcode is never called directly
2021-12-07 09:54:19 +01:00
void GLContext::glopEndList(GLParam *) {
2006-05-16 14:52:36 +00:00
assert(0);
}
2006-05-16 14:52:36 +00:00
// this opcode is never called directly
2021-12-07 09:54:19 +01:00
void GLContext::glopNextBuffer(GLParam *) {
2006-05-16 14:52:36 +00:00
assert(0);
}
2021-12-07 09:54:19 +01:00
void GLContext::glopCallList(GLParam *p) {
2022-01-02 09:22:49 +01:00
uint list = p[1].ui;
GLList *l = find_list(list);
2006-05-16 14:52:36 +00:00
2008-07-29 19:28:19 +00:00
if (!l)
error("list %d not defined", list);
2006-05-16 14:52:36 +00:00
p = l->first_op_buffer->ops;
while (1) {
2022-01-02 09:22:49 +01:00
int op = p[0].op;
2006-05-16 14:52:36 +00:00
if (op == OP_EndList)
break;
if (op == OP_NextBuffer) {
p = (GLParam *)p[1].p;
2006-05-16 14:52:36 +00:00
} else {
2021-12-07 09:54:19 +01:00
op_table_func[op](this, p);
2006-05-16 14:52:36 +00:00
p += op_table_size[op];
}
}
}
void GLContext::gl_NewList(TGLuint list, TGLenum mode) {
2006-05-16 14:52:36 +00:00
assert(mode == TGL_COMPILE || mode == TGL_COMPILE_AND_EXECUTE);
assert(compile_flag == 0);
GLList *l = find_list(list);
2008-07-29 19:28:19 +00:00
if (l)
delete_list(list);
l = alloc_list(list);
current_op_buffer = l->first_op_buffer;
current_op_buffer_index = 0;
compile_flag = 1;
exec_flag = (mode == TGL_COMPILE_AND_EXECUTE);
}
void GLContext::gl_EndList() {
GLParam p[1];
assert(compile_flag == 1);
2006-05-16 14:52:36 +00:00
// end of list
p[0].op = OP_EndList;
gl_compile_op(p);
compile_flag = 0;
exec_flag = 1;
}
TGLboolean GLContext::gl_IsList(TGLuint list) {
GLList *l = find_list(list);
2014-07-02 07:59:22 +02:00
2021-12-27 13:22:38 +01:00
return (l != nullptr);
}
TGLuint GLContext::gl_GenLists(TGLsizei range) {
GLList **lists = shared_state.lists;
int count = 0;
2006-05-16 14:52:36 +00:00
for (int i = 0; i < MAX_DISPLAY_LISTS; i++) {
2008-07-29 19:28:19 +00:00
if (!lists[i]) {
2006-05-16 14:52:36 +00:00
count++;
if (count == range) {
uint list = i - range + 1;
for (int j = 0; j < range; j++) {
alloc_list(list + j);
2006-05-16 14:52:36 +00:00
}
return list;
}
} else {
count = 0;
2006-05-16 14:52:36 +00:00
}
}
2006-05-16 14:52:36 +00:00
return 0;
}
} // end of namespace TinyGL