This commit is contained in:
bjorn 2016-09-28 21:47:36 -07:00
parent e188c0beca
commit 7c707d9d6e
4 changed files with 79 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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);