public void Dispose() { SDL.DestroyRenderer(this.IntPtr); if (TextureManager != null) { var queue = new Queue <IntPtr>(TextureManager.Values.Select(x => x.IntPtr)); while (queue.Any()) { SDL.DestroyTexture(queue.Dequeue()); } } }
/// <summary> /// Sets up a window and enters the gameloop. (Code after this call won't run until the game has exited.) /// </summary> public static void Run(float speed = 100f) { SDL.Init(SDL.InitFlags.EVERYTHING); IMG.Init(IMG.InitFlags.EVERYTHING); Mix.Init(Mix.InitFlags.EVERYTHING); TTF.Init(); //open window SDL.SetHint(SDL.HINT_RENDER_VSYNC, "1"); SDL.SetHint(SDL.HINT_RENDER_SCALE_QUALITY, "2"); WindowHandle = SDL.CreateWindow("HatlessEngine", SDL.WINDOWPOS_UNDEFINED, SDL.WINDOWPOS_UNDEFINED, 800, 600, SDL.WindowFlags.WINDOW_SHOWN | SDL.WindowFlags.WINDOW_RESIZABLE | SDL.WindowFlags.WINDOW_INPUT_FOCUS); RendererHandle = SDL.CreateRenderer(WindowHandle, -1, (uint)(SDL.RendererFlags.RENDERER_ACCELERATED | SDL.RendererFlags.RENDERER_PRESENTVSYNC)); SDL.SetRenderDrawBlendMode(RendererHandle, SDL.BlendMode.BLENDMODE_BLEND); Window.SetIcon(); //add default view that spans the current window new View("default", new Rectangle(Point.Zero, Window.Size), new Rectangle(Point.Zero, new Point(1f, 1f))); //initialize audio system and let Resources handle sound expiration Mix.OpenAudio(44100, SDL.AUDIO_S16SYS, 2, 4096); Mix.AllocateChannels((int)(speed * 2)); //might want to dynamically create and remove channels during runtime Mix.ChannelFinished(new Mix.ChannelFinishedDelegate(Resources.SoundChannelFinished)); Mix.HookMusicFinished(new Mix.MusicFinishedDelegate(Resources.MusicFinished)); //initialize loop StepsPerSecond = speed; Running = true; if (Started != null) { Started(null, EventArgs.Empty); } Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); //do a step before the first draw can occur Step(); long lastStepTick = 0; long lastDrawTick = 0; long lastStepTime = 0; long lastDrawTime = 0; while (Running) { //perform step when needed if (stopWatch.ElapsedTicks >= lastStepTick + TicksPerStep) { if (CatchUpSteps) { lastStepTick = lastStepTick + TicksPerStep; } else { lastStepTick = stopWatch.ElapsedTicks; } Step(); _ActualSPS = (int)(Stopwatch.Frequency / (stopWatch.ElapsedTicks - lastStepTime)); lastStepTime = stopWatch.ElapsedTicks; } //perform draw when ready for a new one if (!RenderframeReady && Running && stopWatch.ElapsedTicks >= lastDrawTick + TicksPerDraw) { lastDrawTick = lastDrawTick + TicksPerDraw; Draw(); _ActualFPS = (int)(Stopwatch.Frequency / (stopWatch.ElapsedTicks - lastDrawTime)); lastDrawTime = stopWatch.ElapsedTicks; } } //cleanup and uninitialization Resources.UnloadAllExternalResources(); Log.CloseAllStreams(); Input.CloseGamepads(); SDL.DestroyWindow(WindowHandle); WindowHandle = IntPtr.Zero; SDL.DestroyRenderer(RendererHandle); RendererHandle = IntPtr.Zero; Mix.CloseAudio(); SDL.Quit(); IMG.Quit(); Mix.Quit(); TTF.Quit(); }