Bitmap: Add ::writeToPng()
This commit is contained in:
		
							parent
							
								
									a92adee7f8
								
							
						
					
					
						commit
						1856e677a3
					
				
					 3 changed files with 38 additions and 12 deletions
				
			
		| 
						 | 
					@ -433,6 +433,18 @@ RB_METHOD(bitmapInitializeCopy)
 | 
				
			||||||
	return self;
 | 
						return self;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					RB_METHOD(bitmapWriteToPng)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						Bitmap *b = getPrivateData<Bitmap>(self);
 | 
				
			||||||
 | 
						const char *filename;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rb_get_args(argc, argv, "z", &filename RB_ARG_END);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						b->writeToPng(filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return self;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
bitmapBindingInit()
 | 
					bitmapBindingInit()
 | 
				
			||||||
| 
						 | 
					@ -466,5 +478,7 @@ bitmapBindingInit()
 | 
				
			||||||
	_rb_define_method(klass, "radial_blur",        bitmapRadialBlur);
 | 
						_rb_define_method(klass, "radial_blur",        bitmapRadialBlur);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_rb_define_method(klass, "write_to_png", bitmapWriteToPng);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	INIT_PROP_BIND(Bitmap, Font, "font");
 | 
						INIT_PROP_BIND(Bitmap, Font, "font");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -245,6 +245,20 @@ struct BitmapPrivate
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		self->modified();
 | 
							self->modified();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void downloadToSurface()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if (!surface)
 | 
				
			||||||
 | 
								allocSurface();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							FBO::bind(gl.fbo);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							glState.viewport.pushSet(IntRect(0, 0, gl.width, gl.height));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							::gl.ReadPixels(0, 0, gl.width, gl.height, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							glState.viewport.pop();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct BitmapOpenHandler : FileSystem::OpenHandler
 | 
					struct BitmapOpenHandler : FileSystem::OpenHandler
 | 
				
			||||||
| 
						 | 
					@ -900,18 +914,7 @@ Color Bitmap::getPixel(int x, int y) const
 | 
				
			||||||
	if (x < 0 || y < 0 || x >= width() || y >= height())
 | 
						if (x < 0 || y < 0 || x >= width() || y >= height())
 | 
				
			||||||
		return Vec4();
 | 
							return Vec4();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!p->surface)
 | 
						p->downloadToSurface();
 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		p->allocSurface();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		FBO::bind(p->gl.fbo);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		glState.viewport.pushSet(IntRect(0, 0, width(), height()));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		gl.ReadPixels(0, 0, width(), height(), GL_RGBA, GL_UNSIGNED_BYTE, p->surface->pixels);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		glState.viewport.pop();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	uint32_t pixel = getPixelAt(p->surface, p->format, x, y);
 | 
						uint32_t pixel = getPixelAt(p->surface, p->format, x, y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1400,6 +1403,12 @@ void Bitmap::setInitFont(Font *value)
 | 
				
			||||||
	p->font = value;
 | 
						p->font = value;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Bitmap::writeToPng(const char *filename)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						p->downloadToSurface();
 | 
				
			||||||
 | 
						IMG_SavePNG(p->surface, filename);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEXFBO &Bitmap::getGLTypes()
 | 
					TEXFBO &Bitmap::getGLTypes()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return p->gl;
 | 
						return p->gl;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -105,6 +105,9 @@ public:
 | 
				
			||||||
	 * use at construction */
 | 
						 * use at construction */
 | 
				
			||||||
	void setInitFont(Font *value);
 | 
						void setInitFont(Font *value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* extensions */
 | 
				
			||||||
 | 
						void writeToPng(const char *filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* <internal> */
 | 
						/* <internal> */
 | 
				
			||||||
	TEXFBO &getGLTypes();
 | 
						TEXFBO &getGLTypes();
 | 
				
			||||||
	SDL_Surface *megaSurface() const;
 | 
						SDL_Surface *megaSurface() const;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue