# Demonstrates the use of the MixerAsync mixer interface wrapper import harfang as hg hg.LoadPlugins() # mount the system file driver hg.MountFileDriver(hg.StdFileDriver()) # create a new mixer and wrap it with the MixerAsync interface mixer = hg.MixerAsync(hg.CreateMixer()) mixer.Open() # start streaming future_channel = mixer.Stream("_data/skaven.it") # future_channel contains a FutureInt, this means that it will contain the channel the stream is playing on once the # mixer thread has executed the Stream call. the FutureInt get method blocks the caller until the value is available # and returns it. channel = future_channel.get() # wait until the user decides to exit the program or the stream ends print("Playing on channel %d, press Ctrl+C to stop." % channel) while mixer.GetPlayState(channel).get() == hg.MixerPlaying: hg.Sleep(hg.time_from_ms(100)) mixer.Close()
# =========================================================== import harfang as hg import teleporter # load all plugin for harfang, like VR plugin hg.LoadPlugins() # Get Plus, which is Harfang high level api plus = hg.GetPlus() # activate the multi-threading plus.CreateWorkers() # mount the system file driver hg.MountFileDriver(hg.StdFileDriver("assets"), "@assets") # create a windows or resolution 800*600 with antialiasing set to 4 plus.RenderInit(800, 600, 4, hg.Windowed) # add a new scene, which is a container of all objects you want to draw/update scn = plus.NewScene() # add light plus.AddLight(scn, hg.Matrix4.TranslationMatrix(hg.Vector3(4, 4, -4))) # add camera to the sene (to actually see something) cam = plus.AddCamera(scn, hg.Matrix4.Identity) scn.SetCurrentCamera(cam) # add some objects in the scene
exe_path = os.path.dirname(sys.executable) dir_ = sys._MEIPASS hg.LoadPlugins(dir_) else: # unfrozen exe_path = dir_ = os.path.dirname(os.path.realpath(__file__)) hg.LoadPlugins() hg.SetLogIsDetailed(True) hg.SetLogLevel(hg.LogAll) plus = hg.GetPlus() #plus.CreateWorkers() # mount the system file driver hg.MountFileDriver(hg.StdFileDriver()) hg.MountFileDriver(hg.StdFileDriver(dir_)) hg.MountFileDriver(hg.StdFileDriver(os.path.join(dir_, "assets")), "@assets") plus.RenderInit(1600, 900, 4, hg.Windowed) plus.SetBlend2D(hg.BlendAlpha) plus.SetBlend3D(hg.BlendAlpha) plus.SetRenderWindow() plus.GetRendererAsync().SetVSync(False) scn = plus.NewScene() scn.AddComponent(hg.Environment()) simple_graphic_scene_overlay = hg.SimpleGraphicSceneOverlay(False)
if False: # slow path through core geometry geo = hg.CoreGeometry() hg.IsoSurfaceToCoreGeometry(iso, geo) geo = plus.CreateGeometry(geo, False) else: geo = hg.RenderGeometry() hg.IsoSurfaceToRenderGeometry(plus.GetRenderSystem(), iso, geo, mat) t_new = hg.time_to_us_f(hg.time_now()) bench_create_geo.append(t_new - t_ref) return geo # hg.MountFileDriver(hg.StdFileDriver("../_data/"), "@data/") plus = hg.GetPlus() plus.RenderInit(1280, 720) mat = plus.LoadMaterial("@core/materials/default.mat") fps = hg.FPSController(w / 2, h / 2, -100) # scn = plus.NewScene() cam = plus.AddCamera(scn, hg.Matrix4.TranslationMatrix(hg.Vector3(0, 1, -10))) plus.AddLight(scn, hg.Matrix4.RotationMatrix(hg.Vector3(0.6, -0.4, 0)), hg.LightModelLinear, 300) plus.AddPlane(scn) renderable_system = scn.GetRenderableSystem()
# Display a terrain scene with light atmospheric features import harfang as hg hg.LoadPlugins() # mount data folder hg.MountFileDriver(hg.StdFileDriver("_data"), "@data") # create the renderer plus = hg.GetPlus() plus.RenderInit(1280, 720, 4) # create scene scn = plus.NewScene(False, True) plus.AddEnvironment(scn, hg.Color.Black, hg.Color.Black, hg.Color(0.85, 0.9, 1), 8000, 60000) # camera cam = plus.AddCamera(scn) cam.GetCamera().SetZNear(1) cam.GetCamera().SetZFar(100000) # 100km # sky lighting sky = hg.RenderScript("@core/lua/sky_lighting.lua") sky.Set("time_of_day", 16.5) sky.Set("attenuation", 0.75) sky.Set("shadow_range", 10000.0) # 10km shadow range sky.Set("shadow_split", hg.Vector4(0.1, 0.2, 0.3, 0.4)) scn.AddComponent(sky)
# path finding sample, left click to change start point, right click to change end point. import harfang as hg hg.LoadPlugins() hg.MountFileDriver(hg.StdFileDriver("_data/")) resolution = hg.Vector2(1280, 720) antialiasing = 4 plus = hg.GetPlus() plus.RenderInit(int(resolution.x), int(resolution.y), antialiasing) # setup scene scn = plus.NewScene(False, False) plus.AddEnvironment(scn,hg.Color.Black, hg.Color(0.25, 0.25, 0.3)) cam = plus.AddCamera(scn, hg.Matrix4.TransformationMatrix(hg.Vector3(0, 65, -55), hg.Vector3(0.9, 0, 0))) lgt = plus.AddLight(scn, hg.Matrix4.TransformationMatrix(hg.Vector3(40, 50, -60), hg.Vector3(0.75, -0.6, 0)), hg.LightModelSpot) lgt.GetLight().SetConeAngle(0.49) lgt.GetLight().SetEdgeAngle(0.1) gfx = hg.SimpleGraphicSceneOverlay(False) scn.AddComponent(gfx) # create the navigation system and component nav_sys = hg.NavigationSystem() nav_sys.SetDebugVisuals(False) scn.AddSystem(nav_sys)
# Sand-boxed access to the file system import os import harfang as hg # mount two std file driver under two different prefixes, only one acts as a sandbox hg.MountFileDriver(hg.StdFileDriver(os.path.dirname(__file__)), "std:") hg.MountFileDriver(hg.StdFileDriver(os.path.dirname(__file__), True), "box:") # access the readme.txt file outside of the std file driver root directory: std_path = "std:../_data/owl.jpg" res_std_access = hg.GetFilesystem().Exists(std_path) # do the same on the sandboxed box: mount point box_path = "box:../_data/owl.jpg" res_box_access = hg.GetFilesystem().Exists(box_path) print("Can access '%s' (expect OK): %s" % (std_path, str(res_std_access))) print("Can access '%s' (expect KO): %s" % (box_path, str(res_box_access)))
# Access to a file inside a Zip archive import harfang as hg archive_path = "_data/hello.zip" # mount a std file driver to access the archive hg.MountFileDriver(hg.StdFileDriver()) # mount a Zip file driver connected to the archive on the prefix "zip:" file = hg.GetFilesystem().Open(archive_path, hg.FileRead) hg.MountFileDriver(hg.ZipFileDriver(file), "zip:") # assert that we can find a file named "hello.txt" on the "zip:" prefix # ie. inside the archive connected to the ZipFileDriver "zip:" resolves to res = hg.GetFilesystem().Exists("zip:hello.txt") print("Can access 'hello.txt' (expect OK): %s" % str(res))
def game(): plus = hg.GetPlus() plus.RenderInit(screen_width, screen_height) al = hg.CreateMixer() al.Open() hg.MountFileDriver(hg.StdFileDriver()) keyboard = hg.GetInputSystem().GetDevice("keyboard") scn, ground = setup_game_level(plus) turret, cannon, turret_mass = create_turret(plus, scn) game_state = "GAME_INIT" while not plus.IsAppEnded(): dt = plus.UpdateClock() # Initialize Game if game_state == "GAME_INIT": enemy_list = [] debris_list = [] spawn_timer = 0.0 turret_cool_down = 0.0 enemy_spawn_interval = max_enemy_spawn_interval player_life = max_player_life target_angle = 0.0 score = 0 play_sound_fx(al, 'game_start') game_state = "TITLE" # Title screen if game_state == "TITLE": display_title_screen(plus, scn) if plus.KeyReleased(hg.KeySpace): game_state = "GAME" # Game elif game_state == "GAME": # Turret if plus.KeyDown(hg.KeyRight): target_angle += hg.time_to_sec_f(dt) * aim_rotation_speed else: if plus.KeyDown(hg.KeyLeft): target_angle -= hg.time_to_sec_f(dt) * aim_rotation_speed if plus.KeyPress(hg.KeySpace): if turret_cool_down < 0.0: throw_bullet(plus, scn, cannon.GetTransform().GetWorld().GetTranslation(), cannon.GetTransform().GetWorld().GetRow(1)) turret_cool_down = turret_cool_down_duration play_sound_fx(al, 'shoot') else: play_sound_fx(al, 'error') turret_cool_down += 10.0 * hg.time_to_sec_f(dt) turret_cool_down -= hg.time_to_sec_f(dt) target_angle = max(min(target_angle, aim_angle_range['max']), aim_angle_range['min']) rotate_turret(turret, target_angle, turret_mass) # Enemies spawn_timer += hg.time_to_sec_f(dt) if spawn_timer > enemy_spawn_interval: spawn_timer = 0 spawn_pos = hg.Vector3(uniform(-10, 10), 2.5, uniform(5.5, 6.5)) spawn_pos.Normalize() spawn_pos *= 10.0 spawn_pos.y = 5.0 new_enemy = spawn_enemy(plus, scn, spawn_pos) enemy_list.append([new_enemy[0], new_enemy[1]]) for enemy in enemy_list: # make enemy crawl toward the player enemy_dir = turret[0].GetTransform().GetPosition() - enemy[0].GetTransform().GetPosition() enemy_dir.Normalize() enemy[1].SetIsSleeping(False) enemy[1].ApplyLinearForce(enemy_dir * 0.25 * enemy_mass) col_pairs = scn.GetPhysicSystem().GetCollisionPairs(enemy[0]) for col_pair in col_pairs: if 'turret' in [col_pair.GetNodeA().GetName(), col_pair.GetNodeB().GetName()]: destroy_enemy(plus, scn, enemy[0]) debris_list.extend(create_explosion(plus, scn, enemy[0].GetTransform().GetPosition())) enemy_list.remove(enemy) play_sound_fx(al, 'explosion') play_sound_fx(al, 'hit') player_life -= 1 turret_cool_down = 0.0 if player_life < 1: play_sound_fx(al, 'game_over') game_state = "GAME_OVER" else: if 'bullet' in [col_pair.GetNodeA().GetName(), col_pair.GetNodeB().GetName()]: play_sound_fx(al, 'explosion') pos = col_pair.GetNodeB().GetTransform().GetPosition() debris_list.extend(create_explosion(plus, scn, pos, 8, 0.25)) pos = enemy[0].GetTransform().GetPosition() destroy_enemy(plus, scn, enemy[0]) enemy_list.remove(enemy) scn.RemoveNode(col_pair.GetNodeB()) debris_list.extend(create_explosion(plus, scn, pos)) score += 10 # Game difficulty enemy_spawn_interval = max(1.0, enemy_spawn_interval - hg.time_to_sec_f(dt) * 0.025) # Cleanup debris if len(debris_list) > max_debris: tmp_debris = debris_list[0] debris_list.remove(debris_list[0]) tmp_debris.RemoveComponent(tmp_debris.GetRigidBody()) # scn.RemoveNode(tmp_debris) render_aim_cursor(plus, scn, target_angle) display_hud(plus, player_life / max_player_life, max(0, turret_cool_down) / turret_cool_down_duration, score) # Game over screen elif game_state == "GAME_OVER": display_game_over(plus, scn, score) if plus.KeyReleased(hg.KeySpace): game_state = "SCENE_RESET" # Reset the playfield for a new game elif game_state == "SCENE_RESET": for enemy in enemy_list: destroy_enemy(plus, scn, enemy[0]) for debris in debris_list: debris.RemoveComponent(debris.GetRigidBody()) game_state = "GAME_INIT" plus.UpdateScene(scn, dt) plus.Flip() plus.EndFrame() return plus
def CreateGameObjects(): for obj in go.game_objects: print(go.game_objects[obj]["name"]) # Renderer initialization ############################################################################################## hg.LoadPlugins() plus = hg.GetPlus() plus.RenderInit(gg.width, gg.height, 4, hg.Windowed) plus.SetWindowTitle("Gilda Six, Private Investigator") # plus.GetRenderSystem().SetShadowMapsResolution(2048) hg.MountFileDriver(hg.StdFileDriver("./")) gg.scene = plus.NewScene(True, True) plus.LoadScene(gg.scene, "assets/main_scene.scn") gg.scene.UpdateAndCommitWaitAll() gg.osd = hg.SimpleGraphicSceneOverlay(False) gg.scene.AddComponent(gg.osd) CreateGameObjects() GameCreateCamera() picker = hg.ScenePicking(plus.GetRenderSystem())
# Access to the file system through mount points import os import harfang as hg # mount a standard file system as the 'std:' prefix hg.MountFileDriver(hg.StdFileDriver(), "std:") # mount a standard file system pointing to the current directory as the 'cwd:' prefix hg.MountFileDriver(hg.StdFileDriver(os.path.dirname(__file__)), "cwd:") # get the full path to this file this_file_path = os.path.realpath(__file__) # get this file name (ie. path relative to the current directory) this_file_name = os.path.basename(__file__) # the engine can access it relative to the current directory through the 'cwd:' mount point cwd_path = "cwd:" + this_file_name res_cwd_access = hg.GetFilesystem().Exists(cwd_path) # and it can access it using its absolute path through the std: mount point std_path = "std:" + this_file_path res_std_access = hg.GetFilesystem().Exists(std_path) # however, when no prefix is specified the engine has no way to access it from its absolute path res_direct_access = hg.GetFilesystem().Exists(this_file_path) print("Can access '%s' (expect KO): %s" % (this_file_path, str(res_direct_access))) print("Can access '%s' (expect OK): %s" % (cwd_path, str(res_cwd_access))) print("Can access '%s' (expect OK): %s" % (std_path, str(res_std_access)))