59 lines
2.3 KiB
Python
59 lines
2.3 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 array import array
|
|
|
|
from engine import (
|
|
INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, BATCH_ORIENTATION_FORMAT_NONE, create_batch, draw_batch, destroy_batch)
|
|
|
|
class Batch:
|
|
__slots__ = '_batch', 'max_size', 'flags', 'meshes', 'translations', 'orientations'
|
|
|
|
def __init__(self, max_size, translation_format, orientation_format):
|
|
assert max_size <= BATCH_MAX_SIZE
|
|
self._batch = create_batch(max_size, translation_format, orientation_format)
|
|
self.max_size = max_size
|
|
self.flags = array('B')
|
|
self.meshes = array('I')
|
|
self.translations = array('f')
|
|
if orientation_format != BATCH_ORIENTATION_FORMAT_NONE:
|
|
self.orientations = array('f')
|
|
else:
|
|
self.orientations = None
|
|
|
|
def __del__(self):
|
|
destroy_batch(self._batch)
|
|
|
|
def append(self, flags, mesh, translation, orientation):
|
|
assert len(translation) == 3
|
|
assert orientation is None or len(orientation) == 3
|
|
index = len(self.flags)
|
|
assert index < self.max_size
|
|
self.flags.append(flags | INSTANCE_FLAG_SPAWNED)
|
|
self.meshes.append(mesh)
|
|
self.translations.extend(translation)
|
|
if self.orientations is not None:
|
|
self.orientations.extend(orientation)
|
|
return index
|
|
|
|
def set_translation(self, index, translation):
|
|
self.translations[index * 3 : index * 3 + 3] = translation
|
|
|
|
def set_orientation(self, index, orientation):
|
|
self.orientations[index * 3 : index * 3 + 3] = orientation
|
|
|
|
def draw(self):
|
|
draw_batch(self._batch, self.flags, self.meshes, self.translations, self.orientations)
|