Skip to content

Commit

Permalink
Updating for web
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Feb 14, 2025
1 parent 0753f29 commit 07f39c1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/JulGame.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module JulGame
using SimpleDirectMediaLayer
const SDL2 = SimpleDirectMediaLayer
MAIN = nothing
IS_WEB = true
IS_EDITOR::Bool = false
IS_DEBUG::Bool = false
IS_PACKAGE_COMPILED::Bool = false
Expand Down
25 changes: 19 additions & 6 deletions src/MainLoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ module MainLoopModule

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)
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 @@ -481,7 +481,7 @@ function game_loop(this::MainLoop, startTime::Ref{UInt64} = Ref(UInt64(0)), last

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 Down Expand Up @@ -535,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 @@ -583,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 Down Expand Up @@ -624,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
4 changes: 4 additions & 0 deletions src/engine/Input/Input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ end
end
end

function update_input_state(this::Input, data::Dict{String, Any})
this.buttonsHeldDown = [key for (key, value) in data if value]
end

function get_button_held_down(this::Input, button::String)
if uppercase(button) in this.buttonsHeldDown
return true
Expand Down
15 changes: 11 additions & 4 deletions src/engine/SceneManagement/SceneBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ module SceneBuilderModule
mutable struct Scene
scene
srcPath::String
function Scene(sceneFileName::String, srcPath::String = joinpath(pwd(), ".."))
type::String

function Scene(sceneFileName::String, srcPath::String = joinpath(pwd(), ".."), type::String="SDLRenderer")
this = new()

this.scene = sceneFileName
this.srcPath = srcPath

this.type = type
path = Base.load_path()[1]
JulGame.IS_PACKAGE_COMPILED = occursin("share", path) && occursin("Project.toml", path)
if Sys.isapple() && JulGame.IS_PACKAGE_COMPILED
srcPath = joinpath(join(split(path, "/")[1:findfirst(x -> x == "Build", split(path, "/"))], "/"))
end

JulGame.BasePath = srcPath
if type == "Web"
JulGame.IS_WEB = true
end

return this
end
Expand Down Expand Up @@ -60,7 +65,8 @@ module SceneBuilderModule
(get(config, "Fullscreen", DEFAULT_CONFIG["Fullscreen"]) == "1" ? SDL2.SDL_WINDOW_FULLSCREEN_DESKTOP : 0)

MAIN.screenSize = size
if !JulGame.IS_EDITOR

if !JulGame.IS_EDITOR && this.type != "Web"
MAIN.window = SDL2.SDL_CreateWindow(MAIN.windowName, SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, MAIN.screenSize.x, MAIN.screenSize.y, flags)
JulGame.Renderer::Ptr{SDL2.SDL_Renderer} = SDL2.SDL_CreateRenderer(MAIN.window, -1, SDL2.SDL_RENDERER_ACCELERATED)
end
Expand All @@ -76,7 +82,7 @@ module SceneBuilderModule
if size.y < MAIN.scene.camera.size.y && size.y > 0
MAIN.scene.camera.size = Vector2(MAIN.scene.camera.size.x, size.y)
end
if !JulGame.IS_EDITOR
if !JulGame.IS_EDITOR && this.type != "Web"
SDL2.SDL_RenderSetLogicalSize(JulGame.Renderer, MAIN.scene.camera.size.x, MAIN.scene.camera.size.y)
end

Expand All @@ -95,6 +101,7 @@ module SceneBuilderModule
end

function deserialize_and_build_scene(this::Scene)

scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))

@debug String("Changing scene to $(this.scene)")
Expand Down

0 comments on commit 07f39c1

Please sign in to comment.