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.

cygnet-test.cc 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <cygnet/Context.h>
  2. #include <cygnet/Window.h>
  3. #include <cygnet/Renderer.h>
  4. #include <swan-common/constants.h>
  5. #include <stdint.h>
  6. #include <SDL_image.h>
  7. #include <SDL.h>
  8. void addTile(Cygnet::Renderer &rnd, const char *path) {
  9. static size_t id = 0;
  10. SDL_Surface *surf = IMG_Load(path);
  11. rnd.registerTileTexture(id++, surf->pixels, surf->pitch * surf->h);
  12. SDL_FreeSurface(surf);
  13. }
  14. int main() {
  15. Cygnet::Context ctx;
  16. IMG_Init(IMG_INIT_PNG);
  17. //Cygnet::Window win("Cygnet Test", 640, 480);
  18. Cygnet::Window win("Cygnet Test", 680, 680);
  19. Cygnet::Renderer rnd;
  20. for (auto path: {
  21. "core.mod/assets/tile/dirt.png",
  22. "core.mod/assets/tile/grass.png",
  23. "core.mod/assets/tile/leaves.png",
  24. "core.mod/assets/tile/stone.png",
  25. "core.mod/assets/tile/torch.png",
  26. "core.mod/assets/tile/tree-trunk.png",
  27. }) addTile(rnd, path);
  28. rnd.uploadTileTexture();
  29. Cygnet::RenderChunk chunk;
  30. {
  31. uint16_t tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT];
  32. memset(tiles, 0, sizeof(tiles));
  33. tiles[0] = 1;
  34. tiles[1] = 2;
  35. tiles[2] = 3;
  36. chunk = rnd.createChunk(tiles);
  37. }
  38. Cygnet::RenderCamera cam = {
  39. .pos = { 0.9, 0.9 },
  40. .zoom = 0.125,
  41. };
  42. while (true) {
  43. SDL_Event evt;
  44. while (SDL_PollEvent(&evt)) {
  45. switch (evt.type) {
  46. case SDL_QUIT:
  47. goto exit;
  48. }
  49. }
  50. rnd.drawChunk({0, 0}, chunk);
  51. win.clear();
  52. rnd.draw(cam);
  53. win.flip();
  54. }
  55. exit:
  56. IMG_Quit();
  57. }