Bitmap: Add #v_flip / #h_flip methods

This commit is contained in:
Amaryllis Kulla 2022-02-01 21:47:07 +01:00
parent 1c2dc115b8
commit a651639524
4 changed files with 76 additions and 0 deletions

View File

@ -445,6 +445,28 @@ RB_METHOD(bitmapWriteToPng)
return self; return self;
} }
RB_METHOD(bitmapVFlip)
{
RB_UNUSED_PARAM;
Bitmap *b = getPrivateData<Bitmap>(self);
b->vFlip();
return Qnil;
}
RB_METHOD(bitmapHFlip)
{
RB_UNUSED_PARAM;
Bitmap *b = getPrivateData<Bitmap>(self);
b->hFlip();
return Qnil;
}
void void
bitmapBindingInit() bitmapBindingInit()
@ -479,6 +501,8 @@ bitmapBindingInit()
} }
_rb_define_method(klass, "write_to_png", bitmapWriteToPng); _rb_define_method(klass, "write_to_png", bitmapWriteToPng);
_rb_define_method(klass, "v_flip", bitmapVFlip);
_rb_define_method(klass, "h_flip", bitmapHFlip);
INIT_PROP_BIND(Bitmap, Font, "font"); INIT_PROP_BIND(Bitmap, Font, "font");
} }

View File

@ -14,6 +14,11 @@ returns: self
Writes the contents of the bitmap to `filename`, in PNG format. Writes the contents of the bitmap to `filename`, in PNG format.
## Bitmap.v_flip() / .h_flip()
returns: nil
Flips the bitmap image vertically / horizontally.
## Input.scroll_v() ## Input.scroll_v()
returns: Integer returns: Integer

View File

@ -259,6 +259,34 @@ struct BitmapPrivate
glState.viewport.pop(); glState.viewport.pop();
} }
void flip(const IntRect &srcRect)
{
TEXFBO newTex = shState->texPool().request(gl.width, gl.height);
SimpleShader &shader = shState->shaders().simple;
shader.bind();
shader.setTexOffsetX(0);
bindTexture(shader);
Quad &quad = shState->gpQuad();
quad.setTexPosRect(srcRect, IntRect(0, 0, gl.width, gl.height));
quad.setColor(Vec4(1, 1, 1, 1));
glState.blend.pushSet(false);
pushSetViewport(shader);
FBO::bind(newTex.fbo);
blitQuad(quad);
popViewport();
glState.blend.pop();
shState->texPool().release(gl);
gl = newTex;
onModified();
}
}; };
struct BitmapOpenHandler : FileSystem::OpenHandler struct BitmapOpenHandler : FileSystem::OpenHandler
@ -1410,6 +1438,22 @@ void Bitmap::writeToPng(const char *filename)
IMG_SavePNG(p->surface, filename); IMG_SavePNG(p->surface, filename);
} }
void Bitmap::vFlip()
{
guardDisposed();
GUARD_MEGA;
p->flip(IntRect(0, p->gl.height, p->gl.width, -p->gl.height));
}
void Bitmap::hFlip()
{
guardDisposed();
GUARD_MEGA;
p->flip(IntRect(p->gl.width, 0, -p->gl.width, p->gl.height));
}
TEXFBO &Bitmap::getGLTypes() TEXFBO &Bitmap::getGLTypes()
{ {
return p->gl; return p->gl;

View File

@ -108,6 +108,9 @@ public:
/* extensions */ /* extensions */
void writeToPng(const char *filename); void writeToPng(const char *filename);
void vFlip();
void hFlip();
/* <internal> */ /* <internal> */
TEXFBO &getGLTypes(); TEXFBO &getGLTypes();
SDL_Surface *megaSurface() const; SDL_Surface *megaSurface() const;