Skip to content

Commit

Permalink
Load assets from a .zip in romfs (romfs/ dir for pc-build) using mini…
Browse files Browse the repository at this point in the history
…zip, 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.
  • Loading branch information
yellows8 committed Oct 29, 2018
1 parent bc6d98a commit bb53a8a
Show file tree
Hide file tree
Showing 34 changed files with 230 additions and 125 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions Makefile.nx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -95,7 +97,7 @@ else
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
Expand Down Expand Up @@ -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

#---------------------------------------------------------------------------------
Expand Down
91 changes: 3 additions & 88 deletions Makefile.pc
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
162 changes: 162 additions & 0 deletions common/assets.c
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions common/assets.h
Original file line number Diff line number Diff line change
@@ -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);

1 change: 1 addition & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Loading

0 comments on commit bb53a8a

Please sign in to comment.