diff --git a/src/graphics/graphics.c b/src/graphics/graphics.c index 17ec8f4d..b95ca07f 100644 --- a/src/graphics/graphics.c +++ b/src/graphics/graphics.c @@ -43,6 +43,7 @@ void lovrGraphicsReset() { lovrGraphicsSetBackgroundColor(0, 0, 0, 0); lovrGraphicsSetColor(255, 255, 255, 255); lovrGraphicsSetColorMask(1, 1, 1, 1); + lovrGraphicsSetScissorEnabled(0); } void lovrGraphicsClear(int color, int depth) { @@ -138,6 +139,36 @@ void lovrGraphicsSetColorMask(unsigned char r, unsigned char g, unsigned char b, glColorMask(r, g, b, a); } +char lovrGraphicsIsScissorEnabled() { + return state.isScissorEnabled; +} + +void lovrGraphicsSetScissorEnabled(char isEnabled) { + state.isScissorEnabled = isEnabled; + if (isEnabled) { + glEnable(GL_SCISSOR_TEST); + } else { + glDisable(GL_SCISSOR_TEST); + } +} + +void lovrGraphicsGetScissor(int* x, int* y, int* width, int* height) { + *x = state.scissor.x; + *y = state.scissor.x; + *width = state.scissor.width; + *height = state.scissor.height; +} + +void lovrGraphicsSetScissor(int x, int y, int width, int height) { + int windowWidth, windowHeight; + glfwGetWindowSize(window, &windowWidth, &windowHeight); + state.scissor.x = x; + state.scissor.x = y; + state.scissor.width = width; + state.scissor.height = height; + glScissor(x, windowHeight - y, width, height); +} + Shader* lovrGraphicsGetShader() { return state.activeShader; } diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index 40df4417..55d906f8 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -13,6 +13,13 @@ typedef vec_t(mat4) vec_mat4_t; #define LOVR_COLOR_B(c) (c >> 8 & 0xff) #define LOVR_COLOR_A(c) (c >> 0 & 0xff) +typedef struct { + int x; + int y; + int width; + int height; +} ScissorRectangle; + typedef struct { Shader* activeShader; Shader* defaultShader; @@ -23,6 +30,8 @@ typedef struct { unsigned int color; unsigned int lastColor; unsigned char colorMask; + char isScissorEnabled; + ScissorRectangle scissor; } GraphicsState; #endif @@ -37,6 +46,10 @@ void lovrGraphicsGetColor(unsigned char* r, unsigned char* g, unsigned char* b, void lovrGraphicsSetColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a); void lovrGraphicsGetColorMask(unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a); void lovrGraphicsSetColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a); +char lovrGraphicsIsScissorEnabled(); +void lovrGraphicsSetScissorEnabled(char isEnabled); +void lovrGraphicsGetScissor(int* x, int* y, int* width, int* height); +void lovrGraphicsSetScissor(int x, int y, int width, int height); Shader* lovrGraphicsGetShader(); void lovrGraphicsSetShader(Shader* shader); void lovrGraphicsSetProjection(float near, float far, float fov); diff --git a/src/lovr/graphics.c b/src/lovr/graphics.c index bbf686df..fac19cff 100644 --- a/src/lovr/graphics.c +++ b/src/lovr/graphics.c @@ -15,6 +15,8 @@ const luaL_Reg lovrGraphics[] = { { "setColor", l_lovrGraphicsSetColor }, { "getColorMask", l_lovrGraphicsGetColorMask }, { "setColorMask", l_lovrGraphicsSetColorMask }, + { "getScissor", l_lovrGraphicsGetScissor }, + { "setScissor", l_lovrGraphicsSetScissor }, { "setShader", l_lovrGraphicsSetShader }, { "setProjection", l_lovrGraphicsSetProjection }, { "push", l_lovrGraphicsPush }, @@ -140,6 +142,37 @@ int l_lovrGraphicsSetColorMask(lua_State* L) { return 0; } +int l_lovrGraphicsGetScissor(lua_State* L) { + if (!lovrGraphicsIsScissorEnabled()) { + lua_pushnil(L); + return 1; + } + + int x, y, width, height; + lovrGraphicsGetScissor(&x, &y, &width, &height); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + return 4; +} + +int l_lovrGraphicsSetScissor(lua_State* L) { + if (lua_gettop(L) <= 1 && lua_isnoneornil(L, 1)) { + lovrGraphicsSetScissorEnabled(0); + return 0; + } + + int x = luaL_checkint(L, 1); + int y = luaL_checkint(L, 2); + int width = luaL_checkint(L, 3); + int height = luaL_checkint(L, 4); + lovrGraphicsSetScissor(x, y, width, height); + lovrGraphicsSetScissorEnabled(1); + + return 0; +} + int l_lovrGraphicsGetShader(lua_State* L) { luax_pushshader(L, lovrGraphicsGetShader()); return 1; diff --git a/src/lovr/graphics.h b/src/lovr/graphics.h index dd39912f..a3489de9 100644 --- a/src/lovr/graphics.h +++ b/src/lovr/graphics.h @@ -17,6 +17,8 @@ int l_lovrGraphicsGetColor(lua_State* L); int l_lovrGraphicsSetColor(lua_State* L); int l_lovrGraphicsGetColorMask(lua_State* L); int l_lovrGraphicsSetColorMask(lua_State* L); +int l_lovrGraphicsGetScissor(lua_State* L); +int l_lovrGraphicsSetScissor(lua_State* L); int l_lovrGraphicsGetShader(lua_State* L); int l_lovrGraphicsSetShader(lua_State* L); int l_lovrGraphicsSetProjection(lua_State* L);