• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

15:00 부근 질문 있습니다.

23.02.03 16:44 작성 조회수 241

0

#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

강민철님의 프로필

강민철

2023.02.03

메모리누수는 CPU 사용량보다는 메모리 사용량을 보시는 것이 더 좋습니다.

그리고 Visual Studio 내장 분석 도구 이외에도 다양한 메모리 누수 점검용 툴이 있습니다.

Valgrind, AddressSanitizer 등이 있습니다.

이런 툴을 활용해보는 것도 좋을 듯합니다.