From edb9ebda3fda70ee89d8dd8fbe4807c1e6a7513b Mon Sep 17 00:00:00 2001 From: mtabachenko Date: Tue, 23 Mar 2010 18:56:10 +0000 Subject: [PATCH] core: - fix overflow in matrices, NDS using only 24 bits for fixed-point numbers (fixed minigame with carrot :) and dialogs with star in "M&L Bowser Inside Story"); tools: - add matrix stack test; --- desmume/src/gfx3d.cpp | 18 +-- desmume/src/matrix.cpp | 2 +- desmume/src/matrix.h | 1 - tools/ds_tests/matrixStack/Makefile | 130 +++++++++++++++++++++ tools/ds_tests/matrixStack/source/main.cpp | 104 +++++++++++++++++ 5 files changed, 240 insertions(+), 15 deletions(-) create mode 100644 tools/ds_tests/matrixStack/Makefile create mode 100644 tools/ds_tests/matrixStack/source/main.cpp diff --git a/desmume/src/gfx3d.cpp b/desmume/src/gfx3d.cpp index 43b4e6c5c..8c7cff972 100644 --- a/desmume/src/gfx3d.cpp +++ b/desmume/src/gfx3d.cpp @@ -878,7 +878,7 @@ static void gfx3d_glLoadIdentity() static BOOL gfx3d_glLoadMatrix4x4(s32 v) { - mtxCurrent[mode][ML4x4ind] = (float)v; + mtxCurrent[mode][ML4x4ind] = (float)((v<<4)>>4); ++ML4x4ind; if(ML4x4ind<16) return FALSE; @@ -897,7 +897,7 @@ static BOOL gfx3d_glLoadMatrix4x4(s32 v) static BOOL gfx3d_glLoadMatrix4x3(s32 v) { - mtxCurrent[mode][ML4x3ind] = (float)v; + mtxCurrent[mode][ML4x3ind] = (float)((v<<4)>>4); ML4x3ind++; if((ML4x3ind & 0x03) == 3) ML4x3ind++; @@ -920,7 +920,7 @@ static BOOL gfx3d_glLoadMatrix4x3(s32 v) static BOOL gfx3d_glMultMatrix4x4(s32 v) { - mtxTemporal[MM4x4ind] = (float)v; + mtxTemporal[MM4x4ind] = (float)((v<<4)>>4); MM4x4ind++; if(MM4x4ind<16) return FALSE; @@ -946,7 +946,7 @@ static BOOL gfx3d_glMultMatrix4x4(s32 v) static BOOL gfx3d_glMultMatrix4x3(s32 v) { - mtxTemporal[MM4x3ind] = (float)v; + mtxTemporal[MM4x3ind] = (float)((v<<4)>>4); MM4x3ind++; if((MM4x3ind & 0x03) == 3) MM4x3ind++; @@ -978,7 +978,7 @@ static BOOL gfx3d_glMultMatrix4x3(s32 v) static BOOL gfx3d_glMultMatrix3x3(s32 v) { - mtxTemporal[MM3x3ind] = (float)v; + mtxTemporal[MM3x3ind] = (float)((v<<4)>>4); MM3x3ind++; @@ -1626,14 +1626,6 @@ s32 gfx3d_GetDirectionalMatrix (unsigned int index) return (s32)(mtxCurrent[2][_index]*(1<<12)); } -void gfx3d_ClearStack() -{ - MatrixStackSetStackPosition(&mtxStack[0], -5); - //MatrixStackSetStackPosition(&mtxStack[1], -55); - //MatrixStackSetStackPosition(&mtxStack[2], -55); //? - MatrixStackSetStackPosition(&mtxStack[3], -5); -} - void gfx3d_glAlphaFunc(u32 v) { gfx3d.state.alphaTestRef = v&31; diff --git a/desmume/src/matrix.cpp b/desmume/src/matrix.cpp index 344e69612..53b0d1543 100644 --- a/desmume/src/matrix.cpp +++ b/desmume/src/matrix.cpp @@ -217,7 +217,7 @@ MatrixStack::MatrixStack(int size, int type) this->type = type; } -void MatrixStackSetStackPosition (MatrixStack *stack, int pos) +static void MatrixStackSetStackPosition (MatrixStack *stack, int pos) { stack->position += pos; diff --git a/desmume/src/matrix.h b/desmume/src/matrix.h index 60b1865fd..3520f2cf6 100644 --- a/desmume/src/matrix.h +++ b/desmume/src/matrix.h @@ -59,7 +59,6 @@ void MatrixIdentity (float *matrix); void MatrixTranspose (float *matrix); void MatrixStackInit (MatrixStack *stack); void MatrixStackSetMaxSize (MatrixStack *stack, int size); -void MatrixStackSetStackPosition (MatrixStack *stack, int pos); void MatrixStackPushMatrix (MatrixStack *stack, const float *ptr); void MatrixStackPopMatrix (float *mtxCurr, MatrixStack *stack, int size); float* MatrixStackGetPos (MatrixStack *stack, int pos); diff --git a/tools/ds_tests/matrixStack/Makefile b/tools/ds_tests/matrixStack/Makefile new file mode 100644 index 000000000..1d2db75df --- /dev/null +++ b/tools/ds_tests/matrixStack/Makefile @@ -0,0 +1,130 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITARM)),) +$(error "Please set DEVKITARM in your environment. export DEVKITARM=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 +#--------------------------------------------------------------------------------- +TARGET := $(shell basename $(CURDIR)) +BUILD := build +SOURCES := source +DATA := data +INCLUDES := include + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +ARCH := -mthumb -mthumb-interwork + +CFLAGS := -g -Wall -O2\ + -march=armv5te -mtune=arm946e-s \ + -ffast-math \ + $(ARCH) + +CFLAGS += $(INCLUDE) -DARM9 +CXXFLAGS := $(CFLAGS) + +ASFLAGS := -g $(ARCH) -march=armv5te -mtune=arm946e-s +LDFLAGS = -specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) + +#--------------------------------------------------------------------------------- +# any extra libraries we wish to link with the project +#--------------------------------------------------------------------------------- +LIBS := -lfat -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 ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export OUTPUT := $(CURDIR)/$(TARGET) + +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) \ + $(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 + + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +$(OUTPUT).nds : $(OUTPUT).arm9 +$(OUTPUT).arm9 : $(OUTPUT).elf +$(OUTPUT).elf : $(OFILES) + +#--------------------------------------------------------------------------------- +%.bin.o : %.bin +#--------------------------------------------------------------------------------- + @echo $(notdir $<) + @$(bin2o) + + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- diff --git a/tools/ds_tests/matrixStack/source/main.cpp b/tools/ds_tests/matrixStack/source/main.cpp new file mode 100644 index 000000000..66e77c830 --- /dev/null +++ b/tools/ds_tests/matrixStack/source/main.cpp @@ -0,0 +1,104 @@ +/* Matrix Stack test + + Copyright 2010 DeSmuME team + + This file is part of DeSmuME + + DeSmuME is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DeSmuME is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DeSmuME; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include +#include + +GL_MATRIX_MODE_ENUM modes[4] = {GL_PROJECTION, GL_POSITION, GL_MODELVIEW, GL_TEXTURE}; +char *modesTXT[4] = {"PROJECTION", "POSITION", "MODELVIEW", "TEXTURE"}; + +PrintConsole *scr = consoleDemoInit(); + +s32 POPstep = 0; +u32 mode = 0; +u32 POPs = 0, PUSHes = 0; +//--------------------------------------------------------------------------------- +int main(void) { +//--------------------------------------------------------------------------------- + int keys; + videoSetModeSub(MODE_0_2D); + videoSetMode(MODE_0_3D); + + vramSetBankA(VRAM_A_MAIN_BG); + vramSetBankC(VRAM_C_SUB_BG); + + consoleInit(scr, 3, BgType_Text4bpp, BgSize_T_256x256, 31, 0, false, true); + + glInit(); + glMatrixMode(GL_PROJECTION); + glFlush(0); + + POPstep=0; + mode = 0; + POPs = 0; + PUSHes = 0; + + while(1) { + scanKeys(); + keys = keysDown(); + if(keys & KEY_A) + { + glPushMatrix(); + PUSHes++; + } + if(keys & KEY_B) + { + glPopMatrix(POPstep); + POPs++; + } + if(keys & KEY_X) + { + mode++; + if (mode > 3) mode = 0; + glMatrixMode(modes[mode]); + } + + if(keys & KEY_UP) + { + if (POPstep < 31) POPstep += 1; + } + if(keys & KEY_DOWN) + { + if (POPstep > -30) POPstep -= 1; + } + + consoleClear(); + iprintf("Matrix mode = %i:%s\n", mode, modesTXT[mode]); + iprintf("\n"); + iprintf("GFX STATUS = 0x%08X\n", GFX_STATUS); + iprintf("\t - Pos&Vec level = %i\n", (GFX_STATUS>>8)&0x1F); + iprintf("\t - Proj. level = %i\n", (GFX_STATUS>>13)&0x01); + iprintf("\t - Error %s\n", (GFX_STATUS>>15)&0x01?"YES":"no"); + iprintf("\n"); + iprintf("POP step = %i\n", POPstep); + iprintf("POP count = %i\n", POPs); + iprintf("PUSH count = %i\n", PUSHes); + + iprintf("\n"); + iprintf("A\t - PUSH\n"); + iprintf("B\t - POP\n"); + iprintf("UP\t- POP increment step\n"); + iprintf("DOWN - POP decrement step\n"); + iprintf("X\t - Change mode\n"); + swiWaitForVBlank(); + } + + return 0; +}