31 #include "util/structures/rect.h"
32 #include "video/sdl/sdlimage.h"
33 #include "video/renderbackend.h"
38 GLImage::GLImage(SDL_Surface* surface):
40 m_sdlimage =
new SDLImage(surface);
47 GLImage::GLImage(
const uint8_t* data,
unsigned int width,
unsigned int height):
48 Image(data, width, height) {
50 m_sdlimage =
new SDLImage(m_surface);
69 void GLImage::resetGlimage() {
75 m_colorkey = RenderBackend::instance()->getColorKey();
78 void GLImage::cleanup() {
80 glDeleteTextures(1, &m_textureids[0]);
82 delete[] m_textureids;
96 if (rect.
right() < 0 || rect.
x >
static_cast<int>(screen->w) || rect.
bottom() < 0 || rect.
y >
static_cast<int>(screen->h)) {
106 float scale_x =
static_cast<float>(rect.
w) / static_cast<float>(m_surface->w);
107 float scale_y =
static_cast<float>(rect.
h) / static_cast<float>(m_surface->h);
110 uint16_t w =
static_cast<int>(round(scale_x*m_surface->w));
111 uint16_t h =
static_cast<int>(round(scale_y*m_surface->h));
114 glColor4ub( 255, 255, 255, alpha );
116 glEnable(GL_TEXTURE_2D);
117 glBindTexture(GL_TEXTURE_2D, m_textureids[0]);
120 glTexCoord2f(0.0f, 0.0f);
121 glVertex2i(rect.
x, rect.
y);
123 glTexCoord2f(0.0f, m_row_tex_coord);
124 glVertex2i(rect.
x, rect.
y + h);
126 glTexCoord2f(m_col_tex_coord, m_row_tex_coord);
127 glVertex2i(rect.
x + w, rect.
y + h);
129 glTexCoord2f(m_col_tex_coord, 0.0f);
130 glVertex2i(rect.
x + w, rect.
y);
132 glDisable(GL_TEXTURE_2D);
136 void GLImage::generateGLTexture() {
137 const unsigned int width = m_surface->w;
138 const unsigned int height = m_surface->h;
145 m_col_tex_coord =
static_cast<float>(m_surface->w%m_chunk_size_w) / static_cast<float>(m_chunk_size_w);
146 m_row_tex_coord =
static_cast<float>(m_surface->h%m_chunk_size_h) / static_cast<float>(m_chunk_size_h);
148 if (m_col_tex_coord == 0.0f){
149 m_col_tex_coord = 1.0f;
152 if (m_row_tex_coord == 0.0f){
153 m_row_tex_coord = 1.0f;
156 uint8_t* data =
static_cast<uint8_t*
>(m_surface->pixels);
157 int pitch = m_surface->pitch;
160 assert(!m_textureids);
162 m_textureids =
new GLuint[1];
163 memset(m_textureids, 0x00, 1*
sizeof(GLuint));
166 uint32_t* oglbuffer =
new uint32_t[m_chunk_size_w * m_chunk_size_h];
167 memset(oglbuffer, 0x00, m_chunk_size_w*m_chunk_size_h*
sizeof(uint32_t));
169 for (
unsigned int y = 0; y < height; ++y) {
170 for (
unsigned int x = 0; x < width; ++x) {
171 unsigned int pos = (y * pitch) + (x * 4);
173 uint8_t r = data[pos + 3];
174 uint8_t g = data[pos + 2];
175 uint8_t b = data[pos + 1];
176 uint8_t a = data[pos + 0];
178 if (RenderBackend::instance()->isColorKeyEnabled()) {
180 if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) {
185 oglbuffer[(y*m_chunk_size_w) + x] = r | (g << 8) | (b << 16) | (a<<24);
190 glGenTextures(1, &m_textureids[0]);
192 glBindTexture(GL_TEXTURE_2D, m_textureids[0]);
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_chunk_size_w, m_chunk_size_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(oglbuffer));
202 void GLImage::saveImage(
const std::string& filename) {
203 const unsigned int swidth = getWidth();
204 const unsigned int sheight = getHeight();
205 SDL_Surface *surface = NULL;
208 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, swidth,
210 RMASK,GMASK,BMASK, NULLMASK);
212 if(surface == NULL) {
216 SDL_LockSurface(surface);
217 pixels =
new uint8_t[swidth * sheight * 3];
218 glReadPixels(0, 0, swidth, sheight, GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<GLvoid*>(pixels));
220 uint8_t *imagepixels =
reinterpret_cast<uint8_t*
>(surface->pixels);
222 for (
int y = (sheight - 1); y >= 0; --y) {
223 uint8_t *rowbegin = pixels + y * swidth * 3;
224 uint8_t *rowend = rowbegin + swidth * 3;
226 std::copy(rowbegin, rowend, imagepixels);
229 imagepixels += surface->pitch;
232 SDL_UnlockSurface(surface);
233 saveAsPng(filename, *surface);
234 SDL_FreeSurface(surface);
239 glScissor(cliparea.
x, getHeight() - cliparea.
y - cliparea.
h, cliparea.
w, cliparea.
h);
242 glClear(GL_COLOR_BUFFER_BIT);
246 bool GLImage::putPixel(
int x,
int y,
int r,
int g,
int b,
int a) {
248 return m_sdlimage->putPixel(x, y, r, g, b, a);
251 void GLImage::drawLine(
const Point& p1,
const Point& p2,
int r,
int g,
int b,
int a) {
253 m_sdlimage->drawLine(p1, p2, r, g, b, a);
256 void GLImage::drawTriangle(
const Point& p1,
const Point& p2,
const Point& p3,
int r,
int g,
int b,
int a) {
258 m_sdlimage->drawTriangle(p1, p2, p3, r, g, b, a);
261 void GLImage::drawRectangle(
const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
263 m_sdlimage->drawRectangle(p, w, h, r, g, b, a);
266 void GLImage::fillRectangle(
const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
268 m_sdlimage->fillRectangle(p, w, h, r, g, b, a);
271 void GLImage::drawQuad(
const Point& p1,
const Point& p2,
const Point& p3,
const Point& p4,
int r,
int g,
int b,
int a) {
273 m_sdlimage->drawQuad(p1, p2, p3, p4, r, g, b, a);
276 void GLImage::drawVertex(
const Point& p,
const uint8_t size,
int r,
int g,
int b,
int a) {
278 m_sdlimage->drawVertex(p, size, r, g, b, a);
281 void GLImage::drawLightPrimitive(
const Point& p, uint8_t intensity,
float radius,
int subdivisions,
float xstretch,
float ystretch, uint8_t red, uint8_t green, uint8_t blue) {
283 m_sdlimage->drawLightPrimitive(p, intensity, radius, subdivisions, xstretch, ystretch, red, green, blue);