Skip to content

Commit

Permalink
Merge branch 'bugfix/ci-fixes' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Feb 15, 2025
2 parents ade2c16 + e05f749 commit 3a0b820
Show file tree
Hide file tree
Showing 20 changed files with 177 additions and 154 deletions.
8 changes: 7 additions & 1 deletion src/JulGame.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ module JulGame
using SimpleDirectMediaLayer
const SDL2 = SimpleDirectMediaLayer
MAIN = nothing
IS_EDITOR = false

IS_WEB::Bool = false
IS_EDITOR::Bool = false
IS_EDITOR_PLAY_MODE::Bool = false
IS_DEBUG::Bool = false
IS_PACKAGE_COMPILED::Bool = false

DELTA_TIME = 0.0
# TODO: Create a globals file

SCENE_CACHE::Dict = Dict{String, Any}()
IMAGE_CACHE::Dict = Dict{String, Any}()
FONT_CACHE::Dict = Dict{String, Any}()
Expand Down
110 changes: 41 additions & 69 deletions src/MainLoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module MainLoopModule
export MainLoop
mutable struct MainLoop
assets::String
autoScaleZoom::Bool
close::Bool
coroutine_condition
currentTestTime::Float64
Expand All @@ -23,6 +22,8 @@ module MainLoopModule
isWindowFocused::Bool
level::JulGame.SceneManagement.SceneBuilderModule.Scene
optimizeSpriteRendering::Bool
scaleFactorX
scaleFactorY
scene::SceneModule.Scene
selectedEntity::Union{Entity, Nothing}
selectedUIElementIndex::Int64
Expand All @@ -34,14 +35,12 @@ module MainLoopModule
testMode::Bool
window::Ptr{SDL2.SDL_Window}
windowName::String
zoom::Float64

function MainLoop()
this::MainLoop = new()

SDL2.init()

this.zoom = 1.0
this.scene = SceneModule.Scene()
this.input = Input()

Expand All @@ -66,16 +65,16 @@ module MainLoopModule
end
end

function prepare_window_scripts_and_start_loop(size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true)
function prepare_window_scripts_and_start_loop(size)
@debug "Preparing window"
if !JulGame.IS_EDITOR
if !JulGame.IS_EDITOR && !JulGame.IS_WEB
@debug "Preparing window for game"
prepare_window(size, isResizable, autoScaleZoom)
prepare_window(size)
end
@debug "Initializing scripts and components"
initialize_scripts_and_components()

if !JulGame.IS_EDITOR
if !JulGame.IS_EDITOR && !JulGame.IS_WEB
@debug "Starting non editor loop"
full_loop(MAIN)
return
Expand Down Expand Up @@ -177,66 +176,14 @@ module MainLoopModule
SceneBuilderModule.create_new_screen_button(this.level)
end

function update_viewport(this::MainLoop, x,y)
@debug "Updating viewport"
if !this.autoScaleZoom
return
end
scale_zoom(this, x, y)
SDL2.SDL_RenderClear(JulGame.Renderer::Ptr{SDL2.SDL_Renderer})
SDL2.SDL_RenderSetScale(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, 1.0, 1.0)

if this.scene.camera !== nothing
this.scene.camera.startingCoordinates = Math.Vector2f(round(x/2) - round(this.scene.camera.size.x/2*this.zoom), round(y/2) - round(this.scene.camera.size.y/2*this.zoom))
@debug string("Set viewport to: ", this.scene.camera.startingCoordinates)
SDL2.SDL_RenderSetViewport(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, Ref(SDL2.SDL_Rect(this.scene.camera.startingCoordinates.x, this.scene.camera.startingCoordinates.y, round(this.scene.camera.size.x*this.zoom), round(this.scene.camera.size.y*this.zoom))))
end

SDL2.SDL_RenderSetScale(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, this.zoom, this.zoom)
end

function scale_zoom(this::MainLoop, x,y)
@debug "Scaling zoom"
if this.scene.camera === nothing
return
end

if this.autoScaleZoom
targetRatio = this.scene.camera.size.x/this.scene.camera.size.y
if this.scene.camera.size.x == max(this.scene.camera.size.x, this.scene.camera.size.y)
for i in x:-1:this.scene.camera.size.x
value = i/targetRatio
isInt = isinteger(value) || (isa(value, AbstractFloat) && trunc(value) == value)
if isInt && value <= y
this.zoom = i/this.scene.camera.size.x
break
end
end
else
for i in y:-1:this.scene.camera.size.y
value = i*targetRatio
isInt = isinteger(value) || (isa(value, AbstractFloat) && trunc(value) == value)
if isInt && value <= x
this.zoom = i/this.scene.camera.size.y
break
end
end
end
end
end

function prepare_window(size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true)
function prepare_window(size)
this::MainLoop = MAIN
this.autoScaleZoom = autoScaleZoom
scale_zoom(this, size.x, size.y)

if this.scene.camera !== nothing
this.scene.camera.startingCoordinates = Math.Vector2f(round(size.x/2) - round(this.scene.camera.size.x/2*this.zoom), round(size.y/2) - round(this.scene.camera.size.y/2*this.zoom))
@debug string("Set viewport to: ", this.scene.camera.startingCoordinates)
SDL2.SDL_RenderSetViewport(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, Ref(SDL2.SDL_Rect(this.scene.camera.startingCoordinates.x, this.scene.camera.startingCoordinates.y, round(this.scene.camera.size.x*this.zoom), round(this.scene.camera.size.y*this.zoom))))
#@debug string("Set viewport to: ", this.scene.camera.startingCoordinates)
#SDL2.SDL_RenderSetViewport(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, Ref(SDL2.SDL_Rect(this.scene.camera.startingCoordinates.x, this.scene.camera.startingCoordinates.y, round(this.scene.camera.size.x), round(this.scene.camera.size.y))))
end

SDL2.SDL_RenderSetScale(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, this.zoom, this.zoom)
this.fpsManager = Ref(SDL2.LibSDL2.FPSmanager(UInt32(0), Cfloat(0.0), UInt32(0), UInt32(0), UInt32(0)))
SDL2.SDL_initFramerate(this.fpsManager)
SDL2.SDL_setFramerate(this.fpsManager, UInt32(this.targetFrameRate))
Expand Down Expand Up @@ -529,14 +476,12 @@ function game_loop(this::MainLoop, startTime::Ref{UInt64} = Ref(UInt64(0)), last
return
end
try
SDL2.SDL_RenderSetScale(JulGame.Renderer::Ptr{SDL2.SDL_Renderer}, this.zoom, this.zoom)

lastStartTime = startTime[]
startTime[] = SDL2.SDL_GetPerformanceCounter()

DEBUG = false
#region Input
if !JulGame.IS_EDITOR
if !JulGame.IS_EDITOR && !JulGame.IS_WEB
JulGame.InputModule.poll_input(this.input)

this.close = this.input.quit
Expand All @@ -547,6 +492,20 @@ function game_loop(this::MainLoop, startTime::Ref{UInt64} = Ref(UInt64(0)), last
cameraPosition = this.scene.camera !== nothing ? (this.scene.camera.position + this.scene.camera.offset) : Math.Vector2f(0,0)
cameraSize = this.scene.camera !== nothing ? this.scene.camera.size : Math.Vector2(0,0)

x = 0
y = 0
if JulGame.InputModule.get_button_held_down(this.input, "Right")
x = 1
elseif JulGame.InputModule.get_button_held_down(this.input, "Left")
x = -1
end

if JulGame.InputModule.get_button_held_down(this.input, "Up")
y = 1
elseif JulGame.InputModule.get_button_held_down(this.input, "Down")
y = -1
end

#region Physics
if !JulGame.IS_EDITOR || this.isGameModeRunningInEditor
currentPhysicsTime = SDL2.SDL_GetTicks()
Expand Down Expand Up @@ -576,7 +535,7 @@ function game_loop(this::MainLoop, startTime::Ref{UInt64} = Ref(UInt64(0)), last

#region Rendering
currentRenderTime = SDL2.SDL_GetTicks()
if this.scene.camera !== nothing && !JulGame.IS_EDITOR
if this.scene.camera !== nothing && !JulGame.IS_EDITOR && !JulGame.IS_WEB
JulGame.CameraModule.update(this.scene.camera)
end

Expand Down Expand Up @@ -624,7 +583,7 @@ function game_loop(this::MainLoop, startTime::Ref{UInt64} = Ref(UInt64(0)), last
deleteat!(JulGame.Coroutines, findfirst(x -> x == coroutine_to_remove, JulGame.Coroutines))
end

if !JulGame.IS_EDITOR
if !JulGame.IS_EDITOR && !JulGame.IS_WEB
render_scene_sprites_and_shapes(this, this.scene.camera)
end

Expand All @@ -636,7 +595,7 @@ function game_loop(this::MainLoop, startTime::Ref{UInt64} = Ref(UInt64(0)), last
end

pos1::Math.Vector2 = windowPos !== nothing ? windowPos : Math.Vector2(0, 0)
this.input.mousePositionWorld = Math.Vector2(floor(Int32,(this.input.mousePosition.x - this.input.mousePositionEditorGameWindowOffset.x + (cameraPosition.x * SCALE_UNITS * this.zoom)) / SCALE_UNITS / this.zoom), floor(Int32,( this.input.mousePosition.y - this.input.mousePositionEditorGameWindowOffset.y + (cameraPosition.y * SCALE_UNITS * this.zoom)) / SCALE_UNITS / this.zoom))
this.input.mousePositionWorld = Math.Vector2(floor(Int32,(this.input.mousePosition.x - this.input.mousePositionEditorGameWindowOffset.x + (cameraPosition.x * SCALE_UNITS)) / SCALE_UNITS), floor(Int32,( this.input.mousePosition.y - this.input.mousePositionEditorGameWindowOffset.y + (cameraPosition.y * SCALE_UNITS)) / SCALE_UNITS))
rawMousePos = Math.Vector2f(this.input.mousePosition.x - pos1.x , this.input.mousePosition.y - pos1.y )
#region Debug
if DEBUG
Expand Down Expand Up @@ -665,9 +624,22 @@ function game_loop(this::MainLoop, startTime::Ref{UInt64} = Ref(UInt64(0)), last
end
end

if !JulGame.IS_EDITOR
if !JulGame.IS_EDITOR && !JulGame.IS_WEB
SDL2.SDL_RenderPresent(JulGame.Renderer::Ptr{SDL2.SDL_Renderer})
SDL2.SDL_framerateDelay(this.fpsManager)
elseif JulGame.IS_WEB
entt = "["
for i = 1:length(this.scene.entities)
entt *= "{ \"x\": $(this.scene.entities[i].transform.position.x), \"y\": $(this.scene.entities[i].transform.position.y) }"

if i < length(this.scene.entities)
entt *= ","
end
end

entt *= "]"

return entt
end
catch e
if JulGame.IS_EDITOR || this.testMode
Expand Down
5 changes: 5 additions & 0 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,16 @@ function show_script_editor(entity, newScriptText)

script = display_files(joinpath(JulGame.BasePath, "scripts"), "scripts", "Add Script")
if script != ""
@debug("Adding script: $(script) to: $(entity.name)")
Base.include(JulGame.ScriptModule, joinpath(JulGame.BasePath, "scripts", "$(script).jl"))
module_name = getfield(JulGame.ScriptModule, Symbol("$(script)Module"))
constructor = Base.invokelatest(getfield, module_name, Symbol(script))
newScript = Base.invokelatest(constructor)
newScript.parent = entity
# TODO: Get this working
# if JulGame.IS_EDITOR_PLAY_MODE
# JulGame.initialize(newScript)
# end
push!(entity.scripts, newScript)
end

Expand Down
2 changes: 2 additions & 0 deletions src/editor/JulGameEditor/Components/FileFinderMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ function display_files(base_path::String, file_type::String, title::String = "",
extension = ".$(split(file, ".")[end])"
if extension in extensions
if CImGui.MenuItem(file)
println("selected: $(file)")
value = "$(joinpath(base_path, file))"
if file_type == "scripts"
value = split(file, ".")[1]
end
break
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/editor/JulGameEditor/Components/TextBoxFields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function show_textbox_fields(selectedTextBox, textBoxField)
if x != Value.r || y != Value.g || z != Value.b || w != Value.a
selectedTextBox.text = selectedTextBox.text
end
elseif fieldName == "position"
elseif fieldName == "position" || fieldName == "anchorOffset"
x = Cint(Value.x)
y = Cint(Value.y)
@c CImGui.InputInt("$(textBoxField) x", &x, 1)
Expand Down
35 changes: 22 additions & 13 deletions src/editor/JulGameEditor/Editor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ module Editor

scrolling = Ref(ImVec2(0.0, 0.0))
zoom_level = Ref(1.0)
playMode = false

animation_window_dict = Ref(Dict())
animator_preview_dict = Ref(Dict())
Expand Down Expand Up @@ -174,7 +173,7 @@ module Editor
end

try
if !playMode && currentSelectedProjectPath[] != "" && unsafe_string(SDL2.SDL_GetWindowTitle(window)) != "$(windowTitle) - $(currentSelectedProjectPath[])"
if !JulGame.IS_EDITOR_PLAY_MODE && currentSelectedProjectPath[] != "" && unsafe_string(SDL2.SDL_GetWindowTitle(window)) != "$(windowTitle) - $(currentSelectedProjectPath[])"
newWindowTitle = "$(windowTitle) - $(currentSelectedProjectPath[])"
SDL2.SDL_SetWindowTitle(window, newWindowTitle)
end
Expand Down Expand Up @@ -247,10 +246,10 @@ module Editor
try
prevSceneWindowSize = sceneWindowSize

wasPlaying = playMode
wasPlaying = JulGame.IS_EDITOR_PLAY_MODE
if show_modal(confirmation_modal)
playMode = !playMode
if playMode
JulGame.IS_EDITOR_PLAY_MODE = !JulGame.IS_EDITOR_PLAY_MODE
if JulGame.IS_EDITOR_PLAY_MODE
startTime[] = SDL2.SDL_GetTicks()

# Animate the text in the window title
Expand All @@ -259,11 +258,11 @@ module Editor
end

sceneWindowSize = show_scene_window(currentSceneMain, sceneTexture, scrolling, zoom_level, duplicationMode, camera)
if playMode != wasPlaying && currentSceneMain !== nothing
if playMode
if JulGame.IS_EDITOR_PLAY_MODE != wasPlaying && currentSceneMain !== nothing
if JulGame.IS_EDITOR_PLAY_MODE
JulGame.MainLoopModule.start_game_in_editor(currentSceneMain, currentSelectedProjectPath[])
currentSceneMain.scene.camera = gameCamera
elseif !playMode
elseif !JulGame.IS_EDITOR_PLAY_MODE
JulGame.MainLoopModule.stop_game_in_editor(currentSceneMain)
JulGame.change_scene(String(currentSceneName))
end
Expand Down Expand Up @@ -606,18 +605,30 @@ module Editor
if length(filesToReload[]) > 0
for file in filesToReload[]
classname = split(file, ".")[begin]
Base.include(JulGame.ScriptModule, joinpath(JulGame.BasePath, "scripts", file))
try
Base.include(JulGame.ScriptModule, joinpath(JulGame.BasePath, "scripts", file))
catch e
@error "Error reloading file: $(file)"
continue
end
for entity in currentSceneMain.scene.entities
i = 1
for script in entity.scripts
script_name = split("$(typeof(script))", ".")[end]
if script_name == classname
try
println("reloading script: $(script_name)")
@debug("reloading script: $(script_name)")
module_name = getfield(JulGame.ScriptModule, Symbol("$(classname)Module"))
constructor = Base.invokelatest(getfield, module_name, Symbol(script_name))

new_script = Base.invokelatest(constructor)
entity.scripts[i] = new_script
entity.scripts[i].parent = entity

# TODO: Get this working
# if JulGame.IS_EDITOR_PLAY_MODE
# JulGame.initialize(new_script)
# end

# Copy all fields from old_script to the new script
for fieldname in fieldnames(typeof(entity.scripts[i]))
Expand All @@ -632,8 +643,6 @@ module Editor
end
end

entity.scripts[i] = new_script
entity.scripts[i].parent = entity
println("script reloaded successfully")
catch e
@error "Error reloading script: $(script_name): $(first(string(e), 1000))"
Expand All @@ -655,8 +664,8 @@ module Editor
backup_file_name = backup_file_name = "$(replace(currentSceneName, ".json" => ""))-backup-$(replace(Dates.format(Dates.now(), "yyyy-mm-ddTHH:MM:SS"), ":" => "-")).json"
@debug string("Backup file name: ", backup_file_name)
SceneWriterModule.serialize_entities(currentSceneMain.scene.entities, currentSceneMain.scene.uiElements, gameCamera, currentSelectedProjectPath[], backup_file_name)
@error "Error in renderloop!" exception=e
Base.show_backtrace(stderr, catch_backtrace())
@warn "Error in renderloop!" exception=e
finally
#TODO: fix these: ImGui_ImplSDLRenderer2_Shutdown();
# ImGui_ImplSDL2_Shutdown();
Expand Down
3 changes: 3 additions & 0 deletions src/editor/JulGameEditor/Utils/EditorUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ function regenerate_ids_event(main)
for index in eachindex(main.scene.entities)
main.scene.entities[index].id = JulGame.generate_uuid()
end
for index in eachindex(main.scene.uiElements)
main.scene.uiElements[index].id = JulGame.generate_uuid()
end
end

return event
Expand Down
13 changes: 4 additions & 9 deletions src/editor/JulGameEditor/Utils/FileContents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,8 @@ end

function config_file_content(projectName)
return
"WindowName=$projectName
Width=800
Height=800
PixelsPerUnit=16
IsResizable=1
Zoom=1.0
AutoScaleZoom=0
Fullscreen=0
FrameRate=60"
"Width=800
Height=800
Fullscreen=0
FrameRate=60"
end
Loading

0 comments on commit 3a0b820

Please sign in to comment.