Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

107 рядки
2.6 KiB

  1. //
  2. // Lol Engine - Sprite tutorial
  3. //
  4. // Copyright: (c) 2011-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2012 Daniel Stephens (artwork)
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #if defined HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include "core.h"
  15. using namespace std;
  16. using namespace lol;
  17. class SpriteTutorial : public WorldEntity
  18. {
  19. public:
  20. SpriteTutorial()
  21. {
  22. m_camera = new Camera();
  23. m_camera->SetView(mat4(1.f));
  24. m_camera->SetProjection(mat4::ortho(0.f, 640.f, 0.f, 480.f, -100.f, 100.f));
  25. Scene::GetDefault()->PushCamera(m_camera);
  26. Ticker::Ref(m_camera);
  27. m_tileset = Tiler::Register("06_sprite.png");
  28. for (int i = 0; i < FRAME_COUNT; ++i)
  29. m_tileset->AddTile(ibox2(i * 24, 376, 24 + i * 24, 24 + 376));
  30. for (int i = 0; i < SPRITE_COUNT; ++i)
  31. {
  32. m_sprites.Push(ivec3(rand(-96, 640), rand(-96, 480), 0),
  33. rand(0.f, 1.f));
  34. }
  35. m_ready = false;
  36. }
  37. ~SpriteTutorial()
  38. {
  39. Tiler::Deregister(m_tileset);
  40. Scene::GetDefault()->PopCamera(m_camera);
  41. Ticker::Unref(m_camera);
  42. }
  43. virtual void TickGame(float seconds)
  44. {
  45. for (int i = 0; i < SPRITE_COUNT; ++i)
  46. {
  47. m_sprites[i].m1.y += 50.f * seconds;
  48. m_sprites[i].m2 = lol::fmod(m_sprites[i].m2 + seconds, 1.f);
  49. if (m_sprites[i].m1.y > 480 + 48)
  50. m_sprites[i].m1.y = rand(-96, -48);
  51. }
  52. WorldEntity::TickGame(seconds);
  53. }
  54. virtual void TickDraw(float seconds)
  55. {
  56. WorldEntity::TickDraw(seconds);
  57. if (!m_ready)
  58. {
  59. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  60. m_ready = true;
  61. }
  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::GetDefault()->AddTile(m_tileset, frame,
  67. (ivec3)m_sprites[i].m1, 0, vec2(2.f));
  68. }
  69. }
  70. private:
  71. Camera *m_camera;
  72. TileSet *m_tileset;
  73. static int const SPRITE_COUNT = 192;
  74. static int const FRAME_COUNT = 16;
  75. Array<vec3, float> m_sprites;
  76. bool m_ready;
  77. };
  78. int main(int argc, char **argv)
  79. {
  80. System::Init(argc, argv);
  81. Application app("Tutorial 6: Sprite", ivec2(640, 480), 60.0f);
  82. new SpriteTutorial();
  83. app.Run();
  84. return EXIT_SUCCESS;
  85. }