cascii-art

Implementing a glyph patterns in C


I have read this implementation of glyphs in the Expert C Programming by Peter Van Der Linden. He states this method to draw a glyph pattern. This question is strictly restricted to the context of C programming.

static unsigned short stopwatch[] = {
    0x07C6,
    0x1FF7,
    0x383B,
    0x600C,
    0x600C,
    0xC006,
    0xC006,
    0xDF06,
    0xC106,
    0xC106,
    0x610C,
    0x610C,
    0x3838,
    0x1FF0,
    0x07C0,
    0x0000
};

and then define

#define X )*2+1
#define _ )*2
#define s ((((((((((((((((0

for drawing glyphs 16-bits wide.

Then the above array is converted to a glyph pattern of a StopWatch.

How do we draw those glyphs on the screen without using the graphics? Is there any method to draw the glyph patterns of other objects like outline of maps, face of a person roughly without having to plot each of the pixels, and without using the regular C graphics?

Are there any algorithms that were followed?


Solution

  • There is just a few lines of code missing:

    int main()
    {
        int i,j;
        for ( i=0;stopwatch[i];i++ )
        {
            for ( j=1<<16;j;j>>=1 ) printf("%c",stopwatch[i]&j?'o':' ');
            printf("\n");
        }
    }
    

    voila, stopwatch:

          ooooo   oo 
        ooooooooo ooo
       ooo     ooo oo
      oo         oo  
      oo         oo  
     oo           oo 
     oo           oo 
     oo ooooo     oo 
     oo     o     oo 
     oo     o     oo 
      oo    o    oo  
      oo    o    oo  
       ooo     ooo   
        ooooooooo    
          ooooo      
    

    The define statements are a shorthand to arrive at the magic values in that list:

    $ gcc -E -x c -
    #define X )*2+1
    #define _ )*2
    #define s ((((((((((((((((0 
    
    s _ _ _ _ _ _ X X X X _ _ _ _ _ _
    
    // will be preprocessed to:
    ^D
    
    ((((((((((((((((0 )*2 )*2 )*2 )*2 )*2 )*2 )*2+1 )*2+1 )*2+1 )*2+1 )*2 )*2 )*2 )*2 )*2 )*2
    

    That last blurb is an expression which leads to some value (in this case 960 or 0x03c0) you could use in that "stopwatch" list.