Skip to content

Commit 8f4419d

Browse files
committed
Add image(img,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2) nine-argument form for sprite sheet cropping
1 parent 0e8a801 commit 8f4419d

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/Processing.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,27 @@ void PApplet::image(PImage* img, float x, float y, float w, float h) {
27152715
if(!img || img->width==0 || img->height==0) return;
27162716
drawImage_impl(img, x, y, w, h);
27172717
}
2718+
// Nine-argument form: image(img, dx1,dy1,dx2,dy2, sx1,sy1,sx2,sy2)
2719+
void PApplet::image(PImage* img, float dx1,float dy1,float dx2,float dy2,
2720+
float sx1,float sy1,float sx2,float sy2) {
2721+
if(!img || img->width==0 || img->height==0) return;
2722+
if(img->dirty) img->uploadTexture();
2723+
if(img->texID==0) return;
2724+
// Convert source pixel coords to UV [0,1]
2725+
float u1=sx1/img->width, v1=sy1/img->height;
2726+
float u2=sx2/img->width, v2=sy2/img->height;
2727+
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
2728+
glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,img->texID);
2729+
glColor4f(doTint?tintR:1.f,doTint?tintG:1.f,doTint?tintB:1.f,doTint?tintA:1.f);
2730+
glBegin(GL_QUADS);
2731+
glTexCoord2f(u1,v1); glVertex2f(dx1,dy1);
2732+
glTexCoord2f(u2,v1); glVertex2f(dx2,dy1);
2733+
glTexCoord2f(u2,v2); glVertex2f(dx2,dy2);
2734+
glTexCoord2f(u1,v2); glVertex2f(dx1,dy2);
2735+
glEnd();
2736+
glBindTexture(GL_TEXTURE_2D,0); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND);
2737+
glColor4f(1.f,1.f,1.f,1.f);
2738+
}
27182739

27192740
void PApplet::imageMode(int m){currentImageMode=m;}
27202741
void PApplet::tint(float gray) {tint(gray, colorMaxA);}

src/Processing.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ class PGraphics : public PImage {
973973
void curveVertex(float x, float y);
974974
void image(PImage* img, float x, float y);
975975
void image(PImage* img, float x, float y, float w, float h);
976+
void image(PImage* img, float dx1,float dy1,float dx2,float dy2,float sx1,float sy1,float sx2,float sy2);
976977
void tint(float gray);
977978
void tint(float gray, float a);
978979
void tint(float r, float g, float b, float a);

0 commit comments

Comments
 (0)