~untaugh/meteormaker

5e1a4a317b487e127e303e03ce528e63e4ddf406 — Oskar Rundgren a month ago 74c3c21
Use other thread for generating meteor. A meteor with large vector count was blocking drawing.
5 files changed, 48 insertions(+), 9 deletions(-)

M CMakeLists.txt
M meteor.cc
M meteor.h
M meteormaker.cc
M meteormaker.h
M CMakeLists.txt => CMakeLists.txt +1 -0
@@ 17,6 17,7 @@ find_package(Freetype REQUIRED)
target_link_libraries(metmak glfw)
target_link_libraries(metmak Vulkan::Vulkan)
target_link_libraries(metmak png)
target_link_libraries(metmak pthread)
target_link_libraries(metmak Freetype::Freetype)

target_include_directories(metmak PRIVATE Vulkan::Vulkan)

M meteor.cc => meteor.cc +7 -5
@@ 10,8 10,8 @@
namespace meteormaker
{

Shape Meteor::shape(const uint32_t size,
                    const uint32_t pointiness) const
void Meteor::generateShape(const uint32_t size,
                    const uint32_t pointiness)
{
  Shape meteor;



@@ 31,12 31,14 @@ Shape Meteor::shape(const uint32_t size,

  meteor.generate();

  return meteor;
  shape = meteor;
  startGenerate = false;
  ready = true;
}

Shape Meteor::shape() const
void Meteor::generateShape()
{
  return shape(size, pointiness);
  generateShape(size, pointiness);
}

void Meteor::setSetting(const Setting &setting)

M meteor.h => meteor.h +6 -2
@@ 26,8 26,9 @@ struct Setting
class Meteor
{
public:
  Shape shape(const uint32_t size, const uint32_t pointiness) const;
  Shape shape() const;
  void generateShape(const uint32_t size, const uint32_t pointiness);
  void generateShape();

  bool settingChanged(entt::registry &registry);

  std::vector <Vertex> generate(uint32_t size,


@@ 35,6 36,9 @@ public:
                                uint32_t seed) const;
  std::vector <Face> generate(std::vector <Vertex> &vertices) const;

  bool ready = false;
  bool startGenerate = false;
  Shape shape;
private:
  uint32_t size = 0;
  uint32_t pointiness = 0;

M meteormaker.cc => meteormaker.cc +32 -2
@@ 4,6 4,8 @@

#include <string>
#include <vector>
#include <thread>
#include <chrono>

#include "meteor.h"
#include "star.h"


@@ 36,6 38,18 @@ void MeteorMaker::init()
  render.init(window);
}

void MeteorMaker::generateLoop(bool &finished, Meteor &meteor)
{
  while(!finished)
  {
    if (meteor.startGenerate)
    {
      meteor.generateShape();
    }
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }
}

void MeteorMaker::loop()
{
  auto meteorEntity = registry.create();


@@ 51,7 65,10 @@ void MeteorMaker::loop()
  Meteor meteor;
  Star star;

  shapes.push_back(meteor.shape(1, 1));
  meteor.generateShape();
  while (!meteor.ready) {};

  shapes.push_back(meteor.shape);
  shapes.push_back(star.shape());

  const char* TEXTURE_POINT[] = {"textures/point00.png",


@@ 122,6 139,10 @@ void MeteorMaker::loop()

  Time time;

  bool finished = false;

  std::thread generate(generateLoop, std::ref(finished), std::ref(meteor));

  while(!glfwWindowShouldClose(window))
  {
    glfwPollEvents();


@@ 142,13 163,22 @@ void MeteorMaker::loop()

    if (meteor.settingChanged(registry))
    {
      shapes[0] = meteor.shape();
      meteor.startGenerate = true;
    }

    if (meteor.ready)
    {
      shapes[0] = meteor.shape;
      render.loadShapes(shapes, registry);
    }

    render.draw();
  }

  finished = true;

  generate.join();

  glfwDestroyWindow(window);
  glfwTerminate();
}

M meteormaker.h => meteormaker.h +2 -0
@@ 8,6 8,7 @@

#include "render_vulkan.h"
#include "sprite.h"
#include "meteor.h"

const int WIDTH = 640;
const int HEIGHT = 480;


@@ 28,6 29,7 @@ class MeteorMaker
  void loop();
  void clean();
 private:
  static void generateLoop(bool &finished, Meteor &meteor);
  GLFWwindow *window;
  RenderVulkan render;
  std::vector<Sprite> sprites;