public void Run() { Threading.RegisterUIThread(); Init(); Load(); //Game loop running = true; #if DEBUGMAC ulong last = SDL2.SDL_GetPerformanceCounter(); ulong startTick = last; #else var timer = new Stopwatch(); timer.Start(); double last = 0.0; #endif double elapsed = 0.0; /* * DEBUGMAC Define: * Use SDL2_GetTicks (less accurate) instead of Stopwatch * Xamarin Studio Debugger crashes on Mac with Stopwatch * DEBUGMAC not required for other platforms */ while (running) { //SDL2 events ProcessEvents(); //Run code for UI thread Threading.Update(); //Run game code #if DEBUGMAC ulong currentTicks = SDL2.SDL_GetPerformanceCounter(); var current = (double)(currentTicks - startTick) / (double)SDL2.SDL_GetPerformanceFrequency(); var t = new GameTime(TimeSpan.FromSeconds(elapsed), TimeSpan.FromSeconds(current)); #else var t = new GameTime(TimeSpan.FromSeconds(elapsed), timer.Elapsed); #endif if (running) { Update(t); } if (running) { GL.glClearColor(0f, 0f, 0f, 1f); GL.glClear(GL.GL_COLOR_BUFFER_BIT); Draw(t); } //Time #if DEBUGMAC elapsed = (double)(currentTicks - last) / (double)SDL2.SDL_GetPerformanceFrequency(); last = currentTicks; #else elapsed = timer.Elapsed.TotalSeconds - last; last = timer.Elapsed.TotalSeconds; #endif if (elapsed < 0) { elapsed = 0; //apparently this can happen? Console.WriteLine("Stopwatch returned negative time"); } SDL2.SDL_GL_SwapWindow(Window.Handle); System.Threading.Thread.Sleep(0); } #if !DEBUGMAC timer.Stop(); #endif SDL2.SDL_Quit(); }
void Init() { //open SDL2 SDL2.Load(); //initialise SDL2 if (SDL2.SDL_Init(SDL2.SDL_INIT_VIDEO) != 0) { SDL2.SDL_ShowSimpleMessageBox( SDL2.SDL_MESSAGEBOX_ERROR, "Error", "SDL_Init failed, exiting.", IntPtr.Zero ); return; } //context attributes SDL2.SDL_GL_SetAttribute(SDL2.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL2.SDL_GL_SetAttribute(SDL2.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL2.SDL_GL_SetAttribute(SDL2.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL2.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE); //create window fullscreen = GraphicsSettings.Fullscreen; var flags = GraphicsSettings.Fullscreen ? SDL2.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP | SDL2.SDL_WindowFlags.SDL_WINDOW_OPENGL : SDL2.SDL_WindowFlags.SDL_WINDOW_OPENGL; var sdlWin = SDL2.SDL_CreateWindow( Window.Title, SDL2.SDL_WINDOWPOS_CENTERED, SDL2.SDL_WINDOWPOS_CENTERED, GraphicsSettings.RequestedWidth, GraphicsSettings.RequestedHeight, flags ); Window.Width = GraphicsSettings.RequestedWidth; Window.Height = GraphicsSettings.RequestedHeight; Mouse = new Mouse(); Keyboard = new Keyboard(); if (sdlWin == IntPtr.Zero) { SDL2.SDL_ShowSimpleMessageBox( SDL2.SDL_MESSAGEBOX_ERROR, "Error", "SDL_CreateWindow failed, exiting.", IntPtr.Zero ); return; } Window.Handle = sdlWin; //create gl context var glcontext = SDL2.SDL_GL_CreateContext(sdlWin); if (glcontext == IntPtr.Zero) { SDL2.SDL_ShowSimpleMessageBox( SDL2.SDL_MESSAGEBOX_ERROR, "Error", "Failed to get GL context, exiting.", IntPtr.Zero ); SDL2.SDL_Quit(); return; } //Load libraries GL.Load(); FT.Load(); //Base gl state GL.glEnable(GL.GL_BLEND); GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); }