游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1734|回复: 1

[求助]我的多线程动态加载TILE地形完成了,可是每次越界

[复制链接]

51

主题

134

帖子

140

积分

注册会员

Rank: 2

积分
140
发表于 2007-10-1 17:22:00 | 显示全部楼层 |阅读模式
        if(!(lox==_lox&&loz==_loz))//当位置越过当前TILE时
        {
                buffernode tempbuffernode[25];//用于保存当前位置的TILE
                bool is_jump_or_no[25];//是否已使用
                UINT nowi=0;//用于标记TEMPBUFFERNODE使用数量
                UINT nowi2=0;
                ::ZeroMemory(tempbuffernode,sizeof(buffernode)*25);
                ::ZeroMemory(is_jump_or_no,sizeof(bool)*25);
                for(UINT tx=0;tx<5;tx++)
                {
                        for(UINT ty=0;ty<5;ty++)
                        {
                                if(existnode[tx][ty])//如果TILES存在存入
                                {
                                        tempbuffernode[nowi].bused=true;//标记存在
                                        tempbuffernode[nowi].tnode=_node[tx][ty];
                                        _node[tx][ty]=NULL;
                                        tempbuffernode[nowi].wz=twz[tx][ty];//将当前位置写入tempbuffernode
                                        nowi++;
                                }
                                existnode[tx][ty]=false;//设置所有5*5的TILES不存在
                                twz[tx][ty].x=lox+tx-2;//重新计算当前的位置
                                twz[tx][ty].y=loz+ty-2;       
                        }
                }
                //查找TEMPBUFFERNODE中有没有需要的NODE
                UINT loxf2=lox-2;
                UINT loyf2=loz-2;
                for(int i=0;i<nowi;i++)
                {
                        if(tempbuffernode.bused)
                        {
                                UINT ttx=tempbuffernode.wz.x-loxf2;//计算此tempbuffernode的相对位置
                                UINT tty=tempbuffernode.wz.y-loyf2;
                                if(ttx<5&&tty<5)//如果相对位置可用,存入当前TILE
                                {
                                        existnode[ttx][tty]=true;
                                        _node[ttx][tty]=tempbuffernode.tnode;
                                        is_jump_or_no=true;
                                }
                        }
                        else
                                break;
                }//end查找TEMPBUFFERNODE中有没有需要的NODE
                //查找_BUFFERNODE中有没有需要的NODE
                for(int i=0;i<buffernode_size;i++)
                {
                        UINT ttx=_buffernode.wz.x-loxf2;
                        UINT tty=_buffernode.wz.y-loyf2;
                        if(_buffernode.bused)
                        {
                                if(ttx<5&&tty<5)
                                {
                                        _node[ttx][tty]=_buffernode.tnode;
                                        _buffernode.bused=false;
                                        _buffernode.tnode=NULL;
                                        existnode[ttx][tty]=true;
                                        break;
                                }
                        }
                }
                //如果还没有。。。
               
                for(int tx=0;tx<5;tx++)
                {
                        for(int ty=0;ty<5;ty++)
                        {
                                if(!existnode[tx][ty])//如果不存在这个TILE的相对位置
                                {
                                        if(twz[tx][ty].x>MAXTERRAINNUM-1||twz[tx][ty].y>MAXTERRAINNUM-1)
                                        {

                                        }
                                        else if(isfileexist[twz[tx][ty].x][twz[tx][ty].y])//表示这个位置的确存在
                                        {
                                                if(tx>0&&tx<4&&ty>0&&ty<4)//过果在3*3TILE内 返回错误
                                                {                                       
                                                        return false;
                                                }
                                                else
                                                {
                                                        //添加到线程列队中
                                                        buffernode tempcreatnode;
                                                        tempcreatnode.bused=true;
                                                        tempcreatnode.wz=twz[tx][ty];
                                                        pushin_creatthread(&tempcreatnode);
                                                }
                                        }
                                }
                        }
                }/////end如果还没有。。。
                zip_buffernode();//将_buffernode压缩
                comsize_buffernode();//重新计算_buffernode数量
                for(int i=0;i<nowi;i++)//计算剩下几个TEMPBUFFERNODE
                {
                        if(!is_jump_or_no)
                        {
                                nowi2++;
                        }
                }
                //将剩下的TEMPBUFFERNODE存入_BUFFERNODE
                //如果_BUFFERNODE已满,释放前面的几个
                if(buffernode_size>(24-nowi2))
                {
                        pushout_buffernode(nowi2+buffernode_size-24);//踢出到释放列队线程中
                }
                for(int i=0;i<nowi;i++)
                {
                        if(!is_jump_or_no)
                        {
                                pushin_buffernode(&(tempbuffernode));
                        }
                }
        }//else !(lox==_lox&&loz==_loz)

//一些函数

bool Terrain_Tile::pushin_buffernode(buffernode *tbn)
{
        if(!tbn->bused)
                return false;
        if(buffernode_size>24)
                return false;
        _buffernode[buffernode_size].bused=(*tbn).bused;
        _buffernode[buffernode_size].wz.x=(*tbn).wz.x;
        _buffernode[buffernode_size].wz.y=(*tbn).wz.y;
        _buffernode[buffernode_size].tnode=(*tbn).tnode;
        buffernode_size++;
        return true;
}

void Terrain_Tile::comsize_buffernode()
{
        buffernode_size=0;
        for(int i=0;i<25;i++)
        {
                if(!_buffernode.bused)
                        break;
                else
                        buffernode_size++;
        }
        return;
}

bool Terrain_Tile::pushin_creatthread(buffernode *tbn)
{
        if(creatthread_size>24)
        {
                for(int i=24;i>0;i--)
                {
                        buffernode ttbn;
                        ttbn=creatthread;
                        creatthread=creatthread[i-1];
                        creatthread[i-1]=ttbn;
                }
        }
        else
                creatthread_size++;
        creatthread[creatthread_size-1]=*tbn;
        return true;
}

void Terrain_Tile::zip_buffernode()
{
        buffernode tbn[25];
        ::ZeroMemory(tbn,25*sizeof(buffernode));
        int nowii=0;
        for(int i=0;i<25;i++)
        {
                if(_buffernode.bused)
                {
                        tbn[nowii]=_buffernode;
                        _buffernode.bused=false;
                        _buffernode.tnode=NULL;
                        nowii++;
                }
        }
        for(int i=0;i<25;i++)
        {
                _buffernode=tbn;
        }
}

bool Terrain_Tile::pushout_buffernode(UINT size)
{
        if(size>25)
                return false;

        for(int i=0;i<size;i++)
        {
                if(_buffernode.bused)
                {
                        if(destorythread_size<25)
                        {
                                pushin_destorythread(&_buffernode);
                                _buffernode.bused=false;
                                _buffernode.tnode=NULL;
                        }
                        else
                        {
                                _buffernode.bused=false;
                                if(!_buffernode.tnode==NULL)
                                {
                                        _buffernode.tnode->~Node_513();
                                }
                        }
                }
        }
        zip_buffernode();
}

44

主题

248

帖子

274

积分

中级会员

Rank: 3Rank: 3

积分
274
发表于 2007-10-4 13:06:00 | 显示全部楼层

Re:[求助]我的多线程动态加载TILE地形完成了,可是每次越

整个工程 都放上来这样才便于别人调试吧,

代码里似乎有很多magic number
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-20 18:32

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表