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

ジャンケンゲームを作ろう。5


今急いで作るものじゃないけど会社の休み時間と通勤時間
何もしないのは勿体無いのでちょこちょこと作業再開中。


GMC4NDS でのモジュール分けの反省点を踏まえて頑張ってみよう。
一先ずの状態のソースなので仕様は変わるかもしれませぬ。


disp module test


main.c

/*---------------------------------------------------------------------------------
	
	disp module test
	
	version 0.01
	Jan 18, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <nds/ndstypes.h>
#include <nds/interrupts.h>
#include <nds/arm9/input.h>
#include <nds/arm9/console.h>
#include "JankenTypes.h"
#include "Disp.h"


//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	u8 win_cnt = 0;
	JANKEN_JUDGE judge = JANKEN_MAKE;
	
	
	consoleDemoInit();
	
	printf("\x01b[0;0Hdisp module test\n\nversion 0.01\nJan 18, 2010\n\nBy. REGEKATSU\n\n\n");
	
	//clearDisp
	Disp_Clear();
	
	while(1) {
		
		swiWaitForVBlank();
		scanKeys();
		
		
		//JankenWinCount
		Disp_SetWinCount( BIT(win_cnt) );
		
		//JankenJudge
		Disp_SetJudge( BIT(judge) );
		
		//JankenHand
		if(keysDown() & KEY_LEFT){
			Disp_SetHand( BIT(JANKEN_GOO) );
			
		}else if(keysDown() & KEY_UP){
			Disp_SetHand( BIT(JANKEN_CHOKI) );
			
		}else if(keysDown() & KEY_RIGHT){
			Disp_SetHand( BIT(JANKEN_PAR) );
			
		}else if(keysDown() & KEY_START){
			Disp_SetHand( BIT(JANKEN_GOO) | BIT(JANKEN_CHOKI) | BIT(JANKEN_PAR) );
			
		}else if(keysDown() & KEY_SELECT){
			Disp_SetHand(0);
			
		}
		
		
		if(keysDown() & KEY_A){
			
			if(win_cnt < 5){
				win_cnt++;
			}else{
				win_cnt = 0;
			}
			
		}
		
		if(keysDown() & KEY_B){
			
			if(judge < JANKEN_KACHI){
				judge++;
			}else{
				judge = JANKEN_MAKE;
			}
			
		}
		
	}
	
}

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


JankenTypes.h

/*---------------------------------------------------------------------------------
	
	jankenTypes define header
	
	version 0.01
	Jan 18, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _JANKEN_TYPES_H_
#define _JANKEN_TYPES_H_


typedef enum{
	JANKEN_MAKE  = 0, 
	JANKEN_AIKO  = 1, 
	JANKEN_KACHI = 2
}JANKEN_JUDGE;


typedef enum{
	JANKEN_GOO   = 0, 
	JANKEN_CHOKI = 1, 
	JANKEN_PAR   = 2
}JANKEN_HAND;


#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}
#endif

#endif	//_JANKEN_TYPES_H_


Disp.h

/*---------------------------------------------------------------------------------
	
	disp module header
	
	version 0.01
	Jan 18, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _DISP_H_
#define _DISP_H_


#include <nds/ndstypes.h>


#ifdef __cplusplus
extern "C" {
#endif


void Disp_Clear(void);

void Disp_SetWinCount(u8 win_cnt);
void Disp_SetJudge(u8 judge);
void Disp_SetHand(u8 hand);


#ifdef __cplusplus
}
#endif

#endif	//_DISP_H_


Disp.c

/*---------------------------------------------------------------------------------
	
	disp module routine
	
	version 0.01
	Jan 18, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include "Disp.h"


typedef struct{
	u8 win_cnt;
	u8 judge;
	u8 hand;
}DispStatus;


static DispStatus st_disp;


#define IS_VAL(x) ((x)?true:false)


void disp_DrawWinCount(void);
void disp_DrawJudge(void);
void disp_DrawHand(void);


//---------------------------------------------------------------------------------
void Disp_Clear(void){
//---------------------------------------------------------------------------------
	
	memset(&st_disp, 0, sizeof(st_disp));
	
	disp_DrawWinCount();
	disp_DrawJudge();
	disp_DrawHand();
	
}

//---------------------------------------------------------------------------------
void Disp_SetWinCount(u8 win_cnt){
//---------------------------------------------------------------------------------
	
	st_disp.win_cnt = win_cnt & 0x3e;
	disp_DrawWinCount();
	
}

//---------------------------------------------------------------------------------
void disp_DrawWinCount(void){
//---------------------------------------------------------------------------------
	
	printf("\x01b[9;0HDISP_WIN_CNT\n"
		"  2  4  8 16 32\n");
	
	printf(" %2d", IS_VAL(st_disp.win_cnt & 2));//win_cnt_1
	printf(" %2d", IS_VAL(st_disp.win_cnt & 4));//win_cnt_2
	printf(" %2d", IS_VAL(st_disp.win_cnt & 8));//win_cnt_3
	printf(" %2d", IS_VAL(st_disp.win_cnt & 16));//win_cnt_4
	printf(" %2d", IS_VAL(st_disp.win_cnt & 32));//win_cnt_5
	
}

//---------------------------------------------------------------------------------
void Disp_SetJudge(u8 judge){
//---------------------------------------------------------------------------------
	
	st_disp.judge = judge & 0x07;
	disp_DrawJudge();
	
}

//---------------------------------------------------------------------------------
void disp_DrawJudge(void){
//---------------------------------------------------------------------------------
	
	printf("\x01b[13;0HDISP_JUDGE\n"
		"  MAKE  AIKO KACHI\n");
	
	printf(" %5d", IS_VAL(st_disp.judge & 1));//JANKEN_MAKE
	printf(" %5d", IS_VAL(st_disp.judge & 2));//JANKEN_AIKO
	printf(" %5d", IS_VAL(st_disp.judge & 4));//JANKEN_KACHI
	
}

//---------------------------------------------------------------------------------
void Disp_SetHand(u8 hand){
//---------------------------------------------------------------------------------
	
	st_disp.hand = hand & 0x07;
	disp_DrawHand();
	
}

//---------------------------------------------------------------------------------
void disp_DrawHand(void){
//---------------------------------------------------------------------------------
	
	printf("\x01b[17;0HDISP_HAND\n");
	printf("\x01b[2K\r");
	
	if(st_disp.hand & 1){
		printf("GOO");
	}
	
	if(st_disp.hand & 2){
		printf("CHO");
	}
		
	if(st_disp.hand & 4){
		printf("PAR");
	}
	
}

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