mirror of
https://github.com/love2d/love.git
synced 2026-08-16 08:11:02 +02:00
Added love.window (issue #6.) All window-related functions have been moved from love.graphics to love.window.
love.graphics.setCaption has been renamed to love.window.setTitle.
This commit is contained in:
@@ -112,6 +112,19 @@ public:
|
||||
|
||||
virtual ~Graphics();
|
||||
|
||||
/**
|
||||
* Sets the current graphics display viewport and initializes the renderer.
|
||||
* @param width The viewport width.
|
||||
* @param height The viewport height.
|
||||
**/
|
||||
virtual bool setMode(int width, int height) = 0;
|
||||
|
||||
/**
|
||||
* Un-sets the current graphics display mode (uninitializing objects if
|
||||
* necessary.)
|
||||
**/
|
||||
virtual void unSetMode() = 0;
|
||||
|
||||
static bool getConstant(const char *in, DrawMode &out);
|
||||
static bool getConstant(DrawMode in, const char *&out);
|
||||
|
||||
|
||||
@@ -46,8 +46,15 @@ Graphics::Graphics()
|
||||
, matrixLimit(0)
|
||||
, userMatrices(0)
|
||||
, colorMask()
|
||||
, width(0)
|
||||
, height(0)
|
||||
, created(false)
|
||||
, savedState()
|
||||
{
|
||||
currentWindow = love::window::sdl::Window::getSingleton();
|
||||
|
||||
if (currentWindow->isCreated())
|
||||
setMode(currentWindow->getWidth(), currentWindow->getHeight());
|
||||
}
|
||||
|
||||
Graphics::~Graphics()
|
||||
@@ -63,11 +70,6 @@ const char *Graphics::getName() const
|
||||
return "love.graphics.opengl";
|
||||
}
|
||||
|
||||
bool Graphics::checkMode(int width, int height, bool fullscreen) const
|
||||
{
|
||||
return currentWindow->checkWindowSize(width, height, fullscreen);
|
||||
}
|
||||
|
||||
DisplayState Graphics::saveState()
|
||||
{
|
||||
DisplayState s;
|
||||
@@ -120,31 +122,14 @@ void Graphics::restoreState(const DisplayState &s)
|
||||
setColorMask(s.colorMask[0], s.colorMask[1], s.colorMask[2], s.colorMask[3]);
|
||||
}
|
||||
|
||||
static void APIENTRY myErrorCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, void *ud)
|
||||
bool Graphics::setMode(int width, int height)
|
||||
{
|
||||
(void)ud;
|
||||
std::cerr << "source = " << source << ", type = " << type << ", id = " << id << ", severity = " << severity << ", length = " << length << "\n" << message << std::endl;
|
||||
}
|
||||
|
||||
bool Graphics::setMode(int width, int height, WindowFlags *flags)
|
||||
{
|
||||
// This operation destroys the OpenGL context, so
|
||||
// we must save the state.
|
||||
DisplayState tempState;
|
||||
if (isCreated())
|
||||
tempState = saveState();
|
||||
savedState = saveState();
|
||||
|
||||
// Unload all volatile objects. These must be reloaded after
|
||||
// the display mode change.
|
||||
Volatile::unloadAll();
|
||||
|
||||
gl.deInitContext();
|
||||
|
||||
bool success = currentWindow->setWindow(width, height, flags);
|
||||
|
||||
// Regardless of failure, we'll have to set up OpenGL once again.
|
||||
width = currentWindow->getWidth();
|
||||
height = currentWindow->getHeight();
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
|
||||
// Okay, setup OpenGL.
|
||||
gl.initContext();
|
||||
@@ -201,7 +186,7 @@ bool Graphics::setMode(int width, int height, WindowFlags *flags)
|
||||
std::cerr << "Could not reload all volatile objects." << std::endl;
|
||||
|
||||
// Restore the display state.
|
||||
restoreState(tempState);
|
||||
restoreState(savedState);
|
||||
pixel_size_stack.clear();
|
||||
pixel_size_stack.reserve(5);
|
||||
pixel_size_stack.push_back(1);
|
||||
@@ -211,30 +196,22 @@ bool Graphics::setMode(int width, int height, WindowFlags *flags)
|
||||
glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &matrixLimit);
|
||||
matrixLimit -= 5;
|
||||
|
||||
// Debug output is temporarily disabled until we either improve it and make
|
||||
// it optional or remove it.
|
||||
if (GLEE_KHR_debug && false)
|
||||
{
|
||||
std::cerr << "debug on" << std::endl;
|
||||
glDebugMessageCallback(myErrorCallback, NULL);
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
}
|
||||
created = true;
|
||||
|
||||
return success;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Graphics::getMode(int &width, int &height, WindowFlags &flags) const
|
||||
void Graphics::unSetMode()
|
||||
{
|
||||
currentWindow->getWindow(width, height, flags);
|
||||
}
|
||||
// Window creation may destroy the OpenGL context, so we must save the state.
|
||||
if (isCreated())
|
||||
savedState = saveState();
|
||||
|
||||
bool Graphics::toggleFullscreen()
|
||||
{
|
||||
int width, height;
|
||||
WindowFlags flags;
|
||||
currentWindow->getWindow(width, height, flags);
|
||||
flags.fullscreen = !flags.fullscreen;
|
||||
return setMode(width, height, &flags);
|
||||
// Unload all volatile objects. These must be reloaded after
|
||||
// the display mode change.
|
||||
Volatile::unloadAll();
|
||||
|
||||
gl.deInitContext();
|
||||
}
|
||||
|
||||
void Graphics::reset()
|
||||
@@ -259,39 +236,21 @@ void Graphics::present()
|
||||
currentWindow->swapBuffers();
|
||||
}
|
||||
|
||||
void Graphics::setIcon(Image *image)
|
||||
{
|
||||
if (image->isCompressed())
|
||||
throw love::Exception("Cannot use compressed image data to set an icon.");
|
||||
|
||||
currentWindow->setIcon(image->getData());
|
||||
}
|
||||
|
||||
void Graphics::setCaption(const char *caption)
|
||||
{
|
||||
std::string title(caption);
|
||||
currentWindow->setWindowTitle(title);
|
||||
}
|
||||
|
||||
std::string Graphics::getCaption() const
|
||||
{
|
||||
return currentWindow->getWindowTitle();
|
||||
}
|
||||
|
||||
int Graphics::getWidth() const
|
||||
{
|
||||
return currentWindow->getWidth();
|
||||
return width;
|
||||
}
|
||||
|
||||
int Graphics::getHeight() const
|
||||
{
|
||||
return currentWindow->getHeight();
|
||||
return height;
|
||||
}
|
||||
|
||||
int Graphics::getRenderWidth() const
|
||||
{
|
||||
if (Canvas::current)
|
||||
return Canvas::current->getWidth();
|
||||
|
||||
return getWidth();
|
||||
}
|
||||
|
||||
@@ -299,44 +258,13 @@ int Graphics::getRenderHeight() const
|
||||
{
|
||||
if (Canvas::current)
|
||||
return Canvas::current->getHeight();
|
||||
|
||||
return getHeight();
|
||||
}
|
||||
|
||||
bool Graphics::isCreated() const
|
||||
{
|
||||
return currentWindow->isCreated();
|
||||
}
|
||||
|
||||
int Graphics::getModes(lua_State *L) const
|
||||
{
|
||||
int n;
|
||||
love::window::Window::WindowSize *modes = currentWindow->getFullscreenSizes(n);
|
||||
|
||||
if (modes == 0)
|
||||
return 0;
|
||||
|
||||
lua_createtable(L, n, 0);
|
||||
|
||||
for (int i = 0; i < n ; 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);
|
||||
}
|
||||
|
||||
delete[] modes;
|
||||
return 1;
|
||||
return created;
|
||||
}
|
||||
|
||||
void Graphics::setScissor(int x, int y, int width, int height)
|
||||
@@ -1379,11 +1307,6 @@ void Graphics::origin()
|
||||
pixel_size_stack.push_back(1);
|
||||
}
|
||||
|
||||
bool Graphics::hasFocus() const
|
||||
{
|
||||
return currentWindow->hasFocus();
|
||||
}
|
||||
|
||||
} // opengl
|
||||
} // graphics
|
||||
} // love
|
||||
|
||||
@@ -111,40 +111,12 @@ public:
|
||||
// Implements Module.
|
||||
const char *getName() const;
|
||||
|
||||
/**
|
||||
* Checks whether a display mode is supported or not. Note
|
||||
* that fullscreen is assumed, because windowed modes are
|
||||
* generally supported regardless of size.
|
||||
* @param width The window width.
|
||||
* @param height The window height.
|
||||
**/
|
||||
bool checkMode(int width, int height, bool fullscreen) const;
|
||||
|
||||
DisplayState saveState();
|
||||
|
||||
void restoreState(const DisplayState &s);
|
||||
|
||||
/**
|
||||
* Sets the current display mode.
|
||||
* @param width The window width.
|
||||
* @param height The window height.
|
||||
* @param flags An optional WindowFlags structure.
|
||||
**/
|
||||
bool setMode(int width, int height, WindowFlags *flags);
|
||||
|
||||
/**
|
||||
* Gets the current display mode.
|
||||
* @param width Pointer to an integer for the window width.
|
||||
* @param height Pointer to an integer for the window height.
|
||||
* @param flags A WindowFlags structure.
|
||||
**/
|
||||
void getMode(int &width, int &height, WindowFlags &flags) const;
|
||||
|
||||
/**
|
||||
* Toggles fullscreen. Note that this also needs to reload the
|
||||
* entire OpenGL context.
|
||||
**/
|
||||
bool toggleFullscreen();
|
||||
virtual bool setMode(int width, int height);
|
||||
virtual void unSetMode();
|
||||
|
||||
/**
|
||||
* Resets the current color, background color,
|
||||
@@ -159,56 +131,25 @@ public:
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* Flips buffers. (Rendered geometry is
|
||||
* presented on screen).
|
||||
* Flips buffers. (Rendered geometry is presented on screen).
|
||||
**/
|
||||
void present();
|
||||
|
||||
/**
|
||||
* Sets the window's icon.
|
||||
**/
|
||||
void setIcon(Image *image);
|
||||
|
||||
/**
|
||||
* Sets the window's caption.
|
||||
**/
|
||||
void setCaption(const char *caption);
|
||||
|
||||
/**
|
||||
* Gets the window's caption.
|
||||
**/
|
||||
std::string getCaption() const;
|
||||
|
||||
/**
|
||||
* Gets the width of the current display mode.
|
||||
* Gets the width of the current graphics viewport.
|
||||
**/
|
||||
int getWidth() const;
|
||||
|
||||
/**
|
||||
* Gets the height of the current display mode.
|
||||
* Gets the height of the current graphics viewport.
|
||||
**/
|
||||
int getHeight() const;
|
||||
|
||||
/**
|
||||
* True if some display mode is set.
|
||||
* True if a graphics viewport is set.
|
||||
**/
|
||||
bool isCreated() const;
|
||||
|
||||
/**
|
||||
* This native Lua function gets available modes
|
||||
* from SDL and returns them as a table on the following format:
|
||||
*
|
||||
* {
|
||||
* { width = 800, height = 600 },
|
||||
* { width = 1024, height = 768 },
|
||||
* ...
|
||||
* }
|
||||
*
|
||||
* Only fullscreen modes are returned here, as all
|
||||
* window sizes are supported (normally).
|
||||
**/
|
||||
int getModes(lua_State *L) const;
|
||||
|
||||
/**
|
||||
* Scissor defines a box such that everything outside that box is discarded and not drawn.
|
||||
* Scissoring is automatically enabled.
|
||||
@@ -541,9 +482,11 @@ public:
|
||||
void shear(float kx, float ky);
|
||||
void origin();
|
||||
|
||||
bool hasFocus() const;
|
||||
private:
|
||||
|
||||
int getRenderWidth() const;
|
||||
int getRenderHeight() const;
|
||||
|
||||
Font *currentFont;
|
||||
love::window::Window *currentWindow;
|
||||
|
||||
@@ -554,8 +497,11 @@ private:
|
||||
GLint userMatrices;
|
||||
bool colorMask[4];
|
||||
|
||||
int getRenderWidth() const;
|
||||
int getRenderHeight() const;
|
||||
int width;
|
||||
int height;
|
||||
bool created;
|
||||
|
||||
DisplayState savedState;
|
||||
|
||||
}; // Graphics
|
||||
|
||||
|
||||
@@ -38,105 +38,6 @@ namespace opengl
|
||||
|
||||
static Graphics *instance = 0;
|
||||
|
||||
bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue)
|
||||
{
|
||||
lua_getfield(L, table_index, key);
|
||||
|
||||
bool retval;
|
||||
if (lua_isnoneornil(L, -1))
|
||||
retval = defaultValue;
|
||||
else
|
||||
retval = lua_toboolean(L, -1);
|
||||
|
||||
lua_pop(L, 1);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int luax_intflag(lua_State *L, int table_index, const char *key, int defaultValue)
|
||||
{
|
||||
lua_getfield(L, table_index, key);
|
||||
|
||||
int retval;
|
||||
if (!lua_isnumber(L, -1))
|
||||
retval = defaultValue;
|
||||
else
|
||||
retval = lua_tonumber(L, -1);
|
||||
|
||||
lua_pop(L, 1);
|
||||
return retval;
|
||||
}
|
||||
|
||||
int w_checkMode(lua_State *L)
|
||||
{
|
||||
int w = luaL_checkint(L, 1);
|
||||
int h = luaL_checkint(L, 2);
|
||||
bool fs = luax_toboolean(L, 3);
|
||||
luax_pushboolean(L, instance->checkMode(w, h, fs));
|
||||
return 1;
|
||||
}
|
||||
|
||||
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->setMode(w, h, 0));
|
||||
return 1;
|
||||
}
|
||||
|
||||
luaL_checktype(L, 3, LUA_TTABLE);
|
||||
|
||||
WindowFlags flags;
|
||||
|
||||
flags.fullscreen = luax_boolflag(L, 3, "fullscreen", false);
|
||||
flags.vsync = luax_boolflag(L, 3, "vsync", true);
|
||||
flags.fsaa = luax_intflag(L, 3, "fsaa", 0);
|
||||
flags.resizable = luax_boolflag(L, 3, "resizable", false);
|
||||
flags.borderless = luax_boolflag(L, 3, "borderless", false);
|
||||
flags.centered = luax_boolflag(L, 3, "centered", true);
|
||||
|
||||
luax_pushboolean(L, instance->setMode(w, h, &flags));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getMode(lua_State *L)
|
||||
{
|
||||
int w, h;
|
||||
WindowFlags flags;
|
||||
instance->getMode(w, h, flags);
|
||||
lua_pushnumber(L, w);
|
||||
lua_pushnumber(L, h);
|
||||
|
||||
lua_newtable(L);
|
||||
|
||||
luax_pushboolean(L, flags.fullscreen);
|
||||
lua_setfield(L, -2, "fullscreen");
|
||||
|
||||
luax_pushboolean(L, flags.vsync);
|
||||
lua_setfield(L, -2, "vsync");
|
||||
|
||||
lua_pushnumber(L, flags.fsaa);
|
||||
lua_setfield(L, -2, "fsaa");
|
||||
|
||||
luax_pushboolean(L, flags.resizable);
|
||||
lua_setfield(L, -2, "resizable");
|
||||
|
||||
luax_pushboolean(L, flags.borderless);
|
||||
lua_setfield(L, -2, "borderless");
|
||||
|
||||
luax_pushboolean(L, flags.centered);
|
||||
lua_setfield(L, -2, "centered");
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
int w_toggleFullscreen(lua_State *L)
|
||||
{
|
||||
luax_pushboolean(L, instance->toggleFullscreen());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_reset(lua_State *)
|
||||
{
|
||||
instance->reset();
|
||||
@@ -155,64 +56,12 @@ int w_present(lua_State *)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_setIcon(lua_State *L)
|
||||
{
|
||||
Image *image = luax_checkimage(L, 1);
|
||||
try
|
||||
{
|
||||
instance->setIcon(image);
|
||||
}
|
||||
catch (love::Exception &e)
|
||||
{
|
||||
return luaL_error(L, "%s", e.what());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_setCaption(lua_State *L)
|
||||
{
|
||||
const char *str = luaL_checkstring(L, 1);
|
||||
instance->setCaption(str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getCaption(lua_State *L)
|
||||
{
|
||||
std::string caption = instance->getCaption();
|
||||
lua_pushstring(L, caption.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getWidth(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, instance->getWidth());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getHeight(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, instance->getHeight());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getDimensions(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, instance->getWidth());
|
||||
lua_pushnumber(L, instance->getHeight());
|
||||
return 2;
|
||||
}
|
||||
|
||||
int w_isCreated(lua_State *L)
|
||||
{
|
||||
luax_pushboolean(L, instance->isCreated());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_getModes(lua_State *L)
|
||||
{
|
||||
return instance->getModes(L);
|
||||
}
|
||||
|
||||
int w_setScissor(lua_State *L)
|
||||
{
|
||||
if (lua_gettop(L) == 0)
|
||||
@@ -1600,20 +1449,10 @@ int w_origin(lua_State * /*L*/)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_hasFocus(lua_State *L)
|
||||
{
|
||||
luax_pushboolean(L, instance->hasFocus());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "checkMode", w_checkMode },
|
||||
{ "setMode", w_setMode },
|
||||
{ "getMode", w_getMode },
|
||||
{ "toggleFullscreen", w_toggleFullscreen },
|
||||
{ "reset", w_reset },
|
||||
{ "clear", w_clear },
|
||||
{ "present", w_present },
|
||||
@@ -1674,19 +1513,8 @@ static const luaL_Reg functions[] =
|
||||
{ "print", w_print },
|
||||
{ "printf", w_printf },
|
||||
|
||||
{ "setCaption", w_setCaption },
|
||||
{ "getCaption", w_getCaption },
|
||||
|
||||
{ "setIcon", w_setIcon },
|
||||
|
||||
{ "getWidth", w_getWidth },
|
||||
{ "getHeight", w_getHeight },
|
||||
{ "getDimensions", w_getDimensions },
|
||||
|
||||
{ "isCreated", w_isCreated },
|
||||
|
||||
{ "getModes", w_getModes },
|
||||
|
||||
{ "setScissor", w_setScissor },
|
||||
{ "getScissor", w_getScissor },
|
||||
|
||||
@@ -1712,8 +1540,6 @@ static const luaL_Reg functions[] =
|
||||
{ "shear", w_shear },
|
||||
{ "origin", w_origin },
|
||||
|
||||
{ "hasFocus", w_hasFocus },
|
||||
|
||||
{ 0, 0 }
|
||||
};
|
||||
|
||||
@@ -1749,7 +1575,7 @@ extern "C" int luaopen_love_graphics(lua_State *L)
|
||||
WrappedModule w;
|
||||
w.module = instance;
|
||||
w.name = "graphics";
|
||||
w.flags = MODULE_T;
|
||||
w.flags = MODULE_GRAPHICS_T;
|
||||
w.functions = functions;
|
||||
w.types = types;
|
||||
|
||||
|
||||
@@ -38,19 +38,9 @@ namespace graphics
|
||||
namespace opengl
|
||||
{
|
||||
|
||||
int w_checkMode(lua_State *L);
|
||||
int w_setMode(lua_State *L);
|
||||
int w_getMode(lua_State *L);
|
||||
int w_toggleFullscreen(lua_State *L);
|
||||
int w_reset(lua_State *L);
|
||||
int w_clear(lua_State *L);
|
||||
int w_present(lua_State *L);
|
||||
int w_setIcon(lua_State *L);
|
||||
int w_setCaption(lua_State *L);
|
||||
int w_getCaption(lua_State *L);
|
||||
int w_getWidth(lua_State *L);
|
||||
int w_getHeight(lua_State *L);
|
||||
int w_getDimensions(lua_State *L);
|
||||
int w_isCreated(lua_State *L);
|
||||
int w_setScissor(lua_State *L);
|
||||
int w_getScissor(lua_State *L);
|
||||
@@ -115,7 +105,6 @@ int w_scale(lua_State *L);
|
||||
int w_translate(lua_State *L);
|
||||
int w_shear(lua_State *L);
|
||||
int w_origin(lua_State *L);
|
||||
int w_hasFocus(lua_State *L);
|
||||
extern "C" LOVE_EXPORT int luaopen_love_graphics(lua_State *L);
|
||||
|
||||
} // opengl
|
||||
|
||||
Reference in New Issue
Block a user