LucidCube/tests/NoiseTest/NoiseTest.cpp
Rebekah Rowe 6c4b2e9186
Implement GPL3+ and Apache2.0 Dual License.
Commit is being made to allow additions of GPL3+ code previously
un-addable. With these changes, contributions back to cuberite are
possible with the backporting exemtion, as well as adding stuff in
minetest with minetest code properly being read through and implimented
to upgrade it to GPL3 from GPL2.

project still has Apache2.0 license and credits to all its contributers, but now has the freedom of GPL3+ and all the code that can be implimented and shared with it.
2023-03-20 11:49:56 -04:00

118 lines
2.5 KiB
C++

/*
* Copyright 2011-2022 Cuberite Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// NoiseTest.cpp
// Implements the main app entrypoint
#include "Globals.h"
#include <time.h>
#include "Noise.h"
void SaveValues(NOISE_DATATYPE * a_Values, const AString & a_FileName)
{
cFile f;
if (!f.Open(a_FileName, cFile::fmWrite))
{
LOGWARNING("Cannot write file %s", a_FileName.c_str());
return;
}
for (int y = 0; y < 256; y++)
{
unsigned char val[256];
for (int x = 0; x < 256; x++)
{
val[x] = std::min(255, std::max(0, (int)(256 * a_Values[x + 256 * y])));
}
f.Write(val, 256);
}
}
clock_t TestCubicNoise(void)
{
cCubicNoise Cubic(0);
NOISE_DATATYPE Values[256 * 256];
// Do a speed test:
clock_t Begin = clock();
for (int i = 0; i < 1000; i++)
{
Cubic.Generate2D(Values, 256, 256, 0, (NOISE_DATATYPE)25.6, 0, (NOISE_DATATYPE)25.6);
}
clock_t Ticks = clock() - Begin;
LOG("cCubicNoise generating 1000 * 256x256 values took %d ticks (%.02f sec)", Ticks, (double)Ticks / CLOCKS_PER_SEC);
// Save the results into a file for visual comparison:
SaveValues(Values, "NoiseCubic.raw");
return Ticks;
}
clock_t TestOldNoise(void)
{
cNoise Noise(0);
NOISE_DATATYPE Values[256 * 256];
// Do a speed test:
clock_t Begin = clock();
for (int i = 0; i < 1000; i++)
{
for (int y = 0; y < 256; y++)
{
float fy = (float)y / 10;
for (int x = 0; x < 256; x++)
{
Values[x + 256 * y] = Noise.CubicNoise2D((float)x / 10, fy);
} // for x
} // for y
}
clock_t Ticks = clock() - Begin;
LOG("cNoise generating 1000 * 256x256 values took %d ticks (%.02f sec)", Ticks, (double)Ticks / CLOCKS_PER_SEC);
// Save the results into a file for visual comparison:
SaveValues(Values, "NoiseOld.raw");
return Ticks;
}
int main(int argc, char * argv[])
{
new cMCLogger(); // Create a logger (will set itself as the main instance
clock_t NewTicks = TestCubicNoise();
clock_t OldTicks = TestOldNoise();
LOG("New method is %.02fx faster", (double)OldTicks / NewTicks);
LOG("Press Enter to quit program");
getchar();
}