From 2e905a80a8f93fc3f7bb6b93125eda280e1ee52c Mon Sep 17 00:00:00 2001 From: Roz K Date: Fri, 30 Dec 2022 12:29:33 +0100 Subject: [PATCH] Use kwargs in Batch construction to provide parameters names. --- game/batch.py | 15 ++++++++++----- game/game.py | 14 ++++++++------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/game/batch.py b/game/batch.py index 5226c86..10afa3d 100644 --- a/game/batch.py +++ b/game/batch.py @@ -19,23 +19,28 @@ from engine import (vec3, mat3, buffer, INSTANCE_FLAG_SPAWNED, BATCH_MAX_SIZE, param_type, params_format, create_batch, draw_batch, destroy_batch) class Batch: - __slots__ = '_batch', 'size', 'max_size', 'flags', 'meshes', 'params', '_params' + __slots__ = '_batch', 'size', 'max_size', 'flags', 'meshes', 'params', '_params', '__dict__' - def __init__(self, vertices, max_size, *params_formats): + def __init__(self, vertices, max_size, **params_decls): assert max_size <= BATCH_MAX_SIZE - nparams = len(params_formats) - self._batch = create_batch(vertices, max_size, params_format(*params_formats)) + nparams = len(params_decls) + names = params_decls.keys() + formats = params_decls.values() + self._batch = create_batch(vertices, max_size, params_format(*formats)) self.size = 0 self.max_size = max_size self.flags = buffer(c_ubyte, max_size) self.meshes = buffer(c_uint, max_size) - self.params = tuple(map(lambda f: buffer(param_type(f), max_size), params_formats)) + self.params = tuple(map(lambda f: buffer(param_type(f), max_size), formats)) self._params = (c_void_p * nparams)(*map(addressof, self.params)) + for name, value in zip(names, self.params): + setattr(self, name, value) def __del__(self): destroy_batch(self._batch) def append(self, flags, mesh, params): + assert len(params) == len(self.params) index = self.size assert index < self.max_size self.flags[index] = flags | INSTANCE_FLAG_SPAWNED diff --git a/game/game.py b/game/game.py index 07fa8c6..86dc2af 100644 --- a/game/game.py +++ b/game/game.py @@ -84,7 +84,7 @@ def main(): rock_model = archive.get_model('rock') mud_model = archive.get_model('mud') lava_model = archive.get_model('lava') - terrain_batch = Batch(tiles_vertices, generated.size ** 2, PARAM_FORMAT_VEC3_SHORT) + terrain_batch = Batch(tiles_vertices, generated.size ** 2, translation = PARAM_FORMAT_VEC3_SHORT) #TODO: generator & for real vc = generated.volcano_c @@ -123,7 +123,9 @@ def main(): blob_model = archive.get_model('blob') cube_model = archive.get_model('cube') clouds_model = archive.get_model('clouds') - tests_batch = Batch(tests_vertices, 3, PARAM_FORMAT_VEC3_FLOAT, PARAM_FORMAT_MAT3_INT10 | PARAM_FORMAT_NORMALIZE) + tests_batch = Batch(tests_vertices, 3, + translation = PARAM_FORMAT_VEC3_FLOAT, + orientation = PARAM_FORMAT_MAT3_INT10 | PARAM_FORMAT_NORMALIZE) blob_spawn_translation = vec3(-100.0, -500.0, 0.0) cube_spawn_translation = vec3(100.0, -500.0, 0.0) blob_forward = math.vec3_normalize((sun_direction[0], sun_direction[1], 0.0)) @@ -151,10 +153,10 @@ def main(): tests_environment_inputs = environment.resolve_inputs(tests_shader) sky_environment_inputs = environment.resolve_inputs(sky_shader) - blob_translation = tests_batch.params[0][blob_id] - cube_translation = tests_batch.params[0][cube_id] - cube_orientation = tests_batch.params[1][cube_id] - clouds_orientation = tests_batch.params[1][clouds_id] + blob_translation = tests_batch.translation[blob_id] + cube_translation = tests_batch.translation[cube_id] + cube_orientation = tests_batch.orientation[cube_id] + clouds_orientation = tests_batch.orientation[clouds_id] print("Running... Ctrl+c to quit") start_time = time.monotonic()