2022-08-28 05:09:52 +02:00
|
|
|
# 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 (
|
2022-12-17 05:03:08 +01:00
|
|
|
INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, create_batch, draw_batch, destroy_batch)
|
2022-08-28 05:09:52 +02:00
|
|
|
|
|
|
|
class Batch:
|
2022-12-17 16:56:52 +01:00
|
|
|
__slots__ = '_batch', 'max_size', 'format', 'flags', 'meshes', 'params'
|
2022-08-28 05:09:52 +02:00
|
|
|
|
2022-12-17 05:03:08 +01:00
|
|
|
def __init__(self, max_size, params_format):
|
2022-08-28 05:09:52 +02:00
|
|
|
assert max_size <= BATCH_MAX_SIZE
|
2022-12-17 05:03:08 +01:00
|
|
|
self._batch = create_batch(max_size, params_format)
|
2022-08-28 05:09:52 +02:00
|
|
|
self.max_size = max_size
|
2022-12-17 16:56:52 +01:00
|
|
|
self.format = params_format
|
2022-08-28 05:09:52 +02:00
|
|
|
self.flags = array('B')
|
|
|
|
self.meshes = array('I')
|
2022-12-17 16:56:52 +01:00
|
|
|
self.params = array('f')
|
2022-08-28 05:09:52 +02:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
destroy_batch(self._batch)
|
|
|
|
|
2022-12-17 05:03:08 +01:00
|
|
|
def append(self, flags, mesh, params):
|
2022-12-17 16:56:52 +01:00
|
|
|
assert len(params) == len(self.format)
|
2022-08-28 05:09:52 +02:00
|
|
|
index = len(self.flags)
|
|
|
|
assert index < self.max_size
|
|
|
|
self.flags.append(flags | INSTANCE_FLAG_SPAWNED)
|
|
|
|
self.meshes.append(mesh)
|
2022-12-17 16:56:52 +01:00
|
|
|
def append_param(param):
|
|
|
|
start = len(self.params)
|
|
|
|
self.params.extend(param)
|
|
|
|
return slice(start, len(self.params))
|
|
|
|
return tuple(map(append_param, params))
|
|
|
|
|
|
|
|
def set_param(self, param, id, value):
|
|
|
|
self.params[id[param]] = value
|
2022-08-28 05:09:52 +02:00
|
|
|
|
|
|
|
def draw(self):
|
2022-12-17 05:03:08 +01:00
|
|
|
draw_batch(self._batch, self.flags, self.meshes, self.params)
|