android c ++在循环中更新二维数组

时间:2018-08-30 20:23:49

标签: android c++ arrays

我正在编写一个手指绘画应用程序,并且用C ++实现了手指绘画。当我用手指移动的新x和y值更新MAIN 2d数组时,一切缓慢,但一切正常,但是当我这样做时,应用崩溃。从我的小小的研究中,我发现问题在于数组的快速更新,而我在renderLine方法中遍历数组的方式类似于在“ java并发修改异常”中。下面是我的代码。请,我需要知道一种在遍历数组时更新数组的更好方法。

            #include "Pencil.h"
            #include "glm.hpp"
            #include "gtc/matrix_transform.hpp"
            #include "gtc/type_ptr.hpp"
            #include "Logger.h"

            #define LOG_TAG "FINGER PAINT CLASS"

            using namespace std;
            GLfloat colors[4];
            vector<vector<shared_ptr<mPath>>>MAIN(12);
            int PageNo = 0;
            Pen::Pen()
            {
                colors[0] = 1.f;
                colors[1] = 0.f;
                colors[2] = 0.f;
                colors[3] = 1.f;
                SNAP = false;
            path = make_shared<mPath>();
                mx = 0.f;
                my = 0.f;
            }
            Pen::~Pen()
            {
                undoMain.clear();
                vector<shared_ptr<mPath>>(undoMain.begin(),undoMain.end()).swap(undoMain);
                MAIN.clear();
                vector<vector<shared_ptr<mPath>>>(MAIN.begin(),MAIN.end()).swap(MAIN);
            }

            void Pen::onFingerDown(float x1, float y1)
            {
              MAIN[PageNo].push_back(path);
                mx = x1;
                my = y1;
                tempPoints.x1=x1;
                tempPoints.y1=y1;
                tempPoints.x2=x1;
                tempPoints.y2=y1;
                path->addVert(tempPoints);
            }

            void Pen::onFingerMove(float x, float y)
            {
                tempPoints.x1=mx;
                tempPoints.y1=my;
                tempPoints.x2=x;
                tempPoints.y2=y;
                path->addVert(tempPoints);
                mx=x;
                 my=y;
            }

            void Pen::onFingerUp()
            {
            path = make_shared<mPath>();
            }

            void Pen::renderLine(const GLuint &program, const glm::mat4 mat)
            {
                GLint perspective=glGetUniformLocation(program,"model");
                glUniformMatrix4fv(perspective,1,GL_FALSE,glm::value_ptr(mat));
                for(auto& p:MAIN[PageNo]){
                    for(auto& m:p->getPath()){
                            GLint vertex=glGetAttribLocation(program,"vertexPos");
                            GLint colo=glGetUniformLocation(program,"vColor");
                            glEnableVertexAttribArray(vertex);
                            glVertexAttribPointer(vertex,2,GL_FLOAT,GL_FALSE,0,&m);
                            glUniform4fv(colo,1,colors);
                            glLineWidth(30.f);

                            glDrawArrays(GL_LINES,0,2);
                            glDisableVertexAttribArray(vertex);
                    }
                }
            }
            mPath::mPath(){}
            mPath::~mPath() {
                mlineHolder.clear();
            }
            void mPath::addVert(fingerPos vert)
            {

                mlineHolder.push_back(vert);
            }
            vector<fingerPos> mPath::getPath()
            {
                return mlineHolder;
            }

0 个答案:

没有答案