经过一番研究,找到了底层的贴图函数之后,加入了一个写中文字符串的函数,
' V B% c: ]9 H目前已经可以往屏幕上写中文字符串了。
9 ^" U! m1 F3 D6 w$ ?
) j7 y7 }7 M* z" F. b. P8 O/ I- d4 k" @汉化抓图
/ [+ V5 s) Q, n& d. t( U2 l' X4 Q& s, a
7 j$ Z; O) I. Q. J$ l" S0 o7 {8 w
代码
" G- @! \. T" U3 P1 cbackends\graphics\opengl\texture.cpp
# h' g6 R* t1 X" B$ i- void Surface::copyRectToTexture(uint x, uint y, uint w, uint h, const void *srcPtr, uint srcPitch) {8 p7 Y) B' f* S1 s- ?% J, d+ i
- copyRectToTextureOld(x, y, w , h , srcPtr, srcPitch);
# q3 D" C, S% @' c6 o - //add by shane007
) v9 s( H n$ t% |% j0 p _0 d8 c - drawStringToSurface();
6 j5 ^: E" a3 Q* ]% K @2 @ - }
( g) l. I! R# s$ n" ] - ; i/ R, A1 B2 D; L+ a" p7 A
- //add by shane007
7 J+ Q* p1 \9 K% k - void Surface::drawStringToSurface() {, H3 j7 W% J1 ~" |# w
- 1 [1 H X* R t+ `6 P" p
- Graphics::Surface *dstSurf = getSurface();
2 E' G1 @" ?, ?( Y5 [! |3 j1 S5 | - // 在Surface上显示字符串6 Z" N3 e6 }9 h" ~
- int x = 100;( f& A5 c( [3 G% g J5 z
- int y = 300;5 }6 ]( l% _; z- g' I, s+ b# i
- int charWidth = 16;
; A% @( U3 l: |* X/ @ v- e5 j - int charHeight = 16;
" p) J/ H8 R) k2 R+ p$ y - int color = 0x0000ff; //0x00BBGGRR
1 D. M0 @9 p+ e - const uint pitch = dstSurf->pitch;
- n9 s1 N8 k3 p7 M" N1 K/ z; b - const uint bytesPerPixel = dstSurf->format.bytesPerPixel;7 E, K1 B. a2 H% B0 o" d6 R
- 4 r: q5 A3 w( r" w; l/ J- l
- unsigned char *dst;* b2 Z# B0 C+ m1 M( C
- const char *str = "中文测试";' R1 n. `2 t$ f
- $ l# f: I% _1 |4 W* w+ Y% h! U
- for (int i = 0; str[i] != '\0'; i = i+2) {
8 l+ j+ \% T4 O& G4 _& A - int ch = (unsigned char)str[i];
3 F" Y/ G( j, L3 z/ z1 Y" j! L) l - int ch2 = (unsigned char)str[i + 1];
: V8 R& p6 K+ K0 n( ?, ^ V. @ - int idx = (ch - 0xa1) * 94 + (ch2 - 0xa1);
7 d" e, }$ Y3 B/ K+ S/ F - const unsigned char *fontData = hzk16_data + 32 * idx;
6 M% A# |0 c& V% L) l - dst = (unsigned char *)dstSurf->getPixels() + y * pitch + (x + (i/2) * charWidth) * bytesPerPixel;/ _9 n; |) ?( B e* d/ \3 |4 g
- ( d5 w6 v$ W& ]8 l" S
- for (int row = 0; row < charHeight; ++row) {. f9 `1 F" [2 {# _$ {
- for (int col = 0; col < charWidth; ++col) {
, E% b/ i1 k- ~! N( d - unsigned char pixelByte = fontData[row * 2 + col / 8]; // 每行对应2个字节
( ^! g, [0 n. Z/ d/ O$ M - unsigned char pixelBit = (pixelByte >> (7 - (col % 8))) & 0x01; // 提取像素对应的位
' `7 s2 {) Z7 C3 Y( f3 I - 3 o4 A) B! l, d3 y2 X0 L( g5 f
- if (pixelBit == 1) {: K( W7 W# {, t
- // 设置像素颜色- N- |2 r4 z% P
- uint32 *pixel = (uint32 *)dst;
5 B+ `2 g4 {7 E% r - *pixel = color;
# p9 _) F' ] Z) _ - }
) D3 a( R, J! Y* \5 y1 Y" q/ M - dst += bytesPerPixel;
x) L6 m* |3 n: P( s& ~: h9 H - }/ N* j& u i0 D/ h
- dst += pitch - charWidth * bytesPerPixel; // 跳到下一行的起始位置
h7 U+ A0 W0 t; b% _ - }
" |' {" n0 F1 k$ A. k# N - }9 D9 M# ^9 S$ }
- }
复制代码
: s Y6 W& p7 m. r0 h {
, f" f1 z' e. f: N( ? |