@@ -15,7 +15,7 @@ import (
1515 gdx "github.com/goplus/spx/v2/pkg/spx/pkg/engine"
1616)
1717
18- // copy these variable to any namespace you want
18+ // Shared engine managers.
1919var (
2020 platformMgr enginewrap.PlatformMgrImpl
2121 resMgr enginewrap.ResMgrImpl
@@ -40,12 +40,12 @@ type LayerSortInfo struct {
4040
4141var curLayerSortMode layerSortMode
4242
43- // SetLayerSortMode configures automatic layer sorting for sprites .
43+ // SetLayerSortMode sets sprite layer sorting.
4444// Supported modes:
45- // - "" or "none": Disables automatic sorting (default)
46- // - "vertical": Sorts by Y-coordinate (descending) , then X-coordinate ( descending)
45+ // - "" or "none": disable sorting (default)
46+ // - "vertical": sort by Y, then X, both descending
4747//
48- // When enabled, manual layer control methods are disabled to prevent conflicts .
48+ // When enabled, manual layer changes are disabled.
4949func SetLayerSortMode (s string ) error {
5050 switch s {
5151 case "" , "none" :
@@ -132,7 +132,7 @@ func OnGameStarted() {
132132 gco .OnInited ()
133133}
134134
135- // callbacks
135+ // Engine callbacks.
136136func onStart () {
137137 defer CheckPanic ()
138138 resetMouseButtonStates ()
@@ -204,13 +204,15 @@ func cacheTriggerEvents() {
204204 triggerMutex .Unlock ()
205205 triggerEventsTemp = triggerEventsTemp [:0 ]
206206}
207+
207208func GetTriggerEvents (lst []TriggerEvent ) []TriggerEvent {
208209 triggerMutex .Lock ()
209210 lst = append (lst , triggerEvents ... )
210211 triggerEvents = triggerEvents [:0 ]
211212 triggerMutex .Unlock ()
212213 return lst
213214}
215+
214216func cacheKeyEvents () {
215217 keyMutex .Lock ()
216218 keyEvents = append (keyEvents , keyEventsTemp ... )
@@ -226,36 +228,27 @@ func GetKeyEvents(lst []KeyEvent) []KeyEvent {
226228 return lst
227229}
228230
229- // DeferPanic is a generic panic handler that should be called with defer.
230- // It recovers from panics and handles them appropriately.
231- // Parameters:
232- // - name: optional identifier for the panic source (pass "" if not needed)
233- // - stack: optional stack trace (pass "" to auto-generate)
234- // - exitOnPanic: if true, calls RequestExit(1) after handling the panic
231+ // DeferPanic recovers a panic, reports it, and optionally exits.
235232func DeferPanic (name , stack string , exitOnPanic bool ) {
236233 if e := recover (); e != nil {
237234 handlePanic (name , stack , e , exitOnPanic )
238235 }
239236}
240237
241- // CheckPanic is a simplified defer panic handler for engine callbacks.
242- // It auto-generates stack trace and exits on panic.
243- // Usage: defer CheckPanic()
238+ // CheckPanic is a shorthand panic handler for engine callbacks.
244239func CheckPanic () {
245240 if e := recover (); e != nil {
246241 handlePanic ("" , "" , e , true )
247242 }
248243}
249244
250- // OnPanic handles a panic with the given name and stack trace.
251- // This is typically called from coroutine panic handlers.
245+ // OnPanic reports a panic with an optional name and stack.
252246func OnPanic (name , stack string ) {
253247 handlePanic (name , stack , nil , true )
254248}
255249
256- // handlePanic is the internal panic handler implementation .
250+ // handlePanic reports a panic and optionally exits .
257251func handlePanic (name , stack string , err any , exitOnPanic bool ) {
258- // Build panic message
259252 var msg string
260253 if err != nil {
261254 msg = fmt .Sprintf ("panic: %v" , err )
@@ -271,31 +264,27 @@ func handlePanic(name, stack string, err any, exitOnPanic bool) {
271264 msg += "\n stack:\n " + stack
272265 }
273266
274- // Report runtime panic to external manager
275267 extMgr .OnRuntimePanic (msg )
276268
277- // Exit if requested
278269 if exitOnPanic {
279270 RequestExit (1 )
280271 }
281272}
282273
283- // Panic triggers a panic with the given message and handles it through the engine's panic system.
284- // This function should be used instead of log.Panicln or panic() for consistent error handling.
285- // It reports the error to the external manager and then panics.
274+ // Panic reports a panic message through the engine.
286275func Panic (args ... any ) {
287276 msg := fmt .Sprint (args ... )
288277 OnPanic (msg , "" )
289278}
290279
291- // Panicf triggers a panic with a formatted message .
280+ // Panicf reports a formatted panic message through the engine .
292281func Panicf (format string , args ... any ) {
293282 msg := fmt .Sprintf (format , args ... )
294283 OnPanic (msg , "" )
295284}
296285
297- // abortCoroutinesAndReset aborts all running coroutines and resets the engine.
298- // This is primarily used on web platforms where we cannot truly exit the process .
286+ // abortCoroutinesAndReset aborts coroutines and resets the engine.
287+ // Used on web, where the process cannot exit.
299288func abortCoroutinesAndReset (exitCode int64 ) {
300289 completed := gco .AbortAllAndWait (2 * stime .Second )
301290 spxlog .Debug ("AbortAllAndWait completed: %v. Requesting engine reset." , completed )
@@ -304,7 +293,7 @@ func abortCoroutinesAndReset(exitCode int64) {
304293
305294func RequestExit (exitCode int64 ) {
306295 if platform .IsWeb () {
307- // On web platform, abort coroutines and request reset
296+ // Web resets instead of exiting.
308297 abortCoroutinesAndReset (exitCode )
309298 return
310299 }
0 commit comments