⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vectorfont.c

📁 compiled for AtMega32 The source, to compile with AVR-G
💻 C
字号:
//
// Title        : vectorfont - part of scopetris
// Author       : Lars Pontoppidan 
// Date         : Aug. 2007
// Version      : 0.1
//
//
// DESCRIPTION:
// ------------
// vectorfont implements simple vector rendering of numbers using drawlib.
// 
// The digits consists of vertical, horizontal and 45 deg. lines.
//
// vfont_number(..) will print an unsigned short with selectable font size
//
//
//
// DISCLAIMER:
// The author is in no way responsible for any problems or damage caused by
// using this code. Use at your own risk.
//
// LICENSE:
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//



#include <avr/io.h>
#include <avr/pgmspace.h>
#include "drawlib.h"


//               dx+1  dy+1
#define VF_N    (1 | (2 << 4))
#define VF_NE   (2 | (2 << 4))
#define VF_E    (2 | (1 << 4))
#define VF_SE   (2 | (0 << 4))
#define VF_S    (1 | (0 << 4))
#define VF_SW   (0 | (0 << 4))
#define VF_W    (0 | (1 << 4))
#define VF_NW   (0 | (2 << 4))
#define VF_ED   (1 | (1 << 4))    // dx=0, dy=0 meaning end of stroke

#define MAX_STROKES 10

struct letter_struct {
  unsigned char start_x;
  unsigned char start_y;
  unsigned char stroke[MAX_STROKES];  // the stroke defines dx and dy
};

const struct letter_struct vf_number[10] PROGMEM = {
  {1, 3 , 
    {VF_SE,VF_S, VF_SW,VF_NW,VF_N, VF_NE,VF_ED,VF_ED,VF_ED,VF_ED}},  // 0
  {0, 3 ,                                                       
    {VF_S, VF_S, VF_S, VF_ED,VF_ED,VF_ED,VF_ED,VF_ED,VF_ED,VF_ED}},  // 1
  {0, 3 ,                                                       
    {VF_E ,VF_SE,VF_SW,VF_SW,VF_E ,VF_E ,VF_ED,VF_ED,VF_ED,VF_ED}},  // 2
  {0, 3 ,                                                       
    {VF_E ,VF_E, VF_SW,VF_SE,VF_SW,VF_W ,VF_ED,VF_ED,VF_ED,VF_ED}},  // 3
  {2, 1 ,                                                       
    {VF_W, VF_W ,VF_N, VF_NE,VF_S ,VF_S ,VF_S ,VF_ED,VF_ED,VF_ED}},  // 4
  {2, 3 ,                                                       
    {VF_W ,VF_W ,VF_S ,VF_E ,VF_SE,VF_SW,VF_W ,VF_ED,VF_ED,VF_ED}},  // 5
  {2, 3 ,                                                       
    {VF_W ,VF_SW,VF_S ,VF_SE,VF_NE,VF_NW,VF_W ,VF_ED,VF_ED,VF_ED}},  // 6
  {0, 3 ,                                                       
    {VF_E ,VF_E ,VF_SW,VF_S ,VF_S ,VF_ED,VF_ED,VF_ED,VF_ED,VF_ED}},  // 7
  {0, 3 , 
    {VF_E ,VF_E, VF_SW,VF_SW,VF_SE,VF_NE,VF_NW,VF_NW,VF_ED,VF_ED}},  // 8
  {2, 1 ,                                                       
    {VF_W ,VF_NW,VF_NE,VF_SE,VF_S ,VF_SW,VF_W ,VF_ED,VF_ED,VF_ED}}   // 9
};


void inline vfont_print(unsigned char x, unsigned char y, unsigned char size, const struct letter_struct *l)
{
  unsigned char s;
  const unsigned char *p;
  
  draw_set_xy(x + pgm_read_byte_near(&(l->start_x))*size, 
              y + pgm_read_byte_near(&(l->start_y))*size);
              
  p = &(l->stroke[0]);
  
  while(1) {
    s = pgm_read_byte_near(p);
    if (s == VF_ED) break;
    draw_pen_dxdy((s&0x0F)-1, (s>>4)-1, size);
    p++;
  }
  
}


unsigned short tenths_tab[5] = {1,10,100,1000,10000};  

void vfont_number(unsigned char x, unsigned char y, unsigned char size, unsigned short value)
{
  unsigned char p;
  unsigned char n;
  
  // Skip starting zeros
  p = 4;
  while (value < tenths_tab[p]) {
    if (--p == 0) break;
  }
  
  do {
    n = 0;
    while (value >= tenths_tab[p]) {
      n++;
      value -= tenths_tab[p];
    }
    
    vfont_print(x, y, size, &(vf_number[n]));
      
    if (n==1)
      x += size;     // number 1 is slimmer than the rest...
    else
      x += size * 3;
      
  } while (p--);
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -