mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-21 21:51:22 +02:00
TextSprite class.
This commit is contained in:
parent
af406fbddc
commit
62b187563f
@ -26,6 +26,7 @@ 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 text_sprite.cpp -o obj/text_sprite.o
|
||||
|
||||
g++ -Wall -g $(sdl2-config --cflags) -c true_type_font.cpp -o obj/true_type_font.o
|
||||
|
||||
@ -37,5 +38,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/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
|
||||
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/text_sprite.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
|
||||
|
||||
|
@ -25,8 +25,9 @@ void MainScene::render() {
|
||||
b2->render();
|
||||
b3->render();
|
||||
|
||||
_s->draw();
|
||||
_s2->draw();
|
||||
//_s->draw();
|
||||
//_s2->draw();
|
||||
_ts->draw();
|
||||
}
|
||||
|
||||
void MainScene::on_first_button_clicked() {
|
||||
@ -55,6 +56,9 @@ MainScene::MainScene() {
|
||||
_iit2 = new Texture(_teximg->get_image());
|
||||
_s2 = new Sprite(_iit2);
|
||||
|
||||
_ts = new TextSprite(_font, "Aqqqqqqq", Color(255, 0, 0));
|
||||
_ts->set_y(50);
|
||||
|
||||
b1 = new Button();
|
||||
b1->transform = Rect2(0, 0, 100, 100);
|
||||
b1->up = new Sprite(_texture);
|
||||
@ -105,6 +109,8 @@ MainScene::~MainScene() {
|
||||
delete _s2;
|
||||
delete _iit2;
|
||||
|
||||
delete _ts;
|
||||
|
||||
delete _font;
|
||||
delete _texture;
|
||||
delete _image;
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "button.h"
|
||||
#include "true_type_font.h"
|
||||
#include "text_image.h"
|
||||
#include "text_sprite.h"
|
||||
|
||||
class MainScene : public Scene {
|
||||
public:
|
||||
@ -37,6 +38,8 @@ public:
|
||||
Sprite *_s;
|
||||
Sprite *_s2;
|
||||
TextImage *_teximg;
|
||||
|
||||
TextSprite *_ts;
|
||||
|
||||
Button *b1;
|
||||
Button *b2;
|
||||
|
@ -84,7 +84,9 @@ TextImage::TextImage(TrueTypeFont *font) {
|
||||
_text_type = BLENDED;
|
||||
_image = new Image();
|
||||
}
|
||||
TextImage::TextImage(TrueTypeFont *font, const String &text) {
|
||||
TextImage::TextImage(TrueTypeFont *font, const String &text, const Color &fg, const Color &bg) {
|
||||
_bg = bg;
|
||||
_fg = fg;
|
||||
_font = font;
|
||||
_text_type = BLENDED;
|
||||
_text = text;
|
||||
|
@ -42,7 +42,7 @@ public:
|
||||
|
||||
TextImage();
|
||||
TextImage(TrueTypeFont *font);
|
||||
TextImage(TrueTypeFont *font, const String &text);
|
||||
TextImage(TrueTypeFont *font, const String &text, const Color &fg = Color(), const Color &bg = Color());
|
||||
virtual ~TextImage();
|
||||
|
||||
private:
|
||||
|
86
03_sdl_basics/16_sdl_ttf/text_sprite.cpp
Normal file
86
03_sdl_basics/16_sdl_ttf/text_sprite.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "text_sprite.h"
|
||||
|
||||
TextImage::TextType TextSprite::get_text_type() {
|
||||
return _text_image->get_text_type();
|
||||
}
|
||||
void TextSprite::set_text_type(const TextImage::TextType type) {
|
||||
_text_image->set_text_type(type);
|
||||
}
|
||||
|
||||
TrueTypeFont *TextSprite::get_font() {
|
||||
return _text_image->get_font();
|
||||
}
|
||||
void TextSprite::set_font(TrueTypeFont *font) {
|
||||
_text_image->set_font(font);
|
||||
}
|
||||
|
||||
String TextSprite::get_text() {
|
||||
return _text_image->get_text();
|
||||
}
|
||||
void TextSprite::set_text(const String &text) {
|
||||
_text_image->set_text(text);
|
||||
|
||||
render_image();
|
||||
}
|
||||
|
||||
Color TextSprite::get_fg_color() {
|
||||
return _text_image->get_fg_color();
|
||||
}
|
||||
void TextSprite::get_fg_color(const Color &color) {
|
||||
_text_image->get_fg_color(color);
|
||||
}
|
||||
|
||||
Color TextSprite::get_bg_color() {
|
||||
return _text_image->get_bg_color();
|
||||
}
|
||||
void TextSprite::get_bg_color(const Color &color) {
|
||||
_text_image->get_bg_color(color);
|
||||
}
|
||||
|
||||
Image *TextSprite::get_image() {
|
||||
return _text_image->get_image();
|
||||
}
|
||||
|
||||
int TextSprite::get_width() {
|
||||
return _text_image->get_width();
|
||||
}
|
||||
int TextSprite::get_height() {
|
||||
return _text_image->get_height();
|
||||
}
|
||||
|
||||
void TextSprite::render_image() {
|
||||
_text_image->render_image();
|
||||
|
||||
Image *img = _text_image->get_image();
|
||||
|
||||
_tex->set_image(img);
|
||||
|
||||
set_w(img->get_width());
|
||||
set_h(img->get_height());
|
||||
|
||||
set_texture_clip_rect(Rect2(0, 0, img->get_width(), img->get_height()));
|
||||
}
|
||||
|
||||
TextSprite::TextSprite() {
|
||||
_text_image = new TextImage();
|
||||
_tex = new Texture();
|
||||
|
||||
set_texture(_tex);
|
||||
}
|
||||
TextSprite::TextSprite(TrueTypeFont *font) {
|
||||
_text_image = new TextImage(font);
|
||||
_tex = new Texture();
|
||||
|
||||
set_texture(_tex);
|
||||
}
|
||||
TextSprite::TextSprite(TrueTypeFont *font, const String &text, const Color &fg, const Color &bg) {
|
||||
_text_image = new TextImage(font, text, fg, bg);
|
||||
_tex = new Texture();
|
||||
|
||||
set_texture(_tex);
|
||||
|
||||
render_image();
|
||||
}
|
||||
TextSprite::~TextSprite() {
|
||||
delete _text_image;
|
||||
}
|
49
03_sdl_basics/16_sdl_ttf/text_sprite.h
Normal file
49
03_sdl_basics/16_sdl_ttf/text_sprite.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef TEXT_SPRITE_H
|
||||
#define TEXT_SPRITE_H
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#include "sprite.h"
|
||||
|
||||
#include "color.h"
|
||||
#include "image.h"
|
||||
#include "string.h"
|
||||
#include "true_type_font.h"
|
||||
#include "vector2.h"
|
||||
#include "text_image.h"
|
||||
|
||||
class TextSprite : public Sprite {
|
||||
public:
|
||||
TextImage::TextType get_text_type();
|
||||
void set_text_type(const TextImage::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();
|
||||
|
||||
TextSprite();
|
||||
TextSprite(TrueTypeFont *font);
|
||||
TextSprite(TrueTypeFont *font, const String &text, const Color &fg = Color(), const Color &bg = Color());
|
||||
virtual ~TextSprite();
|
||||
|
||||
private:
|
||||
TextImage *_text_image;
|
||||
Texture *_tex;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user