TEST(RectTest, SDL) { // SDL const SDL_Rect sdl_rect_result = Rect(50, 50, 100, 100).to_sdl(); const SDL_Rect sdl_rect_expected{50, 50, 50, 50}; ASSERT_TRUE(SDL_RectEquals(&sdl_rect_result, &sdl_rect_expected)); ASSERT_EQ(Rect(SDL_Rect{50, 50, 50, 50}), Rect(50, 50, 100, 100)); }
/* * SDL.rectEquals(r1, r2) * * Arguments: * r1 the first rectangle * r2 the second rectangle * * Returns: * True if equals */ static int l_rectEquals(lua_State *L) { SDL_Rect a, b; videoGetRect(L, 1, &a); videoGetRect(L, 2, &a); return commonPush(L, "b", SDL_RectEquals(&a, &b)); }
bool operator ==(const Rect& lhs, const Rect& rhs) { SDL_Rect a; SDL_Rect b; return SDL_RectEquals(lhs.copyInto(&a), rhs.copyInto(&b)) == SDL_TRUE; }
bool operator==(const TileNode& a, const TileNode& b) { return SDL_RectEquals(&a.rect, &b.rect); }
std::vector<SDL_Rect> getPath(const SDL_Rect& start, const SDL_Rect& goal, const TileMap& map, const std::vector<SDL_Rect> colliders) { TileNode initialNode; initialNode.rect = start; initialNode.parent = nullptr; initialNode.g = 0; initialNode.h = manhattanHeuristic(start, goal, map) * 10; // std::vector<TileNode> openList { initialNode }; std::vector<TileNode> openList; openList.reserve(map.h * map.w); openList.push_back(initialNode); std::vector<TileNode> closedList; closedList.reserve(map.h * map.w); std::vector<SDL_Rect> path; bool done = false; while (!done) { auto currentIt = std::min_element(openList.begin(), openList.end()); if (currentIt == openList.end()) { std::cout << "DEU PAU" << std::endl; for (auto& e : openList) { std::cout << e.rect.x << "," << e.rect.y << std::endl; } return path; } else { } closedList.push_back(*currentIt); TileNode& current = closedList.back(); SDL_Rect currentRect = current.rect; int currentG = current.g; if ( SDL_RectEquals(¤tRect, &goal) ) { path.push_back(goal); TileNode *parent = current.parent; while (parent) { path.push_back(parent->rect); parent = parent->parent; } done = true; break; } auto new_end = std::remove(openList.begin(), openList.end(), current); openList.erase(new_end, openList.end()); // std::cout << std::endl << "ATUAL: " << currentRect.x << "," << currentRect.y << std::endl; auto neighbourhood = getNeighborhood(current, goal, map, colliders); for (auto& neighbour : neighbourhood) { // std::cout << "VIZINHO: " << neighbour.rect.x << "," << neighbour.rect.y << std::endl; if (std::find(closedList.begin(), closedList.end(), neighbour) != closedList.end()) continue; if (std::find(openList.begin(), openList.end(), neighbour) != openList.end()) { int difX = ceil((neighbour.rect.x - currentRect.x) / map.tileW); int difY = ceil((neighbour.rect.y - currentRect.y) / map.tileH); bool diagonal = difX != 0 && difY != 0; int score = currentG + (diagonal ? 14 : 10); if (score < neighbour.g) { neighbour.parent = ¤t; } } else { // if (clearance(neighbour.rect, map, colliders) > 1 || SDL_RectEquals(&neighbour.rect, &goal)) { openList.push_back(neighbour); } } } } return path; }
bool operator==(const SDL_Rect& a, const SDL_Rect& b) { return SDL_RectEquals(&a, &b) != SDL_FALSE; }