Пример #1
0
        private static void Main(string[] args)
        {
            // initialize SDL video
            if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) != 0)
            {
                SDL.SDL_ShowSimpleMessageBox(SDL.SDL_MessageBoxFlags.SDL_MESSAGEBOX_ERROR, "SDL_Init()", SDL.SDL_GetError(), IntPtr.Zero);
                return;
            }

            IsRunning = true;

            // build SDL window and renderer
            SDL2Window window = new SDL2Window("Demo", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, 640, 480, SDL.SDL_WindowFlags.SDL_WINDOW_RESIZABLE);
            SDL2Renderer renderer = new SDL2Renderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);

            // set logical resolution for automatic scaling (letterboxing at larger resolutions)
            RenderLogicalWidth = 640;
            RenderLogicalHeight = 480;
            renderer.RenderSetLogicalSize(640, 480);

            uint currentTic = 0;
            uint currentFrame = 0;
            uint previousTime = SDL.SDL_GetTicks();
            uint lag = 0;

            // initialize demo state machine into "edit" mode
            // i.e. "place obstacles and traffic lanes" mode
            StateMachine stateMachine = new StateMachine();
            EditState editState = new EditState(stateMachine);
            stateMachine.PushState(editState);

            // demo time!
            while (IsRunning)
            {
                // Uh, won't this overflow eventually?
                uint currentTime = SDL.SDL_GetTicks();
                uint elapsedTime = currentTime - previousTime;
                previousTime = currentTime;
                lag += elapsedTime;

                // input
                stateMachine.DoEvents();

                // fixed logic rate
                while (lag >= MillisecondsPerTic)
                {
                    // logic
                    stateMachine.DoLogic(currentTic);

                    lag -= MillisecondsPerTic;
                    currentTic++;
                }

                // render
                renderer.SetRenderDrawColor(0, 0, 0, 255);
                renderer.RenderClear();
                // recalculate ms per tic as a double value
                stateMachine.DoRender(lag / (1000.0 / TicsPerSecond), renderer);
                renderer.RenderPresent();
                currentFrame++;
            }

            // dipose SDL resources
            window.Dispose();
            renderer.Dispose();

            // uninitialize SDL
            SDL.SDL_Quit();
        }