Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

06_sprite.cpp 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Lol Engine — Sprite tutorial
  3. //
  4. // Copyright © 2011—2019 Sam Hocevar <sam@hocevar.net>
  5. // © 2012 Daniel Stephens (artwork)
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #if HAVE_CONFIG_H
  14. # include "config.h"
  15. #endif
  16. #include <lol/engine.h>
  17. using namespace lol;
  18. class SpriteTutorial : public WorldEntity
  19. {
  20. public:
  21. SpriteTutorial()
  22. {
  23. m_camera = new Camera();
  24. m_camera->SetView(mat4(1.f));
  25. m_camera->SetProjection(mat4::ortho(0.f, 640.f, 0.f, 480.f, -100.f, 100.f));
  26. Ticker::Ref(m_camera);
  27. m_tileset = TileSet::create("06_sprite.png");
  28. for (int i = 0; i < FRAME_COUNT; ++i)
  29. m_tileset->define_tile(ibox2(i * 24, 376, 24 + i * 24, 24 + 376));
  30. for (int i = 0; i < SPRITE_COUNT; ++i)
  31. {
  32. m_sprites.push(vec3((float)rand(-96, 640), (float)rand(-96, 480), 0.f),
  33. rand(0.f, 1.f));
  34. }
  35. }
  36. ~SpriteTutorial()
  37. {
  38. TileSet::destroy(m_tileset);
  39. Ticker::Unref(m_camera);
  40. }
  41. virtual void tick_game(float seconds) override
  42. {
  43. for (int i = 0; i < SPRITE_COUNT; ++i)
  44. {
  45. m_sprites[i].m1.y += 50.f * seconds;
  46. m_sprites[i].m2 = lol::fmod(m_sprites[i].m2 + seconds, 1.f);
  47. if (m_sprites[i].m1.y > 480 + 48)
  48. m_sprites[i].m1.y = (float)rand(-96, -48);
  49. }
  50. WorldEntity::tick_game(seconds);
  51. }
  52. virtual bool init_draw() override
  53. {
  54. Scene& scene = Scene::GetScene();
  55. scene.PushCamera(m_camera);
  56. scene.get_renderer()->clear_color(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  57. return true;
  58. }
  59. virtual void tick_draw(float seconds, Scene &scene) override
  60. {
  61. WorldEntity::tick_draw(seconds, scene);
  62. for (int i = 0; i < SPRITE_COUNT; ++i)
  63. {
  64. int frame = (int)(m_sprites[i].m2 * FRAME_COUNT);
  65. // m_sprites[i].m1.z = frame;
  66. scene.AddTile(m_tileset, frame,
  67. m_sprites[i].m1, vec2(2.f), 0.f);
  68. }
  69. }
  70. virtual bool release_draw() override
  71. {
  72. Scene& scene = Scene::GetScene();
  73. scene.PopCamera(m_camera);
  74. return true;
  75. }
  76. private:
  77. Camera *m_camera;
  78. TileSet *m_tileset;
  79. static int const SPRITE_COUNT = 192;
  80. static int const FRAME_COUNT = 16;
  81. array<vec3, float> m_sprites;
  82. };
  83. int main(int argc, char **argv)
  84. {
  85. sys::init(argc, argv);
  86. Application app("Tutorial 6: Sprite", ivec2(640, 480), 60.0f);
  87. new SpriteTutorial();
  88. app.Run();
  89. return EXIT_SUCCESS;
  90. }