TNP資料室 @ ウィキ

文字を描画する方法

最終更新:

kokeiro

- view
だれでも歓迎! 編集
文字列を描画する際に使用する主な関数は

int DrawString( int x , int y , char *String , int Color ) ;
int DrawFormatString( int x , int y , int Color , char *FormatString , ... ) ;

の2つです。





int DrawString( int x , int y , char *String , int Color ) ;

 第1引数、第2引数は描画開始の左上の座標です。DXライブラリでの座標は、左上を(0,0)とし、右方向がxの正方向、下方向がyの正方向となります。慣れるまではわかりにくいかもしれません。第3引数は描画したい文字列です。第4引数は描画に使用するカラーコードを指定します。

 もし、左上に、緑色で「Hello World!」と表示したい場合は以下のように関数を呼び出します。

int green = GetColor( 0 , 255, 0 );
DrawString( 0 , 0 , "Hello World!" , green );



 C言語でprintf関数のように書式付の文字列を描画したいときは後者のDrawFormatString関数を呼び出します。

int DrawFormatString( int x , int y , int Color , char *FormatString , ... ) ;

第1引数、第2引数は描画開始の左上の座標です。先ほどの関数と異なり、カラーコードが第3引数となります。第4引数が描画する文字列です。

int型の値、double型の値をそれぞれ表示する
int white = GetColor( 255 , 255 , 255 );
int murasaki = GetColor( 128 , 0 , 128 );
int SampleInt = 249;
double SampleDouble = 3.14;
DrawFormatString( 0 ,  0 , white , "SampleIntの値は「%d」です。" , SampleInt );
DrawFormatString( 0 , 20 , murasaki , "SampleDoubleの値は「%f」です。" , SampleDouble );


まとめのサンプルソース

#include"DxLib.h"

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
	ChangeWindowMode(TRUE);
	if(DxLib_Init()==-1)
		return 1;
	SetDrawScreen(DX_SCREEN_BACK);

	while(1){
		if( ProcessMessage() != 0 || CheckHitKey( KEY_INPUT_ESCAPE ) != 0 )
			break;
		ClearDrawScreen();

		int green = GetColor( 0 , 255, 0 );
		DrawString( 0 , 0 , "Helllo World!" , green );


		int white = GetColor( 255 , 255 , 255 );
		int murasaki = GetColor( 128 , 0 , 128 );
		int SampleInt = 249;
		double SampleDouble = 3.14;
		DrawFormatString( 0 , 20 , white , "SampleIntの値は「%d」です。" , SampleInt );
		DrawFormatString( 0 , 40 , murasaki , "SampleDoubleの値は「%f」です。" , SampleDouble );

		ScreenFlip();
	}
	DxLib_End();
	return 0;
}



このほかにも、フォントを変更して文字列を描画する関数が用意されています。詳しくは本家関数リファレンスの文字列関連の項目を見てください。

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

目安箱バナー