Skip to content

Instantly share code, notes, and snippets.

@Myoga1012
Last active January 30, 2023 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Myoga1012/e668ec131ea5b17635b3 to your computer and use it in GitHub Desktop.
Save Myoga1012/e668ec131ea5b17635b3 to your computer and use it in GitHub Desktop.
C++とDXライブラリを使って、俳句(平仮名)を点字風のドットマトリックスで表示するプログラムですよっ!
#pragma once
#include "DxLib.h"
#include <string>
// sourceの値が、targetのリストのいずれかとマッチするかどうかを判別します。
bool AnyOneOfMatch( wchar_t source, std::wstring target ) {
bool ans = false;
std::wstring::iterator titer = target.begin();
while( titer != target.end() ) {
if( source == *titer++ ) {
ans = true; break;
}
}
return ans;
}
#define BRAILLE_OK 0
#define BRAILLE_FAILED -1
// 任意の平仮名の文字を点字風にドットマトリックスで表示します。
class BrailleDotMatrix {
private:
// 平仮名 -> 点字符号
static int DecodeToBraille( wchar_t c, bool braille[] ) {
if( !( c >= L'あ' && c <= L'ん' ) || AnyOneOfMatch( c, L"ぁぃぅぇぉゃゅょ" ) )
return BRAILLE_FAILED;
// 拗音以外では以下のセルは使用しません。
braille[0] = braille[1] = braille[2] = braille[5] = false;
// braille[3]は濁音、braille[4]は半濁音で使用します。
braille[3] = AnyOneOfMatch( c, L"がぎぐげござじずぜぞだぢづでどばびぶべぼ" );
braille[4] = AnyOneOfMatch( c, L"ぱぴぷぺぽ" );
// braille[6-8]は基本的に母音によってオンオフを切り替えます。
braille[6] = !AnyOneOfMatch( c, L"おこそとのもやゆよろわゐゑをんごぞどぼぽっ" );
braille[7] = !AnyOneOfMatch( c, L"あいかきさしたちなにはひまみらりわゐゑをんがぎざじだぢばびぱぴっ" );
braille[8] = !AnyOneOfMatch( c, L"あうかくさすたつなぬはふまむやゆよらるわをんがぐざずだづばぶぱぷ" );
// braille[9-11]は基本的に子音によってオンオフを切り替えます。
braille[9] = AnyOneOfMatch( c, L"さしすせそたちつてとまみむめもよらりるれろゑをんざじずぜぞだぢづでど" );
braille[10] = !AnyOneOfMatch( c, L"あいうえおかきくけこさしすせそらりるれろがぎぐげござじずぜぞっ" );
braille[11] = !AnyOneOfMatch( c, L"あいうえおたちつてとなにぬねのやよらりるれろわゐゑをんだぢづでどっ" );
return BRAILLE_OK;
}
static int DecodeBrailleForContracted( wchar_t c1, wchar_t c2, bool braille[] ) {
if( !AnyOneOfMatch( c1, L"きしちにみりぎじぢびぴ" ) || !AnyOneOfMatch( c2, L"ゃゅょ" ) )
return BRAILLE_FAILED;
// 拗音では以下のセルは使用しません。
braille[0] = braille[2] = braille[5] = false;
// 拗音では以下のセルを必ず使用します。
braille[1] = true;
// braille[3]はc1が濁音、braille[4]はc1が半濁音で使用します。
braille[3] = AnyOneOfMatch( c1, L"ぎじぢび" );
braille[4] = AnyOneOfMatch( c1, L"ぴ" );
// braille[6-8]は基本的にc2のゃゅょによってオンオフを切り替えます。
braille[6] = AnyOneOfMatch( c2, L"ゃゅ" );
braille[7] = AnyOneOfMatch( c2, L"ゅょ" );
braille[8] = AnyOneOfMatch( c2, L"ょ" );
// braille[9-11]は基本的にc1の文字によってオンオフを切り替えます。
braille[9] = AnyOneOfMatch( c1, L"しじちぢみり" );
braille[10] = !AnyOneOfMatch( c1, L"きぎしじり" );
braille[11] = !AnyOneOfMatch( c1, L"ちぢにり" );
return BRAILLE_OK;
}
// ドットマトリックス用データを作成します。
static void ConvertBraille( const bool l[], bool d[][4] ) {
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 2; j++ ) {
d[i][j] = l[i * 2 + j];
d[i][j + 2] = l[i * 2 + j + 6];
}
}
public:
static const int DotSize = 20; // ドットのサイズ
static const int SegSizeX = DotSize * 4; // 1セグメント当たりのサイズ
static const int SegSizeY = DotSize * 3;
static const int SegMargin = 5; // セグメントのマージン
// 任意の平仮名1文字を点字風のドットマトリックスで表示します。
static void DrawBraille( int x, int y, wchar_t c, int fillColor, int edgeColor = 0, int xoff = 0, int yoff = 0 ) {
bool braille12[12] = { false };
if( DecodeToBraille( c, braille12 ) == BRAILLE_FAILED ) {
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 4; j++ )
DrawBlank( x, y, i, j, xoff, yoff );
return;
}
DrawBraille1( x, y, braille12, fillColor, edgeColor, xoff, yoff );
}
// 任意の拗音を点字風のドットマトリックスで表示します。
static void DrawBrailleForContracted( int x, int y, wchar_t c1, wchar_t c2, int fillColor, int edgeColor = 0, int xoff = 0, int yoff = 0 ) {
bool braille12[12] = { false };
if( DecodeBrailleForContracted( c1, c2, braille12 ) == BRAILLE_FAILED ) {
for( int i = 0; i < 3; i++ )
for( int j = 0; j < 4; j++ )
DrawBlank( x, y, i, j, xoff, yoff );
return;
}
DrawBraille1( x, y, braille12, fillColor, edgeColor, xoff, yoff );
}
private:
// ドットマトリックス表示
static void DrawBraille1( int x, int y, const bool braille12[], int fillColor, int edgeColor, int xoff, int yoff ) {
bool braille12dmt[3][4] = { false };
ConvertBraille( braille12, braille12dmt );
// ドットマトリックスを表示します。
for( int i = 0; i < 3; i++ ) {
for( int j = 0; j < 4; j++ ) {
if( braille12dmt[i][j] ) {
DrawCircle(
DotSize * j + ( DotSize / 2 ) + x * ( SegSizeX + SegMargin * 2 ) + SegMargin + 1 + xoff, // x1
DotSize * i + ( DotSize / 2 ) + y * ( SegSizeY + SegMargin * 2 ) + SegMargin + 1 + yoff, // y1
( DotSize - 4 ) / 2, edgeColor, true
);
DrawCircle(
DotSize * j + ( DotSize / 2 ) + x * ( SegSizeX + SegMargin * 2 ) + SegMargin + 1 + xoff, // x1
DotSize * i + ( DotSize / 2 ) + y * ( SegSizeY + SegMargin * 2 ) + SegMargin + 1 + yoff, // y1
( DotSize - 6 ) / 2, fillColor, true
);
}
else DrawBlank( x, y, i, j, xoff, yoff );
}
}
}
// 消灯状態のドットマトリックスを表示します。
static void DrawBlank( int x, int y, int i, int j, int xoff, int yoff ) {
DrawCircle(
DotSize * j + ( DotSize / 2 ) + x * ( SegSizeX + SegMargin * 2 ) + SegMargin + 1 + xoff, // x1
DotSize * i + ( DotSize / 2 ) + y * ( SegSizeY + SegMargin * 2 ) + SegMargin + 1 + yoff, // y1
( DotSize - 4 ) / 2, 0x101010, true
);
}
};
class StringBraille {
public:
// 任意の平仮名の文字列を点字で表示します。
static void DrawStringBraille( int x, int y, std::wstring s, int fillColor, int edgeColor = 0, int xoff = 0, int yoff = 0 ) {
std::wstring::iterator si = s.begin();
while( si != s.end() ) {
// 文字列の末尾の時
if( ++si == s.end() ) {
BrailleDotMatrix::DrawBraille( x++, y, *( --si ), fillColor, edgeColor, xoff, yoff );
}
else {
si--;
// 拗音の時( 最終的にイテレーターを2つ進めます )
if( AnyOneOfMatch( *( ++si ), L"ゃゅょ" ) ) {
wchar_t c1 = *( --si );
BrailleDotMatrix::DrawBrailleForContracted( x++, y, c1, *( ++si ), fillColor, edgeColor, xoff, yoff );
}
// 拗音以外の時
else {
BrailleDotMatrix::DrawBraille( x++, y, *( --si ), fillColor, edgeColor, xoff, yoff );
}
}
si++;
}
}
};
// Auther : Myoga Screw-bright
// Twitter : https://twitter.com/Myoga1012
#include "DxLib.h"
#include "Braille.h"
using namespace std;
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
SetMainWindowText( "初桜折しも今日はよき日なり" ); // ウインドウタイトルの変更
ChangeWindowMode( TRUE ); // ウィンドウモード
SetGraphMode( 800, 240, 16 ); // ウィンドウサイズの変更
SetOutApplicationLogValidFlag( false ); // ログを出力しない
// DXライブラリの初期化
if( DxLib_Init() == -1 ) return -1;
// 画面の描画先をリアバッファーに設定する場合、このコメントアウトを解除します。
SetDrawScreen( DX_SCREEN_BACK );
// ここから開始
// 俳句(平仮名)を点字風のドットマトリックスで表示します。
wstring haiku[3] = { L"はつざくら", L"おりしもきょうは", L"よきひなり" };
for( int i = 0; i < 3; i++ ) {
StringBraille::DrawStringBraille( 0, i, haiku[i], 0x00FFFF, 0x2464FF, 10, 10 );
}
ScreenFlip();
WaitKey();
// DXライブラリ終了処理
DxLib_End();
return 0;
}
// BrailleHaiku.cpp(C++ with DXライブラリ)
// Copyright (c) 2014-2015 Myoga-TN.net All Rights Reserved.
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment