Skip to content

Commit

Permalink
- new commands:
Browse files Browse the repository at this point in the history
    scrupp.setBackgroundColor(table or r,g,b values)
    r,g,b = scrupp.getBackgroundColor()
  Every frame is cleared using this color, before the actual rendering 
  happens.
  The default is black (r,g,b = 0,0,0).

- small optimization regarding unicode translation on keypress events

- changed configure script to ignore Lua 5.2 until all libraries are ported
  to the new version
  • Loading branch information
akrinke committed Feb 26, 2012
1 parent dc825c0 commit 583734d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
6 changes: 5 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ PKG_CHECK_MODULES([LUAJIT], [luajit],
fi
done
])
PKG_CHECK_MODULES(LUA, [${LUA_PKG_NAME} >= 5.1])
PKG_CHECK_MODULES(LUA, [${LUA_PKG_NAME} >= 5.1 ${LUA_PKG_NAME} < 5.2], ,
[AC_CHECK_LIB(lua, luaL_newstate, ,
AC_MSG_ERROR([Lua development files are required to compile Scrupp])
)
])
])

# Check for PhysFS
Expand Down
33 changes: 32 additions & 1 deletion src/Graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

SDL_Surface *screen;

/* glClearColor, stored as byte to avoid rounding errors if returned */
GLubyte r_clear = 0;
GLubyte g_clear = 0;
GLubyte b_clear = 0;

Lua_Image *first_image = NULL;

/* calculates the next higher power of two */
Expand Down Expand Up @@ -230,7 +235,7 @@ static int initSDL (lua_State *L, const char *appName, int width, int height, in
width, height, bpp, SDL_GetError());
/* set the OpenGL state */
/* set background color */
glClearColor( 0, 0, 0, 1);
glClearColor((GLfloat)r_clear/255.0f, (GLfloat)g_clear/255.0f, (GLfloat)b_clear/255.0f, 1.0f);
/* set line antialiasing */
glEnable(GL_LINE_SMOOTH);
/* enable blending */
Expand Down Expand Up @@ -332,6 +337,30 @@ static int Lua_Graphics_showCursor(lua_State *L) {
}
}

static int Lua_Graphics_setBackgroundColor(lua_State *L) {
if (lua_istable(L, 1)) {
lua_pushinteger(L, 1);
lua_gettable(L, 1);
lua_pushinteger(L, 2);
lua_gettable(L, 1);
lua_pushinteger(L, 3);
lua_gettable(L, 1);
lua_remove(L, 1);
}
r_clear = (GLubyte)luaL_checkint(L, 1);
g_clear = (GLubyte)luaL_checkint(L, 2);
b_clear = (GLubyte)luaL_checkint(L, 3);
glClearColor((GLfloat)r_clear/255.0f, (GLfloat)g_clear/255.0f, (GLfloat)b_clear/255.0f, 1.0f);
return 0;
}

static int Lua_Graphics_getBackgroundColor(lua_State *L) {
lua_pushinteger(L, r_clear);
lua_pushinteger(L, g_clear);
lua_pushinteger(L, b_clear);
return 3;
}

static int Lua_Graphics_getWindowWidth(lua_State *L) {
lua_pushinteger(L, screen->w);
return 1;
Expand Down Expand Up @@ -1076,6 +1105,8 @@ static int Lua_Graphics_draw(lua_State *L) {

static const struct luaL_Reg graphicslib [] = {
{"init", Lua_Graphics_init},
{"setBackgroundColor", Lua_Graphics_setBackgroundColor},
{"getBackgroundColor", Lua_Graphics_getBackgroundColor},
{"getWindowWidth", Lua_Graphics_getWindowWidth},
{"getWindowHeight", Lua_Graphics_getWindowHeight},
{"getWindowSize", Lua_Graphics_getWindowSize},
Expand Down
27 changes: 15 additions & 12 deletions src/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,22 @@ int main(int argc, char *argv[]) {
lua_rawget(L, LUA_REGISTRYINDEX);
lua_rawgeti(L, -1, event.key.keysym.sym);
lua_remove(L, -2); /* remove the key_table */
/* get the utf-16 code */
wchar = event.key.keysym.unicode;
buf[0] = 0; buf[1] = 0; buf[2] = 0; buf[3] = 0;
/* convert utf-16 to utf-8 */
if (wchar < 0x80) {
buf[0] = wchar;
} else if (wchar < 0x800) {
buf[0] = (0xC0 | wchar >> 6);
buf[1] = (0x80 | wchar & 0x3F);
} else {
buf[0] = (0xE0 | wchar >> 12);
buf[1] = (0x80 | wchar >> 6 & 0x3F);
buf[2] = (0x80 | wchar & 0x3F);
if (SDL_EnableUNICODE(-1) == 1) {
/* get the utf-16 code */
wchar = event.key.keysym.unicode;
printf("wchar: %d\n", wchar);
/* convert utf-16 to utf-8 */
if (wchar < 0x80) {
buf[0] = wchar;
} else if (wchar < 0x800) {
buf[0] = (0xC0 | wchar >> 6);
buf[1] = (0x80 | wchar & 0x3F);
} else {
buf[0] = (0xE0 | wchar >> 12);
buf[1] = (0x80 | wchar >> 6 & 0x3F);
buf[2] = (0x80 | wchar & 0x3F);
}
}
lua_pushstring(L, &buf[0]);
if ((lua_pcall(L, 2, 0, -5) != 0) && !check_for_exit(L)) {
Expand Down

0 comments on commit 583734d

Please sign in to comment.