Added graphics/simplegfx_moviemaker.

This commit is contained in:
yellows8 2019-08-28 18:46:58 -04:00
parent 927256ce99
commit d1d712702a
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43
2 changed files with 402 additions and 0 deletions

View File

@ -0,0 +1,222 @@
#---------------------------------------------------------------------------------
.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
# 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.
# 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
#
# CONFIG_JSON is the filename of the NPDM config file (.json), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.json
# - config.json
# If a JSON file is provided or autodetected, an ExeFS PFS0 (.nsp) is built instead
# of a homebrew executable (.nro). This is intended to be used for sysmodules.
# NACP building is skipped as well.
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES := include
#ROMFS := romfs
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)
CFLAGS += $(INCLUDE) -D__SWITCH__
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
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_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
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)
ifeq ($(strip $(CONFIG_JSON)),)
jsons := $(wildcard *.json)
ifneq (,$(findstring $(TARGET).json,$(jsons)))
export APP_JSON := $(TOPDIR)/$(TARGET).json
else
ifneq (,$(findstring config.json,$(jsons)))
export APP_JSON := $(TOPDIR)/config.json
endif
endif
else
export APP_JSON := $(TOPDIR)/$(CONFIG_JSON)
endif
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
ifneq ($(ROMFS),)
export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS)
endif
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
all: $(BUILD)
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
ifeq ($(strip $(APP_JSON)),)
@rm -fr $(BUILD) $(TARGET).nro $(TARGET).nacp $(TARGET).elf
else
@rm -fr $(BUILD) $(TARGET).nsp $(TARGET).nso $(TARGET).npdm $(TARGET).elf
endif
#---------------------------------------------------------------------------------
else
.PHONY: all
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
ifeq ($(strip $(APP_JSON)),)
all : $(OUTPUT).nro
ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
else
$(OUTPUT).nro : $(OUTPUT).elf
endif
else
all : $(OUTPUT).nsp
$(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm
$(OUTPUT).nso : $(OUTPUT).elf
endif
$(OUTPUT).elf : $(OFILES)
$(OFILES_SRC) : $(HFILES_BIN)
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o %_bin.h : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

View File

@ -0,0 +1,180 @@
// Include the most common headers from the C standard library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// Include the main libnx system header, for Switch development
#include <switch.h>
#ifdef DISPLAY_IMAGE
#include "image_bin.h"//Your own raw RGB888 1280x720 image at "data/image.bin" is required.
#endif
// See also libnx display/framebuffer.h.
// This example shows how to use grc MovieMaker, see also grc.h (and applet.h for the requirements for using this).
// Define the desired framebuffer resolution (here we set it to 720p).
#define FB_WIDTH 1280
#define FB_HEIGHT 720
static int s_nxlinkSock = -1;
static void initNxLink()
{
if (R_FAILED(socketInitializeDefault()))
return;
s_nxlinkSock = nxlinkStdio();
if (s_nxlinkSock < 0)
socketExit();
}
static void deinitNxLink()
{
if (s_nxlinkSock >= 0)
{
close(s_nxlinkSock);
socketExit();
s_nxlinkSock = -1;
}
}
void userAppInit()
{
initNxLink();
}
void userAppExit()
{
deinitNxLink();
}
// Main program entrypoint
int main(int argc, char* argv[])
{
Result rc=0;
GrcMovieMaker maker={0};
GrcOffscreenRecordingParameter makerparam={0};
u8 *audiobuf = NULL;
size_t audiobuf_size = (48000/60)*2; // PCM16 samples for a single frame.
// Initialize and start video recording, the app will immediately exit if this fails.
rc = grcCreateMovieMaker(&maker, GRC_MOVIEMAKER_WORKMEMORY_SIZE_DEFAULT);
printf("grcCreateMovieMaker(): 0x%x\n", rc);
if (R_SUCCEEDED(rc)) {
grcCreateOffscreenRecordingParameter(&makerparam);
rc = grcMovieMakerStart(&maker, &makerparam);
printf("grcMovieMakerStart(): 0x%x\n", rc);
}
if (R_SUCCEEDED(rc)) {
audiobuf = (u8*)malloc(audiobuf_size);
if (audiobuf)
memset(audiobuf, 0, audiobuf_size);
else {
printf("Failed to allocate audiobuf.\n");
rc = 1; // Trigger the below R_FAILED block.
}
}
if (R_FAILED(rc)) {
grcMovieMakerClose(&maker);
return EXIT_FAILURE;
}
// Retrieve the default window + MovieMaker window.
NWindow* win = nwindowGetDefault();
NWindow* win_movie = grcMovieMakerGetNWindow(&maker);
// Create a linear double-buffered framebuffer.
Framebuffer fb;
framebufferCreate(&fb, win, FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2);
framebufferMakeLinear(&fb);
// Create a linear double-buffered framebuffer, for MovieMaker.
Framebuffer fb_movie;
framebufferCreate(&fb_movie, win_movie, FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2);
framebufferMakeLinear(&fb_movie);
#ifdef DISPLAY_IMAGE
u8* imageptr = (u8*)image_bin;
#endif
u32 cnt = 0;
// Main loop
while (appletMainLoop())
{
// Scan all the inputs. This should be done once for each frame
hidScanInput();
// hidKeysDown returns information about which buttons have been
// just pressed in this frame compared to the previous one
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS)
break; // break in order to return to hbmenu
// Retrieve the framebuffer.
u32 stride;
u32* framebuf = (u32*) framebufferBegin(&fb, &stride);
if (cnt != 60)
cnt ++;
else
cnt = 0;
// Each pixel is 4-bytes due to RGBA8888.
for (u32 y = 0; y < FB_HEIGHT; y ++)
{
for (u32 x = 0; x < FB_WIDTH; x ++)
{
u32 pos = y * stride / sizeof(u32) + x;
#ifdef DISPLAY_IMAGE
framebuf[pos] = RGBA8_MAXALPHA(imageptr[pos*3+0]+(cnt*4), imageptr[pos*3+1], imageptr[pos*3+2]);
#else
framebuf[pos] = 0x01010101 * cnt * 4;//Set framebuf to different shades of grey.
#endif
}
}
// Retrieve the MovieMaker framebuffer.
u32* framebuf_movie = (u32*) framebufferBegin(&fb_movie, NULL); // Not using stride since we're just doing memcpy from the above image.
// Copy the above rendered image to the MovieMaker fb.
memcpy(framebuf_movie, framebuf, FB_HEIGHT * stride);
// We're done rendering with MovieMaker, so we end the frame here.
framebufferEnd(&fb_movie);
// We're done rendering, so we end the frame here.
framebufferEnd(&fb);
// If you want audio you should fill audiobuf with actual data, but here we'll leave it at 0.
// If you don't use grcMovieMakerEncodeAudioSample, the recorded video will be missing audio.
rc = grcMovieMakerEncodeAudioSample(&maker, audiobuf, audiobuf_size);
if (R_FAILED(rc)) printf("grcMovieMakerEncodeAudioSample(): 0x%x\n", rc);
}
framebufferClose(&fb);
framebufferClose(&fb_movie);
// Finish video recording.
// You can set your own UserData/thumbnail if you want, but in this example we won't do that.
// If you want the output caps-entry you can do so, however see grc.h regarding that.
rc = grcMovieMakerFinish(&maker, 1280, 720, NULL, 0, NULL, 0, NULL);
printf("grcMovieMakerFinish(): 0x%x\n", rc);
rc = grcMovieMakerGetError(&maker);
printf("grcMovieMakerGetError(): 0x%x\n", rc);
grcMovieMakerClose(&maker);
free(audiobuf);
return 0;
}