Add basic app interface
- Add pool struct for data pools
This commit is contained in:
+3
-3
@@ -5,8 +5,8 @@ int main() {
|
||||
|
||||
Window w({800, 600}, "Render Test");
|
||||
|
||||
ImageID img = w.load_image("./assets/images/arrow.png", true);
|
||||
FontID font = w.load_font(
|
||||
int img = w.load_image("./assets/images/arrow.png", true);
|
||||
int font = w.load_font(
|
||||
"./assets/fonts/charybdis.ttf",
|
||||
24,
|
||||
false,
|
||||
@@ -15,7 +15,7 @@ int main() {
|
||||
true
|
||||
);
|
||||
|
||||
TextID text = w.create_text(font, "Hello Engine", 12, {255, 255, 255, 255});
|
||||
int text = w.create_text(font, "Hello Engine", 12, {255, 255, 255, 255});
|
||||
|
||||
float t = 0.0f;
|
||||
|
||||
|
||||
+30
-70
@@ -15,13 +15,6 @@ Window::Window(Vec2<float> size, const char *title) {
|
||||
|
||||
compute_transform();
|
||||
|
||||
images.reserve(100);
|
||||
free_image_indices.reserve(100);
|
||||
fonts.reserve(100);
|
||||
free_font_indices.reserve(100);
|
||||
texts.reserve(100);
|
||||
free_text_indices.reserve(100);
|
||||
|
||||
text_input_active = false;
|
||||
|
||||
white_tex = SDL_CreateTexture(
|
||||
@@ -37,15 +30,15 @@ Window::Window(Vec2<float> size, const char *title) {
|
||||
};
|
||||
|
||||
Window::~Window() {
|
||||
for (auto tex : images)
|
||||
for (auto tex : image_pool.all())
|
||||
if (tex)
|
||||
SDL_DestroyTexture(tex);
|
||||
|
||||
for (auto tex : texts)
|
||||
for (auto tex : text_pool.all())
|
||||
if (tex)
|
||||
SDL_DestroyTexture(tex);
|
||||
|
||||
for (auto &f : fonts)
|
||||
for (auto &f : font_pool.all())
|
||||
if (f.font)
|
||||
TTF_CloseFont(f.font);
|
||||
|
||||
@@ -55,7 +48,7 @@ Window::~Window() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
ImageID Window::load_image(const char *path, bool pixel_art) {
|
||||
int Window::load_image(const char *path, bool pixel_art) {
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, path);
|
||||
|
||||
if (pixel_art) {
|
||||
@@ -67,31 +60,20 @@ ImageID Window::load_image(const char *path, bool pixel_art) {
|
||||
return UINT32_MAX;
|
||||
}
|
||||
|
||||
if (!free_image_indices.empty()) {
|
||||
ImageID id = free_image_indices.back();
|
||||
free_image_indices.pop_back();
|
||||
images[id] = texture;
|
||||
return id;
|
||||
} else {
|
||||
images.push_back(texture);
|
||||
return images.size() - 1;
|
||||
}
|
||||
|
||||
return UINT32_MAX;
|
||||
return image_pool.acquire(texture);
|
||||
}
|
||||
|
||||
void Window::unload_image(ImageID image_id) {
|
||||
if (image_id >= images.size() || images[image_id] == nullptr) {
|
||||
void Window::unload_image(int image_id) {
|
||||
if (!image_pool.is_valid(image_id)) {
|
||||
fprintf(stderr, "Invalid image ID: %d\n", image_id);
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_DestroyTexture(images[image_id]);
|
||||
images[image_id] = nullptr;
|
||||
free_image_indices.push_back(image_id);
|
||||
SDL_DestroyTexture(image_pool[image_id]);
|
||||
image_pool.release(image_id);
|
||||
}
|
||||
|
||||
FontID Window::load_font(const char *path, int font_size, bool bold, bool italic, bool underline, bool pixel_art) {
|
||||
int Window::load_font(const char *path, int font_size, bool bold, bool italic, bool underline, bool pixel_art) {
|
||||
TTF_Font *font = TTF_OpenFont(path, font_size);
|
||||
|
||||
if (font == nullptr) {
|
||||
@@ -109,38 +91,27 @@ FontID Window::load_font(const char *path, int font_size, bool bold, bool italic
|
||||
|
||||
TTF_SetFontStyle(font, style);
|
||||
|
||||
if (!free_font_indices.empty()) {
|
||||
FontID id = free_font_indices.back();
|
||||
free_font_indices.pop_back();
|
||||
fonts[id] = {font, pixel_art};
|
||||
return id;
|
||||
} else {
|
||||
fonts.push_back({font, pixel_art});
|
||||
return fonts.size() - 1;
|
||||
}
|
||||
|
||||
return UINT32_MAX;
|
||||
return font_pool.acquire({font, pixel_art});
|
||||
}
|
||||
|
||||
void Window::unload_font(FontID font_id) {
|
||||
if (font_id >= fonts.size() || fonts[font_id].font == nullptr) {
|
||||
void Window::unload_font(int font_id) {
|
||||
if (!font_pool.is_valid(font_id)) {
|
||||
fprintf(stderr, "Invalid font ID: %d\n", font_id);
|
||||
return;
|
||||
}
|
||||
|
||||
TTF_CloseFont(fonts[font_id].font);
|
||||
fonts[font_id].font = nullptr;
|
||||
free_font_indices.push_back(font_id);
|
||||
TTF_CloseFont(font_pool[font_id].font);
|
||||
font_pool.release(font_id);
|
||||
}
|
||||
|
||||
TextID Window::create_text(FontID font_id, const char *text, uint32_t length, Color color) {
|
||||
if (font_id >= fonts.size() || fonts[font_id].font == nullptr) {
|
||||
int Window::create_text(int font_id, const char *text, uint32_t length, Color color) {
|
||||
if (!font_pool.is_valid(font_id)) {
|
||||
fprintf(stderr, "Invalid font ID: %d\n", font_id);
|
||||
return UINT32_MAX;
|
||||
}
|
||||
|
||||
SDL_Color sdl_color = {color.r, color.g, color.b, color.a};
|
||||
SDL_Surface *surface = TTF_RenderText_Solid(fonts[font_id].font, text, length, sdl_color);
|
||||
SDL_Surface *surface = TTF_RenderText_Solid(font_pool[font_id].font, text, length, sdl_color);
|
||||
|
||||
if (surface == nullptr) {
|
||||
fprintf(stderr, "Failed to create text surface: %s\n", SDL_GetError());
|
||||
@@ -155,35 +126,24 @@ TextID Window::create_text(FontID font_id, const char *text, uint32_t length, Co
|
||||
return UINT32_MAX;
|
||||
}
|
||||
|
||||
if (fonts[font_id].pixel_art)
|
||||
if (font_pool[font_id].pixel_art)
|
||||
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
|
||||
|
||||
if (!free_text_indices.empty()) {
|
||||
TextID id = free_text_indices.back();
|
||||
free_text_indices.pop_back();
|
||||
texts[id] = texture;
|
||||
return id;
|
||||
} else {
|
||||
texts.push_back(texture);
|
||||
return texts.size() - 1;
|
||||
}
|
||||
|
||||
return UINT32_MAX;
|
||||
return text_pool.acquire(texture);
|
||||
}
|
||||
|
||||
void Window::unload_text(TextID text_id) {
|
||||
if (text_id >= texts.size() || texts[text_id] == nullptr) {
|
||||
void Window::unload_text(int text_id) {
|
||||
if (!text_pool.is_valid(text_id)) {
|
||||
fprintf(stderr, "Invalid text ID: %d\n", text_id);
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_DestroyTexture(texts[text_id]);
|
||||
texts[text_id] = nullptr;
|
||||
free_text_indices.push_back(text_id);
|
||||
SDL_DestroyTexture(text_pool[text_id]);
|
||||
text_pool.release(text_id);
|
||||
}
|
||||
|
||||
bool Window::write(TextID text_id, Vec2<float> position, float z, float angle, Vec2<float> scale) {
|
||||
if (text_id >= texts.size() || texts[text_id] == nullptr) {
|
||||
bool Window::write(int text_id, Vec2<float> position, float z, float angle, Vec2<float> scale) {
|
||||
if (!text_pool.is_valid(text_id)) {
|
||||
fprintf(stderr, "Invalid text ID: %d\n", text_id);
|
||||
return false;
|
||||
}
|
||||
@@ -202,8 +162,8 @@ bool Window::write(TextID text_id, Vec2<float> position, float z, float angle, V
|
||||
return true;
|
||||
};
|
||||
|
||||
bool Window::draw(ImageID image_id, Vec2<float> position, float z, float angle, Vec2<float> scale, float alpha) {
|
||||
if (image_id >= images.size() || images[image_id] == nullptr) {
|
||||
bool Window::draw(int image_id, Vec2<float> position, float z, float angle, Vec2<float> scale, float alpha) {
|
||||
if (!image_pool.is_valid(image_id)) {
|
||||
fprintf(stderr, "Invalid image ID: %d\n", image_id);
|
||||
return false;
|
||||
}
|
||||
@@ -284,7 +244,7 @@ void Window::render() {
|
||||
for (const RenderInstruction &instruction : render_instructions) {
|
||||
switch (instruction.type) {
|
||||
case RenderInstruction::DRAW_TEXT: {
|
||||
SDL_Texture *texture = texts[instruction.draw_text.text_id];
|
||||
SDL_Texture *texture = text_pool[instruction.draw_text.text_id];
|
||||
|
||||
SDL_FlipMode flip = SDL_FLIP_NONE;
|
||||
|
||||
@@ -317,7 +277,7 @@ void Window::render() {
|
||||
} break;
|
||||
|
||||
case RenderInstruction::DRAW_IMAGE: {
|
||||
SDL_Texture *texture = images[instruction.draw_image.image_id];
|
||||
SDL_Texture *texture = image_pool[instruction.draw_image.image_id];
|
||||
|
||||
float w, h;
|
||||
SDL_GetTextureSize(texture, &w, &h);
|
||||
|
||||
Reference in New Issue
Block a user