Anonymous
Что вызывает утечки памяти?
Сообщение
Anonymous » 10 май 2024, 22:56
Я создаю игру под названием Square Eater, где у меня есть квадрат, который пользователь может перемещать с помощью стрелок на клавиатуре и выходить из игры, нажав «q». теперь я убедился, что не выделил ресурсы вручную, поэтому память должна очищаться автоматически. В конце я прикрепил вывод valgrind. вот мой код
Код: Выделить всё
#ifdef __APPLE__
#include
#else
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct point
{
float x;
float y;
}point;
typedef struct color
{
float r;
float g;
float b;
}color;
int number_of_segments = 200;
float logX = 200;
float logY = 200;
int PhyWidth = 1000;
int PhyHeight = 1000;
int windowPosX = 500;
int windowPosY = 500;
float centerX = logX / 2;
float centerY = logY / 2;
int alphaX = 0;
int alphaY = 0;
typedef unsigned char u8;
class square
{
private:
std::array
pts;
float width;
float height;
bool filled;
float max_x;
float min_x;
float max_y;
float min_y;
int id;
bool eaten = false;
color colour = {1.0, 1.0, 1.0};
bool main;
public:
square(point pt, int width, int height, bool filled, bool main)
{
this->width = width;
this->height = height;
this->filled = filled;
this->main = main;
float half_width = width/2;
float half_height = height/2;
min_x = pt.x - half_width;
max_x = pt.x + half_width;
min_y = pt.y - half_height;
max_y = pt.y + half_height;
this->pts[0] = {min_x, min_y};
this->pts[1] = {max_x, min_y};
this->pts[2] = {max_x, max_y};
this->pts[3] = {min_x, max_y};
}
square(point pt, int width, int height, bool main, color colour, int id)
{
this->id = id;
this->width = width;
this->height = height;
this->main = main;
this->colour = colour;
float half_width = width/2;
float half_height = height/2;
min_x = pt.x - half_width;
max_x = pt.x + half_width;
min_y = pt.y - half_height;
max_y = pt.y + half_height;
this->pts[0] = {min_x, min_y};
this->pts[1] = {max_x, min_y};
this->pts[2] = {max_x, max_y};
this->pts[3] = {min_x, max_y};
}
std::array get_pts()
{
return this->pts;
}
void set_eaten()
{
this->eaten = true;
}
bool get_eaten()
{
return eaten;
}
void draw()
{
int alphaX_copy = 0;
int alphaY_copy = 0;
if(main)
{
alphaX_copy = alphaX;
alphaY_copy = alphaY;
}
glColor4f(colour.r, colour.g, colour.b, 1);
glBegin(GL_POLYGON);
glVertex2f(this->pts[0].x + alphaX_copy, this->pts[0].y + alphaY_copy);
glVertex2f(this->pts[1].x + alphaX_copy, this->pts[1].y + alphaY_copy);
glVertex2f(this->pts[2].x + alphaX_copy, this->pts[2].y + alphaY_copy);
glVertex2f(this->pts[3].x + alphaX_copy, this->pts[3].y + alphaY_copy);
glEnd();
}
static bool intersected_squares(square& big_square, square& small_square)
{
for (size_t i = 0; i < 4; i++)
{
if( small_square.pts[i].x > (big_square.min_x + alphaX) && small_square.pts[i].x < (big_square.max_x + alphaX))
{
// means in the range of x
if(small_square.pts[i].y > (big_square.min_y + alphaY) && small_square.pts[i].y < (big_square.max_y + alphaY))
{
// std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78462296/what-is-causing-the-memory-leaks[/url]
1715370990
Anonymous
Я создаю игру под названием Square Eater, где у меня есть квадрат, который пользователь может перемещать с помощью стрелок на клавиатуре и выходить из игры, нажав «q». теперь я убедился, что не выделил ресурсы вручную, поэтому память должна очищаться автоматически. В конце я прикрепил вывод valgrind. вот мой код [code]#ifdef __APPLE__ #include #else #include #endif #include #include #include #include #include #include #include #include typedef struct point { float x; float y; }point; typedef struct color { float r; float g; float b; }color; int number_of_segments = 200; float logX = 200; float logY = 200; int PhyWidth = 1000; int PhyHeight = 1000; int windowPosX = 500; int windowPosY = 500; float centerX = logX / 2; float centerY = logY / 2; int alphaX = 0; int alphaY = 0; typedef unsigned char u8; class square { private: std::array pts; float width; float height; bool filled; float max_x; float min_x; float max_y; float min_y; int id; bool eaten = false; color colour = {1.0, 1.0, 1.0}; bool main; public: square(point pt, int width, int height, bool filled, bool main) { this->width = width; this->height = height; this->filled = filled; this->main = main; float half_width = width/2; float half_height = height/2; min_x = pt.x - half_width; max_x = pt.x + half_width; min_y = pt.y - half_height; max_y = pt.y + half_height; this->pts[0] = {min_x, min_y}; this->pts[1] = {max_x, min_y}; this->pts[2] = {max_x, max_y}; this->pts[3] = {min_x, max_y}; } square(point pt, int width, int height, bool main, color colour, int id) { this->id = id; this->width = width; this->height = height; this->main = main; this->colour = colour; float half_width = width/2; float half_height = height/2; min_x = pt.x - half_width; max_x = pt.x + half_width; min_y = pt.y - half_height; max_y = pt.y + half_height; this->pts[0] = {min_x, min_y}; this->pts[1] = {max_x, min_y}; this->pts[2] = {max_x, max_y}; this->pts[3] = {min_x, max_y}; } std::array get_pts() { return this->pts; } void set_eaten() { this->eaten = true; } bool get_eaten() { return eaten; } void draw() { int alphaX_copy = 0; int alphaY_copy = 0; if(main) { alphaX_copy = alphaX; alphaY_copy = alphaY; } glColor4f(colour.r, colour.g, colour.b, 1); glBegin(GL_POLYGON); glVertex2f(this->pts[0].x + alphaX_copy, this->pts[0].y + alphaY_copy); glVertex2f(this->pts[1].x + alphaX_copy, this->pts[1].y + alphaY_copy); glVertex2f(this->pts[2].x + alphaX_copy, this->pts[2].y + alphaY_copy); glVertex2f(this->pts[3].x + alphaX_copy, this->pts[3].y + alphaY_copy); glEnd(); } static bool intersected_squares(square& big_square, square& small_square) { for (size_t i = 0; i < 4; i++) { if( small_square.pts[i].x > (big_square.min_x + alphaX) && small_square.pts[i].x < (big_square.max_x + alphaX)) { // means in the range of x if(small_square.pts[i].y > (big_square.min_y + alphaY) && small_square.pts[i].y < (big_square.max_y + alphaY)) { // std::cout Подробнее здесь: [url]https://stackoverflow.com/questions/78462296/what-is-causing-the-memory-leaks[/url]