Skip to content

Instantly share code, notes, and snippets.

@Sorebit
Last active November 27, 2015 13:03
Show Gist options
  • Save Sorebit/afb55a20c8bc2abf6718 to your computer and use it in GitHub Desktop.
Save Sorebit/afb55a20c8bc2abf6718 to your computer and use it in GitHub Desktop.
// @Makefile
// all:
// mkdir -p sprites
// g++ -std=c++11 lodepng/lodepng.cpp sprites.cpp -Wall -pedantic -O3 -o squad
//
// The lib can be found at https://github.com/lvandeve/lodepng
// The output will be stored in ./sprites/ as *.png files
// sprites.cpp - sorbet, https://github.com/Sorebit
#include "lodepng/lodepng.h"
#include <iostream>
#include <string>
unsigned width = 116, height = 116;
std::vector<unsigned char> image;
struct Color { int r, g, b; };
Color ice[2] = { {140, 160, 240}, {240, 240, 240} };
void encode(std::string filename, std::vector<unsigned char> & image, unsigned width, unsigned height)
{
std::vector<unsigned char> png;
unsigned error = lodepng::encode(png, image, width, height);
if(!error)
lodepng::save_file(png, filename.c_str());
// If there's an error display it
if(error)
std::cout << "encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
}
void face(Color fg)
{
// Create and clean (not sure if needed) a pixel map
bool pix[5][5];
for(int i = 0; i < 5; ++i)
for(int j = 0; j < 5; ++j)
pix[i][j] = 0;
// Set number of pixels to put
unsigned pc = rand() % 3 + 6, py, px;
std::vector <int> pv = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
// Put some pixels and mirror them
while(pc--)
{
std::vector<int>::iterator ppt = pv.begin() + ( rand() % pv.size() );
py = *(ppt) / 3;
px = *(ppt) % 3;
pix[py][px] = pix[py][4 - px] = true;
pv.erase(ppt);
}
// Copy them to the image vector
for(unsigned y = 0; y < height; ++y)
{
for(unsigned x = 0; x < width; ++x)
{
auto i = image.begin() + 4 * width * y + 4 * x;
if(y < 8 || y >= 108 || x < 8 || x >= 108)
{
// > set color to white
// > never display it
*(i + 0) = *(i + 1) = *(i + 2) = 240;
*(i + 3) = 0;
}
else
{
unsigned dy = y - 8;
unsigned dx = x - 8;
if(pix[dy / 20][dx / 20])
{
*(i + 0) = fg.r;
*(i + 1) = fg.g;
*(i + 2) = fg.b;
// the background is transparent
*(i + 3) = pix[dy / 20][dx / 20] ? 255 : 0;
}
}
}
}
}
int main(int argc, char **argv)
{
srand(time(NULL));
// Change the size of the pixel vector
image.resize(width * height * 4);
for(unsigned i = 1; i <= 9; ++i)
{
// Clear the image vector
for(auto it = image.begin(); it != image.end(); ++it)
*(it) = 0;
// Take two colors and create layers basing on them
Color base = {100 + rand() % 100, 100 + rand() % 100, 100 + rand() % 100};
Color detail = {100 + rand() % 100, 100 + rand() % 100, 100 + rand() % 100};
// If I make a game out of this I will probably want them to be in specific colors
//Color base = ice[1];
//Color detail = ice[0];
face(detail);
face(base);
// Save the image to filaname
encode("sprites/" + std::to_string(i) + ".png", image, width, height);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment