Skip to content

Commit 43c660e

Browse files
committed
Working on preloading
1 parent 5d66fec commit 43c660e

File tree

7 files changed

+77
-7
lines changed

7 files changed

+77
-7
lines changed

src/JulGame.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module JulGame
1717
# TODO: Create a globals file
1818

1919
SCENE_CACHE::Dict = Dict{String, Any}()
20+
PRELOADED_SCENES::Dict = Dict{String, Any}()
2021
IMAGE_CACHE::Dict = Dict{String, Any}()
2122
FONT_CACHE::Dict = Dict{String, Any}()
2223
AUDIO_CACHE::Dict = Dict{String, Any}()

src/editor/JulGameEditor/Components/FieldInputHandler.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ function handle_component_field_input(component, componentField, newScriptText="
3333
return false
3434
end
3535

36-
# Handle vector types
3736
if isa(fieldValue, Math._Vector2{Float64}) || isa(fieldValue, Math._Vector2{Int32})
3837
return show_vector2_input(component, componentField, fieldValue)
3938
elseif isa(fieldValue, Math._Vector3{Float64}) || isa(fieldValue, Math._Vector3{Int32})
4039
return show_vector3_input(component, componentField, fieldValue)
4140
elseif isa(fieldValue, Math._Vector4{Float64}) || isa(fieldValue, Math._Vector4{Int32})
4241
return show_vector4_input(component, componentField, fieldValue)
4342

43+
4444
# Handle scalar types
4545
elseif isa(fieldValue, Bool)
4646
return show_boolean_input(component, componentField, fieldValue)
4747
elseif isa(fieldValue, String)
4848
return show_string_input(component, componentField, fieldValue)
49-
elseif isa(fieldValue, Number)
49+
elseif isa(fieldValue, Int) || isa(fieldValue, Float64) || isa(fieldValue, Float32)
5050
return show_numeric_input(component, componentField, fieldValue)
5151

5252
# Handle scripts specially

src/editor/JulGameEditor/Utils/EditorUtils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ end
659659
function reset_camera_event(main)
660660
event = @event begin
661661
if main.scene.camera === nothing
662-
@warn "No camera found in scene when resetting camera"
662+
@debug "No camera found in scene when resetting camera"
663663
return
664664
end
665665
main.scene.camera.position = JulGame.Math.Vector3f(0.0, 0.0, 0.0)

src/engine/Component/SoundSource.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,11 @@ module SoundSourceModule
146146
SDL2.Mix_PlayChannel(this.channel, this.sound, loops)
147147
end
148148
end
149+
150+
function set_master_volume(volume::Int)
151+
# Convert volume to Int32 and clamp between 0 and 128
152+
volume = Math.TypeConversions.safe_int32_convert(clamp(volume, 0, 128))
153+
SDL2.Mix_MasterVolume(volume)
154+
SDL2.Mix_VolumeMusic(volume)
155+
end
149156
end

src/engine/SceneManagement/SceneBuilder.jl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module SceneBuilderModule
3737
end
3838
end
3939

40-
function load_and_prepare_scene(this::Scene, main = JulGame.MainLoop(); config=parse_config(), windowName::String="Game", isWindowResizable::Bool=false)
40+
function load_and_prepare_scene(this::Scene, main = JulGame.MainLoop(); config=parse_config(), windowName::String="Game", isWindowResizable::Bool=false, preloadAllScenes::Bool=false)
4141
if config === nothing
4242
@info("Config is nothing, parsing config")
4343
config = parse_config()
@@ -67,6 +67,7 @@ module SceneBuilderModule
6767
size = Math.Vector2(displayMode[1].w, displayMode[1].h)
6868
end
6969

70+
7071
scene = nothing
7172
if !JulGame.IS_EDITOR && !JulGame.IS_WEB
7273
# Initialize window manager
@@ -85,6 +86,24 @@ module SceneBuilderModule
8586
@error "Failed to create renderer with window $(MAIN.windowManager.window), $(unsafe_string(SDL2.SDL_GetError()))"
8687
return
8788
end
89+
90+
# Preload all scenes if requested
91+
if preloadAllScenes
92+
@info "Preloading all scenes..."
93+
scenesDir = joinpath(BasePath, "scenes")
94+
if isdir(scenesDir)
95+
for file in readdir(scenesDir)
96+
if endswith(file, ".json")
97+
scenePath = joinpath(scenesDir, file)
98+
@debug "Preloading scene: $file"
99+
SceneReaderModule.preload_scene(scenePath)
100+
end
101+
end
102+
@info "Finished preloading scenes"
103+
else
104+
@warn "Scenes directory not found: $scenesDir"
105+
end
106+
end
88107

89108
# Set default texture scaling mode to linear
90109
SDL2.SDL_SetHint(SDL2.SDL_HINT_RENDER_SCALE_QUALITY, "2")
@@ -96,7 +115,13 @@ module SceneBuilderModule
96115
JulGame.WindowManagerModule.set_vsync(isVsyncEnabled)
97116

98117
@debug "Deserializing scene"
99-
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))
118+
# Use preloaded scene if available
119+
if preloadAllScenes && haskey(JulGame.PRELOADED_SCENES, this.scene)
120+
@info "Using preloaded scene: $(this.scene)"
121+
scene = JulGame.PRELOADED_SCENES[this.scene]
122+
else
123+
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))
124+
end
100125
camera = scene[3]
101126
# Set logical rendering size based on camera
102127
@debug "Setting logical size to $(size.x)x$(size.y)"
@@ -112,7 +137,13 @@ module SceneBuilderModule
112137
end
113138

114139
if scene === nothing
115-
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))
140+
# Use preloaded scene if available
141+
if preloadAllScenes && haskey(JulGame.PRELOADED_SCENES, this.scene)
142+
@debug "Using preloaded scene: $(this.scene)"
143+
scene = JulGame.PRELOADED_SCENES[this.scene]
144+
else
145+
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))
146+
end
116147
end
117148

118149
MAIN.scene.entities = scene[1]

src/engine/SceneManagement/SceneReader.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,40 @@ module SceneReaderModule
2121
() -> (name; fields)
2222
end
2323

24+
export preload_scene
25+
"""
26+
preload_scene(filePath::String)
27+
28+
Preloads a scene from the specified file path and stores it in the PRELOADED_SCENES cache.
29+
This allows for faster scene switching as the scene is already loaded in memory.
30+
31+
# Arguments
32+
- `filePath::String`: The path to the scene file to preload
33+
"""
34+
function preload_scene(filePath::String)
35+
try
36+
if haskey(JulGame.PRELOADED_SCENES, basename(filePath))
37+
@debug("Scene already preloaded: $(basename(filePath))")
38+
return
39+
end
40+
41+
scene = deserialize_scene(filePath)
42+
JulGame.PRELOADED_SCENES[basename(filePath)] = (entities = scene[1], uiElements = scene[2], camera = scene[3])
43+
@debug("Preloaded scene: $(basename(filePath))")
44+
catch e
45+
@error string(e)
46+
Base.show_backtrace(stdout, catch_backtrace())
47+
end
48+
end
49+
2450
export deserialize_scene
2551
function deserialize_scene(filePath)
2652
try
53+
if haskey(JulGame.PRELOADED_SCENES, basename(filePath))
54+
@info "deserialize_scene: Using preloaded scene: $(basename(filePath))"
55+
return JulGame.PRELOADED_SCENES[basename(filePath)]
56+
end
57+
2758
json = nothing
2859
if haskey(JulGame.SCENE_CACHE, basename(filePath))
2960
json = JulGame.SCENE_CACHE[basename(filePath)]

src/engine/UI/TextBox.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ module TextBoxModule
330330

331331
function UI.center_text(this::TextBox)
332332
if MAIN.scene.camera === nothing
333-
@warn "No camera found in scene"
333+
@debug "No camera found in scene"
334334
return
335335
end
336336

0 commit comments

Comments
 (0)