Remove some usage of raw pointers, fixes some segfaults

This commit is contained in:
Adrian Hedqvist 2017-12-27 14:35:45 +01:00
parent 79ff797cfd
commit 231833fb26
5 changed files with 11 additions and 17 deletions

View file

@ -12,17 +12,13 @@ FieldOfView::FieldOfView() {
FieldOfView::FieldOfView(Tilemap *map) {
this->map = map;
seen = new Tilemap(map->GetWidth(), map->GetHeight());
seen = Tilemap(map->GetWidth(), map->GetHeight());
counter = 0;
}
FieldOfView::~FieldOfView() {
delete seen;
}
void FieldOfView::calc(vec2i pos, float range) {
counter++;
seen->set_tile(pos.x, pos.y, counter);
seen.set_tile(pos.x, pos.y, counter);
// Once for each octant
if (map != nullptr) {
cast_light(1, 1.0f, 0.0f, 0, -1, -1, 0, pos.x, pos.y, range);
@ -40,11 +36,11 @@ void FieldOfView::calc(vec2i pos, float range) {
}
bool FieldOfView::can_see(vec2i pos) {
return seen->get_tile(pos.x, pos.y) >= counter;
return seen.get_tile(pos.x, pos.y) >= counter;
}
bool FieldOfView::has_seen(vec2i pos) {
return seen->get_tile(pos.x, pos.y) > 0;
return seen.get_tile(pos.x, pos.y) > 0;
}
void FieldOfView::cast_light(int row, float start, float end, int xx, int xy, int yx, int yy, int startX, int startY,
@ -70,7 +66,7 @@ void FieldOfView::cast_light(int row, float start, float end, int xx, int xy, in
}
if (sqrt(deltaX*deltaX + deltaY*deltaY) <= radius) {
seen->set_tile(currentX, currentY, counter);
seen.set_tile(currentX, currentY, counter);
}
if (blocked) {

View file

@ -6,19 +6,17 @@
#define DUNGEON_FIELDOFVIEW_H
#include "vec2i.h"
class Tilemap;
#include "Tilemap.h"
class FieldOfView {
Tilemap* map;
unsigned int counter;
Tilemap seen;
void cast_light(int row, float start, float end, int xx, int xy, int yx, int yy, int startX, int startY, float radius);
public:
Tilemap* seen;
FieldOfView();
FieldOfView(Tilemap* map);
~FieldOfView();
void calc(vec2i pos, float range);
bool can_see(vec2i pos);
bool has_seen(vec2i pos);

View file

@ -5,7 +5,7 @@ Tilemap generate_level(int seed, int width, int height) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (x == 0 || x == width || y == 0 || y == height) {
if (x == 0 || x == width-1 || y == 0 || y == height-1) {
map.set_tile(x,y, '#');
}
else {

View file

@ -15,12 +15,11 @@ Tilemap::Tilemap(int width, int height)
{
this->width = width;
this->height = height;
tilemap = new unsigned int[width*height]{0};
tilemap = std::vector<unsigned int>(width*height, 0);
}
Tilemap::~Tilemap()
{
delete tilemap;
for (auto var : entities) {
delete var;
}

View file

@ -1,4 +1,5 @@
#pragma once
#include <array>
#include <vector>
#include "Tileset.h"
#include "Entity.h"
@ -8,7 +9,7 @@ class Renderer;
class FieldOfView;
class Tilemap {
unsigned int* tilemap;
std::vector<unsigned int> tilemap;
std::vector<Entity*> entities;
int width;
int height;