diff options
Diffstat (limited to 'src/texture.cpp')
-rw-r--r-- | src/texture.cpp | 67 |
1 files changed, 26 insertions, 41 deletions
diff --git a/src/texture.cpp b/src/texture.cpp index ce1d479..7ef0852 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022 Frederico de Oliveira Linhares + * Copyright 2022-2023 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ #include "texture.hpp" #include "core.hpp" +#include "font.hpp" #include "vk/texture.hpp" void @@ -61,43 +62,27 @@ cg_cTexture_from_image(mrb_state *mrb, mrb_value self) return texture; } -// mrb_value -// cg_cText_from_text(mrb_state *mrb, mrb_value self) -// { -// char *text; -// SDL_Surface *surface = nullptr; -// mrb_bool background; -// struct mrb_value texture = texture_alloc(mrb, self); -// struct cg_color *tx_color_ptr, *bg_color_ptr; -// struct cg_font *font_ptr; -// struct cg_texture *ptr; - -// mrb_get_args( -// mrb, "zdd|d?", &text, &font_ptr, &cg_font_type, -// &tx_color_ptr, &cg_color_type, &bg_color_ptr, &cg_color_type, -// &background); - -// ptr = (struct cg_texture *)DATA_PTR(texture); -// if(ptr) mrb_free(mrb, ptr); -// ptr = (struct cg_texture *)mrb_malloc(mrb, sizeof(struct cg_texture)); - -// // Create surface. -// if(background) -// surface = TTF_RenderUTF8_Shaded( -// font_ptr->data, text, tx_color_ptr->data, bg_color_ptr->data); -// else -// surface = TTF_RenderUTF8_Solid(font_ptr->data, text, tx_color_ptr->data); -// if(surface == nullptr) mrb_raise(mrb, E_RUNTIME_ERROR, TTF_GetError()); - -// // Convert the surface to a texture. -// ptr->data = SDL_CreateTextureFromSurface(cg_core.renderer, surface); -// SDL_FreeSurface(surface); -// if(ptr->data == nullptr) mrb_raise(mrb, E_RUNTIME_ERROR, SDL_GetError()); - -// SDL_QueryTexture(ptr->data, nullptr, nullptr, &ptr->width, &ptr->height); -// mrb_data_init(texture, ptr, &cg_texture_type); -// return texture; -// } +mrb_value +cg_cTexture_from_text(mrb_state *mrb, mrb_value self) +{ + const char *text; + unsigned int width, height; + struct mrb_value texture = texture_alloc(mrb, self); + VK::Font *font_ptr; + std::shared_ptr<VK::Texture> *ptr; + + mrb_get_args(mrb, "dz", &font_ptr, &cg_font_type, &text); + + ptr = (std::shared_ptr<VK::Texture>*)DATA_PTR(texture); + if(ptr) mrb_free(mrb, ptr); + ptr = (std::shared_ptr<VK::Texture>*)mrb_malloc( + mrb, sizeof(std::shared_ptr<VK::Texture>)); + new(ptr)std::shared_ptr<VK::Texture>( + std::make_shared<VK::Texture>(font_ptr, text)); + + mrb_data_init(texture, ptr, &cg_texture_type); + return texture; +} static mrb_value cg_cTexture_width(mrb_state *mrb, mrb_value self) @@ -127,9 +112,9 @@ cg_texture_init(mrb_state *mrb) mrb_define_class_method( mrb, cg_cTexture, "from_image", cg_cTexture_from_image, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(1)); - // mrb_define_class_method( - // mrb, cg_cTexture, "from_text", cg_cText_from_text, - // MRB_ARGS_REQ(3)|MRB_ARGS_OPT(1)); + mrb_define_class_method( + mrb, cg_cTexture, "from_text", cg_cTexture_from_text, + MRB_ARGS_REQ(3)|MRB_ARGS_OPT(2)); mrb_define_method( mrb, cg_cTexture, "width", cg_cTexture_width, MRB_ARGS_NONE()); mrb_define_method( |