首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >快板5:瓷砖地图->滚动滞后

快板5:瓷砖地图->滚动滞后
EN

Stack Overflow用户
提问于 2016-01-29 09:42:37
回答 2查看 353关注 0票数 1

我现在正在做一个小游戏

现在,我已经通过平铺地图编辑器制作了一张地图,并通过al_draw_bitmap_region循环将其放到屏幕上。地图显示在屏幕上,但是当我试图滚动视图时,需要很长时间才能向前移动。这可能是由快板4中的“大”地图(100x100) ->造成的,我也曾使用过这个方法,它对我来说很好(我曾经把doubleBuffer中的所有东西和屏幕上的blit混合起来)。

然而,到目前为止,我似乎还没有找到解决我的问题的方法。我很抱歉,如果像这样的板已经打开了,到目前为止,我还没有找到解决问题的办法。

我真的很感激你的帮助!

代码:

main.cpp:

代码语言:javascript
运行
复制
#include<allegro5\allegro.h>
#include<allegro5\allegro_native_dialog.h>
#include<allegro5\allegro_primitives.h>
#include<allegro5\allegro_image.h>
#include<allegro5\allegro_font.h>
#include<allegro5\allegro_ttf.h>
#include "global.h"
#include "map.h"

using namespace std;



int main()
{

    ALLEGRO_DISPLAY *display = NULL;
    al_init_primitives_addon();
    al_init_image_addon();
    al_init_font_addon();
    al_init_ttf_addon();
    al_init();

    ALLEGRO_FONT *font18 = al_load_font("arial.ttf", 18, 0);

    if (!al_init())
    {
        al_show_native_message_box(NULL,NULL,NULL, "Could not initialize Allegro 5", NULL, NULL);
    }


    bool keys[] = {false, false, false, false};
    enum KEYS{UP,DOWN,LEFT,RIGHT};

    ALLEGRO_TIMER *timer = NULL;

    bool done=false;
    bool reDraw=true;
    int xOff =0;
    int yOff =0;
    int count = 0;
    int FPS = 5;
    int x=10, y=10;
    int moveSpeed = 5;


    display = al_create_display(ScreenWidth,ScreenHeight);

    if(!display)
        al_show_native_message_box(NULL,NULL,NULL, "Could not create Allegro Window", NULL,NULL);


    al_set_window_title(display,"Outfall-RPG");

    al_install_keyboard();
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;

    event_queue = al_create_event_queue();
    timer = al_create_timer(1.0 / FPS);             // 1.0 = Sekunden; / FPS = Frames per Second

    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_timer_event_source(timer));

    al_start_timer(timer);



    while(!done)
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);

        map map;
        map.Init();
        map.LoadMap("map1.txt");


        if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            switch(ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                    done=true;
                    break;
                case ALLEGRO_KEY_LEFT:
                    keys[LEFT] = true;
                    break;
                case ALLEGRO_KEY_RIGHT:
                    keys[RIGHT] = true;
                    break;
                case ALLEGRO_KEY_UP:
                    keys[UP] = true;
                    break;
                case ALLEGRO_KEY_DOWN:
                    keys[DOWN] = true;
                    break;
            }
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch(ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                    done=true;
                    break;
                case ALLEGRO_KEY_LEFT:
                    keys[LEFT] = false;
                    break;
                case ALLEGRO_KEY_RIGHT:
                    keys[RIGHT] = false;
                    break;
                case ALLEGRO_KEY_UP:
                    keys[UP] = false;
                    break;
                case ALLEGRO_KEY_DOWN:
                    keys[DOWN] = false;
                    break;
                }
        }
        else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            break;
        }
        else if(ev.type == ALLEGRO_EVENT_TIMER)
        {
            xOff -= keys[RIGHT] * 3;
            xOff += keys[LEFT] * 3;
            yOff -= keys[DOWN] * 3;
            yOff += keys[UP] * 3;

            reDraw=true;
        }
        if(reDraw && al_is_event_queue_empty(event_queue))
        {
            map.Draw(xOff, yOff);
            //al_set_target_bitmap(al_get_backbuffer(display));
            al_flip_display();
            al_clear_to_color(al_map_rgb(0,0,0));



            reDraw=false;

        }
    }

    al_destroy_event_queue(event_queue);
    al_destroy_timer(timer);
    al_destroy_display(display);
    return 0;
}

map.h

代码语言:javascript
运行
复制
#include <fstream>
#include <string>
#include <algorithm>
#include <iostream>
#include "global.h"
#include<allegro5\allegro.h>
#include<allegro5\allegro_native_dialog.h>
#include<allegro5\allegro_primitives.h>

using namespace std;

class map
{
    public:
        map();
        ~map();

        void Init();
        void Update();
        ALLEGRO_BITMAP  *buffer;

        void LoadMap(const char *filename);
        void Draw(int &xOff, int &yOff);

    private:
        int loadCounterX, loadCounterY;
        int mapSizeX, mapSizeY;
        int MapFile[200][200];
        int BlockSize;

        ALLEGRO_BITMAP  *bgSheet;



        int mapColumns;
        int mapSize;
        int y;
        int sheetSize;

};

map.cpp:

代码语言:javascript
运行
复制
#include "map.h"

map::map()
{
    mapSizeX = 101;
    mapSizeY = 101;
    y=0;
    sheetSize = 13;
}

map::~map()
{
}

void map::Init()
{
    loadCounterX = loadCounterY = 0;
    map::LoadMap("maptest.txt");
    BlockSize = 32;
    int MapFile[100][100];
    mapColumns = 100;
    mapSize = 1000;

}

void map::Update()
{
}

void map::Draw(int &x, int &q)
{
    int xOff = x;
    int yOff = q;
    bgSheet = al_load_bitmap("bilder/testas1.png");
    buffer = al_create_bitmap(800,600);
    //al_set_target_bitmap(buffer);


        for (int i=0; i<mapSizeX; i++)
        {
            for (int j=0; j<mapSizeY; j++)
            {
                y=0;

                while(MapFile[i][j] > sheetSize)
                {
                    MapFile[i][j] = MapFile[i][j] - sheetSize;  
                    y++;
                }

                // al_draw_bitmap_region(bgSheet, BlockSize*(MapFile[i][j]-1),BlockSize*y, BlockSize, BlockSize, xOff + BlockSize * (i%mapColumns), yOff + BlockSize * (j%mapColumns),0);
                //al_set_target_bitmap(buffer);
                al_draw_bitmap_region(bgSheet, BlockSize*(MapFile[i][j]-1),BlockSize*y, BlockSize, BlockSize, xOff + (BlockSize *i), yOff + (BlockSize * j),0);


                //al_set_target_backbuffer(display);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                // Generell gucken, wie ich schneller BITMAPS abbilden kann!
                //ALLEGRO_BITMAP *al_get_backbuffer(ALLEGRO_DISPLAY *display)
                //  al_set_target_bitmap(display);

                // Nur abgebildeter Bereich + Puffer blitten (20x20?)

            }
        }
}

void map::LoadMap(const char *filename)
{

    ifstream openfile(filename);
    if (openfile.is_open())
    {
        string line;
        getline(openfile, line);
        line.erase(remove(line.begin(),line.end(),' '), line.end());
        mapSizeX = 100;

        openfile.seekg(0,ios::beg);
        //openfile >> mapSizeX >> mapSizeY;

        while(!openfile.eof())
        {
            openfile >> MapFile[loadCounterX][loadCounterY];
            loadCounterX ++;

            if (loadCounterX >= mapSizeX)
            {
                loadCounterX = 0;
                loadCounterY++;
            }
        }

        mapSizeY = loadCounterY;
    }   //File is Opend

    else
    {
        //al_show_native_message_box(NULL,NULL,NULL, "Could not load Map", NULL,NULL);
    }
}

使用的瓷砖:

http://i.stack.imgur.com/k4DbZ.png

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-29 12:42:24

map::Draw的这些行是可疑的:

代码语言:javascript
运行
复制
bgSheet = al_load_bitmap("bilder/testas1.png");
buffer = al_create_bitmap(800,600);

每次调用map::Draw时,都会重新加载位图。这不仅会给抽签循环带来不必要的负载,而且每次都会泄漏加载的位图。

尝试只在bgSheet中加载map::Init一次,并对每个抽签循环使用相同的位图。

您还为每个抽签调用分配并随后泄漏一个新的buffer。您似乎没有使用缓冲区,所以我将完全删除该al_create_bitmap调用。如果您的目的是双缓冲,这在allegro5中通常不需要。

而且,看起来在游戏循环中调用了map::Init,这意味着您正在重复加载映射,并且可能会在游戏循环开始之前花费大量时间等待I/O调用map::Init一次。

票数 0
EN

Stack Overflow用户

发布于 2016-01-29 13:11:41

实际上,正是map.init因为重新加载了映射文件而导致延迟!

谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35081254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档