質問編集履歴
6
提示コードを修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -209,4 +209,193 @@
|
|
209
209
|
mSprite = nullptr;
|
210
210
|
}
|
211
211
|
|
212
|
+
```
|
213
|
+
|
214
|
+
```cpp
|
215
|
+
#include "../Header/Sprite.hpp"
|
216
|
+
#include "../Header/Shader.hpp"
|
217
|
+
#include "../Header/Game.hpp"
|
218
|
+
#include "../Header/Texture.hpp"
|
219
|
+
#include "../Header/VertexData.hpp"
|
220
|
+
|
221
|
+
// 数学ライブラリ
|
222
|
+
#include "glm/ext.hpp"
|
223
|
+
#include "glm/glm.hpp"
|
224
|
+
|
225
|
+
// OpenCV
|
226
|
+
#include <opencv2/core.hpp>
|
227
|
+
|
228
|
+
class Game;
|
229
|
+
/*################################################################################################################
|
230
|
+
* 画像 描画クラス
|
231
|
+
################################################################################################################*/
|
232
|
+
|
233
|
+
//コンストラクタ
|
234
|
+
/*
|
235
|
+
* Game クラス
|
236
|
+
* テクスチャ パス
|
237
|
+
* 描画する画像の寸法
|
238
|
+
*/
|
239
|
+
Sprite::Sprite(class Game* g, const char* FileName) : Transform_2D(g)
|
240
|
+
{
|
241
|
+
shader = new Shader(g,"Shader/2D/Sprite.vert", "Shader/2D/Sprite.frag");
|
242
|
+
Owner = g;//Gameクラス
|
243
|
+
|
244
|
+
Transform_2D::setTransfrom(glm::vec2(1,1),0,glm::vec2(0,0)); //トランスフォームを初期化
|
245
|
+
|
246
|
+
//テクスチャを設定
|
247
|
+
glGenTextures(1, &TextureID);
|
248
|
+
glBindTexture(GL_TEXTURE_2D, TextureID);
|
249
|
+
|
250
|
+
float width = 0, height = 0; //画像の寸法
|
251
|
+
int channels = 0; //画像のチャンネル数
|
252
|
+
|
253
|
+
byte* data = LoadTexture(FileName,width,height,channels); //テクスチャを取得
|
254
|
+
|
255
|
+
|
256
|
+
mPicSize.x = width;
|
257
|
+
mPicSize.y = height;
|
258
|
+
|
259
|
+
//printf("widht: %f\n", width);
|
260
|
+
//printf("height: %f\n", height);
|
261
|
+
|
262
|
+
if (data != NULL)
|
263
|
+
{
|
264
|
+
if (channels == 3)
|
265
|
+
{
|
266
|
+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)width, (GLsizei)height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
267
|
+
}
|
268
|
+
else if (channels == 4)
|
269
|
+
{
|
270
|
+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
271
|
+
}
|
272
|
+
}
|
273
|
+
else {
|
274
|
+
std::cerr << "Unable to load texture: " << FileName << std::endl;
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
// mSize = end - start; //画像のサイズを取得
|
280
|
+
|
281
|
+
// printf("width: %.2f\n", mSize.x);
|
282
|
+
// printf("height: %.2f\n",mSize.y);
|
283
|
+
|
284
|
+
/*
|
285
|
+
vertex[0] = VertexAttribute_2D{ -width / 2.0f,height / 2.0f, 0,0 };
|
286
|
+
vertex[1] = VertexAttribute_2D{ -width / 2.0f,-height / 2.0f, 0,1 };
|
287
|
+
vertex[2] = VertexAttribute_2D{ width / 2.0f,-height / 2.0f, 1,1 };
|
288
|
+
|
289
|
+
vertex[3] = VertexAttribute_2D{ -width / 2.0f,height / 2.0f, 0,0 };
|
290
|
+
vertex[4] = VertexAttribute_2D{ width / 2.0f,-height / 2.0f, 1,1 };
|
291
|
+
vertex[5] = VertexAttribute_2D{ width / 2.0f,height / 2.0f, 1,0 };
|
292
|
+
*/
|
293
|
+
|
294
|
+
|
295
|
+
/*
|
296
|
+
*
|
297
|
+
// 頂点情報を設定
|
298
|
+
vertex[0] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x * uvWidth,start.y * uvHeight };
|
299
|
+
vertex[1] = VertexAttribute_2D{ -mSize.x / 2.0f,-mSize.y / 2.0f, start.x * uvWidth,end.y * uvHeight };
|
300
|
+
vertex[2] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x * uvWidth,end.y * uvHeight };
|
301
|
+
|
302
|
+
vertex[3] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x * uvWidth,start.y * uvHeight };
|
303
|
+
vertex[4] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x * uvWidth,end.y * uvHeight };
|
304
|
+
vertex[5] = VertexAttribute_2D{ mSize.x / 2.0f,mSize.y / 2.0f, end.x * uvWidth,start.y * uvHeight };
|
305
|
+
|
306
|
+
*/
|
307
|
+
|
308
|
+
|
309
|
+
//VAO
|
310
|
+
|
311
|
+
|
312
|
+
//ミニマップを設定
|
313
|
+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
314
|
+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
315
|
+
glGenerateMipmap(GL_TEXTURE_2D);
|
316
|
+
|
317
|
+
//異方性フィルタリングを設定
|
318
|
+
GLfloat largest;
|
319
|
+
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest);
|
320
|
+
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, largest);
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
delete data;
|
326
|
+
data = nullptr;
|
327
|
+
}
|
328
|
+
|
329
|
+
//描画
|
330
|
+
void Sprite::DrawGraph(glm::vec2 pos, glm::vec2 start, glm::vec2 end)
|
331
|
+
{
|
332
|
+
|
333
|
+
|
334
|
+
Transform_2D::UpdateTransform(); //トランスフォームを更新
|
335
|
+
Transform_2D::setTransform_Move(pos); //移動
|
336
|
+
|
337
|
+
shader->Enable(); //シェーダーを有効にする
|
338
|
+
shader->SetFloatUniform_3m("uViewMatrix", getViewMatrix());
|
339
|
+
shader->SetFloatUniform_3m("uWorldMatrix", getWorldMatrix());
|
340
|
+
|
341
|
+
|
342
|
+
glm::vec2 mSize;
|
343
|
+
mSize = end - start; //画像のサイズを取得
|
344
|
+
// printf("mSize.x %f\n", mSize.x);
|
345
|
+
//` printf("start.x. x%f\n", end.x);
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
|
350
|
+
// 頂点情報を設定
|
351
|
+
vertex[0] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x / mPicSize.x ,start.y / mPicSize.y };
|
352
|
+
|
353
|
+
// printf("%f\n", start.y / mSize.y);
|
354
|
+
|
355
|
+
vertex[1] = VertexAttribute_2D{ -mSize.x / 2.0f,-mSize.y / 2.0f, start.x / mPicSize.x ,end.y / mPicSize.y };
|
356
|
+
vertex[2] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x / mPicSize.x ,end.y / mPicSize.y };
|
357
|
+
|
358
|
+
vertex[3] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x / mPicSize.x ,start.y / mPicSize.y };
|
359
|
+
vertex[4] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x / mPicSize.x ,end.y / mPicSize.y };
|
360
|
+
vertex[5] = VertexAttribute_2D{ mSize.x / 2.0f,mSize.y / 2.0f, end.x / mPicSize.x ,start.y / mPicSize.y };
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
//VAO
|
365
|
+
glGenVertexArrays(1, &VAO);
|
366
|
+
glBindVertexArray(VAO);
|
367
|
+
|
368
|
+
//VBO
|
369
|
+
glGenBuffers(1, &VBO);
|
370
|
+
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
371
|
+
glBufferData(GL_ARRAY_BUFFER, (sizeof(vertex) / sizeof(vertex[0])) * sizeof(VertexAttribute_2D), vertex, GL_STATIC_DRAW);
|
372
|
+
|
373
|
+
//頂点座標
|
374
|
+
glEnableVertexAttribArray(0);
|
375
|
+
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexAttribute_2D), NULL);
|
376
|
+
|
377
|
+
//UV座標
|
378
|
+
glEnableVertexAttribArray(1);
|
379
|
+
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexAttribute_2D), (void*)(sizeof(float) * 2));
|
380
|
+
|
381
|
+
|
382
|
+
glBindVertexArray(VAO);
|
383
|
+
glBindTexture(GL_TEXTURE_2D, TextureID);
|
384
|
+
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)std::size(vertex));
|
385
|
+
|
386
|
+
// バッファを解放
|
387
|
+
glDeleteBuffers(1,&VAO);
|
388
|
+
glDeleteVertexArrays(1,&VAO);
|
389
|
+
|
390
|
+
glBindVertexArray(0);
|
391
|
+
glBindTexture(GL_TEXTURE_2D, 0);
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
|
396
|
+
// 画像サイズを取得
|
397
|
+
glm::vec2 Sprite::getSize()
|
398
|
+
{
|
399
|
+
return mPicSize;
|
400
|
+
}
|
212
401
|
```
|
5
文章を修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
提示コードのPlayerクラスのUpdate()関数部ですがif()文の攻撃コメント部ですが。ここでpush_backしてその下のfor文で全部削除してデストラクタのprintf()が表示されてしっかりと廃棄されているはずなのですがなぜメモリリークしてしまうのでしょうか?またコピーコンストラクタもしっかり定義されておりデストラクタもしっかりdelete mSprite;と書いて居るのですがなぜでしょうか?
|
2
2
|
|
3
|
+
場所はBullet クラスのnew している部分で片方をコメントアウトしてもリークするので両方が原因と思うのですがどうすればいいのでしょうか?
|
3
4
|
|
4
5
|

|
5
6
|
環境 Windows 10 64 bit
|
4
提示画像を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
提示コードのPlayerクラスのUpdate()関数部ですがif()文の攻撃コメント部ですが。ここでpush_backしてその下のfor文で全部削除してデストラクタのprintf()が表示されてしっかりと廃棄されているはずなのですがなぜメモリリークしてしまうのでしょうか?またコピーコンストラクタもしっかり定義されておりデストラクタもしっかりdelete mSprite;と書いて居るのですがなぜでしょうか?
|
2
2
|
|
3
3
|
|
4
|
-
|
4
|
+

|
5
5
|
環境 Windows 10 64 bit
|
6
6
|
|
7
7
|
|
3
文章とタイトルを大幅に変更しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
デス
|
1
|
+
std::vector<> .erase()メソッドでデストラクタが呼ばれてる居るのにも関わらずメモリリークしてしまう原因が知りたい。
|
body
CHANGED
@@ -1,15 +1,13 @@
|
|
1
|
-
提示コードの
|
2
|
-
Player クラスの攻撃コメント部でpush_backして居るのですがどこが悪いのでしょうか?どうしてもわかりません。。しっかかりコピーコンストラクタを用意しているのですがなぜでしょうか?
|
1
|
+
提示コードのPlayerクラスのUpdate()関数部ですがif()文の攻撃コメント部ですが。ここでpush_backしてその下のfor文で全部削除してデストラクタのprintf()が表示されてしっかりと廃棄されているはずなのですがなぜメモリリークしてしまうのでしょうか?またコピーコンストラクタもしっかり定義されておりデストラクタもしっかりdelete mSprite;と書いて居るのですがなぜでしょうか?
|
3
2
|
|
4
3
|
|
5
|
-
Github: https://github.com/Shigurechan/OpenGL
|
6
4
|
|
5
|
+
環境 Windows 10 64 bit
|
7
6
|
|
7
|
+
|
8
8
|
```cpp
|
9
9
|
#include "../../Header/Game/Player.hpp"
|
10
10
|
|
11
|
-
|
12
|
-
|
13
11
|
// コンストラクタ
|
14
12
|
Player::Player(class Game* g,const char* fileName ) : Actor_2D()
|
15
13
|
{
|
@@ -106,60 +104,24 @@
|
|
106
104
|
}
|
107
105
|
|
108
106
|
// 攻撃
|
109
|
-
if (mInput->
|
107
|
+
if (mInput->KeyDownHold(GLFW_KEY_SPACE) == true)
|
110
108
|
{
|
111
|
-
// Bullet b(Owner, mPosition, mVector);
|
112
109
|
bullet.push_back(Bullet(Owner, mPosition, mVector));
|
113
110
|
}
|
114
111
|
|
115
|
-
|
112
|
+
|
116
113
|
// 弾 更新
|
117
114
|
for (std::vector<Bullet>::iterator itr = bullet.begin(); itr != bullet.end(); )
|
118
115
|
{
|
119
|
-
if (itr->getPosition().x > Owner->getWindowSize().x)
|
120
|
-
{
|
121
|
-
printf("あああ\n");
|
122
|
-
|
123
|
-
|
116
|
+
itr = bullet.erase(itr);
|
124
|
-
}
|
125
|
-
else {
|
126
|
-
printf("いいいい\n");
|
127
|
-
|
128
|
-
itr->Update();
|
129
|
-
itr++;
|
130
|
-
}
|
131
117
|
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
118
|
mPosition += mVector * mSpeed; //実移動
|
144
119
|
}
|
145
120
|
|
146
121
|
|
147
122
|
// 描画
|
148
123
|
void Player::Draw()
|
149
|
-
{
|
124
|
+
{
|
150
|
-
|
151
|
-
for (std::vector<Bullet>::iterator itr = bullet.begin(); itr != bullet.end(); )
|
152
|
-
{
|
153
|
-
itr->Draw();
|
154
|
-
itr++;
|
155
|
-
|
156
|
-
}
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
125
|
mSprite->DrawGraph(mPosition, mSpriteNum.at((int)mKeyCode).at(1).at(0), mSpriteNum.at((int)mKeyCode).at(1).at(1));
|
164
126
|
}
|
165
127
|
|
@@ -220,8 +182,8 @@
|
|
220
182
|
|
221
183
|
|
222
184
|
|
223
|
-
|
185
|
+
|
224
|
-
//
|
186
|
+
// コピーコンストラクタ
|
225
187
|
Bullet::Bullet(const Bullet &b)
|
226
188
|
{
|
227
189
|
printf("Bullet コピーコンストラクタ\n");
|
@@ -230,8 +192,10 @@
|
|
230
192
|
mPosition = b.mPosition;
|
231
193
|
mVector = b.mVector;
|
232
194
|
speed = b.speed;
|
233
|
-
|
195
|
+
Owner = b.Owner;
|
196
|
+
|
234
|
-
mSprite = new Sprite(Owner,"Assets/Bullet.png");
|
197
|
+
mSprite = new Sprite(b.Owner, "Assets/Bullet.png");
|
198
|
+
//mSprite = b.mSprite;
|
235
199
|
}
|
236
200
|
|
237
201
|
|
@@ -239,8 +203,9 @@
|
|
239
203
|
Bullet::~Bullet()
|
240
204
|
{
|
241
205
|
printf("Bullet デストラクタ\n");
|
242
|
-
|
206
|
+
delete mSprite;
|
207
|
+
//printf("アドレス: %x\n",mSprite);
|
243
208
|
mSprite = nullptr;
|
244
209
|
}
|
245
|
-
|
210
|
+
|
246
211
|
```
|
2
文章を修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
Player クラスの攻撃コメント部でpush_backして居るのですがどこが悪いのでしょうか?どうしてもわかりません。。しっかかりコピーコンストラクタを用意しているのですがなぜでしょうか?
|
3
3
|
|
4
4
|
|
5
|
+
Github: https://github.com/Shigurechan/OpenGL
|
5
6
|
|
6
7
|
|
7
|
-
|
8
8
|
```cpp
|
9
9
|
#include "../../Header/Game/Player.hpp"
|
10
10
|
|
1
文章を修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
デスストラクタでアクセスエラーが発生する原因が知りたい。
|
body
CHANGED
@@ -233,8 +233,8 @@
|
|
233
233
|
|
234
234
|
mSprite = new Sprite(Owner,"Assets/Bullet.png");
|
235
235
|
}
|
236
|
-
//////////////////////////////////////////////////////////////
|
237
236
|
|
237
|
+
|
238
238
|
// デストラクタ
|
239
239
|
Bullet::~Bullet()
|
240
240
|
{
|
@@ -242,5 +242,5 @@
|
|
242
242
|
delete mSprite;
|
243
243
|
mSprite = nullptr;
|
244
244
|
}
|
245
|
-
|
245
|
+
////////////////////////////////////////////////////////////////
|
246
246
|
```
|