如何创建一个精灵图像
问题描述:
我正在尝试创建一个非常基本的精灵图像.
I am trying to create a very basic sprite image.
首先我有一个现有的图像(宽度= 100像素,高度= 100像素).
First off i have an existing image (Width=100px, Height=100px).
我将在这张图片中循环10到100次,每次将它放在前一张旁边的精灵上.
I will be looping through this image between 10 and 100 times, each time placing it on the sprite next to the previous one.
该图片的宽度限制为3000px.
The sprite is limited to 3000px wide.
将图像彼此相邻放置是很好的,因为我可以用一种简单的方法将它们组合在一起,但是,我需要将组合图像的宽度限制为3000px,然后从新的一行开始.
Placing the images next to each other is fine, cause i can just combine them with a simple method, however, i need to limit the width of the combined images to 3000px, then start on a new line.
答
让我尝试一些伪代码:
Bitmap originalImage; // that is your image of 100x100 pixels
Bitmap bigImage; // this is your 3000x3000 canvas
int xPut = 0;
int yPut = 0;
int maxHeight = 0;
while (someExitCondition)
{
Bitmap imagePiece = GetImagePieceAccordingToSomeParameters(originalImage);
if (xPut + imagePiece.Width > 3000)
{
xPut = 0;
yPut += maxHeight;
maxHeight = 0;
}
DrawPieceToCanvas(bigImage, xPut, yPut, imagePiece);
xPut += imagePiece.Width;
if (imagePiece.Height > maxHeight) maxHeight = imagePiece.Height;
// iterate until done
}