File: Text.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 (73 lines) | stat: -rw-r--r-- 1,737 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
71
72
73
#include "SDL2/SDL2_gfxPrimitives.h"

#include "Text.h"

SDL_Rect Text::drawString(std::string text, int x, int y, TTF_Font *font, SDL_Color color)
{
    SDL_Rect dest = {0,0,0,0};

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

    SDL_Surface *surface;

    surface = TTF_RenderUTF8_Solid(font, text.c_str(), color);

    if (surface == NULL)
    {
        printf("Couldn't create String %s: %s\n", text.c_str(), SDL_GetError());

        return dest;
    }
    
    dest.x = x;
    dest.y = y;
    dest.w = surface->w;
    dest.h = surface->h;

    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_RenderCopy(renderer, texture, NULL, &dest);
    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);

    return dest;
}

SDL_Rect Text::drawStringBG(std::string text, int x, int y, TTF_Font *font, SDL_Color color, SDL_Color bgColor) {
    SDL_Rect dest = {0,0,0,0};

    if(!text.length()) { 
        return dest;
    }
        
    SDL_Surface *surface;

    surface = TTF_RenderUTF8_Shaded(font, text.c_str(), color, bgColor);

    if (surface == NULL)
    {
        printf("Couldn't create String %s: %s\n", text.c_str(), SDL_GetError());

        return dest;
    }
    
    dest.x = x;
    dest.y = y;
    dest.w = surface->w;
    dest.h = surface->h;

    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_RenderCopy(renderer, texture, NULL, &dest);
    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);

    return dest;
}


// check if text has changed and surface exists
// redraw existing surface
// or make new surface
// for BG, draw bg size of surface
// draw surface