This repository has been archived by the owner on Mar 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Window.m
94 lines (78 loc) · 3.25 KB
/
Window.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
classdef Window < handle
properties (SetAccess = private)
monitor % Monitor containing the window
size % Size [width, height] (pixels)
handle % GLFW window handle
end
methods
% Constructs a window with the optionally provided size. By default the window occupies the fullscreen of the
% primary monitor but an optional fullscreen and monitor argument enable windowed mode and/or selection of a
% secondary monitor for the window.
%
% Typical usage:
% % Windowed mode
% window = Window([640, 480], false);
%
% % Fullscreen mode on primary monitor
% window = Window();
function obj = Window(size, fullscreen, monitor, varargin)
if nargin < 1
size = [640, 480];
end
if nargin < 2
fullscreen = true;
end
if nargin < 3
monitor = Monitor(1);
end
ip = inputParser();
ip.addParameter('refreshRate', 60);
ip.addParameter('visible', GL.TRUE);
ip.parse(varargin{:});
glfwInit();
glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GL.TRUE);
glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW.GLFW_RESIZABLE, GL.FALSE);
glfwWindowHint(GLFW.GLFW_REFRESH_RATE, ip.Results.refreshRate);
glfwWindowHint(GLFW.GLFW_VISIBLE, ip.Results.visible);
if fullscreen
obj.handle = glfwCreateWindow(size(1), size(2), 'Stage', monitor.handle, []);
else
obj.handle = glfwCreateWindow(size(1), size(2), 'Stage', [], []);
end
if ~obj.handle
error('Unable to create window. Verify your drivers support OpenGL 3.2+.');
end
obj.monitor = monitor;
glfwSetInputMode(obj.handle, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);
end
function s = get.size(obj)
[w, h] = glfwGetWindowSize(obj.handle);
s = [w, h];
end
% Swaps the front and back buffers of this window.
function flip(obj)
glfwSwapBuffers(obj.handle);
end
% Processes keyboard and mouse events received by this window.
function pollEvents(obj) %#ok<MANU>
glfwPollEvents();
end
% Gets the last polled state of the specified keyboard key while this window had focus. See GLFW.m for key codes.
function s = getKeyState(obj, key)
s = glfwGetKey(obj.handle, key);
end
function delete(obj)
if obj.handle
% FIXME: Why is Matlab throwing 'Unexpected unknown exception from MEX file..' here?
try
glfwDestroyWindow(obj.handle);
catch
glfwDestroyWindow(obj.handle);
end
end
end
end
end