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 support for background image #51

Merged
merged 1 commit into from
Oct 24, 2021
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/BasicGame/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ HEIGHT = 400
# Width of the game window
WIDTH = 400
# Background color of the game window
BACKGROUND = colorant"purple"
BACKGROUND = "moon.jpg" #colorant"purple"

# Globals to store the velocity of the actor
dx = 2
Expand Down
Binary file added example/BasicGame/images/moon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions src/GameZero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ const paused = Ref{Bool}(false)
function initscreen(gm::Module, name::String)
h = getifdefined(gm, HEIGHTSYMBOL, 400)
w = getifdefined(gm, WIDTHSYMBOL, 600)
color = getifdefined(gm, BACKSYMBOL, ARGB(colorant"white"))
s = Screen(name, w, h, color)
background = getifdefined(gm, BACKSYMBOL, ARGB(colorant"white"))
if !(background isa Colorant)
background=image_surface(background)
end
s = Screen(name, w, h, background)
clear(s)
return s
end
Expand Down
25 changes: 17 additions & 8 deletions src/screen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ struct Screen
renderer
height::Int
width::Int
background::ARGB
background::Union{ARGB, Ptr{SDL2.Surface}}

function Screen(name, w, h, color)
function Screen(name, w, h, background)
win, renderer = makeWinRenderer(name, w, h)
if !(color isa ARGB)
color = ARGB(color)
end
new(win, renderer, h, w, color)
new(win, renderer, h, w, to_ARGB(background))
end
end

#non ARGB colorant is converted to ARGB
#ARGB colorant is rerturned as is
# non colorant is returned as is (required since background is stored as an Union )
to_ARGB(c) = c
to_ARGB(c::ARGB) = c
to_ARGB(c::Colorant) = ARGB(c)


abstract type Geom end

"""
Expand Down Expand Up @@ -177,8 +182,6 @@ end

clear() = clear(game[].screen)

Base.fill(c::Colorant) = Base.fill(game[].screen, c)

function Base.fill(s::Screen, c::Colorant)
SDL2.SetRenderDrawColor(
s.renderer,
Expand All @@ -187,6 +190,12 @@ function Base.fill(s::Screen, c::Colorant)
SDL2.RenderClear(s.renderer)
end

function Base.fill(s::Screen, sf::Ptr{SDL2.Surface})
texture = SDL2.CreateTextureFromSurface(s.renderer, sf)
SDL2.RenderCopy(s.renderer, texture, C_NULL, C_NULL)
SDL2.DestroyTexture(texture)
end

draw(l::T, args...; kv...) where T <: Geom = draw(game[].screen, l, args...; kv...)

function draw(s::Screen, l::Line, c::Colorant=colorant"black")
Expand Down