Skip to content
This repository has been archived by the owner on Apr 28, 2021. It is now read-only.

moved some functionality to GLFW.jl #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/GLWindow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ import GLFW: Window, Monitor
import GLAbstraction: render, N0f8
import GeometryTypes: widths


#compatibility with the GLFW revamp
#things that might be used somewhere in GLWindow, all from GLFW/types.jl
import GLFW: Window, MonitorProperties
const create_glcontext = Window
import GLFW: swapbuffers, make_windowed!, make_fullscreen!, set_visibility!
#things that might be used somewhere in GLWindow, all from GLFW/extensions.jl
import GLFW: register_callbacks,
standard_screen_resolution, standard_context_hints, standard_window_hints,
full_screen_usage_message, poll_glfw, to_arrow_symbol, primarymonitorresolution
include("types.jl")

include("core.jl")
Expand Down
6 changes: 0 additions & 6 deletions src/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ end
Takes a screen and registers a list of callback functions.
Returns a dict{Symbol, Signal}(name_of_callback => signal)
"""
function register_callbacks(window::GLFW.Window, callbacks::Vector{Function})
tmp = map(callbacks) do f
(Symbol(last(split(string(f),"."))), f(window))
end
Dict{Symbol, Any}(tmp)
end
function register_callbacks(window::Screen, callbacks::Vector{Function})
register_callbacks(window.nativewindow, callbacks)
end
16 changes: 1 addition & 15 deletions src/core.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#=
Functions that are derived from Base or other packages
=#
function Base.show(io::IO, m::MonitorProperties)
println(io, "name: ", m.name)
println(io, "physicalsize: ", m.physicalsize[1], "x", m.physicalsize[2])
println(io, "resolution: ", m.videomode.width, "x", m.videomode.height)
println(io, "dpi: ", m.dpi[1], "x", m.dpi[2])
end

function Base.show(io::IO, m::Screen)
println(io, "name: ", m.name)
println(io, "children: ", length(m.children))
Expand Down Expand Up @@ -39,15 +34,6 @@ function isoutside(screens_mouseposition)
true
end

"""
Returns the monitor resolution of the primary monitor.
"""
function primarymonitorresolution()
props = MonitorProperties(GLFW.GetPrimaryMonitor())
w,h = props.videomode.width, props.videomode.height
Vec(Int(w),Int(h))
end

"""
Create a new rectangle with x,y == 0,0 while taking the widths from the original
Rectangle
Expand Down
9 changes: 0 additions & 9 deletions src/events.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@

function to_arrow_symbol(button_set)
for b in button_set
GLFW.KEY_RIGHT == b && return :right
GLFW.KEY_LEFT == b && return :left
GLFW.KEY_DOWN == b && return :down
GLFW.KEY_UP == b && return :up
end
return :nothing
end

function mousedragg_objectid(mouse_dragg, mouse_hover)
map(mouse_dragg) do dragg
Expand Down
170 changes: 6 additions & 164 deletions src/screen.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

#moved some functionality to GLFW.jl/extensions.jl These should probably get implemented for all the supported backends?
"""
Callback which can be used to catch OpenGL errors.
"""
Expand All @@ -25,8 +25,6 @@ global const _openglerrorcallback = cfunction(
openglerrorcallback, Void,
(GLenum, GLenum,GLuint, GLenum, GLsizei, Ptr{GLchar}, Ptr{Void})
)


"""
Screen constructor cnstructing a new screen from a parant screen.
"""
Expand Down Expand Up @@ -114,145 +112,11 @@ function standard_callbacks()
]
end

"""
Tries to create sensible context hints!
Taken from lessons learned at:
[GLFW](http://www.glfw.org/docs/latest/window.html)
"""
function standard_context_hints(major, minor)
# this is spaar...Modern OpenGL !!!!
major < 3 && error("OpenGL major needs to be at least 3.0. Given: $major")
# core profile is only supported for OpenGL 3.2+ (and a must for OSX, so
# for the sake of homogenity, we try to default to it for everyone!)
if (major > 3 || (major == 3 && minor >= 2 ))
profile = GLFW.OPENGL_CORE_PROFILE
else
profile = GLFW.OPENGL_ANY_PROFILE
end
[
(GLFW.CONTEXT_VERSION_MAJOR, major),
(GLFW.CONTEXT_VERSION_MINOR, minor),
(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE),
(GLFW.OPENGL_PROFILE, profile)
]
end

"""
Takes half the resolution of the primary monitor.
This should make for sensible defaults!
"""
function standard_screen_resolution()
w, h = primarymonitorresolution()
(div(w,2), div(h,2)) # half of total resolution seems like a good fit!
end



"""
Standard window hints for creating a plain context without any multisampling
or extra buffers beside the color buffer
"""
function standard_window_hints()
[
(GLFW.SAMPLES, 0),
(GLFW.DEPTH_BITS, 0),

(GLFW.ALPHA_BITS, 8),
(GLFW.RED_BITS, 8),
(GLFW.GREEN_BITS, 8),
(GLFW.BLUE_BITS, 8),

(GLFW.STENCIL_BITS, 0),
(GLFW.AUX_BUFFERS, 0)
]
end


full_screen_usage_message() = """
Keyword arg fullscreen accepts:
Integer: The number of the Monitor to Select
Bool: if true, primary monitor gets fullscreen, false no fullscren (default)
GLFW.Monitor: Fullscreens on the passed monitor
"""

"""
Function to create a pure GLFW OpenGL window
"""
function create_glcontext(
name = "GLWindow";
resolution = standard_screen_resolution(),
debugging = false,
major = 3,
minor = 3,# this is what GLVisualize needs to offer all features
windowhints = standard_window_hints(),
contexthints = standard_context_hints(major, minor),
visible = true,
focus = false,
fullscreen = false,
monitor = nothing
)
# we create a new context, so we need to clear the shader cache.
# TODO, cache shaders in GLAbstraction per GL context
GLFW.WindowHint(GLFW.VISIBLE, visible)
GLFW.WindowHint(GLFW.FOCUSED, focus)
GLAbstraction.empty_shader_cache!()
for ch in contexthints
GLFW.WindowHint(ch[1], ch[2])
end
for wh in windowhints
GLFW.WindowHint(wh[1], wh[2])
end

@static if is_apple()
if debugging
warn("OpenGL debug message callback not available on osx")
debugging = false
end
end

GLFW.WindowHint(GLFW.OPENGL_DEBUG_CONTEXT, Cint(debugging))

monitor = if monitor == nothing
GLFW.GetPrimaryMonitor()
elseif isa(monitor, Integer)
GLFW.GetMonitors()[monitor]
elseif isa(monitor, GLFW.Monitor)
monitor
else
error("Monitor needs to be nothing, int, or GLFW.Monitor. Found: $monitor")
end

window = GLFW.CreateWindow(resolution..., String(name))

if fullscreen
GLFW.SetKeyCallback(window, (_1, button, _2, _3, _4) -> begin
button == GLFW.KEY_ESCAPE && GLWindow.make_windowed!(window)
end)
GLWindow.make_fullscreen!(window, monitor)
end

GLFW.MakeContextCurrent(window)
# tell GLAbstraction that we created a new context.
# This is important for resource tracking
GLAbstraction.new_context()

debugging && glDebugMessageCallbackARB(_openglerrorcallback, C_NULL)
window
end

make_fullscreen!(screen::Screen, monitor::GLFW.Monitor = GLFW.GetPrimaryMonitor()) = make_fullscreen!(nativewindow(screen), monitor)
function make_fullscreen!(window::GLFW.Window, monitor::GLFW.Monitor = GLFW.GetPrimaryMonitor())
vidmodes = GLFW.GetVideoModes(monitor)[end]
GLFW.SetWindowMonitor(window, monitor, 0, 0, vidmodes.width, vidmodes.height, GLFW.DONT_CARE)
return
end


make_windowed!(screen::Screen) = make_windowed!(nativewindow(screen))
function make_windowed!(window::GLFW.Window)
width, height = standard_screen_resolution()
GLFW.SetWindowMonitor(window, GLFW.Monitor(C_NULL), 0, 0, width, height, GLFW.DONT_CARE)
return
end


"""
Most basic Screen constructor, which is usually used to create a parent screen.
Expand Down Expand Up @@ -414,14 +278,7 @@ function set_visibility!(glc::AbstractContext, visible::Bool)
end
return
end
function set_visibility!(screen::GLFW.Window, visible::Bool)
if visible
GLFW.ShowWindow(screen)
else !visible
GLFW.HideWindow(screen)
end
return
end



widths(s::Screen) = widths(value(s.area))
Expand All @@ -434,21 +291,14 @@ Check if a Screen is opened.
function Base.isopen(window::Screen)
isopen(nativewindow(window))
end
function Base.isopen(window::GLFW.Window)
window.handle == C_NULL && return false
!GLFW.WindowShouldClose(window)
end

"""
Swap the framebuffers on the Screen.
"""
function swapbuffers(window::Screen)
swapbuffers(nativewindow(window))
end
function swapbuffers(window::GLFW.Window)
window.handle == C_NULL && return
GLFW.SwapBuffers(window)
return
end

function Base.resize!(x::Screen, w::Integer, h::Integer)
nw = GLWindow.nativewindow(x)
if isroot(x)
Expand All @@ -460,17 +310,9 @@ function Base.resize!(x::Screen, w::Integer, h::Integer)
wf, hf = round.(f .* Vec(w, h))
push!(x.area, SimpleRectangle(area.x, area.y, Int(wf), Int(hf)))
end
function Base.resize!(x::GLFW.Window, w::Integer, h::Integer)
GLFW.SetWindowSize(x, w, h)
end

"""
Poll events on the screen which will propogate signals through react.
"""
function poll_glfw()
GLFW.PollEvents()
end

function mouse2id(s::Screen)
s.inputs[:mouse2id]
end
Expand Down
27 changes: 1 addition & 26 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,33 +145,8 @@ function Base.resize!(fb::GLFramebuffer, window_size)
nothing
end


struct MonitorProperties
name::String
isprimary::Bool
position::Vec{2, Int}
physicalsize::Vec{2, Int}
videomode::GLFW.VidMode
videomode_supported::Vector{GLFW.VidMode}
dpi::Vec{2, Float64}
monitor::Monitor
end

function MonitorProperties(monitor::Monitor)
name = GLFW.GetMonitorName(monitor)
isprimary = GLFW.GetPrimaryMonitor() == monitor
position = Vec{2, Int}(GLFW.GetMonitorPos(monitor)...)
physicalsize = Vec{2, Int}(GLFW.GetMonitorPhysicalSize(monitor)...)
videomode = GLFW.GetVideoMode(monitor)
sfactor = is_apple() ? 2.0 : 1.0
dpi = Vec(videomode.width * 25.4, videomode.height * 25.4) * sfactor ./ Vec{2, Float64}(physicalsize)
videomode_supported = GLFW.GetVideoModes(monitor)

MonitorProperties(name, isprimary, position, physicalsize, videomode, videomode_supported, dpi, monitor)
end

abstract type AbstractContext end

#this should remain here, maybe, it uses a glframebuffer
mutable struct GLContext <: AbstractContext
window::GLFW.Window
framebuffer::GLFramebuffer
Expand Down