Use callback API when applicable, implement MusicStream from payload.

Update proper example.
This commit is contained in:
TSnake41 2021-05-01 22:30:32 +02:00
parent 004ac65b28
commit 3c68c3cfb9
4 changed files with 83 additions and 49 deletions

View File

@ -5,11 +5,13 @@ rl.InitWindow(800, 450, "raylib [shapes] example - basic shapes drawing")
rl.InitAudioDevice()
local logo = rl.LoadTexture "ressources/logo.png"
local music = rl.LoadSound "ressources/mini1111.ogg"
local music = rl.LoadMusicStream "ressources/mini1111.xm"
rl.PlaySound(music)
rl.PlayMusicStream(music)
while not rl.WindowShouldClose() do
rl.UpdateMusicStream(music)
rl.BeginDrawing()
rl.ClearBackground(rl.RAYWHITE)
@ -19,5 +21,7 @@ while not rl.WindowShouldClose() do
rl.EndDrawing()
end
rl.UnloadMusicStream(music)
rl.CloseAudioDevice()
rl.CloseWindow()

View File

@ -18,60 +18,19 @@ local new = ffi.new
-- Load*() wrappers.
if raylua.loadfile then
local LoadImage = rl.LoadImage
function rl.LoadImage(path)
local LoadMusicStream = rl.LoadMusicStream
function rl.LoadMusicStream(path)
local f, err = raylua.loadfile(path)
if f then
local ext = path:gsub(".+%.", "")
local ext = "." .. path:gsub(".+%.", "")
return rl.LoadImageFromMemory(ext, f, #f)
return rl.LoadMusicStreamFromMemory(ext, ffi.cast("void *", f), #f)
else
print(("RAYLUA: %s"):format(err))
return LoadImage(path)
return LoadMusicStream(path)
end
end
function rl.LoadTexture(path)
return rl.LoadTextureFromImage(rl.LoadImage(path))
end
local LoadFont, LoadFontEx = rl.LoadFont, rl.LoadFontEx
function rl.LoadFontEx(path, sz, chars, count)
local f, err = raylua.loadfile(path)
if f then
local ext = path:gsub(".+%.", "")
return rl.LoadFontFromMemory(ext, f, #f, sz, chars, count)
else
return LoadFontEx(sz, chars, count)
end
end
function rl.LoadFont(path)
-- HACK: Hardcoded values (FONT_TTF_DEFAULT_SIZE,
-- FONT_TTF_DEFAULT_NUMCHARS)
return rl.LoadFontEx(path, 32, nil, 95)
end
local LoadWave = rl.LoadWave
function rl.LoadWave(path)
local f, err = raylua.loadfile(path)
if f then
local ext = path:gsub(".+%.", "")
return rl.LoadWaveFromMemory(ext, f, #f)
else
print(("RAYLUA: %s"):format(err))
return LoadWave(path)
end
end
function rl.LoadSound(path)
return rl.LoadSoundFromWave(rl.LoadWave(path))
end
end
-- math metamethods

View File

@ -62,7 +62,12 @@ int raylua_loadfile(lua_State *L)
return 2;
}
mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0);
if (!mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0)) {
free(buffer);
lua_pushnil(L);
lua_pushfstring(L, "%s: Can't extract file.", path);
return 2;
}
lua_pushlstring(L, buffer, size);
free(buffer);
@ -88,6 +93,69 @@ int raylua_listfiles(lua_State *L)
return 1;
}
unsigned char *raylua_loadFileData(const char *path, unsigned int *out_size)
{
int index = mz_zip_reader_locate_file(&zip_file, path, NULL, 0);
if (index == -1) {
printf("RAYLUA: WARN: File not found in payload. '%s'\n", path);
return NULL;
}
mz_zip_archive_file_stat stat;
if (!mz_zip_reader_file_stat(&zip_file, index, &stat)) {
printf("RAYLUA: WARN: Can't get file information in payload. '%s'\n", path);
return NULL;
}
size_t size = stat.m_uncomp_size;
unsigned char *buffer = RL_MALLOC(size);
if (buffer == NULL) {
printf("RAYLUA: WARN: Can't allocate file buffer. '%s'\n", path);
return NULL;
}
if (!mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0)) {
free(buffer);
printf("RAYLUA: WARN: Can't extract file. '%s'\n", path);
return NULL;
}
*out_size = size;
return buffer;
}
char *raylua_loadFileText(const char *path)
{
int index = mz_zip_reader_locate_file(&zip_file, path, NULL, 0);
if (index == -1) {
printf("RAYLUA: WARN: File not found in payload. '%s'\n", path);
return NULL;
}
mz_zip_archive_file_stat stat;
if (!mz_zip_reader_file_stat(&zip_file, index, &stat)) {
printf("RAYLUA: WARN: Can't get file information in payload. '%s'\n", path);
return NULL;
}
size_t size = stat.m_uncomp_size;
char *buffer = RL_MALLOC(size + 1);
if (buffer == NULL) {
printf("RAYLUA: WARN: Can't allocate file buffer. '%s'\n", path);
return NULL;
}
buffer[size] = '\0';
if (!mz_zip_reader_extract_to_mem(&zip_file, index, buffer, size, 0)) {
free(buffer);
printf("RAYLUA: WARN: Can't extract file. '%s'\n", path);
return NULL;
}
return buffer;
}
static bool raylua_init_payload(FILE *self)
{
mz_zip_zero_struct(&zip_file);
@ -127,6 +195,9 @@ int main(int argc, const char **argv)
return 1;
}
SetLoadFileDataCallback(raylua_loadFileData);
SetLoadFileTextCallback(raylua_loadFileText);
if (!raylua_init_payload(self)) {
#ifdef RAYLUA_NO_BUILDER
puts("RAYLUA: No payload.");