- [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:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ mini_debug.exe
|
|||||||
mini_debug
|
mini_debug
|
||||||
.vscode/*
|
.vscode/*
|
||||||
info.plist
|
info.plist
|
||||||
|
*.dll
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
title=HOLA MINI
|
title=HOLA MINI
|
||||||
|
config=minitest
|
||||||
width=160
|
width=160
|
||||||
height=120
|
height=120
|
||||||
zoom=3
|
zoom=3
|
||||||
|
|||||||
@@ -2,11 +2,61 @@ x=0
|
|||||||
|
|
||||||
function _init()
|
function _init()
|
||||||
text="HOLA MINI"
|
text="HOLA MINI"
|
||||||
|
keyRight = tonumber(getconf("keyright")) or KEY_RIGHT
|
||||||
|
keyLeft = tonumber(getconf("keyleft")) or KEY_LEFT
|
||||||
|
_update=normal_update
|
||||||
end
|
end
|
||||||
|
|
||||||
function _update()
|
function _update()
|
||||||
|
end
|
||||||
|
|
||||||
|
function normal_update()
|
||||||
cls(20)
|
cls(20)
|
||||||
prnt(text,x,60)
|
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
|
if x>160 then x=-strlen(text)*4 end
|
||||||
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
|
||||||
|
}
|
||||||
|
|||||||
28
jfile.cpp
28
jfile.cpp
@@ -50,7 +50,7 @@ char *resource_folder = NULL;
|
|||||||
DATA_File *data_file = NULL;
|
DATA_File *data_file = NULL;
|
||||||
int file_source = SOURCE_FILE;
|
int file_source = SOURCE_FILE;
|
||||||
char scratch[255];
|
char scratch[255];
|
||||||
std::string config_folder = NULL;
|
std::string config_folder;
|
||||||
std::vector<keyvalue_t> config;
|
std::vector<keyvalue_t> config;
|
||||||
|
|
||||||
void file_setresourcefilename(const char *str) {
|
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
|
// Crea la carpeta del sistema donde guardar datos
|
||||||
void createSystemFolder(const char *foldername)
|
void file_setconfigfolder(const char *foldername)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
config_folder = std::string(getenv("APPDATA")) + "/" + foldername;
|
config_folder = std::string(getenv("APPDATA")) + "/" + foldername;
|
||||||
@@ -191,14 +191,18 @@ void file_loadconfigvalues() {
|
|||||||
config.clear();
|
config.clear();
|
||||||
std::string config_file = config_folder + "/config.txt";
|
std::string config_file = config_folder + "/config.txt";
|
||||||
FILE *f = fopen(config_file.c_str(), "r");
|
FILE *f = fopen(config_file.c_str(), "r");
|
||||||
if (f) {
|
if (!f) return;
|
||||||
while (!feof(f)) {
|
|
||||||
char key[100], value[100];
|
char line[1024];
|
||||||
fscanf(f, "%s = %S", key, value);
|
while (fgets(line, sizeof(line), f)) {
|
||||||
config.push_back({key, value});
|
char *value = strchr(line, '=');
|
||||||
}
|
if (value) {
|
||||||
fclose(f);
|
*value='\0'; value++;
|
||||||
}
|
value[strlen(value)-1] = '\0';
|
||||||
|
config.push_back({line, value});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void file_saveconfigvalues() {
|
void file_saveconfigvalues() {
|
||||||
@@ -206,7 +210,7 @@ void file_saveconfigvalues() {
|
|||||||
FILE *f = fopen(config_file.c_str(), "w");
|
FILE *f = fopen(config_file.c_str(), "w");
|
||||||
if (f) {
|
if (f) {
|
||||||
for (auto pair : config) {
|
for (auto pair : config) {
|
||||||
fprintf(f, "%s = %s\n", pair.key.c_str(), pair.value.c_str());
|
fprintf(f, "%s=%s\n", pair.key.c_str(), pair.value.c_str());
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
@@ -225,7 +229,7 @@ const char* file_getconfigvalue(const char *key) {
|
|||||||
|
|
||||||
void file_setconfigvalue(const char* key, const char* value) {
|
void file_setconfigvalue(const char* key, const char* value) {
|
||||||
if (config.empty()) file_loadconfigvalues();
|
if (config.empty()) file_loadconfigvalues();
|
||||||
for (auto pair : config) {
|
for (auto &pair : config) {
|
||||||
if (pair.key == std::string(key)) {
|
if (pair.key == std::string(key)) {
|
||||||
pair.value = value;
|
pair.value = value;
|
||||||
file_saveconfigvalues();
|
file_saveconfigvalues();
|
||||||
|
|||||||
2
jfile.h
2
jfile.h
@@ -4,6 +4,8 @@
|
|||||||
#define SOURCE_FILE 0
|
#define SOURCE_FILE 0
|
||||||
#define SOURCE_FOLDER 1
|
#define SOURCE_FOLDER 1
|
||||||
|
|
||||||
|
void file_setconfigfolder(const char *foldername);
|
||||||
|
|
||||||
void file_setresourcefilename(const char *str);
|
void file_setresourcefilename(const char *str);
|
||||||
void file_setresourcefolder(const char *str);
|
void file_setresourcefolder(const char *str);
|
||||||
void file_setsource(const int src);
|
void file_setsource(const int src);
|
||||||
|
|||||||
64
lua.cpp
64
lua.cpp
@@ -445,8 +445,12 @@ extern "C" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int cpp_btnp(lua_State *L) {
|
static int cpp_btnp(lua_State *L) {
|
||||||
uint8_t i = luaL_checkinteger(L, 1);
|
if (lua_gettop(L) >=1 ) {
|
||||||
lua_pushboolean(L, btnp(i));
|
uint8_t i = luaL_checkinteger(L, 1);
|
||||||
|
lua_pushboolean(L, btnp(i));
|
||||||
|
} else {
|
||||||
|
lua_pushinteger(L, wbtnp());
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -696,6 +700,55 @@ extern "C" {
|
|||||||
return 0;
|
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) {
|
static int cpp_exit(lua_State *L) {
|
||||||
exit();
|
exit();
|
||||||
return 0;
|
return 0;
|
||||||
@@ -806,6 +859,13 @@ void push_lua_funcs() {
|
|||||||
lua_pushcfunction(L,cpp_playsound); lua_setglobal(L, "playsound");
|
lua_pushcfunction(L,cpp_playsound); lua_setglobal(L, "playsound");
|
||||||
lua_pushcfunction(L,cpp_stopsound); lua_setglobal(L, "stopsound");
|
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_pushcfunction(L,cpp_exit); lua_setglobal(L, "quit");
|
||||||
|
|
||||||
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");
|
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");
|
||||||
|
|||||||
66
mini.cpp
66
mini.cpp
@@ -19,11 +19,12 @@ struct surface_t {
|
|||||||
|
|
||||||
|
|
||||||
char window_title[256];
|
char window_title[256];
|
||||||
|
char config_folder[256];
|
||||||
uint16_t screen_width = 160;
|
uint16_t screen_width = 160;
|
||||||
uint16_t screen_height = 120;
|
uint16_t screen_height = 120;
|
||||||
uint8_t screen_zoom = 4;
|
uint8_t screen_zoom = 4;
|
||||||
bool screen_fullscreen = false;
|
bool screen_fullscreen = false;
|
||||||
uint8_t show_cursor = SDL_ENABLE;
|
bool screen_cursor = true;
|
||||||
|
|
||||||
surface_t surfaces[10];
|
surface_t surfaces[10];
|
||||||
surface_t *screen_surface = &surfaces[0];
|
surface_t *screen_surface = &surfaces[0];
|
||||||
@@ -100,10 +101,11 @@ void read_ini() {
|
|||||||
if (value != NULL) {
|
if (value != NULL) {
|
||||||
value[strlen(value)-1] = '\0';
|
value[strlen(value)-1] = '\0';
|
||||||
if (strcmp(line, "title") == 0) strcpy(window_title, value);
|
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, "width") == 0) screen_width = atoi(value);
|
||||||
else if (strcmp(line, "height") == 0) screen_height = 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, "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) {
|
else if (strcmp(line, "files") == 0) {
|
||||||
//lua_files = (char*)malloc(strlen(value));
|
//lua_files = (char*)malloc(strlen(value));
|
||||||
strcpy(lua_files, value);
|
strcpy(lua_files, value);
|
||||||
@@ -194,7 +196,7 @@ void createDisplay() {
|
|||||||
mini_ren = SDL_CreateRenderer(mini_win, -1, 0);
|
mini_ren = SDL_CreateRenderer(mini_win, -1, 0);
|
||||||
//SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
|
//SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
|
||||||
SDL_RenderSetLogicalSize(mini_ren, screen_width, screen_height);
|
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);
|
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);
|
file_setresourcefilename(res_file);
|
||||||
#endif
|
#endif
|
||||||
read_ini();
|
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));
|
setdest(newsurf(screen_width, screen_height));
|
||||||
|
|
||||||
SDL_Init(SDL_INIT_EVERYTHING);
|
SDL_Init(SDL_INIT_EVERYTHING);
|
||||||
@@ -248,15 +258,20 @@ int main(int argc,char*argv[]){
|
|||||||
while(SDL_PollEvent(&mini_eve)) {
|
while(SDL_PollEvent(&mini_eve)) {
|
||||||
if (mini_eve.type == SDL_QUIT) { should_exit=true; should_quit=true; break; }
|
if (mini_eve.type == SDL_QUIT) { should_exit=true; should_quit=true; break; }
|
||||||
if (mini_eve.type == SDL_KEYDOWN) {
|
if (mini_eve.type == SDL_KEYDOWN) {
|
||||||
|
/*
|
||||||
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F2) {
|
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F2) {
|
||||||
screen_zoom+=2; if (screen_zoom>=10) screen_zoom=2;
|
screen_zoom+=2; if (screen_zoom>=10) screen_zoom=2;
|
||||||
destroyDisplay();
|
destroyDisplay();
|
||||||
createDisplay();
|
createDisplay();
|
||||||
|
char strzoom[3];
|
||||||
|
file_setconfigvalue("zoom", SDL_itoa(screen_zoom, strzoom, 10));
|
||||||
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F3) {
|
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F3) {
|
||||||
screen_fullscreen = !screen_fullscreen;
|
screen_fullscreen = !screen_fullscreen;
|
||||||
destroyDisplay();
|
destroyDisplay();
|
||||||
createDisplay();
|
createDisplay();
|
||||||
|
file_setconfigvalue("fullscreen", screen_fullscreen?"true":"false");
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F1) {
|
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F1) {
|
||||||
if (lua_is_playing()) {
|
if (lua_is_playing()) {
|
||||||
@@ -846,6 +861,10 @@ bool btn(uint8_t i) {
|
|||||||
return keys[i];
|
return keys[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int wbtnp() {
|
||||||
|
return key_just_pressed;
|
||||||
|
}
|
||||||
|
|
||||||
bool btnp(uint8_t i) {
|
bool btnp(uint8_t i) {
|
||||||
return key_just_pressed == i;
|
return key_just_pressed == i;
|
||||||
}
|
}
|
||||||
@@ -1072,6 +1091,47 @@ void stopsound(int soundchannel) {
|
|||||||
Mix_HaltChannel(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() {
|
void exit() {
|
||||||
should_exit = true;
|
should_exit = true;
|
||||||
|
should_quit = true;
|
||||||
}
|
}
|
||||||
|
|||||||
11
mini.h
11
mini.h
@@ -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);
|
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);
|
bool btn(uint8_t i);
|
||||||
|
int wbtnp();
|
||||||
bool btnp(uint8_t i);
|
bool btnp(uint8_t i);
|
||||||
bool anykey();
|
bool anykey();
|
||||||
|
|
||||||
@@ -267,4 +268,14 @@ void freesound(int soundfile);
|
|||||||
int playsound(int soundfile, const int volume=-1);
|
int playsound(int soundfile, const int volume=-1);
|
||||||
void stopsound(int soundchannel);
|
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();
|
void exit();
|
||||||
|
|||||||
Reference in New Issue
Block a user