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/sdl editor #58

Merged
merged 26 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c474daa
Changes
Kyjor Feb 1, 2024
c4d9596
Fixed a lot of scaling issues
Kyjor Feb 1, 2024
c73def3
Fixing clicking in new editor
Kyjor Feb 2, 2024
b6da56e
Fix sprite rendering editor and starting to create components
Kyjor Feb 2, 2024
c563454
Fixing text input
Kyjor Feb 3, 2024
b79fe2a
Fix issue with text input receiving more than one event at a given time.
Kyjor Feb 3, 2024
236b5ba
Fix for input.
Kyjor Feb 3, 2024
8e10369
Scene loading
Kyjor Feb 4, 2024
90a07e8
Refactoring
Kyjor Feb 4, 2024
d582a3e
More refactoring and docs
Kyjor Feb 4, 2024
3265496
More refactor/docs and fixed scene not loading properly
Kyjor Feb 4, 2024
b0b98e0
Project is opened from file menu
Kyjor Feb 4, 2024
7995690
Fixed issue with text input, made it more modular, working on filter
Kyjor Feb 6, 2024
6d50ee2
Mult-select
Kyjor Feb 6, 2024
da52c53
Some cleanup and starting to work on drag and drop
Kyjor Feb 6, 2024
55f4935
Working on drag and drop and some clean up
Kyjor Feb 7, 2024
f7bdbf7
Merge remote-tracking branch 'Kyjor/main' into feature/sdl-editor
Kyjor Feb 7, 2024
ecf4c24
We can move items around with drag and drop now.
Kyjor Feb 7, 2024
2ebf59a
Component input updates
Kyjor Feb 7, 2024
502694a
Merge remote-tracking branch 'Kyjor/main' into feature/sdl-editor
Kyjor Feb 7, 2024
617b44c
Merge issues and more properties working.
Kyjor Feb 7, 2024
fd6338a
Fixing more properties and cleanup
Kyjor Feb 8, 2024
956e255
Making sure errors bubble up and using eachindex
Kyjor Feb 8, 2024
bb41622
Test fixes
Kyjor Feb 8, 2024
83e41b6
Remove smoke test for now
Kyjor Feb 9, 2024
0be75dd
Added back smoke test and removed platformer
Kyjor Feb 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Component/Sprite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ module SpriteModule
deprecated_get_property(method_props, this, s)
end
function draw(this::InternalSprite, zoom::Float64 = 1.0)
zoom = 1.0
Kyjor marked this conversation as resolved.
Show resolved Hide resolved
if this.image == C_NULL || JulGame.Renderer == C_NULL
return
end

if this.texture == C_NULL
this.texture = SDL2.SDL_CreateTextureFromSurface(JulGame.Renderer, this.image)
this.setColor()
Expand Down
35 changes: 20 additions & 15 deletions src/Main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,27 +222,30 @@
end

if "SPACE" in this.input.buttonsHeldDown
if "LEFT" in this.input.buttonsHeldDown
this.zoom -= .01
this.zoom = clamp(this.zoom, 0.01, 10)
if "LEFT" in this.input.buttonsPressedDown
this.zoom -= .1
this.zoom = round(clamp(this.zoom, 0.2, 3); digits=1)
SDL2.SDL_RenderSetScale(JulGame.Renderer, this.zoom, this.zoom)
println("Zoom: ", this.zoom)

Check warning on line 229 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L225-L229

Added lines #L225 - L229 were not covered by tests
if update != C_NULL
SDL2.SDL_RenderSetScale(JulGame.Renderer, this.zoom, this.zoom)
end
elseif "RIGHT" in this.input.buttonsHeldDown
this.zoom += .01
this.zoom = clamp(this.zoom, 0.01, 10)
elseif "RIGHT" in this.input.buttonsPressedDown
this.zoom += .1
this.zoom = round(clamp(this.zoom, 0.2, 3); digits=1)
println("Zoom: ", this.zoom)

Check warning on line 235 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L232-L235

Added lines #L232 - L235 were not covered by tests

SDL2.SDL_RenderSetScale(JulGame.Renderer, this.zoom, this.zoom)

Check warning on line 237 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L237

Added line #L237 was not covered by tests
if update != C_NULL
SDL2.SDL_RenderSetScale(JulGame.Renderer, this.zoom, this.zoom)
end
end
elseif this.input.getButtonHeldDown("LEFT")
cameraPosition = Math.Vector2f(cameraPosition.x - 0.01, cameraPosition.y)
elseif this.input.getButtonHeldDown("RIGHT")
cameraPosition = Math.Vector2f(cameraPosition.x + 0.01, cameraPosition.y)
elseif this.input.getButtonHeldDown("DOWN")
cameraPosition = Math.Vector2f(cameraPosition.x, cameraPosition.y + 0.1)
cameraPosition = Math.Vector2f(cameraPosition.x, cameraPosition.y + 0.01)

Check warning on line 246 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L246

Added line #L246 was not covered by tests
elseif this.input.getButtonHeldDown("UP")
cameraPosition = Math.Vector2f(cameraPosition.x, cameraPosition.y - 0.1)
cameraPosition = Math.Vector2f(cameraPosition.x, cameraPosition.y - 0.01)

Check warning on line 248 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L248

Added line #L248 was not covered by tests
end

if update != C_NULL && update[6]
Expand Down Expand Up @@ -593,8 +596,9 @@
- `update`: An array containing information to pass back to the editor.

"""
function GameLoop(this, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), isEditor::Bool = false, update::Union{Ptr{Nothing}, Vector{Any}} = C_NULL, windowPos = C_NULL, windowSize = C_NULL)
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 = C_NULL, windowSize = C_NULL)
try
#SDL2.SDL_RenderSetScale(JulGame.Renderer, this.zoom, this.zoom)
lastStartTime = startTime[]
startTime[] = SDL2.SDL_GetPerformanceCounter()

Expand All @@ -614,7 +618,8 @@
end
end
if isEditor && update == C_NULL
this.scene.camera.dimensions = Math.Vector2(windowSize.x, windowSize.y)
this.scene.camera.dimensions = Math.Vector2(1920, 1080)
update_viewport(this, 1920, 1080)

Check warning on line 622 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L621-L622

Added lines #L621 - L622 were not covered by tests
this.scene.camera.windowPos = Math.Vector2(windowPos.x, windowPos.y)
end

Expand Down Expand Up @@ -701,7 +706,7 @@
rendercount = 0
# println("position: $(this.scene.camera.position) offset: $(this.scene.camera.offset) dimensions: $(this.scene.camera.dimensions)")
#println("dimensions: $(this.scene.camera.dimensions)")
zoomMultiplier = (isEditor && update == C_NULL) ? this.zoom : 1.0
zoomMultiplier = 1.0
for layer in this.spriteLayers["sort"]
for sprite in this.spriteLayers["$(layer)"]
spritePosition = sprite.parent.transform.getPosition()
Expand Down Expand Up @@ -760,7 +765,7 @@
end
colliderRenderCount += 1
collider = entity.collider
zoomMultiplier = (isEditor && update == C_NULL) ? this.zoom : 1.0
zoomMultiplier = 1.0

Check warning on line 768 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L768

Added line #L768 was not covered by tests
if collider.getType() == "CircleCollider"
SDL2E.SDL_RenderDrawCircle(
round(Int32, (pos.x - this.scene.camera.position.x) * SCALE_UNITS - ((entity.transform.getScale().x * SCALE_UNITS - SCALE_UNITS) / 2)),
Expand Down Expand Up @@ -805,7 +810,7 @@
println("delete entity with name $(selectedEntity.name) and id $(selectedEntity.id)")
end

zoomMultiplier = (isEditor && update == C_NULL) ? this.zoom : 1.0
zoomMultiplier = 1.0

Check warning on line 813 in src/Main.jl

View check run for this annotation

Codecov / codecov/patch

src/Main.jl#L813

Added line #L813 was not covered by tests
pos = selectedEntity.transform.getPosition()
size = selectedEntity.collider != C_NULL ? selectedEntity.collider.getSize() : selectedEntity.transform.getScale()
size = Math.Vector2f(size.x * zoomMultiplier, size.y * zoomMultiplier)
Expand Down
33 changes: 21 additions & 12 deletions src/editor/JulGameEditor/EditorSDLBackend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@
function run()
info = initSDLAndImGui()
window, renderer, ctx, io, clear_color = info[1], info[2], info[3], info[4], info[5]

sceneTexture = SDL2.SDL_CreateTexture(renderer, SDL2.SDL_PIXELFORMAT_BGRA8888, SDL2.SDL_TEXTUREACCESS_TARGET, 300, 200)# SDL2.SDL_SetRenderTarget(renderer, sceneTexture)
startingSize = ImVec2(1920, 1080)
sceneTexture = SDL2.SDL_CreateTexture(renderer, SDL2.SDL_PIXELFORMAT_BGRA8888, SDL2.SDL_TEXTUREACCESS_TARGET, startingSize.x, startingSize.y)# SDL2.SDL_SetRenderTarget(renderer, sceneTexture)
sceneTextureSize = ImVec2(startingSize.x, startingSize.y)

Check warning on line 32 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L30-L32

Added lines #L30 - L32 were not covered by tests

styleImGui()
showDemoWindow = true
game = nothing
gameInfo = []
windowPos = ImVec2(0, 0)
windowSize = ImVec2(0, 0)

sceneWindowPos = ImVec2(0, 0)
scenewindowSize = ImVec2(startingSize.x, startingSize.y)

Check warning on line 40 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L39-L40

Added lines #L39 - L40 were not covered by tests
quit = false
try
while !quit
Expand All @@ -44,7 +46,7 @@
end

StartFrame()
LibCImGui.igDockSpaceOverViewport(C_NULL, ImGuiDockNodeFlags_PassthruCentralNode, C_NULL) # Creating the "dockspace" that covers the whole window. This allows the child windows to automatically resize.
#LibCImGui.igDockSpaceOverViewport(C_NULL, ImGuiDockNodeFlags_PassthruCentralNode, C_NULL) # Creating the "dockspace" that covers the whole window. This allows the child windows to automatically resize.

################################## RENDER HERE

Expand All @@ -67,17 +69,23 @@
CImGui.End()
end
end

@cstatic begin
CImGui.Begin("Hello World")
windowPos = CImGui.GetWindowPos()
windowSize = CImGui.GetWindowSize()
CImGui.Image(sceneTexture, ImVec2(windowSize.x - 20, windowSize.y - 20))
sceneWindowPos = CImGui.GetWindowPos()
scenewindowSize = CImGui.GetWindowSize()
if scenewindowSize.x != sceneTextureSize.x || scenewindowSize.y != sceneTextureSize.y
SDL2.SDL_DestroyTexture(sceneTexture)
sceneTexture = SDL2.SDL_CreateTexture(renderer, SDL2.SDL_PIXELFORMAT_BGRA8888, SDL2.SDL_TEXTUREACCESS_TARGET, scenewindowSize.x, scenewindowSize.y)
sceneTextureSize = ImVec2(scenewindowSize.x, scenewindowSize.y)

Check warning on line 79 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L74-L79

Added lines #L74 - L79 were not covered by tests
end

CImGui.Image(sceneTexture, sceneTextureSize)

Check warning on line 82 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L82

Added line #L82 was not covered by tests
CImGui.End()
end
SDL2.SDL_SetRenderTarget(renderer, sceneTexture)
SDL2.SDL_RenderClear(renderer)

Check warning on line 86 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L86

Added line #L86 was not covered by tests
#SDL2.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
gameInfo = game === nothing ? [] : game.gameLoop(Ref(UInt64(0)), Ref(UInt64(0)), true, C_NULL, ImVec2(windowPos.x, windowPos.y + 20), ImVec2(windowSize.x, windowSize.y - 20))
gameInfo = game === nothing ? [] : game.gameLoop(Ref(UInt64(0)), Ref(UInt64(0)), true, C_NULL, ImVec2(0, 0), ImVec2(scenewindowSize.x, scenewindowSize.y - 20))

Check warning on line 88 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L88

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

Expand All @@ -86,11 +94,11 @@

################################# STOP RENDERING HERE
CImGui.Render()
SDL2.SDL_RenderSetScale(renderer, unsafe_load(io.DisplayFramebufferScale.x), unsafe_load(io.DisplayFramebufferScale.y));
#SDL2.SDL_RenderSetScale(renderer, unsafe_load(io.DisplayFramebufferScale.x), unsafe_load(io.DisplayFramebufferScale.y));
SDL2.SDL_SetRenderDrawColor(renderer, (UInt8)(round(clear_color[1] * 255)), (UInt8)(round(clear_color[2] * 255)), (UInt8)(round(clear_color[3] * 255)), (UInt8)(round(clear_color[4] * 255)));
SDL2.SDL_RenderClear(renderer);
ImGui_ImplSDLRenderer2_RenderDrawData(CImGui.GetDrawData(), test)
screenA = Ref(SDL2.SDL_Rect(round(windowPos.x), windowPos.y + 20, windowSize.x, windowSize.y - 20))
screenA = Ref(SDL2.SDL_Rect(round(sceneWindowPos.x), sceneWindowPos.y + 20, scenewindowSize.x, scenewindowSize.y - 20))

Check warning on line 101 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L101

Added line #L101 was not covered by tests
SDL2.SDL_RenderSetViewport(renderer, screenA)
################################################# Injecting game loop into editor
if game !== nothing
Expand All @@ -112,6 +120,7 @@
ImGui_ImplSDL2_Shutdown();

CImGui.DestroyContext(ctx)
SDL2.SDL_DestroyTexture(sceneTexture)

Check warning on line 123 in src/editor/JulGameEditor/EditorSDLBackend.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/EditorSDLBackend.jl#L123

Added line #L123 was not covered by tests
SDL_DestroyRenderer(renderer);
SDL2.SDL_DestroyWindow(window);
SDL2.SDL_Quit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
# r = SDL2.SDL_Rect(ix, iy, iz, iw)
r = SDL2.SDL_Rect((Int)(round(clip_min.x)), (Int)(round(clip_min.y)), (Int)(round(clip_max.x - clip_min.x)), (Int)(round(clip_max.y - clip_min.y)))

# @c SDL2.SDL_RenderSetClipRect(sdlRenderer, &r)
@c SDL2.SDL_RenderSetClipRect(sdlRenderer, &r) # This prevents rendering to outside of the current window. For example, if you have a window that is 800x600 and you try to render a 1000x1000 image, it will only render the part that is inside the window.

Check warning on line 173 in src/editor/JulGameEditor/ImGuiSDLBackend/imgui_impl_sdlrenderer2.jl

View check run for this annotation

Codecov / codecov/patch

src/editor/JulGameEditor/ImGuiSDLBackend/imgui_impl_sdlrenderer2.jl#L173

Added line #L173 was not covered by tests

color = unsafe_load(vtx_buffer.Data).col
r = (color >> 16) & 0xFF
Expand Down
Loading