mirror of
https://github.com/switchbrew/switch-examples.git
synced 2025-06-21 05:12:40 +02:00
Added proper gfx examples.
This commit is contained in:
parent
aecd2e5f7b
commit
e491c5af50
143
graphics/printing/hello-world/Makefile
Normal file
143
graphics/printing/hello-world/Makefile
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
.SUFFIXES:
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ifeq ($(strip $(DEVKITA64)),)
|
||||||
|
$(error "Please set DEVKITA64 in your environment. export DEVKITA64=<path to>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
|
||||||
|
#---------------------------------------------------------------------------------------
|
36
graphics/printing/hello-world/source/main.c
Normal file
36
graphics/printing/hello-world/source/main.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <switch.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
@ -1,21 +1,71 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
|
|
||||||
|
#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)
|
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();
|
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<image_width; x++)
|
||||||
|
{
|
||||||
|
for(y=0; y<image_height; y++)
|
||||||
|
{
|
||||||
|
//Each pixel is 4-bytes due to RGBA8888.
|
||||||
|
pos = gfxGetFramebufferDisplayOffset(x, y);
|
||||||
|
pos2 = (x + (y*image_width)) * 3;
|
||||||
|
framebuf32[pos] = RGBA8_MAXALPHA(imageptr[pos2+0]+(cnt*4), imageptr[pos2+1], imageptr[pos2+2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gfxFlushBuffers();
|
||||||
gfxSwapBuffers();
|
gfxSwapBuffers();
|
||||||
gfxWaitForVsync();
|
gfxWaitForVsync();
|
||||||
cnt--;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gfxExit();
|
gfxExit();
|
||||||
|
Loading…
Reference in New Issue
Block a user