15:00 부근 질문 있습니다.
420
投稿した質問数 27
#pragma once
#include "Game2D.h"
namespace jm
{
class MyTank
{
public:
vec2 center = vec2(0.0f, 0.0f);
//vec2 direction = vec2(1.0f, 0.0f, 0.0f);
void draw()
{
beginTransformation();
{
translate(center);
drawFilledBox(Colors::green, 0.25f, 0.1f); // body
translate(-0.02f, 0.1f);
drawFilledBox(Colors::blue, 0.15f, 0.09f); // turret
translate(0.15f, 0.0f);
drawFilledBox(Colors::red, 0.15f, 0.03f); // barrel
}
endTransformation();
}
};
class MyBullet
{
public:
vec2 center = vec2(0.0f, 0.0f);
vec2 velocity = vec2(0.0f, 0.0f);
void draw()
{
beginTransformation();
translate(center);
drawFilledRegularConvexPolygon(Colors::yellow, 0.02f, 8);
drawWiredRegularConvexPolygon(Colors::gray, 0.02f, 8);
endTransformation();
}
void update(const float& dt)
{
center += velocity * dt;
}
};
class TankExample : public Game2D
{
public:
MyTank tank;
//MyBullet* bullet = nullptr;
//MyBullet* bullet2 = nullptr;
// bullet은 총알이 발사됐을 때만 존재하기 때문에 포인터로 되어있고 nullptr로 초기화가 되어있다.
//TODO: allow multiple bullets
int index = 0;
std::vector<MyBullet*> ptr_bullet_array = { nullptr };
//TODO: delete bullets when they go out of the screen
public:
TankExample()
: Game2D("This is my digital canvas!", 1024, 768, false, 2)
{}
~TankExample()
{
//if (bullet != nullptr) delete bullet;
//if (bullet2 != nullptr) delete bullet2;
for (int i = 0; i <= index; ++i)
{
if (ptr_bullet_array[i] != nullptr)
delete ptr_bullet_array[i];
}
}
void update() override
{
// move tank
if (isKeyPressed(GLFW_KEY_LEFT)) tank.center.x -= 0.5f * getTimeStep();
if (isKeyPressed(GLFW_KEY_RIGHT)) tank.center.x += 0.5f * getTimeStep();
if (isKeyPressed(GLFW_KEY_UP)) tank.center.y += 0.5f * getTimeStep();
if (isKeyPressed(GLFW_KEY_DOWN)) tank.center.y -= 0.5f * getTimeStep();
// shoot a cannon ball
if (isKeyPressedAndReleased(GLFW_KEY_SPACE))
{
//if (bullet == nullptr)
//{
// bullet = new MyBullet;
// bullet->center = tank.center;
// bullet->center.x += 0.2f;
// bullet->center.y += 0.1f;
// bullet->velocity = vec2(2.0f, 0.0f);
//}
//else if (bullet2 == nullptr)
//{
// bullet2 = new MyBullet;
// bullet2->center = tank.center;
// bullet2->center.x += 0.2f;
// bullet2->center.y += 0.1f;
// bullet2->velocity = vec2(2.0f, 0.0f);
//}
ptr_bullet_array.push_back(nullptr);
ptr_bullet_array[index] = new MyBullet;
ptr_bullet_array[index]->center = tank.center;
ptr_bullet_array[index]->center.x += 0.2f;
ptr_bullet_array[index]->center.y += 0.1f;
ptr_bullet_array[index]->velocity = vec2(2.0f, 0.0f);
index++;
}
//if (bullet != nullptr) bullet->update(getTimeStep());
//if (bullet2 != nullptr) bullet2->update(getTimeStep());
// rendering
tank.draw();
//if (bullet != nullptr)
//{
// bullet->draw();
//}
//if (bullet2 != nullptr)
//{
// bullet2->draw();
//}
//if (bullet != nullptr && bullet->center.x > 1.2f)
//{
// delete bullet;
// bullet = nullptr;
//}
//if (bullet2 != nullptr && bullet2->center.x > 1.2f)
//{
// delete bullet2;
// bullet2 = nullptr;
//}
//for (int i = 0; i <= index; ++i)
//{
// if (ptr_bullet_array[i] != nullptr && ptr_bullet_array[i]->center.x > 1.2f)
// {
// delete ptr_bullet_array[i];
// ptr_bullet_array[i] = nullptr;
// }
//}
for (int i = 0; i <= index; ++i)
{
if (ptr_bullet_array[i] != nullptr)
{
ptr_bullet_array[i]->update(getTimeStep());
ptr_bullet_array[i]->draw();
if (ptr_bullet_array[i]->center.x > 1.2f)
{
delete ptr_bullet_array[i];
ptr_bullet_array[i] = nullptr;
}
}
}
}
};
}교수님께서 설명하신 대로 연습문제를 풀어봤고 문제없이 잘 실행되는 거 같긴 한데 메모리 누수가 해결됐는지 잘 모르겠습니다. 연습문제를 풀기 전에 디버깅 모드로 봐도 CPU 사용량이 그대로인데 어디서 어떻게 확인할 수 있을까요?
回答 1
1
메모리누수는 CPU 사용량보다는 메모리 사용량을 보시는 것이 더 좋습니다.
그리고 Visual Studio 내장 분석 도구 이외에도 다양한 메모리 누수 점검용 툴이 있습니다.
Valgrind, AddressSanitizer 등이 있습니다.
이런 툴을 활용해보는 것도 좋을 듯합니다.
깃허브에서 받은 코드가 왜 강의코드랑 다를까요
0
73
1
Mac vscode 으로 시작하시려는 분들께
0
313
1
마우스 좌우 버튼을 동시에 눌렀을 때의 원의 위치
0
425
1
정답은 어디서볼수있나요?
1
346
2
예제코드
1
490
2
따배C++ 몇 강까지 학습한 후 수강가능 할까요?
1
642
1
(20.4 참고) 20.4에 해당하는 가이드 페이지가 어디있는지 모르겠습니다.
0
371
1
multimap 질문
0
354
2
도형들의 움직임이 너무 빠릅니다...
0
464
1
vcpkg 설치를 했는데
0
682
2
mutiple bullet 관련 질문 드립니다.
0
480
2
실행 후 화면 꺼짐
0
578
2
예제 파일 실행 시, 에러
1
558
1
아직 못풀어도 괜찮을까요?
0
499
1
코드 열었을 때 오류
1
807
3
코딩공부에 대해서 막히는부분
0
335
1
vcpkg 설치 오류
0
593
2
랜덤값 질문입니다.
0
425
1
vcpkg 다운로드에 문제를 겪고있습니다
1
423
1
if 문에 >= 대신 == 넣으면 작동을 하지 않는 이유가 무엇인가요.
0
282
1
multiple bullet 문제
0
316
1
프로그램 실행 순서 질문
0
272
1
2.2.2 상속으로 깔끔하게 init 메서드 질문
0
227
1
txt file로부터 키바인딩
0
298
2

