Load assets from a .zip in romfs (romfs/ dir for pc-build) using minizip, instead of embedding it in data/. Sync Makefile.nx with latest switch-examples Makefile, with romfs. Automatically build the assets .zip in Makefile. Moved data/ to assets/ and moved data/unused/ to assets_unused/. Use #error in nx_audio.c since it's not supported currently.

This commit is contained in:
yellows8 2018-10-28 22:31:16 -04:00
parent bc6d98a534
commit bb53a8ac81
34 changed files with 230 additions and 125 deletions

View File

@ -8,15 +8,21 @@ endif
all: nx pc
dist-bin:
romfs : assets
@mkdir -p romfs
@rm -f romfs/assets.zip
@zip -rj romfs/assets.zip assets
dist-bin: romfs
$(MAKE) -f Makefile.nx dist-bin
nx:
nx: romfs
$(MAKE) -f Makefile.nx
pc:
pc: romfs
$(MAKE) -f Makefile.pc
clean:
@rm -Rf romfs
$(MAKE) -f Makefile.pc clean
$(MAKE) -f Makefile.nx clean

View File

@ -16,6 +16,7 @@ include $(DEVKITPRO)/libnx/switch_rules
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm".
# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional)
#
# NO_ICON: if set to anything, do not use icon.
# NO_NACP: if set to anything, no .nacp file is generated.
@ -35,6 +36,7 @@ SOURCES := common/ nx_main/ nx_main/loaders/
DATA := data
INCLUDES := include
EXEFS_SRC := exefs_src
ROMFS := romfs
DIST_PATH := $(TARGET)_v$(APP_VERSION)
@ -48,12 +50,12 @@ CFLAGS := -g -Wall -O2 -ffunction-sections \
CFLAGS += $(INCLUDE) -D__SWITCH__ -DVERSION=\"v$(APP_VERSION)\"
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS := `freetype-config --libs` -lconfig -lturbojpeg
LIBS := -lminizip `freetype-config --libs` -lconfig -lturbojpeg
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
@ -134,6 +136,10 @@ ifneq ($(APP_TITLEID),)
export NACPFLAGS += --titleid=$(APP_TITLEID)
endif
ifneq ($(ROMFS),)
export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS)
endif
.PHONY: $(BUILD) clean all dist-bin
#---------------------------------------------------------------------------------

View File

@ -9,97 +9,12 @@ EXTRA_CFLAGS="-D__USE_MINGW_ANSI_STDIO"
EXTRA_LDFLAGS="-lws2_32"
endif
# canned command sequence for binary data
#---------------------------------------------------------------------------------
define bin2o
bin2s $(BIN2S_FLAGS) $< | $(AS) -o $(@)
echo "extern const u8" `(echo $(<F) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(<F) | tr . _)`.h
echo "extern const u8" `(echo $(<F) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(<F) | tr . _)`.h
echo "extern const u32" `(echo $(<F) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(<F) | tr . _)`.h
endef
test : pc_main/main.cpp pc_main/pc_launch.c pc_main/pc_power.c \
common/menu.c common/font.c common/language.c common/launch.c common/worker.c \
common/menu-entry.c common/menu-list.c common/message-box.c common/text.c \
common/ui.c common/math.c common/theme.c \
common/netloader.c \
build_pc/invalid_icon.bin.o build_pc/folder_icon.bin.o \
build_pc/hbmenu_logo_light.bin.o build_pc/hbmenu_logo_dark.bin.o \
build_pc/theme_icon_dark.bin.o build_pc/theme_icon_light.bin.o \
build_pc/charging_icon.bin.o build_pc/battery_icon.bin.o \
#build_pc/tahoma24.o build_pc/tahoma12.o build_pc/interuimedium20.o build_pc/interuimedium30.o build_pc/interuiregular14.o build_pc/interuiregular18.o
gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) `pkg-config freetype2 --cflags` $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ -lpthread `pkg-config freetype2 --libs` -lm -lz -lconfig -lturbojpeg $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@
build_pc/tahoma12.o : data/tahoma12.nxfnt
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/tahoma24.o : data/tahoma24.nxfnt
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/interuimedium20.o : data/interuimedium20.nxfnt
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/interuimedium30.o : data/interuimedium30.nxfnt
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/interuiregular14.o : data/interuiregular14.nxfnt
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/interuiregular18.o : data/interuiregular18.nxfnt
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/invalid_icon.bin.o : data/invalid_icon.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/folder_icon.bin.o : data/folder_icon.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/hbmenu_logo_light.bin.o : data/hbmenu_logo_light.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/hbmenu_logo_dark.bin.o : data/hbmenu_logo_dark.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/theme_icon_light.bin.o : data/theme_icon_light.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/theme_icon_dark.bin.o : data/theme_icon_dark.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/charging_icon.bin.o : data/charging_icon.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
build_pc/battery_icon.bin.o : data/battery_icon.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)
common/ui.c common/assets.c common/math.c common/theme.c \
common/netloader.c
gcc -Wall -O2 -g -DVERSION=\"v$(APP_VERSION)\" $(EXTRA_CFLAGS) `pkg-config freetype2 --cflags` $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ -lpthread `pkg-config freetype2 --libs` -lm -lminizip -lz -lconfig -lturbojpeg $(EXTRA_LDFLAGS) -I. -iquote $(DEVKITPRO)/libnx/include -Ibuild_pc -g -o $@
clean:
rm -rf build_pc/ test test.*

View File

@ -7,7 +7,7 @@ The latest release is available from the [releases](https://github.com/switchbre
#### Building
Build with ```make nx``` or just run ```make```.
The following is required to build: libfreetype (switch-freetype), libconfig (switch-libconfig), and libjpeg-turbo (switch-libjpeg-turbo). Where "({name})" is the pacman package.
The following is required to build: libfreetype (switch-freetype), libconfig (switch-libconfig), and libjpeg-turbo (switch-libjpeg-turbo). Where "({name})" is the pacman package. For the pc-build libminizip is required (for the Switch build, the switch-zlib package includes this).
C11-threads are used, hence building for the pc-build may fail if C11-threads are not available.

162
common/assets.c Normal file
View File

@ -0,0 +1,162 @@
#include "common.h"
#include <minizip/unzip.h>
typedef struct {
u8 *buffer;
size_t size;
const char *filename;
} assetsDataEntry;
#define GENASSET(x) {.filename = x}
static bool g_assetsInitialized = 0;
assetsDataEntry g_assetsDataList[AssetId_Max] = {
GENASSET("battery_icon.bin"),
GENASSET("charging_icon.bin"),
GENASSET("folder_icon.bin"),
GENASSET("invalid_icon.bin"),
GENASSET("hbmenu_logo_dark.bin"),
GENASSET("hbmenu_logo_light.bin"),
GENASSET("theme_icon_dark.bin"),
GENASSET("theme_icon_light.bin"),
};
static void assetsClearEntry(assetsDataEntry *entry) {
free(entry->buffer);
entry->size = 0;
entry->buffer = NULL;
}
static int assetsLoadFile(unzFile zipf, assetsDataEntry *entry) {
int ret;
int filesize=0;
unz_file_info file_info;
u8* buffer = NULL;
ret = unzLocateFile(zipf, entry->filename, 0);
if (ret==UNZ_OK) ret = unzOpenCurrentFile(zipf);
if (ret==UNZ_OK) {
ret = unzGetCurrentFileInfo(zipf, &file_info, NULL, 0, NULL, 0, NULL, 0);
filesize = file_info.uncompressed_size;
if (filesize == 0) ret = -10;
if (ret==UNZ_OK) {
buffer = (u8*)malloc(filesize);
if (buffer) {
memset(buffer, 0, filesize);
} else {
ret = -11;
}
}
if (ret==UNZ_OK) {
ret = unzReadCurrentFile(zipf, buffer, filesize);
if(ret < filesize) {
ret = -12;
} else {
ret = UNZ_OK;
}
}
if (ret!=UNZ_OK && buffer!=NULL) free(buffer);
unzCloseCurrentFile(zipf);
}
if (ret==UNZ_OK) {
entry->buffer = buffer;
entry->size = filesize;
}
return ret;
}
Result assetsInit(void) {
int ret=0;
int i, stopi;
unzFile zipf;
assetsDataEntry *entry = NULL;
char tmp_path[PATH_MAX+1];
if (g_assetsInitialized) return 0;
#ifdef __SWITCH__
Result rc = romfsInit();
if (R_FAILED(rc)) return rc;
#endif
memset(tmp_path, 0, sizeof(tmp_path));
#ifdef __SWITCH__
strncpy(tmp_path, "romfs:/assets.zip", sizeof(tmp_path)-1);
#else
snprintf(tmp_path, sizeof(tmp_path)-1, "%s/romfs/assets.zip", menuGetRootBasePath());
#endif
zipf = unzOpen(tmp_path);
if(zipf==NULL) {
#ifdef __SWITCH__
romfsExit();
#endif
return 0x80;
}
for (i=0; i<AssetId_Max; i++) {
stopi = i;
entry = &g_assetsDataList[i];
ret = assetsLoadFile(zipf, entry);
if (ret!=UNZ_OK) break;
}
if (ret!=UNZ_OK) {
for (i=0; i<stopi; i++) {
assetsClearEntry(&g_assetsDataList[i]);
}
}
if (ret==UNZ_OK) g_assetsInitialized = 1;
unzClose(zipf);
#ifdef __SWITCH__
romfsExit();
#endif
return ret;
}
void assetsExit(void) {
int i;
if (!g_assetsInitialized) return;
g_assetsInitialized = 0;
for (i=0; i<AssetId_Max; i++) {
assetsClearEntry(&g_assetsDataList[i]);
}
}
void assetsGetData(AssetId id, u8 **buffer, size_t *size) {
if (buffer) *buffer = NULL;
if (size) *size = 0;
if (id < 0 || id >= AssetId_Max) return;
assetsDataEntry *entry = &g_assetsDataList[id];
if (buffer) *buffer = entry->buffer;
if (size) *size = entry->size;
}
u8 *assetsGetDataBuffer(AssetId id) {
u8 *buffer = NULL;
assetsGetData(id, &buffer, NULL);
return buffer;
}

21
common/assets.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include "common.h"
typedef enum {
AssetId_battery_icon,
AssetId_charging_icon,
AssetId_folder_icon,
AssetId_invalid_icon,
AssetId_hbmenu_logo_dark,
AssetId_hbmenu_logo_light,
AssetId_theme_icon_dark,
AssetId_theme_icon_light,
AssetId_Max
} AssetId;
Result assetsInit(void);
void assetsExit(void);
void assetsGetData(AssetId id, u8 **buffer, size_t *size);
u8 *assetsGetDataBuffer(AssetId id);

View File

@ -55,6 +55,7 @@ typedef union {
#include "menu.h"
#include "text.h"
#include "ui.h"
#include "assets.h"
#include "launch.h"
#include "worker.h"
#include <turbojpeg.h>

View File

@ -6,13 +6,6 @@
#include "switch/runtime/nxlink.h"
#endif
#include "invalid_icon_bin.h"
#include "folder_icon_bin.h"
#include "theme_icon_dark_bin.h"
#include "theme_icon_light_bin.h"
#include "charging_icon_bin.h"
#include "battery_icon_bin.h"
char rootPathBase[PATH_MAX];
char rootPath[PATH_MAX+8];
void computeFrontGradient(color_t baseColor, int height);
@ -238,17 +231,17 @@ static void drawEntry(menuEntry_s* me, int off_x, int is_active) {
}
else if (me->type == ENTRY_TYPE_FOLDER) {
smallimg = folder_icon_small;
largeimg = folder_icon_bin;
largeimg = assetsGetDataBuffer(AssetId_folder_icon);
}
else if (me->type == ENTRY_TYPE_THEME){
smallimg = theme_icon_small;
if(themeGlobalPreset == THEME_PRESET_DARK)
largeimg = theme_icon_dark_bin;
else largeimg = theme_icon_light_bin;
largeimg = assetsGetDataBuffer(AssetId_theme_icon_dark);
else largeimg = assetsGetDataBuffer(AssetId_theme_icon_light);
}
else {
smallimg = invalid_icon_small;
largeimg = invalid_icon_bin;
largeimg = assetsGetDataBuffer(AssetId_invalid_icon);
}
if (smallimg) {
@ -357,12 +350,12 @@ void menuStartup(void) {
menuScan(rootPath);
folder_icon_small = downscaleImg(folder_icon_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24);
invalid_icon_small = downscaleImg(invalid_icon_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24);
folder_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_folder_icon), 256, 256, 140, 140, IMAGE_MODE_RGB24);
invalid_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_invalid_icon), 256, 256, 140, 140, IMAGE_MODE_RGB24);
if(themeGlobalPreset == THEME_PRESET_DARK)
theme_icon_small = downscaleImg(theme_icon_dark_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24);
theme_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_theme_icon_dark), 256, 256, 140, 140, IMAGE_MODE_RGB24);
else
theme_icon_small = downscaleImg(theme_icon_light_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24);
theme_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_theme_icon_light), 256, 256, 140, 140, IMAGE_MODE_RGB24);
computeFrontGradient(themeCurrent.frontWaveColor, 280);
//menuCreateMsgBox(780, 300, "This is a test");
}
@ -457,9 +450,9 @@ void drawCharge() {
int tmpX = GetTextXCoordinate(interuiregular14, 1180, chargeString, 'r');
DrawText(interuiregular14, tmpX - 15, 0 + 47 + 10 + 21, themeCurrent.textColor, chargeString);
drawIcon(1180 - 11, 0 + 47 + 10 + 6, 10, 15, battery_icon_bin, themeCurrent.textColor);
drawIcon(1180 - 11, 0 + 47 + 10 + 6, 10, 15, assetsGetDataBuffer(AssetId_battery_icon), themeCurrent.textColor);
if (isCharging)
drawIcon(tmpX - 32, 0 + 47 + 10 + 6, 9, 15, charging_icon_bin, themeCurrent.textColor);
drawIcon(tmpX - 32, 0 + 47 + 10 + 6, 9, 15, assetsGetDataBuffer(AssetId_charging_icon), themeCurrent.textColor);
}
}

View File

@ -1,10 +1,4 @@
#include "theme.h"
/*#include "button_a_light_bin.h"
#include "button_a_dark_bin.h"
#include "button_b_light_bin.h"
#include "button_b_dark_bin.h"*/
#include "hbmenu_logo_light_bin.h"
#include "hbmenu_logo_dark_bin.h"
theme_t themeCurrent;
ThemePreset themeGlobalPreset;
@ -35,7 +29,7 @@ void themeStartup(ThemePreset preset) {
.buttonYText = "\uE0E3",
.buttonPText = "\uE0EF",
.buttonMText = "\uE0F0",
.hbmenuLogoImage = hbmenu_logo_light_bin
.hbmenuLogoImage = assetsGetDataBuffer(AssetId_hbmenu_logo_light)
};
theme_t themeDark = (theme_t) {
@ -55,7 +49,7 @@ void themeStartup(ThemePreset preset) {
.buttonYText = "\uE0A3",
.buttonPText = "\uE0B3",
.buttonMText = "\uE0B4",
.hbmenuLogoImage = hbmenu_logo_dark_bin
.hbmenuLogoImage = assetsGetDataBuffer(AssetId_hbmenu_logo_dark)
};
char themePath[PATH_MAX] = {0};

View File

@ -57,12 +57,17 @@ int main(int argc, char **argv)
}
}
if (R_SUCCEEDED(rc)) {
menuStartupPath();
if (R_SUCCEEDED(rc)) menuStartupPath();
themeStartup((ThemePreset)theme);
if (R_SUCCEEDED(rc)) {
rc = assetsInit();
if (R_FAILED(rc)) {
snprintf(errormsg, sizeof(errormsg)-1, "Error: assetsInit() failed: 0x%x.", rc);
}
}
if (R_SUCCEEDED(rc)) themeStartup((ThemePreset)theme);
if (R_SUCCEEDED(rc)) powerInit();
if (R_SUCCEEDED(rc)) {
@ -178,6 +183,7 @@ int main(int argc, char **argv)
workerExit();
netloaderExit();
powerExit();
assetsExit();
plExit();
setsysExit();

View File

@ -5,8 +5,7 @@
#include "../common/common.h"
#ifdef ENABLE_AUDIO
#include "audio_intro_bin.h"//When using this, you must manually add these under data/ since they're not included in .git.
#include "audio_loop_bin.h"
#error "Audio is not supported currently."
#define SAMPLERATE 48000
#define BYTESPERSAMPLE 2

View File

@ -16,6 +16,7 @@ int main()
window.setFramerateLimit(60);
menuStartupPath();
assetsInit();
themeStartup(THEME_PRESET_LIGHT);
textInit();
fontInitialize();
@ -58,6 +59,7 @@ int main()
workerExit();
netloaderExit();
fontExit();
assetsExit();
return 0;
}