Skip to content

Commit

Permalink
Much better UI and now the URL can be copied
Browse files Browse the repository at this point in the history
UI is considerably better.
Now you can also save the patterns as a URL (some features are still missing).
Fixed a bug in the EncoderState.h

(the codebase for the JS version needs a urgent clean up)
  • Loading branch information
rafa-br34 committed Apr 4, 2024
1 parent e9cba9c commit 90b4a01
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 320 deletions.
19 changes: 10 additions & 9 deletions SOURCE/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int main(int ArgCount, const char* ArgValues[]) {

std::cout << "State machine: " << StateMachineToString(StateMachine, "") << '\n';

Vector2<int> CanvasSize(30720, 30720);//{ 30720, 17280 };
Vector2<int> CanvasSize(10, 10);//{ 30720, 17280 };

SimulationState<uint8_t, int> Simulation;
EncoderState Encoder;
Expand All @@ -149,28 +149,29 @@ int main(int ArgCount, const char* ArgValues[]) {
auto Center = CanvasSize / Vector2(2, 2);
//Simulation.AddAnt(Ant<uint8_t>(Center, Vector2<int8_t>(0, -1), {C,C,C,C,C,C,U,C,C,C,C,C,R45,R,R45,L135,U,C,U,L,L,R135,L45,R45,R,R135,L45,R,R,R45}, true));

Simulation.AddAnt(Ant<uint8_t>(Center, Vector2<int8_t>(0, -1), {R,L,C}, true));
Simulation.AddAnt(Ant<uint8_t>(Center, Vector2<int8_t>(0, -1), {R,L,R,R,L,R,R,L,R,R,L,R}, true));
Simulation.AddAnt(Ant<uint8_t>(Center, Vector2<int8_t>(0, -1), {R,L,C}, true));

// ffmpeg -r 60 -i "Frames/%d.png" -b:v 5M -c:v libx264 -preset veryslow -qp 0 output.mp4
// ffmpeg -r 60 -i "Frames/%d.png" -b:v 5M -c:v libx264 -preset veryslow -qp 0 -s 1920x1920 output.mp4
// ffmpeg -r 60 -i "Frames/%d.png" -b:v 5M -c:v libx264 -preset veryslow -qp 0 -s 1920x1920 -sws_flags neighbor output.mp4
// ffmpeg -r 30 -i "Frames/%d.png" -c:v libx264 -preset veryslow -qp 0 -s 7680x4320 output.mp4
// 1ull * 1000000000ull 1b

size_t Iterations = 6ull * 1000000000ull;
double FrameRate = 30.0; // Video frame rate
double Time = 120.0; // Video time
size_t Iterations = 1ull * 100ull;
double FrameRate = 1.0; // Video frame rate
double Time = 100.0; // Video time
size_t Frames = size_t(Time * FrameRate);

size_t CaptureDelta = size_t(double(Iterations) / double(Frames));

Simulation.Reset();
Encoder.Threads = 70;
Encoder.Threads = 2;
for (size_t i = 0; i < Frames; i++) {
std::cout << i << '\n';
Simulation.Simulate(CaptureDelta);
Encoder.EncodeAsync(Simulation, std::string("Frames/State_") + std::to_string(i) +".png");
std::cout << i << ' ' << Simulation.Simulate(CaptureDelta) << '/' << CaptureDelta << '\n';

//Encoder.EncodeSync(Simulation, std::string("Frames/") + std::to_string(i) +".png");
Encoder.EncodeAsync(Simulation, std::string("State_") + std::to_string(i) +".png");
}

Encoder.WaitJobs();
Expand Down
17 changes: 5 additions & 12 deletions SOURCE/Types/EncoderState.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,16 @@ class EncoderState {
m_PaletteCache.push_back(Coloring(s));
}

lodepng::State m_AcquireEncoderState(size_t StateCount) {
template<typename CellType>
lodepng::State m_SetupEncoderState(size_t StateCount) {
lodepng::State State = {};

lodepng_state_init(&State);

unsigned int Depth = 2, RawDepth = (unsigned int)std::floor(std::log2(StateCount) + 1.0);
while (Depth < RawDepth) Depth *= 2;

State.info_raw.colortype = LCT_PALETTE;
State.info_raw.bitdepth = Depth;
State.info_raw.bitdepth = sizeof(CellType) * 8;
State.info_png.color.colortype = LCT_PALETTE;
State.info_png.color.bitdepth = Depth;
State.info_png.color.bitdepth = sizeof(CellType) * 8;
State.encoder.auto_convert = 0;

// DEBUG_PRINT("Setting up palette with %d colors (%d bit depth):\n", (int)StateCount, (int)Depth);
Expand Down Expand Up @@ -105,19 +103,14 @@ class EncoderState {
return State;
}

void m_DiscardEncoderState(lodepng::State& State) {
lodepng_state_cleanup(&State);
}

template<typename CellType, typename SizeType>
unsigned int m_EncodeBuffer(const CellType* Grid, const Vector2<SizeType>& Size, size_t StateCount, const std::string& Path) {
std::vector<uint8_t> Buffer = {};
unsigned int Result = 0;
lodepng::State State = m_AcquireEncoderState(StateCount);
lodepng::State State = m_SetupEncoderState<CellType>(StateCount);

Result = lodepng::encode(Buffer, (const unsigned char*)Grid, (unsigned int)Size.X, (unsigned int)Size.Y, State);

m_DiscardEncoderState(State);
if (Result) { DEBUG_PRINT("lodepng::encode -> %d\n", Result); return Result; }

Result = lodepng::save_file(Buffer, Path);
Expand Down
14 changes: 14 additions & 0 deletions WEBSITE/Scripts/Ant.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ class Ant {
return 1
}

ToObject() {
return {
Direction: { X: this.Direction.X, Y: this.Direction.Y },
Position: { X: this.Position.X, Y: this.Position.Y },
StateMachine: this.StateMachine.slice(0),
StepSize: this.StepSize,
Wrap: this.Wrap
}
}

static FromObject(Value) {
return new Ant(...Object.values(Value.Position), ...Object.values(Value.Direction), Value.StateMachine, Value.Wrap, Value.StepSize)
}

constructor(X, Y, DX, DY, StateMachine, Wrap = false, StepSize = 1) {
this.Position.X = X
this.Position.Y = Y
Expand Down
2 changes: 1 addition & 1 deletion WEBSITE/Scripts/Classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SimulationState {
Reset() {
this.Ants = []
for (let AntObject of this.TemplateAnts) {
this.Ants.push(new Ant(AntObject.Position.X, AntObject.Position.Y, AntObject.Direction.X, AntObject.Direction.Y, AntObject.StateMachine, AntObject.Wrap, AntObject.StepSize))
this.Ants.push(new Ant(AntObject.Position.X, AntObject.Position.Y, AntObject.Direction.X, AntObject.Direction.Y, AntObject.StateMachine, AntObject.Wrap, AntObject.StepSize))
}

this.TotalIterations = 0
Expand Down
Loading

0 comments on commit 90b4a01

Please sign in to comment.