Listing. 1: 画像で数字ならべるよ
"theMain.cpp"
//省略
 
namespace {
	const std::wstring TITLE = L"Sample Game";
	const int WIN_WIDTH = 1024;
	const int WIN_HEIGHT = 768;
	const int SCORE = 5260;
	const int SCOREX = 200;
	const int SCOREY = 10;
	const int STRW = 80;//1文字の実際の幅
	const int NUMW = 96; //切り出す文字サイズx
	const int NUMH = 125;//切り出す文字サイズy
}
 
struct Point
{
	int x, y;
};
struct Rect
{
	Point p;
	int w, h;
};
 
 
void InitDxLib()
{
	ChangeWindowMode(true);
	SetWindowSizeChangeEnableFlag(false, false);
	SetMainWindowText(TITLE.c_str());
 
	SetGraphMode(WIN_WIDTH, WIN_HEIGHT, 32);
	SetWindowSizeExtendRate(1.0);
	//SetBackgroundColor(255, 250, 205);
	//SetBackgroundColor(0, 0, 0);
	SetBackgroundColor(222,184,135);
 
	// DXライブラリ初期化処理
	if (DxLib_Init() == -1)
	{
		DxLib_End();
	}
 
	SetDrawScreen(DX_SCREEN_BACK);
}
 
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
	InitDxLib();
	// メインループ
	int hNumberImage = -1;
	hNumberImage = LoadGraph(L"numbers2024.png");
 
	int dt = 0;//経過フレームタイム
	int sec = 0;//経過秒
	//whileの中身はきっと60fps(ぐらい)でうごいてるよね
	while (ProcessMessage() == 0 && ClearDrawScreen() == 0)
	{
		// ここに処理を書く
		dt++;
		if (dt > 60)
		{
			sec++;
			dt = 0;
		}
		//std::string scoreString = std::to_string(SCORE);
		std::string scoreString = std::to_string(sec);
		scoreString.insert(0, 8 - scoreString.length(), '0');
 
		for (int i = 0; i < 8; i++) {
			int scoreDigit = scoreString[i] - '0';
 
			DrawRectGraph(SCOREX + STRW * i, SCOREY,
				          scoreDigit*NUMW, 0, NUMW, NUMH, 
				          hNumberImage, TRUE, FALSE);
		}
		ScreenFlip();
		if (CheckHitKey(KEY_INPUT_ESCAPE) == 1) { break; }
	}
	DxLib_End();             // DXライブラリ使用の終了処理
	return 0;                // ソフトの終了 
}
Listing. 2: 画像で数字ならべるよ
"theMain.cpp"
//省略
#include <string>
 
 
namespace {
	const std::string TITLE = "Sample Game";
	const int WIN_WIDTH = 1024;
	const int WIN_HEIGHT = 768;
	const int SCORE = 5260;
	const int SCOREX = 200;
	const int SCOREY = 10;
	const int STRW = 80;//1文字の実際の幅
	const int NUMW = 96; //切り出す文字サイズx
	const int NUMH = 125;//切り出す文字サイズy
}
 
struct Point
{
	int x, y;
};
struct Rect
{
	Point p;
	int w, h;
};
 
 
void InitDxLib()
{
	ChangeWindowMode(true);
	SetWindowSizeChangeEnableFlag(false, false);
	SetMainWindowText(TITLE.c_str());
 
	SetGraphMode(WIN_WIDTH, WIN_HEIGHT, 32);
	SetWindowSizeExtendRate(1.0);
	SetBackgroundColor(222,184,135);
 
	// DXライブラリ初期化処理
	if (DxLib_Init() == -1)
	{
		DxLib_End();
	}
 
	SetDrawScreen(DX_SCREEN_BACK);
}
 
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
	InitDxLib();
	// メインループ
 
	int hNumberImage = -1;
	hNumberImage = LoadGraph("numbers2024.png");
 
	int dt = 0;//経過フレームタイム
	int sec = 0;//経過秒
	//whileの中身はきっと60fps(ぐらい)でうごいてるよね
	while (ProcessMessage() == 0 && ClearDrawScreen() == 0)
	{
		// ここに処理を書く
		dt++;
		if (dt > 60)
		{
			sec++;
			dt = 0;
		}
		//std::string scoreString = std::to_string(SCORE);
		std::string scoreString = std::to_string(sec); //int -> string
		scoreString.insert(0, 8 - scoreString.length(), '0'); //stringを0埋め
		//char numStr[9];
		//sprintf(numStr, "%08d", sec);
		//左のけた=>scoreString[0]側のほうから数字を取り出して、0~9のどれかを判別
		for (int i = 0; i < 8; i++) {
			int scoreDigit = scoreString[i] - '0'; //数字から、0~9の数値を作り出す
			//int scoreDigit = numStr[i] - '0';
			//細かい表示位置位置を計算
			DrawRectGraph(SCOREX + STRW * i, SCOREY,
				          scoreDigit*NUMW, 0, NUMW, NUMH, 
				          hNumberImage, TRUE, FALSE);
		}
                //FPS制御のウェイトかけるところとかは自分で書いてみてね。WaitTimer(16);とか書いてんのかな?
		ScreenFlip();
		if (CheckHitKey(KEY_INPUT_ESCAPE) == 1) { break; }
	}
 
	DxLib_End();             // DXライブラリ使用の終了処理
 
	return 0;                // ソフトの終了 
}
  • game-engineer/classes/2024/game-creation-1/number-display.txt
  • 最終更新: 16カ月前
  • by root