def test_content_added(self): core.change_active_scene(self.scene0) len_context0 = len(graphics._active_context) len_sorting0 = len(graphics._active_sorting) core.add_content_to_scene(self.scene0, self.content0) self.assertEqual(len(graphics._active_context) - len_context0, 1) self.assertEqual(len(graphics._active_sorting) - len_sorting0, 1)
def test_content_removed(self): core.change_active_scene(self.scene0) core.add_content_to_scene(self.scene0, self.content0) len_context1 = len(graphics._active_context) len_sorting1 = len(graphics._active_sorting) core.remove_content_from_scene(self.scene0, self.content0) self.assertEqual(len(graphics._active_context) - len_context1, -1) self.assertEqual(len(graphics._active_sorting) - len_sorting1, -1)
def initialize(): # noinspection PyProtectedMember from api.sax_engine._examples.example_tile_grid import tile_map # tile_map = list(zip(*tile_map)) # t_wall = Tile("Wall", {"color": C_L_GRAY}) # t_air = Tile("Air", {}) scene0 = scene_system.SceneObject(name="scene0", tile_grid=tile_map) # import and create debug related stuff from prefabs.debug import FpsOverlay scene_system.add_content_to_scene(scene0, FpsOverlay(Vector2(5, 5), (255, 0, 0))) from prefabs.debug import CameraMarker scene_system.add_content_to_scene(scene0, CameraMarker()) from prefabs.background import TileGridRenderer tgr = TileGridRenderer() scene_system.add_content_to_scene(scene0, tgr) map_mid = v_mul(grid_size(tile_map), (0.5, 0.5)) # import and create player from game.actors.player.player import Player from game.actors.pre_alpha_tier.pre_alpha_tier import PreAlphaTier for _ in range(0, 20, 4): pos = Point(*v_add(map_mid, (_, 0))) tier = PreAlphaTier(position=pos) scene_system.add_content_to_scene(scene0, tier) from api.sax_engine.core.systems.graphics.camera import Camera render_targets = list(graphics_system.get_camera_setup(0)) player0 = Player(position=Point(*v_add(map_mid, (0, 0))), n=0) scene_system.add_content_to_scene(scene0, player0) camera0 = Camera(Point(*map_mid), "camera 0", render_targets[0], pixels_per_tile=32, follow_target=player0, grid_locks_view=True) scene_system.add_content_to_scene(scene0, camera0) # player1 = Player(position=Point(*v_add(map_mid, (0, 0))), n=1) # scene_system.add_content_to_scene(scene0, player1) # camera1 = Camera(Point(*map_mid), "camera 1", render_targets[1], # pixels_per_tile=32, # follow_target=player1, # grid_locks_view=True) # scene_system.add_content_to_scene(scene0, camera1) # from prefabs.utilities import TextContent # scene_system.add_content_to_scene(scene0, TextContent(Point(*map_mid), "Fun with physics!")) # scene_system.add_content_to_scene(scene0, TextContent(Point(55, 0), "Rock bottom!")) scene_system.change_active_scene(scene0)
def test_context_swapping(self): core.change_active_scene(self.scene0) graphics.cache_active_context() # test cache save self.assertTrue(self.scene0 in graphics._context_cache) cameras, instructions = graphics._context_cache[self.scene0] self.assertTrue(cameras is graphics._active_cameras) self.assertTrue(instructions is graphics._active_context) # test core dismiss core.change_active_scene(self.scene1) self.assertFalse(self.scene1 in graphics._context_cache) self.assertFalse(cameras is graphics._active_cameras) self.assertFalse(instructions is graphics._active_context) # test core restore core.change_active_scene(self.scene0) self.assertEqual(cameras, graphics._active_cameras) self.assertEqual(instructions, graphics._active_context) # test cache clear graphics.clear_cache() self.assertFalse(self.scene0 in graphics._context_cache)
from .example_content import ExampleContentEntity content0 = ExampleContentEntity(position=Point(*v_add(map_mid, (0, 0)))) scene_system.add_content_to_scene(scene0, content0) from api.sax_engine.core.systems.graphics.camera import Camera camera0 = Camera(Point(*map_mid), "camera 0", next(graphics_system.get_camera_setup()), pixels_per_tile=16, follow_target=content0, grid_locks_view=True) scene_system.add_content_to_scene(scene0, camera0) from prefabs.utilities import Text scene_system.add_content_to_scene(scene0, Text(Point(*map_mid), "Fun with physics!")) scene_system.add_content_to_scene(scene0, Text(Point(55, 0), "Rock bottom!")) scene_system.change_active_scene(scene0) last_update = time_ms() update_loops_max = 5 update_loops = 0 while True: # update timing parameters time_since_update = time_ms() - last_update delta_time = time_since_update / 1000 # event pump for pygame yield_api_events() if time_since_update > update_delay and update_loops < update_loops_max: # timing last_update += update_delay
from prefabs.debug import FpsOverlay core.add_content_to_scene(scene0, FpsOverlay(Vector2(5, 5), (255, 0, 0))) from prefabs.debug import CameraMarker core.add_content_to_scene(scene0, CameraMarker()) tgr = TileGridRenderer() core.add_content_to_scene(scene0, tgr) map_mid = Point(*v_mul(tgr.grid_size(), (0.5, 0.5))) content0 = ExampleContentEntity(position=Point(*v_add(map_mid, (0, 0)))) content1 = ExampleContentEntity(position=Point(*v_add(map_mid, (1, 1)))) content2 = ExampleContentEntity(position=Point(*v_add(map_mid, (1, 0)))) content3 = ExampleContentEntity(position=Point(*v_add(map_mid, (0, 1)))) core.change_active_scene(scene0) core.add_content_to_scene(scene0, content0) core.add_content_to_scene(scene0, content1) test_ppt = (32, 16) for n, r in enumerate(graphics.get_camera_setup(1)): c = Camera(map_mid, "camera {}".format(n), r, pixels_per_tile=test_ppt[n]) core.add_content_to_scene(scene0, c) seconds = 5 delta_time = 1.0 / 60 translation = Vector2(*v_mul(Vector2(-1, 0), delta_time)) for i in range(int(seconds / delta_time)): content0.position = Point(*v_add(content0.position, translation))
# add a camera to scene0 from api.sax_engine.core.systems.graphics.camera import Camera scene.add_content_to_scene(scene0, Camera(Point(0, 0), "Main camera", render_target)) # add a background clear to scene1 from prefabs.background import PlainBackground scene.add_content_to_scene(scene1, PlainBackground((0, 0, 0))) # add camera markers to both scenes from prefabs.debug import CameraMarker scene.add_content_to_scene(scene0, CameraMarker()) scene.add_content_to_scene(scene1, CameraMarker()) # activate scene0 scene.change_active_scene(scene0) graphics.update() sleep(1) yield_api_events() # add contents to scenes scene.add_content_to_scene(scene0, test_content0) scene.add_content_to_scene(scene1, test_content1) graphics.update() sleep(1) yield_api_events() # move content0 in scene0 translation = Vector2(*v_mul(Vector2(-1, 1), 0.1)) for i in range(100): test_content0.position = Point(*v_add(test_content0.position, translation))