戻る

画面表示

TinyScreenクラスがあるのでそれを使って画面を制御する。
画面を65536色モードに設定して10x10のキャラクタを画面の真ん中に表示するサンプルプログラムです。
96*64*2バイトの画面の大きさと同じscreen_bufferを作成してその中に絵を描きTinyScreen::writeBufferで画面に転送している。
16ビットRGBのバイトオーダーはビッグエンディアンなので注意が必要。
TinyArcadeはリトルエンディアンなのでメモリに格納されるときに1バイト目と2バイト目が入れ替わるためデータはあらかじめ1バイト目と2バイト目を入れ替えてある。

サンプルプログラム

#include <TinyScreen.h>

#define SCREEN_WIDTH  96
#define SCREEN_HEIGHT 64
#define BUFFER_SIZE   (SCREEN_WIDTH * SCREEN_HEIGHT)
#define BUFFER_BYTES  (BUFFER_SIZE * 2)
#define COLOR16(r,g,b) ((((g<<3)&0xE)|(r>>3))<<8)+(b&0xF8)|(g>>5) // GGGRRRRRBBBBBGGG

TinyScreen tiny_screen = TinyScreen(TinyScreenPlus);
unsigned short screen_buffer[BUFFER_SIZE];

// Character image
const unsigned short Hiyoko_1[10 * 10] =
{
	0x0000, 0x0000, 0x0000, 0x9B06, 0x9B06, 0x9B06, 0x0000, 0x0000, 0x0000, 0x0000,
	0x0000, 0x0000, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x0000, 0x0000, 0x0000,
	0x0000, 0x9B06, 0x0358, 0x0358, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x0000, 0x0000,
	0x0000, 0x9B06, 0xFFFF, 0x0358, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x0000, 0x0000,
	0x0000, 0x9B06, 0x0358, 0x0358, 0x9B06, 0x9F46, 0x9F46, 0x9F46, 0x9F46, 0x9F46,
	0x9B03, 0x9B03, 0x9B03, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x9F46,
	0x0000, 0x0000, 0x0000, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x9B06, 0x9F46, 0x0000,
	0x0000, 0x0000, 0x0000, 0x0000, 0x9B06, 0x9B06, 0x9B06, 0x9F46, 0x0000, 0x0000,
	0x0000, 0x0000, 0x0000, 0x0000, 0x9B03, 0x0000, 0x9B03, 0x0000, 0x0000, 0x0000,
	0x0000, 0x0000, 0x0000, 0x0000, 0x9B03, 0x0000, 0x9B03, 0x0000, 0x0000, 0x0000
};

void setup()
{
	tiny_screen.begin();
	// 65536 color mode
	tiny_screen.setBitDepth(TSBitDepth16);
	// Screen brightness
	tiny_screen.setBrightness(7);
}

// Draw sprite no clip
void DrawSprite(const unsigned short* buffer, int x, int y, int width, int height, int color_key)
{
	int scan_x;
	int scan_y;
	int source_address = 0;
	int destination_address = SCREEN_WIDTH * y + x;
	for(scan_y = 0; scan_y < height; ++ scan_y)
	{
		for(scan_x = 0; scan_x < width; ++ scan_x)
		{
			unsigned short color = buffer[source_address + scan_x];
			if(static_cast<int>(color) != color_key)
			{
				screen_buffer[destination_address + scan_x] = color;
			}
		}
		source_address += width;
		destination_address += SCREEN_WIDTH;
	}
}

void loop()
{
	// Clear buffer
	for(int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; ++ i)
	{
		screen_buffer[i] = COLOR16(30, 30, 100);
	}
	// Draw sprite
	DrawSprite(Hiyoko_1, 43, 27, 10, 10, 0);
	// buffer to screen
	tiny_screen.goTo(0, 0);
	tiny_screen.setX(0, SCREEN_WIDTH - 1);
	tiny_screen.setY(0, SCREEN_HEIGHT - 1);
	tiny_screen.startData();
	tiny_screen.writeBuffer(reinterpret_cast<unsigned char*>(screen_buffer), BUFFER_BYTES);
	tiny_screen.endTransfer();
}

実行結果

ScreenSample3.jpg

サンプルプログラムのダウンロード


添付ファイル: fileScreenSample3.zip 1035件 [詳細] fileScreenSample3.jpg 1022件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-06-17 (日) 02:34:05 (2134d)