A simple .NET Framework to make 2D games quick and easy.

Scenes based on type not string id

+1 -1
src/Debug/ConsoleScene.cs
··· 14 14 float scrollYOffset = 0; 15 15 bool pressedScroll = false; 16 16 17 - public ConsoleScene(int width, int height, string id) : base(width, height, id) 17 + public ConsoleScene(int width, int height) : base(width, height) 18 18 { 19 19 ClearColor = UiStyles.Background; 20 20 UpdateOnlyIfActive = true;
+6 -8
src/Debug/Debug.cs
··· 79 79 80 80 public static void Initialize() 81 81 { 82 - SceneHandler.Register(new InspectorScene((int)(Game.Window.Width * 0.201), 1080, "Inspector") 82 + SceneHandler.Register(new InspectorScene((int)(Game.Window.Width * 0.201), 1080) 83 83 .SetAllowWindowResize(false) 84 84 .SetAlwaysRebuildTexture(true) 85 85 .SetRelativeWindowSize(0.8f, 0f, 1.0001f, 1f)); 86 86 87 - SceneHandler.Register(new ConsoleScene((int)(Game.Window.Width * 0.2), (int)(Game.Window.Height * 0.4), "Console") 87 + SceneHandler.Register(new ConsoleScene((int)(Game.Window.Width * 0.2), (int)(Game.Window.Height * 0.4)) 88 88 .SetAllowWindowResize(true) 89 89 .SetRelativeWindowSize(0.1f, 0.1f, 0.4f, 0.6f) 90 90 .SetAlwaysRebuildTexture(true)); 91 91 92 - SceneHandler.Register(new PerformanceScene((int)(Game.Window.Width * 0.2), (int)(Game.Window.Height * 0.4), "Performance") 92 + SceneHandler.Register(new PerformanceScene((int)(Game.Window.Width * 0.2), (int)(Game.Window.Height * 0.4)) 93 93 .SetRelativeWindowSize(0f, 0.89f, 0.10f, 1.001f) 94 94 .SetAlwaysRebuildTexture(true)); 95 95 ··· 106 106 { 107 107 if (args.Length < 1) 108 108 { 109 - Debug.Log(SceneHandler.IsLoaded("Performance")); 109 + Debug.Log(SceneHandler.IsLoaded<PerformanceScene>()); 110 110 return; 111 111 } 112 112 ··· 118 118 119 119 if ((bool)args[0]) 120 120 { 121 - SceneHandler.Load("Performance"); 121 + SceneHandler.Load<PerformanceScene>(); 122 122 } 123 123 else 124 124 { 125 - SceneHandler.Unload("Performance"); 125 + SceneHandler.Unload<PerformanceScene>(); 126 126 } 127 127 }); 128 128 ··· 196 196 lineNum++; 197 197 } 198 198 } 199 - 200 - // Console.WriteLine(message); 201 199 202 200 StackTrace stackTrace = new StackTrace(); 203 201 StackFrame? stackFrame = stackTrace.GetFrame(1);
+1 -1
src/Debug/InspectorScene.cs
··· 12 12 float yOffset = 0; 13 13 public string SelectedScene = ""; 14 14 15 - public InspectorScene(int width, int height, string id) : base(width, height, id) 15 + public InspectorScene(int width, int height) : base(width, height) 16 16 { 17 17 ClearColor = UiStyles.Background; 18 18 // SetUpdateOnlyIfActive(true);
+1 -1
src/Debug/OldInspectorScene.cs
··· 11 11 { 12 12 float yOffset = 0; 13 13 14 - public OldInspectorScene(int width, int height, string id) : base(width, height, id) 14 + public OldInspectorScene(int width, int height) : base(width, height) 15 15 { 16 16 ClearColor = UiStyles.Background; 17 17 UpdateOnlyIfActive = true;
+1 -1
src/Debug/PerformanceScene.cs
··· 26 26 27 27 bool LastSize = false; 28 28 29 - public PerformanceScene(int width, int height, string id) : base(width, height, id) 29 + public PerformanceScene(int width, int height) : base(width, height) 30 30 { 31 31 } 32 32
+10 -10
src/GameManager.cs
··· 162 162 SDL_SetRenderDrawColor(SDLRenderer, 0, 0, 0, 255); 163 163 SDL_RenderClear(SDLRenderer); 164 164 165 - foreach (string id in SceneHandler.LoadedScenes) 165 + foreach (string id in new List<string>(SceneHandler.LoadedScenes)) 166 166 { 167 167 try { 168 168 SceneHandler.Scenes[id].UpdateCall(); ··· 180 180 181 181 if (GlobalKeyboard.Pressed(Key.D, Mod.LShift, Mod.LCtrl)) 182 182 { 183 - if (!SceneHandler.IsLoaded("Inspector")) 184 - SceneHandler.Load("Inspector"); 183 + if (!SceneHandler.IsLoaded<InspectorScene>()) 184 + SceneHandler.Load<InspectorScene>(); 185 185 else 186 - SceneHandler.Unload("Inspector"); 186 + SceneHandler.Unload<InspectorScene>(); 187 187 } 188 188 189 189 if (GlobalKeyboard.Pressed(Key.C, Mod.LShift, Mod.LCtrl)) 190 190 { 191 - if (!SceneHandler.IsLoaded("Console")) { 192 - SceneHandler.Load("Console"); 191 + if (!SceneHandler.IsLoaded<ConsoleScene>()) { 192 + SceneHandler.Load<ConsoleScene>(); 193 193 } else 194 - SceneHandler.Unload("Console"); 194 + SceneHandler.Unload<ConsoleScene>(); 195 195 } 196 196 197 197 if (GlobalKeyboard.Pressed(Key.F, Mod.LShift, Mod.LCtrl)) 198 198 { 199 - if (!SceneHandler.IsLoaded("Performance")) 199 + if (!SceneHandler.IsLoaded<PerformanceScene>()) 200 200 { 201 - SceneHandler.Load("Performance"); 201 + SceneHandler.Load<PerformanceScene>(); 202 202 } 203 203 else 204 204 { ··· 217 217 else 218 218 scene.SetRelativeWindowSize(scene.RelativeWindowSize.x + 0.14f, scene.RelativeWindowSize.y, scene.RelativeWindowSize.w, scene.RelativeWindowSize.h); 219 219 scene.Size = false; 220 - SceneHandler.Unload("Performance"); 220 + SceneHandler.Unload<PerformanceScene>(); 221 221 } 222 222 223 223 }
+5 -10
src/Scenes/Scene.cs
··· 8 8 9 9 public abstract class Scene : ICloneable 10 10 { 11 - public string SceneID { get => SceneID; set => throw new Exception("Can't set SceneID"); } 11 + public string SceneID { get; internal set; } 12 12 13 13 public bool AllowWindowResize { get; set; } = false; 14 14 public bool AlwaysRebuildTexture { get; set; } = false; ··· 81 81 }; 82 82 } 83 83 84 - public string GetSceneID() 85 - { 86 - return SceneID; 87 - } 88 - 89 84 public void ApplyOriginalAspectRatio() 90 85 { 91 86 float ratio = OriginalWindowSize.X / OriginalWindowSize.Y; ··· 95 90 RelativeWindowSize.h = newSize.Y / Game.Window.Height; 96 91 } 97 92 98 - protected Scene(int width, int height, string id) 93 + protected Scene(int width, int height) 99 94 { 100 95 OriginalWindowSize.X = width; 101 96 OriginalWindowSize.Y = height; 102 97 WindowSize.X = width; 103 98 WindowSize.Y = height; 104 99 105 - SceneID = id; 100 + SceneID = this.GetType().Name; 106 101 107 102 RenderTarget = SDL_CreateTexture(Game.SDLRenderer, SDL_PIXELFORMAT_RGBA8888, 108 103 (int)SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, width, height); 109 104 SDL_SetTextureBlendMode(RenderTarget, SDL_BlendMode.SDL_BLENDMODE_BLEND); 110 105 111 - Keyboard = new(id); 112 - Mouse = new(id); 106 + Keyboard = new(SceneID); 107 + Mouse = new(SceneID); 113 108 Camera = new(this); 114 109 } 115 110
+30 -13
src/Scenes/SceneHandler.cs
··· 7 7 internal static Dictionary<string, Scene> Scenes = new (); 8 8 internal static Dictionary<string, Scene> OriginalScenes = new (); 9 9 10 - private static List<string> _loadedScenes = new(); 11 - 12 - internal static List<string> LoadedScenes { 13 - get => new List<string>(_loadedScenes); 14 - set => _loadedScenes = value; 15 - } 10 + public static List<string> LoadedScenes = new(); 16 11 17 12 public static void Initialize() { 18 13 Debug.RegisterCommand("scene_unload", (args) => { ··· 124 119 125 120 public static void Register(Scene scene) 126 121 { 127 - Scenes.Add(scene.GetSceneID(), (Scene)scene.Clone()); 128 - OriginalScenes.Add(scene.GetSceneID(), (Scene)scene.Clone()); 122 + Scenes.Add(scene.SceneID, (Scene)scene.Clone()); 123 + OriginalScenes.Add(scene.SceneID, (Scene)scene.Clone()); 129 124 } 130 125 131 - public static void Load(string id) 126 + internal static void Load(string id) 132 127 { 133 128 if (!LoadedScenes.Contains(id)) 134 129 { 135 130 LoadedScenes.Add(id); 136 131 Scenes[id].AwakeCall(); 132 + } else { 133 + Debug.Log($"Scene \"{id}\" doesnt exist"); 137 134 } 138 135 } 139 136 140 - public static void Unload(string id) 137 + internal static void Unload(string id) 141 138 { 142 139 if (LoadedScenes.Contains(id)) 143 140 { ··· 147 144 FUI.selectedTextField = null; 148 145 } 149 146 150 - public static void Remake(string id) 147 + internal static void Remake(string id) 151 148 { 152 149 Scenes[id] = (Scene)OriginalScenes[id].Clone(); 153 150 Scenes[id].Entities.Clear(); 154 151 Scenes[id].Awake(); 155 152 } 156 153 154 + public static void Load<T>() 155 + { 156 + Load(typeof(T).Name); 157 + } 158 + 159 + public static void Unload<T>() 160 + { 161 + Unload(typeof(T).Name); 162 + } 163 + 164 + public static void Remake<T>() 165 + { 166 + Remake(typeof(T).Name); 167 + } 168 + 157 169 public static T Get<T>() 158 170 { 159 171 var scene = Scenes.Values.ToList().Find((val) => val.GetType() == typeof(T)); ··· 175 187 } 176 188 } 177 189 178 - public static Scene Get(string id) 190 + internal static Scene Get(string id) 179 191 { 180 192 return Scenes[id]; 181 193 } 182 194 183 - public static bool IsLoaded(string id) 195 + internal static bool IsLoaded(string id) 184 196 { 185 197 return LoadedScenes.Contains(id); 198 + } 199 + 200 + public static bool IsLoaded<T>() 201 + { 202 + return IsLoaded(typeof(T).Name); 186 203 } 187 204 }