Phyzix
Loading...
Searching...
No Matches
Scene.h
1//
2// Created by shams on 8/7/24.
3//
4
5#ifndef PHYZIX_SCENE_H
6#define PHYZIX_SCENE_H
7
8
9#include <cstdint>
10#include <stddef.h>
11#include <algorithm>
12#include "Drawable.h"
13#include "Boundary.h"
14
15class Scene {
16public:
17 uint16_t x_size;
18 uint16_t y_size;
19 Drawable ** drawables = nullptr;
20 size_t drawableCount = 0;
21 size_t drawableCapacity = 0;
22public:
23 Boundaries boundaries;
24
25 void resizeDrawables(size_t newCapacity) {
26 auto ** newDrawables = new Drawable*[newCapacity];
27
28 if (drawables) {
29 std::copy(drawables, drawables + drawableCount, newDrawables);
30 delete[] drawables;
31 }
32
33 drawables = newDrawables;
34 drawableCapacity = newCapacity;
35 }
36
37public:
38 Scene(uint16_t x_size, uint16_t y_size) {
39 this->x_size = x_size;
40 this->y_size = y_size;
41 };
42
43 void addDrawable(Drawable* drawable) {
44 if (drawableCount == drawableCapacity) {
45 resizeDrawables(drawableCapacity == 0 ? 1 : drawableCapacity * 2);
46 }
47 drawables[drawableCount++] = drawable;
48 }
49
50 void removeDrawable(Drawable* drawable) {
51 for (size_t i = 0; i < drawableCount; ++i) {
52 if (drawables[i] == drawable) {
53 // Shift remaining elements to the left
54 for (size_t j = i; j < drawableCount - 1; ++j) {
55 drawables[j] = drawables[j + 1];
56 }
57 --drawableCount;
58 break;
59 }
60 }
61 }
62
63 void forEachDrawable(void (* func)(Drawable*)) {
64 for (size_t i = 0; i < drawableCount; ++i) {
65 func(drawables[i]);
66 }
67 }
68};
69
70
71#endif //PHYZIX_SCENE_H
Definition Drawable.h:11
Definition Scene.h:15
A struct to hold a list of boundaries.
Definition Boundary.h:21