// http://stackoverflow.com/questions/99353/how-to-test-if-a-line-segment-intersects-an-axis-aligned-rectange-in-2d public static bool HasIntersection(SDL.SDL_Rect rect, int x1, int y1, int x2, int y2) { Func<int, int, double> implicitLineEqn = new Func<int, int, double>((x, y) => { return ((y2 - y1) * x) + ((x1 - x2) * y) + ((x2 * y1) - (x1 * y2)) * 1.0; }); double topLeft = implicitLineEqn(rect.x, rect.y); double topRight = implicitLineEqn(rect.x + rect.w, rect.y); double bottomLeft = implicitLineEqn(rect.x, rect.y + rect.h); double bottomRight = implicitLineEqn(rect.x + rect.w, rect.y + rect.h); if ((topLeft < 0 && topRight < 0 && bottomLeft < 0 && bottomRight < 0) || (topLeft > 0 && topRight > 0 && bottomLeft > 0 && bottomRight > 0)) { return false; } if ((x1 > rect.x + rect.w) && (x2 > rect.x + rect.w)) { return false; } else if ((x1 < rect.x) && (x2 < rect.x)) { return false; } else if ((y1 < rect.y) && (y2 < rect.y)) { return false; } else if ((y1 > rect.y + rect.h) && (y2 > rect.y + rect.h)) { return false; } else { return true; } }
public SDL2Renderer(SDL2Window window, int index, SDL.SDL_RendererFlags flags) { Renderer = SDL.SDL_CreateRenderer(window.Window, index, (uint)flags); if (Renderer == IntPtr.Zero) { throw new Exception("SDL_CreateRenderer()", new Exception(SDL.SDL_GetError())); } }
public SDL2Window(string title, int x, int y, int w, int h, SDL.SDL_WindowFlags flags) { Window = SDL.SDL_CreateWindow(title, x, y, w, h, flags); if (Window == IntPtr.Zero) { throw new Exception("SDL_CreateWindow()", new Exception(SDL.SDL_GetError())); } }
public MouseWheelEventArgs(SDL.SDL_Event rawEvent) : base(rawEvent) { RawTimeStamp = rawEvent.wheel.timestamp; WindowID = rawEvent.wheel.windowID; MouseDeviceID = rawEvent.wheel.which; HorizontalScrollAmount = rawEvent.wheel.x; VerticalScrollAmount = rawEvent.wheel.y; }
public WindowEventArgs(SDL.SDL_Event rawEvent) : base(rawEvent) { SubEventType = (WindowEventType)rawEvent.window.windowEvent; Data1 = rawEvent.window.data1; Data2 = rawEvent.window.data2; RawTimeStamp = rawEvent.window.timestamp; WindowID = rawEvent.window.windowID; }
public KeyboardEventArgs(SDL.SDL_Event rawEvent) : base(rawEvent) { RawTimeStamp = rawEvent.key.timestamp; repeat = rawEvent.key.repeat; KeyInformation = new KeyInformation(rawEvent.key.keysym.scancode, rawEvent.key.keysym.sym, rawEvent.key.keysym.mod); State = (KeyState)rawEvent.key.state; WindowID = rawEvent.key.windowID; }
public MouseButtonEventArgs(SDL.SDL_Event rawEvent) : base(rawEvent) { RawTimeStamp = rawEvent.button.timestamp; WindowID = rawEvent.button.windowID; MouseDeviceID = rawEvent.button.which; MouseButton = (MouseButtonCode)rawEvent.button.button; State = (MouseButtonState)rawEvent.button.state; RelativeToWindowX = rawEvent.button.x; RelativeToWindowY = rawEvent.button.y; }
public Window( string title, Point2d<int> position, Size2d<int> size, SDL.SDL_WindowFlags flags) { window = SDL.SDL_CreateWindow( title, position.X, position.Y, size.Width, size.Height, flags); }
public TextInputEventArgs(SDL.SDL_Event rawEvent) : base(rawEvent) { RawTimeStamp = rawEvent.text.timestamp; WindowID = rawEvent.text.windowID; byte[] rawBytes = new byte[SDL2.SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE]; // we have an unsafe pointer to a char array from SDL, explicitly marshal this to a byte array of fixed size unsafe { Marshal.Copy((IntPtr)rawEvent.text.text, rawBytes, 0, SDL2.SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE); } // look for the index of the first null terminator int length = Array.IndexOf(rawBytes, (byte)0); // according to the SDL2 migration guide, TextInputEvent will have a UTF-8 encoded string Text = System.Text.Encoding.UTF8.GetString(rawBytes, 0, length); }
public MouseMotionEventArgs(SDL.SDL_Event rawEvent) : base(rawEvent) { RawTimeStamp = rawEvent.motion.timestamp; WindowID = rawEvent.motion.windowID; MouseDeviceID = rawEvent.motion.which; RelativeToWindowX = rawEvent.motion.x; RelativeToWindowY = rawEvent.motion.y; RelativeToLastMotionEventX = rawEvent.motion.xrel; RelativeToLastMotionEventY = rawEvent.motion.yrel; List<MouseButtonCode> buttonsPressed = new List<MouseButtonCode>(); if (SDL.SDL_BUTTON(rawEvent.motion.state) == SDL.SDL_BUTTON_LEFT) buttonsPressed.Add(MouseButtonCode.Left); if (SDL.SDL_BUTTON(rawEvent.motion.state) == SDL.SDL_BUTTON_MIDDLE) buttonsPressed.Add(MouseButtonCode.Middle); if (SDL.SDL_BUTTON(rawEvent.motion.state) == SDL.SDL_BUTTON_RIGHT) buttonsPressed.Add(MouseButtonCode.Right); if (SDL.SDL_BUTTON(rawEvent.motion.state) == SDL.SDL_BUTTON_X1) buttonsPressed.Add(MouseButtonCode.X1); if (SDL.SDL_BUTTON(rawEvent.motion.state) == SDL.SDL_BUTTON_X2) buttonsPressed.Add(MouseButtonCode.X2); MouseButtonsPressed = buttonsPressed; }
public static extern IntPtr TTF_RenderUTF8_Blended_Wrapped( IntPtr font, [In()] [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(LPUtf8StrMarshaler))] string text, SDL.SDL_Color fg, uint wrapped );
public static extern IntPtr TTF_RenderText_Blended( IntPtr font, [In()] [MarshalAs(UnmanagedType.LPStr)] string text, SDL.SDL_Color fg );
public static extern IntPtr TTF_RenderUNICODE_Shaded( IntPtr font, [In()] [MarshalAs(UnmanagedType.LPWStr)] string text, SDL.SDL_Color fg, SDL.SDL_Color bg );
public static string Mix_GetMusicDecoder(int index) { return(SDL.UTF8_ToManaged( INTERNAL_Mix_GetMusicDecoder(index) )); }
public static IntPtr Mix_LoadMUS(string file) { return(INTERNAL_Mix_LoadMUS(SDL.UTF8_ToNative(file))); }
public static string Mix_GetMusicTitle(IntPtr music) { return(SDL.UTF8_ToManaged( INTERNAL_Mix_GetMusicTitle(music) )); }
/// <summary> /// Process a single event and dispatch it to the right window. /// </summary> public static void ProcessEvent(SDL.SDL_Event e) { Window ctrl = null; // Code below is to extract the associated `Window' instance and to find out the window // with focus. In the future, we could even add events handled at the application level. switch (e.type) { case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.button.windowID)); break; case SDL.SDL_EventType.SDL_MOUSEMOTION: ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.motion.windowID)); break; case SDL.SDL_EventType.SDL_MOUSEWHEEL: ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.wheel.windowID)); break; case SDL.SDL_EventType.SDL_KEYDOWN: case SDL.SDL_EventType.SDL_KEYUP: ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.key.windowID)); break; case SDL.SDL_EventType.SDL_TEXTEDITING: ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.edit.windowID)); break; case SDL.SDL_EventType.SDL_TEXTINPUT: ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.text.windowID)); break; case SDL.SDL_EventType.SDL_WINDOWEVENT: { ctrl = WindowFromSdlHandle(SDL.SDL_GetWindowFromID(e.window.windowID)); switch (e.window.windowEvent) { case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: WindowWithFocus = ctrl; break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: WindowWithFocus = null; break; } break; } } ctrl?.ProcessEvent(e); }
public void RenderCopy(IntPtr texture, SDL.SDL_Rect srcrect, SDL.SDL_Rect dstrect) { if (SDL.SDL_RenderCopy(Renderer, texture, ref srcrect, ref dstrect) != 0) { throw new Exception("SDL_RenderCopy()", new Exception(SDL.SDL_GetError())); } }
public static extern IntPtr TTF_RenderUNICODE_Blended_Wrapped( IntPtr font, [In()] [MarshalAs(UnmanagedType.LPWStr)] string text, SDL.SDL_Color fg, uint wrapped );
public static void SDL_IMAGE_VERSION(out SDL.SDL_version X) { X.major = SDL_IMAGE_MAJOR_VERSION; X.minor = SDL_IMAGE_MINOR_VERSION; X.patch = SDL_IMAGE_PATCHLEVEL; }
public static string Mix_GetMusicCopyrightTag(IntPtr music) { return(SDL.UTF8_ToManaged( INTERNAL_Mix_GetMusicCopyrightTag(music) )); }
public static string Mix_GetTimidityCfg() { return(SDL.UTF8_ToManaged( INTERNAL_Mix_GetTimidityCfg() )); }
public static string Mix_GetSoundFonts() { return(SDL.UTF8_ToManaged( INTERNAL_Mix_GetSoundFonts() )); }
public static int Mix_SetSoundFonts(string paths) { return(INTERNAL_Mix_SetSoundFonts( SDL.UTF8_ToNative(paths) )); }
public static int Mix_SetMusicCMD(string command) { return(INTERNAL_Mix_SetMusicCMD( SDL.UTF8_ToNative(command) )); }
public static extern IntPtr TTF_RenderGlyph_Blended( IntPtr font, ushort ch, SDL.SDL_Color fg );
public void RenderDrawPoints(SDL.SDL_Point[] points) { if (SDL.SDL_RenderDrawPoints(Renderer, points, points.Length) != 0) { throw new Exception("SDL_RenderDrawPoints()", new Exception(SDL.SDL_GetError())); } }
/// <summary> /// Process events for the current window /// </summary> public virtual void ProcessEvent(SDL.SDL_Event e) { switch (e.type) { case SDL.SDL_EventType.SDL_QUIT: // When SDL sends a SDL_QUIT message, we have actually clicked on the close button. CloseActions?.Invoke(); break; case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: PointerButtonPressActions?.Invoke(e.button); break; case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: PointerButtonReleaseActions?.Invoke(e.button); break; case SDL.SDL_EventType.SDL_MOUSEMOTION: MouseMoveActions?.Invoke(e.motion); break; case SDL.SDL_EventType.SDL_MOUSEWHEEL: // To match the Windows behavior we multiply the value by 120 e.wheel.x *= 120; e.wheel.y *= 120; MouseWheelActions?.Invoke(e.wheel); break; case SDL.SDL_EventType.SDL_KEYDOWN: KeyDownActions?.Invoke(e.key); break; case SDL.SDL_EventType.SDL_KEYUP: KeyUpActions?.Invoke(e.key); break; case SDL.SDL_EventType.SDL_TEXTEDITING: TextEditingActions?.Invoke(e.edit); break; case SDL.SDL_EventType.SDL_TEXTINPUT: TextInputActions?.Invoke(e.text); break; case SDL.SDL_EventType.SDL_WINDOWEVENT: { switch (e.window.windowEvent) { case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED: ResizeBeginActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED: ResizeEndActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE: CloseActions?.Invoke(); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SHOWN: ActivateActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_HIDDEN: DeActivateActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MINIMIZED: MinimizedActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MAXIMIZED: MaximizedActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED: RestoredActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_ENTER: MouseEnterActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_LEAVE: MouseLeaveActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: FocusGainedActions?.Invoke(e.window); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: FocusLostActions?.Invoke(e.window); break; } break; } } }
public void RenderReadPixels(SDL.SDL_Rect rect, uint format, IntPtr pixels, int pitch) { if (SDL.SDL_RenderReadPixels(Renderer, ref rect, format, pixels, pitch) != 0) { throw new Exception("SDL_RenderReadPixels()", new Exception(SDL.SDL_GetError())); } }
public static IntPtr IMG_Load(string file) { return(INTERNAL_IMG_Load(SDL.UTF8_ToNative(file))); }
public void SetRenderDrawBlendMode(SDL.SDL_BlendMode blendMode) { if (SDL.SDL_SetRenderDrawBlendMode(Renderer, blendMode) != 0) { throw new Exception("SDL_SetRenderDrawBlendMode()", new Exception(SDL.SDL_GetError())); } }
public void RenderCopyEx(IntPtr texture, SDL.SDL_Rect srcrect, SDL.SDL_Rect dstrect, double angle, SDL.SDL_Point center, SDL.SDL_RendererFlip flip) { if (SDL.SDL_RenderCopyEx(Renderer, texture, ref srcrect, ref dstrect, angle, ref center, flip) != 0) { throw new Exception("SDL_RenderCopyEx()", new Exception(SDL.SDL_GetError())); } }
/* IntPtr refers to a Mix_Chunk* */ /* This is an RWops macro in the C header. */ public static IntPtr Mix_LoadWAV(string file) { IntPtr rwops = SDL.SDL_RWFromFile(file, "rb"); return(Mix_LoadWAV_RW(rwops, 1)); }
public void RenderFillRects(SDL.SDL_Rect[] rects) { if (SDL.SDL_RenderFillRects(Renderer, rects, rects.Length) != 0) { throw new Exception("SDL_RenderFillRects()", new Exception(SDL.SDL_GetError())); } }
public BaseEventArgs(SDL.SDL_Event baseEvent) { BaseEvent = baseEvent; }
public void RenderSetViewport(SDL.SDL_Rect rect) { if (SDL.SDL_RenderSetViewport(Renderer, ref rect) != 0) { throw new Exception("SDL_RenderSetViewport()", new Exception(SDL.SDL_GetError())); } }
//left to do: maximizing/minimizing /// <summary> /// Handles all window related SDL.SDL_events. /// </summary> internal static void WindowEvent(SDL.SDL_Event e) { switch (e.window.windowEvent) { case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED: _size = new Point(e.window.data1, e.window.data2); break; } }
public TrafficLane(SDL.SDL_Point origin, SDL.SDL_Point endPoint, LaneDirection direction) { Origin = origin; EndPoint = endPoint; Direction = direction; }
internal static void InputEvent(SDL.SDL_Event e) { switch (e.type) { case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: { CurrentState.Add((Button)(1000 + e.button.button)); break; } case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: { CurrentState.Remove((Button)(1000 + e.button.button)); break; } case SDL.SDL_EventType.SDL_MOUSEWHEEL: { if (e.wheel.y > 0) CurrentState.Add(Button.MousewheelUp); if (e.wheel.y < 0) CurrentState.Add(Button.MousewheelDown); if (e.wheel.x < 0) CurrentState.Add(Button.MousewheelLeft); if (e.wheel.x > 0) CurrentState.Add(Button.MousewheelRight); break; } case SDL.SDL_EventType.SDL_MOUSEMOTION: { UntranslatedMousePosition = new Point(e.motion.x, e.motion.y); break; } case SDL.SDL_EventType.SDL_KEYDOWN: { if (e.key.repeat == 0) //we dont do repeats (yet?) { int sdlKeyDown = (int)e.key.keysym.sym; if (sdlKeyDown < 65536) CurrentState.Add((Button)(2000 + sdlKeyDown)); else CurrentState.Add((Button)(sdlKeyDown - 1073739381)); } //textinput special keys switch (e.key.keysym.sym) { //backspace removes last character from all text input builders case SDL.SDL_Keycode.SDLK_BACKSPACE: TextInputReceivers.ForEach(builder => { int length = builder.Length; if (length > 0) builder.Remove(length - 1, 1); }); break; case SDL.SDL_Keycode.SDLK_RETURN: TextInputReceivers.ForEach(builder => builder.Append('\n')); break; case SDL.SDL_Keycode.SDLK_TAB: TextInputReceivers.ForEach(builder => builder.Append('\t')); break; } break; } case SDL.SDL_EventType.SDL_KEYUP: { int sdlKeyUp = (int)e.key.keysym.sym; if (sdlKeyUp < 65536) CurrentState.Remove((Button)(2000 + sdlKeyUp)); else CurrentState.Remove((Button)(sdlKeyUp - 1073739381)); break; } case SDL.SDL_EventType.SDL_TEXTINPUT: { byte[] rawBytes = new byte[SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE]; unsafe { Marshal.Copy((IntPtr)e.text.text, rawBytes, 0, SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE); } int length = Array.IndexOf<byte>(rawBytes, 0); string text = Encoding.UTF8.GetString(rawBytes, 0, length); //add text to all stringbuilders in TextInputReceivers TextInputReceivers.ForEach(builder => builder.Append(text)); break; } case SDL.SDL_EventType.SDL_CONTROLLERDEVICEADDED: { //get first free gamepad slot int newGamepadId = -1; while (GamepadHandles[++newGamepadId] != IntPtr.Zero) { } GamepadHandles[newGamepadId] = SDL.SDL_GameControllerOpen(e.cdevice.which); IntPtr joystick = SDL.SDL_GameControllerGetJoystick(GamepadHandles[newGamepadId]); GamepadInstanceIDs.Add(SDL.SDL_JoystickInstanceID(joystick), newGamepadId); GamepadCurrentStates[newGamepadId] = new List<Button>(); GamepadPreviousStates[newGamepadId] = new List<Button>(); //init rumble if supported if (SDL.SDL_JoystickIsHaptic(joystick) == 1) { IntPtr hapticHandle = SDL.SDL_HapticOpenFromJoystick(joystick); if (SDL.SDL_HapticRumbleSupported(hapticHandle) == 1) { SDL.SDL_HapticRumbleInit(hapticHandle); GamepadHapticHandles[newGamepadId] = hapticHandle; } else SDL.SDL_HapticClose(hapticHandle); } break; } case SDL.SDL_EventType.SDL_CONTROLLERDEVICEREMOVED: { int gamepadId = GamepadInstanceIDs[e.cdevice.which]; SDL.SDL_GameControllerClose(GamepadHandles[gamepadId]); GamepadHandles[gamepadId] = IntPtr.Zero; GamepadInstanceIDs.Remove(e.cdevice.which); if (GamepadHapticHandles[gamepadId] != IntPtr.Zero) { SDL.SDL_HapticClose(GamepadHapticHandles[gamepadId]); GamepadHapticHandles[gamepadId] = IntPtr.Zero; } break; } case SDL.SDL_EventType.SDL_CONTROLLERBUTTONDOWN: { GamepadCurrentStates[GamepadInstanceIDs[e.cbutton.which]].Add((Button)(3000 + e.cbutton.button)); break; } case SDL.SDL_EventType.SDL_CONTROLLERBUTTONUP: { GamepadCurrentStates[GamepadInstanceIDs[e.cbutton.which]].Remove((Button)(3000 + e.cbutton.button)); break; } case SDL.SDL_EventType.SDL_CONTROLLERAXISMOTION: { int gamepad = GamepadInstanceIDs[e.caxis.which]; byte axis = e.caxis.axis; float value = (float)e.caxis.axisValue / short.MaxValue; GamepadAxisValues[gamepad, axis] = value; if (value <= -GamepadDeadZone) { //add button if it's not added yet Button button = (Button)(3015 + e.caxis.axis * 2); if (!GamepadCurrentStates[gamepad].Contains(button)) GamepadCurrentStates[gamepad].Add(button); } else if (value >= GamepadDeadZone) { Button button = (Button)(3016 + e.caxis.axis * 2); if (!GamepadCurrentStates[gamepad].Contains(button)) GamepadCurrentStates[gamepad].Add(button); } else { //remove both buttons for this axis if it's not in range if (e.caxis.axis < 4) GamepadCurrentStates[gamepad].Remove((Button)(3015 + e.caxis.axis * 2)); GamepadCurrentStates[gamepad].Remove((Button)(3016 + e.caxis.axis * 2)); } break; } } }
public TextEditingEventArgs(SDL.SDL_Event rawEvent) : base(rawEvent) { RawTimeStamp = rawEvent.edit.timestamp; }
public static string TTF_FontFaceStyleName(IntPtr font) { return(SDL.UTF8_ToManaged( INTERNAL_TTF_FontFaceStyleName(font) )); }