redundancy reduced

This commit is contained in:
MihailRis 2024-04-30 14:49:55 +03:00
parent 8c86bcae54
commit 9b843817f8
10 changed files with 136 additions and 146 deletions

View File

@ -1,8 +1,5 @@
function create_setting(id, name, step, postfix) function create_setting(id, name, step, postfix)
local info = core.get_setting_info(id) local info = core.get_setting_info(id)
if postfix == nil then
postfix = ""
end
document.settings_panel:add(gui.template("track_setting", { document.settings_panel:add(gui.template("track_setting", {
id=id, id=id,
name=gui.str(name, "settings"), name=gui.str(name, "settings"),
@ -10,7 +7,7 @@ function create_setting(id, name, step, postfix)
min=info.min, min=info.min,
max=info.max, max=info.max,
step=step, step=step,
postfix=postfix postfix=postfix or ""
})) }))
end end
@ -41,6 +38,6 @@ function on_open()
create_checkbox("camera.shaking", "Camera Shaking") create_checkbox("camera.shaking", "Camera Shaking")
document.langs_btn.text = string.format( document.langs_btn.text = string.format(
"%s: %s", gui.str("Language", "settings"), "%s: %s", gui.str("Language", "settings"),
gui.get_locales_info()[gui.get_locale()].name gui.get_locales_info()[core.get_setting("ui.language")].name
) )
end end

View File

@ -352,52 +352,52 @@ namespace audio {
/// @brief Initialize audio system or use no audio mode /// @brief Initialize audio system or use no audio mode
/// @param enabled try to initialize actual audio /// @param enabled try to initialize actual audio
extern void initialize(bool enabled); void initialize(bool enabled);
/// @brief Load audio file info and PCM data /// @brief Load audio file info and PCM data
/// @param file audio file /// @param file audio file
/// @param headerOnly read header only /// @param headerOnly read header only
/// @throws std::runtime_error if I/O error ocurred or format is unknown /// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return PCM audio data /// @return PCM audio data
extern PCM* load_PCM(const fs::path& file, bool headerOnly); PCM* load_PCM(const fs::path& file, bool headerOnly);
/// @brief Load sound from file /// @brief Load sound from file
/// @param file audio file path /// @param file audio file path
/// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM /// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM
/// @throws std::runtime_error if I/O error ocurred or format is unknown /// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return new Sound instance /// @return new Sound instance
extern Sound* load_sound(const fs::path& file, bool keepPCM); Sound* load_sound(const fs::path& file, bool keepPCM);
/// @brief Create new sound from PCM data /// @brief Create new sound from PCM data
/// @param pcm PCM data /// @param pcm PCM data
/// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM /// @param keepPCM store PCM data in sound to make it accessible with Sound::getPCM
/// @return new Sound instance /// @return new Sound instance
extern Sound* create_sound(std::shared_ptr<PCM> pcm, bool keepPCM); Sound* create_sound(std::shared_ptr<PCM> pcm, bool keepPCM);
/// @brief Open new PCM stream from file /// @brief Open new PCM stream from file
/// @param file audio file path /// @param file audio file path
/// @throws std::runtime_error if I/O error ocurred or format is unknown /// @throws std::runtime_error if I/O error ocurred or format is unknown
/// @return new PCMStream instance /// @return new PCMStream instance
extern PCMStream* open_PCM_stream(const fs::path& file); PCMStream* open_PCM_stream(const fs::path& file);
/// @brief Open new audio stream from file /// @brief Open new audio stream from file
/// @param file audio file path /// @param file audio file path
/// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource /// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource
/// @return new Stream instance /// @return new Stream instance
extern Stream* open_stream(const fs::path& file, bool keepSource); Stream* open_stream(const fs::path& file, bool keepSource);
/// @brief Open new audio stream from source /// @brief Open new audio stream from source
/// @param stream PCM data source /// @param stream PCM data source
/// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource /// @param keepSource store PCMStream in stream to make it accessible with Stream::getSource
/// @return new Stream instance /// @return new Stream instance
extern Stream* open_stream(std::shared_ptr<PCMStream> stream, bool keepSource); Stream* open_stream(std::shared_ptr<PCMStream> stream, bool keepSource);
/// @brief Configure 3D listener /// @brief Configure 3D listener
/// @param position listener position /// @param position listener position
/// @param velocity listener velocity (used for Doppler effect) /// @param velocity listener velocity (used for Doppler effect)
/// @param lookAt point the listener look at /// @param lookAt point the listener look at
/// @param up camera up vector /// @param up camera up vector
extern void set_listener( void set_listener(
glm::vec3 position, glm::vec3 position,
glm::vec3 velocity, glm::vec3 velocity,
glm::vec3 lookAt, glm::vec3 lookAt,
@ -415,7 +415,7 @@ namespace audio {
/// (PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH) /// (PRIORITY_LOW, PRIORITY_NORMAL, PRIORITY_HIGH)
/// @param channel channel index /// @param channel channel index
/// @return speaker id or 0 /// @return speaker id or 0
extern speakerid_t play( speakerid_t play(
Sound* sound, Sound* sound,
glm::vec3 position, glm::vec3 position,
bool relative, bool relative,
@ -435,7 +435,7 @@ namespace audio {
/// @param loop loop stream /// @param loop loop stream
/// @param channel channel index /// @param channel channel index
/// @return speaker id or 0 /// @return speaker id or 0
extern speakerid_t play( speakerid_t play(
std::shared_ptr<Stream> stream, std::shared_ptr<Stream> stream,
glm::vec3 position, glm::vec3 position,
bool relative, bool relative,
@ -454,7 +454,7 @@ namespace audio {
/// @param loop loop stream /// @param loop loop stream
/// @param channel channel index /// @param channel channel index
/// @return speaker id or 0 /// @return speaker id or 0
extern speakerid_t play_stream( speakerid_t play_stream(
const fs::path& file, const fs::path& file,
glm::vec3 position, glm::vec3 position,
bool relative, bool relative,
@ -467,49 +467,49 @@ namespace audio {
/// @brief Get speaker by id /// @brief Get speaker by id
/// @param id speaker id /// @param id speaker id
/// @return speaker or nullptr /// @return speaker or nullptr
extern Speaker* get_speaker(speakerid_t id); Speaker* get_speaker(speakerid_t id);
/// @brief Create new channel. /// @brief Create new channel.
/// All non-builtin channels will be destroyed on audio::reset() call /// All non-builtin channels will be destroyed on audio::reset() call
/// @param name channel name /// @param name channel name
/// @return new channel index /// @return new channel index
extern int create_channel(const std::string& name); int create_channel(const std::string& name);
/// @brief Get channel index by name /// @brief Get channel index by name
/// @param name channel name /// @param name channel name
/// @return channel index or -1 /// @return channel index or -1
extern int get_channel_index(const std::string& name); int get_channel_index(const std::string& name);
/// @brief Get channel by index. 0 - is master channel /// @brief Get channel by index. 0 - is master channel
/// @param index channel index /// @param index channel index
/// @return channel or nullptr /// @return channel or nullptr
extern Channel* get_channel(int index); Channel* get_channel(int index);
/// @brief Get channel by name. /// @brief Get channel by name.
/// @param name channel name /// @param name channel name
/// @return channel or nullptr /// @return channel or nullptr
extern Channel* get_channel(const std::string& name); Channel* get_channel(const std::string& name);
/// @brief Get stream associated with speaker /// @brief Get stream associated with speaker
/// @param id speaker id /// @param id speaker id
/// @return stream or nullptr /// @return stream or nullptr
extern std::shared_ptr<Stream> get_associated_stream(speakerid_t id); std::shared_ptr<Stream> get_associated_stream(speakerid_t id);
/// @brief Get alive speakers number (including paused) /// @brief Get alive speakers number (including paused)
extern size_t count_speakers(); size_t count_speakers();
/// @brief Get playing streams number (including paused) /// @brief Get playing streams number (including paused)
extern size_t count_streams(); size_t count_streams();
/// @brief Update audio streams and sound instanced /// @brief Update audio streams and sound instanced
/// @param delta time elapsed since the last update (seconds) /// @param delta time elapsed since the last update (seconds)
extern void update(double delta); void update(double delta);
/// @brief Stop all playing audio in channel, reset channel state /// @brief Stop all playing audio in channel, reset channel state
extern void reset_channel(int channel); void reset_channel(int channel);
/// @brief Finalize audio system /// @brief Finalize audio system
extern void close(); void close();
}; };
#endif // AUDIO_AUDIO_H_ #endif // AUDIO_AUDIO_H_

View File

@ -29,8 +29,8 @@ inline const std::string BIND_HUD_INVENTORY = "hud.inventory";
class ContentBuilder; class ContentBuilder;
namespace corecontent { namespace corecontent {
extern void setup_bindings(); void setup_bindings();
extern void setup(ContentBuilder* builder); void setup(ContentBuilder* builder);
} }
#endif // CORE_DEFS_H_ #endif // CORE_DEFS_H_

View File

@ -61,7 +61,7 @@ bool files::read(fs::path filename, char* data, size_t size) {
return true; return true;
} }
ubyte* files::read_bytes(fs::path filename, size_t& length) { std::unique_ptr<ubyte[]> files::read_bytes(fs::path filename, size_t& length) {
std::ifstream input(filename, std::ios::binary); std::ifstream input(filename, std::ios::binary);
if (!input.is_open()) if (!input.is_open())
return nullptr; return nullptr;
@ -69,10 +69,10 @@ ubyte* files::read_bytes(fs::path filename, size_t& length) {
length = input.tellg(); length = input.tellg();
input.seekg(0, std::ios_base::beg); input.seekg(0, std::ios_base::beg);
std::unique_ptr<char> data(new char[length]); auto data = std::make_unique<ubyte[]>(length);
input.read(data.get(), length); input.read((char*)data.get(), length);
input.close(); input.close();
return (ubyte*)data.release(); return data;
} }
std::string files::read_string(fs::path filename) { std::string files::read_string(fs::path filename) {

View File

@ -31,20 +31,20 @@ namespace files {
/// @param file target file /// @param file target file
/// @param data data bytes array /// @param data data bytes array
/// @param size size of data bytes array /// @param size size of data bytes array
extern bool write_bytes(fs::path file, const ubyte* data, size_t size); bool write_bytes(fs::path file, const ubyte* data, size_t size);
/// @brief Append bytes array to the file without any extra data /// @brief Append bytes array to the file without any extra data
/// @param file target file /// @param file target file
/// @param data data bytes array /// @param data data bytes array
/// @param size size of data bytes array /// @param size size of data bytes array
extern uint append_bytes(fs::path file, const ubyte* data, size_t size); uint append_bytes(fs::path file, const ubyte* data, size_t size);
/// @brief Write string to the file /// @brief Write string to the file
extern bool write_string(fs::path filename, const std::string content); bool write_string(fs::path filename, const std::string content);
/// @brief Write dynamic data to the JSON file /// @brief Write dynamic data to the JSON file
/// @param nice if true, human readable format will be used, otherwise minimal /// @param nice if true, human readable format will be used, otherwise minimal
extern bool write_json( bool write_json(
fs::path filename, fs::path filename,
const dynamic::Map* obj, const dynamic::Map* obj,
bool nice=true); bool nice=true);
@ -52,21 +52,21 @@ namespace files {
/// @brief Write dynamic data to the binary JSON file /// @brief Write dynamic data to the binary JSON file
/// (see src/coders/binary_json_spec.md) /// (see src/coders/binary_json_spec.md)
/// @param compressed use gzip compression /// @param compressed use gzip compression
extern bool write_binary_json( bool write_binary_json(
fs::path filename, fs::path filename,
const dynamic::Map* obj, const dynamic::Map* obj,
bool compressed=false bool compressed=false
); );
extern bool read(fs::path, char* data, size_t size); bool read(fs::path, char* data, size_t size);
extern ubyte* read_bytes(fs::path, size_t& length); std::unique_ptr<ubyte[]> read_bytes(fs::path, size_t& length);
extern std::string read_string(fs::path filename); std::string read_string(fs::path filename);
/// @brief Read JSON or BJSON file /// @brief Read JSON or BJSON file
/// @param file *.json or *.bjson file /// @param file *.json or *.bjson file
extern std::unique_ptr<dynamic::Map> read_json(fs::path file); std::unique_ptr<dynamic::Map> read_json(fs::path file);
extern std::unique_ptr<dynamic::Map> read_binary_json(fs::path file); std::unique_ptr<dynamic::Map> read_binary_json(fs::path file);
extern std::vector<std::string> read_list(fs::path file); std::vector<std::string> read_list(fs::path file);
} }
#endif /* FILES_FILES_H_ */ #endif /* FILES_FILES_H_ */

View File

@ -25,7 +25,7 @@ void menus::create_version_label(Engine* engine) {
auto gui = engine->getGUI(); auto gui = engine->getGUI();
auto text = ENGINE_VERSION_STRING+" development build"; auto text = ENGINE_VERSION_STRING+" development build";
gui->add(guiutil::create( gui->add(guiutil::create(
"<label z-index='1000' color='#FFFFFF80' gravity='top-right' margin='4'>" "<label z-index='1000' color='#FFFFFF80' gravity='bottom-left' margin='4'>"
+text+ +text+
"</label>" "</label>"
)); ));

View File

@ -90,9 +90,9 @@ static int l_file_mkdirs(lua_State* L) {
static int l_file_read_bytes(lua_State* L) { static int l_file_read_bytes(lua_State* L) {
fs::path path = resolve_path(L, lua_tostring(L, 1)); fs::path path = resolve_path(L, lua_tostring(L, 1));
if (fs::is_regular_file(path)) { if (fs::is_regular_file(path)) {
size_t length = (size_t) fs::file_size(path); size_t length = static_cast<size_t>(fs::file_size(path));
ubyte* bytes = files::read_bytes(path, length); auto bytes = files::read_bytes(path, length);
lua_createtable(L, length, 0); lua_createtable(L, length, 0);
int newTable = lua_gettop(L); int newTable = lua_gettop(L);

View File

@ -413,12 +413,6 @@ static int l_gui_get_locales_info(lua_State* L) {
return 1; return 1;
} }
/// @brief gui.get_locale() -> string
static int l_gui_get_locale(lua_State* L) {
lua_pushstring(L, langs::current->getId().c_str());
return 1;
}
const luaL_Reg guilib [] = { const luaL_Reg guilib [] = {
{"get_viewport", lua_wrap_errors<l_gui_getviewport>}, {"get_viewport", lua_wrap_errors<l_gui_getviewport>},
{"getattr", lua_wrap_errors<l_gui_getattr>}, {"getattr", lua_wrap_errors<l_gui_getattr>},
@ -426,7 +420,6 @@ const luaL_Reg guilib [] = {
{"get_env", lua_wrap_errors<l_gui_get_env>}, {"get_env", lua_wrap_errors<l_gui_get_env>},
{"str", lua_wrap_errors<l_gui_str>}, {"str", lua_wrap_errors<l_gui_str>},
{"reindex", lua_wrap_errors<l_gui_reindex>}, {"reindex", lua_wrap_errors<l_gui_reindex>},
{"get_locale", lua_wrap_errors<l_gui_get_locale>},
{"get_locales_info", lua_wrap_errors<l_gui_get_locales_info>}, {"get_locales_info", lua_wrap_errors<l_gui_get_locales_info>},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -4,9 +4,9 @@
#include <string> #include <string>
namespace platform { namespace platform {
extern void configure_encoding(); void configure_encoding();
// @return environment locale in ISO format ll_CC // @return environment locale in ISO format ll_CC
extern std::string detect_locale(); std::string detect_locale();
} }
#endif // UTIL_PLATFORM_H_ #endif // UTIL_PLATFORM_H_

View File

@ -7,55 +7,55 @@
namespace util { namespace util {
/// @brief Function used for string serialization in text formats /// @brief Function used for string serialization in text formats
extern std::string escape(const std::string& s); std::string escape(const std::string& s);
/// @brief Function used for error messages /// @brief Function used for error messages
extern std::string quote(const std::string& s); std::string quote(const std::string& s);
extern std::wstring lfill(std::wstring s, uint length, wchar_t c); std::wstring lfill(std::wstring s, uint length, wchar_t c);
extern std::wstring rfill(std::wstring s, uint length, wchar_t c); std::wstring rfill(std::wstring s, uint length, wchar_t c);
extern uint encode_utf8(uint32_t c, ubyte* bytes); uint encode_utf8(uint32_t c, ubyte* bytes);
extern uint32_t decode_utf8(uint& size, const char* bytes); uint32_t decode_utf8(uint& size, const char* bytes);
extern std::string wstr2str_utf8(const std::wstring ws); std::string wstr2str_utf8(const std::wstring ws);
extern std::wstring str2wstr_utf8(const std::string s); std::wstring str2wstr_utf8(const std::string s);
extern bool is_integer(std::string text); bool is_integer(std::string text);
extern bool is_integer(std::wstring text); bool is_integer(std::wstring text);
extern bool is_valid_filename(std::wstring name); bool is_valid_filename(std::wstring name);
extern void ltrim(std::string &s); void ltrim(std::string &s);
extern void rtrim(std::string &s); void rtrim(std::string &s);
extern void trim(std::string &s); void trim(std::string &s);
extern std::string to_string(double x); std::string to_string(double x);
extern std::wstring to_wstring(double x, int precision); std::wstring to_wstring(double x, int precision);
extern std::string base64_encode(const ubyte* data, size_t size); std::string base64_encode(const ubyte* data, size_t size);
extern std::vector<ubyte> base64_decode(const char* str, size_t size); std::vector<ubyte> base64_decode(const char* str, size_t size);
extern std::vector<ubyte> base64_decode(const std::string& str); std::vector<ubyte> base64_decode(const std::string& str);
extern std::string mangleid(uint64_t value); std::string mangleid(uint64_t value);
extern int replaceAll(std::string& str, const std::string& from, const std::string& to); int replaceAll(std::string& str, const std::string& from, const std::string& to);
extern double parse_double(const std::string& str); double parse_double(const std::string& str);
extern double parse_double(const std::string& str, size_t offset, size_t len); double parse_double(const std::string& str, size_t offset, size_t len);
extern std::wstring lower_case(const std::wstring& str); std::wstring lower_case(const std::wstring& str);
extern std::wstring upper_case(const std::wstring& str); std::wstring upper_case(const std::wstring& str);
extern std::wstring capitalized(const std::wstring& str); std::wstring capitalized(const std::wstring& str);
extern std::wstring pascal_case(const std::wstring& str); std::wstring pascal_case(const std::wstring& str);
/// @brief Convert `any_prefix:some_data_id` to `some data id`. Leaves /// @brief Convert `any_prefix:some_data_id` to `some data id`. Leaves
/// '_' characters at end of the id. /// '_' characters at end of the id.
/// @param id source id /// @param id source id
/// @return resulting caption or empty string if there's nothing but prefix /// @return resulting caption or empty string if there's nothing but prefix
extern std::string id_to_caption(const std::string& id); std::string id_to_caption(const std::string& id);
extern std::vector<std::string> split(const std::string& str, char delimiter); std::vector<std::string> split(const std::string& str, char delimiter);
extern std::vector<std::wstring> split(const std::wstring& str, char delimiter); std::vector<std::wstring> split(const std::wstring& str, char delimiter);
extern std::string format_data_size(size_t size); std::string format_data_size(size_t size);
} }
#endif // UTIL_STRINGUTIL_H_ #endif // UTIL_STRINGUTIL_H_