Skip to content

Instantly share code, notes, and snippets.

@rkkautsar
Last active August 29, 2015 14:23
Show Gist options
  • Save rkkautsar/6aa9a311e9cbd398be42 to your computer and use it in GitHub Desktop.
Save rkkautsar/6aa9a311e9cbd398be42 to your computer and use it in GitHub Desktop.
Animate colorful, random paint drip art with SFML 1.6
#include <SFML/Graphics.hpp>
#include <vector>
#include <queue>
#include <cmath>
#include <iostream>
#include <ctime>
using namespace std;
const int width = 1366,
height = 768,
base = 250,
update = 1,
skip =8000;
// 4-direction update
const int dx[4] = {-1,0,0,1},
dy[4] = {0,-1,1,0};
sf::Color color(100,128,255,255);
struct coor{
coor(){
x=0,y=0,v=0;
}
coor(int X,int Y,int V){
x=X;
y=Y;
v=V;
}
int x,y,v;
};
int main(){
srand(time(NULL));
// Initialization
sf::RenderWindow window(sf::VideoMode(width,height,32), "Test");
sf::Image img;
img.create(width,height,sf::Color::White);
vector< vector<bool> > pix;
pix.assign(width,vector<bool>(height,false));
queue<coor> q;
sf::Texture texture;
texture.loadFromImage(img);
sf::Sprite sprite;
sprite.setTexture(texture);
img.setPixel(width/2,height/2,color);
pix[width/2][height/2] = true;
q.push(coor(width/2,height/2,base));
coor now;
int nx,ny,nv;
sf::Color newColor;
sf::Clock clock;
sf::Time elapsed = clock.getElapsedTime();
// Main loop
while(window.isOpen()){
// Window close routine
sf::Event event;
while(window.pollEvent(event)){
if ((event.type == sf::Event::Closed) ||
(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) )
window.close();
}
elapsed = clock.getElapsedTime();
if(elapsed.asMilliseconds() >= update){
clock.restart();
for(int u=0;u<skip;++u){
if(q.empty()){
color.r=rand()%255;
color.g=rand()%255;
color.b=rand()%255;
nx=rand()%width,ny=rand()%height;
//cerr<<nx<<' '<<ny<<'\n';
pix.assign(width,vector<bool>(height,false));
pix[nx][ny]=true;
//cerr<<"here\n";
newColor=img.getPixel(nx,ny);
newColor.r=(newColor.r+color.r)/2;
newColor.g=(newColor.g+color.g)/2;
newColor.b=(newColor.b+color.b)/2;
img.setPixel(nx,ny,newColor);
q.push(coor(nx,ny,base));
}
now = q.front();
q.pop();
for(int i=0;i<4;++i){
nx=now.x+dx[i];
ny=now.y+dy[i];
if(nx<0 || nx>=width || ny<0 || ny>=height || pix[nx][ny]) continue;
if(rand()%base > now.v) continue;
pix[nx][ny]=true;
newColor=img.getPixel(nx,ny);
newColor.r=(newColor.r+color.r)/2;
newColor.g=(newColor.g+color.g)/2;
newColor.b=(newColor.b+color.b)/2;
img.setPixel(nx,ny,newColor);
nv=now.v;
nv+=rand()%7-4;
nv=min(base,nv);
q.push(coor(nx,ny,max(1,nv)));
//cout<<now.v-1<<' '<<q.size()<<'\n';
}
}
}
texture.update(img);
texture.setSmooth(true);
window.clear(sf::Color::White);
window.draw(sprite);
window.display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment