Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Controllers for the behaviour of game objects are reusable across sev…
…eral projects. They are also used for [[https://sylvainhb.blogspot.com/search/label/runme|the standalone "runME" tool]] so let's have them in their own directory.
  • Loading branch information
PypeBros committed Apr 22, 2018
1 parent 3515d1b commit aeff7ad
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -3,5 +3,6 @@ ROOT:=$(CURDIR)
include libgeds/Makefile
include ppp9/Makefile
include ppp7/Makefile
include libplugins/Makefile
include Demo/Makefile

63 changes: 63 additions & 0 deletions libplugins/Makefile
@@ -0,0 +1,63 @@
#---------------------------------------------------------------------------------
.SUFFIXES:

ROOT ?= $(abspath $(CURDIR)/..)
M := libplugins
CPU := arm9
TARGET := $M
DATA := data
INCLUDES= include
LIBS = -lfat -lnds9 -ldswifi9
LIBDIRS = $(LIBNDS) $(LIBPPP9) $(LIBGEDS) $(LIBNTXM)

include $(ROOT)/common.mk

#---------------------------------------------------------------------------------
# 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
#---------------------------------------------------------------------------------

LIBRARY := $(OUTPUT)/lib/$M.a
$M_LIBRARY := $(LIBRARY)

.PHONY: $(BUILD) clean install
all: $M-all
$M-all: $(BUILD)
#---------------------------------------------------------------------------------
$(BUILD):
mkdir -p $@

#---------------------------------------------------------------------------------
clean: $M-clean
$M-clean:
@echo $@ ...
rm -fr $(libplugins_BUILD) $(libplugins_LIBRARY)

$M-dox:
-rm dox/html/*.html
doxygen

$M-postdox: dox
cp dox/html/doxygen.css.org dox/html/doxygen.css
cp dox/html/tabs.css.org dox/html/tabs.css
cd dox/html ; fe class*.html "mv % /tmp/% ; sed /tmp/% -e 's:/a>0[0-9]*:/a>:g;' > % ; echo % stripped" ; cd -
cd dox/html ; mv classes.html /tmp/ ; sed /tmp/classes.html -e "s/>[A-Z][a-zA-Z]*::/>/g;" > classes.html ; cd -
echo all done.


#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$M-all: $(LIBRARY) #checklib.elf

$(LIBRARY): $(OFILES)

checklib.elf: $(OFILES) ../main.o

../main.o: ../main.c

-include $(DEPENDS)


66 changes: 66 additions & 0 deletions libplugins/source/dpad.cpp
@@ -0,0 +1,66 @@
#include "nds.h"
#include "interfaces.h"

/** reads the buttons & dpad state and make them available in one of the target gob variables
*/
class DpadController : public iGobController {
NOCOPY(DpadController);
/** some keys are only mentioned "pressed" for a few frames after they are actually changed
* from 'released' to 'pressed' state, to allow e.g. that your character doesn't keep
* jumping. timeout indicates how many frames before the button is considered pressed
* for too long.
*/
static int timeout[32];

/** each state can define which buttons/direction must use timeout array */
unsigned timemask;
static int delay;
/** knowing which button/direction were pressed at the previous frame is critical to
* know which be considered a 'new' keypress.
* WARNING: this code assumes only one GameObject uses a DpadController
*/
static int prevkeys;
public:
DpadController(const iControllerFactory *icf, unsigned m) : iGobController(icf), timemask(m) {}
virtual ~DpadController() {}
virtual iThought think(s16 gob[8], GameObject *g) {
iThought me = NONE;
int prevdirs = gob[GOB_DIRS];
int newkeys = 0;
int kh = (keysHeld()|keysDown()) & ~(KEY_START|KEY_SELECT);

for (unsigned i=0,j=1 ; i<31 ; i++, j=j<<1) {
if ((kh&j)) {
if (!(prevkeys & j)) timeout[i]=delay;
else if ((timemask & j) && timeout[i]>0) timeout[i]--;
} else timeout[i]=0;
if (timeout[i]) newkeys|=j;
}
gob[GOB_DIRS]=newkeys;
prevkeys=kh;
return combine(me, gob, g);
}
};

int DpadController::delay=6;
int DpadController::prevkeys=0;
int DpadController::timeout[32];

class GobDpadFactory : public iControllerFactory {
NOCOPY(GobDpadFactory);
public:
GobDpadFactory(const std::string str) {
name=str;
}
virtual iGobController* create(char* args) {
unsigned mask=KEY_DPAD|KEY_R;
siscanf(args,"%x",&mask);
return new DpadController(this, ~mask);
}
virtual ~GobDpadFactory() {}
};

namespace {
GobDpadFactory dpad("dpad");
};

0 comments on commit aeff7ad

Please sign in to comment.