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) { FieldOfView::FieldOfView(Tilemap *map) {
this->map = map; this->map = map;
seen = new Tilemap(map->GetWidth(), map->GetHeight()); seen = Tilemap(map->GetWidth(), map->GetHeight());
counter = 0; counter = 0;
} }
FieldOfView::~FieldOfView() {
delete seen;
}
void FieldOfView::calc(vec2i pos, float range) { void FieldOfView::calc(vec2i pos, float range) {
counter++; counter++;
seen->set_tile(pos.x, pos.y, counter); seen.set_tile(pos.x, pos.y, counter);
// Once for each octant // Once for each octant
if (map != nullptr) { if (map != nullptr) {
cast_light(1, 1.0f, 0.0f, 0, -1, -1, 0, pos.x, pos.y, range); 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) { 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) { 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, 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) { if (sqrt(deltaX*deltaX + deltaY*deltaY) <= radius) {
seen->set_tile(currentX, currentY, counter); seen.set_tile(currentX, currentY, counter);
} }
if (blocked) { if (blocked) {

View file

@ -6,19 +6,17 @@
#define DUNGEON_FIELDOFVIEW_H #define DUNGEON_FIELDOFVIEW_H
#include "vec2i.h" #include "vec2i.h"
#include "Tilemap.h"
class Tilemap;
class FieldOfView { class FieldOfView {
Tilemap* map; Tilemap* map;
unsigned int counter; 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); void cast_light(int row, float start, float end, int xx, int xy, int yx, int yy, int startX, int startY, float radius);
public: public:
Tilemap* seen;
FieldOfView(); FieldOfView();
FieldOfView(Tilemap* map); FieldOfView(Tilemap* map);
~FieldOfView();
void calc(vec2i pos, float range); void calc(vec2i pos, float range);
bool can_see(vec2i pos); bool can_see(vec2i pos);
bool has_seen(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 y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) { 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, '#'); map.set_tile(x,y, '#');
} }
else { else {

View file

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

View file

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