#5
by かずま » 4年前
DxLib でフォントのサイズを変更したければ、
CreateFontToHandle で、サイズの異なるフォントを用意しておき、
DrawFormatStringToHandle で、文字列を表示すればよいでしょう。
ちょっと作ってみました。
コード:
#include "DxLib.h"
#include <map>
using namespace std;
char *message[3][2] = {
{
"あいうえお\\s[24]あいうえお\\s",
"\\s[24]\\c[255,0,0]あいうえお\\s\\c"
}, {
"\\s[36]かきくけこ\\s\\c[0,0,255]ABCDEFG\\c",
"\\c[0,128,128]\\s[24]さしすせそ\\s\\c"
}, {
"1234567\\s[24]89\\s",
"abcdefg\\c[0,255,0]hijklmnopqr\\c[128,0,128]stuvwxyz\\c"
}
};
int isLeading(char c) { return (c ^ 0x20) - 0xa1 < 60u; }
int createFont(int size)
{
return CreateFontToHandle("MS ゴシック", size, 2,
DX_FONTTYPE_ANTIALIASING);
}
class Text {
int fh; // font handle
int width;
int color;
map<int, int> fonts;
public:
Text() : color(0), width(8) {
fonts[16] = createFont(16);
fonts[24] = createFont(24);
fonts[36] = createFont(36);
fh = fonts[16];
}
void flush(int x, int y, int h, const char *s0, const char *s) {
if (s > s0) {
y += h / 2 - width;
DrawFormatStringToHandle(x, y, color, fh, "%.*s", s - s0, s0);
}
}
void setFont(int size) {
if (size == 0) size = 16;
auto it = fonts.find(size);
if (it != fonts.end()) {
fh = it->second;
width = size / 2;
}
}
void drawString(int x, int y, int h, const char *s) {
const char *s0 = s;
int xpos0 = x, xpos1 = x;
while (*s) {
if (*s == '\\') {
int n = 0, size, r, g, b;
if (s[1] == 's') {
if (sscanf(s+2, "[%d]%n", &size, &n) != 1)
size = 16;
flush(xpos0, y, h, s0, s);
s0 = s += n + 2, xpos0 = xpos1;
setFont(size);
}
else if (s[1] == 'c') {
if (sscanf(s+2, "[%d,%d,%d]%n", &r, &g, &b, &n) != 3)
r = g = b = 0;
flush(xpos0, y, h, s0, s);
s0 = s += n + 2, xpos0 = xpos1;
color = GetColor(r, g, b);
}
else
s++, xpos1 += width;
}
else {
if (isLeading(*s)) s++, xpos1 += width;
s++, xpos1 += width;
}
}
flush(xpos0, y, h, s0, s);
}
};
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
SetBackgroundColor(255, 255, 255);
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen(DX_SCREEN_BACK);
Text text;
int count = 0, id = 0, x = 50, y = 30, height = 36;
while (!ScreenFlip() && !ProcessMessage() && !ClearDrawScreen()) {
if (++count == 60) {
count = 0;
if (++id == 3) id = 0;
}
for (int i = 0; i < 2; i++)
text.drawString(x, y + height * i, height, message[id][i]);
}
DxLib_End();
return 0;
}
フォントのサイズは偶数でなければいけません。
プロポーショナルフォントも使えません。
文字コートは Shift-JIS であることを仮定しています。
分からないことは質問してください。
DxLib でフォントのサイズを変更したければ、
CreateFontToHandle で、サイズの異なるフォントを用意しておき、
DrawFormatStringToHandle で、文字列を表示すればよいでしょう。
ちょっと作ってみました。
[code]
#include "DxLib.h"
#include <map>
using namespace std;
char *message[3][2] = {
{
"あいうえお\\s[24]あいうえお\\s",
"\\s[24]\\c[255,0,0]あいうえお\\s\\c"
}, {
"\\s[36]かきくけこ\\s\\c[0,0,255]ABCDEFG\\c",
"\\c[0,128,128]\\s[24]さしすせそ\\s\\c"
}, {
"1234567\\s[24]89\\s",
"abcdefg\\c[0,255,0]hijklmnopqr\\c[128,0,128]stuvwxyz\\c"
}
};
int isLeading(char c) { return (c ^ 0x20) - 0xa1 < 60u; }
int createFont(int size)
{
return CreateFontToHandle("MS ゴシック", size, 2,
DX_FONTTYPE_ANTIALIASING);
}
class Text {
int fh; // font handle
int width;
int color;
map<int, int> fonts;
public:
Text() : color(0), width(8) {
fonts[16] = createFont(16);
fonts[24] = createFont(24);
fonts[36] = createFont(36);
fh = fonts[16];
}
void flush(int x, int y, int h, const char *s0, const char *s) {
if (s > s0) {
y += h / 2 - width;
DrawFormatStringToHandle(x, y, color, fh, "%.*s", s - s0, s0);
}
}
void setFont(int size) {
if (size == 0) size = 16;
auto it = fonts.find(size);
if (it != fonts.end()) {
fh = it->second;
width = size / 2;
}
}
void drawString(int x, int y, int h, const char *s) {
const char *s0 = s;
int xpos0 = x, xpos1 = x;
while (*s) {
if (*s == '\\') {
int n = 0, size, r, g, b;
if (s[1] == 's') {
if (sscanf(s+2, "[%d]%n", &size, &n) != 1)
size = 16;
flush(xpos0, y, h, s0, s);
s0 = s += n + 2, xpos0 = xpos1;
setFont(size);
}
else if (s[1] == 'c') {
if (sscanf(s+2, "[%d,%d,%d]%n", &r, &g, &b, &n) != 3)
r = g = b = 0;
flush(xpos0, y, h, s0, s);
s0 = s += n + 2, xpos0 = xpos1;
color = GetColor(r, g, b);
}
else
s++, xpos1 += width;
}
else {
if (isLeading(*s)) s++, xpos1 += width;
s++, xpos1 += width;
}
}
flush(xpos0, y, h, s0, s);
}
};
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
SetBackgroundColor(255, 255, 255);
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen(DX_SCREEN_BACK);
Text text;
int count = 0, id = 0, x = 50, y = 30, height = 36;
while (!ScreenFlip() && !ProcessMessage() && !ClearDrawScreen()) {
if (++count == 60) {
count = 0;
if (++id == 3) id = 0;
}
for (int i = 0; i < 2; i++)
text.drawString(x, y + height * i, height, message[id][i]);
}
DxLib_End();
return 0;
}
[/code]
フォントのサイズは偶数でなければいけません。
プロポーショナルフォントも使えません。
文字コートは Shift-JIS であることを仮定しています。
分からないことは質問してください。