mirror of
https://github.com/love2d/love.git
synced 2026-08-20 04:30:09 +02:00
Issue #5: Add Framebuffer:getImageData()
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
#include "Framebuffer.h"
|
#include "Framebuffer.h"
|
||||||
#include <common/Matrix.h>
|
#include <common/Matrix.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -275,6 +277,37 @@ namespace opengl
|
|||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
love::image::ImageData * Framebuffer::getImageData(love::image::Image * image)
|
||||||
|
{
|
||||||
|
int row = 4 * width;
|
||||||
|
int size = row * height;
|
||||||
|
|
||||||
|
// see Graphics::newScreenshot. OpenGL reads from lower-left,
|
||||||
|
// but we need the pixels from upper-left.
|
||||||
|
GLubyte* pixels = new GLubyte[size];
|
||||||
|
GLubyte* screenshot = new GLubyte[size];
|
||||||
|
|
||||||
|
strategy->bindFBO( fbo );
|
||||||
|
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
|
||||||
|
if (current)
|
||||||
|
strategy->bindFBO( current->fbo );
|
||||||
|
else
|
||||||
|
strategy->bindFBO( 0 );
|
||||||
|
|
||||||
|
GLubyte* src = pixels - row; // second line of buffer
|
||||||
|
GLubyte* dst = screenshot + size; // end of buffer
|
||||||
|
|
||||||
|
for (int i = 0; i < height; ++i)
|
||||||
|
memcpy(dst -= row, src += row, row);
|
||||||
|
|
||||||
|
love::image::ImageData * img = image->newImageData(width, height, (void*)screenshot);
|
||||||
|
|
||||||
|
delete[] screenshot;
|
||||||
|
delete[] pixels;
|
||||||
|
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
||||||
} // opengl
|
} // opengl
|
||||||
} // graphics
|
} // graphics
|
||||||
} // love
|
} // love
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
#define LOVE_GRAPHICS_FRAMEBUFFER_H
|
#define LOVE_GRAPHICS_FRAMEBUFFER_H
|
||||||
|
|
||||||
#include <graphics/Drawable.h>
|
#include <graphics/Drawable.h>
|
||||||
#include <graphics/Volatile.h>
|
#include <image/Image.h>
|
||||||
|
#include <image/ImageData.h>
|
||||||
#include <common/math.h>
|
#include <common/math.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "GLee.h"
|
#include "GLee.h"
|
||||||
@@ -29,7 +30,7 @@ namespace opengl
|
|||||||
void stopGrab();
|
void stopGrab();
|
||||||
|
|
||||||
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
|
virtual void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
|
||||||
|
love::image::ImageData * getImageData(love::image::Image * image);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLsizei width;
|
GLsizei width;
|
||||||
|
|||||||
@@ -32,8 +32,18 @@ namespace opengl
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int w_Framebuffer_getImageData(lua_State * L)
|
||||||
|
{
|
||||||
|
Framebuffer * fbo = luax_checkfbo(L, 1);
|
||||||
|
love::image::Image * image = luax_getmodule<love::image::Image>(L, "image", MODULE_IMAGE_T);
|
||||||
|
love::image::ImageData * img = fbo->getImageData( image );
|
||||||
|
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)img);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static const luaL_Reg functions[] = {
|
static const luaL_Reg functions[] = {
|
||||||
{ "renderTo", w_Framebuffer_renderTo },
|
{ "renderTo", w_Framebuffer_renderTo },
|
||||||
|
{ "getImageData", w_Framebuffer_getImageData },
|
||||||
{ 0, 0 }
|
{ 0, 0 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace opengl
|
|||||||
//see Framebuffer.h
|
//see Framebuffer.h
|
||||||
Framebuffer * luax_checkfbo(lua_State * L, int idx);
|
Framebuffer * luax_checkfbo(lua_State * L, int idx);
|
||||||
int w_Framebuffer_renderTo(lua_State * L);
|
int w_Framebuffer_renderTo(lua_State * L);
|
||||||
|
int w_Framebuffer_getImageData(lua_State * L);
|
||||||
int luaopen_framebuffer(lua_State * L);
|
int luaopen_framebuffer(lua_State * L);
|
||||||
|
|
||||||
} // opengl
|
} // opengl
|
||||||
|
|||||||
Reference in New Issue
Block a user