Go Back   BWHacks > Development > Reverse Engineering > Code Snippets and Tutorials

Code Snippets and Tutorials The place for open source releases, great information, and tutorials written by other members.

Reply
 
LinkBack Thread Tools

Old 05-20-2008, 02:05 AM   #1 (permalink)
Perma
rol 3905h, 8
Senior Member
Administrator

Saint
 
Perma's Avatar
 
Join Date: Jul 2004
Location: Canada
Posts: 5,414
Perma has disabled reputation
Default [StarCraft] Creating a Bitmap Font (v1.15.2)

You may have heard me mention this topic if you've watched my video demonstrating some of the interface innovations I've been making with StarCraft. The idea behind it is simple.

You can draw any font you like to the game screen, provided you are willing to take the time to define your font. Making a monospace font is easier, so that each letter takes up the same amount of pixels. I originally wrote this code as a proof of concept to counteract some anti-maphack implementations that could read text from client-side buffers to detect hacks.

The functions listed below are what're used to draw the text. There is a front-end function, which I will demonstrate, that will convert a string you pass to it into the bitmap font.


Font Drawing Functions
Code:
Font_DrawCharacter proc         cbuffer:DWORD,x:DWORD,y:DWORD,clr:BYTE
   ;//Draws the individual character maps.

        pushad
        mov cl, 88
        mov ch, 0
        xor edi, edi
        mov esi, y
        .while (cl != ch)
               mov ebx, cbuffer
               inc ebx
               xor edx, edx
               mov dl, ch
               add ebx, edx
               mov al, byte ptr [ebx]
               .if (al != 0)
                   mov edx, x
                   add edx, edi
                   pushad
                   .if (al == 1)
                        invoke DrawBox, edx, esi, 1, 1, clr
                   .elseif (al == 2)
                        invoke DrawBox, edx, esi, 1, 1, 0
                   .endif
                   popad
               .endif
               inc edi
               .if (edi == 8)
                   xor edi, edi
                   add esi, 1
               .endif
               add ch, 1
        .endw
        popad
        ret

Font_DrawCharacter endp


Font_ConvertString proc         strchar:BYTE,x:DWORD,y:DWORD,clr:BYTE
   ;//Converts a text byte to the appropriate font map.

        mov al, strchar
        mov ebx, offset char_Space
        check:
        mov cl, byte ptr [ebx]
        .if (al == cl)
            invoke Font_DrawCharacter, ebx, x, y, clr
        .else
            .if (cl != 0)
                add ebx, 89
                jmp check
            .else
                invoke Font_DrawCharacter, addr char_ERROR, x, y, clr
            .endif
        .endif
        ret

Font_ConvertString endp


Font_TextOut proc               tbuffer:DWORD,x:DWORD,y:DWORD,clr:BYTE
   ;//Organizes the character maps into lines.

        LOCAL colorb:BYTE

        pushad
        mov eax, tbuffer
        mov cl, clr
        mov colorb, cl
        mov ecx, x
        mov edi, y
        xor ebx, ebx
        .while (byte ptr [eax] != 0)
               .if (byte ptr [eax] > 19)
                   mov bl, byte ptr [eax]
                   pushad
                   invoke Font_ConvertString, bl, ecx, edi, colorb
                   popad
                   add ecx, 8
               .else
                   .if (byte ptr [eax] == 1) || (byte ptr [eax] == 2)
                       mov colorb, 83
                   .elseif (byte ptr [eax] == 3)
                       mov colorb, 51
                   .elseif (byte ptr [eax] == 4)
                       mov colorb, 255
                   .elseif (byte ptr [eax] == 5)
                       mov colorb, 85
                   .elseif (byte ptr [eax] == 6)
                       mov colorb, 111
                   .elseif (byte ptr [eax] == 7)
                       mov colorb, 117
                   .elseif (byte ptr [eax] == 14)
                       mov colorb, 53
                   .elseif (byte ptr [eax] == 15)
                       mov colorb, 158
                   .elseif (byte ptr [eax] == 16)
                       mov colorb, 163
                   .elseif (byte ptr [eax] == 17)
                       mov colorb, 31
                   .elseif (byte ptr [eax] == 18) || (byte ptr [eax] == 19)
                       mov colorb, 83
                   .endif
               .endif
               inc eax
        .endw
        popad
        ret

Font_TextOut endp
I also included color parsing, so that you can use the current StarCraft color codes to change the color of your text. When the function picks out a color code, it will automatically switch the color of the pixels that it is drawing. You can also select a default color when you call the function. Here is an example of calling the function:

Code:
invoke Font_TextOut, CTEXT("I'm some text!"), x-coordinate, y-coordinate, color

Defining Your Font

The tricky part about using bitmap fonts is that you need a way for the computer to know what shape the characters are. Since the idea is to not use any text characters to represent this, I used a series of numbers.

Code:
0 - Transparent
1 - Solid color (the current font color)
2 - Black shadow
You can create blocks of data with a definition byte, like this:

Code:
char_LA         db "a"
                db 0,0,0,0,0,0,0,0
                db 0,0,0,0,0,0,0,0
                db 0,1,1,1,1,0,0,0
                db 1,2,2,2,2,1,2,0
                db 0,1,1,1,1,1,2,0
                db 1,2,2,2,2,1,2,0
                db 1,2,0,0,1,1,2,0
                db 2,1,1,1,2,1,1,0
                db 0,0,2,2,0,2,2,2
                db 0,0,0,0,0,0,0,0
                db 0,0,0,0,0,0,0,0
The above definition is for the lowercase letter A. This process can be quite time consuming and frustrating, as it's difficult to see what the letter will turn out like until you've completed it - and even then, correcting errors can be irritating. I've attached the full character set to my own font, which is based on Courier New.

You will require my DrawBox function to use this code.
Attached Files
File Type: txt Font.txt (23.8 KB, 18 views)
Perma 15 0|\|11|\|3 |\|0\/\/   Reply With Quote
Advertisement
 
Advertisement
Advertisement Sponsored links


Old 05-20-2008, 02:26 AM   #2 (permalink)
ulliklliwi

Disciple
 
ulliklliwi's Avatar
 
Join Date: May 2007
Location: The Code Cave after the JMP Gate
Posts: 545
ulliklliwi has a spectacular aura about
Send a message via MSN to ulliklliwi
Default

starcraft uses directdraw, why just hook the DD Surfaces, then u can draw anything to the screen at anytime. But them again, in masm it will take sometime . . . to programming the createDI bitmap and etc . . . but anyway good job doing it old school + drawing use front pixel by pixel is alot quick . . i remember i had to do that for D3D8/9
ulliklliwi 15 0FF11|\|3   Reply With Quote

Old 05-20-2008, 02:44 AM   #3 (permalink)
Perma
rol 3905h, 8
Senior Member
Administrator

Saint
 
Perma's Avatar
 
Join Date: Jul 2004
Location: Canada
Posts: 5,414
Perma has disabled reputation
Default

I prefer to reinvent the wheel and create my own drawing functions, not to rely on the DirectX libraries to do all of the heavy lifting for me. And, like you mentioned, DirectX programming is quite messy in Masm.
Perma 15 0|\|11|\|3 |\|0\/\/   Reply With Quote

Old 05-20-2008, 05:10 PM   #4 (permalink)
Totte_ch

Deviant
 
Totte_ch's Avatar
 
Join Date: May 2007
Location: Sweden
Posts: 108
Totte_ch is on a distinguished road
Default

Very nice, Perma!
Can someone translate this to C++? I do understand a lot of code, but I don't know how to make the character bitmaps in C++... - make a new structure/class?
Totte_ch 15 0FF11|\|3   Reply With Quote

Old 05-25-2008, 01:24 PM   #5 (permalink)
Sheppard
Banned

Deviant
 
Join Date: Oct 2005
Location: www.w3jsp.com
Posts: 49
Sheppard will become famous soon enough
Default

Very nice, my son.
Sheppard 15 0FF11|\|3   Reply With Quote
Sponsored links
Advertisement
 
Advertisement
Advertisement

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[StarCraft] Transparent Pane Drawing (v1.15.2) Perma Code Snippets and Tutorials 39 07-02-2008 03:47 PM


All times are GMT. The time now is 12:52 AM.


vBulletin style developed by Transverse Styles

Powered by vBulletin Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright © 2004-2008 BWHacksAd Management by RedTyger