|
| 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([""]) |
0 commit comments