Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/using sdl gfx #62

Merged
merged 12 commits into from
Feb 9, 2024
44 changes: 24 additions & 20 deletions src/Input/Input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,12 @@

function check_scan_code(this::Input, keyboardState, keyState, scanCodes)
for scanCode in scanCodes
if keyboardState[Int32(scanCode) + 1] == keyState
return true
try
if keyboardState[Int32(scanCode) + 1] == keyState
return true
end
catch
println("Error checking scan code $(scanCode) at index $(Int32(scanCode) + 1)")

Check warning on line 230 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L230

Added line #L230 was not covered by tests
end
end
return false
Expand All @@ -237,43 +241,43 @@

# Uncomment to debug window events
if windowEvent == SDL2.SDL_WINDOWEVENT_SHOWN
#println(string("Window $(event.window.windowID) shown"))
@info(string("Window $(event.window.windowID) shown"))
elseif windowEvent == SDL2.SDL_WINDOWEVENT_HIDDEN
#println(string("Window $(event.window.windowID) hidden"))
@info(string("Window $(event.window.windowID) hidden"))
elseif windowEvent == SDL2.SDL_WINDOWEVENT_EXPOSED
#println(string("Window $(event.window.windowID) exposed"))
@info(string("Window $(event.window.windowID) exposed"))
elseif windowEvent == SDL2.SDL_WINDOWEVENT_MOVED
#println(string("Window $(event.window.windowID) moved to $(event.window.data1),$(event.window.data2)"))
@info(string("Window $(event.window.windowID) moved to $(event.window.data1),$(event.window.data2)"))
elseif windowEvent == SDL2.SDL_WINDOWEVENT_RESIZED # todo: update zoom and viewport size here
#println(string("Window $(event.window.windowID) resized to $(event.window.data1)x$(event.window.data2)"))
@info(string("Window $(event.window.windowID) resized to $(event.window.data1)x$(event.window.data2)"))

Check warning on line 252 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L252

Added line #L252 was not covered by tests
this.main.updateViewport(event.window.data1, event.window.data2)
elseif windowEvent == SDL2.SDL_WINDOWEVENT_SIZE_CHANGED
#println(string("Window $(event.window.windowID) size changed to $(event.window.data1)x$(event.window.data2)"))
@info(string("Window $(event.window.windowID) size changed to $(event.window.data1)x$(event.window.data2)"))

Check warning on line 255 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L255

Added line #L255 was not covered by tests
elseif windowEvent == SDL2.SDL_WINDOWEVENT_MINIMIZED
#println(string("Window $(event.window.windowID) minimized"))
@info(string("Window $(event.window.windowID) minimized"))

Check warning on line 257 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L257

Added line #L257 was not covered by tests
elseif windowEvent == SDL2.SDL_WINDOWEVENT_MAXIMIZED
#println(string("Window $(event.window.windowID) maximized"))
@info(string("Window $(event.window.windowID) maximized"))

Check warning on line 259 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L259

Added line #L259 was not covered by tests
elseif windowEvent == SDL2.SDL_WINDOWEVENT_RESTORED
#println(string("Window $(event.window.windowID) restored"))
@info(string("Window $(event.window.windowID) restored"))

Check warning on line 261 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L261

Added line #L261 was not covered by tests
elseif windowEvent == SDL2.SDL_WINDOWEVENT_ENTER
#println(string("Mouse entered window $(event.window.windowID)"))
@info(string("Mouse entered window $(event.window.windowID)"))
elseif windowEvent == SDL2.SDL_WINDOWEVENT_LEAVE
#println(string("Mouse left window $(event.window.windowID)"))
@info(string("Mouse left window $(event.window.windowID)"))

Check warning on line 265 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L265

Added line #L265 was not covered by tests
elseif windowEvent == SDL2.SDL_WINDOWEVENT_FOCUS_GAINED
#println(string("Window $(event.window.windowID) gained keyboard focus"))
@info(string("Window $(event.window.windowID) gained keyboard focus"))
this.isWindowFocused = true
elseif windowEvent == SDL2.SDL_WINDOWEVENT_FOCUS_LOST
#println(string("Window $(event.window.windowID) lost keyboard focus"))
@info(string("Window $(event.window.windowID) lost keyboard focus"))

Check warning on line 270 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L270

Added line #L270 was not covered by tests
this.isWindowFocused = false

elseif windowEvent == SDL2.SDL_WINDOWEVENT_CLOSE
#println(string("Window $(event.window.windowID) closed"))
@info(string("Window $(event.window.windowID) closed"))

Check warning on line 274 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L274

Added line #L274 was not covered by tests
elseif windowEvent == SDL2.SDL_WINDOWEVENT_TAKE_FOCUS
#println(string("Window $(event.window.windowID) is offered a focus"))
@info(string("Window $(event.window.windowID) is offered a focus"))

Check warning on line 276 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L276

Added line #L276 was not covered by tests
elseif windowEvent == SDL2.SDL_WINDOWEVENT_HIT_TEST
#println(string("Window $(event.window.windowID) has a special hit test"))
@info(string("Window $(event.window.windowID) has a special hit test"))

Check warning on line 278 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L278

Added line #L278 was not covered by tests
else
#println(string("Window $(event.window.windowID) got unknown event $(event.window.event)"))
@info(string("Window $(event.window.windowID) got unknown event $(event.window.event)"))

Check warning on line 280 in src/Input/Input.jl

View check run for this annotation

Codecov / codecov/patch

src/Input/Input.jl#L280

Added line #L280 was not covered by tests
end
end

Expand Down Expand Up @@ -369,4 +373,4 @@
return false
end

end
end
95 changes: 36 additions & 59 deletions src/Main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
close::Bool
currentTestTime::Float64
debugTextBoxes::Vector{UI.TextBoxModule.TextBox}
fpsManager::Ref{SDL2.LibSDL2.FPSmanager}
globals::Vector{Any}
input::Input
isDraggingEntity::Bool
Expand Down Expand Up @@ -124,7 +125,7 @@

while !this.close
try
GameLoop(this, startTime, lastPhysicsTime, false, C_NULL)
GameLoop(this, startTime, lastPhysicsTime, false)
catch e
if this.testMode
throw(e)
Expand Down Expand Up @@ -166,13 +167,13 @@
end
end

function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), isEditor::Bool = false, update::Union{Ptr{Nothing}, Vector{Any}} = C_NULL, windowPos::Math.Vector2 = Math.Vector2(0,0), windowSize::Math.Vector2 = Math.Vector2(0,0))
function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), isEditor::Bool = false, windowPos::Math.Vector2 = Math.Vector2(0,0), windowSize::Math.Vector2 = Math.Vector2(0,0))

Check warning on line 170 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L170

Added line #L170 was not covered by tests
if this.shouldChangeScene
this.shouldChangeScene = false
this.initializeNewScene(true)
return
end
return GameLoop(this, startTime, lastPhysicsTime, isEditor, update, windowPos, windowSize)
return GameLoop(this, startTime, lastPhysicsTime, isEditor, windowPos, windowSize)

Check warning on line 176 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L176

Added line #L176 was not covered by tests
end

function handle_editor_inputs_camera(this::Main, windowPos::Math.Vector2)
Expand Down Expand Up @@ -373,6 +374,9 @@
# windowInfo = unsafe_wrap(Array, SDL2.SDL_GetWindowSurface(this.window), 1; own = false)[1]

SDL2.SDL_RenderSetScale(JulGame.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(60))
end

function InitializeScriptsAndComponents(this::Main, isUsingEditor::Bool = false)
Expand Down Expand Up @@ -612,7 +616,7 @@
end

"""
GameLoop(this, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), close::Ref{Bool} = Ref(Bool(false)), isEditor::Bool = false, update::Union{Ptr{Nothing}, Vector{Any}} = C_NULL)
GameLoop(this, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), close::Ref{Bool} = Ref(Bool(false)), isEditor::Bool = false, Vector{Any}} = C_NULL)

Runs the game loop.

Expand All @@ -621,32 +625,16 @@
- `startTime`: A reference to the start time of the game loop.
- `lastPhysicsTime`: A reference to the last physics time of the game loop.
- `isEditor`: A boolean indicating whether the game loop is running in editor mode.
- `update`: An array containing information to pass back to the editor.

"""
function GameLoop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), isEditor::Bool = false, update::Union{Ptr{Nothing}, Vector{Any}} = C_NULL, windowPos::Math.Vector2 = Math.Vector2(0,0), windowSize::Math.Vector2 = Math.Vector2(0,0))
function GameLoop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), isEditor::Bool = false, windowPos::Math.Vector2 = Math.Vector2(0,0), windowSize::Math.Vector2 = Math.Vector2(0,0))
try
SDL2.SDL_RenderSetScale(JulGame.Renderer, this.zoom, this.zoom)

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

x,y,w,h = Int32[1], Int32[1], Int32[1], Int32[1]
if isEditor && update != C_NULL
SDL2.SDL_GetWindowPosition(this.window, pointer(x), pointer(y))
SDL2.SDL_GetWindowSize(this.window, pointer(w), pointer(h))

if update[2] != x[1] || update[3] != y[1]
if (update[2] < 2147483648 && update[3] < 2147483648)
SDL2.SDL_SetWindowPosition(this.window, round(update[2]), round(update[3]))
end
end
if update[4] != w[1] || update[5] != h[1]
SDL2.SDL_SetWindowSize(this.window, round(update[4]), round(update[5]))
SDL2.SDL_RenderSetScale(JulGame.Renderer, this.zoom, this.zoom)
end
end
if isEditor && update == C_NULL
if isEditor
this.scene.camera.dimensions = Math.Vector2(windowSize.x, windowSize.y)
# update_viewport_editor(this, windowSize.x, windowSize.y)
end
Expand All @@ -670,7 +658,7 @@

#endregion ============= Input

if !isEditor || update != C_NULL
if !isEditor
SDL2.SDL_RenderClear(JulGame.Renderer)
end

Expand Down Expand Up @@ -731,31 +719,30 @@
# Used for conditional rendering
cameraPosition = this.scene.camera.position
cameraSize = this.scene.camera.dimensions
if !isEditor || update == C_NULL
skipcount = 0
rendercount = 0
for layer in this.spriteLayers["sort"]
for sprite in this.spriteLayers["$(layer)"]
spritePosition = sprite.parent.transform.getPosition()
spriteSize = sprite.parent.transform.getScale()

if ((spritePosition.x + spriteSize.x) < cameraPosition.x || spritePosition.y < cameraPosition.y || spritePosition.x > cameraPosition.x + cameraSize.x/SCALE_UNITS || (spritePosition.y - spriteSize.y) > cameraPosition.y + cameraSize.y/SCALE_UNITS) && sprite.isWorldEntity && this.optimizeSpriteRendering
skipcount += 1
continue
end
rendercount += 1
try
Component.draw(sprite)
catch e
println(sprite.parent.name, " with id: ", sprite.parent.id, " has a problem with it's sprite")
println(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)
end

skipcount = 0
rendercount = 0
for layer in this.spriteLayers["sort"]
for sprite in this.spriteLayers["$(layer)"]
spritePosition = sprite.parent.transform.getPosition()
spriteSize = sprite.parent.transform.getScale()

if ((spritePosition.x + spriteSize.x) < cameraPosition.x || spritePosition.y < cameraPosition.y || spritePosition.x > cameraPosition.x + cameraSize.x/SCALE_UNITS || (spritePosition.y - spriteSize.y) > cameraPosition.y + cameraSize.y/SCALE_UNITS) && sprite.isWorldEntity && this.optimizeSpriteRendering
skipcount += 1
continue

Check warning on line 732 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L731-L732

Added lines #L731 - L732 were not covered by tests
end
rendercount += 1
try
Component.draw(sprite)
catch e
println(sprite.parent.name, " with id: ", sprite.parent.id, " has a problem with it's sprite")
println(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)

Check warning on line 741 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L738-L741

Added lines #L738 - L741 were not covered by tests
end
end
#println("Skipped $skipcount, rendered $rendercount")
end
#println("Skipped $skipcount, rendered $rendercount")

colliderSkipCount = 0
colliderRenderCount = 0
Expand All @@ -768,7 +755,6 @@
if entityShape != C_NULL
entityShape.draw()
end


if DEBUG && entity.collider != C_NULL
SDL2.SDL_SetRenderDrawColor(JulGame.Renderer, 0, 255, 0, SDL2.SDL_ALPHA_OPAQUE)
Expand Down Expand Up @@ -881,22 +867,13 @@

#endregion ============= Debug

if !isEditor || update != C_NULL
SDL2.SDL_RenderPresent(JulGame.Renderer)
end
endTime = SDL2.SDL_GetPerformanceCounter()
elapsedMS = (endTime - startTime[]) / SDL2.SDL_GetPerformanceFrequency() * 1000.0
targetFrameTime::Float64 = 1000/this.targetFrameRate

if elapsedMS < targetFrameTime && !isEditor
SDL2.SDL_Delay(round(targetFrameTime - elapsedMS))

if !isEditor
SDL2.SDL_RenderPresent(JulGame.Renderer)
SDL2.SDL_framerateDelay(this.fpsManager)
end

if isEditor && update != C_NULL
returnData = [[this.scene.entities, this.scene.textBoxes, this.scene.screenButtons], this.mousePositionWorld, cameraPosition, !this.selectedEntityUpdated ? update[7] : this.selectedEntityIndex, this.input.isWindowFocused]
this.selectedEntityUpdated = false
return returnData
end
catch e
println(e)
Base.show_backtrace(stdout, catch_backtrace())
Expand Down
2 changes: 1 addition & 1 deletion src/editor/JulGameEditor/Editor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
SDL2.SDL_SetRenderTarget(renderer, sceneTexture)
SDL2.SDL_RenderClear(renderer)
#SDL2.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
gameInfo = currentSceneMain === nothing ? [] : currentSceneMain.gameLoop(Ref(UInt64(0)), Ref(UInt64(0)), true, C_NULL, Math.Vector2(sceneWindowPos.x + 8, sceneWindowPos.y + 25), Math.Vector2(scenewindowSize.x, scenewindowSize.y)) # Magic numbers for the border of the imgui window. TODO: Make this dynamic if possible
gameInfo = currentSceneMain === nothing ? [] : currentSceneMain.gameLoop(Ref(UInt64(0)), Ref(UInt64(0)), true, Math.Vector2(sceneWindowPos.x + 8, sceneWindowPos.y + 25), Math.Vector2(scenewindowSize.x, scenewindowSize.y)) # Magic numbers for the border of the imgui window. TODO: Make this dynamic if possible

Check warning on line 221 in src/editor/JulGameEditor/Editor.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/Editor.jl#L221

Added line #L221 was not covered by tests
SDL2.SDL_SetRenderTarget(renderer, C_NULL)
SDL2.SDL_RenderClear(renderer)

Expand Down
Loading