- [NEW] configuration file working.

- [NEW] zoom(), fullscreen(), cursor(), getconf(), setconf().
- [CHG] now zoom and fullscreen are controlled by the game, not by mini.
- [FIX] quit() now actually quits.
- [NEW] btnp() without parameters returns key pressed.
- [NEW] zoom, fullscreen and cursor are saved to config automatically.
This commit is contained in:
2023-01-18 17:41:57 +01:00
parent 4c0cf979d3
commit e53befb700
8 changed files with 207 additions and 18 deletions

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@ mini_debug.exe
mini_debug
.vscode/*
info.plist
*.dll

View File

@@ -1,4 +1,5 @@
title=HOLA MINI
config=minitest
width=160
height=120
zoom=3

View File

@@ -2,11 +2,61 @@ x=0
function _init()
text="HOLA MINI"
keyRight = tonumber(getconf("keyright")) or KEY_RIGHT
keyLeft = tonumber(getconf("keyleft")) or KEY_LEFT
_update=normal_update
end
function _update()
end
function normal_update()
cls(20)
prnt(text,x,60)
x=x+1
if btnp(keyRight) then x=x+1 end
if btnp(keyLeft) then x=x-1 end
if btnp(KEY_SPACE) then
redefinekeys.init()
end
if btnp(KEY_ESCAPE) then
quit()
end
if btnp(KEY_F2) then
local val = zoom() + 2
if val >= 10 then val = 2 end
zoom(val)
elseif btnp(KEY_F3) then
fullscreen(not fullscreen())
end
if x>160 then x=-strlen(text)*4 end
end
redefinekeys = {
state = 0,
init = function ()
redefinekeys.state=0
_update=redefinekeys.update
end,
update = function()
cls(20)
if redefinekeys.state == 0 then
prnt("PULSA TECLA PER A DRETA...",0,0)
local key = btnp();
if key ~= 0 then
redefinekeys.state = 1
keyRight=key
setconf("keyright", keyRight)
end
elseif redefinekeys.state == 1 then
prnt("PULSA TECLA PER A ESQUERRA...",0,0)
local key = btnp();
if key ~= 0 then
keyLeft=key
setconf("keyleft", keyLeft)
_update=normal_update
end
end
end
}

View File

@@ -50,7 +50,7 @@ char *resource_folder = NULL;
DATA_File *data_file = NULL;
int file_source = SOURCE_FILE;
char scratch[255];
std::string config_folder = NULL;
std::string config_folder;
std::vector<keyvalue_t> config;
void file_setresourcefilename(const char *str) {
@@ -139,7 +139,7 @@ char *file_getfilebuffer(const char *resourcename, int& filesize) {
}
// Crea la carpeta del sistema donde guardar datos
void createSystemFolder(const char *foldername)
void file_setconfigfolder(const char *foldername)
{
#ifdef _WIN32
config_folder = std::string(getenv("APPDATA")) + "/" + foldername;
@@ -191,15 +191,19 @@ void file_loadconfigvalues() {
config.clear();
std::string config_file = config_folder + "/config.txt";
FILE *f = fopen(config_file.c_str(), "r");
if (f) {
while (!feof(f)) {
char key[100], value[100];
fscanf(f, "%s = %S", key, value);
config.push_back({key, value});
if (!f) return;
char line[1024];
while (fgets(line, sizeof(line), f)) {
char *value = strchr(line, '=');
if (value) {
*value='\0'; value++;
value[strlen(value)-1] = '\0';
config.push_back({line, value});
}
}
fclose(f);
}
}
void file_saveconfigvalues() {
std::string config_file = config_folder + "/config.txt";
@@ -225,7 +229,7 @@ const char* file_getconfigvalue(const char *key) {
void file_setconfigvalue(const char* key, const char* value) {
if (config.empty()) file_loadconfigvalues();
for (auto pair : config) {
for (auto &pair : config) {
if (pair.key == std::string(key)) {
pair.value = value;
file_saveconfigvalues();

View File

@@ -4,6 +4,8 @@
#define SOURCE_FILE 0
#define SOURCE_FOLDER 1
void file_setconfigfolder(const char *foldername);
void file_setresourcefilename(const char *str);
void file_setresourcefolder(const char *str);
void file_setsource(const int src);

60
lua.cpp
View File

@@ -445,8 +445,12 @@ extern "C" {
}
static int cpp_btnp(lua_State *L) {
if (lua_gettop(L) >=1 ) {
uint8_t i = luaL_checkinteger(L, 1);
lua_pushboolean(L, btnp(i));
} else {
lua_pushinteger(L, wbtnp());
}
return 1;
}
@@ -696,6 +700,55 @@ extern "C" {
return 0;
}
static int cpp_zoom(lua_State *L) {
const int value = luaL_optinteger(L, 1, 0);
if (value==0) {
lua_pushinteger(L, getzoom());
return 1;
} else {
setzoom(value);
return 0;
}
}
static int cpp_fullscreen(lua_State *L) {
if (lua_gettop(L) >= 1) {
setfullscreen(lua_toboolean(L, 1));
return 0;
} else {
lua_pushboolean(L, getfullscreen());
return 1;
}
}
static int cpp_cursor(lua_State *L) {
if (lua_gettop(L) >= 1) {
setcursor(lua_toboolean(L, 1));
return 0;
} else {
lua_pushboolean(L, getcursor());
return 1;
}
}
static int cpp_getconf(lua_State *L) {
const char* key = luaL_checkstring(L, 1);
const char* value = getconfig(key);
if (value==NULL) {
lua_pushnil(L);
} else {
lua_pushstring(L, value);
}
return 1;
}
static int cpp_setconf(lua_State *L) {
const char* key = luaL_checkstring(L, 1);
const char* value = luaL_checkstring(L, 2);
setconfig(key, value);
return 0;
}
static int cpp_exit(lua_State *L) {
exit();
return 0;
@@ -806,6 +859,13 @@ void push_lua_funcs() {
lua_pushcfunction(L,cpp_playsound); lua_setglobal(L, "playsound");
lua_pushcfunction(L,cpp_stopsound); lua_setglobal(L, "stopsound");
lua_pushcfunction(L,cpp_zoom); lua_setglobal(L, "zoom");
lua_pushcfunction(L,cpp_fullscreen); lua_setglobal(L, "fullscreen");
lua_pushcfunction(L,cpp_cursor); lua_setglobal(L, "cursor");
lua_pushcfunction(L,cpp_getconf); lua_setglobal(L, "getconf");
lua_pushcfunction(L,cpp_setconf); lua_setglobal(L, "setconf");
lua_pushcfunction(L,cpp_exit); lua_setglobal(L, "quit");
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");

View File

@@ -19,11 +19,12 @@ struct surface_t {
char window_title[256];
char config_folder[256];
uint16_t screen_width = 160;
uint16_t screen_height = 120;
uint8_t screen_zoom = 4;
bool screen_fullscreen = false;
uint8_t show_cursor = SDL_ENABLE;
bool screen_cursor = true;
surface_t surfaces[10];
surface_t *screen_surface = &surfaces[0];
@@ -100,10 +101,11 @@ void read_ini() {
if (value != NULL) {
value[strlen(value)-1] = '\0';
if (strcmp(line, "title") == 0) strcpy(window_title, value);
else if (strcmp(line, "config") == 0) strcpy(config_folder, value);
else if (strcmp(line, "width") == 0) screen_width = atoi(value);
else if (strcmp(line, "height") == 0) screen_height = atoi(value);
else if (strcmp(line, "zoom") == 0) screen_zoom = atoi(value);
else if (strcmp(line, "hidemouse") == 0) show_cursor = SDL_DISABLE;
else if (strcmp(line, "fullscreen") == 0) screen_fullscreen = atoi(value);
else if (strcmp(line, "files") == 0) {
//lua_files = (char*)malloc(strlen(value));
strcpy(lua_files, value);
@@ -194,7 +196,7 @@ void createDisplay() {
mini_ren = SDL_CreateRenderer(mini_win, -1, 0);
//SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
SDL_RenderSetLogicalSize(mini_ren, screen_width, screen_height);
SDL_ShowCursor(show_cursor);
SDL_ShowCursor(screen_cursor?SDL_ENABLE:SDL_DISABLE);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height);
}
@@ -224,6 +226,14 @@ int main(int argc,char*argv[]){
file_setresourcefilename(res_file);
#endif
read_ini();
file_setconfigfolder(config_folder);
const char *zoom = file_getconfigvalue("zoom");
if (zoom) screen_zoom=atoi(zoom);
const char *fullscreen = file_getconfigvalue("fullscreen");
if (fullscreen) screen_fullscreen=strcmp(fullscreen, "true")==0?true:false;
const char *cursor = file_getconfigvalue("cursor");
if (cursor) screen_cursor=strcmp(cursor, "true")?true:false;
setdest(newsurf(screen_width, screen_height));
SDL_Init(SDL_INIT_EVERYTHING);
@@ -248,15 +258,20 @@ int main(int argc,char*argv[]){
while(SDL_PollEvent(&mini_eve)) {
if (mini_eve.type == SDL_QUIT) { should_exit=true; should_quit=true; break; }
if (mini_eve.type == SDL_KEYDOWN) {
/*
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F2) {
screen_zoom+=2; if (screen_zoom>=10) screen_zoom=2;
destroyDisplay();
createDisplay();
char strzoom[3];
file_setconfigvalue("zoom", SDL_itoa(screen_zoom, strzoom, 10));
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F3) {
screen_fullscreen = !screen_fullscreen;
destroyDisplay();
createDisplay();
file_setconfigvalue("fullscreen", screen_fullscreen?"true":"false");
}
*/
#ifdef DEBUG
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F1) {
if (lua_is_playing()) {
@@ -846,6 +861,10 @@ bool btn(uint8_t i) {
return keys[i];
}
int wbtnp() {
return key_just_pressed;
}
bool btnp(uint8_t i) {
return key_just_pressed == i;
}
@@ -1072,6 +1091,47 @@ void stopsound(int soundchannel) {
Mix_HaltChannel(soundchannel);
}
int getzoom() {
return screen_zoom;
}
void setzoom(const int value) {
screen_zoom = value;
destroyDisplay();
createDisplay();
char strzoom[3];
file_setconfigvalue("zoom", SDL_itoa(screen_zoom, strzoom, 10));
}
bool getfullscreen() {
return screen_fullscreen;
}
void setfullscreen(const bool value) {
screen_fullscreen=value;
destroyDisplay();
createDisplay();
file_setconfigvalue("fullscreen", screen_fullscreen?"true":"false");
}
bool getcursor() {
return screen_cursor;
}
void setcursor(const bool value) {
screen_cursor=value;
SDL_ShowCursor(screen_cursor?SDL_ENABLE:SDL_DISABLE);
}
const char* getconfig(const char* key) {
return file_getconfigvalue(key);
}
void setconfig(const char* key, const char* value) {
file_setconfigvalue(key, value);
}
void exit() {
should_exit = true;
should_quit = true;
}

11
mini.h
View File

@@ -200,6 +200,7 @@ void mset(int celx, int cely, uint8_t snum);
void map(int celx, int cely, int sx, int sy, uint8_t celw, uint8_t celh, uint8_t layer=0);
bool btn(uint8_t i);
int wbtnp();
bool btnp(uint8_t i);
bool anykey();
@@ -267,4 +268,14 @@ void freesound(int soundfile);
int playsound(int soundfile, const int volume=-1);
void stopsound(int soundchannel);
int getzoom();
void setzoom(const int value);
bool getfullscreen();
void setfullscreen(const bool value);
bool getcursor();
void setcursor(const bool value);
const char* getconfig(const char* key);
void setconfig(const char* key, const char* value);
void exit();