rk_island/game/batch.py

48 lines
1.8 KiB
Python

# Copyright (C) 2022 RozK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ctypes import c_ubyte, c_uint, POINTER
from engine import (
INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, buffer, create_batch, draw_batch, destroy_batch)
class Batch:
__slots__ = '_batch', '_paramsp', 'size', 'max_size', 'flags', 'meshes', 'params'
def __init__(self, vertices, max_size, params_format, params_type):
assert max_size <= BATCH_MAX_SIZE
self._batch = create_batch(vertices, max_size, params_format)
self._paramsp = POINTER(params_type)
self.size = 0
self.max_size = max_size
self.flags = buffer(c_ubyte, max_size)
self.meshes = buffer(c_uint, max_size)
self.params = buffer(params_type, max_size)
def __del__(self):
destroy_batch(self._batch)
def append(self, flags, mesh, params):
index = self.size
assert index < self.max_size
self.flags[index] = flags | INSTANCE_FLAG_SPAWNED
self.meshes[index] = mesh
self.params[index] = params
self.size += 1
return index
def draw(self):
draw_batch(self._batch, self.size, self.flags, self.meshes, self._paramsp(self.params))