Custom Characters

I read there's a way to create custom console characters in BASIC. I was wondering
if there's a way to do that in C++.
Like this: http://www.cracked.com/blog/wp-content/uploads/2010/01/spaceinvadersprite.jpg
That's an old hardware trick for EGA/VGA cards.

On modern GUI systems (like Windows), just supply an appropriate font. That's how Microsoft originally did their window decorations and the like -- see the font "Marlett". (Start -> Program Files -> Accessories -> System Tools -> Character Map)

I recommend playing with FontForge
http://fontforge.sourceforge.net/

You will also need the MS Console Reference to manipulate the console Code Page and Font.
http://www.google.com/search?btnI=1&q=msdn+console+reference


On Windows, you could also just draw bitmaps directly on the console's display.
http://www.daniweb.com/code/snippet216431.html

His code uses LoadImage() to read the file. There are issues with that function... so I always roll my own load-bitmap function. Here's a simple Windows-only version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <windows.h>

HBITMAP BMPLoadFromFile( LPCTSTR filename )
  {
  HANDLE           f;
  BITMAPFILEHEADER bmFH;
  DWORD            size;
  HGLOBAL          g;
  LPVOID           p;
  HDC              dc;
  DWORD            count;
  BOOL             ok;
  HBITMAP          result = 0;

  /* Open the bitmap file */
  f = CreateFile(
        filename,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,  /* no security attributes: handle cannot be inherited */
        OPEN_EXISTING,
        FILE_FLAG_SEQUENTIAL_SCAN, /* optimize for how we'll use the file */
        NULL
        );
  if (f == INVALID_HANDLE_VALUE) return 0;

  /* Read and validate the bitmap file header */
  ok = ReadFile( f, (LPVOID)&bmFH, sizeof( BITMAPFILEHEADER ), &count, NULL );
  if (!ok || (count != sizeof( BITMAPFILEHEADER )) || (bmFH.bfType != 0x4D42))
    goto done_file;

  /* Get the size of the bitmap structure */
  size = GetFileSize( f, NULL );
  if (size == 0xFFFFFFFF) goto done_file;
  size -= sizeof( BITMAPFILEHEADER );

  /* And try to allocate and load it */
  g = GlobalAlloc( GHND, size );
  if (g == NULL) goto done_file;

  p = GlobalLock( g );
  if (p == NULL) goto done_global;

  ok = ReadFile( f, p, size, &count, NULL );
  if (!ok || (count != size)) goto done_global;

  /* OK, now we can get down to business and turn our data into an HBITMAP */
  dc = GetDC( NULL );
  if (dc == NULL) goto done_global;

  result = CreateDIBitmap(
             dc,
             (BITMAPINFOHEADER*)p,
             CBM_INIT,
             (LPVOID)((CHAR*)p +bmFH.bfOffBits),
             (BITMAPINFO*)p,
             DIB_RGB_COLORS
             );

  ReleaseDC( NULL, dc );

  done_global: if (g) GlobalFree( g );
  done_file:          CloseHandle( f );

  return result;
  }
It is C, but it will compile as C++ also.

BTW, don't complain about the gotos. If you hate them, get rid of them. Just be sure that your code cleans up after itself... (This is one of the reasons "goto" exists.) There are other ways of doing this, to be sure.

Hope this helps.
Topic archived. No new replies allowed.