Facebook
From ddenizozgur, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 133
  1. #ifndef MCANV_H
  2. #define MCANV_H
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8.  
  9. #define STD_PALETTE_BLACK       ((RGBA32){0x00,0x00,0x00,0xFF})
  10. #define STD_PALETTE_BLUE        ((RGBA32){0x00,0x00,0xAA,0xFF})
  11. #define STD_PALETTE_GREEN       ((RGBA32){0x00,0xAA,0x00,0xFF})
  12. #define STD_PALETTE_CYAN        ((RGBA32){0x00,0xAA,0xAA,0xFF})
  13. #define STD_PALETTE_RED         ((RGBA32){0xAA,0x00,0x00,0xFF})
  14. #define STD_PALETTE_PURPLE      ((RGBA32){0xAA,0x00,0xAA,0xFF})
  15. #define STD_PALETTE_BROWN       ((RGBA32){0xAA,0x55,0x00,0xFF})
  16. #define STD_PALETTE_LTGRAY      ((RGBA32){0xAA,0xAA,0xAA,0xFF})
  17. #define STD_PALETTE_DKGRAY      ((RGBA32){0x55,0x55,0x55,0xFF})
  18. #define STD_PALETTE_LTBLUE      ((RGBA32){0x55,0x55,0xFF,0xFF})
  19. #define STD_PALETTE_LTGREEN     ((RGBA32){0x55,0xFF,0x55,0xFF})
  20. #define STD_PALETTE_LTCYAN      ((RGBA32){0x55,0xFF,0xFF,0xFF})
  21. #define STD_PALETTE_LTRED       ((RGBA32){0xFF,0x55,0x55,0xFF})
  22. #define STD_PALETTE_LTPURPLE    ((RGBA32){0xFF,0x55,0xFF,0xFF})
  23. #define STD_PALETTE_YELLOW      ((RGBA32){0xFF,0xFF,0x55,0xFF})
  24. #define STD_PALETTE_WHITE       ((RGBA32){0xFF,0xFF,0xFF,0xFF})
  25.  
  26. #define STD_FONT_WIDTH  8
  27. #define STD_FONT_HEIGHT 8
  28. #define STD_FONT_NUM    256
  29.  
  30. #define BUFF_SIZE 1024
  31.  
  32. static unsigned long std_font[STD_FONT_NUM]={
  33.     0x0000000000000000,0x0000000000000000,
  34.     0x000000FF00000000,0x000000FF00FF0000,//騵
  35.     0x1818181818181818,0x6C6C6C6C6C6C6C6C,//竟
  36.     0x181818F800000000,0x6C6C6CEC0CFC0000,//硭
  37.     0x1818181F00000000,0x6C6C6C6F607F0000,//蕃
  38.     0x000000F818181818,0x000000FC0CEC6C6C,//檀
  39.     0x0000001F18181818,0x0000007F606F6C6C,//椇
  40.     0x0000000000000000,0x0000000000000000,
  41.     0x0000000000000000,0x0000000000000000,
  42.     0x0000000000000000,0x0000000000000000,
  43.     0x0000000000000000,0x0000000000000000,
  44.     0x0000000000000000,0x0000000000000000,
  45.     0x0000000000000000,0x0000000000000000,
  46.     0x0000000000000000,0x0000000000000000,
  47.     0x0000000000000000,0x0000000000000000,
  48.     0x0000000000000000,0x0008000000000000,//
  49.     0x0000000000000000,0x00180018183C3C18,// !
  50.     0x0000000000363636,0x006C6CFE6CFE6C6C,//"#
  51.     0x00187ED07C16FC30,0x0060660C18306606,//$$%
  52.     0x00DC66B61C36361C,0x0000000000181818,//&'
  53.     0x0030180C0C0C1830,0x000C18303030180C,//()
  54.     0x0000187E3C7E1800,0x000018187E181800,//*+
  55.     0x0C18180000000000,0x000000007E000000,//,-
  56.     0x0018180000000000,0x0000060C18306000,//./
  57.     0x003C666E7E76663C,0x007E181818181C18,//01
  58.     0x007E0C183060663C,0x003C66603860663C,//23
  59.     0x0030307E363C3830,0x003C6660603E067E,//45
  60.     0x003C66663E060C38,0x000C0C0C1830607E,//67
  61.     0x003C66663C66663C,0x001C30607C66663C,//89
  62.     0x0018180018180000,0x0C18180018180000,//:;
  63.     0x0030180C060C1830,0x0000007E007E0000,//<=
  64.     0x000C18306030180C,0x001800181830663C,//>?
  65.     0x003C06765676663C,0x006666667E66663C,//@A
  66.     0x003E66663E66663E,0x003C66060606663C,//BC
  67.     0x001E36666666361E,0x007E06063E06067E,//DE
  68.     0x000606063E06067E,0x003C66667606663C,//FG
  69.     0x006666667E666666,0x007E18181818187E,//HI
  70.     0x001C36303030307C,0x0066361E0E1E3666,//JK
  71.     0x007E060606060606,0x00C6C6D6D6FEEEC6,//LM
  72.     0x006666767E6E6666,0x003C66666666663C,//NO
  73.     0x000606063E66663E,0x006C36566666663C,//PQ
  74.     0x006666363E66663E,0x003C66603C06663C,//RS
  75.     0x001818181818187E,0x003C666666666666,//TU
  76.     0x00183C6666666666,0x00C6EEFED6D6C6C6,//VW
  77.     0x0066663C183C6666,0x001818183C666666,//XY
  78.     0x007E060C1830607E,0x003E06060606063E,//Z[
  79.     0x00006030180C0600,0x007C60606060607C,//\]
  80.     0x000000000000663C,0xFFFF000000000000,//^_
  81.     0x000000000030180C,0x007C667C603C0000,//`a
  82.     0x003E6666663E0606,0x003C6606663C0000,//bc
  83.     0x007C6666667C6060,0x003C067E663C0000,//de
  84.     0x000C0C0C3E0C0C38,0x3C607C66667C0000,//fg
  85.     0x00666666663E0606,0x003C1818181C0018,//hi
  86.     0x0E181818181C0018,0x0066361E36660606,//jk
  87.     0x003C18181818181C,0x00C6D6D6FE6C0000,//lm
  88.     0x00666666663E0000,0x003C6666663C0000,//no
  89.     0x06063E66663E0000,0xE0607C66667C0000,//pq
  90.     0x000606066E360000,0x003E603C067C0000,//rs
  91.     0x00380C0C0C3E0C0C,0x007C666666660000,//tu
  92.     0x00183C6666660000,0x006CFED6D6C60000,//vw
  93.     0x00663C183C660000,0x3C607C6666660000,//xy
  94.     0x007E0C18307E0000,0x003018180E181830,//z{
  95.     0x0018181818181818,0x000C18187018180C,//|}
  96.     0x000000000062D68C,0xFFFFFFFFFFFFFFFF,//~
  97.     0x1E30181E3303331E,0x007E333333003300,//€�
  98.     0x001E033F331E0038,0x00FC667C603CC37E,//�
  99.     0x007E333E301E0033,0x007E333E301E0007,//�
  100.     0x007E333E301E0C0C,0x3C603E03033E0000,//�
  101.     0x003C067E663CC37E,0x001E033F331E0033,//�
  102.     0x001E033F331E0007,0x001E0C0C0C0E0033,//�
  103.     0x003C1818181C633E,0x001E0C0C0C0E0007,//�
  104.     0x00333F33331E0C33,0x00333F331E000C0C,//�
  105.     0x003F061E063F0038,0x00FE33FE30FE0000,//�
  106.     0x007333337F33367C,0x001E33331E00331E,//�
  107.     0x001E33331E003300,0x001E33331E000700,//�
  108.     0x007E33333300331E,0x007E333333000700,//�
  109.     0x1F303F3333003300,0x001C3E63633E1C63,//�
  110.     0x001E333333330033,0x18187E03037E1818,//�
  111.     0x003F67060F26361C,0x000C3F0C3F1E3333,//�
  112.     0x70337B332F1B1B0F,0x0E1B18187E18D870,//�
  113.     0x007E333E301E0038,0x001E0C0C0C0E001C,//
  114.     0x001E33331E003800,0x007E333333003800,//╯
  115.     0x003333331F001F00,0x00333B3F3733003F,//丰
  116.     0x00007E007C36363C,0x00007E003C66663C,//戍
  117.     0x001E3303060C000C,0x000003033F000000,//貝
  118.     0x000030303F000000,0xF81973C67C1B3363,//物
  119.     0xC0F9F3E6CF1B3363,0x183C3C1818001800,//洎
  120.     0x0000CC663366CC00,0x00003366CC663300,//悖
  121.     0x1144114411441144,0x55AA55AA55AA55AA,//停
  122.     0xEEBBEEBBEEBBEEBB,0x1818181818181818,//眾
  123.     0x1818181F18181818,0x1818181F181F1818,//斯
  124.     0x6C6C6C6F6C6C6C6C,0x6C6C6C7F00000000,//須
  125.     0x1818181F181F0000,0x6C6C6C6F606F6C6C,//號
  126.     0x6C6C6C6C6C6C6C6C,0x6C6C6C6F607F0000,//獄
  127.     0x0000007F606F6C6C,0x0000007F6C6C6C6C,//播
  128.     0x0000001F181F1818,0x1818181F00000000,//噶
  129.     0x000000F818181818,0x000000FF18181818,//擱
  130.     0x181818FF00000000,0x181818F818181818,//藏
  131.     0x000000FF00000000,0x181818FF18181818,//霰
  132.     0x181818F818F81818,0x6C6C6CEC6C6C6C6C,//
  133.     0x000000FC0CEC6C6C,0x6C6C6CEC0CFC0000,//
  134.     0x000000FF00EF6C6C,0x6C6C6CEF00FF0000,//帊
  135.     0x6C6C6CEC0CEC6C6C,0x000000FF00FF0000,//昅
  136.     0x6C6C6CEF00EF6C6C,0x000000FF00FF1818,//恘
  137.     0x000000FF6C6C6C6C,0x181818FF00FF0000,//倳
  138.     0x6C6C6CFF00000000,0x000000FC6C6C6C6C,//眑
  139.     0x000000F818F81818,0x181818F818F80000,//婭
  140.     0x6C6C6CFC00000000,0x6C6C6CEF6C6C6C6C,//笫
  141.     0x181818FF00FF1818,0x0000001F18181818,//崷
  142.     0x181818F800000000,0xFFFFFFFFFFFFFFFF,//窙
  143.     0xFFFFFFFF00000000,0x0F0F0F0F0F0F0F0F,//嗲
  144.     0xF0F0F0F0F0F0F0F0,0x00000000FFFFFFFF,//睧
  145.     0x006E3B133B6E0000,0x03031F331F331E00,//颬
  146.     0x0003030303637F00,0x0036363636367F00,//睼
  147.     0x007F660C180C667F,0x001E3333337E0000,//麧
  148.     0x03063E6666666600,0x00181818183B6E00,//緗
  149.     0x3F0C1E33331E0C3F,0x001C36637F63361C,//鴇
  150.     0x007736366363361C,0x001E33333E180C38,//膹
  151.     0x00007EDBDB7E0000,0x03067EDBDB7E3060,//擨
  152.     0x003C06033F03063C,0x003333333333331E,//闀
  153.     0x00003F003F003F00,0x003F000C0C3F0C0C,//貘
  154.     0x003F00060C180C06,0x003F00180C060C18,//覷
  155.     0x1818181818D8D870,0x0E1B1B1818181818,//鏷
  156.     0x000C0C003F000C0C,0x0000394E00394E00,//禴
  157.     0x000000001C36361C,0x0000001818000000,//矙
  158.     0x0000001800000000,0x383C3637303030F0,//
  159.     0x000000363636361E,0x0000003E061C301E,//
  160.     0x00003C3C3C3C0000,0xFFFFFFFFFFFFFFFF,//�
  161. };
  162.  
  163. typedef union RGBA32
  164. {
  165.     unsigned char u8[4];
  166.     unsigned int u32;
  167. } RGBA32;
  168.  
  169. typedef struct MCANVINFO
  170. {
  171.     int width,height;
  172.     //rgba32_t std_palette[STD_PALETTE_NUM];
  173.     unsigned long *std_font;
  174. } MCANVINFO;
  175.  
  176. typedef struct MCANV
  177. {
  178.     MCANVINFO info;
  179.     RGBA32 color;
  180.     unsigned int *buff;
  181.     int pitch;
  182. } MCANV;
  183.  
  184. MCANV *MCANV_Alloc(MCANVINFO *info)
  185. {
  186.     MCANV *canv=NULL;
  187.     if (info->width<=0 || info->height<=0)
  188.         return canv;
  189.     canv=(MCANV*)malloc(sizeof(MCANV));
  190.     canv->info =*info;
  191.     canv->pitch=info->width*sizeof(unsigned int);
  192.     canv->buff =(unsigned int*)malloc(canv->pitch*info->height);
  193.     memset(canv->buff,0,canv->pitch*info->height);
  194.     return canv;
  195. }
  196.  
  197. bool MCANV_Realloc(MCANV *canv,MCANVINFO *info)
  198. {
  199.     if (canv==NULL || info->width<=0 || info->height<=0)
  200.         return false;
  201.     canv->info=*info;
  202.     canv->pitch=info->width*sizeof(unsigned int);
  203.     canv->buff =(unsigned int*)realloc(canv->buff,canv->pitch*info->height);
  204.     memset(canv->buff,0,canv->pitch*info->height);
  205.     return true;
  206. }
  207.  
  208. void MCANV_Free(MCANV *canv)
  209. {
  210.     free(canv->buff);
  211.     canv->buff=NULL;
  212.     free(canv);
  213.     canv=NULL;
  214. }
  215.  
  216. void MCANV_Plot(MCANV *canv,int x,int y)
  217. {
  218.     if (x>=0 && x<canv->info.width && y>=0 && y<canv->info.height)
  219.         canv->buff[y*canv->info.width+x]=canv->color.u32;
  220. }
  221.  
  222. RGBA32 MCANV_Peek(MCANV *canv,int x,int y)
  223. {
  224.     RGBA32 color={0x00,0x00,0x00,0x00};
  225.     if (x>=0 && x<canv->info.width && y>=0 && y<canv->info.height)
  226.         color.u32=canv->buff[y*canv->info.width+x];
  227.     return color;
  228. }
  229.  
  230. void MCANV_Fill(MCANV *canv)
  231. {
  232.     for (int j=0;j<canv->info.height;j++)
  233.         for (int i=0;i<canv->info.width;i++)
  234.             MCANV_Plot(canv,i,j);
  235. }
  236.  
  237. void MCANV_FillRect0(MCANV *canv,int left,int right,int top,int bottom)
  238. {
  239.     for (int j=top;j<bottom;j++)
  240.         for (int i=left;i<right;i++)
  241.             MCANV_Plot(canv,i,j);
  242. }
  243.  
  244. void MCANV_FillRect(MCANV *canv,int x,int y,int w,int h)
  245. {
  246.     for (int j=y;j<(y+h);j++)
  247.         for (int i=x;i<(x+w);i++)
  248.             MCANV_Plot(canv,i,j);
  249. }
  250.  
  251. void MCANV_Put64(MCANV *canv,int x,int y,unsigned long u64)
  252. {
  253.     for (int j=0;j<STD_FONT_HEIGHT;j++)
  254.         for (int i=0;i<STD_FONT_WIDTH;i++)
  255.             if (u64>>(j*STD_FONT_WIDTH+i)&1)
  256.                 MCANV_Plot(canv,x+i,y+j);
  257. }
  258.  
  259. void MCANV_PutChar(MCANV *canv,int x,int y,char ch)
  260. {
  261.     MCANV_Put64(canv,x,y,canv->info.std_font[ch]);
  262. }
  263.  
  264. void MCANV_PutCharUL(MCANV *canv,int x,int y,char ch)
  265. {
  266.     MCANV_Put64(canv,x,y,canv->info.std_font[ch]);
  267.     for (int i=0;i<STD_FONT_WIDTH;i++)
  268.         MCANV_Plot(canv,x+i,y+STD_FONT_HEIGHT-1);
  269. }
  270.  
  271. void MCANV_PutStr(MCANV *canv,int x,int y,const char *str)
  272. {
  273.     int x0=x;
  274.     while (*str) {
  275.         if (*str=='\n') {
  276.             x=x0;
  277.             y+=STD_FONT_HEIGHT;
  278.             str++;
  279.         } else {
  280.             MCANV_PutChar(canv,x,y,*str);
  281.             x+=STD_FONT_WIDTH;
  282.             str++;
  283.         }
  284.     }
  285. }
  286.  
  287. void MCANV_PutStrUL(MCANV *canv,int x,int y,const char *str)
  288. {
  289.     int x0=x;
  290.     while (*str) {
  291.         if (*str=='\n') {
  292.             x=x0;
  293.             y+=STD_FONT_HEIGHT;
  294.             str++;
  295.         } else {
  296.             MCANV_PutCharUL(canv,x,y,*str);
  297.             x+=STD_FONT_WIDTH;
  298.             str++;
  299.         }
  300.     }
  301. }
  302.  
  303. int MCANV_PrintFmt(MCANV *canv,int x,int y,const char *fmt,...)
  304. {
  305.     va_list args;
  306.     va_start(args,fmt);
  307.     char buff[BUFF_SIZE];
  308.     int len=vsprintf(buff,fmt,args);
  309.     MCANV_PutStr(canv,x,y,buff);
  310.     return len;
  311. }
  312.  
  313. int MCANV_PrintFmtUL(MCANV *canv,int x,int y,const char *fmt,...)
  314. {
  315.     va_list args;
  316.     va_start(args,fmt);
  317.     char buff[BUFF_SIZE];
  318.     int len=vsprintf(buff,fmt,args);
  319.     MCANV_PutStrUL(canv,x,y,buff);
  320.     return len;
  321. }
  322. #endif//MCANV_H