帮助我理解并修复此算法

时间:2009-11-16 05:35:14

标签: c++ algorithm

我写了以下内容,但是在修改它之后我不理解它,以适应单个像素(图形显示)而不是单个字符(字符显示)。

XRES x YRES是每个字符的像素分辨率。 LCDGraphic根据这些值绘制自己的字符。这个转换算法的想法是你可以向右,向左或(两个)向右一行,离开下一行,然后向右等等......文本版本就像它应该的那样工作,但当我翻译它时图形显示,它表现得很奇怪。

每次执行LCOLS时,

transition_tick_为256(sentinal),并且LCDGraphic::Transition()递增,直到此sentinel为止。因此col可以在0-255之间的范围内。好吧,当像素左右移动时,它们应该一起移动。然而,由于某种原因,直线移动直到它们完成,然后向左移动的线移动直到它们完成。似乎col < 128左侧移动线在调整,col>= 128时,右侧移动线会调整。我对此非常困惑。

void LCDGraphic::Transition() {
    int direction = visitor_->GetDirection();
    int col;
    transitioning_ = true;
    for(unsigned int row = 0; row < LROWS / YRES; row++) {
        if( direction == TRANSITION_LEFT ||
            (direction == TRANSITION_BOTH && row % 2 == 0))
            col = LCOLS - transition_tick_;
        else if( direction == TRANSITION_RIGHT || direction == TRANSITION_BOTH)
            col = transition_tick_;
        else
            col = 0;

        if(col < 0)
            col = 0;

        for(unsigned int i = 0; i < YRES; i++) {
            int n = row * YRES * LCOLS + i * LCOLS;
            for(unsigned int l = 0; l < 1; l++) {// LAYERS; l++) {
                RGBA tmp[LCOLS];
                memcpy(tmp + XRES, GraphicFB[l] + n + col + XRES, (LCOLS - col) * sizeof(RGBA));
                for(unsigned j = 0; j < XRES; j++)
                        tmp[j] = NO_COL;
                memcpy(GraphicFB[l] + n + col, tmp, sizeof(RGBA) * (LCOLS - col));
            }
        }    
    }

    transition_tick_+=XRES;
    if( transition_tick_ >= (int)LCOLS ) {
        transitioning_ = false;
        transition_tick_ = 0;
        emit static_cast<LCDEvents *>(
            visitor_->GetWrapper())->_TransitionFinished();
    }

    GraphicBlit(0, 0, LROWS, LCOLS);
}

1 个答案:

答案 0 :(得分:1)

我明白了。只有一半的LCOLS。虽然奇怪的问题。我还是有点困惑。

void LCDGraphic::Transition() {
    int direction = visitor_->GetDirection();
    int col;
    transitioning_ = true;
    for(unsigned int row = 0; row < LROWS / YRES; row++) {
        if( direction == TRANSITION_LEFT ||
            (direction == TRANSITION_BOTH && row % 2 == 0))
            col = LCOLS / 2 - transition_tick_; // changed this line
        else if( direction == TRANSITION_RIGHT || direction == TRANSITION_BOTH)
            col = transition_tick_;
        else
            col = 0;

        if(col < 0)
            col = 0;

        for(unsigned int i = 0; i < YRES; i++) {
            int n = row * YRES * LCOLS + i * LCOLS;
            for(unsigned int l = 0; l < 1; l++) {// LAYERS; l++) {
                RGBA tmp[LCOLS];
                LCDError("Transition: LROWS: %u, LCOLS: %u, n: %d, row: %d, col: %d, calc1: %d, calc2: %d, fb: %p, tmp: %p",
                    LROWS, LCOLS, n, row, col, n + col + XRES, (LCOLS - col) * sizeof(RGBA), GraphicFB, tmp);
                memcpy(tmp + XRES, GraphicFB[l] + n + col + XRES, (LCOLS - col) * sizeof(RGBA));
                for(unsigned j = 0; j < XRES; j++)
                        tmp[j] = NO_COL;
                memcpy(GraphicFB[l] + n + col, tmp, sizeof(RGBA) * (LCOLS - col));
            }
        }
    }

    transition_tick_+=XRES;
    if( transition_tick_ >= (int)LCOLS / 2) { //changed this line
        transitioning_ = false;
        transition_tick_ = 0;
        emit static_cast<LCDEvents *>(
            visitor_->GetWrapper())->_TransitionFinished();
    }

    GraphicBlit(0, 0, LROWS, LCOLS);
}