NDS/Tutorials Day 5
From Dev-Scene
Hardware sprites free us from the fixed tiles we have looked at so far. These are crucial to any 2D game.
Contents |
[edit] intro
This is a tutorial to teach you the basics of sprites,
source data gfx
[edit] Setting your sprite up for GRIT
place your sprite in the gfx folder for the purpose of this tutorial call it sprite.png (make sure its 8-bit),
in the gfx folder you also need to make a file called sprite.grit, open it up with programers notepad or notepad and fill it with this code:
#8 bit bitmap -gB8 #Tile the image -gt #cyan transparent -gT 00FFFF
I have set this up so cyan is transparent, the transparency is the hex color on the last line, it can be changed as you like.
[edit] Make File
in your project folder create a file named "makefile" open it with programers notepad and paste the following code:
.SUFFIXES: ifeq ($(strip $(DEVKITARM)),) $(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM") endif include $(DEVKITARM)/ds_rules TARGET := $(shell basename $(CURDIR)) BUILD := build SOURCES := source DATA := data INCLUDES := include GRAPHICS := gfx 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) -mno-fpu -Wl,-Map,$(notdir $*.map) LIBS := -lnds9 LIBDIRS := $(LIBNDS) ifneq ($(BUILD),$(notdir $(CURDIR))) export OUTPUT := $(CURDIR)/$(TARGET) export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ $(foreach dir,$(GRAPHICS),$(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)/*.*))) PNGFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png))) ifeq ($(strip $(CPPFILES)),) export LD := $(CC) else export LD := $(CXX) endif export OFILES := $(addsuffix .o,$(BINFILES)) \ $(PNGFILES:.png=.o) \ $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ $(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 --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile clean: @echo clean ... @rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(TARGET).arm9 $(TARGET).ds.gba else DEPENDS := $(OFILES:.o=.d) $(OUTPUT).nds : $(OUTPUT).arm9 $(OUTPUT).arm9 : $(OUTPUT).elf $(OUTPUT).elf : $(OFILES) %.bin.o : %.bin @echo $(notdir $<) @$(bin2o) %.s %.h : %.png %.grit grit $< -fts -o$* -include $(DEPENDS) endif
[edit] C++ Code
now in your source folder create a new cpp file called "main.cpp", thn fill it with the following code:
/**************************************** * NDS Sprite Tutorial * Author: opearn ****************************************/ /*includes*/ //include #include <nds.h> //include sprites #include "sprite.h" int main() { // Setup the video modes. videoSetMode( MODE_0_2D ); vramSetPrimaryBanks(VRAM_A_MAIN_SPRITE,VRAM_B_LCD,VRAM_C_LCD,VRAM_D_LCD); //setup sprites oamInit(&oamMain, SpriteMapping_Bmp_1D_128, false); //initialize the oam u16* gfx = oamAllocateGfx(&oamMain, SpriteSize_64x64,SpriteColorFormat_256Color);//make room for the sprite dmaCopy(spriteTiles,gfx, spriteTilesLen);//copy the sprite dmaCopy(spritePal, SPRITE_PALETTE, spritePalLen); //copy the sprites palette oamEnable(&oamMain); while (1) //infinite loop { oamSet(&oamMain,0,64,32,0,0,SpriteSize_64x64,SpriteColorFormat_256Color,gfx,0,false,false,false,false,false); swiWaitForVBlank(); } return 0; }
You should now have a basic app that shows your sprite at 64 32, but now to make it practical, you'll wan't to create some variables using
int x=0; int y=0;
in the oam set line of code can you tell what the sprites coordinates are? if so replace them with x and y
oamSet(&oamMain,0,x,y,0,0,SpriteSize_64x64,SpriteColorFormat_256Color,gfx,0,false,false,false,false,false);
[edit] oamset functions
http://libnds.devkitpro.org/a00100.html#605548d2e83c72673de90d505e0f816e