Skip to content

Commit

Permalink
Add examples of library usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rw-r-r-0644 committed Sep 25, 2019
1 parent 6cd0b5a commit 32495f2
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 0 deletions.
23 changes: 23 additions & 0 deletions samples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.0)
set(CMAKE_TOOLCHAIN_FILE "$ENV{DEVKITPRO}/wut/share/wut.toolchain.cmake")

project(romfs-helloworld C)
include("${DEVKITPRO}/wut/share/wut.cmake" REQUIRED)
include("${DEVKITPRO}/portlibs/wiiu/share/romfs-wiiu.cmake" REQUIRED)

# add source files
add_executable(romfs-helloworld
helloworld.c)

# create and add romfs from "romfs" directory
romfs_add(romfs-helloworld "romfs")

# link requireed libraries
target_link_libraries(romfs-helloworld
whb
coreinit
proc_ui
sysapp)

# build an rpx
wut_create_rpx(romfs-helloworld.rpx romfs-helloworld)
59 changes: 59 additions & 0 deletions samples/cmake/helloworld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <coreinit/thread.h>
#include <coreinit/time.h>
#include <whb/proc.h>
#include <whb/log.h>
#include <whb/log_console.h>
#include <romfs-wiiu.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
char line[128];

// initialize procui/console
WHBProcInit();
WHBLogConsoleInit();

// initialize romfs library
int res = romfsInit();
if (res) {
WHBLogPrintf(">> Failed to init romfs: %d", res);
goto end;
}

// open helloworld.txt
FILE *fp = fopen("romfs:/helloworld.txt", "r");
if (fp == NULL) {
WHBLogPrint(">> Failed to open file");
goto end;
}

WHBLogPrint(">> Content of romfs:/helloworld.txt:");

// output content to console
while (fgets(line, sizeof(line), fp) != NULL)
WHBLogPrint(line);

// cleanup
fclose(fp);

end:
WHBLogPrint(">> Done. Press Home to exit");

// draw the contents of console to screen
WHBLogConsoleDraw();

// wait for the user to exit
while(WHBProcIsRunning())
OSSleepTicks(OSMillisecondsToTicks(100));

// deinitialize romfs library
romfsExit();

// deinitialize procui/console
WHBLogConsoleFree();
WHBProcShutdown();

return 0;
}
2 changes: 2 additions & 0 deletions samples/cmake/romfs/helloworld.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello world!
This is an example romfs file
132 changes: 132 additions & 0 deletions samples/make/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#-------------------------------------------------------------------------------
.SUFFIXES:
#-------------------------------------------------------------------------------

ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif

TOPDIR ?= $(CURDIR)

include $(DEVKITPRO)/wut/share/wut_rules

#-------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# ROMFS is a folder to generate app's romfs
#-------------------------------------------------------------------------------
TARGET := romfs-helloworld
BUILD := build
SOURCES := source
INCLUDES := include
ROMFS := romfs

#-------------------------------------------------------------------------------
# options for code generation
#-------------------------------------------------------------------------------
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(MACHDEP)

CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__

CXXFLAGS := $(CFLAGS)

ASFLAGS := -g $(MACHDEP)
LDFLAGS := -g $(MACHDEP) $(RPXSPECS) -Wl,-Map,$(notdir $*.map)

LIBS := -lwut

#-------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level
# containing include and lib
#-------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(WUT_ROOT)


#-------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#-------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#-------------------------------------------------------------------------------

export OUTPUT := $(CURDIR)/$(TARGET)
export TOPDIR := $(CURDIR)

export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)

CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))

#-------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#-------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#-------------------------------------------------------------------------------
export LD := $(CC)
#-------------------------------------------------------------------------------
else
#-------------------------------------------------------------------------------
export LD := $(CXX)
#-------------------------------------------------------------------------------
endif
#-------------------------------------------------------------------------------

export SRCFILES := $(CPPFILES) $(CFILES) $(SFILES)
export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)

export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)

export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)

.PHONY: $(BUILD) clean all

#-------------------------------------------------------------------------------
all: $(BUILD)

$(BUILD): $(SRCFILES)
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#-------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).rpx $(TARGET).elf

#-------------------------------------------------------------------------------
else
.PHONY: all

#-------------------------------------------------------------------------------
# romfs
#-------------------------------------------------------------------------------
include $(PORTLIBS_PATH)/wiiu/share/romfs-wiiu.mk
CFLAGS += $(ROMFS_CFLAGS)
CXXFLAGS += $(ROMFS_CFLAGS)
LIBS += $(ROMFS_LIBS)
OFILES += $(ROMFS_TARGET)
#-------------------------------------------------------------------------------

DEPENDS := $(OFILES:.o=.d)

#-------------------------------------------------------------------------------
# main targets
#-------------------------------------------------------------------------------
all : $(OUTPUT).rpx

$(OUTPUT).rpx : $(OUTPUT).elf

$(OUTPUT).elf : $(OFILES)

-include $(DEPENDS)

#-------------------------------------------------------------------------------
endif
#-------------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions samples/make/romfs/helloworld.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello world!
This is an example romfs file
59 changes: 59 additions & 0 deletions samples/make/source/helloworld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <coreinit/thread.h>
#include <coreinit/time.h>
#include <whb/proc.h>
#include <whb/log.h>
#include <whb/log_console.h>
#include <romfs-wiiu.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
char line[128];

// initialize procui/console
WHBProcInit();
WHBLogConsoleInit();

// initialize romfs library
int res = romfsInit();
if (res) {
WHBLogPrintf(">> Failed to init romfs: %d", res);
goto end;
}

// open helloworld.txt
FILE *fp = fopen("romfs:/helloworld.txt", "r");
if (fp == NULL) {
WHBLogPrint(">> Failed to open file");
goto end;
}

WHBLogPrint(">> Content of romfs:/helloworld.txt:");

// output content to console
while (fgets(line, sizeof(line), fp) != NULL)
WHBLogPrint(line);

// cleanup
fclose(fp);

end:
WHBLogPrint(">> Done. Press Home to exit");

// draw the contents of console to screen
WHBLogConsoleDraw();

// wait for the user to exit
while(WHBProcIsRunning())
OSSleepTicks(OSMillisecondsToTicks(100));

// deinitialize romfs library
romfsExit();

// deinitialize procui/console
WHBLogConsoleFree();
WHBProcShutdown();

return 0;
}

0 comments on commit 32495f2

Please sign in to comment.