Skip to content

Commit

Permalink
create window earlier to fix dependency issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Aug 17, 2024
1 parent b4c4694 commit 79cbc7a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 65 deletions.
15 changes: 0 additions & 15 deletions src/Main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,9 @@ module MainLoop

function prepare_window(isUsingEditor::Bool = false, size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true)
this::Main = MAIN
if size == Math.Vector2()
displayMode = SDL2.SDL_DisplayMode[SDL2.SDL_DisplayMode(0x12345678, 800, 600, 60, C_NULL)]
SDL2.SDL_GetCurrentDisplayMode(0, pointer(displayMode))
size = Math.Vector2(displayMode[1].w, displayMode[1].h)
end
this.autoScaleZoom = autoScaleZoom
scale_zoom(this, size.x, size.y)

this.screenSize = size != C_NULL ? size : this.scene.camera.size

flags = SDL2.SDL_RENDERER_ACCELERATED |
(isUsingEditor ? (SDL2.SDL_WINDOW_POPUP_MENU | SDL2.SDL_WINDOW_ALWAYS_ON_TOP | SDL2.SDL_WINDOW_BORDERLESS) : 0) |
(isResizable || isUsingEditor ? SDL2.SDL_WINDOW_RESIZABLE : 0) |
(size == Math.Vector2() ? SDL2.SDL_WINDOW_FULLSCREEN_DESKTOP : 0)

this.window = SDL2.SDL_CreateWindow(this.windowName, SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, this.screenSize.x, this.screenSize.y, flags)

JulGame.Renderer::Ptr{SDL2.SDL_Renderer} = SDL2.SDL_CreateRenderer(this.window, -1, SDL2.SDL_RENDERER_ACCELERATED)
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))
@info 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))))
Expand Down
27 changes: 23 additions & 4 deletions src/SceneManagement/SceneBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,38 @@ module SceneBuilderModule
MAIN.globals = globals
MAIN.level = this
MAIN.targetFrameRate = targetFrameRate
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene), isUsingEditor)
MAIN.scene.entities = scene[1]
MAIN.scene.uiElements = scene[2]

if size == Math.Vector2()
displayMode = SDL2.SDL_DisplayMode[SDL2.SDL_DisplayMode(0x12345678, 800, 600, 60, C_NULL)]
SDL2.SDL_GetCurrentDisplayMode(0, pointer(displayMode))
size = Math.Vector2(displayMode[1].w, displayMode[1].h)
end

if size.x < camSize.x && size.x > 0
camSize = Vector2(size.x, camSize.y)
end
if size.y < camSize.y && size.y > 0
camSize = Vector2(camSize.x, size.y)
end
MAIN.scene.camera = CameraModule.Camera(camSize, Vector2f(),Vector2f(), C_NULL)

flags = SDL2.SDL_RENDERER_ACCELERATED |
(isUsingEditor ? (SDL2.SDL_WINDOW_POPUP_MENU | SDL2.SDL_WINDOW_ALWAYS_ON_TOP | SDL2.SDL_WINDOW_BORDERLESS) : 0) |
(isResizable || isUsingEditor ? SDL2.SDL_WINDOW_RESIZABLE : 0) |
(size == Math.Vector2() ? SDL2.SDL_WINDOW_FULLSCREEN_DESKTOP : 0)

MAIN.screenSize = size != C_NULL ? size : MAIN.scene.camera.size
if !isUsingEditor
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

scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene), isUsingEditor)
MAIN.scene.entities = scene[1]
MAIN.scene.uiElements = scene[2]

for uiElement in MAIN.scene.uiElements
if "$(typeof(uiElement))" == "JulGame.UI.TextBoxModule.Textbox" && uiElement.isWorldEntity
if "$(typeof(uiElement))" == "JulGame.UI.TextBoxModule.Textbox" && !uiElement.isWorldEntity
UI.center_text(uiElement)
end
end
Expand Down
3 changes: 2 additions & 1 deletion src/SceneManagement/SceneReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ module SceneReaderModule
try
newUIElement = nothing
if uiElement.type == "TextBox"
newUIElement = TextBox(uiElement.name, uiElement.fontPath, uiElement.fontSize, Vector2(uiElement.position.x, uiElement.position.y), uiElement.text, uiElement.isCenteredX, uiElement.isCenteredY)
newUIElement = TextBox(uiElement.name, uiElement.fontPath, uiElement.fontSize, Vector2(uiElement.position.x, uiElement.position.y), uiElement.text, uiElement.isCenteredX, uiElement.isCenteredY)
newUIElement.isWorldEntity = uiElement.isWorldEntity
else
newUIElement = ScreenButton(uiElement.name, uiElement.buttonUpSpritePath, uiElement.buttonDownSpritePath, Vector2(uiElement.size.x, uiElement.size.y), Vector2(uiElement.position.x, uiElement.position.y), uiElement.fontPath, uiElement.text, Vector2(uiElement.textOffset.x, uiElement.textOffset.y))
end
Expand Down
3 changes: 1 addition & 2 deletions src/UI/TextBox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ module TextBoxModule
end

function UI.load_font(this::TextBox, basePath::String, fontPath::String)
println("loading font from base: $(basePath)")
println("loading font from $(fontPath)")
@info string("loading font from $(basePath)\\$(fontPath)")
this.font = CallSDLFunction(SDL2.TTF_OpenFont, joinpath(basePath, fontPath), this.fontSize)
if this.font == C_NULL
return
Expand Down
3 changes: 2 additions & 1 deletion src/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ function CallSDLFunction(func::Function, args...)
# Call SDL function and check for errors
ret = func(args...)
if (isa(ret, Number) && ret < 0) || ret == C_NULL
println(unsafe_string(SDL2.SDL_GetError()))
@error "$(unsafe_string(SDL2.SDL_GetError())) $(@__FILE__):$(@__LINE__)"
@info "Check Utils.jl"
Base.show_backtrace(stdout, catch_backtrace())
end

Expand Down
29 changes: 26 additions & 3 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function show_field_editor(entity, fieldName, animation_window_dict)
end

if isa(field, SpriteModule.InternalSprite)
show_sprite_fields(entity.sprite)
show_sprite_fields(entity.sprite, animation_window_dict)
elseif isa(field, SoundSourceModule.InternalSoundSource)
show_sound_source_fields(entity.soundSource)
elseif isa(field, AnimatorModule.InternalAnimator)
Expand Down Expand Up @@ -279,7 +279,7 @@ and updates the `sprite.imagePath` field with the current text in the text box.
- `sprite`: The sprite object to display the fields for.
"""
function show_sprite_fields(sprite)
function show_sprite_fields(sprite, animation_window_dict)
for field in fieldnames(typeof(sprite))
fieldString = "$(field)"

Expand Down Expand Up @@ -309,7 +309,30 @@ function show_sprite_fields(sprite)
sprite.imagePath = imagePath
end
CImGui.Button("Load Image") && (Component.load_image(sprite, currentTextInTextBox))
else
elseif fieldString == "crop"
crop_x, crop_y, crop_w, crop_h = sprite.crop.x, sprite.crop.y, sprite.crop.z, sprite.crop.t

points = Ref(Vector{ImVec2}([ImVec2(crop_x, crop_y), ImVec2(crop_x + crop_w, crop_y + crop_h)]))
scrolling = Ref(ImVec2(0.0, 0.0))
adding_line = Ref(false)
zoom_level = Ref(1.0)
grid_step = Ref(Int32(64))
# put these in a ref dictionary
window_info = Ref(Dict("points" => points, "scrolling" => scrolling, "adding_line" => adding_line, "zoom_level" => zoom_level, "grid_step" => grid_step))
# check if animation_window_dict has the key "frame $(k)"
key = "crop-$(sprite.parent.id)"
if haskey(animation_window_dict[], key)
# animation_window_dict[]["frame $(k)"][]["points"] = points
window_info[] = animation_window_dict[][key][]
else
animation_window_dict[][key] = window_info
end

crop_x, crop_y, crop_w, crop_h = show_animation_window("crop", window_info, sprite.texture, sprite.size.x, sprite.size.y)
vec4i = Cint[crop_x, crop_y, crop_w, crop_h]
@c CImGui.InputInt4("crop", vec4i)
sprite.crop = JulGame.Math.Vector4(Int32(vec4i[1]), Int32(vec4i[2]), Int32(vec4i[3]), Int32(vec4i[4]))
else
show_component_field_input(sprite, field)
end
end
Expand Down
1 change: 0 additions & 1 deletion src/editor/JulGameEditor/Components/MainMenuBar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ function ShowMenuFile(events)
events[end]()
end
if length(events) > 1 && CImGui.MenuItem("Save", "Ctrl+S")
@info "Trigger Save | find me here: $(@__FILE__):$(@__LINE__)"
events[1]()
end
end
45 changes: 7 additions & 38 deletions src/editor/JulGameEditor/Components/SpriteViewer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,8 @@ using CImGui.CSyntax
using CImGui.CSyntax.CFor
using CImGui.CSyntax.CStatic

function load_texture_from_file(filename::String, renderer::Ptr{SDL2.SDL_Renderer})
width = Ref{Cint}()
height = Ref{Cint}()
channels = Ref{Cint}()

surface = SDL2.IMG_Load(filename)
if surface == C_NULL
@error "Failed to load image: $(unsafe_string(SDL2.SDL_GetError()))"
return false, C_NULL, 0, 0
end

surfaceInfo = unsafe_wrap(Array, surface, 10; own = false)
width[] = surfaceInfo[1].w
height[] = surfaceInfo[1].h

if surface == C_NULL
@error "Failed to create SDL surface: $(unsafe_string(SDL2.SDL_GetError()))"
return false, C_NULL, 0, 0
end

texture_ptr = SDL2.SDL_CreateTextureFromSurface(renderer, surface)

if texture_ptr == C_NULL
@error "Failed to create SDL texture: $(unsafe_string(SDL2.SDL_GetError()))"
end

SDL2.SDL_FreeSurface(surface)

return true, texture_ptr, width[], height[] #, data
end


"""
ShowExampleAppCustomRendering(p_open::Ref{Bool})
show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_tex_h)
Demonstrate using the low-level ImDrawList to draw custom shapes.
"""
function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_tex_h)
Expand Down Expand Up @@ -92,7 +60,7 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
end
if window_info[]["adding_line"][]
window_info[]["points"][][end] = mouse_pos_in_canvas_zoom_adjusted
if !CImGui.IsMouseDown(CImGui.ImGuiMouseButton_Left)
if is_active && !CImGui.IsMouseDown(CImGui.ImGuiMouseButton_Left)
window_info[]["points"][] = [window_info[]["points"][][end-1], window_info[]["points"][][end]] # only keep last two points
window_info[]["adding_line"][] = false
end
Expand All @@ -105,14 +73,14 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
end

# Zoom
if unsafe_load(io.KeyCtrl)
if is_active && unsafe_load(io.KeyCtrl)
window_info[]["zoom_level"][] += unsafe_load(io.MouseWheel) * 4.0 # * 0.10
window_info[]["zoom_level"][] = clamp(window_info[]["zoom_level"][], 1.0, 50.0)
end

# Context menu
drag_delta = CImGui.GetMouseDragDelta(CImGui.ImGuiMouseButton_Right)
if CImGui.IsMouseReleased(CImGui.ImGuiMouseButton_Right) && drag_delta.x == 0.0 && drag_delta.y == 0.0
if is_active && CImGui.IsMouseReleased(CImGui.ImGuiMouseButton_Right) && drag_delta.x == 0.0 && drag_delta.y == 0.0
CImGui.OpenPopupOnItemClick("context")
end
if CImGui.BeginPopup("context")
Expand All @@ -139,7 +107,10 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
for y in 0:GRID_STEP:canvas_sz.y*10 # TODO: 10 is arbitrary
CImGui.AddLine(draw_list, ImVec2(canvas_p0.x, origin.y + canvas_p0.y + y), ImVec2(canvas_p1.x, origin.y + canvas_p0.y + y), IM_COL32(200, 200, 200, 40))
end
CImGui.PopClipRect(draw_list)

CImGui.AddImage(draw_list, my_tex_id, ImVec2(origin.x + canvas_p0.x, origin.y + canvas_p0.y), ImVec2(origin.x + (my_tex_w * window_info[]["zoom_level"][]) + canvas_p0.x, origin.y + (my_tex_h * window_info[]["zoom_level"][]) + canvas_p0.y), ImVec2(0,0), ImVec2(1,1), IM_COL32(255,255,255,255))

# Draw squares with add rect
for n in 1:2:length(window_info[]["points"][])-1
p1 = ImVec2(origin.x + canvas_p0.x + (window_info[]["points"][][n].x * window_info[]["zoom_level"][]), origin.y + canvas_p0.y + (window_info[]["points"][][n].y * window_info[]["zoom_level"][]))
Expand All @@ -148,8 +119,6 @@ function show_animation_window(frame_name, window_info, my_tex_id, my_tex_w, my_
CImGui.AddRect(draw_list, p1, p2, IM_COL32(255, 255, 0, 255))
end

CImGui.AddImage(draw_list, my_tex_id, ImVec2(origin.x + canvas_p0.x, origin.y + canvas_p0.y), ImVec2(origin.x + (my_tex_w * window_info[]["zoom_level"][]) + canvas_p0.x, origin.y + (my_tex_h * window_info[]["zoom_level"][]) + canvas_p0.y), ImVec2(0,0), ImVec2(1,1), IM_COL32(255,255,255,255))
CImGui.PopClipRect(draw_list)

CImGui.End()

Expand Down

0 comments on commit 79cbc7a

Please sign in to comment.