aboutsummaryrefslogtreecommitdiff
path: root/render/render_text.c
blob: 75b2c916bc939c9a7bd09a1c6e5b508a375f8c02 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include "render_text.h"

void char_width_init (int *char_width) {
    for (int i=0; i<128; char_width[i++]=5);
    char_width['('] = 2; char_width[')'] = 2;
    char_width['['] = 2; char_width[']'] = 2;
    char_width['{'] = 3; char_width['}'] = 3;
    char_width[','] = 1; char_width['-'] = 3; char_width['.'] = 1; 
    char_width['/'] = 4; char_width['!'] = 1;
    char_width[':'] = 1; char_width[';'] = 1;
    char_width['<'] = 3; char_width['>'] = 3; char_width['='] = 4;
    char_width['I'] = 1;
    char_width['f'] = 4; char_width['i'] = 1; char_width['j'] = 3;
    char_width['l'] = 1; char_width['k'] = 4; char_width['t'] = 4;
}

int get_text_width (char str[], txtd *t) {
    int width = 0;
    for (int i=0; str[i]!='\0'; i++) { 
        width += t->cw[str[i]]; 
        if (str[i+1]!='\0') width++;
    }
    return width;
}

void render_text (SDL_Renderer* gRenderer, char str[], float off[], txtd *t) 
{
    int width = 0;
    for (int i=0; str[i]!='\0'; i++) {
        int char_i = str[i];
        SDL_Rect srcRect = { (char_i%32)*6+1, (char_i/32)*12+1, 5, 11 };
        SDL_Rect dstRect = { off[0]+width, off[1], 5, 11 };
        SDL_RenderCopy(gRenderer, t->tex, &srcRect, &dstRect);
        width += t->cw[char_i]+1;
    }
}

void render_text_scaled (SDL_Renderer* rend, char str[], 
    float off[], txtd *t, float scale) 
{
    int width = 0;
    for (int i=0; str[i]!='\0'; i++) {
        int char_i = str[i];
        SDL_Rect srcRect = { (char_i%32)*6+1, (char_i/32)*12+1, 5, 11 };
        SDL_Rect dstRect = { off[0]+width, off[1], 5*scale, 11*scale };
        SDL_RenderCopy(rend, t->tex, &srcRect, &dstRect);
        width += t->cw[char_i]*scale+1*scale;
    }
}

void render_text_small (SDL_Renderer* rend, char str[], float off[], txtd *t)
{
    int width = 0;
    for (int i=0; str[i]!='\0'; i++) {
        int char_i = str[i];
        SDL_Rect srcRect = { (char_i%32)*5+1, (char_i/32)*7+1, 4, 6 };
        SDL_Rect dstRect = { off[0]+width, off[1], 4, 6 };
        SDL_RenderCopy(rend, t->tex_small, &srcRect, &dstRect);
        width += 5;
    }
}