Skip to content

Commit

Permalink
Initial public commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
yellows8 committed Feb 12, 2018
0 parents commit b6816ab
Show file tree
Hide file tree
Showing 36 changed files with 3,518 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*~
*.exe
*.o
test
tahoma12.c
tahoma24.c
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all:
make -f Makefile.nx
make -f Makefile.pc

clean:
make -f Makefile.pc clean
make -f Makefile.nx clean
189 changes: 189 additions & 0 deletions Makefile.nx
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

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

TOPDIR ?= $(CURDIR)
include $(DEVKITPRO)/libnx/switch_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
# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm".
#
# NO_ICON: if set to anything, do not use icon.
# NO_NACP: if set to anything, no .nacp file is generated.
# APP_TITLE is the name of the app stored in the .nacp file (Optional)
# APP_AUTHOR is the author of the app stored in the .nacp file (Optional)
# APP_VERSION is the version of the app stored in the .nacp file (Optional)
# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional)
# ICON is the filename of the icon (.jpg), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.jpg
# - icon.jpg
# - <libnx folder>/default_icon.jpg
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := common/ nx_main/ nx_main/loaders/
DATA := data
INCLUDES := include
EXEFS_SRC := exefs_src

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a -mtp=soft -fPIE

CFLAGS := -g -Wall -O2 \
-ffast-math \
$(ARCH) $(DEFINES)

CFLAGS += $(INCLUDE) -DSWITCH

CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11

ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

LIBS := -lnx

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


#---------------------------------------------------------------------------------
# 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)) \
$(foreach dir,$(DATA),$(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)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

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

export OFILES := $(addsuffix .o,$(BINFILES)) \
$(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)

export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC)

ifeq ($(strip $(ICON)),)
icons := $(wildcard *.jpg)
ifneq (,$(findstring $(TARGET).jpg,$(icons)))
export APP_ICON := $(TOPDIR)/$(TARGET).jpg
else
ifneq (,$(findstring icon.jpg,$(icons)))
export APP_ICON := $(TOPDIR)/icon.jpg
endif
endif
else
export APP_ICON := $(TOPDIR)/$(ICON)
endif

ifeq ($(strip $(NO_ICON)),)
export NROFLAGS += --icon=$(APP_ICON)
endif

ifeq ($(strip $(NO_NACP)),)
export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp
endif

ifneq ($(APP_TITLEID),)
export NACPFLAGS += --titleid=$(APP_TITLEID)
endif

.PHONY: $(BUILD) clean all

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

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

#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf


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

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

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
all : $(OUTPUT).pfs0 $(OUTPUT).nro

$(OUTPUT).pfs0 : $(OUTPUT).nso

$(OUTPUT).nso : $(OUTPUT).elf

ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
else
$(OUTPUT).nro : $(OUTPUT).elf
endif

$(OUTPUT).elf : $(OFILES)

#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)

%.nxfnt.o : %.nxfnt
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)

-include $(DEPENDS)

#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
38 changes: 38 additions & 0 deletions Makefile.pc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# canned command sequence for binary data
#---------------------------------------------------------------------------------
define bin2o
bin2s $< | $(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 \
common/menu.c common/font.c common/language.c common/launch.c \
common/menu-entry.c common/menu-list.c common/text.c \
common/nanojpeg.c common/ui.c \
build_pc/tahoma24.o build_pc/tahoma12.o build_pc/switchicon_questionmark.bin.o build_pc/folder_icon.bin.o
gcc -Wall -O2 -g0 $^ -lsfml-graphics -lsfml-window -lsfml-system -lstdc++ -I. -Ibuild_pc -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/switchicon_questionmark.bin.o : data/switchicon_questionmark.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)

build_pc/folder_icon.bin.o : data/folder_icon.bin
mkdir -p $(dir $@)
@echo $(notdir $<)
@$(bin2o)

clean:
rm -rf build_pc/ test
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#### Building
Build with Makefile.nx directly or just run ```make```.

#### Credits

* This uses code based on 3DS [new-hbmenu](https://github.com/fincs/new-hbmenu).
* The "switchicon-questionmark" icon is based on the icon by [Sweet Farm from the Noun Project](https://thenounproject.com/term/nintendo-switch/694750/).
* "folder_icon": [Original](https://www.iconfinder.com/icons/97888/docs_folder_google_icon) icon by Dakirby309, modified by @fincs.
* [nanojpeg](svn.emphy.de/nanojpeg/trunk/nanojpeg/nanojpeg.c) is used for handling JPEG icons.
106 changes: 106 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#pragma once

#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <limits.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <malloc.h>
#ifdef SWITCH
#include <switch.h>
#endif

#include <stdint.h>
typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;

#define M_TAU (2*M_PI)

typedef union {
uint32_t abgr;
struct {
uint8_t r,g,b,a;
};
} color_t;

#include "font.h"
#include "nacp.h"
#include "menu.h"
#include "text.h"
#include "ui.h"
#include "launch.h"
#include "nro.h"
#include "nanojpeg.h"

void menuStartup();
void menuLoop();

static inline uint8_t BlendColor(uint32_t src, uint32_t dst, uint8_t alpha)
{
uint8_t one_minus_alpha = (uint8_t)255 - alpha;
return (dst*alpha + src*one_minus_alpha)/(uint8_t)255;
}

static inline color_t MakeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
color_t clr;
clr.r = r;
clr.g = g;
clr.b = b;
clr.a = a;
return clr;
}

#ifdef SWITCH
extern uint8_t* g_framebuf;
static inline void DrawPixel(uint32_t x, uint32_t y, color_t clr)
{
if (x >= 1280 || y >= 720)
return;
u32 off = 4*gfxGetFramebufferDisplayOffset(x, y);
g_framebuf[off] = BlendColor(g_framebuf[off], clr.r, clr.a); off++;
g_framebuf[off] = BlendColor(g_framebuf[off], clr.g, clr.a); off++;
g_framebuf[off] = BlendColor(g_framebuf[off], clr.b, clr.a); off++;
g_framebuf[off] = 0xff;
}
static inline void DrawPixelRaw(uint32_t x, uint32_t y, color_t clr)
{
if (x >= 1280 || y >= 720)
return;
u32 off = 4*gfxGetFramebufferDisplayOffset(x, y);
g_framebuf[off] = clr.r; off++;
g_framebuf[off] = clr.g; off++;
g_framebuf[off] = clr.b; off++;
g_framebuf[off] = 0xff;
}
#else
extern color_t pixels[720][1280];
static inline void DrawPixel(uint32_t x, uint32_t y, color_t clr)
{
if (x >= 1280 || y >= 720)
return;
pixels[y][x].r = BlendColor(pixels[y][x].r, clr.r, clr.a);
pixels[y][x].g = BlendColor(pixels[y][x].g, clr.g, clr.a);
pixels[y][x].b = BlendColor(pixels[y][x].b, clr.b, clr.a);
pixels[y][x].a = 0xff;
}
static inline void DrawPixelRaw(uint32_t x, uint32_t y, color_t clr)
{
if (x >= 1280 || y >= 720)
return;
pixels[y][x].r = clr.r;
pixels[y][x].g = clr.g;
pixels[y][x].b = clr.b;
pixels[y][x].a = 0xff;
}
#endif

void DrawPixel(uint32_t x, uint32_t y, color_t clr);
void DrawText(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t clr, const char* text);
void DrawTextTruncate(const ffnt_header_t* font, uint32_t x, uint32_t y, color_t clr, const char* text, uint32_t max_width, const char* end_text);
Loading

0 comments on commit b6816ab

Please sign in to comment.