summaryrefslogtreecommitdiffstats
path: root/edit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'edit.cpp')
-rw-r--r--edit.cpp109
1 files changed, 57 insertions, 52 deletions
diff --git a/edit.cpp b/edit.cpp
index 0936379..e03f92c 100644
--- a/edit.cpp
+++ b/edit.cpp
@@ -1,7 +1,6 @@
#include "edit.h"
#include "level.h"
#include "geometry.h"
-#include <stdlib.h>
static int editMode = EDIT_MODE_VIEW;
@@ -48,9 +47,9 @@ void setHoveredVertex(Vertex *v) {
hoveredRoom = NULL;
for(i = 0; i < l->nRooms; i++) {
- if(vertexInPolygon(v, &l->rooms[i].polygon)) {
- //hoveredRoom = &l->rooms[i];
- //break;
+ if(l->rooms[i].polygon.contains(*v)) {
+ hoveredRoom = &l->rooms[i];
+ break;
}
}
}
@@ -62,8 +61,7 @@ void setHoveredVertex(Vertex *v) {
void startAddMode() {
activeRoom = (ROOM*)calloc(1, sizeof(ROOM));
- activeRoom->polygon.nVertices = 0;
- activeRoom->polygon.vertices = NULL;
+ activeRoom->polygon = Polygon();
activeRoom->name = (char*)calloc(1, sizeof(unsigned char));
editMode = EDIT_MODE_ADD;
@@ -77,94 +75,101 @@ ROOM *getHoveredRoom() {
return hoveredRoom;
}
-static int isLineOk(LINE *l) {
+static bool isLineOk(LINE *l) {
LEVEL *lvl = getLevel();
LINE l2;
- int i;
if(activeRoom) {
- for(i = 0; i+2 < activeRoom->polygon.nVertices; i++) {
- l2.v1 = activeRoom->polygon.vertices[i];
- l2.v2 = activeRoom->polygon.vertices[i+1];
+ for(int i = 0; i+2 < activeRoom->polygon.size(); i++) {
+ l2.v1 = activeRoom->polygon[i];
+ l2.v2 = activeRoom->polygon[i+1];
- if(lineIntersection(l, &l2, NULL) == INTERSECTION_SEGMENT_SEGMENT) return 0;
+ if(lineIntersection(l, &l2, NULL) == INTERSECTION_SEGMENT_SEGMENT) return false;
}
- if(activeRoom->polygon.nVertices > 1) {
- l2.v1 = activeRoom->polygon.vertices[activeRoom->polygon.nVertices-2];
- l2.v2 = activeRoom->polygon.vertices[activeRoom->polygon.nVertices-1];
- if(vertexOnLine(&l->v2, &l2)) return 0;
+ if(activeRoom->polygon.size() > 1) {
+ l2.v1 = activeRoom->polygon[activeRoom->polygon.size()-2];
+ l2.v2 = activeRoom->polygon.back();
+ if(vertexOnLine(&l->v2, &l2)) return false;
}
}
- for(i = 0; i < lvl->nRooms; i++) {
- if(linePolygonIntersection(l, &lvl->rooms[i].polygon))
- return 0;
+ for(int i = 0; i < lvl->nRooms; i++) {
+ if(lvl->rooms[i].polygon.intersects(l))
+ return false;
}
- return 1;
+ return true;
}
-int isVertexOk(Vertex *v) {
+bool isVertexOk(Vertex *v) {
LEVEL *lvl = getLevel();
LINE l;
int i;
+
for(i = 0; i < lvl->nRooms; i++) {
- if(vertexInPolygon(v, &lvl->rooms[i].polygon)) return 0;
+ if(lvl->rooms[i].polygon.contains(*v)) return false;
}
- if(!(getActiveRoom() && getActiveRoom()->polygon.nVertices))
- return 1;
+ if(!(getActiveRoom() && !getActiveRoom()->polygon.empty()))
+ return true;
- l.v1 = getActiveRoom()->polygon.vertices[getActiveRoom()->polygon.nVertices-1];
+ l.v1 = getActiveRoom()->polygon.back();
l.v2 = *v;
-
+
return isLineOk(&l);
}
-int isPolygonOk(POLYGON *polygon) {
+bool isPolygonOk(Polygon *polygon) {
LEVEL *lvl = getLevel();
LINE l, l2;
- int i, j;
- if(!polygon->nVertices) return 0;
+ if(polygon->empty()) return false;
- for(i = 0; i < lvl->nRooms; i++) {
- if(!lvl->rooms[i].polygon.nVertices) continue;
+ for(int i = 0; i < lvl->nRooms; i++) {
+ if(lvl->rooms[i].polygon.empty()) continue;
- if(vertexInPolygon(&polygon->vertices[0], &lvl->rooms[i].polygon))
- return 0;
+ if(lvl->rooms[i].polygon.contains(polygon->front()));
+ return false;
- if(vertexInPolygon(&lvl->rooms[i].polygon.vertices[0], polygon))
- return 0;
+ if(polygon->contains(lvl->rooms[i].polygon.front()));
+ return false;
}
- if(polygon->nVertices == 1)
- return 1;
+ if(polygon->size() == 1)
+ return true;
- for(i = 0; i < polygon->nVertices; i++) {
- l.v1 = polygon->vertices[i];
- l.v2 = polygon->vertices[(i+1)%polygon->nVertices];
+ for(Polygon::const_iterator it = polygon->begin(); it != polygon->end(); it++) {
+ Polygon::const_iterator it2 = it+1;
+ if(it2 == polygon->end()) it2 = polygon->begin();
+
+ l.v1 = *it;
+ l.v2 = *it2;
- for(j = 0; j < lvl->nRooms; j++) {
- if(linePolygonIntersection(&l, &lvl->rooms[j].polygon))
- return 0;
+ for(int i = 0; i < lvl->nRooms; i++) {
+ if(lvl->rooms[i].polygon.intersects(&l))
+ return false;
}
- for(j = i+2; j < polygon->nVertices; j++) {
- if(i == 0 && j == polygon->nVertices-1) continue;
-
- l2.v1 = polygon->vertices[j];
- l2.v2 = polygon->vertices[(j+1)%polygon->nVertices];
-
- if(lineIntersection(&l, &l2, NULL) == INTERSECTION_SEGMENT_SEGMENT)
- return 0;
+ if(it2 != polygon->begin()) {
+ for(Polygon::const_iterator it3 = it2+1; it3 != polygon->end(); it3++) {
+ Polygon::const_iterator it4 = it3+1;
+ if(it4 == polygon->end()) it4 = polygon->begin();
+
+ if(it == polygon->begin() && it4 == polygon->begin()) continue;
+
+ l2.v1 = *it3;
+ l2.v2 = *it4;
+
+ if(lineIntersection(&l, &l2, NULL) == INTERSECTION_SEGMENT_SEGMENT)
+ return false;
+ }
}
}
- return 1;
+ return true;
}