Skip to content

Commit e9dded8

Browse files
committed
Is editor global
1 parent ac6bf30 commit e9dded8

File tree

11 files changed

+102
-75
lines changed

11 files changed

+102
-75
lines changed

src/JulGame.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ module JulGame
33
using SimpleDirectMediaLayer
44
const SDL2 = SimpleDirectMediaLayer
55
MAIN = nothing
6+
IS_EDITOR = false
67

78
include("ModuleExtensions/SDL2Extension.jl")
89
const SDL2E = SDL2Extension
9-
export SDL2, SDL2E, MAIN
10+
export IS_EDITOR, SDL2, SDL2E, MAIN
1011

1112
include("utils/Utils.jl")
1213
export CallSDLFunction

src/Main.jl

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,25 @@ module MainLoop
6969
end
7070
end
7171

72-
function prepare_window_scripts_and_start_loop(isUsingEditor = false, size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true, isNewEditor::Bool = false)
73-
if !isNewEditor
74-
prepare_window(isUsingEditor, size, isResizable, autoScaleZoom)
72+
function prepare_window_scripts_and_start_loop(size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true)
73+
if !JulGame.IS_EDITOR
74+
prepare_window(size, isResizable, autoScaleZoom)
7575
end
76-
initialize_scripts_and_components(isUsingEditor)
76+
println("Preparing window")
7777

78-
if !isUsingEditor
78+
initialize_scripts_and_components()
79+
80+
if !JulGame.IS_EDITOR
7981
full_loop(MAIN)
8082
return
8183
end
8284
end
8385

84-
function initialize_new_scene(this::Main, isUsingEditor::Bool = false)
85-
SceneBuilderModule.deserialize_and_build_scene(this.level, isUsingEditor)
86-
initialize_scripts_and_components(isUsingEditor)
86+
function initialize_new_scene(this::Main)
87+
SceneBuilderModule.deserialize_and_build_scene(this.level)
88+
initialize_scripts_and_components()
8789

88-
if !isUsingEditor
90+
if !JulGame.IS_EDITOR
8991
full_loop(this)
9092
return
9193
end
@@ -103,7 +105,7 @@ module MainLoop
103105
lastPhysicsTime = Ref(UInt64(SDL2.SDL_GetTicks()))
104106
while !this.close
105107
try
106-
game_loop(this, startTime, lastPhysicsTime, false)
108+
game_loop(this, startTime, lastPhysicsTime)
107109
catch e
108110
if this.testMode
109111
throw(e)
@@ -142,7 +144,7 @@ module MainLoop
142144
SDL2.SDL_Quit()
143145
else
144146
this.shouldChangeScene = false
145-
initialize_new_scene(this, false)
147+
initialize_new_scene(this)
146148
end
147149
end
148150
end
@@ -197,7 +199,7 @@ module MainLoop
197199
end
198200
end
199201

200-
function prepare_window(isUsingEditor::Bool = false, size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true)
202+
function prepare_window(size = C_NULL, isResizable::Bool = false, autoScaleZoom::Bool = true)
201203
this::Main = MAIN
202204
this.autoScaleZoom = autoScaleZoom
203205
scale_zoom(this, size.x, size.y)
@@ -213,7 +215,7 @@ module MainLoop
213215
SDL2.SDL_setFramerate(this.fpsManager, UInt32(60))
214216
end
215217

216-
function initialize_scripts_and_components(isUsingEditor::Bool = false)
218+
function initialize_scripts_and_components()
217219
this::Main = MAIN
218220
scripts = []
219221
for entity in this.scene.entities
@@ -230,7 +232,7 @@ function initialize_scripts_and_components(isUsingEditor::Bool = false)
230232

231233
this.spriteLayers = build_sprite_layers()
232234

233-
if !isUsingEditor || this.isGameModeRunningInEditor
235+
if !JulGame.IS_EDITOR || this.isGameModeRunningInEditor
234236
for script in scripts
235237
try
236238
# TODO: only call latest if in editor and in game mode
@@ -249,16 +251,14 @@ end
249251

250252
export change_scene
251253
"""
252-
change_scene(sceneFileName::String, isEditor::Bool = false)
254+
change_scene(sceneFileName::String)
253255
254256
Change the scene to the specified `sceneFileName`. This function destroys the current scene, including all entities, textboxes, and screen buttons, except for the ones marked as persistent. It then loads the new scene and sets the camera and persistent entities, textboxes, and screen buttons.
255257
256258
# Arguments
257259
- `sceneFileName::String`: The name of the scene file to load.
258-
- `isEditor::Bool`: Whether the scene is being loaded in the editor. Default is `false`.
259-
260260
"""
261-
function JulGame.change_scene(sceneFileName::String, isEditor::Bool = false)
261+
function JulGame.change_scene(sceneFileName::String)
262262
this::Main = MAIN
263263
# println("Changing scene to: ", sceneFileName)
264264
this.close = true
@@ -269,24 +269,26 @@ function JulGame.change_scene(sceneFileName::String, isEditor::Bool = false)
269269
skipcount = 0
270270
persistentEntities = []
271271
for entity in this.scene.entities
272-
if entity.persistentBetweenScenes && (!isEditor || this.isGameModeRunningInEditor)
272+
if entity.persistentBetweenScenes && (!JulGame.IS_EDITOR || this.isGameModeRunningInEditor)
273273
#println("Persistent entity: ", entity.name, " with id: ", entity.id)
274274
push!(persistentEntities, entity)
275275
skipcount += 1
276276
continue
277277
end
278278

279279
destroy_entity_components(this, entity)
280-
for script in entity.scripts
281-
try
282-
# TODO: only call latest if in editor and in game mode
283-
Base.invokelatest(JulGame.on_shutdown, script)
284-
catch e
285-
if typeof(e) != ErrorException
286-
println("Error shutting down script")
287-
@error string(e)
288-
Base.show_backtrace(stdout, catch_backtrace())
289-
rethrow(e)
280+
if !JulGame.IS_EDITOR
281+
for script in entity.scripts
282+
try
283+
# TODO: only call latest if in editor and in game mode
284+
Base.invokelatest(JulGame.on_shutdown, script)
285+
catch e
286+
if typeof(e) != ErrorException
287+
println("Error shutting down script")
288+
@error string(e)
289+
Base.show_backtrace(stdout, catch_backtrace())
290+
rethrow(e)
291+
end
290292
end
291293
end
292294
end
@@ -319,8 +321,8 @@ function JulGame.change_scene(sceneFileName::String, isEditor::Bool = false)
319321
this.scene.camera = camera
320322
this.level.scene = sceneFileName
321323

322-
if isEditor
323-
initialize_new_scene(this, true)
324+
if JulGame.IS_EDITOR
325+
initialize_new_scene(this)
324326
end
325327
end
326328

@@ -452,21 +454,19 @@ function JulGame.create_entity(entity)
452454
end
453455

454456
"""
455-
game_loop(this::Main, 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)
457+
game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), close::Ref{Bool} = Ref(Bool(false)), Vector{Any}} = C_NULL)
456458
457459
Runs the game loop.
458460
459461
Parameters:
460462
- `this`: The main struct.
461463
- `startTime`: A reference to the start time of the game loop.
462464
- `lastPhysicsTime`: A reference to the last physics time of the game loop.
463-
- `isEditor`: A boolean indicating whether the game loop is running in editor mode.
464-
465465
"""
466-
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))
467-
if this.shouldChangeScene && !isEditor
466+
function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhysicsTime::Ref{UInt64} = Ref(UInt64(0)), windowPos::Math.Vector2 = Math.Vector2(0,0), windowSize::Math.Vector2 = Math.Vector2(0,0))
467+
if this.shouldChangeScene && !JulGame.IS_EDITOR
468468
this.shouldChangeScene = false
469-
initialize_new_scene(this, false)
469+
initialize_new_scene(this)
470470
return
471471
end
472472
try
@@ -475,29 +475,29 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys
475475
lastStartTime = startTime[]
476476
startTime[] = SDL2.SDL_GetPerformanceCounter()
477477

478-
if isEditor
478+
if JulGame.IS_EDITOR
479479
this.scene.camera.size = Math.Vector2(windowSize.x, windowSize.y)
480480
end
481481

482482
DEBUG = false
483483
#region Input
484-
if !isEditor
484+
if !JulGame.IS_EDITOR
485485
JulGame.InputModule.poll_input(this.input)
486486
end
487487

488-
if this.input.quit && !isEditor
488+
if this.input.quit && !JulGame.IS_EDITOR
489489
this.close = true
490490
end
491491
DEBUG = this.input.debug
492492

493493
cameraPosition = Math.Vector2f()
494494

495-
if !isEditor
495+
if !JulGame.IS_EDITOR
496496
SDL2.SDL_RenderClear(JulGame.Renderer::Ptr{SDL2.SDL_Renderer})
497497
end
498498

499499
#region Physics
500-
if !isEditor || this.isGameModeRunningInEditor
500+
if !JulGame.IS_EDITOR || this.isGameModeRunningInEditor
501501
currentPhysicsTime = SDL2.SDL_GetTicks()
502502
deltaTime = (currentPhysicsTime - lastPhysicsTime[]) / 1000.0
503503

@@ -530,7 +530,7 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys
530530
continue
531531
end
532532

533-
if !isEditor || this.isGameModeRunningInEditor
533+
if !JulGame.IS_EDITOR || this.isGameModeRunningInEditor
534534
try
535535
JulGame.update(entity, deltaTime)
536536
if this.close && !this.isGameModeRunningInEditor
@@ -666,7 +666,7 @@ function game_loop(this::Main, startTime::Ref{UInt64} = Ref(UInt64(0)), lastPhys
666666
end
667667
end
668668

669-
if !isEditor
669+
if !JulGame.IS_EDITOR
670670
SDL2.SDL_RenderPresent(JulGame.Renderer::Ptr{SDL2.SDL_Renderer})
671671
SDL2.SDL_framerateDelay(this.fpsManager)
672672
end

src/editor/JulGameEditor/Components/ComponentInputs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function show_sprite_fields(sprite, animation_window_dict)
329329
# animation_window_dict[]["frame $(k)"][]["points"] = points
330330
window_info[] = animation_window_dict[][key][]
331331
else
332-
print("Adding crop window info for: $key")
332+
# print("Adding crop window info for: $key")
333333
animation_window_dict[][key] = window_info
334334
end
335335

src/editor/JulGameEditor/Editor.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ module Editor
9999
if CImGui.Button("$(scene)")
100100
currentSceneName = SceneLoaderModule.get_scene_file_name_from_full_scene_path(scene)
101101
if currentSceneMain === nothing
102+
JulGame.IS_EDITOR = true
103+
JulGame.PIXELS_PER_UNIT = 16
102104
currentSceneMain = load_scene(scene, renderer)
103105
currentSceneMain.cameraBackgroundColor = (50, 50, 50)
104-
JulGame.PIXELS_PER_UNIT = 16
105-
currentSceneMain.autoScaleZoom = true
106106
currentSelectedProjectPath = SceneLoaderModule.get_project_path_from_full_scene_path(scene)
107107
else
108-
JulGame.change_scene(String(currentSceneName), true)
108+
JulGame.change_scene(String(currentSceneName))
109109
end
110110
end
111111
CImGui.NewLine()
@@ -132,7 +132,7 @@ module Editor
132132
JulGame.MainLoop.start_game_in_editor(currentSceneMain, currentSelectedProjectPath)
133133
elseif !playMode[]
134134
JulGame.MainLoop.stop_game_in_editor(currentSceneMain)
135-
JulGame.change_scene(String(currentSceneName), true)
135+
JulGame.change_scene(String(currentSceneName))
136136
end
137137
end
138138
# show_game_window(currentSceneMain, sceneTexture, scrolling, zoom_level, duplicationMode)
@@ -320,7 +320,7 @@ module Editor
320320

321321
SDL2.SDL_SetRenderTarget(renderer, sceneTexture)
322322
SDL2.SDL_RenderClear(renderer)
323-
gameInfo = currentSceneMain === nothing ? [] : JulGame.MainLoop.game_loop(currentSceneMain, startTime, lastPhysicsTime, 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
323+
gameInfo = currentSceneMain === nothing ? [] : JulGame.MainLoop.game_loop(currentSceneMain, startTime, lastPhysicsTime, 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
324324
SDL2.SDL_SetRenderTarget(renderer, C_NULL)
325325
SDL2.SDL_RenderClear(renderer)
326326

src/editor/JulGameEditor/Utils/SceneUtils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ The loaded main struct.
8181
function load_scene(scenePath::String, renderer)
8282
game = C_NULL
8383
try
84-
game = SceneLoaderModule.load_scene_from_editor(scenePath, renderer, true);
84+
game = SceneLoaderModule.load_scene_from_editor(scenePath, renderer);
8585
catch e
8686
rethrow(e)
8787
end

src/engine/SceneManagement/SceneBuilder.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ module SceneBuilderModule
4040
end
4141

4242
function __init__()
43+
# if not using PackageCompiler, then we need to call init() here
4344
if ccall(:jl_generating_output, Cint, ()) != 1
4445
init()
4546
end
4647
end
4748

49+
# if using PackageCompiler, then we need to call init here
4850
if ccall(:jl_generating_output, Cint, ()) == 1
4951
init()
5052
end
@@ -64,7 +66,7 @@ module SceneBuilderModule
6466
end
6567
end
6668

67-
function load_and_prepare_scene(this::Scene, windowName::String = "Game", isUsingEditor = false, size::Vector2 = Vector2(800, 800), camSize::Vector2 = Vector2(800,800), isResizable::Bool = true, zoom::Float64 = 1.0, autoScaleZoom::Bool = true, targetFrameRate = 60.0, globals = []; isNewEditor = false)
69+
function load_and_prepare_scene(this::Scene, windowName::String = "Game", size::Vector2 = Vector2(800, 800), camSize::Vector2 = Vector2(800,800), isResizable::Bool = true, zoom::Float64 = 1.0, autoScaleZoom::Bool = true, targetFrameRate = 60.0, globals = [])
6870
#file loading
6971
if autoScaleZoom
7072
zoom = 1.0
@@ -91,17 +93,17 @@ module SceneBuilderModule
9193
MAIN.scene.camera = CameraModule.Camera(camSize, Vector2f(),Vector2f(), C_NULL)
9294

9395
flags = SDL2.SDL_RENDERER_ACCELERATED |
94-
(isUsingEditor ? (SDL2.SDL_WINDOW_POPUP_MENU | SDL2.SDL_WINDOW_ALWAYS_ON_TOP | SDL2.SDL_WINDOW_BORDERLESS) : 0) |
95-
(isResizable || isUsingEditor ? SDL2.SDL_WINDOW_RESIZABLE : 0) |
96+
(JulGame.IS_EDITOR ? (SDL2.SDL_WINDOW_POPUP_MENU | SDL2.SDL_WINDOW_ALWAYS_ON_TOP | SDL2.SDL_WINDOW_BORDERLESS) : 0) |
97+
(isResizable || JulGame.IS_EDITOR ? SDL2.SDL_WINDOW_RESIZABLE : 0) |
9698
(size == Math.Vector2() ? SDL2.SDL_WINDOW_FULLSCREEN_DESKTOP : 0)
9799

98100
MAIN.screenSize = size != C_NULL ? size : MAIN.scene.camera.size
99-
if !isUsingEditor
101+
if !JulGame.IS_EDITOR
100102
MAIN.window = SDL2.SDL_CreateWindow(MAIN.windowName, SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, MAIN.screenSize.x, MAIN.screenSize.y, flags)
101103
JulGame.Renderer::Ptr{SDL2.SDL_Renderer} = SDL2.SDL_CreateRenderer(MAIN.window, -1, SDL2.SDL_RENDERER_ACCELERATED)
102104
end
103105

104-
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene), isUsingEditor)
106+
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))
105107
MAIN.scene.entities = scene[1]
106108
MAIN.scene.uiElements = scene[2]
107109

@@ -121,7 +123,7 @@ module SceneBuilderModule
121123
push!(MAIN.scene.colliders, entity.collider)
122124
end
123125

124-
if !isUsingEditor
126+
if !JulGame.IS_EDITOR
125127
scriptCounter = 1
126128
for script in entity.scripts
127129
params = []
@@ -159,11 +161,11 @@ module SceneBuilderModule
159161
end
160162

161163
MAIN.assets = joinpath(BasePath, "assets")
162-
JulGame.MainLoop.prepare_window_scripts_and_start_loop(isUsingEditor, size, isResizable, autoScaleZoom, isNewEditor)
164+
JulGame.MainLoop.prepare_window_scripts_and_start_loop(size, isResizable, autoScaleZoom)
163165
end
164166

165-
function deserialize_and_build_scene(this::Scene, isUsingEditor::Bool = false)
166-
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene), isUsingEditor)
167+
function deserialize_and_build_scene(this::Scene)
168+
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))
167169

168170
# println("Changing scene to $this.scene")
169171
# println("Entities in main scene: ", length(MAIN.scene.entities))
@@ -192,7 +194,7 @@ module SceneBuilderModule
192194
push!(MAIN.scene.colliders, entity.collider)
193195
end
194196

195-
if !isUsingEditor
197+
if !JulGame.IS_EDITOR
196198
scriptCounter = 1
197199
for script in entity.scripts
198200
params = []

0 commit comments

Comments
 (0)