33 SDL_SetSurfaceBlendMode(surface.
get(), SDL_BLENDMODE_NONE);
34 SDL_BlitSurface(surface.
get(), &area, image2.get(), &area);
37 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
size[0],
size[1], 0,
38 GL_RGBA, GL_UNSIGNED_BYTE, image2->pixels);
47 return loadTexture(std::move(surface),
size);
48 }
catch (MSXException&
e) {
49 throw MSXException(
"Error loading image ",
filename,
": ",
80 borderR = borderG = borderB = borderA = 0;
82 bgR[i] = (rgba >> 24) & 0xff;
83 bgG[i] = (rgba >> 16) & 0xff;
84 bgB[i] = (rgba >> 8) & 0xff;
85 unsigned alpha = (rgba >> 0) & 0xff;
86 bgA[i] = (alpha == 255) ? 256 : alpha;
92 int borderSize_,
unsigned borderRGBA)
97 borderSize = borderSize_;
99 bgR[i] = (rgba[i] >> 24) & 0xff;
100 bgG[i] = (rgba[i] >> 16) & 0xff;
101 bgB[i] = (rgba[i] >> 8) & 0xff;
102 unsigned alpha = (rgba[i] >> 0) & 0xff;
103 bgA[i] = (alpha == 255) ? 256 : alpha;
106 borderR = (borderRGBA >> 24) & 0xff;
107 borderG = (borderRGBA >> 16) & 0xff;
108 borderB = (borderRGBA >> 8) & 0xff;
109 unsigned alpha = (borderRGBA >> 0) & 0xff;
110 borderA = (alpha == 255) ? 256 : alpha;
116 : texture(loadTexture(std::move(
image),
size))
120 void GLImage::initBuffers()
123 uint8_t indices[10] = {4, 0, 5, 1, 6, 2, 7, 3, 4, 0};
124 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementsBuffer.
get());
125 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(indices), indices, GL_STATIC_DRAW);
126 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
139 int bx = (
size[0] > 0) ? borderSize : -borderSize;
140 int by = (
size[1] > 0) ? borderSize : -borderSize;
141 ivec2 positions[8] = {
142 pos +
ivec2( + bx, + by),
153 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
155 glBindBuffer(GL_ARRAY_BUFFER, vbo[0].
get());
156 glBufferData(GL_ARRAY_BUFFER,
sizeof(positions), positions, GL_STREAM_DRAW);
168 r / 255.0f,
g / 255.0f, b / 255.0f, alpha / 255.0f);
169 glUniformMatrix4fv(
gl::context->unifTexMvp, 1, GL_FALSE,
171 const ivec2* offset =
nullptr;
172 glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0, offset + 4);
173 glEnableVertexAttribArray(0);
174 glBindBuffer(GL_ARRAY_BUFFER, vbo[1].
get());
175 glBufferData(GL_ARRAY_BUFFER,
sizeof(tex), tex, GL_STREAM_DRAW);
176 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0,
nullptr);
177 glEnableVertexAttribArray(1);
179 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
180 glDisableVertexAttribArray(1);
181 glDisableVertexAttribArray(0);
187 glUniformMatrix4fv(
gl::context->unifFillMvp, 1, GL_FALSE,
189 glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 0,
nullptr);
190 glEnableVertexAttribArray(0);
191 glVertexAttrib4f(1, borderR / 255.0f, borderG / 255.0f, borderB / 255.0f,
192 (borderA * alpha) / (255.0f * 255.0f));
194 if ((2 * borderSize >= abs(
size[0])) ||
195 (2 * borderSize >= abs(
size[1]))) {
197 glDrawArrays(GL_TRIANGLE_FAN, 4, 4);
200 if (borderSize > 0) {
201 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementsBuffer.
get());
202 glDrawElements(GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_BYTE,
nullptr);
203 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
207 uint8_t col[4][4] = {
208 { bgR[0], bgG[0], bgB[0], uint8_t((bgA[0] * alpha) / 256) },
209 { bgR[2], bgG[2], bgB[2], uint8_t((bgA[2] * alpha) / 256) },
210 { bgR[3], bgG[3], bgB[3], uint8_t((bgA[3] * alpha) / 256) },
211 { bgR[1], bgG[1], bgB[1], uint8_t((bgA[1] * alpha) / 256) },
213 glBindBuffer(GL_ARRAY_BUFFER, vbo[2].
get());
214 glBufferData(GL_ARRAY_BUFFER,
sizeof(col), col, GL_STREAM_DRAW);
215 glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0,
nullptr);
216 glEnableVertexAttribArray(1);
217 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
218 glDisableVertexAttribArray(1);
220 glDisableVertexAttribArray(0);
222 glDisableVertexAttribArray(0);
223 glBindBuffer(GL_ARRAY_BUFFER, 0);
Wrapper around a SDL_Surface.
Most basic/generic texture: only contains a texture ID.
GLuint get() const
Returns the underlying openGL handler id.
void bind()
Makes this texture the active GL texture.
static void checkSize(gl::ivec2 size)
Performs a sanity check on image size.
void draw(OutputSurface &output, gl::ivec2 pos, uint8_t r, uint8_t g, uint8_t b, uint8_t alpha) override
GLImage(OutputSurface &output, const std::string &filename)
A frame buffer where pixels can be written to.
constexpr vecN< N, int > trunc(const vecN< N, T > &x)
std::optional< Context > context
SDLSurfacePtr load(const std::string &filename, bool want32bpp)
Load the given PNG file in a SDL_Surface.
This file implemented 3 utility functions:
const T & get(const Event &event)
constexpr const char *const filename
size_t size(std::string_view utf8)
constexpr auto xrange(T e)