リバイバルオブ「へぇボタン for NDS」。


昨日、息抜きで何となくへぇボタンNDSを書き直し始めたら
大体動くようになってしまいました。


手早く仕上げたので昨年版にあったタッチスクリーン対応部分は未実装です。
FSM 内のボタン押離の判断箇所に if してタッチ検出する処理を書けば済む話ですが。


直し箇所

・恐怖の1本ソースはモジュール毎に分割し、
 ハードコーディングな部分(グラフィックリソースへのアクセス)はポインタ経由に変更した
・モジュール分割は、先日 GMC4NDS モジュールに対する akkera102 さんの指摘を参考に
 モジュールが増えないよう工夫を凝らしてみた
・グラフィック・バナーアイコン・サウンドリソースは昨年作ったものをそのまま利用。
 リソースの名称と、grit ファイルの内容だけは書き直し
・昨年版にあったクレジットタイトル・意味のない状態遷移を無くした
・有限状態機械で状態遷移を組み、単純な構造のものとした


リソース・メイクファイル・grit ファイルを含めた
プロジェクト一式はタッチスクリーン対応化してから
アーカイブしてアップします。


Hey-button for NDS


main.c

/*---------------------------------------------------------------------------------
	
	Hey-button for NDS
	
	version 0.01
	Dec 23, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/ndstypes.h>
#include <nds/interrupts.h>
#include <nds/arm9/input.h>

#include <stdio.h>

#include "Bg.h"
#include "Snd.h"
#include "Heybtn.h"


void main_Init(void);


//---------------------------------------------------------------------------------
void main_Init(void) {
//---------------------------------------------------------------------------------
	
	Bg_Init();
	Snd_Init();
	Heybtn_Init();
	
	printf("\x01b[22;26Hreset");
	
}

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	main_Init();
	
	while(1) {
		
		swiWaitForVBlank();
		
		scanKeys();
		
		Heybtn_Update();
		
	}
	
}

//---------------------------------------------------------------------------------


Heybtn.h

/*---------------------------------------------------------------------------------
	
	heybtn module header
	
	version 0.01
	Dec 23, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _HEYBTN_H_
#define _HEYBTN_H_


#ifdef __cplusplus
extern "C" {
#endif


void Heybtn_Init(void);
void Heybtn_Update(void);


#ifdef __cplusplus
}
#endif

#endif	// _HEYBTN_H_


Heybtn.c

/*---------------------------------------------------------------------------------
	
	heybtn module routine
	
	version 0.01
	Dec 23, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/ndstypes.h>
#include <nds/arm9/input.h>

#include "Heybtn.h"

#include "Bg.h"
#include "Snd.h"


#define COUNT_MIN 0
#define COUNT_MAX 20


typedef enum{
	HEYBTN_UP, 
	HEYBTN_DOWN, 
	HEYBTN_CLEAR, 
	HEYBTN_NULL
}HEYBTN_ACT;

typedef struct{
	u16 act;
	u8 count;
}HeybtnStatus;


static HeybtnStatus st_heybtn = {HEYBTN_NULL, COUNT_MIN};


bool heybtn_IsButtonUp(void);
bool heybtn_IsButtonDown(void);
bool heybtn_IsClearCount(void);

void heybtn_CheckState(void);

void heybtn_ButtonUp(void);
void heybtn_ButtonDown(void);
void heybtn_ClearCount(void);

void heybtn_DoAction(void);


//---------------------------------------------------------------------------------
bool heybtn_IsButtonUp(void){
//---------------------------------------------------------------------------------
	
	return (keysUp() & KEY_A) ? true:false;
	
}

//---------------------------------------------------------------------------------
bool heybtn_IsButtonDown(void){
//---------------------------------------------------------------------------------
	
	return (keysDown() & KEY_A) ? true:false;
	
}

//---------------------------------------------------------------------------------
bool heybtn_IsClearCount(void){
//---------------------------------------------------------------------------------
	
	return (keysUp() & KEY_B) ? true:false;
	
}

//---------------------------------------------------------------------------------
void heybtn_CheckState(void){
//---------------------------------------------------------------------------------
	
	if(heybtn_IsButtonUp()){
		st_heybtn.act = HEYBTN_UP;
		
	}else if(heybtn_IsButtonDown()){
		st_heybtn.act = HEYBTN_DOWN;
		
	}else if(heybtn_IsClearCount()){
		st_heybtn.act = HEYBTN_CLEAR;
		
	}else{
		st_heybtn.act = HEYBTN_NULL;
	}
	
}



//---------------------------------------------------------------------------------
void heybtn_ButtonUp(void){
//---------------------------------------------------------------------------------
	
	if(st_heybtn.count < COUNT_MAX){
		st_heybtn.count++;
		Bg_DrawCounter(st_heybtn.count);
	}
	
	Bg_DrawButtonUp();
	
}

//---------------------------------------------------------------------------------
void heybtn_ButtonDown(void){
//---------------------------------------------------------------------------------
	
	if(st_heybtn.count < COUNT_MAX){
		Snd_Play();
	}
	
	Bg_DrawButtonDown();
	
}

//---------------------------------------------------------------------------------
void heybtn_ClearCount(void){
//---------------------------------------------------------------------------------
	
	st_heybtn.count = COUNT_MIN;
	Bg_DrawCounter(st_heybtn.count);
	
}

//---------------------------------------------------------------------------------
void heybtn_DoAction(void){
//---------------------------------------------------------------------------------
	
	switch(st_heybtn.act){
		
	case HEYBTN_UP:
		heybtn_ButtonUp();
		break;
		
	case HEYBTN_DOWN:
		heybtn_ButtonDown();
		break;
		
	case HEYBTN_CLEAR:
		heybtn_ClearCount();
		break;
		
	default: //HEYBTN_NULL
		break;
		
	}
	
}



//---------------------------------------------------------------------------------
void Heybtn_Init(void){
//---------------------------------------------------------------------------------
	
	st_heybtn.act = HEYBTN_NULL;
	st_heybtn.count = COUNT_MIN;
	
	Bg_DrawButtonUp();
	Bg_DrawCounter(st_heybtn.count);
	
}

//---------------------------------------------------------------------------------
void Heybtn_Update(void){
//---------------------------------------------------------------------------------
	
	heybtn_CheckState();
	heybtn_DoAction();
	
}

//---------------------------------------------------------------------------------


Bg.h

/*---------------------------------------------------------------------------------
	
	bg module header
	
	version 0.01
	Dec 23, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _BG_H_
#define _BG_H_


#ifdef __cplusplus
extern "C" {
#endif


void Bg_Init(void);

void Bg_DrawButtonUp(void);
void Bg_DrawButtonDown(void);
void Bg_DrawCounter(int count);


#ifdef __cplusplus
}
#endif

#endif	// _BG_H_


Bg.c

/*---------------------------------------------------------------------------------
	
	bg module routine
	
	version 0.01
	Dec 23, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <string.h>

#include <nds/ndstypes.h>
#include <nds/dma.h>
#include <nds/arm9/video.h>
#include <nds/arm9/background.h>
#include <nds/arm9/console.h>

#include "Bg.h"

#include "topBtnUp.h"
#include "topBtnDown.h"
#include "bottomBtnUp.h"
#include "bottomBtnDown.h"

#include "topCnt00.h"
#include "topCnt01.h"
#include "topCnt02.h"
#include "topCnt03.h"
#include "topCnt04.h"
#include "topCnt05.h"
#include "topCnt06.h"
#include "topCnt07.h"
#include "topCnt08.h"
#include "topCnt09.h"
#include "topCnt10.h"
#include "topCnt11.h"
#include "topCnt12.h"
#include "topCnt13.h"
#include "topCnt14.h"
#include "topCnt15.h"
#include "topCnt16.h"
#include "topCnt17.h"
#include "topCnt18.h"
#include "topCnt19.h"
#include "topCnt20.h"


typedef struct{
	u16 *tile;
	u16 *map;
	u32 len;
}TopCntStatus;


typedef struct{
	
	int top_cnt;
	int top_btn;
	int bottom_btn;
	
	u16 *top_cnt_tile;
	u16 *top_btn_tile;
	u16 *bottom_btn_tile;
	
	u16 *top_cnt_map;
	u16 *top_btn_map;
	u16 *bottom_btn_map;
	
	PrintConsole bottom_cons;
	
}BgStatus;


static TopCntStatus st_top_cnt[21];
static BgStatus st_bg;


void bg_InitCounter(void);


//---------------------------------------------------------------------------------
void bg_InitCounter(void){
//---------------------------------------------------------------------------------
	
	st_top_cnt[0].tile = (u16 *)topCnt00Tiles;
	st_top_cnt[0].map  = (u16 *)topCnt00Map;
	st_top_cnt[0].len  = topCnt00TilesLen;
	
	st_top_cnt[1].tile = (u16 *)topCnt01Tiles;
	st_top_cnt[1].map  = (u16 *)topCnt01Map;
	st_top_cnt[1].len  = topCnt01TilesLen;
	
	st_top_cnt[2].tile = (u16 *)topCnt02Tiles;
	st_top_cnt[2].map  = (u16 *)topCnt02Map;
	st_top_cnt[2].len  = topCnt02TilesLen;
	
	st_top_cnt[3].tile = (u16 *)topCnt03Tiles;
	st_top_cnt[3].map  = (u16 *)topCnt03Map;
	st_top_cnt[3].len  = topCnt03TilesLen;
	
	st_top_cnt[4].tile = (u16 *)topCnt04Tiles;
	st_top_cnt[4].map  = (u16 *)topCnt04Map;
	st_top_cnt[4].len  = topCnt04TilesLen;
	
	st_top_cnt[5].tile = (u16 *)topCnt05Tiles;
	st_top_cnt[5].map  = (u16 *)topCnt05Map;
	st_top_cnt[5].len  = topCnt05TilesLen;
	
	st_top_cnt[6].tile = (u16 *)topCnt06Tiles;
	st_top_cnt[6].map  = (u16 *)topCnt06Map;
	st_top_cnt[6].len  = topCnt06TilesLen;
	
	st_top_cnt[7].tile = (u16 *)topCnt07Tiles;
	st_top_cnt[7].map  = (u16 *)topCnt07Map;
	st_top_cnt[7].len  = topCnt07TilesLen;
	
	st_top_cnt[8].tile = (u16 *)topCnt08Tiles;
	st_top_cnt[8].map  = (u16 *)topCnt08Map;
	st_top_cnt[8].len  = topCnt08TilesLen;
	
	st_top_cnt[9].tile = (u16 *)topCnt09Tiles;
	st_top_cnt[9].map  = (u16 *)topCnt09Map;
	st_top_cnt[9].len  = topCnt09TilesLen;
	
	st_top_cnt[10].tile = (u16 *)topCnt10Tiles;
	st_top_cnt[10].map  = (u16 *)topCnt10Map;
	st_top_cnt[10].len  = topCnt10TilesLen;
	
	st_top_cnt[11].tile = (u16 *)topCnt11Tiles;
	st_top_cnt[11].map  = (u16 *)topCnt11Map;
	st_top_cnt[11].len  = topCnt11TilesLen;
	
	st_top_cnt[12].tile = (u16 *)topCnt12Tiles;
	st_top_cnt[12].map  = (u16 *)topCnt12Map;
	st_top_cnt[12].len  = topCnt12TilesLen;
	
	st_top_cnt[13].tile = (u16 *)topCnt13Tiles;
	st_top_cnt[13].map  = (u16 *)topCnt13Map;
	st_top_cnt[13].len  = topCnt13TilesLen;
	
	st_top_cnt[14].tile = (u16 *)topCnt14Tiles;
	st_top_cnt[14].map  = (u16 *)topCnt14Map;
	st_top_cnt[14].len  = topCnt14TilesLen;
	
	st_top_cnt[15].tile = (u16 *)topCnt15Tiles;
	st_top_cnt[15].map  = (u16 *)topCnt15Map;
	st_top_cnt[15].len  = topCnt15TilesLen;
	
	st_top_cnt[16].tile = (u16 *)topCnt16Tiles;
	st_top_cnt[16].map  = (u16 *)topCnt16Map;
	st_top_cnt[16].len  = topCnt16TilesLen;
	
	st_top_cnt[17].tile = (u16 *)topCnt17Tiles;
	st_top_cnt[17].map  = (u16 *)topCnt17Map;
	st_top_cnt[17].len  = topCnt17TilesLen;
	
	st_top_cnt[18].tile = (u16 *)topCnt18Tiles;
	st_top_cnt[18].map  = (u16 *)topCnt18Map;
	st_top_cnt[18].len  = topCnt18TilesLen;
	
	st_top_cnt[19].tile = (u16 *)topCnt19Tiles;
	st_top_cnt[19].map  = (u16 *)topCnt19Map;
	st_top_cnt[19].len  = topCnt19TilesLen;
	
	st_top_cnt[20].tile = (u16 *)topCnt20Tiles;
	st_top_cnt[20].map  = (u16 *)topCnt20Map;
	st_top_cnt[20].len  = topCnt20TilesLen;
	
}

//---------------------------------------------------------------------------------
void Bg_Init(void){
//---------------------------------------------------------------------------------
	
	//initStatus
	memset(&st_bg, 0, sizeof(st_bg));
	
	
	//initVideoMode
	videoSetMode(MODE_0_2D);
	videoSetModeSub(MODE_0_2D);
	
	//initVRAMBank
	vramSetMainBanks(VRAM_A_MAIN_BG, VRAM_B_LCD, VRAM_C_SUB_BG, VRAM_D_LCD);
	
	
	//initConsole
	st_bg.bottom_cons = *consoleInit(0, 0, BgType_Text4bpp, BgSize_T_256x256, 0, 1, false, true);
	consoleSelect(&st_bg.bottom_cons);
	
	
	//initBg
	st_bg.top_cnt = bgInit(0, BgType_Text8bpp, BgSize_T_256x256, 0, 1);
	st_bg.top_btn = bgInit(1, BgType_Text8bpp, BgSize_T_256x256, 1, 2);
	st_bg.bottom_btn = bgInitSub(1, BgType_Text8bpp, BgSize_T_256x256, 1, 2);
	
	//initBGPriority
	bgSetPriority(st_bg.top_cnt, 0);
	bgSetPriority(st_bg.top_btn, 1);
	bgSetPriority(st_bg.bottom_btn, 1);
	
	//getTilePtr
	st_bg.top_cnt_tile = bgGetGfxPtr(st_bg.top_cnt);
	st_bg.top_btn_tile = bgGetGfxPtr(st_bg.top_btn);
	st_bg.bottom_btn_tile = bgGetGfxPtr(st_bg.bottom_btn);
	
	//getMapPtr
	st_bg.top_cnt_map = bgGetMapPtr(st_bg.top_cnt);
	st_bg.top_btn_map = bgGetMapPtr(st_bg.top_btn);
	st_bg.bottom_btn_map = bgGetMapPtr(st_bg.bottom_btn);
	
	
	//initBGPalette
	dmaCopy(topBtnUpPal, BG_PALETTE, topBtnUpPalLen);
	dmaCopy(bottomBtnUpPal, BG_PALETTE_SUB, bottomBtnUpPalLen);
	
	BG_PALETTE_SUB[255] = RGB15(31, 31, 31);
	
	
	//initCounterStatus
	bg_InitCounter();
	
}

//---------------------------------------------------------------------------------
void Bg_DrawButtonUp(void){
//---------------------------------------------------------------------------------
	
	//setBGTiles
	dmaCopy(topBtnUpTiles, st_bg.top_btn_tile, topBtnUpTilesLen);
	dmaCopy(bottomBtnUpTiles, st_bg.bottom_btn_tile, bottomBtnUpTilesLen);
	
	//setBGMap
	dmaCopy(topBtnUpMap, st_bg.top_btn_map, topBtnUpMapLen);
	dmaCopy(bottomBtnUpMap, st_bg.bottom_btn_map, bottomBtnUpMapLen);
	
}

//---------------------------------------------------------------------------------
void Bg_DrawButtonDown(void){
//---------------------------------------------------------------------------------
	
	//setBGTiles
	dmaCopy(topBtnDownTiles, st_bg.top_btn_tile, topBtnDownTilesLen);
	dmaCopy(bottomBtnDownTiles, st_bg.bottom_btn_tile, bottomBtnDownTilesLen);
	
	//setBGMap
	dmaCopy(topBtnDownMap, st_bg.top_btn_map, topBtnDownMapLen);
	dmaCopy(bottomBtnDownMap, st_bg.bottom_btn_map, bottomBtnDownMapLen);
	
}

//---------------------------------------------------------------------------------
void Bg_DrawCounter(int count){
//---------------------------------------------------------------------------------
	
	int x, y, i;
	
	
	//setBGTiles
	dmaCopy(st_top_cnt[count].tile, st_bg.top_cnt_tile, st_top_cnt[count].len);
	
	//setBGMap
	i = 0;
	
	for(y = 0;y < 8;y++){
		for(x = 0;x < 13;x++){
			st_bg.top_cnt_map[(x + 5) + (32 * (y + 14))] = st_top_cnt[count].map[i];
			i++;
		}
	}
	
}

//---------------------------------------------------------------------------------


Snd.h

/*---------------------------------------------------------------------------------
	
	snd module header
	
	version 0.01
	Dec 23, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _SND_H_
#define _SND_H_


#ifdef __cplusplus
extern "C" {
#endif


void Snd_Init(void);
void Snd_Play(void);


#ifdef __cplusplus
}
#endif

#endif	// _SND_H_


Snd.c

/*---------------------------------------------------------------------------------
	
	snd module routine
	
	version 0.01
	Dec 23, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/ndstypes.h>
#include <nds/arm9/sound.h>

#include "Snd.h"
#include "snd_voice_raw.h"


//---------------------------------------------------------------------------------
void Snd_Init(void){
//---------------------------------------------------------------------------------
	
	soundEnable();
	
}

//---------------------------------------------------------------------------------
void Snd_Play(void){
//---------------------------------------------------------------------------------
	
	soundPlaySample(snd_voice_raw, SoundFormat_16Bit, snd_voice_raw_size, 11025, 127, 63, false, 0);
	
}

//---------------------------------------------------------------------------------