examples: Add example to compare all dual 3D modes

This commit is contained in:
Antonio Niño Díaz 2023-10-10 01:03:33 +01:00
parent 8445136b6b
commit 5cd0909b5c
6 changed files with 409 additions and 0 deletions

View File

@ -0,0 +1,220 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
include $(DEVKITARM)/ds_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
# INCLUDES is a list of directories containing extra header files
# DATA is a list of directories containing binary files embedded using bin2o
# GRAPHICS is a list of directories containing image files to be converted with grit
# AUDIO is a list of directories containing audio to be converted by maxmod
# ICON is the image used to create the game icon, leave blank to use default rule
# NITRO is a directory that will be accessible via NitroFS
#---------------------------------------------------------------------------------
TARGET := $(shell basename $(CURDIR))
BUILD := build
SOURCES := source
INCLUDES := include
DATA := data
GRAPHICS :=
AUDIO :=
ICON :=
# specify a directory which contains the nitro filesystem
# this is relative to the Makefile
NITRO :=
# These set the information text in the nds file
GAME_TITLE := $(shell basename $(CURDIR))
GAME_SUBTITLE1 := Nitro Engine example
GAME_SUBTITLE2 := github.com/AntonioND/nitro-engine
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -marm -mthumb-interwork -march=armv5te -mtune=arm946e-s
CFLAGS := -g -Wall -O3\
$(ARCH) $(INCLUDE) -DARM9
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project (order is important)
#---------------------------------------------------------------------------------
LIBS := -lNE -lfat -lnds9
# automatigically add libraries for NitroFS
ifneq ($(strip $(NITRO)),)
LIBS := -lfilesystem -lfat $(LIBS)
endif
# automagically add maxmod library
ifneq ($(strip $(AUDIO)),)
LIBS := -lmm9 $(LIBS)
endif
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(LIBNDS) $(PORTLIBS) $(DEVKITPRO)/nitro-engine
#---------------------------------------------------------------------------------
# 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 VPATH := $(CURDIR)/$(subst /,,$(dir $(ICON)))\
$(foreach dir,$(SOURCES),$(CURDIR)/$(dir))\
$(foreach dir,$(DATA),$(CURDIR)/$(dir))\
$(foreach dir,$(GRAPHICS),$(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)))
PNGFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
# prepare NitroFS directory
ifneq ($(strip $(NITRO)),)
export NITRO_FILES := $(CURDIR)/$(NITRO)
endif
# get audio list for maxmod
ifneq ($(strip $(AUDIO)),)
export MODFILES := $(foreach dir,$(notdir $(wildcard $(AUDIO)/*.*)),$(CURDIR)/$(AUDIO)/$(dir))
# place the soundbank file in NitroFS if using it
ifneq ($(strip $(NITRO)),)
export SOUNDBANK := $(NITRO_FILES)/soundbank.bin
# otherwise, needs to be loaded from memory
else
export SOUNDBANK := soundbank.bin
BINFILES += $(SOUNDBANK)
endif
endif
#---------------------------------------------------------------------------------
# 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_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(PNGFILES:.png=.o) $(OFILES_BIN) $(OFILES_SOURCES)
export HFILES := $(PNGFILES:.png=.h) $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE := $(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir))\
$(foreach dir,$(LIBDIRS),-I$(dir)/include)\
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.bmp)
ifneq (,$(findstring $(TARGET).bmp,$(icons)))
export GAME_ICON := $(CURDIR)/$(TARGET).bmp
else
ifneq (,$(findstring icon.bmp,$(icons)))
export GAME_ICON := $(CURDIR)/icon.bmp
endif
endif
else
ifeq ($(suffix $(ICON)), .grf)
export GAME_ICON := $(CURDIR)/$(ICON)
else
export GAME_ICON := $(CURDIR)/$(BUILD)/$(notdir $(basename $(ICON))).grf
endif
endif
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
$(BUILD):
@mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(SOUNDBANK)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).nds: $(OUTPUT).elf $(GAME_ICON)
$(OUTPUT).elf: $(OFILES)
# source files depend on generated headers
$(OFILES_SOURCES) : $(HFILES)
# need to build soundbank first
$(OFILES): $(SOUNDBANK)
#---------------------------------------------------------------------------------
# rule to build solution from music files
#---------------------------------------------------------------------------------
$(SOUNDBANK) : $(MODFILES)
#---------------------------------------------------------------------------------
mmutil $^ -d -o$@ -hsoundbank.h
#---------------------------------------------------------------------------------
%.bin.o %_bin.h : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
#---------------------------------------------------------------------------------
# This rule creates assembly source files using grit
# grit takes an image file and a .grit describing how the file is to be processed
# add additional rules like this for each image extension
# you use in the graphics folders
#---------------------------------------------------------------------------------
%.s %.h: %.png %.grit
#---------------------------------------------------------------------------------
grit $< -fts -o$*
#---------------------------------------------------------------------------------
# Convert non-GRF game icon to GRF if needed
#---------------------------------------------------------------------------------
$(GAME_ICON): $(notdir $(ICON))
#---------------------------------------------------------------------------------
@echo convert $(notdir $<)
@grit $< -g -gt -gB4 -gT FF00FF -m! -p -pe 16 -fh! -ftr
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

View File

@ -0,0 +1,3 @@
BINDIRS := data
include ../../Makefile.example.blocksds

View File

@ -0,0 +1,19 @@
#!/bin/sh
NITRO_ENGINE=$DEVKITPRO/nitro-engine
ASSETS=$NITRO_ENGINE/examples/assets
TOOLS=$NITRO_ENGINE/tools
OBJ2DL=$TOOLS/obj2dl/obj2dl.py
mkdir -p data
python3 $OBJ2DL \
--input $ASSETS/teapot.obj \
--output data/teapot.bin \
--texture 32 32 \
--scale 0.1
python3 $OBJ2DL \
--input $ASSETS/sphere.obj \
--output data/sphere.bin \
--texture 32 32

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,167 @@
// SPDX-License-Identifier: CC0-1.0
//
// SPDX-FileContributor: Antonio Niño Díaz, 2008-2011, 2019, 2022-2023
//
// This file is part of Nitro Engine
// The special screen effects of this is demo don't work on DesMuME. It works on
// melonDS and hardware.
//
// Dual 3D:
// - When the CPU hangs, the last drawn screen is shown in both screens.
// - The debug console works.
// - Special effects work normally.
//
// Dual 3D FB:
// - When the CPU hangs, the output is stable.
// - The debug console doesn't work.
// - Special effects don't work properly.
//
// Dual 3D DMA:
// - When the CPU hangs, the output is stable.
// - The debug console works.
// - Special effects work normally.
// - The CPU performance is lower. It requires more DMA and CPU copies.
#include <NEMain.h>
#include "teapot_bin.h"
#include "sphere_bin.h"
NE_Camera *Camera;
NE_Model *Teapot, *Sphere;
void Draw3DScene(void)
{
NE_ClearColorSet(NE_Red, 31, 63);
NE_CameraUse(Camera);
NE_ModelDraw(Teapot);
}
void Draw3DScene2(void)
{
NE_ClearColorSet(NE_Green, 31, 63);
NE_CameraUse(Camera);
NE_ModelDraw(Sphere);
}
void init_all(void)
{
// Allocate objects...
Teapot = NE_ModelCreate(NE_Static);
Sphere = NE_ModelCreate(NE_Static);
Camera = NE_CameraCreate();
// Setup camera
NE_CameraSet(Camera,
0, 0, -2,
0, 0, 0,
0, 1, 0);
// Load models
NE_ModelLoadStaticMesh(Teapot, (u32 *)teapot_bin);
NE_ModelLoadStaticMesh(Sphere, (u32 *)sphere_bin);
// Set light color and direction
NE_LightSet(0, NE_White, -0.5, -0.5, -0.5);
// Enable shading
NE_ShadingEnable(true);
}
int main(void)
{
// This is needed for special screen effects
irqEnable(IRQ_HBLANK);
irqSet(IRQ_VBLANK, NE_VBLFunc);
irqSet(IRQ_HBLANK, NE_HBLFunc);
// Init dual 3D mode and console
NE_InitDual3D();
NE_InitConsole();
init_all();
while (1)
{
NE_WaitForVBL(0);
// Draw 3D scenes
NE_ProcessDual(Draw3DScene, Draw3DScene2);
// Refresh keys
scanKeys();
uint32 keys = keysHeld();
uint32 kdown = keysDown();
printf("\x1b[0;0H"
"START: Lock CPU until released\n"
"A: Sine effect.\n"
"B: Deactivate effect.\n"
"SELECT: Dual 3D DMA mode\n"
"X: Dual 3D FB mode (no console)\n"
"Y: Dual 3D mode\n"
"Pad: Rotate.\n");
// Lock CPU in an infinite loop to simulate a drop in framerate
while (keys & KEY_START)
{
scanKeys();
keys = keysHeld();
}
// Rotate model
if (keys & KEY_UP)
{
NE_ModelRotate(Sphere, 0, 0, 2);
NE_ModelRotate(Teapot, 0, 0, 2);
}
if (keys & KEY_DOWN)
{
NE_ModelRotate(Sphere, 0, 0, -2);
NE_ModelRotate(Teapot, 0, 0, -2);
}
if (keys & KEY_RIGHT)
{
NE_ModelRotate(Sphere, 0, 2, 0);
NE_ModelRotate(Teapot, 0, 2, 0);
}
if (keys & KEY_LEFT)
{
NE_ModelRotate(Sphere, 0, -2, 0);
NE_ModelRotate(Teapot, 0, -2, 0);
}
// Deactivate effect
if (kdown & KEY_B)
NE_SpecialEffectSet(0);
// Activate effect
if (kdown & KEY_A)
NE_SpecialEffectSet(NE_SINE);
if (kdown & KEY_Y)
{
NE_SpecialEffectSet(0);
NE_InitDual3D();
NE_InitConsole();
init_all();
}
if (kdown & KEY_X)
{
NE_SpecialEffectSet(0);
NE_InitDual3D_FB();
init_all();
}
if (kdown & KEY_SELECT)
{
NE_SpecialEffectSet(0);
NE_InitDual3D_DMA();
NE_InitConsole();
init_all();
}
}
return 0;
}