ハートに16連射。(12)


ショットウォッチの基本モジュールです。
このモジュールを用いて連打計測や時計表示なんかが出来たりなんかして。
フォントは過去に公開してるアタリ風フォントリソースも使ってます。


NDS Homebrew 用に利用可能なフォントリソース。(2)
http://d.hatena.ne.jp/dumbo001/20090324/1237936437


Bg.h

/*---------------------------------------------------------------------------------
	
	shtwatch
	bg module header
	
	version 0.01
	Feb 03, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _BG_H_
#define _BG_H_


#include <nds/ndstypes.h>


#ifdef __cplusplus
extern "C" {
#endif


void Bg_Init(void);
void Bg_DrawConsClear(void);
void Bg_DrawConsValue(int x, int y, int value);
void Bg_DrawConsString(int x, int y, char *string);
int Bg_GetConsScrollX(void);
void Bg_SetConsScrollX(int scrollX);
void Bg_SetPalette(u8 pal_num, u8 red, u8 green, u8 blue, bool main_disp);


#ifdef __cplusplus
}
#endif

#endif	//_BG_H_


Bg.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	bg module routine
	
	version 0.01
	Feb 03, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <nds/ndstypes.h>
#include <nds/arm9/background.h>
#include <nds/arm9/video.h>
#include <nds/arm9/console.h>
#include "Bg.h"
#include "font.h"


typedef struct{
	
	//console state
	PrintConsole cons;
	ConsoleFont font;
	int cons_scrollx;

}BgStatus;

static BgStatus st_bg;


//---------------------------------------------------------------------------------
void Bg_Init(void){
//---------------------------------------------------------------------------------
	
	//initStruct
	memset(&st_bg, 0, sizeof(st_bg));
	
	//initBgole
	st_bg.cons = *consoleDemoInit();
	
	//initFont
	st_bg.font.gfx = (u16*)fontTiles;
	st_bg.font.bpp = 4;
	st_bg.font.numChars = 128;
	st_bg.font.convertSingleColor = true;
	
	consoleSetFont(&st_bg.cons, &st_bg.font);
	
}

//---------------------------------------------------------------------------------
void Bg_DrawConsClear(void){
//---------------------------------------------------------------------------------
	
	printf("\x01b[2J");
	
}

//---------------------------------------------------------------------------------
void Bg_DrawConsValue(int x, int y, int value){
//---------------------------------------------------------------------------------
	
	printf("\x01b[%d;%dH%4d", y, x, value);
	
}

//---------------------------------------------------------------------------------
void Bg_DrawConsString(int x, int y, char *string){
//---------------------------------------------------------------------------------
	
	printf("\x01b[%d;%dH%s", y, x, string);
	
}

//---------------------------------------------------------------------------------
int Bg_GetConsScrollX(void){
//---------------------------------------------------------------------------------
	
	return st_bg.cons_scrollx;
	
}

//---------------------------------------------------------------------------------
void Bg_SetConsScrollX(int scrollX){
//---------------------------------------------------------------------------------
	
	st_bg.cons_scrollx = scrollX;
	bgSetScroll(st_bg.cons.bgId, st_bg.cons_scrollx, 0);
	
	bgUpdate();
	
}

//---------------------------------------------------------------------------------
void Bg_SetPalette(u8 pal_num, u8 red, u8 green, u8 blue, bool main_disp){
//---------------------------------------------------------------------------------
	
	if(main_disp){
		BG_PALETTE[pal_num] = RGB8(red, green, blue);
	}else{
		BG_PALETTE_SUB[pal_num] = RGB8(red, green, blue);
	}
	
}

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

Spr.h

/*---------------------------------------------------------------------------------
	
	shtwatch
	spr module header
	
	version 0.01
	Feb 02, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _SPR_H_
#define _SPR_H_


typedef enum{
	SEG_SECOND, 
	SEG_MINUTE, 
	SEG_HOUR
}SEG_SELECT;


#ifdef __cplusplus
extern "C" {
#endif


void Spr_Init(void);
void Spr_Clear(void);
void Spr_Update(void);
void Spr_SetColon(u8 colon);
void Spr_SetCursor(u8 cursor);
void Spr_SetNumber(SEG_SELECT seg_select, int number, bool zero_suppress);
void Spr_SetSegment(u8 seg_low, u8 seg_high, SEG_SELECT seg_select);


#ifdef __cplusplus
}
#endif

#endif	//_SPR_H_


Spr.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	spr module routine
	
	version 0.01
	Feb 02, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <nds/ndstypes.h>
#include "Spr.h"


//|SEG|Dp|g |f |e |d |c |b |a |
//|bit|07|06|05|04|03|02|01|00|

typedef enum{
	SEG_A  = 0x01, 
	SEG_B  = 0x02, 
	SEG_C  = 0x04, 
	SEG_D  = 0x08, 
	SEG_E  = 0x10, 
	SEG_F  = 0x20, 
	SEG_G  = 0x40, 
	SEG_DP = 0x80
}SEG_VAL;

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


typedef struct{
	u8 colon;
	u8 cursor;
	u8 seg_bit[6];
}SprStatus;

static SprStatus st_spr;


void spr_DrawColon(void);
void spr_DrawCursor(void);
void spr_DrawSegment(void);


//---------------------------------------------------------------------------------
void Spr_Init(void){
//---------------------------------------------------------------------------------
	
	//ToDo
	
	Spr_Clear();
	
}

//---------------------------------------------------------------------------------
void Spr_Clear(void){
//---------------------------------------------------------------------------------
	
	memset(&st_spr, 0, sizeof(st_spr));
	
}

//---------------------------------------------------------------------------------
void Spr_Update(void){
//---------------------------------------------------------------------------------
	
	spr_DrawSegment();
	spr_DrawColon();
	spr_DrawCursor();
	
}

//---------------------------------------------------------------------------------
void Spr_SetColon(u8 colon){
//---------------------------------------------------------------------------------
	
	st_spr.colon = colon & 0x03;
	
}

//---------------------------------------------------------------------------------
void Spr_SetCursor(u8 cursor){
//---------------------------------------------------------------------------------
	
	st_spr.cursor = cursor & 0x1f;
	
}

//---------------------------------------------------------------------------------
void Spr_SetNumber(SEG_SELECT seg_select, int number, bool zero_suppress){
//---------------------------------------------------------------------------------
	
	//7 Seg LED number pattern
	const static u8 seg_bit[] = {
		0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 
		0x00
	};
	
	int temp;
	u8 low, high;
	
	
	//round to value
	if(number > 99){
		temp = 99;
	}else if(number < 0){
		temp = 0;
	}else{
		temp = number;
	}
	
	//rank division
	low  = temp % 10;
	high = temp / 10;
	
	//zero suppress
	if(high == 0 && zero_suppress == false){
		high = 10;
	}
	
	//set segment bits
	Spr_SetSegment(seg_bit[low], seg_bit[high], seg_select);
	
}

//---------------------------------------------------------------------------------
void Spr_SetSegment(u8 seg_low, u8 seg_high, SEG_SELECT seg_select){
//---------------------------------------------------------------------------------
	
	switch(seg_select){
		
	case SEG_SECOND:
		st_spr.seg_bit[0] = seg_low;
		st_spr.seg_bit[1] = seg_high;
		break;
		
	case SEG_MINUTE:
		st_spr.seg_bit[2] = seg_low;
		st_spr.seg_bit[3] = seg_high;
		break;
		
	case SEG_HOUR:
		st_spr.seg_bit[4] = seg_low;
		st_spr.seg_bit[5] = seg_high;
		break;
		
	}
	
}

//---------------------------------------------------------------------------------
void spr_DrawColon(void){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;%dH%d", 13, 9, IS_VAL(st_spr.colon & 0x01));
	printf("\x1b[%d;%dH%d", 15, 9, IS_VAL(st_spr.colon & 0x02));
	
}

//---------------------------------------------------------------------------------
void spr_DrawCursor(void){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;%dH%d", 20, 1, IS_VAL(st_spr.cursor & 0x01));
	printf("\x1b[%d;%dH%d", 20, 3, IS_VAL(st_spr.cursor & 0x02));
	printf("\x1b[%d;%dH%d", 20, 5, IS_VAL(st_spr.cursor & 0x04));
	printf("\x1b[%d;%dH%d", 20, 7, IS_VAL(st_spr.cursor & 0x08));
	printf("\x1b[%d;%dH%d", 20, 9, IS_VAL(st_spr.cursor & 0x10));
	
}

//---------------------------------------------------------------------------------
void spr_DrawSegment(void){
//---------------------------------------------------------------------------------
	
	//hour high
	printf("\x1b[%d;%dH%d", 12, 2, IS_VAL(st_spr.seg_bit[5] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1, IS_VAL(st_spr.seg_bit[5] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3, IS_VAL(st_spr.seg_bit[5] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2, IS_VAL(st_spr.seg_bit[5] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1, IS_VAL(st_spr.seg_bit[5] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3, IS_VAL(st_spr.seg_bit[5] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2, IS_VAL(st_spr.seg_bit[5] & SEG_D));
	
	//hour low
	printf("\x1b[%d;%dH%d", 12, 2 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_D));
	
	
	//minute high
	printf("\x1b[%d;%dH%d", 12, 2 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_D));
	
	//minute low
	printf("\x1b[%d;%dH%d", 12, 2 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_D));
	
	
	//second high
	printf("\x1b[%d;%dH%d", 12, 2 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_D));
	
	//second low
	printf("\x1b[%d;%dH%d", 12, 2 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_D));
	
}

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


Rtc.h

/*---------------------------------------------------------------------------------
	
	shtwatch
	rtc module header
	
	version 0.01
	Feb 02, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _RTC_H_
#define _RTC_H_


#ifdef __cplusplus
extern "C" {
#endif


int Rtc_GetSecond(void);
int Rtc_GetMinute(void);
int Rtc_GetHour(void);


#ifdef __cplusplus
}
#endif

#endif	//_RTC_H_


Rtc.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	rtc module routine
	
	version 0.01
	Feb 02, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <time.h>
#include "Rtc.h"


struct tm *rtc_GetTime(void);


//---------------------------------------------------------------------------------
int Rtc_GetSecond(void){
//---------------------------------------------------------------------------------
	
	struct tm tm;
	
	tm = *rtc_GetTime();
	
	return tm.tm_sec;
	
}

//---------------------------------------------------------------------------------
int Rtc_GetMinute(void){
//---------------------------------------------------------------------------------
	
	struct tm tm;
	
	tm = *rtc_GetTime();
	
	return tm.tm_min;
	
}

//---------------------------------------------------------------------------------
int Rtc_GetHour(void){
//---------------------------------------------------------------------------------
	
	struct tm tm;
	
	tm = *rtc_GetTime();
	
	return tm.tm_hour;
	
}

//---------------------------------------------------------------------------------
struct tm* rtc_GetTime(void){
//---------------------------------------------------------------------------------
	
	time_t unix_time;
	
	unix_time = time(NULL);
	
	return gmtime((const time_t *)&unix_time);
	
}

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


State.h

/*---------------------------------------------------------------------------------
	
	shtwatch
	state module header
	
	version 0.01
	Feb 03, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _STATE_H_
#define _STATE_H_


#include <nds/ndstypes.h>


#ifdef __cplusplus
extern "C" {
#endif


void State_Init(void);
int State_GetHighScore(void);
void State_SetHighScore(int high_score);
bool State_GetDiceMode(void);
void State_SetDiceMode(bool dice_mode);
bool State_GetSlotMode(void);
void State_SetSlotMode(bool slot_mode);

bool State_GetSoundState(void);
void State_SetSoundState(bool play_sound);


#ifdef __cplusplus
}
#endif

#endif	//_STATE_H_


State.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	state module routine
	
	version 0.01
	Feb 03, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <string.h>
#include "State.h"


typedef struct{
	
	int high_score;
	bool dice_mode;
	bool slot_mode;
	
	bool play_sound;
	
}State;

static State st_state;


//---------------------------------------------------------------------------------
void State_Init(void){
//---------------------------------------------------------------------------------
	
	memset(&st_state, 0, sizeof(st_state));
	
	st_state.dice_mode = false;
	st_state.slot_mode = false;
	
	st_state.play_sound = true;
	
}

//---------------------------------------------------------------------------------
int State_GetHighScore(void){
//---------------------------------------------------------------------------------
	
	return st_state.high_score;
	
}

//---------------------------------------------------------------------------------
void State_SetHighScore(int high_score){
//---------------------------------------------------------------------------------
	
	st_state.high_score = high_score;
	
}

//---------------------------------------------------------------------------------
bool State_GetDiceMode(void){
//---------------------------------------------------------------------------------
	
	return st_state.dice_mode;
	
}

//---------------------------------------------------------------------------------
void State_SetDiceMode(bool dice_mode){
//---------------------------------------------------------------------------------
	
	st_state.dice_mode = dice_mode;
	
}

//---------------------------------------------------------------------------------
bool State_GetSlotMode(void){
//---------------------------------------------------------------------------------
	
	return st_state.slot_mode;
	
}

//---------------------------------------------------------------------------------
void State_SetSlotMode(bool slot_mode){
//---------------------------------------------------------------------------------
	
	st_state.slot_mode = slot_mode;
	
}

//---------------------------------------------------------------------------------
bool State_GetSoundState(void){
//---------------------------------------------------------------------------------
	
	return st_state.play_sound;
	
}

//---------------------------------------------------------------------------------
void State_SetSoundState(bool play_sound){
//---------------------------------------------------------------------------------
	
	st_state.play_sound = play_sound;
	
}

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