This repository was archived by the owner on Mar 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphics.cpp
More file actions
130 lines (97 loc) · 3.31 KB
/
Copy pathgraphics.cpp
File metadata and controls
130 lines (97 loc) · 3.31 KB
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
#include "graphics.h"
BitmapData::BitmapData(const std::initializer_list<GLubyte> &i_list) {
std::copy(i_list.begin(), i_list.end(), begin());
}
const Texture *Texture::get(const PaletteData &palette, const BitmapData &bitmap) {
auto ids = std::make_pair(palette.id(), bitmap.id());
auto result = texture_cache.find(ids);
if (result == texture_cache.end()) {
texture_cache[ids] = new Texture(palette, bitmap);
}
return texture_cache.at(ids);
}
const Texture *Texture::of(const Palettes::Definition &palette, const Bitmaps::Definition &bitmap) {
return get(*palette.data, *bitmap.data);
}
const Texture *Texture::of(const PaletteData *palette, const Bitmaps::Definition &bitmap) {
return get(*palette, *bitmap.data);
}
BitmapData::BitmapData(const GLubyte *data) {
std::copy(data, data + size(), begin());
}
void Texture::apply() const {
glBindTexture(GL_TEXTURE_2D, m_gl_tex_id);
}
const PaletteData &Texture::palette() const {
return m_palette;
}
std::map<std::pair<Id, Id>, Texture *> Texture::texture_cache{};
Texture::Texture(const PaletteData &palette, const BitmapData &bitmap)
: m_palette(palette), m_bitmap(bitmap)
{
glGenTextures(1, &m_gl_tex_id);
glBindTexture(GL_TEXTURE_2D, m_gl_tex_id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// The documents make me a promise in writing:
// "I'll map colors for you, waste not implementing!"
// But I sit here defeated, my soul slowly dying,
// As MSDN was just fucking lying.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, BITMAP_WH, BITMAP_WH, 0, GL_RGBA, GL_UNSIGNED_BYTE, data());
}
GLubyte *Texture::data() const {
GLubyte *texture_data = new GLubyte[m_bitmap.size() * 4];
// The bitmap and palette are fused into one!
// One turns to four and the process is done.
for (int i = 0; i < m_bitmap.size(); i++) {
Color palette_color = m_palette[m_bitmap.data()[i]];
#pragma warning ( suppress : 6386 ) // kindly shut the fuck up
texture_data[i * 4 + RED] = std::get<RED>(palette_color);
texture_data[i * 4 + GREEN] = std::get<GREEN>(palette_color);
texture_data[i * 4 + BLUE] = std::get<BLUE>(palette_color);
texture_data[i * 4 + ALPHA] = std::get<ALPHA>(palette_color);
}
return texture_data;
}
Point operator+(const Point &l, const Point &r) {
double x = get<X>(l) + get<X>(r);
double y = get<Y>(l) + get<Y>(r);
return Point(x, y);
}
Point &operator+=(Point &l, const Point &r) {
get<X>(l) += get<X>(r);
get<Y>(l) += get<Y>(r);
return l;
}
Point operator-(const Point &l, const Point &r) {
double x = get<X>(l) - get<X>(r);
double y = get<Y>(l) - get<Y>(r);
return Point(x, y);
}
Point &operator-=(Point &l, const Point &r) {
get<X>(l) -= get<X>(r);
get<Y>(l) -= get<Y>(r);
return l;
}
Point operator*(const Point &l, const GLdouble &r) {
double x = get<X>(l) * r;
double y = get<Y>(l) * r;
return Point(x, y);
}
Point &operator*=(Point &l, const GLdouble &r) {
get<X>(l) *= r;
get<Y>(l) *= r;
return l;
}
Point operator/(const Point &l, const GLdouble &r) {
double x = get<X>(l) / r;
double y = get<Y>(l) / r;
return Point(x, y);
}
Point &operator/=(Point &l, const GLdouble &r) {
get<X>(l) /= r;
get<Y>(l) /= r;
return l;
}