first glfw window for osx

This commit is contained in:
2018-09-19 19:36:45 +07:00
parent 20d4c1f44b
commit d9b57d9679
8 changed files with 243 additions and 13 deletions

View File

@@ -85,6 +85,17 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO)
#
# 3rd party
#
if(APPLE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
add_subdirectory(modules/glfw)
endif(APPLE)
#
# subdirectories
#

View File

@@ -10,10 +10,23 @@
namespace e2d
{
class bad_window_operation final : public exception {
public:
const char* what() const noexcept final {
return "bad window operation";
}
};
class window final : public module<window> {
public:
window();
window(const v2u& size, str_view title, bool fullscreen);
~window() noexcept;
v2u real_size() const noexcept;
v2u virtual_size() const noexcept;
bool should_close() const noexcept;
static bool poll_events() noexcept;
private:
class state;
std::unique_ptr<state> state_;

View File

@@ -42,6 +42,7 @@ function(add_e2d_sample NAME)
if(APPLE)
target_link_libraries(${SAMPLE_NAME}
glfw
${Cocoa}
${IOKit}
${CoreVideo}

View File

@@ -5,3 +5,11 @@
******************************************************************************/
#include "../common.hpp"
using namespace e2d;
int main() {
window w{{640, 480}, "Enduro2D", false};
while ( !w.should_close() ) {
window::poll_events();
}
}

View File

@@ -5,15 +5,3 @@
******************************************************************************/
#include <enduro2d/core/window.hpp>
namespace e2d
{
class window::state final : private e2d::noncopyable {
public:
};
window::window()
: state_(new state()){}
window::~window() noexcept = default;
}

View File

@@ -0,0 +1,24 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018 Matvey Cherevko
******************************************************************************/
#pragma once
#include <enduro2d/core/window.hpp>
#define E2D_WINDOW_MODE_NONE 1
#define E2D_WINDOW_MODE_GLFW 2
#ifndef E2D_FILES_MODE
# if defined(E2D_PLATFORM) && E2D_PLATFORM == E2D_PLATFORM_IOS
# define E2D_WINDOW_MODE E2D_WINDOW_MODE_NONE
# elif defined(E2D_PLATFORM) && E2D_PLATFORM == E2D_PLATFORM_LINUX
# define E2D_WINDOW_MODE E2D_WINDOW_MODE_NONE
# elif defined(E2D_PLATFORM) && E2D_PLATFORM == E2D_PLATFORM_MACOSX
# define E2D_WINDOW_MODE E2D_WINDOW_MODE_GLFW
# elif defined(E2D_PLATFORM) && E2D_PLATFORM == E2D_PLATFORM_WINDOWS
# define E2D_WINDOW_MODE E2D_WINDOW_MODE_NONE
# endif
#endif

View File

@@ -0,0 +1,138 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018 Matvey Cherevko
******************************************************************************/
#include "window.hpp"
#if defined(E2D_WINDOW_MODE) && E2D_WINDOW_MODE == E2D_WINDOW_MODE_GLFW
#include <3rdparty/glfw/glfw3.h>
namespace
{
using namespace e2d;
class glfw_state;
using glfw_state_ptr = std::shared_ptr<glfw_state>;
class glfw_state : private noncopyable {
public:
glfw_state() {
if ( !glfwInit() ) {
throw bad_window_operation();
}
}
~glfw_state() noexcept {
glfwTerminate();
}
public:
static bool poll_events() noexcept {
std::lock_guard<std::mutex> guard(mutex_);
if ( shared_state_ ) {
glfwPollEvents();
return true;
}
return false;
}
static glfw_state_ptr get_shared_state() {
std::lock_guard<std::mutex> guard(mutex_);
if ( !shared_state_ ) {
shared_state_ = std::make_shared<glfw_state>();
}
return shared_state_;
}
private:
static std::mutex mutex_;
static std::shared_ptr<glfw_state> shared_state_;
};
std::mutex glfw_state::mutex_;
std::shared_ptr<glfw_state> glfw_state::shared_state_;
}
namespace e2d
{
class window::state final : private e2d::noncopyable {
public:
using window_uptr = std::unique_ptr<
GLFWwindow, void(*)(GLFWwindow*)>;
glfw_state_ptr shared_state;
window_uptr window;
v2u virtual_size;
public:
state(const v2u& size, str_view title, bool fullscreen)
: shared_state(glfw_state::get_shared_state())
, window(open_window_(size, make_utf8(title), fullscreen))
, virtual_size(size)
{
if ( !window ) {
throw bad_window_operation();
}
}
~state() noexcept {
// reset window before shared state
window.reset();
}
private:
static window_uptr open_window_(
const v2u& size, const str& title, bool fullscreen) noexcept
{
if ( fullscreen ) {
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
if ( !monitor ) {
return {nullptr, glfwDestroyWindow};
}
const GLFWvidmode* video_mode = glfwGetVideoMode(monitor);
if ( !video_mode ) {
return {nullptr, glfwDestroyWindow};
}
GLFWwindow* w = glfwCreateWindow(
video_mode->width,
video_mode->height,
title.c_str(),
monitor,
nullptr);
return {w, glfwDestroyWindow};
} else {
GLFWwindow* w = glfwCreateWindow(
math::numeric_cast<int>(size.x),
math::numeric_cast<int>(size.y),
title.c_str(),
nullptr,
nullptr);
return {w, glfwDestroyWindow};
}
}
};
window::window(const v2u& size, str_view title, bool fullscreen)
: state_(new state(size, title, fullscreen)) {}
window::~window() noexcept = default;
v2u window::real_size() const noexcept {
E2D_ASSERT(state_->window);
int w = 0, h = 0;
glfwGetWindowSize(state_->window.get(), &w, &h);
return make_vec2(w,h).cast_to<u32>();
}
v2u window::virtual_size() const noexcept {
return state_->virtual_size;
}
bool window::should_close() const noexcept {
E2D_ASSERT(state_->window);
return glfwWindowShouldClose(state_->window.get());
}
bool window::poll_events() noexcept {
return glfw_state::poll_events();
}
}
#endif

View File

@@ -0,0 +1,47 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018 Matvey Cherevko
******************************************************************************/
#include "window.hpp"
#if defined(E2D_WINDOW_MODE) && E2D_WINDOW_MODE == E2D_WINDOW_MODE_NONE
namespace e2d
{
class window::state final : private e2d::noncopyable {
public:
v2u size;
str title;
bool fullscreen = false;
public:
state(const v2u& size, str_view title, bool fullscreen)
: size(size)
, title(make_utf8(title))
, fullscreen(fullscreen) {}
~state() noexcept = default;
};
window::window(const v2u& size, str_view title, bool fullscreen)
: state_(new state(size, title, fullscreen)) {}
window::~window() noexcept = default;
v2u window::real_size() const noexcept {
return state_->size;
}
v2u window::virtual_size() const noexcept {
return state_->size;
}
bool window::should_close() const noexcept {
return false;
}
bool window::poll_events() noexcept {
return false;
}
}
#endif