Rename Display to Window.
It makes more sense to be consistent and always call it window.
This commit is contained in:
parent
416a71f744
commit
24da7f45e0
33 changed files with 257 additions and 255 deletions
63
source/Window/DisplayMode.cpp
Normal file
63
source/Window/DisplayMode.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
|
||||
#include <Spectre/Window/DisplayMode.h>
|
||||
#include <Platform/PlatformMisc.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace sp {
|
||||
|
||||
struct DisplayModeCmp
|
||||
{
|
||||
inline bool operator() (const DisplayMode& a, const DisplayMode& b)
|
||||
{
|
||||
if (a.bpp == b.bpp) {
|
||||
if (a.width == b.width) {
|
||||
return a.height > b.height;
|
||||
}
|
||||
return a.width > b.width;
|
||||
}
|
||||
return a.bpp > b.bpp;
|
||||
}
|
||||
};
|
||||
|
||||
DisplayMode::DisplayMode() :
|
||||
width (0),
|
||||
height (0),
|
||||
bpp (0)
|
||||
{
|
||||
}
|
||||
|
||||
DisplayMode::DisplayMode(unsigned int width, unsigned int height, unsigned int bpp) :
|
||||
width (width),
|
||||
height (height),
|
||||
bpp (bpp)
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<DisplayMode> DisplayMode::getFullscreenModes()
|
||||
{
|
||||
static std::vector<DisplayMode> modes;
|
||||
|
||||
if (modes.size() < 1) {
|
||||
|
||||
PlatformMisc::GetDisplayModes(modes);
|
||||
|
||||
// Sort.
|
||||
std::sort(modes.begin(), modes.end(), DisplayModeCmp());
|
||||
|
||||
// remove duplicates
|
||||
modes.erase(std::unique(modes.begin(), modes.end()), modes.end());
|
||||
}
|
||||
return modes;
|
||||
}
|
||||
|
||||
DisplayMode DisplayMode::getDesktopMode()
|
||||
{
|
||||
return PlatformMisc::GetDesktopMode();
|
||||
}
|
||||
|
||||
bool DisplayMode::empty() const
|
||||
{
|
||||
return (width + height + bpp) == 0;
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
26
source/Window/GLContext.cpp
Normal file
26
source/Window/GLContext.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
#include <Spectre/Window/GLContext.h>
|
||||
|
||||
#ifdef SPECTRE_PLATFORM_WIN
|
||||
#include <Platform/Win32/Win32GLContext.h>
|
||||
typedef sp::Win32GLContext ContextType;
|
||||
#elif SPECTRE_PLATFORM_UNIX
|
||||
#include <Platform/Unix/GLXContext.h>
|
||||
typedef sp::GLXContext ContextType;
|
||||
#else
|
||||
#error "No GLContext implementation exists"
|
||||
#endif
|
||||
|
||||
namespace sp {
|
||||
|
||||
GLContext* GLContext::create()
|
||||
{
|
||||
return new ContextType();
|
||||
}
|
||||
|
||||
GLContext::~GLContext()
|
||||
{
|
||||
// Nothing to do.
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
198
source/Window/Window.cpp
Normal file
198
source/Window/Window.cpp
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <Spectre/System/Event.h>
|
||||
#include <Spectre/System/MessageQueue.h>
|
||||
#include <Spectre/System/Log.h>
|
||||
#include <Spectre/Window/Window.h>
|
||||
#include <Spectre/Window/GLContext.h>
|
||||
#include <Spectre/Graphics/Image.h>
|
||||
#include <Platform/PlatformWindow.h>
|
||||
|
||||
#include <Graphics/GL/gl.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
#define CAPTION_DEFAULT "Spectre"
|
||||
#define ICON_DEFAULT "./assets/app.ico"
|
||||
|
||||
Window::Window()
|
||||
{
|
||||
m_caption = CAPTION_DEFAULT;
|
||||
m_fmode = WINDOWED;
|
||||
|
||||
m_context = GLContext::create();
|
||||
m_impl = PlatformWindow::make(this);
|
||||
}
|
||||
|
||||
Window::~Window()
|
||||
{
|
||||
delete m_context;
|
||||
delete m_impl;
|
||||
}
|
||||
|
||||
bool Window::create(WindowDescription description)
|
||||
{
|
||||
destroy();
|
||||
|
||||
if (!m_impl->create(description)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_context->create(m_impl)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
m_description = description;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Window::init()
|
||||
{
|
||||
m_impl->setCaption(m_caption);
|
||||
|
||||
setIcon(ICON_DEFAULT);
|
||||
|
||||
activate(true);
|
||||
|
||||
enableVSync(false);
|
||||
showCursor(true);
|
||||
}
|
||||
|
||||
void Window::destroy()
|
||||
{
|
||||
m_context->destroy();
|
||||
|
||||
m_impl->destroy();
|
||||
}
|
||||
|
||||
void Window::setCaption(const std::string& caption)
|
||||
{
|
||||
m_caption = caption;
|
||||
m_impl->setCaption(m_caption);
|
||||
}
|
||||
|
||||
const std::string& Window::getCaption() const
|
||||
{
|
||||
return m_caption;
|
||||
}
|
||||
|
||||
void Window::setIcon(const std::string& filename)
|
||||
{
|
||||
Image img;
|
||||
|
||||
if (img.loadFromFile(filename)) {
|
||||
setIcon(img.getWidth(), img.getHeight(), img.getPixels());
|
||||
}
|
||||
}
|
||||
|
||||
void Window::setIcon(unsigned int width, unsigned int height, const uint8_t *pixels)
|
||||
{
|
||||
m_impl->setIcon(width, height, pixels);
|
||||
}
|
||||
|
||||
void Window::setSize(unsigned int width, unsigned int height)
|
||||
{
|
||||
m_description.mode.width = width;
|
||||
m_description.mode.height = height;
|
||||
|
||||
m_impl->setSize(width, height);
|
||||
}
|
||||
|
||||
sp::Vector2u Window::getSize() const
|
||||
{
|
||||
return m_impl->getSize();
|
||||
}
|
||||
|
||||
void Window::setVideoMode(Mode mode)
|
||||
{
|
||||
if (m_fmode == mode) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode != FULLSCREEN) {
|
||||
m_impl->exitFullscreen();
|
||||
}
|
||||
|
||||
// Cache window position when entering fullscreen.
|
||||
if (mode == FULLSCREEN || mode == WINDOWEDFULLSCREEN) {
|
||||
m_cachePos = m_impl->getPosition();
|
||||
}
|
||||
|
||||
// True fullscreen
|
||||
if (mode == FULLSCREEN) {
|
||||
m_impl->enterFullscreen(m_description.mode);
|
||||
}
|
||||
// Windowed fullscreen.
|
||||
else if (mode == WINDOWEDFULLSCREEN) {
|
||||
DisplayMode desktop = DisplayMode::getDesktopMode();
|
||||
m_impl->setDecoration(WindowDecorate::Empty);
|
||||
m_impl->setSize(desktop.width, desktop.height);
|
||||
m_impl->setPosition(0, 0);
|
||||
}
|
||||
// Window mode.
|
||||
else {
|
||||
// Set stored decoration.
|
||||
m_impl->setDecoration(m_description.decoration);
|
||||
|
||||
// Restore size and position.
|
||||
m_impl->setSize(m_description.mode.width, m_description.mode.height);
|
||||
m_impl->setPosition(m_cachePos.x, m_cachePos.y);
|
||||
}
|
||||
|
||||
m_fmode = mode;
|
||||
}
|
||||
|
||||
enum Window::Mode Window::getVideoMode() const
|
||||
{
|
||||
return m_fmode;
|
||||
}
|
||||
|
||||
void Window::setVisible(bool visible)
|
||||
{
|
||||
m_impl->setVisible(visible);
|
||||
}
|
||||
|
||||
void Window::showCursor(bool value)
|
||||
{
|
||||
m_impl->showCursor(value);
|
||||
}
|
||||
|
||||
void Window::grabCursor(bool value)
|
||||
{
|
||||
m_impl->grabCursor(value);
|
||||
}
|
||||
|
||||
bool Window::activate(bool value)
|
||||
{
|
||||
if (value) {
|
||||
return m_context->activate();
|
||||
}
|
||||
return m_context->deactivate();
|
||||
}
|
||||
|
||||
bool Window::enableVSync(bool value)
|
||||
{
|
||||
return m_context->setSwapInterval(value ? 1 : 0);
|
||||
}
|
||||
|
||||
void Window::swapBuffers()
|
||||
{
|
||||
if (activate(true)) {
|
||||
m_context->swapBuffers();
|
||||
}
|
||||
}
|
||||
|
||||
void Window::onReshape(int width, int height)
|
||||
{
|
||||
// TODO: This should even not be here.
|
||||
// Generic Display should not have any GL calls.
|
||||
// But it's better to have it here then in the platform specific GLContext
|
||||
if (activate(true)) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
18
source/Window/WindowDescription.cpp
Normal file
18
source/Window/WindowDescription.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
#include <Spectre/Window/WindowDescription.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
WindowDescription::WindowDescription() :
|
||||
mode (),
|
||||
decoration (WindowDecorate::Default)
|
||||
{
|
||||
}
|
||||
|
||||
WindowDescription::WindowDescription(DisplayMode mode, unsigned decoration) :
|
||||
mode (mode),
|
||||
decoration (decoration)
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
Loading…
Add table
Add a link
Reference in a new issue