Skip to content

Commit

Permalink
Fixed bug in GetWindowDisplayIndex. API update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amun committed Aug 11, 2024
1 parent 9e1c78c commit 3dd2829
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 27 deletions.
63 changes: 47 additions & 16 deletions src/nsdl2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@

{.push raises: [].}

when NimMajor >= 2 and defined nimPreviewSlimSystem:
when defined nimPreviewSlimSystem:
from std/assertions import assert

import std/options

export options

import nsdl2/config
import nsdl2/libsdl2
import nsdl2/utils
Expand Down Expand Up @@ -1939,12 +1943,13 @@ proc RenderWindowToLogical*(renderer: Renderer, window_x: int, window_y: int,
logical_y = outy
true

proc SetRenderDrawBlendMode*(renderer: Renderer, blend_mode: BlendMode): bool =
## ```c
## int SDL_SetRenderDrawBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode);
## ```
ensure_zero "SDL_SetRenderDrawBlendMode":
SDL_SetRenderDrawBlendMode renderer, blend_mode
when use_blendmode:
proc SetRenderDrawBlendMode*(renderer: Renderer, blend_mode: BlendMode): bool =
## ```c
## int SDL_SetRenderDrawBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode);
## ```
ensure_zero "SDL_SetRenderDrawBlendMode":
SDL_SetRenderDrawBlendMode renderer, blend_mode

proc SetRenderDrawColor*(renderer: Renderer, r: byte, g: byte, b: byte,
a: byte = 0xff): bool =
Expand All @@ -1971,13 +1976,14 @@ proc SetTextureAlphaMod*(texture: Texture, a: byte): bool =
ensure_zero "SDL_SetTextureAlphaMod":
SDL_SetTextureAlphaMod texture, a

proc SetTextureBlendMode*(texture: Texture, blend_mode: BlendMode): bool =
## ```c
## int SDL_SetTextureBlendMode(SDL_Texture *texture,
## SDL_BlendMode blendMode);
## ```
ensure_zero "SDL_SetTextureBlendMode":
SDL_SetTextureBlendMode texture, blend_mode
when use_blendmode:
proc SetTextureBlendMode*(texture: Texture, blend_mode: BlendMode): bool =
## ```c
## int SDL_SetTextureBlendMode(SDL_Texture *texture,
## SDL_BlendMode blendMode);
## ```
ensure_zero "SDL_SetTextureBlendMode":
SDL_SetTextureBlendMode texture, blend_mode

proc SetTextureColorMod*(texture: Texture, r: byte, g: byte, b: byte): bool =
## ```c
Expand Down Expand Up @@ -2551,6 +2557,18 @@ proc GetDisplayBounds*(display_index: Natural, rect: var Rect): bool =
ensure_zero "SDL_GetDisplayBounds":
SDL_GetDisplayBounds(display_index.cint, rect.addr)

proc GetDisplayBounds*(display_index: Natural): Option[Rect] =
## Get the desktop area represented by a display.
##
## ```c
## int SDL_GetDisplayBounds(int displayIndex, SDL_Rect *rect);
## ```
var bounds = Rect.init(-1, -1, -1, -1)
if not GetDisplayBounds(display_index, bounds):
return none Rect
some bounds

# XXX: deprecate?
proc GetDisplayBounds*(display_index: Natural, x: var int, y: var int,
width: var int, height: var int): bool =
## Get the desktop area represented by a display.
Expand Down Expand Up @@ -2637,7 +2655,20 @@ proc GetDisplayUsableBounds*(display_index: Natural, rect: var Rect): bool =
ensure_zero "SDL_GetDisplayUsableBounds":
SDL_GetDisplayUsableBounds(display_index.cint, rect.addr)

# XXX:
proc GetDisplayUsableBounds*(display_index: Natural): Option[Rect] =
## Get the usable desktop area represented by a display.
##
## Available since SDL 2.0.5. Equal to `GetDisplayBounds`_ prior SDL 2.0.5.
##
## ```c
## int SDL_GetDisplayUsableBounds(int displayIndex, SDL_Rect *rect);
## ```
var rect {.noinit.}: Rect
if not GetDisplayUsableBounds(display_index, rect):
return none Rect
some rect

# XXX: deprecate?
proc GetDisplayUsableBounds*(display_index: Natural, x: var int, y: var int,
width: var int, height: var int): bool =
## Get the usable desktop area represented by a display.
Expand Down Expand Up @@ -2733,7 +2764,7 @@ proc GetWindowDisplayIndex*(window: Window): int =
## ```c
## int SDL_GetWindowDisplayIndex(SDL_Window *window)
## ```
ensure_positive "SDL_GetWindowDisplayIndex":
ensure_natural "SDL_GetWindowDisplayIndex":
SDL_GetWindowDisplayIndex window

proc GetWindowDisplayMode*(window: Window, mode: var DisplayMode): bool =
Expand Down
12 changes: 8 additions & 4 deletions src/nsdl2/libsdl2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ dlgencalls "sdl2", lib_paths:
# float SDL_GameControllerGetSensorDataRate(SDL_GameController *gamecontroller, SDL_SensorType type)
# int SDL_GameControllerGetSensorDataWithTimestamp(SDL_GameController *gamecontroller, SDL_SensorType type, Uint64 *timestamp, float *data, int num_values)
# const char * SDL_GameControllerGetSerial(SDL_GameController *gamecontroller)
# Uint64 SDL_GameControllerGetSteamHandle(SDL_GameController *gamecontroller)
# const char* SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
# const char* SDL_GameControllerGetStringForButton(SDL_GameControllerButton button)
# int SDL_GameControllerGetTouchpadFinger(SDL_GameController *gamecontroller, int touchpad, int finger, Uint8 *state, float *x, float *y, float *pressure)
Expand Down Expand Up @@ -907,17 +908,19 @@ dlgencalls "sdl2", lib_paths:
window_x, window_y: cint,
logical_x, logical_y: ptr cfloat) {.unchecked.}

proc SDL_SetRenderDrawBlendMode(renderer: Renderer,
blend_mode: BlendMode): cint
when use_blendmode:
proc SDL_SetRenderDrawBlendMode(renderer: Renderer,
blend_mode: BlendMode): cint

proc SDL_SetRenderDrawColor(renderer: Renderer, r, g, b, a: byte): cint

proc SDL_SetRenderTarget(renderer: Renderer, texture: Texture): cint

proc SDL_SetTextureAlphaMod(texture: Texture, a: byte): cint

proc SDL_SetTextureBlendMode(texture: Texture,
blend_mode: BlendMode): cint
when use_blendmode:
proc SDL_SetTextureBlendMode(texture: Texture,
blend_mode: BlendMode): cint

proc SDL_SetTextureColorMod(texture: Texture, r, g, b: byte): cint

Expand Down Expand Up @@ -1136,6 +1139,7 @@ dlgencalls "sdl2", lib_paths:

proc SDL_DestroyWindow(window: Window)

# SDL 2.28.0.
# int SDL_DestroyWindowSurface(SDL_Window *window)

proc SDL_DisableScreenSaver()
Expand Down
12 changes: 5 additions & 7 deletions src/nsdl2/sdl2inc/events.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type
EVENT_CONTROLLERTOUCHPADMOTION = 0x657
EVENT_CONTROLLERTOUCHPADUP = 0x658
EVENT_CONTROLLERSENSORUPDATE = 0x659
EVENT_CONTROLLERUPDATECOMPLETE_RESERVED_FOR_SDL3
EVENT_CONTROLLERSTEAMHANDLEUPDATED

# Touch events.
EVENT_FINGERDOWN = 0x700
Expand Down Expand Up @@ -302,8 +304,9 @@ type
ControllerDeviceEvent* {.final, pure.} = object
## Controller device event.
typ* : EventType ## `EVENT_CONTROLLERDEVICEADDED`,
## `EVENT_CONTROLLERDEVICEREMOVED`
## or `EVENT_CONTROLLERDEVICEREMAPPED`.
## `EVENT_CONTROLLERDEVICEREMOVED`,
## `EVENT_CONTROLLERDEVICEREMAPPED`
## or `EVENT_CONTROLLERSTEAMHANDLEUPDATED`.
timestamp* : uint32 ## Timestamp (ms).
which* : int32 ## Joystick device index for the `ADDED` event
## or instance id for the `REMOVED`
Expand Down Expand Up @@ -401,11 +404,6 @@ type
typ* : EventType ## `EVENT_QUIT`.
timestamp* : uint32 ## Timestamp (ms).

OSEvent* {.final, pure.} = object
## OS Specific event.
typ* : EventType ## `EVENT_QUIT`.
timestamp* : uint32 ## Timestamp (ms).

UserEvent* {.final, pure.} = object
## A user-defined event type.
typ* : EventType ## `EVENT_USEREVENT` - `EVENT_USEREVENT8`.
Expand Down
21 changes: 21 additions & 0 deletions src/nsdl2/sdl2inc/hints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ gen_hint_enum_type "HintName",
"IME_SUPPORT_EXTENDED_TEXT",
"IOS_HIDE_HOME_INDICATOR",
"JOYSTICK_ALLOW_BACKGROUND_EVENTS",
"JOYSTICK_ARCADESTICK_DEVICES",
"JOYSTICK_ARCADESTICK_DEVICES_EXCLUDED",
"JOYSTICK_BLACKLIST_DEVICES",
"JOYSTICK_BLACKLIST_DEVICES_EXCLUDED",
"JOYSTICK_DEVICE",
"JOYSTICK_FLIGHTSTICK_DEVICES",
"JOYSTICK_FLIGHTSTICK_DEVICES_EXCLUDED",
"JOYSTICK_GAMECUBE_DEVICES",
"JOYSTICK_GAMECUBE_DEVICES_EXCLUDED",
"JOYSTICK_GAMECUBE_RUMBLE_BRAKE",
"JOYSTICK_HIDAPI_COMBINE_JOY_CONS",
"JOYSTICK_HIDAPI_GAMECUBE",
Expand All @@ -86,6 +94,7 @@ gen_hint_enum_type "HintName",
"JOYSTICK_HIDAPI_SHIELD",
"JOYSTICK_HIDAPI_STADIA",
"JOYSTICK_HIDAPI_STEAM",
"JOYSTICK_HIDAPI_STEAMDECK",
"JOYSTICK_HIDAPI_SWITCH_HOME_LED",
"JOYSTICK_HIDAPI_SWITCH_PLAYER_LED",
"JOYSTICK_HIDAPI_SWITCH",
Expand All @@ -98,17 +107,25 @@ gen_hint_enum_type "HintName",
"JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED",
"JOYSTICK_HIDAPI_XBOX_ONE",
"JOYSTICK_HIDAPI_XBOX",
"JOYSTICK_IOKIT",
"JOYSTICK_MFI",
"JOYSTICK_RAWINPUT_CORRELATE_XINPUT",
"JOYSTICK_RAWINPUT",
"JOYSTICK_ROG_CHAKRAM",
"JOYSTICK_THREAD",
"JOYSTICK_THROTTLE_DEVICES",
"JOYSTICK_THROTTLE_DEVICES_EXCLUDED",
"JOYSTICK_WGI",
"JOYSTICK_WHEEL_DEVICES",
"JOYSTICK_WHEEL_DEVICES_EXCLUDED",
"JOYSTICK_ZERO_CENTERED_DEVICES",
"KMSDRM_DEVICE_INDEX",
"KMSDRM_REQUIRE_DRM_MASTER",
"LINUX_DIGITAL_HATS",
"LINUX_HAT_DEADZONES",
"LINUX_JOYSTICK_CLASSIC",
"LINUX_JOYSTICK_DEADZONES",
"LOGGING",
"MAC_BACKGROUND_APP",
"MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK",
"MAC_OPENGL_ASYNC_DISPATCH",
Expand Down Expand Up @@ -139,12 +156,16 @@ gen_hint_enum_type "HintName",
"RENDER_DRIVER",
"RENDER_LINE_METHOD",
"RENDER_LOGICAL_SIZE_MODE",
"RENDER_METAL_PREFER_LOW_POWER_DEVICE",
"RENDER_OPENGL_SHADERS",
"RENDER_SCALE_QUALITY",
"RENDER_VSYNC",
"RETURN_KEY_HIDES_IME",
"ROG_GAMEPAD_MICE",
"ROG_GAMEPAD_MICE_EXCLUDED",
"RPI_VIDEO_LAYER",
"SCREENSAVER_INHIBIT_ACTIVITY_NAME",
"SHUTDOWN_DBUS_ON_QUIT",
"THREAD_FORCE_REALTIME_TIME_CRITICAL",
"THREAD_PRIORITY_POLICY",
"THREAD_STACK_SIZE",
Expand Down
11 changes: 11 additions & 0 deletions src/nsdl2/sdl2inc/pixels.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type
PIXELTYPE_ARRAYU32
PIXELTYPE_ARRAYF16
PIXELTYPE_ARRAYF32
PIXELTYPE_INDEX2

BitmapOrder* {.size: cint.sizeof.} = enum
## Bitmap pixel order (high bit to low bit).
Expand Down Expand Up @@ -112,6 +113,8 @@ type
PIXELFORMAT_BGRA8888 = define_pixelformat(PIXELTYPE_PACKED32, PACKEDORDER_BGRA, PACKEDLAYOUT_8888, 32, 4)
PIXELFORMAT_RGB24 = define_pixelformat(PIXELTYPE_ARRAYU8, ARRAYORDER_RGB, 0, 24, 3)
PIXELFORMAT_BGR24 = define_pixelformat(PIXELTYPE_ARRAYU8, ARRAYORDER_BGR, 0, 24, 3)
PIXELFORMAT_INDEX2LSB = define_pixelformat(PIXELTYPE_INDEX2, BITMAPORDER_4321, 0, 2, 0)
PIXELFORMAT_INDEX2MSB = define_pixelformat(PIXELTYPE_INDEX2, BITMAPORDER_1234, 0, 2, 0)

PIXELFORMAT_EXTERNAL_OES = define_pixelfourcc('O', 'E', 'S', ' ')
PIXELFORMAT_NV21 = define_pixelfourcc('N', 'V', '2', '1')
Expand All @@ -136,12 +139,20 @@ when cpuEndian == bigEndian:
PIXELFORMAT_ARGB32* = PIXELFORMAT_ARGB8888
PIXELFORMAT_BGRA32* = PIXELFORMAT_BGRA8888
PIXELFORMAT_ABGR32* = PIXELFORMAT_ABGR8888
PIXELFORMAT_RGBX32* = PIXELFORMAT_RGBX8888
PIXELFORMAT_XRGB32* = PIXELFORMAT_XRGB8888
PIXELFORMAT_BGRX32* = PIXELFORMAT_BGRX8888
PIXELFORMAT_XBGR32* = PIXELFORMAT_XBGR8888
else:
const
PIXELFORMAT_RGBA32* = PIXELFORMAT_ABGR8888
PIXELFORMAT_ARGB32* = PIXELFORMAT_BGRA8888
PIXELFORMAT_BGRA32* = PIXELFORMAT_ARGB8888
PIXELFORMAT_ABGR32* = PIXELFORMAT_RGBA8888
PIXELFORMAT_RGBX32* = PIXELFORMAT_XBGR8888
PIXELFORMAT_XRGB32* = PIXELFORMAT_BGRX8888
PIXELFORMAT_BGRX32* = PIXELFORMAT_XRGB8888
PIXELFORMAT_XBGR32* = PIXELFORMAT_RGBX8888

# func pixelFlag(x: PixelFormatEnum): uint32 {.inline.} =
# (x.uint32 shr 28) and 0x0f
Expand Down

0 comments on commit 3dd2829

Please sign in to comment.