Fixed bug in sweep and updated tests

This commit is contained in:
valtermiari 2023-03-17 11:45:32 +01:00
parent ce714db3f1
commit 78ccade17d
6 changed files with 56 additions and 32 deletions

View file

@ -26,8 +26,8 @@ class Game {
private:
//std::vector<Player*> *players;
std::vector<Player> *players;
std::vector<Player*> *players;
//std::vector<Player> *players;
Point *dimensions;
public:
@ -38,18 +38,18 @@ public:
}
void init() {
//players = static_cast<std::vector<Player*>*>(GC::Heap::alloc(sizeof(Player*) * MAX_PLAYERS));
players = static_cast<std::vector<Player>*>(GC::Heap::alloc(sizeof(Player) * MAX_PLAYERS));
players = static_cast<std::vector<Player*>*>(GC::Heap::alloc(sizeof(Player*) * MAX_PLAYERS));
//players = static_cast<std::vector<Player>*>(GC::Heap::alloc(sizeof(Player) * MAX_PLAYERS));
dimensions = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
dimensions->x = X_LENGTH;
dimensions->y = Y_LENGTH;
}
void add_player(Player *p) {
players->push_back(*p);
players->push_back(p);
}
Player* create_player(string s, Point pos, Point size, Point dir) {
Player* create_player(string *s, Point *pos, Point *size, Point *dir) {
Player *p = static_cast<Player*>(GC::Heap::alloc(sizeof(Player)));
/*
Cannot allocate by new, since it the allocates outside of "out" heap. That also lead so us having to
@ -62,7 +62,13 @@ public:
void create_players(int nr) {
for (int i = 0; i < nr; i++) {
Player *p = create_player(std::to_string(i), Point(i, i), Point(2, 2), Point(0, 0));
std::string *str = static_cast<std::string*>(GC::Heap::alloc(sizeof(std::string)));
Point *pos = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
Point *size = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
Point *dir = static_cast<Point*>(GC::Heap::alloc(sizeof(Point)));
Player *p = create_player(str, pos, size, dir);
add_player(p);
}
}

View file

@ -78,11 +78,12 @@ int main() {
// long *l = static_cast<long *>(gc->alloc(sizeof(long))); // 0x6-0xd | 0x
// This is allocated outside of the scope of the GC (if gc->init() isn't called), thus garbage
/* long *longs[21];
/* long *longs;
std::cout << "Pointer to ints:\t" << longs << std::endl;
for (int i = 0; i < 21; i++) {
longs[i] = static_cast<long *>(gc->alloc(sizeof(long)));
} */
longs = static_cast<long *>(gc->alloc(sizeof(long)));
longs++;
} */
Node *root = static_cast<Node *>(gc->alloc(sizeof(Node)));
root = test_chain(3, false);
@ -93,7 +94,7 @@ int main() {
std::cout << "Root child, child:\t" << root_child->child << std::endl;
gc->collect(GC::MARK);
gc->collect(GC::COLLECT_ALL);
gc->print_contents();
return 0;
}

View file

@ -15,36 +15,37 @@ class Player {
private:
string name;
Point position;
Point size;
Point direction;
string *name;
Point *position;
Point *size;
Point *direction;
public:
Player() {}
Player(string n, Point pos, Point s, Point dir)
/* Player(string n, Point pos, Point s, Point dir)
: name(n), position(pos.x, pos.y), size(s.x, s.y), direction(dir.x, dir.y)
{}
{} */
void move() {
position.x += direction.x;
position.y += direction.y;
position->x += direction->x;
position->y += direction->y;
}
void set_speed(int dx, int dy) {
direction.x = dx;
direction.y = dy;
direction->x = dx;
direction->y = dy;
}
// This is probably neccessary to initialize an object with our GC
// Since allocation and construction cannot be done at the same time
void init(string n, Point pos, Point s, Point dir) {
void init(string *n, Point *pos, Point *s, Point *dir) {
name = n;
position = pos;
size = s;
direction = dir;
}
};