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

Swap out sdl2 for sdl2_nim #15

Merged
merged 7 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Wiish provides 2 main things:
| App icon | | Y | | Y | |
| File associations | | | | - | - |
| OpenGL windows | | Y | Y | Y | |
| SDL2 windows | | Y | Y | | |
| SDL2 windows | | Y | Y | Y | |
| Included widget lib | | | | | |

**Y** = complete, **-** = not applicable
Expand Down
30 changes: 16 additions & 14 deletions examples/helloworld/main_desktop.nim
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
## Hello, World Wiish App
import wiishpkg/desktop
import logging
import sdl2, sdl2/gfx, sdl2/ttf
import sdl2/sdl
# import sdl2/sdl_gfx
import sdl2/sdl_ttf as ttf

app.launched.handle:
debug "App launched"
ttfInit()
discard ttf.init()

var w = app.newSDLWindow(title = "Hello, SDL Wiish!")
var renderer = createRenderer(w.sdlWindow, -1, Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture)
if renderer.isNil:
destroy w.sdlWindow
w.sdlWindow.destroyWindow()
quit(1)

# Open the font file
let fontsize = (32.0).cint
let fontfile = app.resourcePath("Lato-Regular.ttf")
debug "Trying to open font: ", fontfile.repr
let font:FontPtr = openFont(fontfile, fontsize)
var rectangle = rect(50, 50, 50, 50)
let font = openFont(fontfile, fontsize)
var rectangle = sdl.Rect(x: 50, y: 50, w: 50, h: 50)

# Create the text
var textSurface:SurfacePtr = font.renderTextBlended("Hello, World!", color(50, 100, 50, 255))
var textRect = rect(20, 20, textSurface.w, textSurface.h)
var texture:TexturePtr = renderer.createTextureFromSurface(textSurface)
var textSurface = font.renderTextBlended("Hello, World!", tupleToColor((50, 100, 50, 255)))
var textRect = sdl.Rect(x: 20, y: 20, w: textSurface.w, h: textSurface.h)
var texture = renderer.createTextureFromSurface(textSurface)
textSurface.freeSurface()

# Perform drawing for the window.
w.onDraw.handle(rect):
debug "onDraw"
# Draw background
renderer.setDrawColor 255,255,255,255
renderer.clear
discard renderer.setRenderDrawColor(255,255,255,255)
discard renderer.renderClear()

# Draw rectangle
renderer.setDrawColor 0,0,255,255
renderer.fillRect(rectangle.unsafeAddr)
discard renderer.setRenderDrawColor(0,0,255,255)
discard renderer.renderFillRect(rectangle.unsafeaddr)

# Render the text
renderer.copy(texture, nil, textRect.unsafeAddr)
discard renderer.renderCopy(texture, nil, textRect.unsafeAddr)

# Make it so!
renderer.present
renderer.renderPresent()

app.willExit.handle:
# Run this code just before the application exits
Expand Down
41 changes: 23 additions & 18 deletions examples/helloworld/main_mobile.nim
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
## Hello, World Wiish App
import wiishpkg/mobile
import logging
import sdl2
import sdl2/gfx
import sdl2/ttf

debug "Pre launch..."
import sdl2/sdl
import sdl2/sdl_ttf as ttf

app.launched.handle:
debug "App launched"
# if not ttfInit():
# debug $getError()
discard ttf.init()

var w = app.newSDLWindow(title = "Hello, SDL Wiish!")
var renderer = createRenderer(w.sdlWindow, -1, Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture)
if renderer.isNil:
destroy w.sdlWindow
w.sdlWindow.destroyWindow()
quit(1)

# Open the font file
# let fontsize = (32.0).cint
# let fontfile = app.resourcePath("Lato-Regular.ttf")
# debug "Trying to open font: ", fontfile.repr
# let font:FontPtr = openFont(fontfile, fontsize)
var rectangle = rect(50, 50, 50, 50)
let fontsize = (32.0).cint
let fontfile = app.resourcePath("Lato-Regular.ttf")
debug "Trying to open font: ", fontfile.repr
let font = openFont(fontfile, fontsize)
var rectangle = sdl.Rect(x: 50, y: 50, w: 50, h: 50)

# Create the text
var textSurface = font.renderTextBlended("Hello, World!", tupleToColor((50, 100, 50, 255)))
var textRect = sdl.Rect(x: 20, y: 20, w: textSurface.w, h: textSurface.h)
var texture = renderer.createTextureFromSurface(textSurface)
textSurface.freeSurface()

# Create the text
# var textSurface:SurfacePtr = font.renderTextBlended("Hello, World!", color(50, 100, 50, 255))
Expand All @@ -35,18 +37,21 @@ app.launched.handle:
w.onDraw.handle(rect):
debug "onDraw"
# Draw background
renderer.setDrawColor 255,255,255,255
renderer.clear
discard renderer.setRenderDrawColor(255,255,255,255)
discard renderer.renderClear()

# Draw rectangle
renderer.setDrawColor 0,0,255,255
renderer.fillRect(rectangle.unsafeAddr)
discard renderer.setRenderDrawColor(0,0,255,255)
discard renderer.renderFillRect(rectangle.unsafeaddr)

# Render the text
discard renderer.renderCopy(texture, nil, textRect.unsafeAddr)

# Render the text
# renderer.copy(texture, nil, textRect.unsafeAddr)

# Make it so!
renderer.present
renderer.renderPresent()

app.willExit.handle:
# Run this code just before the application exits
Expand Down
2 changes: 1 addition & 1 deletion examples/opengl/main_desktop.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Hello, World Wiish App
import sdl2
import sdl2/sdl except log
import wiishpkg/desktop
import logging
import opengl
Expand Down
2 changes: 1 addition & 1 deletion examples/opengl/main_mobile.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Hello, World Mobile Wiish App
import sdl2
import sdl2/sdl except log
import wiishpkg/mobile
import logging
import opengl
Expand Down
30 changes: 16 additions & 14 deletions examples/sdl2/main_desktop.nim
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
## Hello, World Wiish App
import wiishpkg/desktop
import logging
import sdl2, sdl2/gfx, sdl2/ttf
import sdl2/sdl
# import sdl2/sdl_gfx
import sdl2/sdl_ttf as ttf

app.launched.handle:
debug "App launched"
ttfInit()
discard ttf.init()

var w = app.newSDLWindow(title = "Hello, SDL Wiish!")
var renderer = createRenderer(w.sdlWindow, -1, Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture)
if renderer.isNil:
destroy w.sdlWindow
w.sdlWindow.destroyWindow()
quit(1)

# Open the font file
let fontsize = (32.0).cint
let fontfile = app.resourcePath("Lato-Regular.ttf")
debug "Trying to open font: ", fontfile.repr
let font:FontPtr = openFont(fontfile, fontsize)
var rectangle = rect(50, 50, 50, 50)
let font = openFont(fontfile, fontsize)
var rectangle = sdl.Rect(x: 50, y: 50, w: 50, h: 50)

# Create the text
var textSurface:SurfacePtr = font.renderTextBlended("Hello, World!", color(50, 100, 50, 255))
var textRect = rect(20, 20, textSurface.w, textSurface.h)
var texture:TexturePtr = renderer.createTextureFromSurface(textSurface)
var textSurface = font.renderTextBlended("Hello, World!", tupleToColor((50, 100, 50, 255)))
var textRect = sdl.Rect(x: 20, y: 20, w: textSurface.w, h: textSurface.h)
var texture = renderer.createTextureFromSurface(textSurface)
textSurface.freeSurface()

# Perform drawing for the window.
w.onDraw.handle(rect):
debug "onDraw"
# Draw background
renderer.setDrawColor 255,255,255,255
renderer.clear
discard renderer.setRenderDrawColor(255,255,255,255)
discard renderer.renderClear()

# Draw rectangle
renderer.setDrawColor 0,0,255,255
renderer.fillRect(rectangle.unsafeAddr)
discard renderer.setRenderDrawColor(0,0,255,255)
discard renderer.renderFillRect(rectangle.unsafeaddr)

# Render the text
renderer.copy(texture, nil, textRect.unsafeAddr)
discard renderer.renderCopy(texture, nil, textRect.unsafeAddr)

# Make it so!
renderer.present
renderer.renderPresent()

app.willExit.handle:
# Run this code just before the application exits
Expand Down
41 changes: 23 additions & 18 deletions examples/sdl2/main_mobile.nim
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
## Hello, World Wiish App
import wiishpkg/mobile
import logging
import sdl2
import sdl2/gfx
import sdl2/ttf

debug "Pre launch..."
import sdl2/sdl
import sdl2/sdl_ttf as ttf

app.launched.handle:
debug "App launched"
# if not ttfInit():
# debug $getError()
discard ttf.init()

var w = app.newSDLWindow(title = "Hello, SDL Wiish!")
var renderer = createRenderer(w.sdlWindow, -1, Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture)
if renderer.isNil:
destroy w.sdlWindow
w.sdlWindow.destroyWindow()
quit(1)

# Open the font file
# let fontsize = (32.0).cint
# let fontfile = app.resourcePath("Lato-Regular.ttf")
# debug "Trying to open font: ", fontfile.repr
# let font:FontPtr = openFont(fontfile, fontsize)
var rectangle = rect(50, 50, 50, 50)
let fontsize = (32.0).cint
let fontfile = app.resourcePath("Lato-Regular.ttf")
debug "Trying to open font: ", fontfile.repr
let font = openFont(fontfile, fontsize)
var rectangle = sdl.Rect(x: 50, y: 50, w: 50, h: 50)

# Create the text
var textSurface = font.renderTextBlended("Hello, World!", tupleToColor((50, 100, 50, 255)))
var textRect = sdl.Rect(x: 20, y: 20, w: textSurface.w, h: textSurface.h)
var texture = renderer.createTextureFromSurface(textSurface)
textSurface.freeSurface()

# Create the text
# var textSurface:SurfacePtr = font.renderTextBlended("Hello, World!", color(50, 100, 50, 255))
Expand All @@ -35,18 +37,21 @@ app.launched.handle:
w.onDraw.handle(rect):
debug "onDraw"
# Draw background
renderer.setDrawColor 255,255,255,255
renderer.clear
discard renderer.setRenderDrawColor(255,255,255,255)
discard renderer.renderClear()

# Draw rectangle
renderer.setDrawColor 0,0,255,255
renderer.fillRect(rectangle.unsafeAddr)
discard renderer.setRenderDrawColor(0,0,255,255)
discard renderer.renderFillRect(rectangle.unsafeaddr)

# Render the text
discard renderer.renderCopy(texture, nil, textRect.unsafeAddr)

# Render the text
# renderer.copy(texture, nil, textRect.unsafeAddr)

# Make it so!
renderer.present
renderer.renderPresent()

app.willExit.handle:
# Run this code just before the application exits
Expand Down
34 changes: 33 additions & 1 deletion src/wiishpkg/building/build_ios.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ proc buildSDLlib(sdk_version:string, simulator:bool = true):string =
## Returns the path to libSDL2.a, creating it if necessary
let
platform = if simulator: "iphonesimulator" else: "iphoneos"
xcodeProjPath = DATADIR/"sdl2src/Xcode-iOS/SDL"
xcodeProjPath = DATADIR/"SDL/Xcode-iOS/SDL"
result = (xcodeProjPath/"build/Release-" & platform)/"libSDL2.a"
if not fileExists(result):
debug &"Building {result.basename}..."
Expand All @@ -60,6 +60,32 @@ proc buildSDLlib(sdk_version:string, simulator:bool = true):string =
if not fileExists(result):
raise newException(CatchableError, "Failed to build libSDL2.a")

proc buildSDLTTFlib(sdk_version:string, simulator:bool = true):string =
## Returns the path to libSDL2
let
platform = if simulator: "iphonesimulator" else: "iphoneos"
xcodeProjPath = DATADIR/"SDL_TTF/Xcode-iOS"
result = (xcodeProjPath/"build/Release-" & platform)/"libSDL2_ttf.a"
if not fileExists(result):
debug &"Building {result.basename}..."
var args = @[
"xcodebuild",
"-project", xcodeProjPath/"SDL_ttf.xcodeproj",
"-configuration", "Release",
"-sdk", platform & sdk_version,
"SYMROOT=build",
]
if simulator:
args.add("ARCHS=i386 x86_64")
else:
args.add("ARCHS=arm64 armv7")
run(args)
else:
debug &"Using existing {result.basename}"

if not fileExists(result):
raise newException(CatchableError, "Failed to build libSDL2.a")

proc listPossibleSDKVersions(simulator: bool):seq[string] =
## List all SDK versions installed on this computer
for kind, thing in walkDir(simulator_sdk_root):
Expand Down Expand Up @@ -161,6 +187,9 @@ proc doiOSBuild*(directory:string, configPath:string, release:bool = true):strin

debug "Obtaining SDL2 library ..."
let sdllibSrc = buildSDLlib(sdk_version, simulator)

debug "Obtaining SDL2_ttf library ..."
let sdlttflibSrc = buildSDLTTFlib(sdk_version, simulator)

debug "Configuring build ..."
template linkAndCompile(flag:untyped) =
Expand All @@ -172,6 +201,7 @@ proc doiOSBuild*(directory:string, configPath:string, release:bool = true):strin
"-d:ios",
"-d:iPhone",
"--dynlibOverride:SDL2",
"--dynlibOverride:SDL2_ttf",
&"-d:appBundleIdentifier={config.bundle_identifier}",
])
if simulator:
Expand All @@ -186,11 +216,13 @@ proc doiOSBuild*(directory:string, configPath:string, release:bool = true):strin
linkerFlags.add([
"-fobjc-link-runtime",
"-L", sdllibSrc.parentDir,
"-L", sdlttflibSrc.parentDir,
])
linkAndCompile(["-isysroot", sdkPath])

nimFlags.add(["--threads:on"])
linkerFlags.add("-lSDL2")
linkerFlags.add("-lSDL2_ttf")
nimFlags.add([
"--warning[LockLevel]:off",
"--verbosity:0",
Expand Down
3 changes: 2 additions & 1 deletion src/wiishpkg/building/data/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sdl2src/Xcode-iOS/SDL/build
SDL/Xcode-iOS/SDL/build
SDL_TTF/Xcode-iOS/build
Loading