Skip to content

Commit

Permalink
Continue Lua scripting implementation (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
oitofelix committed Feb 15, 2017
1 parent 621e1f1 commit 25e5dc8
Show file tree
Hide file tree
Showing 20 changed files with 421 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gdbinit
@@ -0,0 +1 @@
source tools/luagdb.txt
12 changes: 7 additions & 5 deletions Makefile.am
Expand Up @@ -126,10 +126,11 @@ ${LUA_INCLUDE} -I$(top_srcdir)/gnulib -I${builddir}/src \
-I${srcdir}/src/kid -I${srcdir}/src/levels -I${srcdir}/src/wall \
-I${srcdir}/src/script

AM_CFLAGS = ${ALLEGRO_CFLAGS} ${ALLEGRO_IMAGE_CFLAGS} \
${ALLEGRO_AUDIO_CFLAGS} ${ALLEGRO_ACODEC_CFLAGS} \
${ALLEGRO_FONT_CFLAGS} ${ALLEGRO_PRIMITIVES_CFLAGS} \
${ALLEGRO_MAIN_CFLAGS} ${ALLEGRO_DIALOG_CFLAGS}
AM_CFLAGS = ${ALLEGRO_CFLAGS} ${ALLEGRO_IMAGE_CFLAGS} \
${ALLEGRO_AUDIO_CFLAGS} ${ALLEGRO_ACODEC_CFLAGS} \
${ALLEGRO_FONT_CFLAGS} ${ALLEGRO_PRIMITIVES_CFLAGS} \
${ALLEGRO_MAIN_CFLAGS} ${ALLEGRO_DIALOG_CFLAGS} \
${ALLEGRO_COLOR_CFLAGS}

if DEBUG
CFLAGS += -O0 -Wall -ggdb3 -Werror -Wno-error=unused-function \
Expand All @@ -149,7 +150,8 @@ endif
LIBS = -lm ${ALLEGRO_LIBS} ${ALLEGRO_IMAGE_LIBS} \
${ALLEGRO_AUDIO_LIBS} ${ALLEGRO_ACODEC_LIBS} ${ALLEGRO_FONT_LIBS} \
${ALLEGRO_PRIMITIVES_LIBS} ${ALLEGRO_MAIN_LIBS} \
${ALLEGRO_DIALOG_LIBS} ${mingw_ldadd} ${LUA_LIB}
${ALLEGRO_DIALOG_LIBS} ${ALLEGRO_COLOR_LIBS} ${mingw_ldadd} \
${LUA_LIB}
LDADD = gnulib/libgnu.a

gnulib/libgnu.a:
Expand Down
14 changes: 13 additions & 1 deletion configure.ac
Expand Up @@ -155,6 +155,8 @@ AS_CASE([$host],
ALLEGRO_PRIMITIVES_LIBS="-lallegro_primitives${debug_suffix} ${ALLEGRO_LIBS}"
ALLEGRO_DIALOG_CFLAGS="${ALLEGRO_CFLAGS}"
ALLEGRO_DIALOG_LIBS="-lallegro_dialog${debug_suffix} ${ALLEGRO_LIBS}"
ALLEGRO_COLOR_CFLAGS="${ALLEGRO_CFLAGS}"
ALLEGRO_COLOR_LIBS="-lallegro_color${debug_suffix} ${ALLEGRO_LIBS}"
mingw_ldadd="-lFLAC \
-lfreetype \
-lvorbisfile \
Expand Down Expand Up @@ -225,7 +227,15 @@ AS_CASE([$host],
[allegro_dialog${debug_suffix}-5 >= $allegro_minver],,
[PKG_CHECK_MODULES([ALLEGRO_DIALOG],
[allegro_dialog${debug_suffix}-5.0 >= $allegro_minver],,
AC_MSG_ERROR([MININIM requires Allegro $allegro_minver (or superior) dialogs addon]))])])
AC_MSG_ERROR([MININIM requires Allegro $allegro_minver (or superior) dialogs addon]))])
PKG_CHECK_MODULES([ALLEGRO_COLOR],
[allegro_color${debug_suffix}-5 >= $allegro_minver],,
[PKG_CHECK_MODULES([ALLEGRO_COLOR],
[allegro_color${debug_suffix}-5.0 >= $allegro_minver],,
AC_MSG_ERROR([MININIM requires Allegro $allegro_minver (or superior) colors addon]))])
])

AC_SUBST([ALLEGRO_CFLAGS])
AC_SUBST([ALLEGRO_LIBS])
Expand All @@ -243,6 +253,8 @@ AC_SUBST([ALLEGRO_MAIN_CFLAGS])
AC_SUBST([ALLEGRO_MAIN_LIBS])
AC_SUBST([ALLEGRO_DIALOG_CFLAGS])
AC_SUBST([ALLEGRO_DIALOG_LIBS])
AC_SUBST([ALLEGRO_COLOR_CFLAGS])
AC_SUBST([ALLEGRO_COLOR_LIBS])
AC_SUBST([mingw_ldadd])


Expand Down
15 changes: 15 additions & 0 deletions data/cga/cga.lua
Expand Up @@ -25,6 +25,8 @@ local P = {package_type = "VIDEO MODE", package_name = "CGA",
-- imports
local M = mininim
local common = require "script/common"
local color = M.video.color
local setmetatable = setmetatable
local function b (filename)
return common.load_bitmap (P, filename)
Expand All @@ -33,6 +35,13 @@ end
-- body
setfenv (1, P)
local function hgc_palette (c)
if c == color (85, 255, 255) then return color (170, 170, 170)
elseif c == color (255, 85, 255) then return color (85, 85, 85)
else return c
end
end
function load ()
local W = M.place_width
local H = M.place_height
Expand All @@ -49,6 +58,12 @@ function load ()
coord = torch_coord}}}
M.video[package_name] = V
local HGC = {}
setmetatable (HGC, {__index = V})
HGC.base_palette = hgc_palette
M.video["HGC"] = HGC
return P
end
Expand Down
1 change: 0 additions & 1 deletion data/script/mininim.lua
Expand Up @@ -24,7 +24,6 @@ local M = mininim
-- called during loading screen for loading assets
function M.load_assets_hook ()
-- video modes
M.setting.video_mode = "VGA"
require "vga/vga".load ()
require "ega/ega".load ()
require "cga/cga".load ()
Expand Down
5 changes: 4 additions & 1 deletion src/anim.c
Expand Up @@ -66,7 +66,10 @@ play_anim (void (*draw_callback) (void),
case ALLEGRO_EVENT_TIMER:
if (event.timer.source == timer) {
/* ensures Lua stack is empty */
assert (! lua_gettop (L));
if (DEBUG && lua_gettop (L)) {
L_dump_stack (L);
assert (false);
}

/* update main menu */
main_menu ();
Expand Down
10 changes: 8 additions & 2 deletions src/kernel/video.c
Expand Up @@ -358,10 +358,16 @@ compare_palette_caches (const void *pc0, const void *pc1)

ALLEGRO_BITMAP *
apply_palette (ALLEGRO_BITMAP *bitmap, palette p)
{
return apply_palette_k (bitmap, p, p);
}

ALLEGRO_BITMAP *
apply_palette_k (ALLEGRO_BITMAP *bitmap, palette p, const void *k)
{
if (! bitmap) return NULL;

ALLEGRO_BITMAP *cached = get_cached_palette (bitmap, p);
ALLEGRO_BITMAP *cached = get_cached_palette (bitmap, k);
if (cached) return cached;

int x, y;
Expand All @@ -377,7 +383,7 @@ apply_palette (ALLEGRO_BITMAP *bitmap, palette p)

struct palette_cache pc;
pc.ib = bitmap;
pc.pal = p;
pc.pal = k;
pc.ob = rbitmap;

palette_cache =
Expand Down
2 changes: 2 additions & 0 deletions src/kernel/video.h
Expand Up @@ -84,6 +84,8 @@ ALLEGRO_BITMAP *oitofelix_face (enum vm vm);
/* palette */
int compare_palette_caches (const void *pc0, const void *pc1);
ALLEGRO_BITMAP *apply_palette (ALLEGRO_BITMAP *bitmap, palette p);
ALLEGRO_BITMAP *apply_palette_k (ALLEGRO_BITMAP *bitmap, palette p,
const void *k);
ALLEGRO_BITMAP *get_cached_palette (ALLEGRO_BITMAP *bitmap, palette p);
ALLEGRO_COLOR hgc_palette (ALLEGRO_COLOR c);

Expand Down
22 changes: 15 additions & 7 deletions src/mininim.c
Expand Up @@ -104,6 +104,7 @@ bool force_hue = false;
enum gm gm = ORIGINAL_GM;
char *audio_mode;
char *video_mode;
char *env_mode;
bool immortal_mode;
int initial_total_lives = KID_INITIAL_TOTAL_LIVES, total_lives;
int initial_current_lives = KID_INITIAL_CURRENT_LIVES, current_lives;
Expand Down Expand Up @@ -761,19 +762,22 @@ Levels have been converted using module %s into native format at\n\
e = optval_to_enum (&i, key, arg, state, video_mode_enum, 0);
if (e) return e;
switch (i) {
case 0: vm = VGA; break;
case 1: vm = EGA; break;
case 2: vm = CGA; break;
case 3: vm = CGA, hgc = true; break;
case 0: vm = VGA; set_string_var (&video_mode, "VGA"); break;
case 1: vm = EGA; set_string_var (&video_mode, "EGA"); break;
case 2: vm = CGA; set_string_var (&video_mode, "CGA"); break;
case 3: vm = CGA, hgc = true; set_string_var (&video_mode, "HGC"); break;
}
break;
case ENVIRONMENT_MODE_OPTION:
e = optval_to_enum (&i, key, arg, state, environment_mode_enum, 0);
if (e) return e;
switch (i) {
case 0: force_em = false; break;
case 1: force_em = true, em = DUNGEON; break;
case 2: force_em = true, em = PALACE; break;
case 0: force_em = false;
set_string_var (&env_mode, "ORIGINAL"); break;
case 1: force_em = true, em = DUNGEON;
set_string_var (&env_mode, "DUNGEON"); break;
case 2: force_em = true, em = PALACE;
set_string_var (&env_mode, "PALACE"); break;
}
break;
case HUE_MODE_OPTION:
Expand Down Expand Up @@ -1333,6 +1337,10 @@ free_argv (size_t *cargc, char ***cargv)
int
main (int _argc, char **_argv)
{
set_string_var (&audio_mode, "SBLAST");
set_string_var (&video_mode, "VGA");
set_string_var (&env_mode, "DUNGEON");

struct config_info config_info;

/* glob command line for MinGW */
Expand Down
2 changes: 2 additions & 0 deletions src/mininim.h
Expand Up @@ -62,6 +62,7 @@
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_color.h>

/* Lua */
#include <lua.h>
Expand Down Expand Up @@ -236,6 +237,7 @@ extern bool force_hue;
extern enum gpm gpm;
extern char *audio_mode;
extern char *video_mode;
extern char *env_mode;
extern bool immortal_mode;
extern int initial_total_lives, total_lives,
initial_current_lives, current_lives;
Expand Down
11 changes: 5 additions & 6 deletions src/script/L_mininim.audio.c
Expand Up @@ -66,11 +66,10 @@ __index (lua_State *L)
lua_pushcfunction (L, load);
return 1;
} if (! strcasecmp (key, "current")) {
if (audio_mode) {
L_get_registry (L, audio_mode_ref);
lua_pushstring (L, audio_mode);
lua_gettable (L, 1);
} else lua_pushnil (L);
assert (audio_mode);
L_get_registry (L, audio_mode_ref);
lua_pushstring (L, audio_mode);
lua_gettable (L, 1);
return 1;
} else {
L_get_registry (L, audio_mode_ref);
Expand All @@ -93,7 +92,7 @@ __newindex (lua_State *L)
if (! strcasecmp (key, "load")) {
return luaL_error (L, "mininim.audio.load is read-only");
} else if (! strcasecmp (key, "current")) {
L_set_audio_mode (L, 3);
L_set_string_var (L, 3, "mininim.audio.current", &audio_mode);
return 0;
} else {
L_get_registry (L, audio_mode_ref);
Expand Down
2 changes: 1 addition & 1 deletion src/script/L_mininim.level.c
Expand Up @@ -88,7 +88,7 @@ L_is_valid_pos (lua_State *L, int index, struct pos *p)
lua_Number place = lua_tonumber (L, -1);
lua_pop (L, 1);

if (pos) new_pos (p, &global_level, room, floor, place);
if (p) new_pos (p, &global_level, room, floor, place);

return true;

Expand Down
31 changes: 15 additions & 16 deletions src/script/L_mininim.setting.c
Expand Up @@ -58,6 +58,9 @@ __index (lua_State *L)
} else if (! strcasecmp (key, "video_mode")) {
lua_pushstring (L, video_mode);
return 1;
} else if (! strcasecmp (key, "env_mode")) {
lua_pushstring (L, env_mode);
return 1;
} else return L_error_invalid_key_string (L, key, "mininim.setting");
default: return L_error_invalid_key_type (L, type, "mininim.setting");
}
Expand All @@ -72,34 +75,30 @@ __newindex (lua_State *L)
case LUA_TSTRING:
key = lua_tostring (L, 2);
if (! strcasecmp (key, "audio_mode")) {
L_set_audio_mode (L, 3);
L_set_string_var (L, 3, "mininim.setting.audio_mode", &audio_mode);
return 0;
} else if (! strcasecmp (key, "video_mode")) {
L_set_video_mode (L, 3);
L_set_string_var (L, 3, "mininim.setting.video_mode", &video_mode);
return 0;
} else if (! strcasecmp (key, "env_mode")) {
L_set_string_var (L, 3, "mininim.setting.env_mode", &env_mode);
return 0;
} else return L_error_invalid_key_string (L, key, "mininim.setting");
default: return L_error_invalid_key_type (L, type, "mininim.setting");
}
}

void
L_set_audio_mode (lua_State *L, int index)
set_string_var (char **var, const char *value)
{
const char *value = lua_tostring (L, 3);
if (value) {
if (audio_mode) al_free (audio_mode);
audio_mode = xasprintf ("%s", value);
} else luaL_error
(L, "mininim.setting.audio_mode must be a string");
if (*var) al_free (*var);
*var = xasprintf ("%s", value);
}

void
L_set_video_mode (lua_State *L, int index)
L_set_string_var (lua_State *L, int index, const char *name, char **var)
{
const char *value = lua_tostring (L, 3);
if (value) {
if (video_mode) al_free (video_mode);
video_mode = xasprintf ("%s", value);
} else luaL_error
(L, "mininim.setting.video_mode must be a string");
const char *value = lua_tostring (L, index);
if (value) set_string_var (var, value);
else luaL_error (L, "%s must be a string", name);
}
4 changes: 2 additions & 2 deletions src/script/L_mininim.setting.h
Expand Up @@ -22,7 +22,7 @@
#define MININIM_L_MININIM_SETTING_H

void define_L_mininim_setting (lua_State *L);
void L_set_audio_mode (lua_State *L, int index);
void L_set_video_mode (lua_State *L, int index);
void set_string_var (char **var, const char *value);
void L_set_string_var (lua_State *L, int index, const char *name, char **var);

#endif /* MININIM_L_MININIM_SETTING_H */

0 comments on commit 25e5dc8

Please sign in to comment.