SDL侧卷轴不一致滚动

时间:2010-05-19 02:01:05

标签: c++ sdl

所以我正在从我以前的项目(我在这里发布以进行代码审查)进行升级,这次实现重复背景(就像卡通上使用的那样),这样SDL就不必加载非常大的图像一个级别。 然而,程序中存在一个奇怪的不一致:用户第一次向右滚动时,显示的面板比指定的少。向后(向左)显示正确数量的面板(即面板重复代码中指定的次数)。在此之后,似乎再次向右移动(一直在左侧),显示正确数量的面板并向后移动。这是一些选定的代码,这里是.zip of all my code

构造

Game::Game(SDL_Event* event, SDL_Surface* scr, int level_w, int w, int h, int bpp) {
    this->event = event;
    this->bpp = bpp;
    level_width = level_w;
    screen = scr;
    w_width = w;
    w_height = h;

    //load images and set rects
    background = format_surface("background.jpg");
    person = format_surface("person.png");

    background_rect_left = background->clip_rect;
    background_rect_right = background->clip_rect;
    current_background_piece = 1; //we are displaying the first clip
    rect_in_view = &background_rect_right;
    other_rect = &background_rect_left;

    person_rect = person->clip_rect;

    background_rect_left.x = 0; background_rect_left.y = 0;
    background_rect_right.x = background->w; background_rect_right.y = 0;
    person_rect.y = background_rect_left.h - person_rect.h;
    person_rect.x = 0;
}

这里的移动方法可能会造成所有麻烦:

void Game::move(SDLKey direction) {
if(direction == SDLK_RIGHT) {
    if(move_screen(direction)) {
        if(!background_reached_right()) {
            //move background right

            background_rect_left.x += movement_increment;
            background_rect_right.x += movement_increment;

            if(rect_in_view->x >= 0) {
                //move the other rect in to fill the empty space
                SDL_Rect* temp;

                other_rect->x = -w_width + rect_in_view->x;

                temp = rect_in_view;
                rect_in_view = other_rect;
                other_rect = temp;

                current_background_piece++;
                std::cout << current_background_piece << std::endl;
            }

            if(background_overshoots_right()) {
                //sees if this next blit is past the surface
                //this is used only for re-aligning the rects when 
                //the end of the screen is reached

                background_rect_left.x = 0;
                background_rect_right.x = w_width;
            }
        }
    }
    else {
        //move the person instead

        person_rect.x += movement_increment;
        if(get_person_right_side() > w_width) {
            //person went too far right

            person_rect.x = w_width - person_rect.w;
        }
    }
}

else if(direction == SDLK_LEFT) {
    if(move_screen(direction)) {
        if(!background_reached_left()) {
            //moves background left

            background_rect_left.x -= movement_increment;
            background_rect_right.x -= movement_increment;

            if(rect_in_view->x <= -w_width) {
                //swap the rect in view
                SDL_Rect* temp;

                rect_in_view->x = w_width;

                temp = rect_in_view;
                rect_in_view = other_rect;
                other_rect = temp;

                current_background_piece--;
                std::cout << current_background_piece << std::endl;
            }

            if(background_overshoots_left()) {
                background_rect_left.x = 0;
                background_rect_right.x = w_width;
            }
        }
    }
    else {
        //move the person instead

        person_rect.x -= movement_increment;
        if(person_rect.x < 0) {
            //person went too far left

            person_rect.x = 0;
        }
    }
}
}

没有剩下的代码,这没有多大意义。由于它太多了,我会上传它here进行测试。无论如何,有谁知道如何解决这种不一致问题?

1 个答案:

答案 0 :(得分:0)

打印出代表您认为可能怀疑的数据的值,并确保它们改变您认为应该的方式。这听起来像一个非常典型的fencepost error