public SdlRenderer() { SDL.Init(InitFlags.Video); _window = SDL.CreateWindow(".NET Core SDL2-CS Tutorial", SDL.WINDOWPOS_CENTERED, SDL.WINDOWPOS_CENTERED, 1024, 768, WindowFlags.Resizable); _renderer = SDL.CreateRenderer(_window, -1, RendererFlags.Accelerated); this.blockSize = 20; }
public Window(string title, int left, int top, int width, int height, SDL.SDL_WindowFlags flags, bool createRenderer = false) { this.Title = title; this.Left = left; this.Top = top; this.Width = width; this.Height = height; this.Flags = flags; this.IntPtr = SDL.CreateWindow(title, left, top, width, height, flags); if (createRenderer) { //IntPtr windowIntPtr;// = new IntPtr(); //IntPtr rendrerIntPtr;// = new IntPtr(); //var rv=SDL.CreateWindowAndRenderer(width, height, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN, out windowIntPtr, out rendrerIntPtr); //if (Convert.ToBoolean(windowIntPtr.ToInt64()) && Convert.ToBoolean(rendrerIntPtr.ToInt64())) //{ //this.Renderer = new Renderer() { IntPtr = rendrerIntPtr };// this.Renderer = (Renderer)SDL.CreateRenderer(this.IntPtr, 0, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED); //} } }
/// <summary> /// Creates a new hardware-accelerated 2D renderer. /// </summary> /// <param name="window">The window to attach this rendering context to</param> public Renderer(Window window) { RendererPtr = SDL.CreateRenderer(window.WindowPtr, -1, SDL_RENDERER_ACCELERATED); ThrowIfFailed(RendererPtr); //SDL.SetHint(HintRenderScaleQuality, ((int)ScaleQuality.Nearest).ToString()); }
/// <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(); }
public Renderer(Window window, int index, SDL.SDL_RendererFlags renderFlags) { this.Window = window; this.IntPtr = SDL.CreateRenderer((IntPtr)window, index, renderFlags); }