add fullscreen mode

This commit is contained in:
Hartmut Seichter 2019-01-10 11:26:31 +01:00
parent b2b12b64ab
commit 5160ec4b0b
6 changed files with 183 additions and 120 deletions

View file

@ -11,7 +11,8 @@ void script_system::load(sol::table &ns)
"update",&window::update,
"title",sol::writeonly_property(&window::set_title),
"size",sol::property(&window::size,&window::set_size),
"position",sol::property(&window::position,&window::set_position)
"position",sol::property(&window::position,&window::set_position),
"fullscreen",sol::property(&window::fullscreen,&window::set_fullscreen)
);
ns.new_usertype<input>("input",
@ -19,6 +20,7 @@ void script_system::load(sol::table &ns)
"get",&input::get,
"mouse_position",sol::readonly_property(&input::mouse_position),
"mouse_button",sol::readonly_property(&input::mouse_button),
"mouse_pressed",sol::readonly_property(&input::mouse_pressed),
"input_string",sol::readonly_property(&input::input_string)
);
}

View file

@ -78,6 +78,7 @@ do
if (pw.input:get().mouse_button == 1) then
print(pw.input:get().mouse_position.x,pw.input:get().mouse_position.y)
-- w.fullscreen = not w.fullscreen
end

View file

@ -12,8 +12,13 @@ public:
static input& get();
point mouse_position() const { return _mouse_position; }
int mouse_button() const { return _mouse_button; }
std::string input_string() const { return _input_string; }
bool mouse_pressed() const { return _mouse_pressed; }
int mouse_button() const { return _mouse_button; }
std::string input_string() const { return _input_string; }
~input() = default;
@ -23,6 +28,8 @@ public:
released
};
void reset();
protected:
friend class window;
@ -33,6 +40,7 @@ private:
point _mouse_position;
int _mouse_button;
bool _mouse_pressed;
std::string _input_string;

View file

@ -18,13 +18,17 @@ public:
void set_title(const std::string& title);
void set_size(const size& s);
size size() const;
pw::size size() const;
void set_position(const point& p);
point position() const;
typedef void drop_callback;
bool fullscreen() const;
void set_fullscreen(bool use_fullscreen);
protected:
struct impl;

View file

@ -12,5 +12,11 @@ input &input::get()
return instance;
}
void input::reset()
{
_input_string.clear();
_mouse_button = 0;
}
}

View file

@ -16,10 +16,10 @@ namespace pw {
struct window_context : context
{
virtual bool make_current() override;
virtual void resize() override;
// virtual context::size size() override;
virtual void flush() override;
virtual bool make_current() override;
virtual void resize() override;
// virtual context::size size() override;
virtual void flush() override;
};
bool window_context::make_current()
@ -36,158 +36,188 @@ void window_context::flush()
struct window::impl {
GLFWwindow *_window = nullptr;
GLFWwindow *_window = nullptr;
// window_context _context;
sizei _old_size;
pointi _old_pos;
static void drop_callback(GLFWwindow* window, int count, const char** paths)
{
// std::cout << __FUNCTION__ << std::endl;
// for (int i = 0; i < count; i++)
// std::cout << "\t" << paths[i] << std::endl;
}
// window_context _context;
static void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
std::cout << __FUNCTION__ << std::endl;
}
static void drop_callback(GLFWwindow* window, int count, const char** paths)
{
// std::cout << __FUNCTION__ << std::endl;
// for (int i = 0; i < count; i++)
// std::cout << "\t" << paths[i] << std::endl;
}
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
input::get()._mouse_button = button;
static void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
std::cout << __FUNCTION__ << std::endl;
}
std::cout << __FUNCTION__ << " " << button << " " << action << " " << mods << std::endl;
// input::get()._mouse_position
}
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
input::get()._mouse_button = button;
static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos)
{
input::get()._mouse_position = point(xpos,ypos);
}
std::cout << __FUNCTION__ << " " << button << " " << action << " " << mods << std::endl;
// input::get()._mouse_position
}
static void key_callback(GLFWwindow *window,int key, int scancode, int action, int mods)
{
std::cout << __FUNCTION__ << std::endl;
}
static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos)
{
input::get()._mouse_position = point(xpos,ypos);
}
// static void character_callback(GLFWwindow* window, unsigned int codepoint)
// {
// std::cout << __FUNCTION__ << std::endl;
// }
static void key_callback(GLFWwindow *window,int key, int scancode, int action, int mods)
{
std::cout << __FUNCTION__ << std::endl;
}
static void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
{
// build the string from a Unicode code point
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
std::string u8str = converter.to_bytes(codepoint);
// static void character_callback(GLFWwindow* window, unsigned int codepoint)
// {
// std::cout << __FUNCTION__ << std::endl;
// }
input::get()._input_string = u8str;
}
static void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
{
// build the string from a Unicode code point
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
std::string u8str = converter.to_bytes(codepoint);
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
window::impl* impl = static_cast<window::impl*>(glfwGetWindowUserPointer(window));
// impl->on_resize(width,height);
input::get()._input_string = u8str;
}
// std::cout << "framebuffer " << width << "x" << height << std::endl;
// glViewport(0, 0, width, height);
}
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
window::impl* impl = static_cast<window::impl*>(glfwGetWindowUserPointer(window));
// impl->on_resize(width,height);
impl()
{
glfwInit();
// std::cout << "framebuffer " << width << "x" << height << std::endl;
// glViewport(0, 0, width, height);
}
_window = glfwCreateWindow(640, 480, "pixwerxs", nullptr, nullptr);
impl()
{
glfwInit();
glfwSetWindowUserPointer(_window,this);
_window = glfwCreateWindow(640, 480, "pixwerxs", nullptr, nullptr);
glfwSetFramebufferSizeCallback(_window, window::impl::framebuffer_size_callback);
glfwSetKeyCallback(_window, window::impl::key_callback);
// glfwSetCharCallback(_window, character_callback);
glfwSetCharModsCallback(_window, charmods_callback);
glfwSetScrollCallback(_window, scroll_callback);
glfwSetWindowUserPointer(_window,this);
glfwSetDropCallback(_window, drop_callback);
glfwSetFramebufferSizeCallback(_window, window::impl::framebuffer_size_callback);
glfwSetKeyCallback(_window, window::impl::key_callback);
// glfwSetCharCallback(_window, character_callback);
glfwSetCharModsCallback(_window, charmods_callback);
glfwSetScrollCallback(_window, scroll_callback);
glfwSetCursorPosCallback(_window, cursor_pos_callback);
glfwSetMouseButtonCallback(_window, mouse_button_callback);
glfwSetScrollCallback(_window, scroll_callback);
glfwSetDropCallback(_window, drop_callback);
glfwSetCursorPosCallback(_window, cursor_pos_callback);
glfwSetMouseButtonCallback(_window, mouse_button_callback);
glfwSetScrollCallback(_window, scroll_callback);
#if 0
glfwMakeContextCurrent(_window);
glfwMakeContextCurrent(_window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
#endif
}
}
~impl()
{
glfwDestroyWindow(_window);
}
~impl()
{
glfwDestroyWindow(_window);
}
bool update()
{
if (!glfwWindowShouldClose(_window)) {
bool update()
{
if (!glfwWindowShouldClose(_window)) {
glfwPollEvents();
// reset input
input::get().reset();
// do other stuff
// get new events
glfwPollEvents();
// do other stuff
#if 0
glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
#endif
glfwSwapBuffers(_window);
glfwSwapBuffers(_window);
return true;
}
return true;
}
return false;
return false;
}
}
void set_title(const std::string& title)
{
glfwSetWindowTitle(_window,title.c_str());
}
void set_title(const std::string& title)
{
glfwSetWindowTitle(_window,title.c_str());
}
void set_size(int w,int h)
{
glfwSetWindowSize(_window,w,h);
}
void set_size(int w,int h)
{
glfwSetWindowSize(_window,w,h);
}
::pw::size size() const
{
int w,h;
glfwGetWindowSize(_window,&w,&h);
return ::pw::size(w,h);
}
::pw::size size() const
{
int w,h;
glfwGetWindowSize(_window,&w,&h);
return ::pw::size(w,h);
}
::pw::point position() const
{
int x,y;
glfwGetWindowPos(_window,&x,&y);
return ::pw::point(x,y);
}
::pw::point position() const
{
int x,y;
glfwGetWindowPos(_window,&x,&y);
return ::pw::point(x,y);
}
void set_position(int x,int y)
{
glfwSetWindowPos(_window,x,y);
}
void set_position(int x,int y)
{
glfwSetWindowPos(_window,x,y);
}
void set_fullscreen()
{
// glfwSetWindow
}
void set_fullscreen(bool use_fullscreen)
{
if (fullscreen() == use_fullscreen)
return;
if (use_fullscreen)
{
glfwGetWindowPos( _window, &_old_pos.x, &_old_pos.y );
glfwGetWindowSize( _window, &_old_size.width, &_old_size.height );
GLFWmonitor * monitor = glfwGetPrimaryMonitor();
const GLFWvidmode * mode = glfwGetVideoMode(monitor);
glfwSetWindowMonitor( _window, monitor, 0, 0, mode->width, mode->height, 0 );
} else
{
glfwSetWindowMonitor( _window, nullptr, _old_pos.x,_old_pos.y, _old_size.width,_old_size.height,0);
}
// glfwSetWindow
}
bool fullscreen() const {
return glfwGetWindowMonitor(_window) != nullptr;
}
};
window::window()
: _impl(std::make_unique<window::impl>())
: _impl(std::make_unique<window::impl>())
{
}
@ -197,34 +227,46 @@ window::~window()
bool window::update()
{
return _impl->update();
return _impl->update();
}
void window::set_title(const std::string& title)
{
_impl->set_title(title);
_impl->set_title(title);
}
void window::set_size(const ::pw::size& s)
{
_impl->set_size(static_cast<int>(std::round(s.width)),static_cast<int>(std::round(s.height)));
_impl->set_size(static_cast<int>(std::round(s.width)),static_cast<int>(std::round(s.height)));
}
size window::size() const
{
return _impl->size();
return _impl->size();
}
void window::set_position(const point &p)
{
_impl->set_position(p.x,p.y);
_impl->set_position(p.x,p.y);
}
point window::position() const
{
return _impl->position();
return _impl->position();
}
bool window::fullscreen() const
{
return _impl->fullscreen();
}
void window::set_fullscreen(bool use_fullscreen)
{
_impl->set_fullscreen(use_fullscreen);
}
}