Skip to content

Commit 88b6321

Browse files
authored
Merge pull request #21 from NHDaly/sdl2_example
Add examples for building a *fully self-contained* SDL2 app.
2 parents ddd0bf3 + e019f9c commit 88b6321

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

examples/sdl.jl

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# examples/sdl.jl
2+
# This is an example of an Application that uses a package containing binary
3+
# dependencies, in this case SimpleDirectMediaLayer.jl. The binary
4+
# dependencies need to be provided to ApplicationBuilder at build-time. Please
5+
# see the associated build file here:
6+
# test/build_examples/sdl.jl
7+
# https://github.com/NHDaly/ApplicationBuilder.jl/tree/master/test/build_examples/sdl.jl
8+
9+
using ApplicationBuilder
10+
11+
using SimpleDirectMediaLayer
12+
SDL2 = SimpleDirectMediaLayer
13+
14+
fontFile = joinpath(Pkg.dir("SimpleDirectMediaLayer"),
15+
"assets","fonts","FiraCode","ttf","FiraCode-Regular.ttf")
16+
17+
# Override SDL libs + assets locations if this script is being compiled for mac .app builds
18+
if get(ENV, "COMPILING_APPLE_BUNDLE", "false") == "true"
19+
eval(SDL2, :(libSDL2 = "libSDL2.dylib"))
20+
eval(SDL2, :(libSDL2_ttf = "libSDL2_ttf.dylib"))
21+
eval(SDL2, :(libSDL2_mixer = "libSDL2_mixer.dylib"))
22+
23+
fontFile = basename(fontFile)
24+
end
25+
26+
function helloFromSDL()
27+
28+
SDL2.init()
29+
30+
win = SDL2.CreateWindow("Hello World!", Int32(100), Int32(100), Int32(300), Int32(400),
31+
UInt32(SDL2.WINDOW_SHOWN))
32+
renderer = SDL2.CreateRenderer(win, Int32(-1),
33+
UInt32(SDL2.RENDERER_ACCELERATED | SDL2.RENDERER_PRESENTVSYNC))
34+
35+
running = true
36+
while running
37+
# Check for quitting
38+
e = SDL2.event()
39+
if isa(e, SDL2.QuitEvent)
40+
running = false
41+
end
42+
43+
x,y = Int[1], Int[1]
44+
SDL2.PumpEvents()
45+
SDL2.GetMouseState(pointer(x), pointer(y))
46+
47+
# Set background render color
48+
SDL2.SetRenderDrawColor(renderer, 200, 200, 200, 255)
49+
SDL2.RenderClear(renderer)
50+
51+
# Draw over background
52+
SDL2.SetRenderDrawColor(renderer, 20, 50, 105, 255)
53+
SDL2.RenderDrawLine(renderer,0,0,800,600)
54+
55+
56+
# Create text
57+
if isfile(fontFile)
58+
font = SDL2.TTF_OpenFont(fontFile, 14)
59+
txt = "Hello, world!"
60+
text = SDL2.TTF_RenderText_Blended(font, txt, SDL2.Color(20,20,20,255))
61+
tex = SDL2.CreateTextureFromSurface(renderer,text)
62+
63+
fx,fy = Int[1], Int[1]
64+
SDL2.TTF_SizeText(font, txt, pointer(fx), pointer(fy))
65+
fx,fy = fx[],fy[]
66+
SDL2.RenderCopy(renderer, tex, C_NULL, pointer_from_objref(SDL2.Rect(Int32(10), Int32(10),fx,fy)))
67+
end
68+
69+
# Draw on mouse
70+
rect = SDL2.Rect(x[],y[],50,50)
71+
SDL2.RenderFillRect(renderer, pointer_from_objref(rect) )
72+
73+
# Flip screen
74+
SDL2.RenderPresent(renderer)
75+
sleep(0.001)
76+
end
77+
# Close window
78+
SDL2.DestroyWindow(win)
79+
SDL2.Quit()
80+
end
81+
82+
Base.@ccallable function julia_main(args::Vector{String})::Cint
83+
ApplicationBuilder.App.change_dir_if_bundle()
84+
helloFromSDL()
85+
return 0
86+
end
87+
88+
#julia_main([""])

test/build_examples/sdl.jl

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using ApplicationBuilder; using BuildApp
2+
3+
examples_blink = joinpath(@__DIR__, "..", "..", "examples", "sdl.jl")
4+
5+
# Allow this file to be called either as a standalone file to build the above
6+
# example, or from runtests.jl using a globally-defined builddir.
7+
isdefined(:builddir) || (builddir="builddir")
8+
9+
using SimpleDirectMediaLayer
10+
SDL2 = SimpleDirectMediaLayer
11+
12+
sdlPkg = Pkg.dir("SimpleDirectMediaLayer")
13+
homebrew = Pkg.dir("Homebrew")
14+
15+
# Copy and modify all of the required libraries for SDL2 to run. Only need
16+
# to do this manual step once.
17+
# Note: this step is required because the SDL libs are linked to reference
18+
# eachother, so some manual modification must be done. Perhaps this could be
19+
# automated by ApplicationBuilder as well in the future.
20+
libs = joinpath(builddir, "sdl_libs")
21+
mkpath(libs)
22+
function cp_lib(l)
23+
name = basename(l)
24+
cp(l, joinpath(libs, name), follow_symlinks=true)
25+
l = joinpath(libs, name)
26+
run(`install_name_tool -id "$name" $l`)
27+
try
28+
external_deps = readlines(pipeline(`otool -L $l`,
29+
`grep .julia`, # filter julia lib deps
30+
`sed 's/(.*)$//'`)) # remove trailing parens
31+
for line in external_deps
32+
path = strip(line)
33+
depname = basename(path)
34+
depname = "$(match(r"(\w+)", depname)[1]).dylib" # strip version
35+
cmd = `install_name_tool -change "$path" "@rpath/$depname" $l`
36+
println(cmd)
37+
run(cmd)
38+
end
39+
end
40+
end
41+
cp_lib(SDL2.libSDL2)
42+
cp_lib(SDL2.libSDL2_ttf)
43+
cp_lib(SDL2.libSDL2_mixer)
44+
cp_lib(joinpath(homebrew, "deps/usr/lib/libmodplug.dylib"))
45+
cp_lib(joinpath(homebrew, "deps/usr/lib/libvorbisfile.dylib"))
46+
cp_lib(joinpath(homebrew, "deps/usr/lib/libvorbis.dylib"))
47+
cp_lib(joinpath(homebrew, "deps/usr/lib/libfreetype.dylib"))
48+
cp_lib(joinpath(homebrew, "deps/usr/lib/libogg.dylib"))
49+
cp_lib(joinpath(homebrew, "deps/usr/lib/libpng16.dylib"))
50+
51+
52+
BuildApp.build_app_bundle(examples_blink;
53+
verbose = true,
54+
resources = [joinpath(sdlPkg,
55+
"assets","fonts","FiraCode","ttf","FiraCode-Regular.ttf"),
56+
],
57+
libraries = ["libs/*"],
58+
appname="HelloSDL2", builddir=builddir)

0 commit comments

Comments
 (0)