aboutsummaryrefslogtreecommitdiff
path: root/dxwindow/src/dxwindow.cc
blob: 8848a7ec48408a5067d438f73eb3f3c581b2aa42 (plain)
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
#include "dxwindow.h"

#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>

#include <cassert>
#include <cstdio>

namespace dx
{

static char glfw_error[1024] = {};

static void glfw_error_callback(int error, const char* description)
{
    sprintf_s(glfw_error, sizeof(glfw_error),
        "GLFW error %d: %s", error, description);
}

Window::~Window()
{
    if (m_window != nullptr)
    {
        glfwDestroyWindow(m_window);
    }
}

bool Window::Initialise(int width, int height, const char* title)
{
    // GLFW by default creates an OpenGL context with the window.
    // Use GLFW_NO_API to tell it not to do so.
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);

    if ((m_window = glfwCreateWindow(
        width, height, title, /*monitor=*/NULL, /*share=*/NULL)) == nullptr)
    {
        return false;
    }

    return true;
}

HWND Window::GetWindowHandle()
{
    if (!m_window)
    {
        return NULL;
    }
    return glfwGetWin32Window(m_window);
}

void Window::Update()
{
    assert(m_window);
    glfwPollEvents();
}

bool Window::ShouldClose() const
{
    assert(m_window);
    return glfwWindowShouldClose(m_window) == GLFW_TRUE;
}

bool WindowInitialise()
{
    glfwSetErrorCallback(glfw_error_callback);
    return glfwInit() == GLFW_TRUE;
}

void WindowTerminate()
{
    glfwTerminate();
}

const char* GetWindowError()
{
    return glfw_error;
}

}  // namespace dx