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

Added GLFW specific functionality from GLWindow.jl #118

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
deps/*
!deps/build.jl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this new line.

236 changes: 236 additions & 0 deletions src/glfw3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,116 @@ end
Base.show(io::IO, m::Monitor) = write(io, "Monitor($(m.handle == C_NULL ? m.handle : GetMonitorName(m)))")

const WindowHandle = Ptr{Void}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this new line.

type Window
handle::WindowHandle
callbacks::Vector{Function}
end
Window(handle::WindowHandle) = Window(handle, Vector{Function}(_window_callbacks_len[]))
#Came from GLWindow.jl/screen.jl
"""
Function to create a pure GLFW OpenGL window
"""
function Window(
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
)
WindowHint(VISIBLE, visible)
WindowHint(FOCUSED, focus)
for ch in contexthints
WindowHint(ch[1], ch[2])
end
for wh in windowhints
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

WindowHint(OPENGL_DEBUG_CONTEXT, Cint(debugging))

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

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

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

MakeContextCurrent(window)

debugging && glDebugMessageCallbackARB(_openglerrorcallback, C_NULL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glDebugMessageCallbackARB appears to be undefined. This line might need to be removed.

window
end


import Base.==; ==(x::Window, y::Window) = (x.handle == y.handle)
Base.cconvert(::Type{WindowHandle}, window::Window) = window.handle
Base.cconvert(::Type{Window}, handle::WindowHandle) = ccall( (:glfwGetWindowUserPointer, lib), Ref{Window}, (WindowHandle,), handle)
Base.hash(window::Window, h::UInt64) = hash(window.handle, h)
function Base.isopen(window::Window)
window.handle == C_NULL && return false
!WindowShouldClose(window)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this function (for now). "is open" and "should not close" are two different concepts in my opinion, and the alternative can still be done in one line.


function Base.resize!(x::Window, w::Integer, h::Integer)
SetWindowSize(x, w, h)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this function (for now). The documented description of resize! is for collections, so using it here seems a bit off. The alternative (calling SetWindowSize) isn't any longer.


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

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

function swapbuffers(window::Window)
window.handle == C_NULL && return
SwapBuffers(window)
return
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this function. It's not adding enough value in my opinion.


"""
Sets visibility of OpenGL window. Will still render if not visible.
Only applies to the root screen holding the opengl context.
"""
function set_visibility!(screen::Window, visible::Bool)
if visible
ShowWindow(screen)
else !visible
HideWindow(screen)
end
return
end

immutable Cursor
handle::Ptr{Void}
Expand All @@ -298,6 +399,39 @@ immutable GLFWError <: Exception
end
Base.showerror(io::IO, e::GLFWError) = print(io, "GLFWError ($(e.code)): ", e.description)

#Came from GLWindow.jl/types.jl
struct MonitorProperties
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change struct to immutable for Julia 0.5 compatibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This request was made irrelevant by #121. However, I couldn't get GitHub to resolve merges through it's UI, so I did it manually and push it to #122.

name::String
isprimary::Bool
position::Vector{Int}
physicalsize::Vector{Int}
videomode::VidMode
videomode_supported::Vector{VidMode}
dpi::Vector{Float64}
monitor::Monitor
end

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

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

#Came from GLWindow/core.jl
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

#************************************************************************
# GLFW API functions
#************************************************************************
Expand Down Expand Up @@ -473,3 +607,105 @@ GetProcAddress(procname::AbstractString) = ccall((:glfwGetProcAddress, lib), Ptr
function SetWindowMonitor(window::Window, monitor::Monitor, xpos, ypos, width, height, refreshRate)
ccall((:glfwSetWindowMonitor, lib), Void, (WindowHandle, Monitor, Cint, Cint, Cint, Cint, Cint), window, monitor, xpos, ypos, width, height, refreshRate)
end

#came from GLWindow/core.jl
"""
Returns the monitor resolution of the primary monitor.
"""
function primarymonitorresolution()
props = MonitorProperties(GetPrimaryMonitor())
w,h = props.videomode.width, props.videomode.height
# Vec(Int(w),Int(h))
Vector([Int(w),Int(h)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just return (Int(w), Int(h))

end

#Came from GLWindow.jl/screen.jl


#question: Is this correct?
"""
Takes a Window and registers a list of callback functions.
Returns a Dict{Symbol, Any}(name_of_callback => signal)
"""
function register_callbacks(window::Window, callbacks::Vector{Function})
tmp = map(callbacks) do f
(Symbol(last(split(string(f),"."))), f(window))
end
Dict{Symbol, Any}(tmp)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this function (for now). I think it's a little too magical.


"""
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


"""
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 = OPENGL_CORE_PROFILE
else
profile = OPENGL_ANY_PROFILE
end
[
(CONTEXT_VERSION_MAJOR, major),
(CONTEXT_VERSION_MINOR, minor),
(OPENGL_FORWARD_COMPAT, Cint(1)),
(OPENGL_PROFILE, profile)
]
end


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

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

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

#question: what is this exactly?
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
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this function. It appears to be unused.


function poll_glfw()
PollEvents()
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this function. It is not adding value.


#Came from: GLWindow/events.jl
function to_arrow_symbol(button_set)
for b in button_set
KEY_RIGHT == b && return :right
KEY_LEFT == b && return :left
KEY_DOWN == b && return :down
KEY_UP == b && return :up
end
return :nothing
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this function. It appears to be unused.