Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD playdate.system.setMenuImage() #101

Merged
merged 25 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ef92175
Organize utility code
Nycto Oct 20, 2024
13b524a
Use playdate realloc directly for SDK allocated pointers
Nycto Oct 20, 2024
7e88020
Completely elid memtrace when disabled
Nycto Jan 14, 2025
1172f2d
WIP setMenuImage
ninovanhooff Jan 27, 2025
3936f50
Fix PlaydateSys.logToConsole argument type
Nycto Feb 5, 2025
f3b0066
Support nil LCDBitmap conversion
Nycto Jan 24, 2025
a384e41
CHANGE separate macos compile step
ninovanhooff Feb 9, 2025
c470f59
Fix incompatible function pointer errors in clang
Nycto Feb 11, 2025
7b9d532
Merge branch 'elideMemtrace' into setMenuImage-elide
ninovanhooff Feb 11, 2025
bfd570c
Simplify patch file location
Nycto Feb 12, 2025
6b07aaf
Fix memtrace build action failure
Nycto Feb 13, 2025
e4b7b3c
Implement pdn (#98)
Nycto Feb 13, 2025
9441f26
FIX Mac simulator path
ninovanhooff Feb 13, 2025
41ffd68
REFACTOR lcdbitmap out of graphics
ninovanhooff Feb 13, 2025
21b5498
Merge remote-tracking branch 'upstream/dev' into setMenuImagerefactor
ninovanhooff Feb 13, 2025
6423a6f
Merge remote-tracking branch 'nycto/bin' into setMenuImagerefactor
ninovanhooff Feb 13, 2025
1e1c6d1
ADD stc/playdate to Nim source path
ninovanhooff Feb 13, 2025
2774e49
Merge remote-tracking branch 'upstream/dev' into setMenuImagerefactor
ninovanhooff Feb 14, 2025
ed782f5
FIX imports in lcdbitmap
ninovanhooff Feb 14, 2025
7f9de77
REMOVE duplicate import
ninovanhooff Feb 14, 2025
ac27f1d
REMOVE useless imports
ninovanhooff Feb 14, 2025
037b6ec
REVERT disabling of echo in graphics.nim
ninovanhooff Feb 14, 2025
973ac8e
ADD tests
ninovanhooff Feb 14, 2025
7036add
REFACTOR lcdbitmap to /playdate
ninovanhooff Feb 16, 2025
4c1013f
CLEANUP leftover comments
ninovanhooff Feb 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/playdate/bindings/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ sdktype:
setAutoLockDisabled {.importc: "setAutoLockDisabled".}: proc (disable: cint) {.
cdecl, raises: [].}

setMenuImage {.importsdk.}: proc (bitmap: LCDBitmapPtr;
xOffset: cint)
setMenuImage {.importc: "setMenuImage".}: proc (bitmap: LCDBitmapPtr;
xOffset: cint) {.cdecl, raises: [].}
addMenuItem {.importc: "addMenuItem".}: proc (title: cstring;
callback: PDMenuItemCallbackFunctionRaw; userdata: pointer): PDMenuItemPtr {.
cdecl, raises: [].}
Expand Down
40 changes: 2 additions & 38 deletions src/playdate/graphics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,14 @@ import std/importutils
import system
import bindings/[api, types]
import bindings/graphics
import lcdbitmap {.all.}
export lcdbitmap

# Only export public symbols, then import all
export graphics
{.hint[DuplicateModuleImport]: off.}
import bindings/graphics {.all.}

type
LCDBitmapObj = object of RootObj
res {.requiresinit.}: LCDBitmapPtr

LCDBitmapObjRef = ref LCDBitmapObj

LCDBitmap* = object
case managed: bool
of true:
obj: LCDBitmapObjRef
of false:
res: LCDBitmapPtr

proc `=destroy`(this: var LCDBitmapObj) =
privateAccess(PlaydateGraphics)
if this.res != nil:
playdate.graphics.freeBitmap(this.res)

proc `=copy`(a: var LCDBitmapObj, b: LCDBitmapObj) {.error.}

converter bitmapPtr*(point: LCDBitmapPtr): auto =
LCDBitmap(managed: false, res: point)

proc bitmapRef(point: LCDBitmapPtr): auto =
LCDBitmap(managed: true, obj: LCDBitmapObjRef(res: point))

proc `==`*(bitmap: LCDBitmap, point: LCDBitmapPtr): bool =
not bitmap.managed and bitmap.res == point

proc resource*(bitmap: LCDBitmap): LCDBitmapPtr =
if bitmap.managed:
return if bitmap.obj != nil: bitmap.obj.res else: nil
else:
return bitmap.res

proc isNil*(bitmap: LCDBitmap): bool =
return bitmap.resource == nil

type LCDVideoPlayerObj = object of RootObj
resource {.requiresinit.}: LCDVideoPlayerPtr
context: LCDBitmap
Expand Down
40 changes: 40 additions & 0 deletions src/playdate/lcdbitmap.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import std/importutils
import bindings/[api, types, graphics]

type
LCDBitmapObj = object of RootObj
res {.requiresinit.}: LCDBitmapPtr

LCDBitmapObjRef = ref LCDBitmapObj

LCDBitmap* = object
case managed: bool
of true:
obj: LCDBitmapObjRef
of false:
res: LCDBitmapPtr

proc `=destroy`(this: var LCDBitmapObj) =
privateAccess(PlaydateGraphics)
if this.res != nil:
playdate.graphics.freeBitmap(this.res)

proc `=copy`(a: var LCDBitmapObj, b: LCDBitmapObj) {.error.}

converter bitmapPtr*(point: LCDBitmapPtr): auto =
LCDBitmap(managed: false, res: point)

proc bitmapRef(point: LCDBitmapPtr): auto =
LCDBitmap(managed: true, obj: LCDBitmapObjRef(res: point))

proc `==`*(bitmap: LCDBitmap, point: LCDBitmapPtr): bool =
not bitmap.managed and bitmap.res == point

proc resource(bitmap: LCDBitmap): LCDBitmapPtr =
if bitmap.managed:
return if bitmap.obj != nil: bitmap.obj.res else: nil
else:
return bitmap.res

proc isNil*(bitmap: LCDBitmap): bool =
return bitmap.resource == nil
9 changes: 8 additions & 1 deletion src/playdate/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sequtils

import bindings/[api, types]
import bindings/system
import lcdbitmap {.all.}

# Only export public symbols, then import all
export system
Expand Down Expand Up @@ -185,7 +186,13 @@ proc removeAllMenuItems*(this: ptr PlaydateSys) =
item.active = false
menuItems.setLen(0)
this.removeAllMenuItems()
# ---

proc setMenuImage*(this: ptr PlaydateSys, image: LCDBitmap, xOffset: int32 = 0) =
privateAccess(PlaydateSys)
this.setMenuImage(
image.resource,
xOffset.cint
)

proc getReduceFlashing* (this: ptr PlaydateSys): bool =
privateAccess(PlaydateSys)
Expand Down
3 changes: 2 additions & 1 deletion tests/src/playdate_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
##

import playdate/api
import ../[t_buttons, t_graphics, t_nineslice, t_files, t_midi, t_scoreboards]
import ../[ t_system, t_buttons, t_graphics, t_nineslice, t_files, t_midi, t_scoreboards]

proc runTests() {.raises: [].} =
try:
execSystemTests(false)
execButtonsTests()
execGraphicsTests(true)
execNineSliceTests(true)
Expand Down
18 changes: 18 additions & 0 deletions tests/t_system.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest, playdate/api

proc execSystemTests*(runnable: bool)=
suite "System API":

test "setMenuImage with nil":
if(runnable):
playdate.system.setMenuImage(nil, 0)

test "setMenuImage with bitmap":
if(runnable):
let bgBitmap = playdate.graphics.newBitmap(400, 240, kColorBlack)
playdate.system.setMenuImage(bgBitmap, 0)

when isMainModule:
# We can't run these methods from the tests, so we're only interested in
# whether they compile.
execSystemTests(runnable = false)