Basic
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
.vscode
|
||||||
|
output.mp4
|
||||||
|
conway.map
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
const int h=1080;
|
||||||
|
const int w=1920;
|
||||||
|
const int f=3600;
|
||||||
|
const int t=12;
|
||||||
|
|
||||||
|
void update(unsigned char* _p1,unsigned char* _p2,int* rng){
|
||||||
|
int a=0;
|
||||||
|
for(int i=-1;i<2;i++){
|
||||||
|
if(*(_p1-w+i)==255){
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
if(*(_p1+w+i)==255){
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
if(*(_p1+i)==255&&i!=0){
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if((*rng)/(a+1)==0){
|
||||||
|
*rng=((double)rand()/RAND_MAX)*100000;
|
||||||
|
if(*_p1==255){
|
||||||
|
*_p2=0;
|
||||||
|
}else{
|
||||||
|
*_p2=255;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
*rng-=1;
|
||||||
|
if(*_p1==0&&a==3){
|
||||||
|
*_p2=255;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(*_p1==255&&a<2){
|
||||||
|
*_p2=0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(*_p1==255&&a>3){
|
||||||
|
*_p2=0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(*_p1==255){
|
||||||
|
*_p2=255;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct update_struct{
|
||||||
|
int low;
|
||||||
|
int high;
|
||||||
|
int fra;
|
||||||
|
int* rng;
|
||||||
|
unsigned char* new;
|
||||||
|
unsigned char* mai;
|
||||||
|
unsigned char* frame;
|
||||||
|
};
|
||||||
|
|
||||||
|
void* update_thread(void* _i){
|
||||||
|
struct update_struct _p = *(struct update_struct*)_i;
|
||||||
|
for(int he=_p.low;he<_p.high;he++){
|
||||||
|
for(int wi=0;wi<w;wi++){
|
||||||
|
if(he!=0&&he!=h-1&&wi!=0&&wi!=w-1){
|
||||||
|
if(_p.fra%2==0){
|
||||||
|
update(_p.new+he*w+wi,_p.mai+he*w+wi,_p.rng);
|
||||||
|
}else{
|
||||||
|
update(_p.mai+he*w+wi,_p.new+he*w+wi,_p.rng);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(int he=_p.low;he<_p.high;he++){
|
||||||
|
for(int wi=0;wi<w;wi++){
|
||||||
|
if(_p.fra%2==0){
|
||||||
|
if(*(_p.mai+wi+he*w)==255){
|
||||||
|
*(_p.frame+(wi+he*w)*3)=255;
|
||||||
|
*(_p.frame+(wi+he*w)*3+1)=255;
|
||||||
|
*(_p.frame+(wi+he*w)*3+2)=255;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if(*(_p.new+wi+he*w)==255){
|
||||||
|
*(_p.frame+(wi+he*w)*3)=255;
|
||||||
|
*(_p.frame+(wi+he*w)*3+1)=255;
|
||||||
|
*(_p.frame+(wi+he*w)*3+2)=255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
unsigned char* mai=malloc(w*h);
|
||||||
|
unsigned char* new=malloc(w*h);
|
||||||
|
memset(mai,0,w*h);
|
||||||
|
memset(new,0,w*h);
|
||||||
|
FILE* conway = fopen("conway.map","r");
|
||||||
|
if(!conway){
|
||||||
|
for(int i=0;i<h;i++){
|
||||||
|
if(i==0||i==h-1){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for(int j=0;j<w;j++){
|
||||||
|
if(j==0||j==w-1){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(rand()<RAND_MAX/2){
|
||||||
|
*(mai+j+w*i)=255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
fread(mai,1,w*h,conway);
|
||||||
|
fclose(conway);
|
||||||
|
}
|
||||||
|
size_t _fsize = w*h*3;
|
||||||
|
unsigned char* frame = malloc(_fsize);
|
||||||
|
for(int he=0;he<h;he++){
|
||||||
|
for(int wi=0;wi<w;wi++){
|
||||||
|
if(*(mai+wi+he*w)==255){
|
||||||
|
*(frame+(wi+he*w)*3)=255;
|
||||||
|
*(frame+(wi+he*w)*3+1)=255;
|
||||||
|
*(frame+(wi+he*w)*3+2)=255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char cmd[512];
|
||||||
|
snprintf(cmd, sizeof(cmd),"ffmpeg -y -f rawvideo -pixel_format rgb24 -video_size %dx%d -framerate 60 -i pipe:0 -c:v libx264 -preset fast -crf 23 -pix_fmt yuv420p output.mp4",w, h);
|
||||||
|
FILE *ffmpeg = popen(cmd, "w");
|
||||||
|
size_t written = fwrite(frame,1,w*h*3,ffmpeg);
|
||||||
|
if (written != _fsize) {
|
||||||
|
fprintf(stderr, "Failed to write frame\n");
|
||||||
|
}
|
||||||
|
pthread_t pt[t];struct update_struct us[t];int* _rng=malloc(t*sizeof(int));
|
||||||
|
for(int i=0;i<t;i++){
|
||||||
|
us[i].rng=_rng+i;
|
||||||
|
*(us[i].rng)=((double)rand()/RAND_MAX)*50000;
|
||||||
|
us[i].low=((h-h%t)/t)*i;
|
||||||
|
us[i].high=(i!=t-1?((h-h%t)/t)*(i+1):h-1);
|
||||||
|
us[i].frame=frame;
|
||||||
|
us[i].mai=mai;
|
||||||
|
us[i].new=new;
|
||||||
|
}
|
||||||
|
for(int fra=1;fra<f;fra++){
|
||||||
|
for(int i=0;i<t;i++){
|
||||||
|
us[i].fra=fra;
|
||||||
|
pthread_create(pt+i,NULL,update_thread,us+i);
|
||||||
|
}
|
||||||
|
for(int i=0;i<t;i++){
|
||||||
|
pthread_join(pt[i], NULL);
|
||||||
|
}
|
||||||
|
if(fra%2==0){
|
||||||
|
memset(new,0,w*h);
|
||||||
|
}else{
|
||||||
|
memset(mai,0,w*h);
|
||||||
|
}
|
||||||
|
size_t written = fwrite(frame,1,w*h*3,ffmpeg);
|
||||||
|
if (written != _fsize) {
|
||||||
|
fprintf(stderr, "Failed to write frame\n");
|
||||||
|
}
|
||||||
|
memset(frame,0,_fsize);
|
||||||
|
}
|
||||||
|
conway = fopen("conway.map","w");
|
||||||
|
if(f%2==0){
|
||||||
|
fwrite(new,1,w*h,conway);
|
||||||
|
}else{
|
||||||
|
fwrite(mai,1,w*h,conway);
|
||||||
|
}
|
||||||
|
fclose(conway);
|
||||||
|
free(frame);free(mai);free(new);free(_rng);
|
||||||
|
pclose(ffmpeg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user