Homebrew Help

 

 Intro

I figured since I did some homebrew DS game making and found it pretty difficult to find some of the answers I would share. So this is where I wrote down all the things I had to figure out myself. I don’t claim to know much at all and this project was my first time working with the DS and C so mileage may vary but I’ll answer the questions I had going in, so hopefully you won’t be as lost as me if you do the same. This guide is not an alternative to the other tutorials and should be taken as what it is; (limited) supplementary material. I will link to the tutorials I used/found because they explain a lot of this stuff better than I ever could.

So let’s start off with some useful links I’ve collected

devkitPro
gl2D
Texture Packer (for use with gl2D)
Emulators desmume and NO$GBA are both good
Patater
Dev-scene
drunkencoders
Grit readme

MakeFile

This thing was really mysterious when I started I could get them too work with the example files that came with libnds but changing anything messed me up. here’s what the make file I use now looks like.

#———————————————————————————
.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 data
# GRAPHICS is a list of directories containing files to be processed by grit
#
# All directories are specified relative to the project directory where
# the makefile is found
#
#———————————————————————————
TARGET        :=    $(notdir $(CURDIR))
BUILD        :=    build
SOURCES        :=    source
DATA        :=    data
INCLUDES    :=    include
GRAPHICS    :=    gfx

#———————————————————————————
# options for code generation
#———————————————————————————
ARCH        :=    -mthumb -mthumb-interwork

CFLAGS    :=    -g -Wall -O2\
-march=armv5te -mtune=arm946e-s -fomit-frame-pointer\
-ffast-math \
$(ARCH)

CFLAGS    +=    $(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
#———————————————————————————
LIBS    := -lgl2d -lnds9

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

#———————————————————————————
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#———————————————————————————

ifneq ($(BUILDDIR), $(CURDIR))
#———————————————————————————

export OUTPUT    :=    $(CURDIR)/$(TARGET)

export VPATH    :=    $(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)))
BINFILES    :=    $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
BMPFILES    :=    $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.bmp)))
PNGFILES    :=    $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))

#———————————————————————————
# 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)) \
$(BMPFILES:.bmp=.o) \
$(PNGFILES:.png=.o) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)

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)

.PHONY: $(BUILD) clean

#———————————————————————————
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@make BUILDDIR=`cd $(BUILD) && pwd` –no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#———————————————————————————
clean:
@echo clean …
@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(TARGET).ds.gba

#———————————————————————————
else

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

#———————————————————————————
# main targets
#———————————————————————————
$(OUTPUT).nds    :     $(OUTPUT).elf
$(OUTPUT).elf    :    $(OFILES)

#———————————————————————————
# The bin2o rule should be copied and modified
# for each extension used in the data directories
#———————————————————————————

#———————————————————————————
# This rule links in binary data with the .bin extension
#———————————————————————————
%.bin.o    :    %.bin
#———————————————————————————
@echo $(notdir $<)
@$(bin2o)

#———————————————————————————
# This rule links in binary data with the .raw extension
#———————————————————————————
%.raw.o    :    %.raw
#———————————————————————————
@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    : %.bmp %.grit
#———————————————————————————
grit $< -fts -o$*

#———————————————————————————
%.s %.h    : %.png %.grit
#———————————————————————————
grit $< -fts -o$*

-include $(DEPENDS)

 

 

>#—————————————————————————————
endif
#—————————————————————————————

This kind of thing can look pretty intimidating. Most example makefiles are built from the same place with some tweaks added here and there here’s the most common places to see changes this is pretty key in understanding what should be changed or not. Most are also well commented so anything weird should be made pretty obvious.

So let’s jump into this.

TARGET        :=    $(notdir $(CURDIR))
BUILD        :=    build
SOURCES        :=    source
DATA        :=    data
INCLUDES    :=    include
GRAPHICS    :=    gfx

Pretty self explanitory. As you can see those variables are the places the makefile will be looking for your stuff. So make sure you name the folder you put your source code in the same as what your makefile says where your source code is and etc for all the parts. Because it would really suck to put your sprites in a folder called “sprites” only to find out your make file was looking for a folder called “gfx” four hours later. (totally not speaking from personal experience at all) The way it is set up is all the folders being relative to the make file ie in the same folder.

LIBS    := -lgl2d -lnds9

If you’re going to be using gl2D or any other library don’t forget to add it to your libraries.

A quick note on graphics make sure your make file is set up to handle the right file types I like to use bmp files for my DS art so I have these lines here

BMPFILES    :=    $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.bmp)))
PNGFILES    :=    $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))

and and here

$(BMPFILES:.bmp=.o) \
$(PNGFILES:.png=.o) \

and here

#———————————————————————————
%.s %.h    : %.bmp %.grit
#———————————————————————————
grit $< -fts -o$*

#———————————————————————————
%.s %.h    : %.png %.grit
#———————————————————————————
grit $< -fts -o$*

 

You’ll notice I have stuff written for pngs as well as bmps here that’s simply because if I happen to not want/or remember to convert a file from bmp to png (or vice versa) I won’t ever have to think about it because the make file will just take them both for me no questions asked and I like not having to think about it.

Graphics

This part is for use with the regular 2D OAM if you are using gl2D read the documentation that comes with it it’s really well put together and explains how to use the texture packer as well. Don’t forget that it only works on one screen at a time at this moment

As you either know or will eventually find out the DS doesn’t use your usually image files as is and need to be converted that’s where Grit comes in. now Grit is pretty simple creates a header file for you to use in your code quite nicely the only reason I’m going to mention it here is that not many tutorials mention what grit can do outside of the context of the example. I have the Grit readme linked up top it’s a recommended read.

This part is more of a tips/things to remember section so it will be pretty quick.

1.Include the header file.

#include “youspritesname.h”; is all you need for that one

2.set up a VRAM make sure it’s on the right screen when you do it too

vramSetBankD(VRAM_D_SUB_SPRITE);
or
vramSetBankD(VRAM_D_MAIN_SPRITE);

3. initialize the OAM and Allocate some space for the sprite. on the correct screen remember

oamInit(&oamSub, SpriteMapping_1D_128, false);
u16* gfx = oamAllocateGfx(&oamSub, SpriteSize_64x64,SpriteColorFormat_256Color);

4.set up the sprite and it’s pallet. those pal and tiles things are defined by the .h file for you

dmaCopy(youspritesnameTiles,gfx, youspritesnameTilesLen);
dmaCopy(youspritesnamePal, SPRITE_PALETTE_SUB, youspritesnamePalLen);

5.this is the important one put this in your while loop. x and y have to be set by you but it’s how you move your graphics around on screen. remember to set this to the correct screen.

oamSet(&oamSub,0,youspritesnamey,youspritesnamex,0,0,SpriteSize_64x64,SpriteColorFormat_256Color,gfx,0,false,false,false,false,false);

6. Update the OAM. for the correct screen of course

oamUpdate(&oamSub);