aboutsummaryrefslogtreecommitdiff
path: root/src/strings33.c
blob: 8613c023de71b2728f8908c576026b92150836bf (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "stdarg.h"
#include "stdio.h"
#include "string.h"

#include "strings33.h"
#include "types.h"


extern Font33 defFont;
extern Font33 bigFont;

static Font33 *fonts[2] = {
  &defFont, &bigFont
};


static u32 textColours[9] = {
  0xff000000,
  0xffffffff,
  0xffff0000,
  0xff00ff00,
  0xff0000ff,
  0xffffff00,
  0xff00ffff,
  0xffff00ff,
  0xffffaa00,
};


void e33_uprintf( Surface *surf, Size f, u32 col, Size x, Size y, const char *fmt, ... )
{
  static char tmp[512];

  va_list args;
  va_start(args, fmt);

  vsprintf(tmp, fmt, args);
  e33_uprint( surf, f, col, x, y, tmp );

  va_end(args);
}


void e33_uprint( Surface *surf, Size f, u32 col, Size x, Size y, const char *str )
{
  Size i, j, ofs;
  Font33 *font = fonts[f];
  Size sWidth = surf->w;
  Size sHeight = surf->h;
  FontGlyph33 *glyphs = font->glyphs;
  Size fbw = surf->w;
  Size fbh = surf->h;
  u32 *out;
  uSize slen = strlen(str);

  Size strWid = 0;
  for( i = 0; i < slen; ++i ) {
    if( str[i] < 0x20 ) continue;
    strWid += glyphs[ str[i] - 0x20 ].width;
  }

  if( (x + strWid) >= sWidth ) {
    x -= (strWid - (sWidth - x));
  }
  if( (y + font->height) >= sHeight ) {
    y -= (font->height - (sHeight - y));
  }

  if(x < 0) { x = 0; }
  if(y < 0) { y = 0; }
  out = surf->data + (x + (y * surf->w));

  for( i = 0, ofs = 0; i < slen; ++i )
  {
    if( str[i] < 0x20 )
    {
      col = textColours[str[i]];
      continue;
    }

    FontGlyph33 *glyph = &glyphs[ str[i] - 0x20 ];
    Size numPlots = glyph->num_plots;
    Size gWidth = glyph->width;
    u16 *plots = glyph->plots;

    for( j = 0; j < numPlots; ++j )
    {
      Size plot = plots[j];
      Size offset = (((plot & font->yAnd) * fbw) + (plot >> font->xShift)) + ofs;

      memcpy( out + offset, &col, sizeof(u32) );
    }

    ofs += gWidth;
  }
}