File: Label.cpp

package info (click to toggle)
viz1090 0.1.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 492 kB
  • sloc: cpp: 3,260; ansic: 2,293; python: 86; makefile: 22; sh: 17
file content (70 lines) | stat: -rw-r--r-- 1,272 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "Label.h"
#include <string>

void Label::draw(SDL_Renderer *renderer) {
    SDL_Rect rect = getRect();

    if(rect.h == 0 || rect.w == 0) {
        return;
    }

    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_RenderCopy(renderer, texture, NULL, &rect);
    SDL_DestroyTexture(texture);
}

void Label::makeSurface() {
    if(surface != NULL) {
        SDL_FreeSurface(surface);
    }
    
    surface = TTF_RenderUTF8_Solid(font, text.c_str(), color);   
}

SDL_Rect Label::getRect() {
    SDL_Rect rect = {0,0,0,0};

    if(!text.length()) { 
        return rect;
    }

    if(surface == NULL) {
        return rect;
    }

    rect.x = x;
    rect.y = y;
    rect.w = surface->w;
    rect.h = surface->h;

    return rect;
}

void Label::setText(std::string text) {
    this->text = text;
    makeSurface();
}

void Label::setPosition(int x, int y) {
    this->x = x;
    this->y = y;
}

void Label::setFont(TTF_Font *font) {
    this->font = font;
}

void Label::setColor(SDL_Color color) {
    this->color = color;
}
// 
Label::Label() {
    this->color = {255,255,255,255};
    surface = NULL;
}

Label::~Label() {
    SDL_FreeSurface(surface);
}