A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lighting-test.cc 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <swan/LightServer.h>
  2. #include <swan/log.h>
  3. #include <png++/png.hpp>
  4. #include <chrono>
  5. class CB final: public Swan::LightCallback {
  6. public:
  7. void onLightChunkUpdated(const Swan::LightChunk &chunk, Swan::Vec2i pos) final {
  8. Swan::info << "light chunk at " << pos;
  9. chunk_ = chunk;
  10. done_ = true;
  11. cond_.notify_one();
  12. }
  13. Swan::LightChunk chunk_;
  14. bool done_ = false;
  15. std::mutex mut_;
  16. std::condition_variable cond_;
  17. };
  18. int main() {
  19. CB cb;
  20. Swan::LightServer lt(cb);
  21. Swan::NewLightChunk nc;
  22. auto set = [&](int x, int y) { nc.blocks[y * Swan::CHUNK_WIDTH + x] = true; };
  23. set(0, 0);
  24. set(18, 3);
  25. set(12, 13);
  26. set(28, 22);
  27. set(22, 12);
  28. for (int x = 4; x < 28; ++x) {
  29. set(x, 24);
  30. }
  31. for (int x = 12; x < 20; ++x) {
  32. set(x, 26);
  33. }
  34. nc.light_sources = {
  35. { { 20, 10 }, 20 },
  36. { { 16, 30 }, 20 },
  37. { { 5, 27 }, 20 },
  38. };
  39. lt.onChunkAdded({0, 0}, std::move(nc));
  40. std::unique_lock<std::mutex> lock(cb.mut_);
  41. cb.cond_.wait(lock, [&] { return cb.done_; });
  42. cb.done_ = false;
  43. lt.onSolidBlockAdded({ 10, 10 });
  44. cb.cond_.wait(lock, [&] { return cb.done_; });
  45. cb.done_ = false;
  46. png::image<png::rgb_pixel> image(Swan::CHUNK_WIDTH, Swan::CHUNK_HEIGHT);
  47. for (int y = 0; y < Swan::CHUNK_HEIGHT; ++y) {
  48. for (int x = 0; x < Swan::CHUNK_WIDTH; ++x) {
  49. uint8_t light = cb.chunk_.lightLevels[y * Swan::CHUNK_WIDTH + x];
  50. bool block = false;
  51. if (cb.chunk_.blocks[y * Swan::CHUNK_WIDTH + x]) {
  52. block = true;
  53. }
  54. bool isLight =
  55. (x == 20 && y == 10) ||
  56. (x == 16 && y == 30) ||
  57. (x == 5 && y == 27);
  58. unsigned char lightcol = (unsigned char)(sqrt(light) * 30);
  59. if (block) {
  60. image[y][x] = {
  61. lightcol, lightcol, lightcol };
  62. } else if (isLight) {
  63. image[y][x] = {
  64. 255, 255, 64 };
  65. } else {
  66. image[y][x] = {
  67. lightcol, 0, 0 };
  68. }
  69. }
  70. }
  71. image.write("lighting-test.png");
  72. }