经过一番研究,找到了底层的贴图函数之后,加入了一个写中文字符串的函数,% Q5 ^/ w( q& \, V3 N: }
目前已经可以往屏幕上写中文字符串了。
l) U/ X. Y$ n9 s$ j" [6 l. S8 g& h3 K
汉化抓图" r8 k% t9 Y* k
; l0 s0 O# {" I2 X( y! P+ X- ]3 r; ~% a1 K$ m% k, D9 V
代码
$ A8 ~& o6 G" U. U2 w- H# Kbackends\graphics\opengl\texture.cpp
' h3 ]5 L0 R8 Z- @. E- void Surface::copyRectToTexture(uint x, uint y, uint w, uint h, const void *srcPtr, uint srcPitch) {
) F5 a2 r. s8 ^% d/ S% ^7 o' | - copyRectToTextureOld(x, y, w , h , srcPtr, srcPitch);, o6 `+ Z0 R% h3 L% d7 p
- //add by shane007
1 c, k, ^8 \- J - drawStringToSurface();
8 m7 x9 X) c2 ~, h4 P% f4 } - }
. g9 @0 f" |' |7 {# E - # V, d0 R! ?% u; ]) Q
- //add by shane0079 R8 _7 W# q' K( J
- void Surface::drawStringToSurface() {
. p. n3 C/ }; a% H' N' t3 @! Y" x - 9 c3 S5 C5 c9 J K% G+ G4 e B
- Graphics::Surface *dstSurf = getSurface();2 }3 \9 H( t3 E& g
- // 在Surface上显示字符串
0 {1 Z( \4 q f' {' m0 W - int x = 100;& i6 ]3 d" N: O& C# u) V
- int y = 300;4 z, }& O5 x0 E( R
- int charWidth = 16;: u4 k, @! [. J* G
- int charHeight = 16;
) ~3 s, b0 t, n' l( O - int color = 0x0000ff; //0x00BBGGRR
6 Y7 W: C9 ^! V! ?( \' ^2 F" A - const uint pitch = dstSurf->pitch;6 T! S) U$ a, l2 P
- const uint bytesPerPixel = dstSurf->format.bytesPerPixel;" i/ F' {7 Q* |9 [4 H
0 |/ o# Z5 D6 N0 O) `0 g0 ?1 n U- unsigned char *dst;
$ z- u1 p2 C& h! @% i - const char *str = "中文测试";* F& I2 |! o. B& ^ }4 k- P. }
+ b. ^# K3 \5 B0 e7 W- for (int i = 0; str[i] != '\0'; i = i+2) {
: B& B, f6 S& R4 x2 |6 u% g9 x$ S - int ch = (unsigned char)str[i];+ a& Q9 y) E+ |- l0 t% d! y' F
- int ch2 = (unsigned char)str[i + 1];6 q6 i3 l! J6 N
- int idx = (ch - 0xa1) * 94 + (ch2 - 0xa1);
! }: X* [6 ?6 o - const unsigned char *fontData = hzk16_data + 32 * idx;
) x( J& Z9 r1 _+ P& v - dst = (unsigned char *)dstSurf->getPixels() + y * pitch + (x + (i/2) * charWidth) * bytesPerPixel;
( l; f/ X# I# p6 I0 E; \6 ~+ I" t: U - ( h W' I X3 x
- for (int row = 0; row < charHeight; ++row) {% U4 \( L* _9 E1 ~5 A6 B0 v" P
- for (int col = 0; col < charWidth; ++col) {
- z: y7 m3 X# {8 b* m - unsigned char pixelByte = fontData[row * 2 + col / 8]; // 每行对应2个字节: C0 n& C/ q6 D
- unsigned char pixelBit = (pixelByte >> (7 - (col % 8))) & 0x01; // 提取像素对应的位 ]. ` y8 p( Q' i* _1 g9 C
% @! `9 C7 R) ]" \/ S0 Y9 h- if (pixelBit == 1) {. |& ^' C! r3 K
- // 设置像素颜色
5 F. ~+ I& S9 p$ ~, j - uint32 *pixel = (uint32 *)dst;3 h {; t: n E5 H. L2 G. g
- *pixel = color;- k0 e5 o' p9 s; u: C( I+ w4 w8 \
- }
# o8 G, d$ O& @7 R9 B5 | - dst += bytesPerPixel;
* h7 z9 C* n6 d0 g- c J- `' Y - }
2 T+ @+ A) x1 \ - dst += pitch - charWidth * bytesPerPixel; // 跳到下一行的起始位置
$ F* V6 f2 X+ d, k - }0 x6 Y1 b# w9 i& }3 e& ^
- }5 j. X2 Z$ s7 i* W/ \& T
- }
复制代码 $ M: N& }. p+ C
6 P" A ]" I. |- m |