public static int Main() { //The window we'll be rendering to IntPtr window = IntPtr.Zero; //The surface contained by the window IntPtr screenSurface; //Initialize SDL if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) < 0) { Console.WriteLine("SDL could not initialize! SDL_Error: {0}\n", SDL.SDL_GetError()); } else { //Create window window = SDL.SDL_CreateWindow("SDL Tutorial", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN); if (window == IntPtr.Zero) { Console.WriteLine("Window could not be created! SDL_Error: {0}\n", SDL.SDL_GetError()); } else { //Get window surface screenSurface = SDL.SDL_GetWindowSurface(window); //Fill the surface white var sur = Marshal.PtrToStructure<SDL.SDL_Surface>(screenSurface); var rect = new SDL.SDL_Rect(); SDL.SDL_FillRect(screenSurface, ref rect, SDL.SDL_MapRGB(sur.format, 0xFF, 0xFF, 0xFF)); //Update the surface SDL.SDL_UpdateWindowSurface(window); //Wait two seconds SDL.SDL_Delay(2000); } } //Destroy window SDL.SDL_DestroyWindow(window); //Quit SDL subsystems SDL.SDL_Quit(); return 0; }
internal void RenderTexture(IntPtr textureHandle, float positionX, float positionY, Rectangle source) { Debug.Assert(textureHandle != IntPtr.Zero, Errors.E_TEXTURE_NULL); int width = source.Width; int height = source.Height; // SDL only accepts integer positions (x,y) in the rendering Rect SDL.SDL_Rect destinationRectangle = new SDL.SDL_Rect() { x = (int)positionX, y = (int)positionY, w = width, h = height }; SDL.SDL_Rect sourceRectangle = new SDL.SDL_Rect() { x = source.X, y = source.Y, w = width, h = height }; int result = SDL.SDL_RenderCopy(Handle, textureHandle, ref sourceRectangle, ref destinationRectangle); if (Utilities.IsError(result)) { throw new Exception(Utilities.GetErrorMessage("SDL_RenderCopy")); } }
internal void RenderTexture(IntPtr textureHandle, float positionX, float positionY, int sourceWidth, int sourceHeight, double angle, Vector center) { Debug.Assert(textureHandle != IntPtr.Zero, Errors.E_TEXTURE_NULL); // SDL only accepts integer positions (x,y) in the rendering Rect SDL.SDL_Rect destinationRectangle = new SDL.SDL_Rect() { x = (int)positionX, y = (int)positionY, w = sourceWidth, h = sourceHeight }; SDL.SDL_Rect sourceRectangle = new SDL.SDL_Rect() { x = 0, y = 0, w = sourceWidth, h = sourceHeight }; SDL.SDL_Point centerPoint = new SDL.SDL_Point() { x = (int)center.X, y = (int)center.Y }; int result = SDL.SDL_RenderCopyEx(Handle, textureHandle, ref sourceRectangle, ref destinationRectangle, angle, ref centerPoint, SDL.SDL_RendererFlip.SDL_FLIP_NONE); if (Utilities.IsError(result)) { throw new Exception(Utilities.GetErrorMessage("SDL_RenderCopyEx")); } }
internal override void TextureDataFromStream( Stream stream, out int width, out int height, out byte[] pixels, int reqWidth = -1, int reqHeight = -1, bool zoom = false ) { // Load the Stream into an SDL_RWops* byte[] mem = new byte[stream.Length]; GCHandle handle = GCHandle.Alloc(mem, GCHandleType.Pinned); stream.Read(mem, 0, mem.Length); IntPtr rwops = SDL.SDL_RWFromMem(mem, mem.Length); // Load the SDL_Surface* from RWops, get the image data IntPtr surface = SDL_image.IMG_Load_RW(rwops, 1); handle.Free(); if (surface == IntPtr.Zero) { // File not found, supported, etc. width = 0; height = 0; pixels = null; return; } surface = INTERNAL_convertSurfaceFormat(surface); // Image scaling, if applicable if (reqWidth != -1 && reqHeight != -1) { // Get the file surface dimensions now... int rw; int rh; unsafe { SDL_Surface* surPtr = (SDL_Surface*) surface; rw = surPtr->w; rh = surPtr->h; } // Calculate the image scale factor bool scaleWidth; if (zoom) { scaleWidth = rw < rh; } else { scaleWidth = rw > rh; } float scale; if (scaleWidth) { scale = reqWidth / (float) rw; } else { scale = reqHeight / (float) rh; } // Calculate the scaled image size, crop if zoomed int resultWidth; int resultHeight; SDL.SDL_Rect crop = new SDL.SDL_Rect(); if (zoom) { resultWidth = reqWidth; resultHeight = reqHeight; if (scaleWidth) { crop.x = 0; crop.w = rw; crop.y = (int) (rh / 2 - (reqHeight / scale) / 2); crop.h = (int) (reqHeight / scale); } else { crop.y = 0; crop.h = rh; crop.x = (int) (rw / 2 - (reqWidth / scale) / 2); crop.w = (int) (reqWidth / scale); } } else { resultWidth = (int) (rw * scale); resultHeight = (int) (rh * scale); } // Alloc surface, blit! IntPtr newSurface = SDL.SDL_CreateRGBSurface( 0, resultWidth, resultHeight, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 ); SDL.SDL_SetSurfaceBlendMode( surface, SDL.SDL_BlendMode.SDL_BLENDMODE_NONE ); if (zoom) { SDL.SDL_BlitScaled( surface, ref crop, newSurface, IntPtr.Zero ); } else { SDL.SDL_BlitScaled( surface, IntPtr.Zero, newSurface, IntPtr.Zero ); } SDL.SDL_FreeSurface(surface); surface = newSurface; } // Copy surface data to output managed byte array unsafe { SDL_Surface* surPtr = (SDL_Surface*) surface; width = surPtr->w; height = surPtr->h; pixels = new byte[width * height * 4]; // MUST be SurfaceFormat.Color! Marshal.Copy(surPtr->pixels, pixels, 0, pixels.Length); } SDL.SDL_FreeSurface(surface); /* Ensure that the alpha pixels are... well, actual alpha. * You think this looks stupid, but be assured: Your paint program is * almost certainly even stupider. * -flibit */ for (int i = 0; i < pixels.Length; i += 4) { if (pixels[i + 3] == 0) { pixels[i] = 0; pixels[i + 1] = 0; pixels[i + 2] = 0; } } }
public void Draw( SDLTexture texture, int x, int y ) { // Sanity check our inputs, make sure they are valid. if ( texture == null ) { throw new ArgumentNullException( "texture" ); } if ( x < 0 ) { throw new ArgumentOutOfRangeException( "x" ); } else if ( y < 0 ) { throw new ArgumentOutOfRangeException( "y" ); } // Set up the target rectangle, which is the area on the screen that this texture will // be drawn to. SDL.SDL_Rect target = new SDL.SDL_Rect(); target.x = x; target.y = y; target.w = texture.Width; target.h = texture.Height; // Set up the source rectangle, which is the area of the texture that will be drawn to // the screen. SDL.SDL_Rect source = new SDL.SDL_Rect(); source.x = 0; source.y = 0; source.w = texture.Width; source.h = texture.Height; // Now issue the draw command to SDL. SDL.SDL_RenderCopy( mRenderer, texture.TexturePtr, ref source, ref target ); }
public void DoLogic(uint currentTic) { if (CurrentMode == EditorMode.None) { FirstPoint = null; SecondPoint = null; } else if (CurrentMode == EditorMode.DrawSquare) { if (MouseClicked) { int mouseX; int mouseY; Util.GetVirtualMouseCoordinates(out mouseX, out mouseY); if (FirstPoint == null) { FirstPoint = new Tuple<int, int>(mouseX, mouseY); } else if (SecondPoint == null) { SecondPoint = new Tuple<int, int>(mouseX, mouseY); } if (FirstPoint != null && SecondPoint != null) { Squares.Add(new SDL.SDL_Rect() { x = FirstPoint.Item1, y = FirstPoint.Item2, w = Math.Abs(SecondPoint.Item1 - FirstPoint.Item1), h = Math.Abs(SecondPoint.Item2 - FirstPoint.Item2) }); FirstPoint = null; SecondPoint = null; CurrentMode = EditorMode.None; } } } else if (CurrentMode == EditorMode.DrawTraffic) { if (MouseClicked) { int mouseX; int mouseY; Util.GetVirtualMouseCoordinates(out mouseX, out mouseY); if (SelectedTrafficOrigin == TrafficLane.LaneDirection.TopToBottom) TrafficLanes.Add(new TrafficLane(new SDL.SDL_Point { x = mouseX, y = 0 }, new SDL.SDL_Point { x = mouseX, y = Program.RenderLogicalHeight - 1 }, SelectedTrafficOrigin)); else if (SelectedTrafficOrigin == TrafficLane.LaneDirection.BottomToTop) TrafficLanes.Add(new TrafficLane(new SDL.SDL_Point { x = mouseX, y = Program.RenderLogicalHeight - 1 }, new SDL.SDL_Point { x = mouseX, y = 0 }, SelectedTrafficOrigin)); else if (SelectedTrafficOrigin == TrafficLane.LaneDirection.RightToLeft) TrafficLanes.Add(new TrafficLane(new SDL.SDL_Point { x = Program.RenderLogicalWidth - 1, y = mouseY }, new SDL.SDL_Point { x = 0, y = mouseY }, SelectedTrafficOrigin)); else if (SelectedTrafficOrigin == TrafficLane.LaneDirection.LeftToRight) TrafficLanes.Add(new TrafficLane(new SDL.SDL_Point { x = 0, y = mouseY }, new SDL.SDL_Point { x = Program.RenderLogicalWidth - 1, y = mouseY }, SelectedTrafficOrigin)); SelectedTrafficOrigin = TrafficLane.LaneDirection.TopToBottom; CurrentMode = EditorMode.None; } } else if (CurrentMode == EditorMode.DeleteElement) { if (MouseClicked) { int mouseX; int mouseY; Util.GetVirtualMouseCoordinates(out mouseX, out mouseY); for (int index = 0; index < Squares.Count; index++) { SDL.SDL_Rect rect = Squares[index]; if (Util.PointOnRectangle(rect, new SDL.SDL_Point() { x = mouseX, y = mouseY })) { Squares.RemoveAt(index); break; } } for (int index = 0; index < TrafficLanes.Count; index++) { SDL.SDL_Rect searchRegion = new SDL.SDL_Rect { x = mouseX - 5, y = mouseY - 5, w = 11, h = 11 }; var lane = TrafficLanes[index]; if (Util.HasIntersection(searchRegion, lane.Origin.x, lane.Origin.y, lane.EndPoint.x, lane.EndPoint.y)) { TrafficLanes.RemoveAt(index); break; } } CurrentMode = EditorMode.None; } } MouseClicked = false; }