Пример #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();
        }
Пример #2
0
 private void DrawTraffic(SDL2Renderer renderer)
 {
     foreach (var lane in TrafficLanes)
     {
         // lanes are red
         renderer.SetRenderDrawColor(255, 0, 0, 255);
         renderer.RenderDrawLine(lane.Origin.x, lane.Origin.y, lane.EndPoint.x, lane.EndPoint.y);
     }
 }
Пример #3
0
        public void DoRender(double interpolation, SDL2Renderer renderer)
        {
            if (SignaledState == SignalState.None)
                renderer.SetRenderDrawColor(255, 255, 255, 255);
            else if (SignaledState == SignalState.ReceivedDeciding)
                renderer.SetRenderDrawColor(255, 255, 0, 255);
            else if (SignaledState == SignalState.ReceivedTransmitting)
                renderer.SetRenderDrawColor(255, 0, 0, 255);
            else if (SignaledState == SignalState.ReceivedNotTransmitting)
                renderer.SetRenderDrawColor(255, 127, 0, 255);

            int lerpX = (int)(Position.x + (Velocity.x * interpolation));
            int lerpY = (int)(Position.y + (Velocity.y * interpolation));

            //renderer.RenderDrawPoint(lerpX, lerpY);
            renderer.RenderFillRect(new SDL.SDL_Rect { x = lerpX - 2, y = lerpY - 2, w = 5, h = 5 });
        }
Пример #4
0
        private void DrawEditingPoints(SDL2Renderer renderer)
        {
            // draw points
            if (FirstPoint != null)
            {
                // green
                renderer.SetRenderDrawColor(0, 255, 0, 255);
                renderer.RenderDrawPoint(FirstPoint.Item1, FirstPoint.Item2);
            }

            if (SecondPoint != null)
            {
                renderer.SetRenderDrawColor(0, 255, 0, 255);
                renderer.RenderDrawPoint(SecondPoint.Item1, SecondPoint.Item2);
            }
        }
Пример #5
0
 private void DrawSquares(SDL2Renderer renderer)
 {
     // draw squares
     foreach (SDL.SDL_Rect rect in Squares)
     {
         // squares are blue
         renderer.SetRenderDrawColor(0, 0, 255, 255);
         renderer.RenderDrawRect(rect);
     }
 }
Пример #6
0
        private void DrawCrosshairs(SDL2Renderer renderer)
        {
            int mouseX;
            int mouseY;

            // color based on mode
            if (CurrentMode == EditorMode.None)
                // white
                renderer.SetRenderDrawColor(255, 255, 255, 255);
            else
                // red
                renderer.SetRenderDrawColor(255, 0, 0, 255);

            // draw the crosshair
            Util.GetVirtualMouseCoordinates(out mouseX, out mouseY);
            // horizontal line
            renderer.RenderDrawLine(0, mouseY, Program.RenderLogicalWidth - 1, mouseY);
            // vertical line
            renderer.RenderDrawLine(mouseX, 0, mouseX, Program.RenderLogicalHeight - 1);
            // lane deletion region
            renderer.RenderDrawRect(new SDL.SDL_Rect { x = mouseX - 5, y = mouseY - 5, w = 11, h = 11 });
        }
Пример #7
0
 public void DoRender(double interpolation, SDL2Renderer renderer)
 {
     DrawSquares(renderer);
     DrawEditingPoints(renderer);
     DrawTraffic(renderer);
     DrawCrosshairs(renderer);
 }
Пример #8
0
 private void DrawVehicles(double interpolation, SDL2Renderer renderer)
 {
     foreach (Vehicle vehicle in Vehicles)
     {
         vehicle.DoRender(interpolation, renderer);
     }
 }
Пример #9
0
 private void DrawSignal(SDL2Renderer renderer)
 {
     if (EmergencyVehicle != null)
     {
         renderer.SetRenderDrawColor(0, 255, 0, 255);
         renderer.RenderDrawPoint((int)EmergencyVehicle.Position.x, (int)EmergencyVehicle.Position.y);
         SDL.SDL_Point[] signalRadius = Util.GenerateCircularPolygon(16, EmergencyVehicle.LastSignal.EmergencyRadius);
         Util.TranslatePoints(signalRadius, (int)EmergencyVehicle.Position.x, (int)EmergencyVehicle.Position.y);
         renderer.RenderDrawLines(signalRadius);
     }
 }
Пример #10
0
 public void DoRender(double interpolation, SDL2Renderer renderer)
 {
     DrawSquares(renderer);
     DrawSignal(renderer);
     DrawVehicles(interpolation, renderer);
 }