zoukankan      html  css  js  c++  java
  • 大学的第一个自己的程序

      之前高中的时候,用字符界面开发(能叫开发吗。。)了一些小游戏,算是从底层写起吧。但是因为性能上的先天不足(显示字符最小也是3*5像素,然后还不是方形的,最重要的是刷新太慢),历经层层优化也只能达到全屏120*60字符或者离散200字符 10fps的刷新率,可以写一些贪吃蛇,国际象棋之类的简单游戏和程序,但是更高的想法就不能满足了。在进入大学之后,有了更多的时间,想再自己从底层写一些东西出来,于是有了下面这种过程。。

      首先是选择API,一开始我有三种想法:WINAPI,Direct_x,openGL。但在查阅相关资料后,放弃了这三种选项,WINAPI太过繁琐,而且Microsoft那堆程序员的程序标准我也真是不习惯,全大写函数名我就不说了,元音字母省略让人根本看不懂,最后函数名还是那么冗长。。而Direct_x和openGL整合太好了,不符合我从较基础(至少功能上)写起的初衷。

      然后我想起了曾经使用pascal时的恐惧,哦不是,是舒服。pascal当时有个Graphic库,函数简单实用,性能也不错,著名的pascal自带游戏就是用的那个东西。听说很多语言都可以用。然后试着百度了一下,发现一个国内团体还在提供可使用的graphic库,名字叫easyX。就下下来作为API的选择。附链接:easyX

      第一个软件写什么?这要说到我自己遇到的一个问题。我在上大学之前入手了一台surface3,平常上课的时候可以带去看课件,记笔记,在寝室里用“重型”笔记本的时候,它就会被我架在旁边,充着电。因为用笔记本打代码的原因,并不想碰鼠标,加上任务栏上时间太小且一般隐藏任务栏,所以一个在surface上看时间的应用成为了我的需求。这类应用应该不少,但是直接的桌面应用界面都非常麻烦,不够简洁,metro界面则没有找到这样一款应用,可能是大家觉得太简单了吧。于是我准备自己来写这样一个东西。

      初步目标是,写一个应用,全屏显示当前时间。

       

      然后我想增加一些功能,我在使用时可能需要计时和倒计时功能来规范工作时间。然后我想更改字的颜色以符合我的癖好。

      更改颜色很简单,只需要加一个变量和一个函数就够了。但是其他的功能会比较麻烦。

      因为理论上来说都是显示时间,于是我新建了一个叫TimeShower的类,准备现在时间显示,计时,倒计时分别用这样一个类来表示,除了计时方面共用同样的显示函数。

      计时和倒计时也不难。计时可以从0开始每秒加一,然后暂停功能自然而然的实现。倒计时也简单,将用户给出的秒数每秒减一显示就可以了。因为要让用户选择倒计时的开始时间,对于倒计时界面增加了一个箭头的显示。按动上下键选择时,箭头颜色会发生变化使容易看见。

        

      似乎该实现的功能都实现了,但是单一的字符大小有一点不是很适合所有情况,所以我添加了一个函数以改变所有显示的大小。但是一个字符的显示有许多参数(都是自己设置的),包括笔画宽度,字宽度,字高度,数字间距,数字与间隔号间距,两旁间距,上下间距等。如果有如此多的函数设置起来也会非常麻烦,然后会出现许多可能的bug比如 笔画宽度 * 2 < 字符宽度 就会出现一些奇怪的事。同样的,如果字宽度和字高度比例达到很奇怪的数值比如1:1也会很难看。所以我设计了一个函数,传入参数只有一个简单的百分比(一个0~1之间的小数),表示的意思是字高占据屏幕高度的比例,一般(如上面的图)是0.5 。当然也可以自己改动。使用这个函数后整个界面默认居中显示。

      突然,我发现了一个我没有考虑到的点。现在我有三个TImeShower类,但是我有可能在倒计时运行的过程中查看现在的时间。这样就有一个问题,如何实现在显示某个TImeShower时,其他的TimeShower一样在运行。一开始我想到了多线程,但是想想又觉得太麻烦,于是我就想伪造这样一个情况。新建了一个TImeControler类,在里面添加三个TimeShower类分别是现在时间,倒计时,计时。接着更改了计时和倒计时的机制。计时改为记录一个当前时间(这个时间在每次更新显示时即不暂停又在显示时会自动更新),记录一个显示时间(即显示的那个时钟),记录一个上次挂起的时间(退出时会挂起),当然还有一个是否暂停中的bool变量。每次进入时先判断是否是暂停状态,如果是,那么显示时间就赋值为挂起时间,如果不是,那么显示时间就是挂起时间加上现在时间(ctime的time()函数)减去挂起时的当前时间。倒计时改为无挂起时间时进入选择时间,有挂起时间时,显示时间赋值为挂起时间减去现在时间加上挂起时的当前时间。

      这样,我们就实现了三种时间的同时(看起来同时)的进行。

      接下来,我又觉得黑色的背景太过难看,于是添加了一段显示背景的代码。首先利用easyX提供的库读入图片,得到颜色表(即每个像素对应的颜色)。再将颜色表显示在屏幕上。因为数字需要重绘,重绘时不能像原来直接用黑色覆盖而需要将一个数组中的某些值复制到另一个数组中,效率有明显的降低。我终于能够理解为什么在苹果的早起移动设备中(比如ipod touch 2)不能设置桌面背景图片了。

      背后有了图片,我就想让文字透明显示。透明也很简单。比如两层的颜色,前景的透明度为50%的意思就是每个像素的RGB三个值,就是前景的RGB三个值乘上50%再加上背景的RGB三色值乘上50%。就像调颜料一样按不同比例添加最后比例和为1就行了。这个又会多一次计算,不过因为字的颜色统一,透明度统一,可以预先计算。

      最后呢,我尝试使用字符的图片来代替数字显示。这一块代码量比较大,然后写的时候兼容调比例函数时发现一个问题。原来我是在上一个设置的基础上调比例,比如说先调至很小的60 * 24像素,再调大至300 * 120像素,虽然读入时是300 * 120像素的较高清图片,但是经过这个过程之后就会全是马赛克。。。改过之后就好了。

      

      于是现在我的开发就算完成了,因为懒没有写用户界面什么的,在运行中改设置和操控会很奇葩,但是总体质量比高中的好了不少。有什么不足吗?有,代码上还是太多蛮干而不是利用面向对象的特性,visual studio运用的也不太熟练,时间函数上也用的精确度很低的系统函数。这些方面在我以后的程序中也会不断的改进。

      下面就贴一下代码吧:

     1 #include "targetver.h"
     2 #include <stdio.h>
     3 #include <tchar.h>
     4 #include <ctime>
     5 #include <fstream>
     6 #include <graphics.h>
     7 #include <conio.h>
     8 #include <algorithm>
     9 
    10 #define SCREENX GetSystemMetrics(SM_CXSCREEN)
    11 #define SCREENY GetSystemMetrics(SM_CYSCREEN)
    stdafx.h
      1 void FullScreen()
      2 {
      3     HWND hwnd = GetHWnd();
      4     SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) - WS_CAPTION);
      5     SetWindowPos(hwnd, HWND_TOP, 0, 0, SCREENX, SCREENY, SWP_SHOWWINDOW);
      6 }
      7 
      8 
      9 struct NumberBox
     10 {
     11     bool exist[600][200];
     12 
     13     NumberBox ChangeXY(int newx, int newy, int x, int y,NumberBox* Origin = NULL)
     14     {
     15         if (Origin == NULL) Origin = this;
     16         NumberBox ret;
     17         double xratio = (double)x / newx;
     18         double yratio = (double)y / newy;
     19         for (int i = 0; i < newx; ++i)
     20         {
     21             for (int j = 0; j < newy; ++j)
     22             {
     23                 ret.exist[j][i] = Origin->exist[ (int)(j * yratio) ][ (int)(i * xratio) ];
     24             }
     25         }
     26         return ret;
     27     }
     28 };
     29 
     30 class NumberShower
     31 {
     32 private:
     33     IMAGE Number;
     34     DWORD Graph[300][1200];
     35     DWORD* Buffer;
     36 public:
     37     NumberShower()
     38     {
     39         loadimage(&Number, L"Number.bmp", 1200, 300);
     40         Buffer = GetImageBuffer(&Number);
     41         DWORD* end = Buffer + 1200 * 300;
     42         DWORD* j = &Graph[0][0];    
     43         for (DWORD* i = Buffer; i < end; ++i, ++j)
     44             *j = *i;
     45         return;
     46     }
     47 
     48     NumberBox GetNumber(int t)
     49     {
     50         if (t == 0) t = 10;
     51         int sx = t * 120 - 120;
     52         int sy = 0;
     53         int ex = t * 120 - 1;
     54         int ey = 300;
     55         NumberBox ret;
     56         for (int j = sy; j < ey; ++j)
     57         {
     58             for (int i = sx; i <= ex; ++i)
     59             {
     60                 ret.exist[j - sy][i - sx] = Graph[j][i]? 0 : 1;
     61             }
     62         }
     63         return ret;
     64     }
     65 };
     66 
     67 class TimeShower
     68 {
     69 private:
     70     int AllColor;
     71     int left;
     72     int top;
     73     int xsize;
     74     int ysize;
     75     int lwidth;
     76     int gapsize;
     77     int DNsize;
     78     bool pause;
     79     double alpha;
     80 
     81     DWORD* Buffer;
     82     int CoTab[864][1536];
     83     int rstart;// = xsize - lwidth;
     84     int midstart;// = (ysize - lwidth) / 2;
     85     int mid;// = ysize / 2;
     86     int botstart;// = ysize - lwidth;
     87     time_t ntime;
     88     time_t sustime;
     89     time_t lastime;
     90     tm nowtime;
     91     NumberShower TypeOr;
     92     NumberBox Type[10];
     93 public:
     94 
     95     //Source Draw
     96 
     97     int GetAlphaColor(int color,double alph = -1)
     98     {
     99         if (alph == -1) alph = alpha;
    100         if (alph > 1 || alph < 0) return 0;
    101         int R = color >> 16;
    102         int G = (color >> 8) & 255;
    103         int B = color & 255;
    104         R *= alph;
    105         G *= alph;
    106         B *= alph;
    107         return R << 16 | G << 8 | B;
    108     }
    109 
    110     void ChangeAlpha(double alph)
    111     {
    112         if (alph < 0 || alph > 1) return;
    113         alpha = alph;
    114     }
    115 
    116     void VEline(int sx, int sy, bool half = false, int color = -1, double alph = -1)//^v
    117     {
    118         if (color = -1) color = this->AllColor;
    119         if (alph == 0)
    120         {
    121             int ex = sx + lwidth;
    122             int ey = sy + ysize;
    123             for (int i = sx; i < ex; ++i)
    124             {
    125                 for (int j = sy; j < ey; ++j)
    126                 {
    127                     putpixel(i , j , CoTab[j][i]);
    128                 }
    129             }
    130             return;
    131         }
    132         if (alph == -1) alph = alpha;
    133         int ys = ysize;
    134         if (half)
    135             ys = ysize / 2;
    136         color = GetAlphaColor(color, alph);
    137         for (int i = 0; i < lwidth; ++i)
    138         {
    139             for (int j = 0; j < ys; ++j)
    140             {
    141                 putpixel(i + sx, j + sy, color + GetAlphaColor(CoTab[j + sy][i + sx],1-alph) );
    142             }
    143         }
    144         return;
    145     }
    146 
    147     void HOline(int sx, int sy, int color = -1, double alph = -1)//<-->
    148     {
    149         if (color = -1) color = this->AllColor;
    150         if (alph == 0)
    151         {
    152             int ex = sx + xsize;
    153             int ey = sy + lwidth;
    154             for (int i = sx; i < ex; ++i)
    155             {
    156                 for (int j = sy; j < ey; ++j)
    157                 {
    158                     putpixel(i , j, CoTab[j][i]);
    159                 }
    160             }
    161             return;
    162         }
    163         if (alph == -1) alph = alpha;
    164         int xs = xsize;
    165         color = GetAlphaColor(color, alph);
    166         for (int i = 0; i < xs; ++i)
    167         {
    168             for (int j = 0; j < lwidth; ++j)
    169             {
    170                 putpixel(i + sx, j + sy, color + GetAlphaColor(CoTab[j + sy][i + sx], 1 - alph));
    171             }
    172         }
    173         return;
    174     }
    175 
    176     void Dot(int sx, int sy, int color = -1,double alph = -1)
    177     {
    178         if (color = -1) color = this->AllColor;
    179         if (alph == -1) alph = alpha;
    180         color = GetAlphaColor(color, alph);
    181         for (int i = 0; i < lwidth; ++i)
    182         {
    183             for (int j = 0; j < lwidth; ++j)
    184             {
    185                 putpixel(i + sx, j + sy, color + GetAlphaColor(CoTab[j + sy][i + sx], 1 - alph));
    186             }
    187         }
    188         return;
    189     }
    190 
    191     void UperTri(int sx, int sy, int color = -1,double alph = -1)
    192     {
    193         if (color == -1) color = AllColor;
    194         if (alph == -1) alph = alpha;
    195         color = GetAlphaColor(color, alph);
    196         for (int j = 0; j <= xsize/4; ++j)
    197         {
    198             for (int i = j - xsize/4; i <= xsize/4 - j; ++i)
    199             {
    200                 int x = sx + (xsize / 2) + i;
    201                 int y = sy - lwidth - j;
    202                 putpixel(x, y, color + GetAlphaColor(CoTab[y][x], 1 - alph));
    203             }
    204         }
    205         return;
    206     }
    207 
    208     void LowerTri(int sx, int sy, int color = -1, double alph = -1)
    209     {
    210         if (color == -1) color = AllColor;
    211         if (alph == -1) alph = alpha;
    212         color = GetAlphaColor(color, alph);
    213         for (int j = 0; j <= xsize/4; ++j)
    214         {
    215             for (int i = - xsize/4 +j; i <= -j + xsize/4 ; ++i)
    216             {
    217                 int x = sx + (xsize / 2) + i;
    218                 int y = sy + lwidth + ysize + j;
    219                 putpixel(x,y, color + GetAlphaColor(CoTab[y][x], 1 - alph));
    220             }
    221         }
    222         return;
    223     }
    224 
    225     //NumberDraw
    226 
    227     void DrawType(NumberBox in, int sx, int sy, int color = -1, double alph = -1) 
    228     {
    229         if (alph == 0)
    230         {
    231             for (int j = 0; j < ysize; ++j)
    232             {
    233                 for (int i = 0; i < xsize; ++i)
    234                 {
    235                     if (in.exist[j][i])
    236                     {
    237                         putpixel(i + sx, j + sy,CoTab[j + sy][i + sx]); 
    238                     }
    239                 }
    240             }
    241             return;
    242         }
    243         if (color == -1) color = AllColor;
    244         if (alph == -1) alph = alpha;
    245         color = GetAlphaColor(color, alph);
    246         for (int j = 0; j < ysize; ++j)
    247         {
    248             for (int i = 0; i < xsize; ++i)
    249             {
    250                 if (in.exist[j][i])
    251                 {
    252                     putpixel(i + sx, j + sy, color + GetAlphaColor(CoTab[j + sy][i + sx], 1 - alph)); 
    253                 }
    254             }
    255         }
    256         return;
    257     }
    258 
    259     void p1(int sx, int sy, double alph = -1)
    260     {
    261         VEline(sx + rstart, sy,false,-1,alph);
    262     }
    263 
    264     void p2(int sx, int sy, double alph = -1)
    265     {
    266         HOline(sx, sy,-1,alph);
    267         HOline(sx, sy + midstart,-1,alph);
    268         HOline(sx, sy + botstart,-1,alph);
    269         VEline(sx, sy + mid, true,-1,alph);
    270         VEline(sx + rstart, sy, true,-1,alph);
    271     }
    272 
    273     void p3(int sx, int sy, double alph = -1)
    274     {
    275         HOline(sx, sy,-1,alph);
    276         HOline(sx, sy + midstart ,- 1, alph);
    277         HOline(sx, sy + botstart, -1, alph);
    278         VEline(sx + rstart, sy,0 , -1, alph);
    279     }
    280 
    281     void p4(int sx, int sy, double alph = -1)
    282     {
    283         HOline(sx, sy + midstart, -1, alph);
    284         VEline(sx, sy, true, -1, alph);
    285         VEline(sx + rstart, sy,0, -1, alph);
    286     }
    287 
    288     void p5(int sx, int sy, double alph = -1)
    289     {
    290         HOline(sx, sy, -1, alph);
    291         HOline(sx, sy + midstart, -1, alph);
    292         HOline(sx, sy + botstart, -1, alph);
    293         VEline(sx + rstart, sy + mid, true, -1, alph);
    294         VEline(sx, sy, true, -1, alph);
    295     }
    296 
    297     void p6(int sx, int sy, double alph = -1)
    298     {
    299         HOline(sx, sy, -1, alph);
    300         HOline(sx, sy + midstart, -1, alph);
    301         HOline(sx, sy + botstart, -1, alph);
    302         VEline(sx + rstart, sy + mid, true, -1, alph);
    303         VEline(sx, sy,0,  -1, alph);
    304     }
    305 
    306     void p7(int sx, int sy, double alph = -1)
    307     {
    308         HOline(sx, sy, -1, alph);
    309         VEline(sx + rstart, sy,0, -1, alph);
    310     }
    311 
    312     void p8(int sx, int sy, double alph = -1)
    313     {
    314         HOline(sx, sy, -1, alph);
    315         HOline(sx, sy + midstart, -1, alph);
    316         HOline(sx, sy + botstart, -1, alph);
    317         VEline(sx, sy,0, -1, alph);
    318         VEline(sx + rstart, sy,0, -1, alph);
    319     }
    320 
    321     void p9(int sx, int sy, double alph = -1)
    322     {
    323         HOline(sx, sy, -1, alph);
    324         HOline(sx, sy + midstart, -1, alph);
    325         HOline(sx, sy + botstart, -1, alph);
    326         VEline(sx + rstart, sy,0, -1, alph);
    327         VEline(sx, sy, true, -1, alph);
    328     }
    329 
    330     void p0(int sx, int sy, double alph = -1)
    331     {
    332         HOline(sx, sy, -1, alph);
    333         HOline(sx, sy + botstart, -1, alph);
    334         VEline(sx, sy,0, -1, alph);
    335         VEline(sx + rstart, sy,0, -1, alph);
    336     }
    337 
    338     void Clean(int sx, int sy, int ex, int ey)
    339     {
    340         for (int j = sy; j <= ey; ++j)
    341         {
    342             for (int i = sx; i <= ex; ++i)
    343             {
    344                 putpixel(i, j, CoTab[j][i]);
    345             }
    346         }
    347     }
    348 
    349     void CleanHuge(int sx, int sy)
    350     {
    351 //        p8(sx, sy, 0);
    352         Clean(sx, sy, sx + xsize, sy + ysize);
    353         UperTri(sx, sy, -1, 0);
    354         LowerTri(sx, sy, -1, 0);
    355     }
    356 
    357     void DrawNumber(int sx, int sy, int t, double alph = -1)
    358     {
    359         CleanHuge(sx, sy);
    360 /*        if (t == 1) p1(sx, sy,alph);
    361         if (t == 2) p2(sx, sy, alph);
    362         if (t == 3) p3(sx, sy, alph);
    363         if (t == 4) p4(sx, sy, alph);
    364         if (t == 5) p5(sx, sy, alph);
    365         if (t == 6) p6(sx, sy, alph);
    366         if (t == 7) p7(sx, sy, alph);
    367         if (t == 8) p8(sx, sy, alph);
    368         if (t == 9) p9(sx, sy, alph);
    369         if (t == 0) p0(sx, sy, alph);
    370 */
    371         DrawType(Type[t],sx,sy,-1,alph );
    372     }
    373 
    374     void DrawTwo(int sx, int sy, int number,double alph  = -1)
    375     {
    376         DrawNumber(sx, sy, number / 10, alph);
    377         DrawNumber(sx + xsize + gapsize, sy, number % 10, alph);
    378     }
    379 
    380     void DrawPoint(int sx, int sy,double alph = -1)
    381     {
    382         Dot(sx, sy + ysize / 3 - lwidth / 2, alph);
    383         Dot(sx, sy + 2 * ysize / 3 - lwidth / 2, alph);
    384     }
    385 
    386     void GetCoTab()
    387     {
    388         Buffer = GetImageBuffer();
    389         DWORD* end = Buffer + SCREENX * SCREENY;
    390         int* j = &CoTab[0][0];
    391         for (DWORD* i = Buffer; i < end; ++i, ++j)
    392         {
    393             *j = (*i >> 16) | (*i & 65280) | ((*i & 255) << 16);
    394         }
    395     }
    396 
    397     TimeShower(int AC = RGB(255, 255, 255), int l = 0, int t = 0, int x = 120, int y = 300, int w = 20, int g = 20, int DN = 60,double alph = 0.5)
    398     {
    399 //        BeginBatchDraw();
    400         for (int i = 0; i <= 9; ++i)
    401             Type[i] = TypeOr.GetNumber(i);
    402         alpha = alph;
    403         pause = false;
    404         sustime = 0;
    405         lastime = 0;
    406         AllColor = AC;
    407         left = l;
    408         top = t;
    409         xsize = x;
    410         ysize = y;
    411         lwidth = w;
    412         gapsize = g;
    413         DNsize = DN;
    414         ntime = time(0);
    415         localtime_s(&nowtime, &ntime);
    416         rstart = xsize - lwidth;
    417         midstart = (ysize - lwidth) / 2;
    418         mid = ysize / 2;
    419         botstart = ysize - lwidth;
    420         return;
    421     }
    422 
    423     void ChangeInfo(int AC = RGB(255, 255, 255), int l = 0, int t = 0, int x = 120, int y = 300, int w = 20, int g = 20, int DN = 60)
    424     {
    425         AllColor = AC;
    426         left = l;
    427         top = t;
    428         xsize = x;
    429         ysize = y;
    430         for (int i = 0; i <= 9; ++i)
    431         {
    432             Type[i] = TypeOr.GetNumber(i);
    433             Type[i] = Type[i].ChangeXY(xsize, ysize,120,300);
    434         }
    435         lwidth = w;
    436         gapsize = g;
    437         DNsize = DN;
    438         rstart = xsize - lwidth;
    439         midstart = (ysize - lwidth) / 2;
    440         mid = ysize / 2;
    441         botstart = ysize - lwidth;
    442         return;
    443     }
    444 
    445     void GetNowTime()
    446     {
    447         ntime = time(0);
    448     }
    449 
    450     void CleanTime()
    451     {
    452         ntime = 0;
    453     }
    454 
    455     void IncTime()
    456     {
    457         ntime++;
    458     }
    459 
    460     void SetTime(long long tim)
    461     {
    462         ntime = tim;
    463     }
    464 
    465     void DecTime()
    466     {
    467         ntime--;
    468         if (ntime < 0) ntime = 0;
    469     }
    470 
    471     void AutoSize(double ratio = 0.4,int x = SCREENX,int y = SCREENY)
    472     {
    473         if (ratio < 0 || ratio >= 0.7) return;
    474         ysize = y * ratio;
    475         top = (y - ysize) / 2;
    476         xsize = ysize * 0.618 / 2;
    477         for (int i = 0; i <= 9; ++i)
    478         {
    479             Type[i] = TypeOr.GetNumber(i);
    480             Type[i] = Type[i].ChangeXY(xsize, ysize, 120, 300);
    481         }
    482         lwidth = xsize / 6;
    483         gapsize = lwidth * 2;
    484         DNsize = gapsize * 1.5;
    485         left = (x - (DNsize * 4 + gapsize * 3 + xsize * 6)) / 2;
    486         rstart = xsize - lwidth;
    487         midstart = (ysize - lwidth) / 2;
    488         mid = ysize / 2;
    489         botstart = ysize - lwidth;
    490         return;
    491     }
    492 
    493     void AutoSize(int sx, int sy, int nx, int ny, double ratio = 0.4)
    494     {
    495         int x = nx - sx;
    496         int y = ny - sy;
    497         if (x <= 0 || y <= 0) return;
    498         if (ratio < 0 || ratio >= 0.7) return;
    499         ysize = y * ratio;
    500         top = (y - ysize) / 2 + sy;
    501         xsize = ysize * 0.618 / 2;
    502         lwidth = xsize / 6;
    503         gapsize = lwidth * 2;
    504         DNsize = gapsize * 1.5;
    505         left = (x - (DNsize * 4 + gapsize * 3 + xsize * 6)) / 2 + sx;
    506         rstart = xsize - lwidth;
    507         midstart = (ysize - lwidth) / 2;
    508         mid = ysize / 2;
    509         botstart = ysize - lwidth;
    510         return;
    511     }
    512 
    513     void DrawTIMER(bool local = true,double alph = -1)
    514     {
    515         if (local) localtime_s(&nowtime, &ntime);
    516         else
    517             gmtime_s(&nowtime, &ntime);
    518         int hour = nowtime.tm_hour;
    519         int min = nowtime.tm_min;
    520         int sec = nowtime.tm_sec;
    521         int nowx = left;
    522         DrawTwo(nowx, top, hour,alph);
    523         nowx += xsize * 2 + gapsize + DNsize;
    524         DrawPoint(nowx, top, alph);
    525         nowx += DNsize + lwidth;
    526         DrawTwo(nowx, top, min, alph);
    527         nowx += xsize * 2 + gapsize + DNsize;
    528         DrawPoint(nowx, top, alph);
    529         nowx += DNsize + lwidth;
    530         DrawTwo(nowx, top, sec, alph);
    531         FlushBatchDraw();
    532     }//need
    533 
    534     void DrawChoosenNumber(int sx,int sy,int t,double alph = -1)
    535     {
    536         CleanHuge(sx, sy); 
    537 /*        if (t == 1) p1(sx, sy, alph);
    538         if (t == 2) p2(sx, sy, alph);
    539         if (t == 3) p3(sx, sy, alph);
    540         if (t == 4) p4(sx, sy, alph);
    541         if (t == 5) p5(sx, sy, alph);
    542         if (t == 6) p6(sx, sy, alph);
    543         if (t == 7) p7(sx, sy, alph);
    544         if (t == 8) p8(sx, sy, alph);
    545         if (t == 9) p9(sx, sy, alph);
    546         if (t == 0) p0(sx, sy, alph);
    547 */
    548         DrawType(Type[t], sx, sy, -1, alph);
    549         UperTri(sx, sy, alph);
    550         LowerTri(sx, sy, alph);
    551     }
    552 
    553     void DrawChangingNumber(int sx, int sy, int t,bool ul,double alph = -1)
    554     {
    555         CleanHuge(sx, sy);
    556 /*        if (t == 1) p1(sx, sy, alph);
    557         if (t == 2) p2(sx, sy, alph);
    558         if (t == 3) p3(sx, sy, alph);
    559         if (t == 4) p4(sx, sy, alph);
    560         if (t == 5) p5(sx, sy, alph);
    561         if (t == 6) p6(sx, sy, alph);
    562         if (t == 7) p7(sx, sy, alph);
    563         if (t == 8) p8(sx, sy, alph);
    564         if (t == 9) p9(sx, sy, alph);
    565         if (t == 0) p0(sx, sy, alph);
    566 */
    567         DrawType(Type[t], sx, sy, -1, alph);
    568         int orcolor = AllColor;
    569         AllColor -= RGB(0, 200, 50);
    570         if (ul)
    571         {
    572             UperTri(sx, sy, alph);
    573             AllColor = orcolor;
    574             LowerTri(sx, sy, alph);
    575         }
    576         else
    577         {
    578             LowerTri(sx, sy, alph);
    579             AllColor = orcolor;
    580             UperTri(sx, sy, alph);
    581         }
    582     }
    583 
    584     void DrawChoosenTwo(int sx, int sy, int number,double alph = -1)
    585     {
    586         DrawChoosenNumber(sx, sy, number / 10, alph);
    587         DrawChoosenNumber(sx + xsize + gapsize, sy, number % 10, alph);
    588     }
    589 
    590     void DrawChangingTwo(int sx, int sy, int number,bool ul)
    591     {
    592         DrawChangingNumber(sx, sy, number / 10,ul);
    593         DrawChangingNumber(sx + xsize + gapsize, sy, number % 10,ul);
    594     }
    595 
    596     void DrawChoosenTIMER(int chspart,double alph = -1)
    597     {
    598         gmtime_s(&nowtime, &ntime);
    599         int hour = nowtime.tm_hour;
    600         int min = nowtime.tm_min;
    601         int sec = nowtime.tm_sec;
    602         int nowx = left;
    603         if (chspart == 2)
    604             DrawChoosenTwo(nowx, top, hour, alph);
    605         else
    606             DrawTwo(nowx, top,hour);
    607         nowx += xsize * 2 + gapsize + DNsize;
    608         DrawPoint(nowx, top, alph);
    609         nowx += DNsize + lwidth;
    610         if (chspart == 1)
    611             DrawChoosenTwo(nowx, top, min, alph);
    612         else
    613             DrawTwo(nowx, top, min);
    614         nowx += xsize * 2 + gapsize + DNsize;
    615         DrawPoint(nowx, top, alph);
    616         nowx += DNsize + lwidth;
    617         if (chspart == 0)
    618             DrawChoosenTwo(nowx, top, sec, alph);
    619         else
    620             DrawTwo(nowx, top, sec, alph);
    621         FlushBatchDraw();
    622     }
    623 
    624     void DrawChangingTIMER(int chspart,bool ul,double alph = -1)
    625     {
    626         gmtime_s(&nowtime, &ntime);
    627         int hour = nowtime.tm_hour;
    628         int min = nowtime.tm_min;
    629         int sec = nowtime.tm_sec;
    630         int nowx = left;
    631         if (chspart == 2)
    632             DrawChangingTwo(nowx, top, hour,ul);
    633         else
    634             DrawTwo(nowx, top, hour, alph);
    635         nowx += xsize * 2 + gapsize + DNsize;
    636         DrawPoint(nowx, top, alph);
    637         nowx += DNsize + lwidth;
    638         if (chspart == 1)
    639             DrawChangingTwo(nowx, top, min,ul);
    640         else
    641             DrawTwo(nowx, top, min, alph);
    642         nowx += xsize * 2 + gapsize + DNsize;
    643         DrawPoint(nowx, top, alph);
    644         nowx += DNsize + lwidth;
    645         if (chspart == 0)
    646             DrawChangingTwo(nowx, top, sec,ul);
    647         else
    648             DrawTwo(nowx, top, sec, alph);
    649         FlushBatchDraw();
    650     }
    651 
    652     void SwitchTimeControl()
    653     {
    654         CleanTime();
    655         int nowpos = 0;
    656         int add[3] = { 1,60,3600 };
    657         int allt = 86400;
    658         DrawChoosenTIMER(nowpos);
    659         while (1)
    660         {
    661             if (!_kbhit()) continue;
    662             char ch = _getch();
    663             if (ch == 13) 
    664             {
    665                 DrawTIMER(0);
    666                 return;
    667             }
    668             if (ch != 0 && ch != -32) continue;
    669             DrawChoosenTIMER(nowpos);
    670             ch = _getch();
    671             if (ch == 72)
    672             {
    673                 ntime += add[nowpos];
    674                 if (ntime >= allt)
    675                     ntime = 0;
    676                 DrawChangingTIMER(nowpos,true);
    677                 Sleep(100);
    678                 DrawChoosenTIMER(nowpos);
    679             }
    680             if (ch == 80)
    681             {
    682 
    683                 ntime -= add[nowpos];
    684                 if (ntime < 0) ntime += allt;
    685                 DrawChangingTIMER(nowpos,false);
    686                 Sleep(100);
    687                 DrawChoosenTIMER(nowpos);
    688             }
    689             if (ch == 75)
    690             {
    691                 nowpos = (nowpos + 1) % 3;
    692             }
    693             if (ch == 77)
    694             {
    695                 nowpos = nowpos - 1;
    696                 if (nowpos < 0) nowpos = 2;
    697             }
    698             DrawChoosenTIMER(nowpos);
    699         }
    700     }
    701 
    702     void Suspend()
    703     {
    704         sustime = ntime;
    705         lastime = time(0);
    706     }
    707 
    708     int CurrentTimeShow()
    709     {
    710         int lasttime = time(0);
    711         while (1)
    712         {
    713             if (_kbhit())
    714             {
    715                 char ch = _getch();
    716                 if (ch == 'c' || ch == 'C')
    717                     return 2;
    718                 if (ch == 't' || ch == 'T')
    719                     return 3;
    720                 if (ch == 'q' || ch == 'Q')
    721                     return 4;
    722                 if (ch == 'x' || ch == 'X')
    723                     return 0;
    724                 if (ch == 's' || ch == 'S')
    725                     return 5;
    726                 if (ch == 'r' || ch == 'R')
    727                     return 6;
    728             }
    729             if (time(0) != lasttime)
    730             {
    731                 GetNowTime();
    732                 DrawTIMER();
    733                 lasttime = time(0);
    734             }
    735             Sleep(50);
    736         }
    737         return 0;
    738     }
    739 
    740     int DecTimeShow()
    741     {
    742         int lasttime = time(0);
    743         if (!sustime)
    744             SwitchTimeControl();
    745         else
    746         {
    747             ntime = sustime - (time(0) - lastime);
    748             if (ntime < 0) ntime = 0;
    749             DrawTIMER(0);
    750         }
    751         while (1)
    752         {
    753             if (_kbhit())
    754             {
    755                 char ch = _getch();
    756                 if (ch == 'n' || ch == 'N')
    757                 {
    758                     Suspend();
    759                     return 1;
    760                 }
    761                 if (ch == 't' || ch == 'T')
    762                 {
    763                     Suspend();
    764                     return 3;
    765                 }
    766                 if (ch == 'x' || ch == 'X')
    767                 {
    768                     Suspend();
    769                     return 0;
    770                 }
    771                 if (ch == 'q' || ch == 'Q')
    772                 {
    773                     Suspend();
    774                     return 4;
    775                 }
    776                 if (ch == 's' || ch == 'S')
    777                 {
    778                     Suspend();
    779                     return 5;
    780                 }
    781                 if (ch == 'r' || ch == 'R')
    782                 {
    783                     Suspend();
    784                     return 6;
    785                 }
    786                 if (ch == 'a' || ch == 'A')
    787                 {
    788                     sustime = 0;
    789                     return 2;
    790                 }
    791             }
    792             if (time(0) != lasttime)
    793             {
    794                 DecTime();
    795                 DrawTIMER(0);
    796                 lasttime = time(0);
    797             }
    798             Sleep(50);
    799         }
    800         return 0;
    801     }
    802 
    803     int IncTimeShow()
    804     {
    805         int lasttime = time(0);
    806         if (sustime == 0)
    807         {
    808             ntime = 0;
    809             DrawTIMER(0);
    810             char ch2;
    811             while (ch2 = _getch())
    812             {
    813                 if (ch2 == 13) break;
    814             }
    815         }
    816         else
    817         {
    818             if (!pause)
    819                 ntime = sustime + time(0) - lastime;
    820             else
    821                 ntime = sustime;
    822             DrawTIMER(0);
    823         }
    824         while (1)
    825         {
    826             if (_kbhit())
    827             {
    828                 char ch = _getch();
    829                 if (ch == 'n' || ch == 'N')
    830                 {
    831                     Suspend();
    832                     return 1;
    833                 }
    834                 if (ch == 'c' || ch == 'C')
    835                 {
    836                     Suspend();
    837                     return 2;
    838                 }
    839                 if (ch == 'x' || ch == 'X')
    840                 {
    841                     Suspend();
    842                     return 0;
    843                 }
    844                 if (ch == 'q' || ch == 'Q')
    845                 {
    846                     Suspend();
    847                     return 4;
    848                 }
    849                 if (ch == 's' || ch == 'S')
    850                 {
    851                     Suspend();
    852                     return 5;
    853                 }
    854                 if (ch == 'a' || ch == 'A')
    855                 {
    856                     sustime = 0;
    857                     return 3;
    858                 }
    859                 if (ch == 'r' || ch == 'R')
    860                 {
    861                     Suspend();
    862                     return 6;
    863                 }
    864                 if (ch == 13)
    865                     pause = !pause;
    866             }
    867             if (time(0) != lasttime )
    868             {
    869                 if (!pause)
    870                 {
    871                     IncTime();
    872                     DrawTIMER(0);
    873                 }
    874                 lasttime = time(0);
    875             }
    876             Sleep(50);
    877         }
    878         return 0;
    879     }
    880 
    881 };
    882 
    883 class TimerControler
    884 {
    885 private:
    886     TimeShower NowTime, DecTime, IncTime;
    887     IMAGE BackGround;
    888 
    889 public:
    890     void TimerControl(double ratio = 0.5, int colo = RGB(255, 255, 255),double alpha = 0.6)
    891     {
    892         loadimage(&BackGround, _T("test.jpg"),SCREENX,SCREENY);
    893         putimage(0,0,&BackGround);
    894         NowTime.ChangeInfo(colo);
    895         DecTime.ChangeInfo(colo);
    896         IncTime.ChangeInfo(colo);
    897         NowTime.AutoSize(ratio);
    898         IncTime.AutoSize(ratio);
    899         DecTime.AutoSize(ratio);
    900         NowTime.ChangeAlpha(alpha);
    901         DecTime.ChangeAlpha(alpha);
    902         IncTime.ChangeAlpha(alpha);
    903         NowTime.GetCoTab();
    904         IncTime.GetCoTab();
    905         DecTime.GetCoTab();
    906         int t = NowTime.CurrentTimeShow();
    907         while (1)
    908         {
    909             if (t == 1)
    910                 t = NowTime.CurrentTimeShow();
    911             if (t == 2)
    912                 t = DecTime.DecTimeShow();
    913             if (t == 3)
    914                 t = IncTime.IncTimeShow();
    915             if (t == 4)
    916             {
    917                 int r, g, b;
    918                 scanf_s("%d%d%d", &r,&g,&b);
    919                 colo = RGB(r, g, b);
    920                 NowTime.ChangeInfo(colo);
    921                 DecTime.ChangeInfo(colo);
    922                 IncTime.ChangeInfo(colo);
    923                 NowTime.AutoSize(ratio);
    924                 IncTime.AutoSize(ratio);
    925                 DecTime.AutoSize(ratio);
    926                 t = 1;
    927             }
    928             if (t == 5)
    929             {
    930                 scanf_s("%lf", &ratio); 
    931                 initgraph(SCREENX, SCREENY);
    932                 FullScreen();
    933                 putimage(0, 0, &BackGround);
    934                 NowTime.AutoSize(ratio);
    935                 IncTime.AutoSize(ratio);
    936                 DecTime.AutoSize(ratio);
    937                 t = 1;
    938             }
    939             if (t == 6)
    940             {
    941                 scanf_s("%lf", &alpha);
    942                 NowTime.ChangeAlpha(alpha);
    943                 DecTime.ChangeAlpha(alpha);
    944                 IncTime.ChangeAlpha(alpha);
    945                 t = 1;
    946             }
    947             if (t == 0)
    948                 return;
    949         }
    950     }
    951 
    952 };
    TIMER.h
     1 #include "stdafx.h"
     2 #include "TIMER.h"
     3 using namespace std;
     4 
     5 TimerControler TimeCo;
     6 int main()
     7 {
     8     initgraph(SCREENX, SCREENY);
     9     FullScreen();
    10     BeginBatchDraw();
    11     TimeCo.TimerControl();
    12     closegraph();
    13     return 0;
    14 }
    TIMER.cpp
  • 相关阅读:
    hihoCoder #1077 RMQ问题再临-线段树
    ms sql 获取字符串首字母
    如何设置gen_server在退出时执行相关操作
    C++拾遗
    [置顶] Linux下文件和目录权限说明
    Android百度地图之显示地图
    USACO March. 2012
    JNI之HelloWorld
    复习C语言系列二:动态调用函数指针数组
    HDU2527:Safe Or Unsafe(哈弗曼树)
  • 原文地址:https://www.cnblogs.com/SymenYang/p/5207907.html
Copyright © 2011-2022 走看看