经过一番研究,找到了底层的贴图函数之后,加入了一个写中文字符串的函数,
% E- I( W i1 v1 \, R& V目前已经可以往屏幕上写中文字符串了。
1 y* W, x5 [7 o v; k* C0 ^2 G, x: _ f: T" ]
汉化抓图$ u6 z% K a: O4 P' v' P
8 d" P7 W4 ~, T, O7 @1 l; N! `
) O; D6 [2 n7 v& D
代码
% Y' {$ | x& G1 ]$ u( L2 j. jbackends\graphics\opengl\texture.cpp# O0 V" m" A/ W7 | U. ~2 S
- void Surface::copyRectToTexture(uint x, uint y, uint w, uint h, const void *srcPtr, uint srcPitch) {+ Q# D) b9 p& L4 a1 P" E2 y
- copyRectToTextureOld(x, y, w , h , srcPtr, srcPitch);
' e! z1 Y C2 `* V - //add by shane007! r7 Z8 m$ p9 q1 N0 \$ g$ e
- drawStringToSurface();
, ]" Q$ g# u( Q8 b3 l/ N1 ~! @( f - }) F+ G; s& d8 v
- $ N6 ?$ [- o1 h
- //add by shane0074 B7 r: z F7 M: c# H0 g( z5 [ T7 C
- void Surface::drawStringToSurface() {
7 |0 ^% z% J% V5 D
8 h' D, {8 C7 |. a5 L! C' h4 t" {- Graphics::Surface *dstSurf = getSurface();
) ^2 q+ Y7 q6 X7 t- t# z - // 在Surface上显示字符串
) z ~' r( H. v2 A, G: O - int x = 100;4 x" X8 D- Y4 v& i" z3 t
- int y = 300;- M, [' ]$ `! C4 [
- int charWidth = 16;. a: M& Q' g4 B: e" Z: F7 r1 |" c
- int charHeight = 16;
- G6 ?8 h$ ]3 \! M/ M - int color = 0x0000ff; //0x00BBGGRR
# K. y+ ?: J: f" a1 m6 b/ f( J - const uint pitch = dstSurf->pitch;
% j5 g/ d7 H6 _% f# ~! B5 i$ p$ j - const uint bytesPerPixel = dstSurf->format.bytesPerPixel;, C" \* F0 w9 s# [; O% v- g
4 q- G3 d- |4 F, S- unsigned char *dst;9 d2 }% B s% O1 _
- const char *str = "中文测试";
' d2 L' [; Y& b9 O - 5 k+ b5 Q$ D8 H
- for (int i = 0; str[i] != '\0'; i = i+2) {# x/ _# y0 Q' h3 z+ c& w
- int ch = (unsigned char)str[i];
3 M% Z* t7 C4 y& x) F( G6 d - int ch2 = (unsigned char)str[i + 1];+ c! Y% O. v# j; l
- int idx = (ch - 0xa1) * 94 + (ch2 - 0xa1);
# z( D" {! p# Y: _2 h - const unsigned char *fontData = hzk16_data + 32 * idx;% @2 k5 Y( W- X# N0 d4 g$ S
- dst = (unsigned char *)dstSurf->getPixels() + y * pitch + (x + (i/2) * charWidth) * bytesPerPixel;
$ T: f8 T% c) G% F1 a$ J - * _ L4 Q3 Y+ x6 n- `- e Q
- for (int row = 0; row < charHeight; ++row) {
& o+ D7 Q2 O0 y* C/ L9 c - for (int col = 0; col < charWidth; ++col) {1 z+ a, F2 U$ M8 \& L$ M4 _
- unsigned char pixelByte = fontData[row * 2 + col / 8]; // 每行对应2个字节8 U* W" `' u# t
- unsigned char pixelBit = (pixelByte >> (7 - (col % 8))) & 0x01; // 提取像素对应的位
7 w; Q w* Q! H- y |5 b
* p; C( t+ I4 o+ s, V& |( R- if (pixelBit == 1) {3 x3 p) a' T! S8 X2 N7 N& O
- // 设置像素颜色& G s" H1 z2 a" g g
- uint32 *pixel = (uint32 *)dst;
) j- U6 `7 {3 H: J - *pixel = color;. x7 z% G8 q( R, O# K+ P6 }' x8 R* a
- }
5 U. b: D* ~ S3 W9 c- i - dst += bytesPerPixel; ?1 ?: z3 B, _
- }
% K1 o: H& f. j; V8 @9 a - dst += pitch - charWidth * bytesPerPixel; // 跳到下一行的起始位置$ }' B" {2 Y3 T6 O
- }6 H. j* \1 H+ Y% T% r7 G
- }
/ L* }5 b9 R3 } - }
复制代码
, T( Z. Z3 V9 A' @1 Z6 T: }" E" c- c+ [- D: F8 j/ G
|