move bboxes
This commit is contained in:
parent
e31683d2fe
commit
e86be28cc0
|
@ -19,12 +19,18 @@
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
void draw(const freling::BoundingBox& box,
|
Rectangle rect_from_bbox(const freling::BoundingBox& box,
|
||||||
const Vector2& offset,
|
const Vector2& offset) {
|
||||||
const Color& color) {
|
|
||||||
const Rectangle rect = {offset.x + box.x, offset.y + box.y,
|
const Rectangle rect = {offset.x + box.x, offset.y + box.y,
|
||||||
static_cast<float>(box.width),
|
static_cast<float>(box.width),
|
||||||
static_cast<float>(box.height)};
|
static_cast<float>(box.height)};
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(const freling::BoundingBox& box,
|
||||||
|
const Vector2& offset,
|
||||||
|
const Color& color) {
|
||||||
|
const Rectangle rect = rect_from_bbox(box, offset);
|
||||||
DrawRectangleRec(rect, ColorAlpha(color, 0.3));
|
DrawRectangleRec(rect, ColorAlpha(color, 0.3));
|
||||||
DrawRectangleLinesEx(rect, 3, color);
|
DrawRectangleLinesEx(rect, 3, color);
|
||||||
}
|
}
|
||||||
|
@ -78,7 +84,8 @@ int main(int argc, const char* argv[]) {
|
||||||
bboxes.push_back(freling::BoundingBox({x, y, w, h}));
|
bboxes.push_back(freling::BoundingBox({x, y, w, h}));
|
||||||
i += 4;
|
i += 4;
|
||||||
}
|
}
|
||||||
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE};
|
const std::vector<Color> bbox_colors = {RED, GREEN, BLUE,
|
||||||
|
YELLOW, ORANGE, PURPLE};
|
||||||
std::vector<freling::BoundingBox> packed_bboxes;
|
std::vector<freling::BoundingBox> packed_bboxes;
|
||||||
|
|
||||||
Vector2 win_size = {800, 450};
|
Vector2 win_size = {800, 450};
|
||||||
|
@ -102,8 +109,36 @@ int main(int argc, const char* argv[]) {
|
||||||
SetWindowSize(win_size.x, win_size.y);
|
SetWindowSize(win_size.x, win_size.y);
|
||||||
|
|
||||||
const Vector2 b_size = {150, 30};
|
const Vector2 b_size = {150, 30};
|
||||||
|
Vector2 mouse_pos = {0};
|
||||||
|
Vector2 mouse_delta = {0};
|
||||||
|
int b_moving_idx = -1;
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
|
//
|
||||||
|
// Update
|
||||||
|
//
|
||||||
|
mouse_pos = GetMousePosition();
|
||||||
|
mouse_delta = GetMouseDelta();
|
||||||
|
for (int b = 0, size = bboxes.size(); b < size; ++b) {
|
||||||
|
const auto& box = bboxes[b];
|
||||||
|
const Rectangle rect = rect_from_bbox(box, in_offset);
|
||||||
|
if (CheckCollisionPointRec(mouse_pos, rect) and
|
||||||
|
IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
||||||
|
b_moving_idx = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
|
||||||
|
b_moving_idx = -1;
|
||||||
|
}
|
||||||
|
if (b_moving_idx != -1) {
|
||||||
|
auto& box = bboxes[b_moving_idx];
|
||||||
|
box.x += mouse_delta.x;
|
||||||
|
box.y += mouse_delta.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Draw
|
||||||
|
//
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
@ -150,7 +185,7 @@ int main(int argc, const char* argv[]) {
|
||||||
|
|
||||||
int c = 0;
|
int c = 0;
|
||||||
for (const auto& b : bboxes) {
|
for (const auto& b : bboxes) {
|
||||||
const auto& color = bbox_colors[c % bboxes.size()];
|
const auto& color = bbox_colors[c % bbox_colors.size()];
|
||||||
draw(b, in_offset, color);
|
draw(b, in_offset, color);
|
||||||
++c;
|
++c;
|
||||||
};
|
};
|
||||||
|
@ -167,7 +202,7 @@ int main(int argc, const char* argv[]) {
|
||||||
|
|
||||||
int c = 0;
|
int c = 0;
|
||||||
for (const auto& b : packed_bboxes) {
|
for (const auto& b : packed_bboxes) {
|
||||||
const auto& color = bbox_colors[c % bboxes.size()];
|
const auto& color = bbox_colors[c % bbox_colors.size()];
|
||||||
draw(b, pack_offset, color);
|
draw(b, pack_offset, color);
|
||||||
++c;
|
++c;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue