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 all 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
6 changes: 3 additions & 3 deletions src/Component/CircleCollider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
this.isGrounded = this.parent.rigidbody.grounded


for i in 1:length(colliders)
for i in eachindex(colliders)

Check warning on line 73 in src/Component/CircleCollider.jl

View check run for this annotation

Codecov / codecov/patch

src/Component/CircleCollider.jl#L73

Added line #L73 was not covered by tests
#TODO: Skip any out of a certain range of this. This will prevent a bunch of unnecessary collision checks
if !colliders[i].getParent().isActive || !colliders[i].enabled
if this.parent.rigidbody.grounded && i == length(colliders)
Expand All @@ -82,7 +82,7 @@
collision = CheckCollision(this, colliders[i])
if CheckIfResting(this, colliders[i])[1] == true && length(this.currentRests) > 0 && !(colliders[i] in this.currentRests)
# if this collider isn't already in the list of current rests, check if it is on the same Y level and the same size as any of the current rests, if it is, then add it to current rests
for j in 1:length(this.currentRests)
for j in eachindex(this.currentRests)

Check warning on line 85 in src/Component/CircleCollider.jl

View check run for this annotation

Codecov / codecov/patch

src/Component/CircleCollider.jl#L85

Added line #L85 was not covered by tests
if this.currentRests[j].getParent().transform.getPosition().y == colliders[i].getParent().transform.getPosition().y && this.currentRests[j].getSize().y == colliders[i].getSize().y
push!(this.currentRests, colliders[i])
break
Expand Down Expand Up @@ -135,7 +135,7 @@
# end
end
end
for i in 1:length(this.currentRests)
for i in eachindex(this.currentRests)

Check warning on line 138 in src/Component/CircleCollider.jl

View check run for this annotation

Codecov / codecov/patch

src/Component/CircleCollider.jl#L138

Added line #L138 was not covered by tests
if CheckIfResting(this, this.currentRests[i])[1] == false
deleteat!(this.currentRests, i)
break
Expand Down
7 changes: 4 additions & 3 deletions src/Component/Collider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
collision = CheckCollision(this, collider)
if CheckIfResting(this, collider)[1] == true && length(this.currentRests) > 0 && !(collider in this.currentRests)
# if this collider isn't already in the list of current rests, check if it is on the same Y level and the same size as any of the current rests, if it is, then add it to current rests
for j in 1:length(this.currentRests)
for j in eachindex(this.currentRests)

Check warning on line 133 in src/Component/Collider.jl

View check run for this annotation

Codecov / codecov/patch

src/Component/Collider.jl#L133

Added line #L133 was not covered by tests
if this.currentRests[j].getParent().transform.getPosition().y == collider.getParent().transform.getPosition().y && this.currentRests[j].getSize().y == collider.getSize().y
push!(this.currentRests, collider)
break
Expand Down Expand Up @@ -173,7 +173,8 @@
eventToCall(collider)
catch e
println(e)
Base.show_backtrace(stdout, catch_backtrace())
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)

Check warning on line 177 in src/Component/Collider.jl

View check run for this annotation

Codecov / codecov/patch

src/Component/Collider.jl#L176-L177

Added lines #L176 - L177 were not covered by tests
end
end
#Begin to overlap, correct position
Expand All @@ -193,7 +194,7 @@

#println("Skipped $colliderSkipCount colliders, checked $colliderCheckedCount")

for i in 1:length(this.currentRests)
for i in eachindex(this.currentRests)
if CheckIfResting(this, this.currentRests[i])[1] == false
deleteat!(this.currentRests, i)
break
Expand Down
39 changes: 20 additions & 19 deletions src/Component/Sprite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ module SpriteModule
position::Math.Vector2f
rotation::Float64
pixelsPerUnit::Int32
center::Math.Vector2
end

export InternalSprite
mutable struct InternalSprite
center::Math.Vector2
color::Math.Vector3
crop::Union{Ptr{Nothing}, Math.Vector4}
isFlipped::Bool
Expand All @@ -35,12 +37,13 @@ module SpriteModule
size::Math.Vector2
texture::Union{Ptr{Nothing}, Ptr{SDL2.LibSDL2.SDL_Texture}}

function InternalSprite(parent::Any, imagePath::String, crop::Union{Ptr{Nothing}, Math.Vector4}=C_NULL, isFlipped::Bool=false, color::Math.Vector3 = Math.Vector3(255,255,255), isCreatedInEditor::Bool=false; pixelsPerUnit::Int32=Int32(-1), isWorldEntity::Bool=true, position::Math.Vector2f = Math.Vector2f(), rotation::Float64 = 0.0, layer::Int32 = Int32(0))
function InternalSprite(parent::Any, imagePath::String, crop::Union{Ptr{Nothing}, Math.Vector4}=C_NULL, isFlipped::Bool=false, color::Math.Vector3 = Math.Vector3(255,255,255), isCreatedInEditor::Bool=false; pixelsPerUnit::Int32=Int32(-1), isWorldEntity::Bool=true, position::Math.Vector2f = Math.Vector2f(), rotation::Float64 = 0.0, layer::Int32 = Int32(0), center::Math.Vector2 = Math.Vector2(0,0))
this = new()

this.offset = Math.Vector2f()
this.isFlipped = isFlipped
this.imagePath = imagePath
this.center = center
this.color = color
this.crop = crop
this.image = C_NULL
Expand Down Expand Up @@ -77,21 +80,19 @@ module SpriteModule

function Base.getproperty(this::InternalSprite, s::Symbol)
method_props = (
draw = Component.draw,
initialize = Component.initialize,
flip = Component.flip,
setParent = Component.set_parent,
loadImage = Component.load_image,
destroy = Component.destroy,
setColor = Component.set_color
)
deprecated_get_property(method_props, this, s)
end
function Component.draw(this::InternalSprite, zoom::Float64 = 1.0)

function Component.draw(this::InternalSprite)
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 All @@ -100,43 +101,43 @@ module SpriteModule
parentTransform = this.parent.transform

cameraDiff = this.isWorldEntity ?
Math.Vector2(MAIN.scene.camera.position.x * SCALE_UNITS - MAIN.scene.camera.windowPos.x, MAIN.scene.camera.position.y * SCALE_UNITS - MAIN.scene.camera.windowPos.y) :
Math.Vector2(MAIN.scene.camera.position.x * SCALE_UNITS, MAIN.scene.camera.position.y * SCALE_UNITS) :
Math.Vector2(0,0)
position = this.isWorldEntity ?
parentTransform.getPosition() :
this.position

srcRect = (this.crop == Math.Vector4(0,0,0,0) || this.crop == C_NULL) ? C_NULL : Ref(SDL2.SDL_Rect(this.crop.x, this.crop.y, this.crop.z, this.crop.t))
dstRect = Ref(SDL2.SDL_FRect(
(position.x + this.offset.x) * SCALE_UNITS * zoom - cameraDiff.x - (parentTransform.getScale().x * SCALE_UNITS - SCALE_UNITS) / 2, # TODO: Center the sprite within the entity
(position.y + this.offset.y) * SCALE_UNITS * zoom - cameraDiff.y - (parentTransform.getScale().y * SCALE_UNITS - SCALE_UNITS) / 2,
(this.crop == C_NULL ? this.size.x : this.crop.z) * zoom * SCALE_UNITS,
(this.crop == C_NULL ? this.size.y : this.crop.t) * zoom * SCALE_UNITS
(position.x + this.offset.x) * SCALE_UNITS - cameraDiff.x - (parentTransform.getScale().x * SCALE_UNITS - SCALE_UNITS) / 2, # TODO: Center the sprite within the entity
(position.y + this.offset.y) * SCALE_UNITS - cameraDiff.y - (parentTransform.getScale().y * SCALE_UNITS - SCALE_UNITS) / 2,
(this.crop == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS,
(this.crop == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS
))

if this.pixelsPerUnit > 0 || JulGame.PIXELS_PER_UNIT > 0
ppu = this.pixelsPerUnit > 0 ? this.pixelsPerUnit : JulGame.PIXELS_PER_UNIT
dstRect = Ref(SDL2.SDL_FRect(
(position.x + this.offset.x) * SCALE_UNITS * zoom - cameraDiff.x - ((srcRect == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS / ppu - SCALE_UNITS) / 2,
(position.y + this.offset.y) * SCALE_UNITS * zoom - cameraDiff.y - ((srcRect == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS / ppu - SCALE_UNITS) / 2,
(srcRect == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS/ppu * zoom,
(srcRect == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS/ppu * zoom
(position.x + this.offset.x) * SCALE_UNITS - cameraDiff.x - ((srcRect == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS / ppu - SCALE_UNITS) / 2,
(position.y + this.offset.y) * SCALE_UNITS - cameraDiff.y - ((srcRect == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS / ppu - SCALE_UNITS) / 2,
(srcRect == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS/ppu,
(srcRect == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS/ppu
))
end

srcRect = !this.isFloatPrecision ? (this.crop == Math.Vector4(0,0,0,0) || this.crop == C_NULL) ? C_NULL : Ref(SDL2.SDL_Rect(this.crop.x,this.crop.y,this.crop.z,this.crop.t)) : srcRect
dstRect = !this.isFloatPrecision ? Ref(SDL2.SDL_Rect(
convert(Int32, round((position.x + this.offset.x) * SCALE_UNITS * zoom - cameraDiff.x - (parentTransform.getScale().x * SCALE_UNITS - SCALE_UNITS) / 2)), # TODO: Center the sprite within the entity
convert(Int32, round((position.y + this.offset.y) * SCALE_UNITS * zoom - cameraDiff.y - (parentTransform.getScale().y * SCALE_UNITS - SCALE_UNITS) / 2)),
convert(Int32, round((position.x + this.offset.x) * SCALE_UNITS - cameraDiff.x - (parentTransform.getScale().x * SCALE_UNITS - SCALE_UNITS) / 2)), # TODO: Center the sprite within the entity
convert(Int32, round((position.y + this.offset.y) * SCALE_UNITS - cameraDiff.y - (parentTransform.getScale().y * SCALE_UNITS - SCALE_UNITS) / 2)),
convert(Int32, round(this.crop == C_NULL ? this.size.x : this.crop.z)),
convert(Int32, round(this.crop == C_NULL ? this.size.y : this.crop.t))
)) : dstRect

if this.pixelsPerUnit > 0 || JulGame.PIXELS_PER_UNIT > 0 && !this.isFloatPrecision
ppu = this.pixelsPerUnit > 0 ? this.pixelsPerUnit : JulGame.PIXELS_PER_UNIT
dstRect = Ref(SDL2.SDL_Rect(
convert(Int32, round((position.x + this.offset.x) * SCALE_UNITS * zoom - cameraDiff.x - ((srcRect == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS / ppu - SCALE_UNITS) / 2)),
convert(Int32, round((position.y + this.offset.y) * SCALE_UNITS * zoom - cameraDiff.y - ((srcRect == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS / ppu - SCALE_UNITS) / 2)),
convert(Int32, round((position.x + this.offset.x) * SCALE_UNITS - cameraDiff.x - ((srcRect == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS / ppu - SCALE_UNITS) / 2)),
convert(Int32, round((position.y + this.offset.y) * SCALE_UNITS - cameraDiff.y - ((srcRect == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS / ppu - SCALE_UNITS) / 2)),
convert(Int32, round((srcRect == C_NULL ? this.size.x : this.crop.z) * SCALE_UNITS/ppu)),
convert(Int32, round((srcRect == C_NULL ? this.size.y : this.crop.t) * SCALE_UNITS/ppu))
))
Expand All @@ -147,7 +148,7 @@ module SpriteModule
this.texture,
srcRect,
dstRect,
this.rotation, # ROTATION
this.rotation,
C_NULL, # Ref(SDL2.SDL_Point(0,0)) CENTER
this.isFlipped ? SDL2.SDL_FLIP_HORIZONTAL : SDL2.SDL_FLIP_NONE) != 0

Expand Down
14 changes: 8 additions & 6 deletions src/Entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
animator::Union{InternalAnimator, Ptr{Nothing}}
collider::Union{InternalCollider, Ptr{Nothing}}
circleCollider::Union{InternalCircleCollider, Ptr{Nothing}}
isActive::Bool
name::String
persistentBetweenScenes::Bool
rigidbody::Union{InternalRigidbody, Ptr{Nothing}}
scripts::Vector{Any}
shape::Union{InternalShape, Ptr{Nothing}}
soundSource::Union{InternalSoundSource, Ptr{Nothing}}
sprite::Union{InternalSprite, Ptr{Nothing}}
transform::Transform
isActive::Bool
name::String
persistentBetweenScenes::Bool
scripts::Vector{Any}

function Entity(name::String = "New entity", transform::Transform = Transform(), scripts::Vector = [])
this = new()
Expand Down Expand Up @@ -79,6 +79,7 @@
catch e
println(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)

Check warning on line 82 in src/Entity.jl

View check run for this annotation

Codecov / codecov/patch

src/Entity.jl#L82

Added line #L82 was not covered by tests
end
end

Expand All @@ -89,6 +90,7 @@
catch e
println(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)

Check warning on line 93 in src/Entity.jl

View check run for this annotation

Codecov / codecov/patch

src/Entity.jl#L93

Added line #L93 was not covered by tests
end
end
end
Expand Down Expand Up @@ -156,13 +158,13 @@
return newSoundSource
end

function JulGame.add_sprite(this::Entity, isCreatedInEditor::Bool = false, sprite::Sprite = Sprite(Math.Vector3(255, 255, 255), C_NULL, false, "", true, 0, Math.Vector2f(0,0), Math.Vector2f(0,0), 0, -1))
function JulGame.add_sprite(this::Entity, isCreatedInEditor::Bool = false, sprite::Sprite = Sprite(Math.Vector3(255, 255, 255), C_NULL, false, "", true, 0, Math.Vector2f(0,0), Math.Vector2f(0,0), 0, -1, Math.Vector2(0,0)))
if this.sprite != C_NULL
println("Sprite already exists on entity named ", this.name)
return
end

this.sprite = InternalSprite(this::Entity, sprite.imagePath, sprite.crop, sprite.isFlipped, sprite.color, isCreatedInEditor; pixelsPerUnit=sprite.pixelsPerUnit, isWorldEntity=sprite.isWorldEntity, position=sprite.position, rotation=sprite.rotation, layer=sprite.layer)
this.sprite = InternalSprite(this::Entity, sprite.imagePath, sprite.crop, sprite.isFlipped, sprite.color, isCreatedInEditor; pixelsPerUnit=sprite.pixelsPerUnit, isWorldEntity=sprite.isWorldEntity, position=sprite.position, rotation=sprite.rotation, layer=sprite.layer, center=sprite.center)
if this.animator != C_NULL
Component.set_sprite(this.animator, this.sprite)
end
Expand Down
Loading
Loading