Implementing more bits.

This commit is contained in:
2024-09-24 21:39:03 -04:00
parent 7bf000445a
commit 201cf7ed6a
2 changed files with 146 additions and 60 deletions

View File

@@ -10,6 +10,10 @@ endif()
set(CMAKE_CXX_STANDARD 20)
# Set for profiling
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(cmake/CPM.cmake)

View File

@@ -17,7 +17,10 @@ private:
CaveGame::Core::PerlinNoise noise(0);
int test_tiles[512][512];
constexpr int test_grid_size = 256;
constexpr float test_grid_scale = 2;
int test_tiles[test_grid_size][test_grid_size];
JGL::Font Jupiteroid;
float z = 0;
@@ -25,6 +28,16 @@ float z = 0;
class CaveGameWindow : public ReWindow::RWindow
{
public:
uint64_t frame_count = 0;
float frame_rate = 0;
float avg_frame_rate = 0;
float delta_time = 0;
float last_frame_elapsed = 0;
bool render_grid = true;
bool generate_grid = true;
CaveGameWindow() : ReWindow::RWindow() {}
CaveGameWindow(const std::string& title, int width, int height) : ReWindow::RWindow(title, width, height) {}
@@ -40,6 +53,81 @@ public:
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
procgen();
}
void draw_grid()
{
JGL::J2D::Begin();
for (int tx = 0; tx < test_grid_size; tx++)
{
for (int ty = 0; ty < test_grid_size; ty++)
{
Color4 color = {0,0,0,0};
int tile_id = test_tiles[tx][ty];
switch(tile_id)
{
case 0:
color = Colors::Reds::Crimson; break;
case 1:
color = Colors::Reds::LightCoral; break;
case 2:
color = Colors::Browns::Bisque; break;
case 3:
color = Colors::Browns::BlanchedAlmond; break;
case 4:
color = Colors::Yellow; break;
case 5:
color = Colors::Browns::Maroon; break;
case 6:
color = Colors::Greens::LawnGreen; break;
case 7:
color = Colors::Oranges::Tomato; break;
case 8:
color = Colors::Yellow; break;
case 9:
color = Colors::Greens::Chartreuse; break;
case 10:
color = Colors::Blues::SkyBlue; break;
default:
color = Colors::White;
}
JGL::J2D::FillRect(color,
{static_cast<float>(tx*test_grid_scale), static_cast<float>(ty*test_grid_scale)},
{1*test_grid_scale, 1*test_grid_scale});
}
}
JGL::J2D::End();
}
void draw_debug_info(std::vector<std::string> tokens)
{
int font_size = 14;
Color4 bg_color = {0,0,0,192};
Color4 text_color = Colors::Blues::SkyBlue;
JGL::Font font = Jupiteroid;
Vector2 pos = {2, 2};
Vector2 text_size = font.MeasureString(tokens[0], font_size);
JGL::J2D::Begin();
int index = 0;
for (auto token : tokens)
{
text_size = font.MeasureString(token, font_size);
JGL::J2D::FillRect(bg_color, pos, text_size);
JGL::J2D::DrawString(text_color, token, pos.x, pos.y, 1.f, font_size, font);
pos.y += text_size.y;
}
JGL::J2D::End();
}
void display()
@@ -50,80 +138,85 @@ public:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (render_grid)
draw_grid();
JGL::J2D::Begin();
for (int tx = 0; tx < 512; tx++)
draw_debug_info({
"Re-CaveGame - Redacted Software",
std::format("frame: {}", frame_count),
std::format("delta: {}ms", J3ML::Math::Round(delta_time*1000)),
std::format("fps: {}", J3ML::Math::Round(frame_rate)),
});
}
void procgen()
{
z += 0.025;
for (int tx = 0; tx < test_grid_size; tx++)
{
for (int ty = 0; ty < 512; ty++)
for (int ty = 0; ty < test_grid_size; ty++)
{
Color4 color = {0,0,0,0};
float sx = 40.f, sy = 40.f;
float s2x = 10.f, s2y = 10.f;
int tile_id = test_tiles[tx][ty];
float scale = 10;
auto raw_noise = noise.Noise(tx/sx, ty/sy, z) + 0.5;
if (tile_id == 0)
color = Colors::Reds::Crimson;
if (tile_id == 1)
color = Colors::Reds::LightCoral;
if (tile_id == 2)
color = Colors::Browns::Bisque;
if (tile_id == 3)
color = Colors::Browns::BlanchedAlmond;
if (tile_id == 4)
color = Colors::Yellow;
if (tile_id == 5)
color = Colors::Browns::Maroon;
if (tile_id == 6)
color = Colors::Greens::LawnGreen;
if (tile_id == 7)
color = Colors::Oranges::Tomato;
if (tile_id == 8)
color = Colors::Yellow;
if (tile_id == 9)
color = Colors::Greens::Chartreuse;
if (tile_id == 10)
color = Colors::Blues::SkyBlue;
auto noise_overlay = noise.Noise(tx/s2x, ty/s2y, z/2.f) + 0.5f;
JGL::J2D::FillRect(color, {static_cast<float>(tx*2), static_cast<float>(ty)}, {2, 1});
test_tiles[tx][ty] = (int)((raw_noise*noise_overlay) *scale);
}
}
JGL::J2D::End();
// Draw Watermark
JGL::J2D::Begin();
JGL::J2D::DrawString(Colors::White, "Re-CaveGame - Redacted Software", 0,0, 1, 12, Jupiteroid);
JGL::J2D::End();
}
void OnRefresh(float elapsed) override
{
z += 0.05;
for (int tx = 0; tx < 512; tx++)
{
for (int ty = 0; ty < 512; ty++)
{
float sx = 20.f, sy = 20.f;
auto begin_frame = std::chrono::high_resolution_clock::now();
float scale = 10;
auto raw_noise = noise.Noise(tx/sx, ty/sy, z) + 0.5;
test_tiles[tx][ty] = (int)(raw_noise*scale);
}
if (generate_grid) {
if (frame_count % 2 == 0)
procgen();
}
display();
int glError = glGetError();
if (glError != GL_NO_ERROR)
std::cout << glError << std::endl;
glSwapBuffers();
auto end_frame = std::chrono::high_resolution_clock::now();
auto frame_time = end_frame - begin_frame;
int frame_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(frame_time).count();
float frame_time_s = frame_time_ms / 1000.f;
delta_time = frame_time_s;
frame_rate = 1.f / delta_time;
frame_count++;
}
void OnMouseButtonDown(const ReWindow::WindowEvents::MouseButtonDownEvent &ev) override
{
RWindow::OnMouseButtonDown(ev);
}
void OnKeyUp(const ReWindow::WindowEvents::KeyUpEvent &ev) override
{
if (ev.key == Keys::G)
generate_grid = !generate_grid;
if (ev.key == Keys::V)
render_grid = !render_grid;
//RWindow::OnKeyUp(ev);
}
void OnMouseButtonUp(const ReWindow::WindowEvents::MouseButtonUpEvent &ev) override
{
RWindow::OnMouseButtonUp(ev);
@@ -138,23 +231,12 @@ public:
int main() {
srand(0);
for (int tx = 0; tx < 512; tx++)
{
for (int ty = 0; ty < 512; ty++)
{
float sx = 100.f, sy = 100.f;
float scale = 6;
auto raw_noise = noise.Noise(tx/sx, ty/sy, z) + 0.5;
test_tiles[tx][ty] = (int)(raw_noise*scale);
}
}
auto* window = new CaveGameWindow("Re-CaveGame", 1280, 720);
window->setRenderer(RenderingAPI::OPENGL);
window->Open();
window->initGL();
window->setResizable(true);
window->setVsyncEnabled(true);
window->setVsyncEnabled(false);
while (window->isAlive()) {
window->pollEvents();