質問編集履歴

6

提示コードを修正

2021/01/27 08:41

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -421,3 +421,381 @@
421
421
 
422
422
 
423
423
  ```
424
+
425
+
426
+
427
+ ```cpp
428
+
429
+ #include "../Header/Sprite.hpp"
430
+
431
+ #include "../Header/Shader.hpp"
432
+
433
+ #include "../Header/Game.hpp"
434
+
435
+ #include "../Header/Texture.hpp"
436
+
437
+ #include "../Header/VertexData.hpp"
438
+
439
+
440
+
441
+ // 数学ライブラリ
442
+
443
+ #include "glm/ext.hpp"
444
+
445
+ #include "glm/glm.hpp"
446
+
447
+
448
+
449
+ // OpenCV
450
+
451
+ #include <opencv2/core.hpp>
452
+
453
+
454
+
455
+ class Game;
456
+
457
+ /*################################################################################################################
458
+
459
+ * 画像 描画クラス
460
+
461
+ ################################################################################################################*/
462
+
463
+
464
+
465
+ //コンストラクタ
466
+
467
+ /*
468
+
469
+ * Game クラス
470
+
471
+ * テクスチャ パス
472
+
473
+ * 描画する画像の寸法
474
+
475
+ */
476
+
477
+ Sprite::Sprite(class Game* g, const char* FileName) : Transform_2D(g)
478
+
479
+ {
480
+
481
+ shader = new Shader(g,"Shader/2D/Sprite.vert", "Shader/2D/Sprite.frag");
482
+
483
+ Owner = g;//Gameクラス
484
+
485
+
486
+
487
+ Transform_2D::setTransfrom(glm::vec2(1,1),0,glm::vec2(0,0)); //トランスフォームを初期化
488
+
489
+
490
+
491
+ //テクスチャを設定
492
+
493
+ glGenTextures(1, &TextureID);
494
+
495
+ glBindTexture(GL_TEXTURE_2D, TextureID);
496
+
497
+
498
+
499
+ float width = 0, height = 0; //画像の寸法
500
+
501
+ int channels = 0; //画像のチャンネル数
502
+
503
+
504
+
505
+ byte* data = LoadTexture(FileName,width,height,channels); //テクスチャを取得
506
+
507
+
508
+
509
+
510
+
511
+ mPicSize.x = width;
512
+
513
+ mPicSize.y = height;
514
+
515
+
516
+
517
+ //printf("widht: %f\n", width);
518
+
519
+ //printf("height: %f\n", height);
520
+
521
+
522
+
523
+ if (data != NULL)
524
+
525
+ {
526
+
527
+ if (channels == 3)
528
+
529
+ {
530
+
531
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)width, (GLsizei)height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
532
+
533
+ }
534
+
535
+ else if (channels == 4)
536
+
537
+ {
538
+
539
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
540
+
541
+ }
542
+
543
+ }
544
+
545
+ else {
546
+
547
+ std::cerr << "Unable to load texture: " << FileName << std::endl;
548
+
549
+ }
550
+
551
+
552
+
553
+
554
+
555
+
556
+
557
+ // mSize = end - start; //画像のサイズを取得
558
+
559
+
560
+
561
+ // printf("width: %.2f\n", mSize.x);
562
+
563
+ // printf("height: %.2f\n",mSize.y);
564
+
565
+
566
+
567
+ /*
568
+
569
+ vertex[0] = VertexAttribute_2D{ -width / 2.0f,height / 2.0f, 0,0 };
570
+
571
+ vertex[1] = VertexAttribute_2D{ -width / 2.0f,-height / 2.0f, 0,1 };
572
+
573
+ vertex[2] = VertexAttribute_2D{ width / 2.0f,-height / 2.0f, 1,1 };
574
+
575
+
576
+
577
+ vertex[3] = VertexAttribute_2D{ -width / 2.0f,height / 2.0f, 0,0 };
578
+
579
+ vertex[4] = VertexAttribute_2D{ width / 2.0f,-height / 2.0f, 1,1 };
580
+
581
+ vertex[5] = VertexAttribute_2D{ width / 2.0f,height / 2.0f, 1,0 };
582
+
583
+ */
584
+
585
+
586
+
587
+
588
+
589
+ /*
590
+
591
+ *
592
+
593
+ // 頂点情報を設定
594
+
595
+ vertex[0] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x * uvWidth,start.y * uvHeight };
596
+
597
+ vertex[1] = VertexAttribute_2D{ -mSize.x / 2.0f,-mSize.y / 2.0f, start.x * uvWidth,end.y * uvHeight };
598
+
599
+ vertex[2] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x * uvWidth,end.y * uvHeight };
600
+
601
+
602
+
603
+ vertex[3] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x * uvWidth,start.y * uvHeight };
604
+
605
+ vertex[4] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x * uvWidth,end.y * uvHeight };
606
+
607
+ vertex[5] = VertexAttribute_2D{ mSize.x / 2.0f,mSize.y / 2.0f, end.x * uvWidth,start.y * uvHeight };
608
+
609
+
610
+
611
+ */
612
+
613
+
614
+
615
+
616
+
617
+ //VAO
618
+
619
+
620
+
621
+
622
+
623
+ //ミニマップを設定
624
+
625
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
626
+
627
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
628
+
629
+ glGenerateMipmap(GL_TEXTURE_2D);
630
+
631
+
632
+
633
+ //異方性フィルタリングを設定
634
+
635
+ GLfloat largest;
636
+
637
+ glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest);
638
+
639
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, largest);
640
+
641
+
642
+
643
+
644
+
645
+
646
+
647
+
648
+
649
+ delete data;
650
+
651
+ data = nullptr;
652
+
653
+ }
654
+
655
+
656
+
657
+ //描画
658
+
659
+ void Sprite::DrawGraph(glm::vec2 pos, glm::vec2 start, glm::vec2 end)
660
+
661
+ {
662
+
663
+
664
+
665
+
666
+
667
+ Transform_2D::UpdateTransform(); //トランスフォームを更新
668
+
669
+ Transform_2D::setTransform_Move(pos); //移動
670
+
671
+
672
+
673
+ shader->Enable(); //シェーダーを有効にする
674
+
675
+ shader->SetFloatUniform_3m("uViewMatrix", getViewMatrix());
676
+
677
+ shader->SetFloatUniform_3m("uWorldMatrix", getWorldMatrix());
678
+
679
+
680
+
681
+
682
+
683
+ glm::vec2 mSize;
684
+
685
+ mSize = end - start; //画像のサイズを取得
686
+
687
+ // printf("mSize.x %f\n", mSize.x);
688
+
689
+ //` printf("start.x. x%f\n", end.x);
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+ // 頂点情報を設定
700
+
701
+ vertex[0] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x / mPicSize.x ,start.y / mPicSize.y };
702
+
703
+
704
+
705
+ // printf("%f\n", start.y / mSize.y);
706
+
707
+
708
+
709
+ vertex[1] = VertexAttribute_2D{ -mSize.x / 2.0f,-mSize.y / 2.0f, start.x / mPicSize.x ,end.y / mPicSize.y };
710
+
711
+ vertex[2] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x / mPicSize.x ,end.y / mPicSize.y };
712
+
713
+
714
+
715
+ vertex[3] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x / mPicSize.x ,start.y / mPicSize.y };
716
+
717
+ vertex[4] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x / mPicSize.x ,end.y / mPicSize.y };
718
+
719
+ vertex[5] = VertexAttribute_2D{ mSize.x / 2.0f,mSize.y / 2.0f, end.x / mPicSize.x ,start.y / mPicSize.y };
720
+
721
+
722
+
723
+
724
+
725
+
726
+
727
+ //VAO
728
+
729
+ glGenVertexArrays(1, &VAO);
730
+
731
+ glBindVertexArray(VAO);
732
+
733
+
734
+
735
+ //VBO
736
+
737
+ glGenBuffers(1, &VBO);
738
+
739
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
740
+
741
+ glBufferData(GL_ARRAY_BUFFER, (sizeof(vertex) / sizeof(vertex[0])) * sizeof(VertexAttribute_2D), vertex, GL_STATIC_DRAW);
742
+
743
+
744
+
745
+ //頂点座標
746
+
747
+ glEnableVertexAttribArray(0);
748
+
749
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexAttribute_2D), NULL);
750
+
751
+
752
+
753
+ //UV座標
754
+
755
+ glEnableVertexAttribArray(1);
756
+
757
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexAttribute_2D), (void*)(sizeof(float) * 2));
758
+
759
+
760
+
761
+
762
+
763
+ glBindVertexArray(VAO);
764
+
765
+ glBindTexture(GL_TEXTURE_2D, TextureID);
766
+
767
+ glDrawArrays(GL_TRIANGLES, 0, (GLsizei)std::size(vertex));
768
+
769
+
770
+
771
+ // バッファを解放
772
+
773
+ glDeleteBuffers(1,&VAO);
774
+
775
+ glDeleteVertexArrays(1,&VAO);
776
+
777
+
778
+
779
+ glBindVertexArray(0);
780
+
781
+ glBindTexture(GL_TEXTURE_2D, 0);
782
+
783
+
784
+
785
+ }
786
+
787
+
788
+
789
+
790
+
791
+ // 画像サイズを取得
792
+
793
+ glm::vec2 Sprite::getSize()
794
+
795
+ {
796
+
797
+ return mPicSize;
798
+
799
+ }
800
+
801
+ ```

5

文章を修正

2021/01/27 08:41

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
 
4
4
 
5
+ 場所はBullet クラスのnew している部分で片方をコメントアウトしてもリークするので両方が原因と思うのですがどうすればいいのでしょうか?
6
+
5
7
 
6
8
 
7
9
  ![イメージ説明](7fa4a55fd4706f3c1a3025ab7d9c5576.jpeg)

4

提示画像を追加

2021/01/27 08:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
-
7
+ ![イメージ説明](7fa4a55fd4706f3c1a3025ab7d9c5576.jpeg)
8
8
 
9
9
  環境 Windows 10 64 bit
10
10
 

3

文章とタイトルを大幅に変更しました。

2021/01/27 07:07

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- デストラクタでアクセスエラー発生する原因が知りたい。
1
+ std::vector<> .erase()メソッドでデストラクタが呼ばれて居るのにも関わらずメモリリークしてしまう原因が知りたい。
test CHANGED
@@ -1,12 +1,12 @@
1
- 提示コードの////コメント部のコードですがなぜてーが出るのでしょうか?
2
-
3
- Player クラスの攻撃コメント部でpush_backして居るのですがどこが悪いのでしょうか?どうしてもわかりません。。しっかかりコピーコンストラクタを用意しているのですがなぜでしょうか?
4
-
5
-
6
-
7
-
8
-
9
- Github: https://github.com/Shigurechan/OpenGL
1
+ 提示コードのPlayerクラスのUpdate()関数部ですがif()文の攻撃コメント部ですが。ここでpush_backしてそ下のfor文全部削除してデストラクタのprintf()が表示されてしっかりと廃棄されているはずなのですがなぜメモリリークししまうのでしょうか?またコピーコンストラタもしっかり定義されておりデクタもしっかりdelete mSprite;と書いて居るのですがなぜでしょうか?
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+ 環境 Windows 10 64 bit
10
10
 
11
11
 
12
12
 
@@ -18,10 +18,6 @@
18
18
 
19
19
 
20
20
 
21
-
22
-
23
-
24
-
25
21
  // コンストラクタ
26
22
 
27
23
  Player::Player(class Game* g,const char* fileName ) : Actor_2D()
@@ -214,11 +210,9 @@
214
210
 
215
211
  // 攻撃
216
212
 
217
- if (mInput->KeyDown(GLFW_KEY_SPACE) == true)
213
+ if (mInput->KeyDownHold(GLFW_KEY_SPACE) == true)
218
-
214
+
219
- {
215
+ {
220
-
221
- // Bullet b(Owner, mPosition, mVector);
222
216
 
223
217
  bullet.push_back(Bullet(Owner, mPosition, mVector));
224
218
 
@@ -226,7 +220,7 @@
226
220
 
227
221
 
228
222
 
229
-
223
+
230
224
 
231
225
  // 弾 更新
232
226
 
@@ -234,105 +228,173 @@
234
228
 
235
229
  {
236
230
 
231
+ itr = bullet.erase(itr);
232
+
233
+ }
234
+
237
- if (itr->getPosition().x > Owner->getWindowSize().x)
235
+ mPosition += mVector * mSpeed; //実移動
236
+
238
-
237
+ }
238
+
239
+
240
+
241
+
242
+
243
+ // 描画
244
+
245
+ void Player::Draw()
246
+
239
- {
247
+ {
248
+
240
-
249
+ mSprite->DrawGraph(mPosition, mSpriteNum.at((int)mKeyCode).at(1).at(0), mSpriteNum.at((int)mKeyCode).at(1).at(1));
250
+
251
+ }
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+ // デストラクタ
266
+
267
+ Player::~Player()
268
+
269
+ {
270
+
271
+
272
+
273
+ }
274
+
275
+
276
+
277
+ ```
278
+
279
+
280
+
281
+ ```cpp
282
+
283
+ #include "../../Header/Game/Bullet.hpp"
284
+
285
+ #include "../../Header/Sprite.hpp"
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+ // コンストラクタ
294
+
295
+ Bullet::Bullet(class Game* g,glm::vec2 pos,glm::vec2 vec) : Actor_2D()
296
+
297
+ {
298
+
299
+ Owner = g; //Game クラス
300
+
301
+
302
+
303
+ mPosition = pos; //座標
304
+
305
+ mVector = vec; //方向
306
+
307
+ speed = 10; //速度
308
+
309
+
310
+
311
+ mSprite = new Sprite(g,"Assets/Bullet.png"); // スプライトクラス
312
+
313
+ }
314
+
315
+
316
+
317
+ // 計算
318
+
319
+ void Bullet::Update()
320
+
321
+ {
322
+
323
+ mPosition.x += mVector.x * speed;
324
+
325
+ mPosition.y += mVector.y * speed;
326
+
327
+
328
+
329
+ // printf("mPosition.x: %f\n", mPosition.x);
330
+
331
+ // printf("mPosition.y: %f\n",mPosition.y);
332
+
333
+
334
+
335
+ }
336
+
337
+
338
+
339
+ // 描画
340
+
341
+ void Bullet::Draw()
342
+
343
+ {
344
+
345
+ glm::vec2 size = mSprite->getSize();
346
+
347
+ mSprite->DrawGraph(mPosition,glm::vec2(0,1),glm::vec2(size.x,size.y));
348
+
349
+ }
350
+
351
+
352
+
353
+
354
+
355
+ glm::vec2 Bullet::getPosition()
356
+
357
+ {
358
+
359
+ return mPosition;
360
+
361
+ }
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+
370
+
371
+ // コピーコンストラクタ
372
+
373
+ Bullet::Bullet(const Bullet &b)
374
+
375
+ {
376
+
241
- printf("あああ\n");
377
+ printf("Bullet コピーコンストラクタ\n");
242
378
 
243
379
 
244
380
 
381
+
382
+
383
+ mPosition = b.mPosition;
384
+
245
- itr = bullet.erase(itr);
385
+ mVector = b.mVector;
246
-
247
- }
386
+
248
-
249
- else {
250
-
251
- printf("いいいい\n");
252
-
253
-
254
-
255
- itr->Update();
387
+ speed = b.speed;
256
-
257
- itr++;
388
+
258
-
259
- }
260
-
261
- }
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
-
275
-
276
-
277
-
278
-
279
-
280
-
281
-
282
-
283
-
284
-
285
- mPosition += mVector * mSpeed; //実移動
286
-
287
- }
288
-
289
-
290
-
291
-
292
-
293
- // 描画
294
-
295
- void Player::Draw()
389
+ Owner = b.Owner;
296
-
297
- {
298
390
 
299
391
 
300
392
 
301
- for (std::vector<Bullet>::iterator itr = bullet.begin(); itr != bullet.end(); )
393
+ mSprite = new Sprite(b.Owner, "Assets/Bullet.png");
302
-
303
- {
394
+
304
-
305
- itr->Draw();
395
+ //mSprite = b.mSprite;
306
-
307
- itr++;
396
+
308
-
309
-
310
-
311
- }
397
+ }
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
- mSprite->DrawGraph(mPosition, mSpriteNum.at((int)mKeyCode).at(1).at(0), mSpriteNum.at((int)mKeyCode).at(1).at(1));
326
-
327
- }
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
398
 
337
399
 
338
400
 
@@ -340,152 +402,20 @@
340
402
 
341
403
  // デストラクタ
342
404
 
343
- Player::~Player()
405
+ Bullet::~Bullet()
344
-
406
+
345
- {
407
+ {
408
+
346
-
409
+ printf("Bullet デストラクタ\n");
410
+
347
-
411
+ delete mSprite;
412
+
413
+ //printf("アドレス: %x\n",mSprite);
414
+
415
+ mSprite = nullptr;
348
416
 
349
417
  }
350
418
 
351
419
 
352
420
 
353
421
  ```
354
-
355
-
356
-
357
- ```cpp
358
-
359
- #include "../../Header/Game/Bullet.hpp"
360
-
361
- #include "../../Header/Sprite.hpp"
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
- // コンストラクタ
370
-
371
- Bullet::Bullet(class Game* g,glm::vec2 pos,glm::vec2 vec) : Actor_2D()
372
-
373
- {
374
-
375
- Owner = g; //Game クラス
376
-
377
-
378
-
379
- mPosition = pos; //座標
380
-
381
- mVector = vec; //方向
382
-
383
- speed = 10; //速度
384
-
385
-
386
-
387
- mSprite = new Sprite(g,"Assets/Bullet.png"); // スプライトクラス
388
-
389
- }
390
-
391
-
392
-
393
- // 計算
394
-
395
- void Bullet::Update()
396
-
397
- {
398
-
399
- mPosition.x += mVector.x * speed;
400
-
401
- mPosition.y += mVector.y * speed;
402
-
403
-
404
-
405
- // printf("mPosition.x: %f\n", mPosition.x);
406
-
407
- // printf("mPosition.y: %f\n",mPosition.y);
408
-
409
-
410
-
411
- }
412
-
413
-
414
-
415
- // 描画
416
-
417
- void Bullet::Draw()
418
-
419
- {
420
-
421
- glm::vec2 size = mSprite->getSize();
422
-
423
- mSprite->DrawGraph(mPosition,glm::vec2(0,1),glm::vec2(size.x,size.y));
424
-
425
- }
426
-
427
-
428
-
429
-
430
-
431
- glm::vec2 Bullet::getPosition()
432
-
433
- {
434
-
435
- return mPosition;
436
-
437
- }
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
- ////////////////////////////////////////////////////////////////
446
-
447
- //
448
-
449
- Bullet::Bullet(const Bullet &b)
450
-
451
- {
452
-
453
- printf("Bullet コピーコンストラクタ\n");
454
-
455
-
456
-
457
-
458
-
459
- mPosition = b.mPosition;
460
-
461
- mVector = b.mVector;
462
-
463
- speed = b.speed;
464
-
465
-
466
-
467
- mSprite = new Sprite(Owner,"Assets/Bullet.png");
468
-
469
- }
470
-
471
-
472
-
473
-
474
-
475
- // デストラクタ
476
-
477
- Bullet::~Bullet()
478
-
479
- {
480
-
481
- printf("Bullet デストラクタ\n");
482
-
483
- delete mSprite;
484
-
485
- mSprite = nullptr;
486
-
487
- }
488
-
489
- ////////////////////////////////////////////////////////////////
490
-
491
- ```

2

文章を修正

2021/01/27 06:41

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
-
9
+ Github: https://github.com/Shigurechan/OpenGL
10
10
 
11
11
 
12
12
 

1

文章を修正

2021/01/27 05:21

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- コピーコンストラクタが上手く動作せずアクセスしてしまう原因が知りたい。
1
+ トラクタアクセスエラーが発生する原因が知りたい。
test CHANGED
@@ -468,7 +468,7 @@
468
468
 
469
469
  }
470
470
 
471
- //////////////////////////////////////////////////////////////
471
+
472
472
 
473
473
 
474
474
 
@@ -486,6 +486,6 @@
486
486
 
487
487
  }
488
488
 
489
-
489
+ ////////////////////////////////////////////////////////////////
490
490
 
491
491
  ```