经过一番研究,找到了底层的贴图函数之后,加入了一个写中文字符串的函数,
. M2 u: X$ @0 \6 p1 k0 ?目前已经可以往屏幕上写中文字符串了。: K S% r. i' j
' d# P+ R9 Y- G _3 f
汉化抓图% \: f' ~ a# a% V: u# J1 T
: C2 \& x- k: n* G& b4 h" x
9 a* E& N' O" F0 t$ x* l" f+ N代码
# d* R& R! @& _: vbackends\graphics\opengl\texture.cpp" |5 i1 ]7 l9 |8 V2 A( }
- void Surface::copyRectToTexture(uint x, uint y, uint w, uint h, const void *srcPtr, uint srcPitch) {
& R( m2 M' p) \: l; o9 S. p+ W - copyRectToTextureOld(x, y, w , h , srcPtr, srcPitch);
- @: b4 U# [" D" ~9 w5 C - //add by shane0071 q6 c( S) h1 A- y# ]: a
- drawStringToSurface();0 Z8 v2 e4 D( g+ e: _. F
- }
( ?" L6 o+ g( h2 z/ o6 L9 w
6 f: u9 \! A& T- //add by shane007
3 ~ T& S0 Q: X t3 ]/ u - void Surface::drawStringToSurface() {: \5 l( z4 N. a, ^ t
7 \# F- Y- q' ^) N* w- Graphics::Surface *dstSurf = getSurface();/ e# a& q0 l3 x9 }& E& r# Z
- // 在Surface上显示字符串
3 ]9 K: w+ W# F. z - int x = 100;
; a, W" @) _6 C8 R! l - int y = 300;
* A8 J" O6 M$ J3 @: X5 a% c - int charWidth = 16;
8 t' v5 `; w& q3 {6 r9 R" F - int charHeight = 16;# i7 M6 M: N7 r' G
- int color = 0x0000ff; //0x00BBGGRR
( b8 Y+ j" R4 Q! n+ r( Z - const uint pitch = dstSurf->pitch;
4 I6 k- Z* b; |* ~ - const uint bytesPerPixel = dstSurf->format.bytesPerPixel;
6 G# c1 {% h/ ~
& B0 J/ n: n: E. M8 f m- unsigned char *dst;3 l7 C: i8 W% }) a4 w9 H
- const char *str = "中文测试"; S7 P- R6 ^6 ^$ O% C
- 5 D) k( h3 I1 Q3 ~0 {, {+ l9 `
- for (int i = 0; str[i] != '\0'; i = i+2) {
. E T* z/ o% U' H$ H. N. a - int ch = (unsigned char)str[i];# q0 J6 e, E" Z8 S' c6 [
- int ch2 = (unsigned char)str[i + 1];0 t) Z0 B5 Y1 h# y1 a
- int idx = (ch - 0xa1) * 94 + (ch2 - 0xa1);
" s5 R! J; n: Y/ v - const unsigned char *fontData = hzk16_data + 32 * idx;
1 U7 C2 M( s" c3 o( G( z) {! W - dst = (unsigned char *)dstSurf->getPixels() + y * pitch + (x + (i/2) * charWidth) * bytesPerPixel;
( s7 a2 g) L) D2 c7 ]- S
% b) }+ z' I4 [- N- for (int row = 0; row < charHeight; ++row) {: H; c" K, R J
- for (int col = 0; col < charWidth; ++col) {$ F4 |+ p/ x" D% E6 ?* }4 r1 N6 k
- unsigned char pixelByte = fontData[row * 2 + col / 8]; // 每行对应2个字节
R7 F( H# a8 Q% U; ? - unsigned char pixelBit = (pixelByte >> (7 - (col % 8))) & 0x01; // 提取像素对应的位
7 J. H: x; z9 T# W$ s2 U - 0 l$ ~1 L7 t M+ x
- if (pixelBit == 1) {& n1 V" t( [% ]! f
- // 设置像素颜色7 p8 T. O1 L: X8 @# c% P
- uint32 *pixel = (uint32 *)dst;7 P" E3 s9 i, L* [! ]
- *pixel = color;! g) |* ?. _7 z- c* s
- }
' U" l, a5 ?. e; p5 }3 l6 ` - dst += bytesPerPixel;# ~/ O8 @9 Y6 S1 Z8 S, u/ j
- } u# g, Q6 {7 Q$ ]' D8 ]4 _: l" w4 S
- dst += pitch - charWidth * bytesPerPixel; // 跳到下一行的起始位置! D$ ~ R6 S1 y& `
- }
, ? Q/ _+ {' v - }, R' O4 z6 H' P# C
- }
复制代码 9 P0 S1 T. q( J% @" B7 F$ D' }$ Q
( a, x, l% b# x0 A' P
|