From e491c5af50dbcbcf1e06fcc190b91e3d47dad1da Mon Sep 17 00:00:00 2001 From: yellows8 Date: Wed, 27 Dec 2017 22:09:59 -0500 Subject: [PATCH] Added proper gfx examples. --- graphics/printing/hello-world/Makefile | 143 ++++++++++++++++++++ graphics/printing/hello-world/source/main.c | 36 +++++ graphics/simplegfx/source/main.c | 60 +++++++- 3 files changed, 234 insertions(+), 5 deletions(-) create mode 100644 graphics/printing/hello-world/Makefile create mode 100644 graphics/printing/hello-world/source/main.c diff --git a/graphics/printing/hello-world/Makefile b/graphics/printing/hello-world/Makefile new file mode 100644 index 0000000..07b60f4 --- /dev/null +++ b/graphics/printing/hello-world/Makefile @@ -0,0 +1,143 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITA64)),) +$(error "Please set DEVKITA64 in your environment. export DEVKITA64=DEVKITA64") +endif + +TOPDIR ?= $(CURDIR) +include $(DEVKITA64)/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 input directory containing data copied into exefs, normally should only contain "main.npdm". +#--------------------------------------------------------------------------------- +TARGET := $(notdir $(CURDIR)) +BUILD := build +SOURCES := source +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=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) + +.PHONY: $(BUILD) clean all + +#--------------------------------------------------------------------------------- +all: $(BUILD) + +$(BUILD): + @[ -d $@ ] || mkdir -p $@ + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).elf + + +#--------------------------------------------------------------------------------- +else +.PHONY: all + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +all : $(OUTPUT).pfs0 $(OUTPUT).nro + +$(OUTPUT).pfs0 : $(OUTPUT).nso + +$(OUTPUT).nso : $(OUTPUT).elf + +$(OUTPUT).nro : $(OUTPUT).elf + +$(OUTPUT).elf : $(OFILES) + +#--------------------------------------------------------------------------------- +# you need a rule like this for each extension you use as binary data +#--------------------------------------------------------------------------------- +%.bin.o : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/graphics/printing/hello-world/source/main.c b/graphics/printing/hello-world/source/main.c new file mode 100644 index 0000000..a0c8a7d --- /dev/null +++ b/graphics/printing/hello-world/source/main.c @@ -0,0 +1,36 @@ +#include +#include + +#include + +int main(int argc, char **argv) +{ + gfxInitDefault(); + + //Initialize console. Using NULL as the second argument tells the console library to use the internal console structure as current one. + consoleInit(NULL); + + //Move the cursor to row 16 and column 20 and then prints "Hello World!" + //To move the cursor you have to print "\x1b[r;cH", where r and c are respectively + //the row and column where you want your cursor to move + printf("\x1b[16;20HHello World!"); + + //"Safe default, single Joy-Con have buttons/sticks rotated for orientation" + hidSetControllerLayout(CONTROLLER_PLAYER_1, LAYOUT_DEFAULT); + + while(appletMainLoop()) + { + //Scan all the inputs. This should be done once for each frame + hidScanInput(); + + if ((hidKeysDown(CONTROLLER_PLAYER_1) | hidKeysDown(CONTROLLER_HANDHELD)) & KEY_PLUS) break; + + gfxFlushBuffers(); + gfxSwapBuffers(); + gfxWaitForVsync(); + } + + gfxExit(); + return 0; +} + diff --git a/graphics/simplegfx/source/main.c b/graphics/simplegfx/source/main.c index c54731b..f7e69b0 100644 --- a/graphics/simplegfx/source/main.c +++ b/graphics/simplegfx/source/main.c @@ -1,21 +1,71 @@ #include #include - #include +#include "image_bin.h"//Your own raw RGB888 1280x720 image at "data/image.bin" is required. + +//See also libnx gfx.h + gfx.c source for gfxWaitForVsync(). + int main(int argc, char **argv) { - u32 cnt=60*5; + u8 *framebuf; + u32 *framebuf32; + u32 width=0, height=0; + u32 image_width = 1280, image_height = 720; + u32 tmp_width, tmp_height; + u32 x, y; + u32 pos; + u32 pos2; + u8 *imageptr = (u8*)image_bin; + u32 cnt=0; + //gfxInitResolutionDefault();//Enable max-1080p support. Remove for 720p-only resolution. gfxInitDefault(); + //gfxConfigureAutoResolutionDefault(true);//Set current resolution automatically depending on current/changed OperationMode. Only use this when using gfxInitResolution*(). + //Note that while the above commented code enables 1080p for docked-mode, this example only draws a 720p image, with the rest of the screen being left at white. - while(cnt)//Unknown how to properly handle exiting this loop. + //"Safe default, single Joy-Con have buttons/sticks rotated for orientation" + hidSetControllerLayout(CONTROLLER_PLAYER_1, LAYOUT_DEFAULT); + + while(appletMainLoop()) { - //Currently unknown how to write to {framebuffer}. + //Scan all the inputs. This should be done once for each frame + hidScanInput(); + if ((hidKeysDown(CONTROLLER_PLAYER_1) | hidKeysDown(CONTROLLER_HANDHELD)) & KEY_PLUS) break; + + framebuf = gfxGetFramebuffer((u32*)&width, (u32*)&height); + framebuf32 = (u32*)framebuf; + + if(cnt==60) + { + cnt=0; + } + else + { + cnt++; + } + + tmp_width = image_width; + tmp_height = image_height; + if(tmp_width > width) tmp_width = width; + if(tmp_height > height) tmp_height = height; + + memset(framebuf, 0xff, gfxGetFramebufferSize()); + for(x=0; x