-
Notifications
You must be signed in to change notification settings - Fork 3
/
layer.h
159 lines (126 loc) · 3.56 KB
/
layer.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2021 Justin Meiners
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CC_LAYER_H
#define CC_LAYER_H
#include <wctype.h>
#include "bitmap.h"
#include "transform.h"
#include "config.h"
#include "stb_truetype.h"
typedef enum
{
TEXT_ALIGN_CENTER,
TEXT_ALIGN_LEFT,
TEXT_ALIGN_RIGHT
} CcTextAlign;
typedef struct
{
CcBitmap* bitmaps;
CcColorBlend blend;
int x;
int y;
int font;
stbtt_fontinfo font_info;
int font_size;
CcTextAlign font_align;
uint32_t font_color;
size_t text_buffer_size;
wchar_t* text;
} CcLayer;
static inline
CcRect cc_layer_rect(const CcLayer* layer)
{
CcRect r = {
layer->x, layer->y, layer->bitmaps->w, layer->bitmaps->h
};
return r;
}
static inline
int cc_layer_w(const CcLayer* layer)
{
if (!layer->bitmaps) return 0;
return layer->bitmaps->w;
}
static inline
int cc_layer_h(const CcLayer* layer)
{
if (!layer->bitmaps) return 0;
return layer->bitmaps->h;
}
void cc_layer_init(CcLayer* layer, int x, int y);
void cc_layer_shutdown(CcLayer* layer);
void cc_layer_reset(CcLayer* layer);
void cc_layer_flip(CcLayer* layer, int horiz);
void cc_layer_set_bitmap(CcLayer* layer, CcBitmap* new_bitmap);
void cc_layer_rotate_90(CcLayer* layer, int repeat);
void cc_layer_rotate_angle(CcLayer* layer, double angle, uint32_t bg_color);
void cc_layer_stretch(CcLayer* layer, int w, int h, int w_angle, int h_angle, uint32_t bg_color);
void cc_layer_resize(CcLayer* layer, int new_w, int new_h, uint32_t bg_color);
void cc_layer_ensure_size(CcLayer* layer, int w, int h);
void cc_layer_set_text(CcLayer* layer, const wchar_t* text);
void cc_layer_render(CcLayer* layer);
unsigned char* cc_bitmap_compress(const CcBitmap* b, size_t* out_size);
CcBitmap* cc_bitmap_decompress(unsigned char* compressed_data, size_t compressed_size);
void test_text_wordwrap(void);
void cc_text_render(
CcBitmap* bitmap,
const wchar_t* text,
const stbtt_fontinfo* font_info,
int font_size,
CcTextAlign align,
uint32_t font_color
);
typedef struct
{
// origin point of viewport in
// paint coordinates.
int paint_x;
int paint_y;
// Width of visible pain area.
// May be larger than actual image.
int w;
int h;
int zoom;
} CcViewport;
static inline
void cc_viewport_init(CcViewport* v)
{
v->paint_x = 0;
v->paint_y = 0;
v->zoom = 1;
v->w = 0;
v->h = 0;
}
static inline
void cc_viewport_coord_to_paint(const CcViewport* v, int x, int y, int* out_x, int* out_y)
{
*out_x = (x / v->zoom) + v->paint_x;
*out_y = (y / v->zoom) + v->paint_y;
}
static inline
CcViewport cc_viewport_zoom_centered(const CcViewport* v, int new_zoom)
{
CcViewport out;
out.w = v->w;
out.h = v->h;
out.zoom = new_zoom;
int center_x = v->paint_x + v->w / (2 * v->zoom);
int center_y = v->paint_y + v->h / (2 * v->zoom);
out.paint_x = center_x - v->w / (2 * new_zoom);
out.paint_y = center_y - v->h / (2 * new_zoom);
return out;
}
#endif