imported Löve GLES branch (changeset 1ba9037e558b)

This commit is contained in:
Martin Felis
2013-12-05 18:05:13 +01:00
parent 41b2db04a7
commit 2644d1ee18
615 changed files with 150300 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "Window.h"
namespace love
{
namespace window
{
Window *Window::singleton = NULL;
Window::~Window()
{
if (singleton == this)
singleton = NULL;
}
void Window::swapBuffers()
{
}
WindowAttributes::WindowAttributes()
: fullscreen(false)
, fstype(Window::FULLSCREEN_TYPE_NORMAL)
, vsync(true)
, fsaa(0)
, resizable(false)
, minwidth(1)
, minheight(1)
, borderless(false)
, centered(true)
, display(0)
{
}
bool Window::getConstant(const char *in, Window::FullscreenType &out)
{
return fullscreenTypes.find(in, out);
}
bool Window::getConstant(Window::FullscreenType in, const char *&out)
{
return fullscreenTypes.find(in, out);
}
bool Window::getConstant(const char *in, Window::Attribute &out)
{
return attributes.find(in, out);
}
bool Window::getConstant(Window::Attribute in, const char *&out)
{
return attributes.find(in, out);
}
StringMap<Window::Attribute, Window::ATTRIB_MAX_ENUM>::Entry Window::attributeEntries[] =
{
{"fullscreen", ATTRIB_FULLSCREEN},
{"fullscreentype", ATTRIB_FULLSCREEN_TYPE},
{"vsync", ATTRIB_VSYNC},
{"fsaa", ATTRIB_FSAA},
{"resizable", ATTRIB_RESIZABLE},
{"minwidth", ATTRIB_MIN_WIDTH},
{"minheight", ATTRIB_MIN_HEIGHT},
{"borderless", ATTRIB_BORDERLESS},
{"centered", ATTRIB_CENTERED},
{"display", ATTRIB_DISPLAY}
};
StringMap<Window::Attribute, Window::ATTRIB_MAX_ENUM> Window::attributes(Window::attributeEntries, sizeof(Window::attributeEntries));
StringMap<Window::FullscreenType, Window::FULLSCREEN_TYPE_MAX_ENUM>::Entry Window::fullscreenTypeEntries[] =
{
{"normal", Window::FULLSCREEN_TYPE_NORMAL},
{"desktop", Window::FULLSCREEN_TYPE_DESKTOP},
};
StringMap<Window::FullscreenType, Window::FULLSCREEN_TYPE_MAX_ENUM> Window::fullscreenTypes(Window::fullscreenTypeEntries, sizeof(Window::fullscreenTypeEntries));
} // window
} // love
+160
View File
@@ -0,0 +1,160 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_WINDOW_WINDOW_H
#define LOVE_WINDOW_WINDOW_H
// LOVE
#include "common/Module.h"
#include "common/StringMap.h"
#include "image/ImageData.h"
// C++
#include <string>
#include <vector>
namespace love
{
namespace window
{
// Forward-declared so it can be used in the class methods. We can't define the
// whole thing here because it uses the Window::Type enum.
struct WindowAttributes;
class Window : public Module
{
public:
// Types of window attributes.
enum Attribute
{
ATTRIB_FULLSCREEN,
ATTRIB_FULLSCREEN_TYPE,
ATTRIB_VSYNC,
ATTRIB_FSAA,
ATTRIB_RESIZABLE,
ATTRIB_MIN_WIDTH,
ATTRIB_MIN_HEIGHT,
ATTRIB_BORDERLESS,
ATTRIB_CENTERED,
ATTRIB_DISPLAY,
ATTRIB_MAX_ENUM
};
enum FullscreenType
{
FULLSCREEN_TYPE_NORMAL,
FULLSCREEN_TYPE_DESKTOP,
FULLSCREEN_TYPE_MAX_ENUM
};
struct WindowSize
{
int width;
int height;
};
virtual ~Window();
virtual bool setWindow(int width = 800, int height = 600, WindowAttributes *attribs = 0) = 0;
virtual void getWindow(int &width, int &height, WindowAttributes &attribs) = 0;
virtual bool setFullscreen(bool fullscreen, FullscreenType fstype) = 0;
virtual bool setFullscreen(bool fullscreen) = 0;
virtual bool onWindowResize(int width, int height) = 0;
virtual int getDisplayCount() const = 0;
virtual std::vector<WindowSize> getFullscreenSizes(int displayindex) const = 0;
virtual int getWidth() const = 0;
virtual int getHeight() const = 0;
virtual void getDesktopDimensions(int displayindex, int &width, int &height) const = 0;
virtual bool isCreated() const = 0;
virtual void setWindowTitle(const std::string &title) = 0;
virtual const std::string &getWindowTitle() const = 0;
virtual bool setIcon(love::image::ImageData *imgd) = 0;
virtual love::image::ImageData *getIcon() = 0;
// default no-op implementation
virtual void swapBuffers();
virtual bool hasFocus() const = 0;
virtual bool hasMouseFocus() const = 0;
virtual bool isVisible() const = 0;
virtual void setMouseVisible(bool visible) = 0;
virtual bool getMouseVisible() const = 0;
virtual void setMouseGrab(bool grab) = 0;
virtual bool isMouseGrabbed() const = 0;
virtual const void *getHandle() const = 0;
//virtual static Window *createSingleton() = 0;
//virtual static Window *getSingleton() = 0;
// No virtual statics, of course, but you are supposed to implement these statics.
static bool getConstant(const char *in, Attribute &out);
static bool getConstant(Attribute in, const char *&out);
static bool getConstant(const char *in, FullscreenType &out);
static bool getConstant(FullscreenType in, const char *&out);
protected:
static Window *singleton;
private:
static StringMap<Attribute, ATTRIB_MAX_ENUM>::Entry attributeEntries[];
static StringMap<Attribute, ATTRIB_MAX_ENUM> attributes;
static StringMap<FullscreenType, FULLSCREEN_TYPE_MAX_ENUM>::Entry fullscreenTypeEntries[];
static StringMap<FullscreenType, FULLSCREEN_TYPE_MAX_ENUM> fullscreenTypes;
}; // Window
struct WindowAttributes
{
WindowAttributes();
bool fullscreen; // = false
Window::FullscreenType fstype; // = FULLSCREEN_TYPE_NORMAL
bool vsync; // = true
int fsaa; // = 0
bool resizable; // = false
int minwidth; // = 1
int minheight; // = 1
bool borderless; // = false
bool centered; // = true
int display; // = 0
}; // WindowFlags
} // window
} // love
#endif // LOVE_WINDOW_WINDOW_H
+621
View File
@@ -0,0 +1,621 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "common/config.h"
#include "graphics/Graphics.h"
#include "Window.h"
// C++
#include <iostream>
#include <vector>
#include <algorithm>
namespace love
{
namespace window
{
namespace sdl
{
Window::Window()
: windowTitle("")
, created(false)
, mouseGrabbed(false)
, window(0)
, context(0)
{
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
throw love::Exception("%s", SDL_GetError());
}
Window::~Window()
{
if (curMode.icon)
curMode.icon->release();
if (window)
SDL_DestroyWindow(window);
if (context)
SDL_GL_DeleteContext(context);
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
Window::_currentMode::_currentMode()
: width(800)
, height(600)
, attribs()
, icon(0)
{
}
bool Window::setWindow(int width, int height, WindowAttributes *attribs)
{
graphics::Graphics *gfx = (graphics::Graphics *) Module::findInstance("love.graphics.");
if (gfx)
gfx->unSetMode();
WindowAttributes f;
if (attribs)
f = *attribs;
f.minwidth = std::max(f.minwidth, 1);
f.minheight = std::max(f.minheight, 1);
f.display = std::min(std::max(f.display, 0), getDisplayCount());
// Use the desktop resolution if a width or height of 0 is specified.
if (width == 0 || height == 0)
{
SDL_DisplayMode mode = {};
SDL_GetDesktopDisplayMode(f.display, &mode);
width = mode.w;
height = mode.h;
}
Uint32 sdlflags = SDL_WINDOW_OPENGL;
if (f.fullscreen)
{
if (f.fstype == FULLSCREEN_TYPE_DESKTOP)
sdlflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
else
{
sdlflags |= SDL_WINDOW_FULLSCREEN;
// Fullscreen window creation will bug out if no mode can be used.
SDL_DisplayMode mode = {0, width, height, 0, 0};
if (SDL_GetClosestDisplayMode(f.display, &mode, &mode) == 0)
return false;
}
}
if (f.resizable)
sdlflags |= SDL_WINDOW_RESIZABLE;
if (f.borderless)
sdlflags |= SDL_WINDOW_BORDERLESS;
// Destroy and recreate the window if the dimensions or flags have changed.
if (window)
{
int curdisplay = SDL_GetWindowDisplayIndex(window);
Uint32 wflags = SDL_GetWindowFlags(window);
wflags &= (SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS);
if (sdlflags != wflags || width != curMode.width || height != curMode.height
|| f.display != curdisplay || f.fsaa != curMode.attribs.fsaa)
{
SDL_DestroyWindow(window);
window = 0;
// The old window may have generated pending events which are no
// longer relevant. Destroy them all!
SDL_FlushEvent(SDL_WINDOWEVENT);
}
}
int centeredpos = SDL_WINDOWPOS_CENTERED_DISPLAY(f.display);
int uncenteredpos = SDL_WINDOWPOS_UNDEFINED_DISPLAY(f.display);
if (!window)
{
// In Windows and Linux, some GL attributes are set on window creation.
setWindowGLAttributes(f.fsaa);
const char *title = windowTitle.c_str();
int pos = f.centered ? centeredpos : uncenteredpos;
window = SDL_CreateWindow(title, pos, pos, width, height, sdlflags);
if (!window && f.fsaa > 0)
{
// FSAA might have caused the failure, disable it and try again.
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
window = SDL_CreateWindow(title, pos, pos, width, height, sdlflags);
f.fsaa = 0;
}
// Make sure the window keeps any previously set icon.
if (window && curMode.icon)
setIcon(curMode.icon);
}
if (!window)
{
displayError("Could not create window", SDL_GetError());
return false;
}
// Enforce minimum window dimensions.
SDL_SetWindowMinimumSize(window, f.minwidth, f.minheight);
if (f.centered && !f.fullscreen)
SDL_SetWindowPosition(window, centeredpos, centeredpos);
SDL_RaiseWindow(window);
if (!setContext(f.fsaa, f.vsync))
{
created = false;
return false;
}
created = true;
updateAttributes(f);
if (gfx)
{
if (!gfx->setMode(curMode.width, curMode.height))
displayError("Could not set graphics mode", "Unsupported OpenGL version?");
}
// Make sure the mouse keeps its previous grab setting.
setMouseGrab(mouseGrabbed);
return true;
}
bool Window::onWindowResize(int width, int height)
{
if (!window)
return false;
curMode.width = width;
curMode.height = height;
return true;
}
bool Window::setContext(int fsaa, bool vsync)
{
// We would normally only need to recreate the context if FSAA changes or
// SDL_GL_MakeCurrent is unsuccessful, but in Windows MakeCurrent can
// sometimes claim success but the context will actually be trashed.
if (context)
{
SDL_GL_DeleteContext(context);
context = 0;
}
// Make sure the proper attributes are set.
setWindowGLAttributes(fsaa);
context = SDL_GL_CreateContext(window);
if (!context && fsaa > 0)
{
// FSAA might have caused the failure, disable it and try again.
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
context = SDL_GL_CreateContext(window);
}
if (!context)
{
displayError("Could not create OpenGL context", SDL_GetError());
return false;
}
// Set vertical synchronization.
SDL_GL_SetSwapInterval(vsync ? 1 : 0);
// Verify FSAA setting.
int buffers;
int samples;
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &buffers);
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &samples);
// Don't fail because of this, but issue a warning.
if ((!buffers && fsaa) || (samples != fsaa))
{
std::cerr << "Warning, FSAA setting failed! (Result: buffers: " << buffers << ", samples: " << samples << ")" << std::endl;
fsaa = (buffers > 0) ? samples : 0;
}
curMode.attribs.fsaa = fsaa;
curMode.attribs.vsync = SDL_GL_GetSwapInterval() != 0;
return true;
}
void Window::setWindowGLAttributes(int fsaa) const
{
// Set GL window attributes.
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
// FSAA.
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, (fsaa > 0) ? 1 : 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, (fsaa > 0) ? fsaa : 0);
const char *glesHint = SDL_GetHint("LOVE_GRAPHICS_USE_OPENGLES");
if (glesHint && *glesHint == '1')
{
// Use OpenGL ES 2+.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
}
else
{
// Use desktop OpenGL.
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); // default
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
}
}
void Window::updateAttributes(const WindowAttributes &newattribs)
{
Uint32 wflags = SDL_GetWindowFlags(window);
// Set the new display mode as the current display mode.
SDL_GetWindowSize(window, &curMode.width, &curMode.height);
if ((wflags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
curMode.attribs.fullscreen = true;
curMode.attribs.fstype = FULLSCREEN_TYPE_DESKTOP;
}
else if ((wflags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)
{
curMode.attribs.fullscreen = true;
curMode.attribs.fstype = FULLSCREEN_TYPE_NORMAL;
}
else
{
curMode.attribs.fullscreen = false;
curMode.attribs.fstype = newattribs.fstype;
}
// The min width/height is set to 0 internally in SDL when in fullscreen.
if (curMode.attribs.fullscreen)
{
curMode.attribs.minwidth = newattribs.minwidth;
curMode.attribs.minheight = newattribs.minheight;
}
else
SDL_GetWindowMinimumSize(window, &curMode.attribs.minwidth, &curMode.attribs.minheight);
curMode.attribs.resizable = (wflags & SDL_WINDOW_RESIZABLE) != 0;
curMode.attribs.borderless = (wflags & SDL_WINDOW_BORDERLESS) != 0;
curMode.attribs.centered = newattribs.centered;
curMode.attribs.display = std::max(SDL_GetWindowDisplayIndex(window), 0);
// Only minimize on focus loss if the window is in exclusive-fullscreen
// mode (mimics behaviour of SDL 2.0.2+).
// In OS X we always disable this to prevent dock minimization weirdness.
#ifndef LOVE_MACOSX
if (curMode.attribs.fullscreen && curMode.attribs.fstype == FULLSCREEN_TYPE_NORMAL)
SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "1");
else
#endif
SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
}
void Window::displayError(const std::string &title, const std::string &text) const
{
std::cerr << title << ": " << text << std::endl;
if (!window)
return;
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title.c_str(), text.c_str(), window);
}
void Window::getWindow(int &width, int &height, WindowAttributes &attribs)
{
// Window position may be different from creation - update display index.
if (window)
curMode.attribs.display = std::max(SDL_GetWindowDisplayIndex(window), 0);
width = curMode.width;
height = curMode.height;
attribs = curMode.attribs;
}
bool Window::setFullscreen(bool fullscreen, Window::FullscreenType fstype)
{
if (!window)
return false;
WindowAttributes newattribs = curMode.attribs;
newattribs.fullscreen = fullscreen;
newattribs.fstype = fstype;
Uint32 sdlflags = 0;
if (fullscreen)
{
if (fstype == FULLSCREEN_TYPE_DESKTOP)
sdlflags = SDL_WINDOW_FULLSCREEN_DESKTOP;
else
{
sdlflags = SDL_WINDOW_FULLSCREEN;
SDL_DisplayMode mode = {};
mode.w = curMode.width;
mode.h = curMode.height;
SDL_GetClosestDisplayMode(SDL_GetWindowDisplayIndex(window), &mode, &mode);
SDL_SetWindowDisplayMode(window, &mode);
}
}
if (SDL_SetWindowFullscreen(window, sdlflags) == 0)
{
SDL_GL_MakeCurrent(window, context);
updateAttributes(newattribs);
// Update the viewport size now instead of waiting for event polling.
graphics::Graphics *gfx = (graphics::Graphics *) Module::findInstance("love.graphics.");
if (gfx)
gfx->setViewportSize(curMode.width, curMode.height);
return true;
}
return false;
}
bool Window::setFullscreen(bool fullscreen)
{
return setFullscreen(fullscreen, curMode.attribs.fstype);
}
int Window::getDisplayCount() const
{
return SDL_GetNumVideoDisplays();
}
typedef Window::WindowSize WindowSize;
std::vector<WindowSize> Window::getFullscreenSizes(int displayindex) const
{
std::vector<WindowSize> sizes;
SDL_DisplayMode mode = {};
std::vector<WindowSize>::const_iterator it;
for (int i = 0; i < SDL_GetNumDisplayModes(displayindex); i++)
{
SDL_GetDisplayMode(displayindex, i, &mode);
// SDL2's display mode list has multiple entries for modes of the same
// size with different bits per pixel, so we need to filter those out.
bool alreadyhassize = false;
for (it = sizes.begin(); it != sizes.end(); ++it)
{
if (it->width == mode.w && it->height == mode.h)
{
alreadyhassize = true;
break;
}
}
if (!alreadyhassize)
{
WindowSize w = {mode.w, mode.h};
sizes.push_back(w);
}
}
return sizes;
}
int Window::getWidth() const
{
return curMode.width;
}
int Window::getHeight() const
{
return curMode.height;
}
void Window::getDesktopDimensions(int displayindex, int &width, int &height) const
{
if (displayindex >= 0 && displayindex < getDisplayCount())
{
SDL_DisplayMode mode = {};
SDL_GetDesktopDisplayMode(displayindex, &mode);
width = mode.w;
height = mode.h;
}
else
{
width = 0;
height = 0;
}
}
bool Window::isCreated() const
{
return created;
}
void Window::setWindowTitle(const std::string &title)
{
windowTitle = title;
if (window)
SDL_SetWindowTitle(window, title.c_str());
}
const std::string &Window::getWindowTitle() const
{
return windowTitle;
}
bool Window::setIcon(love::image::ImageData *imgd)
{
if (!imgd)
return false;
imgd->retain();
if (curMode.icon)
curMode.icon->release();
curMode.icon = imgd;
if (!window)
return false;
Uint32 rmask, gmask, bmask, amask;
#ifdef LOVE_BIG_ENDIAN
rmask = 0xFF000000;
gmask = 0x00FF0000;
bmask = 0x0000FF00;
amask = 0x000000FF;
#else
rmask = 0x000000FF;
gmask = 0x0000FF00;
bmask = 0x00FF0000;
amask = 0xFF000000;
#endif
int w = imgd->getWidth();
int h = imgd->getHeight();
int pitch = imgd->getWidth() * 4;
SDL_Surface *sdlicon = 0;
{
// We don't want another thread modifying the ImageData mid-copy.
love::thread::Lock lock(imgd->getMutex());
sdlicon = SDL_CreateRGBSurfaceFrom(imgd->getData(), w, h, 32, pitch, rmask, gmask, bmask, amask);
}
if (!sdlicon)
return false;
SDL_SetWindowIcon(window, sdlicon);
SDL_FreeSurface(sdlicon);
return true;
}
love::image::ImageData *Window::getIcon()
{
return curMode.icon;
}
void Window::swapBuffers()
{
SDL_GL_SwapWindow(window);
}
bool Window::hasFocus() const
{
return (window && SDL_GetKeyboardFocus() == window);
}
bool Window::hasMouseFocus() const
{
return (window && SDL_GetMouseFocus() == window);
}
bool Window::isVisible() const
{
return window && (SDL_GetWindowFlags(window) & SDL_WINDOW_SHOWN) != 0;
}
void Window::setMouseVisible(bool visible)
{
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
}
bool Window::getMouseVisible() const
{
return (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE);
}
void Window::setMouseGrab(bool grab)
{
mouseGrabbed = grab;
if (window)
SDL_SetWindowGrab(window, (SDL_bool) grab);
}
bool Window::isMouseGrabbed() const
{
if (window)
return (bool) SDL_GetWindowGrab(window);
else
return mouseGrabbed;
}
const void *Window::getHandle() const
{
return window;
}
love::window::Window *Window::createSingleton()
{
if (!singleton)
singleton = new Window();
else
singleton->retain();
return singleton;
}
love::window::Window *Window::getSingleton()
{
return singleton;
}
const char *Window::getName() const
{
return "love.window.sdl";
}
} // sdl
} // window
} // love
+126
View File
@@ -0,0 +1,126 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_WINDOW_SDL_WINDOW_H
#define LOVE_WINDOW_SDL_WINDOW_H
// LOVE
#include "window/Window.h"
// SDL
#include <SDL.h>
namespace love
{
namespace window
{
namespace sdl
{
class Window : public love::window::Window
{
public:
Window();
~Window();
bool setWindow(int width = 800, int height = 600, WindowAttributes *attribs = 0);
void getWindow(int &width, int &height, WindowAttributes &attribs);
bool setFullscreen(bool fullscreen, FullscreenType fstype);
bool setFullscreen(bool fullscreen);
bool onWindowResize(int width, int height);
int getDisplayCount() const;
std::vector<WindowSize> getFullscreenSizes(int displayindex) const;
int getWidth() const;
int getHeight() const;
void getDesktopDimensions(int displayindex, int &width, int &height) const;
bool isCreated() const;
void setWindowTitle(const std::string &title);
const std::string &getWindowTitle() const;
bool setIcon(love::image::ImageData *imgd);
love::image::ImageData *getIcon();
void swapBuffers();
bool hasFocus() const;
bool hasMouseFocus() const;
bool isVisible() const;
void setMouseVisible(bool visible);
bool getMouseVisible() const;
void setMouseGrab(bool grab);
bool isMouseGrabbed() const;
const void *getHandle() const;
static love::window::Window *createSingleton();
static love::window::Window *getSingleton();
const char *getName() const;
private:
bool setContext(int fsaa, bool vsync);
void setWindowGLAttributes(int fsaa) const;
// Update the saved window attribs based on the window's actual state.
void updateAttributes(const WindowAttributes &newattribs);
// Shows a warning/error message box.
void displayError(const std::string &title, const std::string &text) const;
std::string windowTitle;
struct _currentMode
{
_currentMode();
int width;
int height;
WindowAttributes attribs;
love::image::ImageData *icon;
} curMode;
bool created;
bool mouseGrabbed;
SDL_Window *window;
SDL_GLContext context;
}; // Window
} // sdl
} // window
} // love
#endif // LOVE_WINDOW_WINDOW_H
+341
View File
@@ -0,0 +1,341 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_Window.h"
#include "sdl/Window.h"
namespace love
{
namespace window
{
static Window *instance = 0;
int w_getDisplayCount(lua_State *L)
{
lua_pushinteger(L, instance->getDisplayCount());
return 1;
}
static const char *attribName(Window::Attribute attrib)
{
const char *name = nullptr;
Window::getConstant(attrib, name);
return name;
}
int w_setMode(lua_State *L)
{
int w = luaL_checkint(L, 1);
int h = luaL_checkint(L, 2);
if (lua_isnoneornil(L, 3))
{
luax_pushboolean(L, instance->setWindow(w, h, 0));
return 1;
}
luaL_checktype(L, 3, LUA_TTABLE);
// We want to error for invalid / misspelled window attributes.
lua_pushnil(L);
while (lua_next(L, 3))
{
if (lua_type(L, -2) != LUA_TSTRING)
return luax_typerror(L, -2, "string");
const char *key = luaL_checkstring(L, -2);
Window::Attribute attrib;
if (!Window::getConstant(key, attrib))
return luaL_error(L, "Invalid window attribute: %s", key);
lua_pop(L, 1);
}
WindowAttributes attribs;
lua_getfield(L, 3, attribName(Window::ATTRIB_FULLSCREEN_TYPE));
if (!lua_isnoneornil(L, -1))
{
const char *typestr = luaL_checkstring(L, -1);
if (!Window::getConstant(typestr, attribs.fstype))
return luaL_error(L, "Invalid fullscreen type: %s", typestr);
}
else
{
// Default to "normal" fullscreen.
attribs.fstype = Window::FULLSCREEN_TYPE_NORMAL;
}
lua_pop(L, 1);
attribs.fullscreen = luax_boolflag(L, 3, attribName(Window::ATTRIB_FULLSCREEN), false);
attribs.vsync = luax_boolflag(L, 3, attribName(Window::ATTRIB_VSYNC), true);
attribs.fsaa = luax_intflag(L, 3, attribName(Window::ATTRIB_FSAA), 0);
attribs.resizable = luax_boolflag(L, 3, attribName(Window::ATTRIB_RESIZABLE), false);
attribs.minwidth = luax_intflag(L, 3, attribName(Window::ATTRIB_MIN_WIDTH), 1);
attribs.minheight = luax_intflag(L, 3, attribName(Window::ATTRIB_MIN_HEIGHT), 1);
attribs.borderless = luax_boolflag(L, 3, attribName(Window::ATTRIB_BORDERLESS), false);
attribs.centered = luax_boolflag(L, 3, attribName(Window::ATTRIB_CENTERED), true);
attribs.display = luax_intflag(L, 3, attribName(Window::ATTRIB_DISPLAY), 1);
// Display index is 1-based in Lua and 0-based internally.
attribs.display--;
EXCEPT_GUARD(luax_pushboolean(L, instance->setWindow(w, h, &attribs));)
return 1;
}
int w_getMode(lua_State *L)
{
int w, h;
WindowAttributes attribs;
instance->getWindow(w, h, attribs);
lua_pushnumber(L, w);
lua_pushnumber(L, h);
lua_newtable(L);
const char *fstypestr = "normal";
Window::getConstant(attribs.fstype, fstypestr);
lua_pushstring(L, fstypestr);
lua_setfield(L, -2, attribName(Window::ATTRIB_FULLSCREEN_TYPE));
luax_pushboolean(L, attribs.fullscreen);
lua_setfield(L, -2, attribName(Window::ATTRIB_FULLSCREEN));
luax_pushboolean(L, attribs.vsync);
lua_setfield(L, -2, attribName(Window::ATTRIB_VSYNC));
lua_pushinteger(L, attribs.fsaa);
lua_setfield(L, -2, attribName(Window::ATTRIB_FSAA));
luax_pushboolean(L, attribs.resizable);
lua_setfield(L, -2, attribName(Window::ATTRIB_RESIZABLE));
lua_pushinteger(L, attribs.minwidth);
lua_setfield(L, -2, attribName(Window::ATTRIB_MIN_WIDTH));
lua_pushinteger(L, attribs.minheight);
lua_setfield(L, -2, attribName(Window::ATTRIB_MIN_HEIGHT));
luax_pushboolean(L, attribs.borderless);
lua_setfield(L, -2, attribName(Window::ATTRIB_BORDERLESS));
luax_pushboolean(L, attribs.centered);
lua_setfield(L, -2, attribName(Window::ATTRIB_CENTERED));
// Display index is 0-based internally and 1-based in Lua.
lua_pushinteger(L, attribs.display + 1);
lua_setfield(L, -2, attribName(Window::ATTRIB_DISPLAY));
return 3;
}
int w_getFullscreenModes(lua_State *L)
{
int displayindex = luaL_optint(L, 1, 1) - 1;
std::vector<Window::WindowSize> modes = instance->getFullscreenSizes(displayindex);
lua_createtable(L, modes.size(), 0);
for (size_t i = 0; i < modes.size(); i++)
{
lua_pushinteger(L, i+1);
lua_createtable(L, 0, 2);
// Inner table attribs.
lua_pushinteger(L, modes[i].width);
lua_setfield(L, -2, "width");
lua_pushinteger(L, modes[i].height);
lua_setfield(L, -2, "height");
// Inner table attribs end.
lua_settable(L, -3);
}
return 1;
}
int w_setFullscreen(lua_State *L)
{
bool fullscreen = luax_toboolean(L, 1);
Window::FullscreenType fstype = Window::FULLSCREEN_TYPE_MAX_ENUM;
const char *typestr = lua_isnoneornil(L, 2) ? 0 : luaL_checkstring(L, 2);
if (typestr && !Window::getConstant(typestr, fstype))
return luaL_error(L, "Invalid fullscreen type: %s", typestr);
bool success = false;
if (fstype == Window::FULLSCREEN_TYPE_MAX_ENUM)
success = instance->setFullscreen(fullscreen);
else
success = instance->setFullscreen(fullscreen, fstype);
luax_pushboolean(L, success);
return 1;
}
int w_getFullscreen(lua_State *L)
{
int w, h;
WindowAttributes attribs;
instance->getWindow(w, h, attribs);
const char *typestr;
if (!Window::getConstant(attribs.fstype, typestr))
luaL_error(L, "Unknown fullscreen type.");
luax_pushboolean(L, attribs.fullscreen);
lua_pushstring(L, typestr);
return 2;
}
int w_isCreated(lua_State *L)
{
luax_pushboolean(L, instance->isCreated());
return 1;
}
int w_getWidth(lua_State *L)
{
lua_pushinteger(L, instance->getWidth());
return 1;
}
int w_getHeight(lua_State *L)
{
lua_pushinteger(L, instance->getHeight());
return 1;
}
int w_getDimensions(lua_State *L)
{
lua_pushinteger(L, instance->getWidth());
lua_pushinteger(L, instance->getHeight());
return 2;
}
int w_getDesktopDimensions(lua_State *L)
{
int width = 0, height = 0;
int displayindex = luaL_optint(L, 1, 1) - 1;
instance->getDesktopDimensions(displayindex, width, height);
lua_pushinteger(L, width);
lua_pushinteger(L, height);
return 2;
}
int w_setIcon(lua_State *L)
{
image::ImageData *i = luax_checktype<image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
luax_pushboolean(L, instance->setIcon(i));
return 1;
}
int w_getIcon(lua_State *L)
{
image::ImageData *i = instance->getIcon();
if (i)
{
i->retain();
luax_pushtype(L, "ImageData", IMAGE_IMAGE_DATA_T, i);
}
else
lua_pushnil(L);
return 1;
}
int w_setTitle(lua_State *L)
{
std::string title = luax_checkstring(L, 1);
instance->setWindowTitle(title);
return 0;
}
int w_getTitle(lua_State *L)
{
luax_pushstring(L, instance->getWindowTitle());
return 1;
}
int w_hasFocus(lua_State *L)
{
luax_pushboolean(L, instance->hasFocus());
return 1;
}
int w_hasMouseFocus(lua_State *L)
{
luax_pushboolean(L, instance->hasMouseFocus());
return 1;
}
int w_isVisible(lua_State *L)
{
luax_pushboolean(L, instance->isVisible());
return 1;
}
static const luaL_Reg functions[] =
{
{ "getDisplayCount", w_getDisplayCount },
{ "setMode", w_setMode },
{ "getMode", w_getMode },
{ "getFullscreenModes", w_getFullscreenModes },
{ "setFullscreen", w_setFullscreen },
{ "getFullscreen", w_getFullscreen },
{ "isCreated", w_isCreated },
{ "getWidth", w_getWidth },
{ "getHeight", w_getHeight },
{ "getDimensions", w_getDimensions },
{ "getDesktopDimensions", w_getDesktopDimensions },
{ "setIcon", w_setIcon },
{ "getIcon", w_getIcon },
{ "setTitle", w_setTitle },
{ "getTitle", w_getTitle },
{ "hasFocus", w_hasFocus },
{ "hasMouseFocus", w_hasMouseFocus },
{ "isVisible", w_isVisible },
{ 0, 0 }
};
extern "C" int luaopen_love_window(lua_State *L)
{
EXCEPT_GUARD(instance = sdl::Window::createSingleton();)
WrappedModule w;
w.module = instance;
w.name = "window";
w.flags = MODULE_T;
w.functions = functions;
w.types = 0;
return luax_register_module(L, w);
}
} // window
} // love
+55
View File
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2006-2013 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_WINDOW_WRAP_WINDOW_H
#define LOVE_WINDOW_WRAP_WINDOW_H
#include "common/config.h"
#include "common/runtime.h"
namespace love
{
namespace window
{
int w_getDisplayCount(lua_State *L);
int w_setMode(lua_State *L);
int w_getMode(lua_State *L);
int w_getFullscreenModes(lua_State *L);
int w_setFullscreen(lua_State *L);
int w_getFullscreen(lua_State *L);
int w_isCreated(lua_State *L);
int w_getWidth(lua_State *L);
int w_getHeight(lua_State *L);
int w_getDimensions(lua_State *L);
int w_getDesktopDimensions(lua_State *L);
int w_setIcon(lua_State *L);
int w_getIcon(lua_State *L);
int w_setTitle(lua_State *L);
int w_getTitle(lua_State *L);
int w_hasFocus(lua_State *L);
int w_hasMouseFocus(lua_State *L);
int w_isVisible(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_window(lua_State *L);
} // window
} // love
#endif // LOVE_WINDOW_WRAP_WINDOW_H