読者です 読者をやめる 読者になる 読者になる

ハートに16連射。(28)


サウンドトラックモードの作成。
所謂サウンドテストってやつ。


サウンドモジュール、BGM, SE が未着手なので再生のタイミングを printf() 辺りと想定して記述。
基本的な動作は下記コードの通り。
printf() で表示してるタイトルやメニューは
本ちゃんでは自前(BG モジュール内)の Draw 関数に挿げ替えるのを忘れないようにしなくては。


sountra module test


main.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	sountra module test
	
	version 0.01
	Feb 15, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <nds.h>
#include "Sountra.h"


//---------------------------------------------------------------------------------
int main(void){
//---------------------------------------------------------------------------------
	
	consoleDemoInit();
	
	while(1) {
		
		swiWaitForVBlank();
		scanKeys();
		
		Sountra_Update();
		
	}
	
}


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


sountra.h

/*---------------------------------------------------------------------------------
	
	shtwatch
	sountra module header
	
	version 0.01
	Feb 15, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _SOUNTRA_H_
#define _SOUNTRA_H_


#ifdef __cplusplus
extern "C" {
#endif


void Sountra_Clear(void);
void Sountra_Update(void);


#ifdef __cplusplus
}
#endif

#endif	//_SOUNTRA_H_


sountra.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	sountra module routine
	
	version 0.01
	Feb 15, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>//debug print
#include <stdlib.h>//debug
#include <string.h>
#include <nds/ndstypes.h>
#include <nds/arm9/input.h>
#include "Sountra.h"


#define SND_VAL_POSX 5
#define SND_VAL_POSY 4

#define SND_BGM_MAX  6
#define SND_SE_MAX  18

typedef enum{
	SND_BGM = 0, 
	SND_SE, 
	SND_EXIT
}SND_VAL;

const static int snd_max[2] = {SND_BGM_MAX, SND_SE_MAX};


typedef enum{
	SOUNTRA_INIT = 0, 
	SOUNTRA_EXEC, 
	SOUNTRA_EXIT
}SOUNTRA_ACT;

typedef struct{
	
	u16 act;
	
	u32 kdr;
	
	int snd_cur;
	int snd_idx[2];
	
}SountraStatus;

static SountraStatus st_sountra;


void sountra_Init(void);
void sountra_Exec(void);
void sountra_Exit(void);
void sountra_ExecKeys(void);
void sountra_ExecCursorX(void);
void sountra_ExecCursorY(void);
void sountra_ExecPlay(void);
void sountra_ExecStop(void);
void sountra_ExecQuit(void);


//---------------------------------------------------------------------------------
void Sountra_Clear(void){
//---------------------------------------------------------------------------------
	
	sountra_Exit();
	
}

//---------------------------------------------------------------------------------
void Sountra_Update(void){
//---------------------------------------------------------------------------------
	
	switch(st_sountra.act){
		
	case SOUNTRA_INIT:
		sountra_Init();
		break;
		
	case SOUNTRA_EXEC:
		sountra_Exec();
		break;
		
	case SOUNTRA_EXIT:
		sountra_Exit();
		break;
		
	}
	
}

//---------------------------------------------------------------------------------
void sountra_Init(void){
//---------------------------------------------------------------------------------
	
	memset(&st_sountra, 0, sizeof(st_sountra));
	
	keysSetRepeat(20, 10);
	
	//debug print
	printf("\x01b[1;5H- SOUND TRACK MODE -");
	printf("\x01b[%d;%dHBGM :%4d", SND_VAL_POSY,     SND_VAL_POSX, st_sountra.snd_idx[SND_BGM]);
	printf("\x01b[%d;%dHSE  :%4d", SND_VAL_POSY + 1, SND_VAL_POSX, st_sountra.snd_idx[SND_SE]);
	printf("\x01b[%d;%dHEXIT",     SND_VAL_POSY + 2, SND_VAL_POSX);
	
	//debug print
	printf("\x01b[%d;%dH>", SND_VAL_POSY + st_sountra.snd_cur, SND_VAL_POSX - 1);
	
	st_sountra.act = SOUNTRA_EXEC;
	
}

//---------------------------------------------------------------------------------
void sountra_Exec(void){
//---------------------------------------------------------------------------------
	
	sountra_ExecKeys();
	
	sountra_ExecCursorX();
	sountra_ExecCursorY();
	sountra_ExecPlay();
	sountra_ExecStop();
	sountra_ExecQuit();
	
}

//---------------------------------------------------------------------------------
void sountra_Exit(void){
//---------------------------------------------------------------------------------
	
	//debug print
	printf("\x01b[18;4HEXIT SOUNDTRA");
	st_sountra.act = SOUNTRA_INIT;
	
	//debug
	exit(0);
	
}

//---------------------------------------------------------------------------------
void sountra_ExecKeys(void){
//---------------------------------------------------------------------------------
	
	st_sountra.kdr = keysDownRepeat();
	
}

//---------------------------------------------------------------------------------
void sountra_ExecCursorX(void){
//---------------------------------------------------------------------------------
	
	if(st_sountra.snd_cur == SND_EXIT) return;
	
	
	if(st_sountra.kdr & KEY_LEFT){
		
		if(st_sountra.snd_idx[st_sountra.snd_cur] > 0){
			st_sountra.snd_idx[st_sountra.snd_cur]--;
		}else{
			st_sountra.snd_idx[st_sountra.snd_cur] = snd_max[st_sountra.snd_cur] - 1;
		}
		
		//debug print
		printf("\x01b[%d;%dH%4d", SND_VAL_POSY + st_sountra.snd_cur, SND_VAL_POSX + 5, st_sountra.snd_idx[st_sountra.snd_cur]);
		
	}else if(st_sountra.kdr & KEY_RIGHT){
		
		if(st_sountra.snd_idx[st_sountra.snd_cur] < snd_max[st_sountra.snd_cur] - 1){
			st_sountra.snd_idx[st_sountra.snd_cur]++;
		}else{
			st_sountra.snd_idx[st_sountra.snd_cur] = 0;
		}
		
		//debug print
		printf("\x01b[%d;%dH%4d", SND_VAL_POSY + st_sountra.snd_cur, SND_VAL_POSX + 5, st_sountra.snd_idx[st_sountra.snd_cur]);
		
	}
	
}

//---------------------------------------------------------------------------------
void sountra_ExecCursorY(void){
//---------------------------------------------------------------------------------
	
	if(st_sountra.kdr & KEY_UP){
		
		//debug print
		printf("\x01b[%d;%dH ", SND_VAL_POSY + st_sountra.snd_cur, SND_VAL_POSX - 1);
		
		if(st_sountra.snd_cur > SND_BGM){
			st_sountra.snd_cur--;
		}else{
			st_sountra.snd_cur = SND_EXIT;
		}
		
		//debug print
		printf("\x01b[%d;%dH>", SND_VAL_POSY + st_sountra.snd_cur, SND_VAL_POSX - 1);
		
	}else if(st_sountra.kdr & KEY_DOWN){
		
		//debug print
		printf("\x01b[%d;%dH ", SND_VAL_POSY + st_sountra.snd_cur, SND_VAL_POSX - 1);
		
		if(st_sountra.snd_cur < SND_EXIT){
			st_sountra.snd_cur++;
		}else{
			st_sountra.snd_cur = SND_BGM;
		}
		
		//debug print
		printf("\x01b[%d;%dH>", SND_VAL_POSY + st_sountra.snd_cur, SND_VAL_POSX - 1);
		
	}
	
}

//---------------------------------------------------------------------------------
void sountra_ExecPlay(void){
//---------------------------------------------------------------------------------
	
	if(st_sountra.snd_cur == SND_EXIT) return;
	
	
	if(st_sountra.kdr & KEY_A){
		
		if(st_sountra.snd_cur == SND_BGM){
			
			//debug print
			printf("\x01b[15;4HPLAY BGM");
			
		}else if(st_sountra.snd_cur == SND_SE){
			
			//debug print
			printf("\x01b[16;4HPLAY SE ");
			
		}
		
	}
	
}

//---------------------------------------------------------------------------------
void sountra_ExecStop(void){
//---------------------------------------------------------------------------------
	
	if(st_sountra.snd_cur == SND_EXIT) return;
	
	
	if(st_sountra.kdr & KEY_B){
		
		if(st_sountra.snd_cur == SND_BGM){
			
			//debug print
			printf("\x01b[15;4HSTOP BGM");
			
		}else if(st_sountra.snd_cur == SND_SE){
			
			//debug print
			printf("\x01b[16;4HSTOP SE ");
			
		}
		
	}
	
}

//---------------------------------------------------------------------------------
void sountra_ExecQuit(void){
//---------------------------------------------------------------------------------
	
	if( (st_sountra.kdr & KEY_START) || 
		( (st_sountra.kdr & KEY_A) && st_sountra.snd_cur == SND_EXIT ) )
	{
		
		//debug print
		printf("\x01b[15;4HSTOP BGM");
		printf("\x01b[16;4HSTOP SE ");
		
		st_sountra.act = SOUNTRA_EXIT;
		
	}
	
}

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