mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-21 21:51:22 +02:00
TextImage class.
This commit is contained in:
parent
0a4a254245
commit
af406fbddc
@ -25,8 +25,9 @@ g++ -Wall -g $(sdl2-config --cflags) -c image.cpp -o obj/image.o
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c texture.cpp -o obj/texture.o
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c sprite.cpp -o obj/sprite.o
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c camera.cpp -o obj/camera.o
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c text_image.cpp -o obj/text_image.o
|
||||
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c true_type_font.cpp -o obj/true_type_font.o
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c true_type_font.cpp -o obj/true_type_font.o
|
||||
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c button.cpp -o obj/button.o
|
||||
|
||||
@ -36,5 +37,5 @@ g++ -Wall -g $(sdl2-config --cflags) -c main_scene.cpp -o obj/main_scene.o
|
||||
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c main.cpp -o obj/main.o
|
||||
|
||||
g++ -o bin/program obj/math.o obj/rect2.o obj/color.o obj/vector2.o obj/vector3.o obj/string.o obj/renderer.o obj/image.o obj/texture.o obj/sprite.o obj/camera.o obj/true_type_font.o obj/button.o obj/scene.o obj/application.o obj/main_scene.o obj/main.o $(sdl2-config --libs) -lSDL2_ttf
|
||||
g++ -o bin/program obj/math.o obj/rect2.o obj/color.o obj/vector2.o obj/vector3.o obj/string.o obj/renderer.o obj/image.o obj/texture.o obj/sprite.o obj/camera.o obj/text_image.o obj/true_type_font.o obj/button.o obj/scene.o obj/application.o obj/main_scene.o obj/main.o $(sdl2-config --libs) -lSDL2_ttf
|
||||
|
||||
|
@ -221,6 +221,15 @@ SDL_Surface *Image::get_surface() {
|
||||
|
||||
void Image::set_surface(SDL_Surface *surface) {
|
||||
_surface = surface;
|
||||
|
||||
if (_surface != nullptr && _surface->format->format != SDL_PIXELFORMAT_RGBA8888) {
|
||||
//Nem ARGB8888 as formátum, konvertáljuk át
|
||||
SDL_Surface *n = SDL_ConvertSurfaceFormat(_surface, SDL_PIXELFORMAT_RGBA8888, 0);
|
||||
|
||||
free();
|
||||
|
||||
_surface = n;
|
||||
}
|
||||
}
|
||||
|
||||
Image::Image() {
|
||||
|
@ -26,6 +26,7 @@ void MainScene::render() {
|
||||
b3->render();
|
||||
|
||||
_s->draw();
|
||||
_s2->draw();
|
||||
}
|
||||
|
||||
void MainScene::on_first_button_clicked() {
|
||||
@ -50,6 +51,10 @@ MainScene::MainScene() {
|
||||
_iit = new Texture(_ii);
|
||||
_s = new Sprite(_iit);
|
||||
|
||||
_teximg = new TextImage(_font, "daDAdadad");
|
||||
_iit2 = new Texture(_teximg->get_image());
|
||||
_s2 = new Sprite(_iit2);
|
||||
|
||||
b1 = new Button();
|
||||
b1->transform = Rect2(0, 0, 100, 100);
|
||||
b1->up = new Sprite(_texture);
|
||||
@ -96,6 +101,10 @@ MainScene::~MainScene() {
|
||||
delete _iit;
|
||||
delete _ii;
|
||||
|
||||
delete _teximg;
|
||||
delete _s2;
|
||||
delete _iit2;
|
||||
|
||||
delete _font;
|
||||
delete _texture;
|
||||
delete _image;
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "camera.h"
|
||||
#include "button.h"
|
||||
#include "true_type_font.h"
|
||||
#include "text_image.h"
|
||||
|
||||
class MainScene : public Scene {
|
||||
public:
|
||||
@ -32,7 +33,10 @@ public:
|
||||
TrueTypeFont *_font;
|
||||
Image *_ii;
|
||||
Texture *_iit;
|
||||
Texture *_iit2;
|
||||
Sprite *_s;
|
||||
Sprite *_s2;
|
||||
TextImage *_teximg;
|
||||
|
||||
Button *b1;
|
||||
Button *b2;
|
||||
|
97
03_sdl_basics/16_sdl_ttf/text_image.cpp
Normal file
97
03_sdl_basics/16_sdl_ttf/text_image.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include "text_image.h"
|
||||
|
||||
TextImage::TextType TextImage::get_text_type() {
|
||||
return _text_type;
|
||||
}
|
||||
void TextImage::set_text_type(const TextImage::TextType type) {
|
||||
_text_type = type;
|
||||
}
|
||||
|
||||
TrueTypeFont *TextImage::get_font() {
|
||||
return _font;
|
||||
}
|
||||
void TextImage::set_font(TrueTypeFont *font) {
|
||||
_font = font;
|
||||
}
|
||||
|
||||
String TextImage::get_text() {
|
||||
return _text;
|
||||
}
|
||||
void TextImage::set_text(const String &text) {
|
||||
_text = text;
|
||||
|
||||
render_image();
|
||||
}
|
||||
|
||||
Color TextImage::get_fg_color() {
|
||||
return _fg;
|
||||
}
|
||||
void TextImage::get_fg_color(const Color &color) {
|
||||
_fg = color;
|
||||
}
|
||||
|
||||
Color TextImage::get_bg_color() {
|
||||
return _bg;
|
||||
}
|
||||
void TextImage::get_bg_color(const Color &color) {
|
||||
_bg = color;
|
||||
}
|
||||
|
||||
Image *TextImage::get_image() {
|
||||
return _image;
|
||||
}
|
||||
|
||||
int TextImage::get_width() {
|
||||
return _image->get_width();
|
||||
}
|
||||
int TextImage::get_height() {
|
||||
return _image->get_height();
|
||||
}
|
||||
|
||||
void TextImage::render_image() {
|
||||
_image->free();
|
||||
|
||||
if (!_font) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (_text_type) {
|
||||
case SOLID:
|
||||
_font->render_text_solid(_text, _fg, _image);
|
||||
break;
|
||||
case SHADED:
|
||||
_font->render_text_shaded(_text, _fg, _bg, _image);
|
||||
break;
|
||||
case BLENDED:
|
||||
_font->render_text_blended(_text, _fg, _image);
|
||||
break;
|
||||
case BLENDED_WRAPPED:
|
||||
_font->render_text_blended_wrapped(_text, _fg, 100, _image);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TextImage::TextImage() {
|
||||
_font = nullptr;
|
||||
_text_type = BLENDED;
|
||||
_image = new Image();
|
||||
}
|
||||
TextImage::TextImage(TrueTypeFont *font) {
|
||||
_font = font;
|
||||
_text_type = BLENDED;
|
||||
_image = new Image();
|
||||
}
|
||||
TextImage::TextImage(TrueTypeFont *font, const String &text) {
|
||||
_font = font;
|
||||
_text_type = BLENDED;
|
||||
_text = text;
|
||||
_image = new Image();
|
||||
|
||||
render_image();
|
||||
}
|
||||
TextImage::~TextImage() {
|
||||
delete _image;
|
||||
}
|
57
03_sdl_basics/16_sdl_ttf/text_image.h
Normal file
57
03_sdl_basics/16_sdl_ttf/text_image.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef TEXT_IMAGE_H
|
||||
#define TEXT_IMAGE_H
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#include "color.h"
|
||||
#include "image.h"
|
||||
#include "string.h"
|
||||
#include "true_type_font.h"
|
||||
#include "vector2.h"
|
||||
|
||||
class TextImage {
|
||||
public:
|
||||
enum TextType {
|
||||
SOLID,
|
||||
SHADED,
|
||||
BLENDED,
|
||||
BLENDED_WRAPPED
|
||||
};
|
||||
|
||||
TextType get_text_type();
|
||||
void set_text_type(const TextType type);
|
||||
|
||||
TrueTypeFont *get_font();
|
||||
void set_font(TrueTypeFont *font);
|
||||
|
||||
String get_text();
|
||||
void set_text(const String &text);
|
||||
|
||||
Color get_fg_color();
|
||||
void get_fg_color(const Color &color);
|
||||
|
||||
Color get_bg_color();
|
||||
void get_bg_color(const Color &color);
|
||||
|
||||
Image *get_image();
|
||||
|
||||
int get_width();
|
||||
int get_height();
|
||||
|
||||
void render_image();
|
||||
|
||||
TextImage();
|
||||
TextImage(TrueTypeFont *font);
|
||||
TextImage(TrueTypeFont *font, const String &text);
|
||||
virtual ~TextImage();
|
||||
|
||||
private:
|
||||
TrueTypeFont *_font;
|
||||
String _text;
|
||||
Image *_image;
|
||||
TextType _text_type;
|
||||
Color _fg;
|
||||
Color _bg;
|
||||
};
|
||||
|
||||
#endif
|
@ -87,92 +87,202 @@ Vector2 TrueTypeFont::get_size_utf8(const String &text) {
|
||||
return Vector2(w, h);
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_text_solid(const String &text, const Color &fg) {
|
||||
Image *TrueTypeFont::render_text_solid(const String &text, const Color &fg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderText_Solid(_font, text.c_str(), fg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderText_Solid(_font, text.c_str(), fg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_utf8_solid(const String &text, const Color &fg) {
|
||||
Image *TrueTypeFont::render_utf8_solid(const String &text, const Color &fg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderUTF8_Solid(_font, text.c_str(), fg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderUTF8_Solid(_font, text.c_str(), fg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_glyph_solid(const Uint16 ch, const Color &fg) {
|
||||
Image *TrueTypeFont::render_glyph_solid(const Uint16 ch, const Color &fg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderGlyph_Solid(_font, ch, fg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderGlyph_Solid(_font, ch, fg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_text_shaded(const String &text, const Color &fg, const Color &bg) {
|
||||
Image *TrueTypeFont::render_text_shaded(const String &text, const Color &fg, const Color &bg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderText_Shaded(_font, text.c_str(), fg.to_sdl_color(), bg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderText_Shaded(_font, text.c_str(), fg.to_sdl_color(), bg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_utf8_shaded(const String &text, const Color &fg, const Color &bg) {
|
||||
Image *TrueTypeFont::render_utf8_shaded(const String &text, const Color &fg, const Color &bg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderUTF8_Shaded(_font, text.c_str(), fg.to_sdl_color(), bg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderUTF8_Shaded(_font, text.c_str(), fg.to_sdl_color(), bg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_glyph_shaded(const Uint16 ch, const Color &fg, const Color &bg) {
|
||||
Image *TrueTypeFont::render_glyph_shaded(const Uint16 ch, const Color &fg, const Color &bg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderGlyph_Shaded(_font, ch, fg.to_sdl_color(), bg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderGlyph_Shaded(_font, ch, fg.to_sdl_color(), bg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_text_blended(const String &text, const Color &fg) {
|
||||
Image *TrueTypeFont::render_text_blended(const String &text, const Color &fg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderText_Blended(_font, text.c_str(), fg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderText_Blended(_font, text.c_str(), fg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_utf8_blended(const String &text, const Color &fg) {
|
||||
Image *TrueTypeFont::render_utf8_blended(const String &text, const Color &fg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderUTF8_Blended(_font, text.c_str(), fg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderUTF8_Blended(_font, text.c_str(), fg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_text_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length) {
|
||||
Image *TrueTypeFont::render_text_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderText_Blended_Wrapped(_font, text.c_str(), fg.to_sdl_color(), wrap_length));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderText_Blended_Wrapped(_font, text.c_str(), fg.to_sdl_color(), wrap_length));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_utf8_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length) {
|
||||
Image *TrueTypeFont::render_utf8_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderUTF8_Blended_Wrapped(_font, text.c_str(), fg.to_sdl_color(), wrap_length));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderUTF8_Blended_Wrapped(_font, text.c_str(), fg.to_sdl_color(), wrap_length));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
Image *TrueTypeFont::render_glyph_blended(const Uint16 ch, const Color &fg) {
|
||||
Image *TrueTypeFont::render_glyph_blended(const Uint16 ch, const Color &fg, Image *img) {
|
||||
if (!_font) {
|
||||
return nullptr;
|
||||
if (img) {
|
||||
img->free();
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
return new Image(TTF_RenderGlyph_Blended(_font, ch, fg.to_sdl_color()));
|
||||
if (!img) {
|
||||
img = new Image();
|
||||
}
|
||||
|
||||
img->set_surface(TTF_RenderGlyph_Blended(_font, ch, fg.to_sdl_color()));
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
void TrueTypeFont::load(const String &file_name, const int ptsize, const int index) {
|
||||
|
@ -50,21 +50,21 @@ public:
|
||||
Vector2 get_size_utf8(const String &text);
|
||||
Vector2 get_size_unicode(const String &text);
|
||||
|
||||
Image *render_text_solid(const String &text, const Color &fg);
|
||||
Image *render_utf8_solid(const String &text, const Color &fg);
|
||||
Image *render_glyph_solid(const Uint16 ch, const Color &fg);
|
||||
Image *render_text_solid(const String &text, const Color &fg, Image *img = nullptr);
|
||||
Image *render_utf8_solid(const String &text, const Color &fg, Image *img = nullptr);
|
||||
Image *render_glyph_solid(const Uint16 ch, const Color &fg, Image *img = nullptr);
|
||||
|
||||
Image *render_text_shaded(const String &text, const Color &fg, const Color &bg);
|
||||
Image *render_utf8_shaded(const String &text, const Color &fg, const Color &bg);
|
||||
Image *render_glyph_shaded(const Uint16 ch, const Color &fg, const Color &bg);
|
||||
Image *render_text_shaded(const String &text, const Color &fg, const Color &bg, Image *img = nullptr);
|
||||
Image *render_utf8_shaded(const String &text, const Color &fg, const Color &bg, Image *img = nullptr);
|
||||
Image *render_glyph_shaded(const Uint16 ch, const Color &fg, const Color &bg, Image *img = nullptr);
|
||||
|
||||
Image *render_text_blended(const String &text, const Color &fg);
|
||||
Image *render_utf8_blended(const String &text, const Color &fg);
|
||||
Image *render_text_blended(const String &text, const Color &fg, Image *img = nullptr);
|
||||
Image *render_utf8_blended(const String &text, const Color &fg, Image *img = nullptr);
|
||||
|
||||
Image *render_text_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length);
|
||||
Image *render_utf8_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length);
|
||||
Image *render_text_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length, Image *img = nullptr);
|
||||
Image *render_utf8_blended_wrapped(const String &text, const Color &fg, Uint32 wrap_length, Image *img = nullptr);
|
||||
|
||||
Image *render_glyph_blended(const Uint16 ch, const Color &fg);
|
||||
Image *render_glyph_blended(const Uint16 ch, const Color &fg, Image *img = nullptr);
|
||||
|
||||
void load(const String &file_name, const int ptsize, const int index = -1);
|
||||
void free();
|
||||
|
Loading…
Reference in New Issue
Block a user