2

此函数中的代码是否过于具体而无法使用循环进行重构?没有我能想到的算法可以准确地重现所需的结果。

bool Mouse::CreateDefaultImage() {
    if(_defaultImage != nullptr) return false;

    const int DEPTH = 32;
    const int WIDTH = 11;
    const int HEIGHT = 19;
    _defaultImage = create_bitmap_ex(DEPTH, WIDTH, HEIGHT);
    if(_defaultImage == nullptr) return false;

    const int BLACK = 0x000000;
    const int WHITE = 0xFFFFFF;
    const int PINK = 0xFF00FF;

    //Color bitmap to transparent
    clear_to_color(_defaultImage, PINK);

    //Only color areas that are NOT transparent.
    //Each block is a row of pixels.

    /************************************************************************
     * SHOULD PRODUCE THIS RESULT   * = BLACK; 0 = WHITE; - = PINK          *
     * *----------                                                          *
     * **---------                                                          *
     * *0*--------                                                          *
     * *00*-------                                                          *
     * *000*------                                                          *
     * *0000*-----                                                          *
     * *00000*----                                                          *
     * *000000*---                                                          *
     * *0000000*--                                                          *
     * *00000000*-                                                          *
     * *00000*****                                                          *
     * *00*00*----                                                          *
     * *0*-*00*---                                                          *
     * **--*00*---                                                          *
     * *----*00*--                                                          *
     * -----*00*--                                                          *
     * ------*00*-                                                          *
     * ------*00*-                                                          *
     * -------**--                                                          *
     ************************************************************************/

    ((long *)_defaultImage->line[0])[0] = BLACK;
    //((long *)_defaultImage->line[0])[1] = PINK;
    //((long *)_defaultImage->line[0])[2] = PINK;
    //((long *)_defaultImage->line[0])[3] = PINK;
    //((long *)_defaultImage->line[0])[4] = PINK;
    //((long *)_defaultImage->line[0])[5] = PINK;
    //((long *)_defaultImage->line[0])[6] = PINK;
    //((long *)_defaultImage->line[0])[7] = PINK;
    //((long *)_defaultImage->line[0])[8] = PINK;
    //((long *)_defaultImage->line[0])[9] = PINK;
    //((long *)_defaultImage->line[0])[10] = PINK;

    ((long *)_defaultImage->line[1])[0] = BLACK;
    ((long *)_defaultImage->line[1])[1] = BLACK;
    //((long *)_defaultImage->line[1])[2] = PINK;
    //((long *)_defaultImage->line[1])[3] = PINK;
    //((long *)_defaultImage->line[1])[4] = PINK;
    //((long *)_defaultImage->line[1])[5] = PINK;
    //((long *)_defaultImage->line[1])[6] = PINK;
    //((long *)_defaultImage->line[1])[7] = PINK;
    //((long *)_defaultImage->line[1])[8] = PINK;
    //((long *)_defaultImage->line[1])[9] = PINK;
    //((long *)_defaultImage->line[1])[10] = PINK;


    ((long *)_defaultImage->line[2])[0] = BLACK;
    ((long *)_defaultImage->line[2])[1] = WHITE;
    ((long *)_defaultImage->line[2])[2] = BLACK;
    //((long *)_defaultImage->line[2])[3] = PINK;
    //((long *)_defaultImage->line[2])[4] = PINK;
    //((long *)_defaultImage->line[2])[5] = PINK;
    //((long *)_defaultImage->line[2])[6] = PINK;
    //((long *)_defaultImage->line[2])[7] = PINK;
    //((long *)_defaultImage->line[2])[8] = PINK;
    //((long *)_defaultImage->line[2])[9] = PINK;
    //((long *)_defaultImage->line[2])[10] = PINK;

    ((long *)_defaultImage->line[3])[0] = BLACK;
    ((long *)_defaultImage->line[3])[1] = WHITE;
    ((long *)_defaultImage->line[3])[2] = WHITE;
    ((long *)_defaultImage->line[3])[3] = BLACK;
    //((long *)_defaultImage->line[3])[4] = PINK;
    //((long *)_defaultImage->line[3])[5] = PINK;
    //((long *)_defaultImage->line[3])[6] = PINK;
    //((long *)_defaultImage->line[3])[7] = PINK;
    //((long *)_defaultImage->line[3])[8] = PINK;
    //((long *)_defaultImage->line[3])[9] = PINK;
    //((long *)_defaultImage->line[3])[10] = PINK;

    ((long *)_defaultImage->line[4])[0] = BLACK;
    ((long *)_defaultImage->line[4])[1] = WHITE;
    ((long *)_defaultImage->line[4])[2] = WHITE;
    ((long *)_defaultImage->line[4])[3] = WHITE;
    ((long *)_defaultImage->line[4])[4] = BLACK;
    //((long *)_defaultImage->line[4])[5] = PINK;
    //((long *)_defaultImage->line[4])[6] = PINK;
    //((long *)_defaultImage->line[4])[7] = PINK;
    //((long *)_defaultImage->line[4])[8] = PINK;
    //((long *)_defaultImage->line[4])[9] = PINK;
    //((long *)_defaultImage->line[4])[10] = PINK;

    ((long *)_defaultImage->line[5])[0] = BLACK;
    ((long *)_defaultImage->line[5])[1] = WHITE;
    ((long *)_defaultImage->line[5])[2] = WHITE;
    ((long *)_defaultImage->line[5])[3] = WHITE;
    ((long *)_defaultImage->line[5])[4] = WHITE;
    ((long *)_defaultImage->line[5])[5] = BLACK;
    //((long *)_defaultImage->line[5])[6] = PINK;
    //((long *)_defaultImage->line[5])[7] = PINK;
    //((long *)_defaultImage->line[5])[8] = PINK;
    //((long *)_defaultImage->line[5])[9] = PINK;
    //((long *)_defaultImage->line[5])[10] = PINK;

    ((long *)_defaultImage->line[6])[0] = BLACK;
    ((long *)_defaultImage->line[6])[1] = WHITE;
    ((long *)_defaultImage->line[6])[2] = WHITE;
    ((long *)_defaultImage->line[6])[3] = WHITE;
    ((long *)_defaultImage->line[6])[4] = WHITE;
    ((long *)_defaultImage->line[6])[5] = WHITE;
    ((long *)_defaultImage->line[6])[6] = BLACK;
    //((long *)_defaultImage->line[6])[7] = PINK;
    //((long *)_defaultImage->line[6])[8] = PINK;
    //((long *)_defaultImage->line[6])[9] = PINK;
    //((long *)_defaultImage->line[6])[10] = PINK;

    ((long *)_defaultImage->line[7])[0] = BLACK;
    ((long *)_defaultImage->line[7])[1] = WHITE;
    ((long *)_defaultImage->line[7])[2] = WHITE;
    ((long *)_defaultImage->line[7])[3] = WHITE;
    ((long *)_defaultImage->line[7])[4] = WHITE;
    ((long *)_defaultImage->line[7])[5] = WHITE;
    ((long *)_defaultImage->line[7])[6] = WHITE;
    ((long *)_defaultImage->line[7])[7] = BLACK;
    //((long *)_defaultImage->line[7])[8] = PINK;
    //((long *)_defaultImage->line[7])[9] = PINK;
    //((long *)_defaultImage->line[7])[10] = PINK;

    ((long *)_defaultImage->line[8])[0] = BLACK;
    ((long *)_defaultImage->line[8])[1] = WHITE;
    ((long *)_defaultImage->line[8])[2] = WHITE;
    ((long *)_defaultImage->line[8])[3] = WHITE;
    ((long *)_defaultImage->line[8])[4] = WHITE;
    ((long *)_defaultImage->line[8])[5] = WHITE;
    ((long *)_defaultImage->line[8])[6] = WHITE;
    ((long *)_defaultImage->line[8])[7] = WHITE;
    ((long *)_defaultImage->line[8])[8] = BLACK;
    //((long *)_defaultImage->line[8])[9] = PINK;
    //((long *)_defaultImage->line[8])[10] = PINK;

    ((long *)_defaultImage->line[9])[0] = BLACK;
    ((long *)_defaultImage->line[9])[1] = WHITE;
    ((long *)_defaultImage->line[9])[2] = WHITE;
    ((long *)_defaultImage->line[9])[3] = WHITE;
    ((long *)_defaultImage->line[9])[4] = WHITE;
    ((long *)_defaultImage->line[9])[5] = WHITE;
    ((long *)_defaultImage->line[9])[6] = WHITE;
    ((long *)_defaultImage->line[9])[7] = WHITE;
    ((long *)_defaultImage->line[9])[8] = WHITE;
    ((long *)_defaultImage->line[9])[9] = BLACK;
    //((long *)_defaultImage->line[9])[10] = PINK;

    ((long *)_defaultImage->line[10])[0] = BLACK;
    ((long *)_defaultImage->line[10])[1] = WHITE;
    ((long *)_defaultImage->line[10])[2] = WHITE;
    ((long *)_defaultImage->line[10])[3] = WHITE;
    ((long *)_defaultImage->line[10])[4] = WHITE;
    ((long *)_defaultImage->line[10])[5] = WHITE;
    ((long *)_defaultImage->line[10])[6] = BLACK;
    ((long *)_defaultImage->line[10])[7] = BLACK;
    ((long *)_defaultImage->line[10])[8] = BLACK;
    ((long *)_defaultImage->line[10])[9] = BLACK;
    ((long *)_defaultImage->line[10])[10] = BLACK;

    ((long *)_defaultImage->line[11])[0] = BLACK;
    ((long *)_defaultImage->line[11])[1] = WHITE;
    ((long *)_defaultImage->line[11])[2] = WHITE;
    ((long *)_defaultImage->line[11])[3] = BLACK;
    ((long *)_defaultImage->line[11])[4] = WHITE;
    ((long *)_defaultImage->line[11])[5] = WHITE;
    ((long *)_defaultImage->line[11])[6] = BLACK;
    //((long *)_defaultImage->line[11])[7] = PINK;
    //((long *)_defaultImage->line[11])[8] = PINK;
    //((long *)_defaultImage->line[11])[9] = PINK;
    //((long *)_defaultImage->line[11])[10] = PINK;

    ((long *)_defaultImage->line[12])[0] = BLACK;
    ((long *)_defaultImage->line[12])[1] = WHITE;
    ((long *)_defaultImage->line[12])[2] = BLACK;
    //((long *)_defaultImage->line[12])[3] = PINK;
    ((long *)_defaultImage->line[12])[4] = BLACK;
    ((long *)_defaultImage->line[12])[5] = WHITE;
    ((long *)_defaultImage->line[12])[6] = WHITE;
    ((long *)_defaultImage->line[12])[7] = BLACK;
    //((long *)_defaultImage->line[12])[8] = PINK;
    //((long *)_defaultImage->line[12])[9] = PINK;
    //((long *)_defaultImage->line[12])[10] = PINK;

    ((long *)_defaultImage->line[13])[0] = BLACK;
    ((long *)_defaultImage->line[13])[1] = BLACK;
    //((long *)_defaultImage->line[13])[2] = PINK;
    //((long *)_defaultImage->line[13])[3] = PINK;
    ((long *)_defaultImage->line[13])[4] = BLACK;
    ((long *)_defaultImage->line[13])[5] = WHITE;
    ((long *)_defaultImage->line[13])[6] = WHITE;
    ((long *)_defaultImage->line[13])[7] = BLACK;
    //((long *)_defaultImage->line[13])[8] = PINK;
    //((long *)_defaultImage->line[13])[9] = PINK;
    //((long *)_defaultImage->line[13])[10] = PINK;

    ((long *)_defaultImage->line[14])[0] = BLACK;
    //((long *)_defaultImage->line[14])[1] = PINK;
    //((long *)_defaultImage->line[14])[2] = PINK;
    //((long *)_defaultImage->line[14])[3] = PINK;
    //((long *)_defaultImage->line[14])[4] = PINK;
    ((long *)_defaultImage->line[14])[5] = BLACK;
    ((long *)_defaultImage->line[14])[6] = WHITE;
    ((long *)_defaultImage->line[14])[7] = WHITE;
    ((long *)_defaultImage->line[14])[8] = BLACK;
    //((long *)_defaultImage->line[14])[9] = PINK;
    //((long *)_defaultImage->line[14])[10] = PINK;

    //((long *)_defaultImage->line[15])[0] = PINK;
    //((long *)_defaultImage->line[15])[1] = PINK;
    //((long *)_defaultImage->line[15])[2] = PINK;
    //((long *)_defaultImage->line[15])[3] = PINK;
    //((long *)_defaultImage->line[15])[4] = PINK;
    ((long *)_defaultImage->line[15])[5] = BLACK;
    ((long *)_defaultImage->line[15])[6] = WHITE;
    ((long *)_defaultImage->line[15])[7] = WHITE;
    ((long *)_defaultImage->line[15])[8] = BLACK;
    //((long *)_defaultImage->line[15])[9] = PINK;
    //((long *)_defaultImage->line[15])[10] = PINK;

    //((long *)_defaultImage->line[16])[0] = PINK;
    //((long *)_defaultImage->line[16])[1] = PINK;
    //((long *)_defaultImage->line[16])[2] = PINK;
    //((long *)_defaultImage->line[16])[3] = PINK;
    //((long *)_defaultImage->line[16])[4] = PINK;
    //((long *)_defaultImage->line[16])[5] = PINK;
    ((long *)_defaultImage->line[16])[6] = BLACK;
    ((long *)_defaultImage->line[16])[7] = WHITE;
    ((long *)_defaultImage->line[16])[8] = WHITE;
    ((long *)_defaultImage->line[16])[9] = BLACK;
    //((long *)_defaultImage->line[16])[10] = PINK;

    //((long *)_defaultImage->line[17])[0] = PINK;
    //((long *)_defaultImage->line[17])[1] = PINK;
    //((long *)_defaultImage->line[17])[2] = PINK;
    //((long *)_defaultImage->line[17])[3] = PINK;
    //((long *)_defaultImage->line[17])[4] = PINK;
    //((long *)_defaultImage->line[17])[5] = PINK;
    ((long *)_defaultImage->line[17])[6] = BLACK;
    ((long *)_defaultImage->line[17])[7] = WHITE;
    ((long *)_defaultImage->line[17])[8] = WHITE;
    ((long *)_defaultImage->line[17])[9] = BLACK;
    //((long *)_defaultImage->line[17])[10] = PINK;

    //((long *)_defaultImage->line[18])[0] = PINK;
    //((long *)_defaultImage->line[18])[1] = PINK;
    //((long *)_defaultImage->line[18])[2] = PINK;
    //((long *)_defaultImage->line[18])[3] = PINK;
    //((long *)_defaultImage->line[18])[4] = PINK;
    //((long *)_defaultImage->line[18])[5] = PINK;
    //((long *)_defaultImage->line[18])[6] = PINK;
    ((long *)_defaultImage->line[18])[7] = BLACK;
    ((long *)_defaultImage->line[18])[8] = BLACK;
    //((long *)_defaultImage->line[18])[9] = PINK;
    //((long *)_defaultImage->line[18])[10] = PINK;


    return true;
}
4

1 回答 1

6

什么 templatetypedef 说:

const char *up_left = 
  "*----------"
  "**---------"
  "*0*--------"
  "*00*-------"
  "*000*------"
  // etc
;

for (int r = 0; i < HEIGHT; ++r) {
  for (int c = 0; c < WIDTH; ++c) {
    switch(up_left[r * WIDTH + c]) {
      case '*':
        ((long *)_defaultImage->line[r])[c] = BLACK;
        break;
      case '0'):
        ((long *)_defaultImage->line[r])[c] = WHITE;
        break;
    }
  }
}

唯一需要担心的是,如果你的内存很紧,一个大部分是粉红色的图像最终是否会比字符串中每个像素 1 个字节的代码“压缩”更少。如果是这样,您可以发明非常简单的字节码来压缩图像(可能只是运行长度编码),以及将正确像素写入图像的解释器。

于 2012-07-07T01:35:23.883 回答