1 /*
2  *  The ManaPlus Client
3  *  Copyright (C) 2011-2019  The ManaPlus Developers
4  *  Copyright (C) 2019-2021  Andrei Karas
5  *
6  *  This file is part of The ManaPlus Client.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifdef USE_OPENGL
23 
24 #include "render/vertexes/openglgraphicsvertexes.h"
25 
26 #include "render/graphics.h"
27 
28 #include "debug.h"
29 
30 unsigned int vertexBufSize = 500;
31 
OpenGLGraphicsVertexes()32 OpenGLGraphicsVertexes::OpenGLGraphicsVertexes() :
33     ptr(0),
34     mFloatTexArray(nullptr),
35     mIntTexArray(nullptr),
36     mIntVertArray(nullptr),
37     mShortVertArray(nullptr),
38     mVp(),
39     mFloatTexPool(),
40     mIntVertPool(),
41     mShortVertPool(),
42     mIntTexPool(),
43     mVbo()
44 {
45     mFloatTexPool.reserve(30);
46     mIntVertPool.reserve(30);
47     mShortVertPool.reserve(30);
48     mIntTexPool.reserve(30);
49     mVp.reserve(30);
50     mVbo.reserve(30);
51 }
52 
~OpenGLGraphicsVertexes()53 OpenGLGraphicsVertexes::~OpenGLGraphicsVertexes()
54 {
55     clear();
56 }
57 
clear()58 void OpenGLGraphicsVertexes::clear() restrict2
59 {
60     for (STD_VECTOR<GLfloat*>::iterator it = mFloatTexPool.begin();
61         it != mFloatTexPool.end(); ++ it)
62     {
63         delete [] (*it);
64     }
65     mFloatTexPool.clear();
66 
67     for (STD_VECTOR<GLint*>::iterator it = mIntVertPool.begin();
68         it != mIntVertPool.end(); ++ it)
69     {
70         delete [] (*it);
71     }
72     mIntVertPool.clear();
73 
74     for (STD_VECTOR<GLshort*>::iterator it = mShortVertPool.begin();
75         it != mShortVertPool.end(); ++ it)
76     {
77         delete [] (*it);
78     }
79     mShortVertPool.clear();
80 
81     for (STD_VECTOR<GLint*>::iterator it = mIntTexPool.begin();
82         it != mIntTexPool.end(); ++ it)
83     {
84         delete [] (*it);
85     }
86     mIntTexPool.clear();
87 
88     const int sz = CAST_S32(mVbo.size());
89     if (sz > 0)
90     {
91         mainGraphics->removeArray(sz, &mVbo[0]);
92         mVbo.clear();
93     }
94 
95     mVp.clear();
96     if (ptr != 0)
97     {
98         ptr = 0;
99         delete []mFloatTexArray;
100         mFloatTexArray = nullptr;
101         delete []mIntTexArray;
102         mIntTexArray = nullptr;
103         delete []mIntVertArray;
104         mIntVertArray = nullptr;
105         delete []mShortVertArray;
106         mShortVertArray = nullptr;
107     }
108 }
109 
init()110 void OpenGLGraphicsVertexes::init() restrict2
111 {
112     clear();
113 }
114 
switchFloatTexArray()115 GLfloat *OpenGLGraphicsVertexes::switchFloatTexArray() restrict2
116 {
117     mFloatTexArray = new GLfloat[CAST_SIZE(vertexBufSize * 4 + 30)];
118     mFloatTexPool.push_back(mFloatTexArray);
119     return mFloatTexArray;
120 }
121 
switchIntVertArray()122 GLint *OpenGLGraphicsVertexes::switchIntVertArray() restrict2
123 {
124     mIntVertArray = new GLint[CAST_SIZE(vertexBufSize * 4 + 30)];
125     mIntVertPool.push_back(mIntVertArray);
126     return mIntVertArray;
127 }
128 
switchShortVertArray()129 GLshort *OpenGLGraphicsVertexes::switchShortVertArray() restrict2
130 {
131     mShortVertArray = new GLshort[CAST_SIZE(vertexBufSize * 4 + 30)];
132     mShortVertPool.push_back(mShortVertArray);
133     return mShortVertArray;
134 }
135 
switchIntTexArray()136 GLint *OpenGLGraphicsVertexes::switchIntTexArray() restrict2
137 {
138     mIntTexArray = new GLint[CAST_SIZE(vertexBufSize * 4 + 30)];
139     mIntTexPool.push_back(mIntTexArray);
140     return mIntTexArray;
141 }
142 
switchVp(const int n)143 void OpenGLGraphicsVertexes::switchVp(const int n) restrict2
144 {
145     mVp.push_back(n);
146 }
147 
continueVp()148 int OpenGLGraphicsVertexes::continueVp() restrict2
149 {
150     if (mVp.empty())
151     {
152         return 0;
153     }
154     else
155     {
156         const int val = mVp.back();
157         mVp.pop_back();
158         return val;
159     }
160 }
161 
updateVp(const int n)162 void OpenGLGraphicsVertexes::updateVp(const int n) restrict2
163 {
164     if (!mVp.empty())
165         mVp.pop_back();
166     mVp.push_back(n);
167 }
168 
continueFloatTexArray()169 GLfloat *OpenGLGraphicsVertexes::continueFloatTexArray() restrict2
170 {
171     if (mFloatTexPool.empty())
172     {
173         mFloatTexArray = new GLfloat[CAST_SIZE(vertexBufSize) * 4 + 30];
174         mFloatTexPool.push_back(mFloatTexArray);
175     }
176     else
177     {
178         mFloatTexArray = mFloatTexPool.back();
179     }
180     return mFloatTexArray;
181 }
182 
continueIntVertArray()183 GLint *OpenGLGraphicsVertexes::continueIntVertArray() restrict2
184 {
185     if (mIntVertPool.empty())
186     {
187         mIntVertArray = new GLint[CAST_SIZE(vertexBufSize * 4 + 30)];
188         mIntVertPool.push_back(mIntVertArray);
189     }
190     else
191     {
192         mIntVertArray = mIntVertPool.back();
193     }
194     return mIntVertArray;
195 }
196 
continueShortVertArray()197 GLshort *OpenGLGraphicsVertexes::continueShortVertArray() restrict2
198 {
199     if (mShortVertPool.empty())
200     {
201         mShortVertArray = new GLshort[CAST_SIZE(vertexBufSize) * 4 + 30];
202         mShortVertPool.push_back(mShortVertArray);
203     }
204     else
205     {
206         mShortVertArray = mShortVertPool.back();
207     }
208     return mShortVertArray;
209 }
210 
continueIntTexArray()211 GLint *OpenGLGraphicsVertexes::continueIntTexArray() restrict2
212 {
213     if (mIntTexPool.empty())
214     {
215         mIntTexArray = new GLint[CAST_SIZE(vertexBufSize * 4 + 30)];
216         mIntTexPool.push_back(mIntTexArray);
217     }
218     else
219     {
220         mIntTexArray = mIntTexPool.back();
221     }
222     return mIntTexArray;
223 }
224 #endif  // USE_OPENGL
225