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

ラインアートフレームワーク。


ゲームとして使えそうな部品をフレームワークとして気の向くまま作ってこうかと。
という訳(?)でラインアートです。
ゲームと全然関係ない!
まったく意味が分からないのは書いた本人も一緒なので気にしない。


動画ジグソーパズル Dyna-Mo
http://end.skr.jp/dynamo.html


ここの DoJa ソースの LineArt クラスをポーティングしました(するなよぉ)。
ベタなまんまでは四角形が1つしか表示できない構造だったので
マルチプルインスタンス化(最近こればっか(笑)。)。


マルチプルインスタンス化と内部隠蔽してて気づいたのはソースが素直でなくなるということ。
フレームワークとして)外から便利に扱えるようにするということは
便利を提供するため中ではその分の手間を請け負う必要があるんだと。


サイトへの公開は帰ってからしますので先にソースだけでも
(ビルドには libmy/draw.h, libmy/draw.c, libmy/Random.h, libmy/Random.c を含みます。)。


Lineart framework


main.c

/*---------------------------------------------------------------------------------
	
	Lineart framework test
	
	version 0.01
	May 10, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds.h>
#include "libmy/draw.h"
#include "fwmy/Lineart.h"


#define SQUARE_NUM 7


void drawLineart(Lineart lineart, int square_num, u16 *buf);


//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	int counter = 0;
	Lineart lineart1, lineart2;
	
	//initVideoMode
	videoSetMode(MODE_FB0);
	
	//initVRAMBank
	vramSetBankA(VRAM_A_LCD);
	
	lineart1 = Lineart_Create(0, SQUARE_NUM, 3);
	lineart2 = Lineart_Create(1, SQUARE_NUM, 3);
	
	while(1){
		
		swiWaitForVBlank();
		
		if(counter++ % 6 == 0){
			
			Lineart_Update(lineart1);
			Lineart_Update(lineart2);
			
			drawCls(VRAM_A, 0);
			drawLineart(lineart1, SQUARE_NUM, VRAM_A);
			drawLineart(lineart2, SQUARE_NUM, VRAM_A);
			
		}
		
		if(keysDown() & KEY_A){
			lineart1 = Lineart_Destroy(lineart1);
			lineart2 = Lineart_Destroy(lineart2);
			return 0;
		}
		
	}
	
}

//---------------------------------------------------------------------------------
void drawLineart(Lineart lineart, int square_num, u16 *buf) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return;
	
	int i;
	for(i = 0;i < square_num;i++){
		
		drawLine(buf, Lineart_GetLineX1(lineart, 0, i), Lineart_GetLineY1(lineart, 0, i), Lineart_GetLineX2(lineart, 0, i), Lineart_GetLineY2(lineart, 0, i), Lineart_GetLineColor(lineart));
		drawLine(buf, Lineart_GetLineX1(lineart, 1, i), Lineart_GetLineY1(lineart, 1, i), Lineart_GetLineX2(lineart, 1, i), Lineart_GetLineY2(lineart, 1, i), Lineart_GetLineColor(lineart));
		drawLine(buf, Lineart_GetLineX1(lineart, 2, i), Lineart_GetLineY1(lineart, 2, i), Lineart_GetLineX2(lineart, 2, i), Lineart_GetLineY2(lineart, 2, i), Lineart_GetLineColor(lineart));
		drawLine(buf, Lineart_GetLineX1(lineart, 3, i), Lineart_GetLineY1(lineart, 3, i), Lineart_GetLineX2(lineart, 3, i), Lineart_GetLineY2(lineart, 3, i), Lineart_GetLineColor(lineart));
		
	}
	
}

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


fwmy/Lineart.h

/*---------------------------------------------------------------------------------
	
	Lineart framework header
	
	version 0.01
	May 10, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _LINEART_H_
#define _LINEART_H_


typedef struct _Lineart *Lineart;


#ifdef __cplusplus
extern "C" {
#endif


Lineart Lineart_Create(int seed, int square_num, int brightness);
Lineart Lineart_Destroy(Lineart lineart);
void Lineart_Update(Lineart lineart);
void Lineart_SetLineBrightness(Lineart lineart, int brightness);
int Lineart_GetLineBrightness(Lineart lineart);
int Lineart_GetLineColor(Lineart lineart);
int Lineart_GetLineX1(Lineart lineart, int line_num, int square_num);
int Lineart_GetLineY1(Lineart lineart, int line_num, int square_num);
int Lineart_GetLineX2(Lineart lineart, int line_num, int square_num);
int Lineart_GetLineY2(Lineart lineart, int line_num, int square_num);


#ifdef __cplusplus
}
#endif

#endif	// _LINEART_H_


fwmy/Lineart.c

/*---------------------------------------------------------------------------------
	
	Lineart framework routine
	
	version 0.01
	May 10, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdlib.h>
#include <nds/arm9/background.h>
#include "../libmy/Random.h"
#include "Lineart.h"


#define LINE_NUM 4


typedef struct _Lineart{
	
	int *x;
	int *y;
	
	int vx[LINE_NUM];
	int vy[LINE_NUM];
	
	int square_num;
	
	int color;
	int counter;
	int brightness;
	
	Random random;
	
}_Lineart;


//---------------------------------------------------------------------------------
Lineart Lineart_Create(int seed, int square_num, int brightness) {
//---------------------------------------------------------------------------------
	
	int i, j, x, y;
	Lineart lineart;
	
	lineart = (Lineart)malloc(sizeof(_Lineart));
	
	lineart->square_num = square_num;
	lineart->x = malloc(sizeof(int) * (LINE_NUM * lineart->square_num));
	lineart->y = malloc(sizeof(int) * (LINE_NUM * lineart->square_num));
	
	lineart->random = Random_Create(GENERATOR_LCGS);
	Random_SetSeed(lineart->random, seed);
	
	lineart->counter = Random_GetValue(lineart->random);
	
	for(i = 0;i < LINE_NUM;i++){
		
		x = Random_GetValue(lineart->random) % SCREEN_WIDTH;
		y = Random_GetValue(lineart->random) % SCREEN_HEIGHT;
		
		for(j = 0;j < square_num;j++){
			
			lineart->x[i * lineart->square_num + j] = x;
			lineart->y[i * lineart->square_num + j] = y;
			
		}
		
		lineart->vx[i] = Random_GetValue(lineart->random) % 15 - 7;
		lineart->vy[i] = Random_GetValue(lineart->random) % 15 - 7;
		
	}
	
	lineart->brightness = brightness;
	
	return lineart;
	
}

//---------------------------------------------------------------------------------
Lineart Lineart_Destroy(Lineart lineart) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return NULL;
	
	lineart->random = Random_Destroy(lineart->random);
	free(lineart->y);
	free(lineart->x);
	free(lineart);
	
	return NULL;
	
}

//---------------------------------------------------------------------------------
void Lineart_Update(Lineart lineart) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return;
	
	int i, j, r, g, b;
	
	for(i = 0;i < LINE_NUM;i++){
		
		for(j = lineart->square_num - 1;j >= 1;j--){
			
			lineart->x[i * lineart->square_num + j] = lineart->x[i * lineart->square_num + j - 1];
			lineart->y[i * lineart->square_num + j] = lineart->y[i * lineart->square_num + j - 1];
			
		}
		
	}
	
	for(i = 0;i < LINE_NUM;i++){
		
		lineart->x[i * lineart->square_num + 0] += lineart->vx[i];
		lineart->y[i * lineart->square_num + 0] += lineart->vy[i];
		
		if(lineart->x[i * lineart->square_num + 0] <= 0){
			
			lineart->x[i * lineart->square_num + 0] = 0;
			lineart->vx[i] = Random_GetValue(lineart->random) % 7 + 1;
			
		}else if(lineart->x[i * lineart->square_num + 0] >= SCREEN_WIDTH - 1){
			
			lineart->x[i * lineart->square_num + 0] = SCREEN_WIDTH - 1;
			lineart->vx[i] = -(Random_GetValue(lineart->random) % 7 + 1);
			
		}
		
		if(lineart->y[i * lineart->square_num + 0] <= 0){
			
			lineart->y[i * lineart->square_num + 0] = 0;
			lineart->vy[i] = Random_GetValue(lineart->random) % 7 + 1;
			
		}else if(lineart->y[i * lineart->square_num + 0] >= SCREEN_HEIGHT - 1){
			
			lineart->y[i * lineart->square_num + 0] = SCREEN_HEIGHT - 1;
			lineart->vy[i] = -(Random_GetValue(lineart->random) % 7 + 1);
			
		}
		
	}
	
	r = 160 + (((((lineart->counter / 77) % 8) & 0x01) != 0) ? 64 : 0) + (lineart->counter + 0) % 32;
	g = 160 + (((((lineart->counter / 77) % 8) & 0x02) != 0) ? 64 : 0) + (lineart->counter + 11) % 32;
	b = 160 + (((((lineart->counter / 77) % 8) & 0x04) != 0) ? 64 : 0) + (lineart->counter + 21) % 32;
	
	lineart->color = RGB8(r * (lineart->brightness + 4) / 8, g * (lineart->brightness + 4) / 8, b * (lineart->brightness + 4) / 8);
	
	lineart->counter++;
	
}

//---------------------------------------------------------------------------------
void Lineart_SetLineBrightness(Lineart lineart, int brightness) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return;
	
	lineart->brightness = brightness & 0x03;
	
}

//---------------------------------------------------------------------------------
int Lineart_GetLineBrightness(Lineart lineart) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return 0;
	
	return lineart->brightness;
	
}

//---------------------------------------------------------------------------------
int Lineart_GetLineColor(Lineart lineart) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return 0;
	
	return lineart->color;
	
}

//---------------------------------------------------------------------------------
int Lineart_GetLineX1(Lineart lineart, int line_num, int square_num) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return 0;
	if(line_num < 0 || line_num >= LINE_NUM) return 0;
	if(square_num < 0 || square_num >= lineart->square_num) return 0;
	
	return lineart->x[line_num * lineart->square_num + square_num];
	
}

//---------------------------------------------------------------------------------
int Lineart_GetLineY1(Lineart lineart, int line_num, int square_num) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return 0;
	if(line_num < 0 || line_num >= LINE_NUM) return 0;
	if(square_num < 0 || square_num >= lineart->square_num) return 0;
	
	return lineart->y[line_num * lineart->square_num + square_num];
	
}

//---------------------------------------------------------------------------------
int Lineart_GetLineX2(Lineart lineart, int line_num, int square_num) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return 0;
	if(line_num < 0 || line_num >= LINE_NUM) return 0;
	if(square_num < 0 || square_num >= lineart->square_num) return 0;
	
	return lineart->x[(++line_num & 0x03) * lineart->square_num + square_num];
	
}

//---------------------------------------------------------------------------------
int Lineart_GetLineY2(Lineart lineart, int line_num, int square_num) {
//---------------------------------------------------------------------------------
	
	if(lineart == NULL) return 0;
	if(line_num < 0 || line_num >= LINE_NUM) return 0;
	if(square_num < 0 || square_num >= lineart->square_num) return 0;
	
	return lineart->y[(++line_num & 0x03) * lineart->square_num + square_num];
	
}

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