use
OpenGL::GLFW
qw/ glfwInit glfwGetVersionString glfwTerminate NULL GLFW_TRUE GLFW_FALSE
glfwGetPrimaryMonitor glfwCreateWindow glfwMakeContextCurrent glfwDestroyWindow
glfwSwapInterval glfwSwapBuffers glfwPollEvents
glfwWindowHint GLFW_VISIBLE GLFW_DECORATED GLFW_MAXIMIZED GLFW_DOUBLEBUFFER
/
;
our
$VERSION
=
'0.110'
;
our
$glfw_init
;
our
%instances
;
sub
new {
my
$class
=
shift
;
my
%opts
=
ref
$_
[0] eq
'HASH'
? %{
$_
[0]} :
@_
;
(
$glfw_init
//= glfwInit)
or croak
"GLFW Initialization Failed"
;
my
$self
=
bless
{},
$class
;
glfwWindowHint(GLFW_VISIBLE, (
$opts
{visible} // 1)? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_DECORATED,
$opts
{noframe}? GLFW_FALSE : GLFW_TRUE);
my
$w
= glfwCreateWindow(
$opts
{width} // 640,
$opts
{height} // 480,
$opts
{title} //
'OpenGL'
,
$opts
{fullscreen}? glfwGetPrimaryMonitor() : NULL,
NULL
) or croak
"glfwCreateWindow failed"
;
$self
->{window}=
$w
;
glfwSetWindowPos(
$w
,
$opts
{x}//0,
$opts
{y}//0)
if
$opts
{x} ||
$opts
{y};
glfwMakeContextCurrent(
$w
);
glfwSwapInterval(1)
if
$opts
{vsync} // 1;
weaken(
$instances
{
$self
}=
$self
);
return
$self
;
}
sub
DESTROY {
my
$self
=
shift
;
glfwDestroyWindow(
delete
$self
->{window})
if
defined
$self
->{window};
delete
$instances
{
$self
};
}
END {
for
(
values
%instances
) {
glfwDestroyWindow(
delete
$_
->{window})
if
defined
$_
->{window};
}
glfwTerminate
if
$glfw_init
;
}
sub
window {
shift
->{window} }
sub
context_info {
my
$self
=
shift
;
sprintf
(
"OpenGL::GLFW %s, glfw version %s, OpenGL version %s\n"
,
OpenGL::GLFW->VERSION, glfwGetVersionString(), glGetString(GL_VERSION));
}
sub
swap_buffers {
my
$self
=
shift
;
if
(
$self
->window) {
glfwSwapBuffers(
$self
->window);
glfwPollEvents;
}
}
1;