Add basic app interface
- Add pool struct for data pools
This commit is contained in:
@@ -14,6 +14,16 @@ template <typename T>
|
||||
struct Vec2 {
|
||||
T x, y;
|
||||
|
||||
template <typename U>
|
||||
Vec2<float> operator*(Vec2<U> s) {
|
||||
return {x * s.x, y * s.y};
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
Vec2<float> operator/(Vec2<U> s) {
|
||||
return {x / s.x, y / s.y};
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
Vec2 operator*(U s) const {
|
||||
return {x * s, y * s};
|
||||
@@ -98,6 +108,10 @@ struct Rect {
|
||||
};
|
||||
}
|
||||
|
||||
static Rect from_size(Vec2<float> position, Vec2<float> size) {
|
||||
return Rect(position, position + size);
|
||||
}
|
||||
|
||||
bool contains(const Vec2<float> &point) const {
|
||||
return point.x >= diagonal.start.x && point.x <= diagonal.end.x &&
|
||||
point.y >= diagonal.start.y && point.y <= diagonal.end.y;
|
||||
@@ -142,4 +156,44 @@ struct Rect {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Pool {
|
||||
Pool() {
|
||||
items.reserve(20);
|
||||
free_indices.reserve(20);
|
||||
}
|
||||
|
||||
T &operator[](int index) {
|
||||
return items[index];
|
||||
}
|
||||
|
||||
const std::vector<T> &all() {
|
||||
return items;
|
||||
}
|
||||
|
||||
int acquire(const T &item) {
|
||||
if (!free_indices.empty()) {
|
||||
int index = free_indices.back();
|
||||
free_indices.pop_back();
|
||||
items[index] = item;
|
||||
return index;
|
||||
} else {
|
||||
items.push_back(item);
|
||||
return items.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
void release(int index) {
|
||||
free_indices.push_back(index);
|
||||
}
|
||||
|
||||
bool is_valid(size_t index) const {
|
||||
return index >= 0 && index < items.size() && std::find(free_indices.begin(), free_indices.end(), index) == free_indices.end();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<T> items;
|
||||
std::vector<int> free_indices;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user