пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <lol/engine.h>
  15. using namespace lol;
  16. class SpriteTutorial : public WorldEntity
  17. {
  18. public:
  19. SpriteTutorial()
  20. {
  21. m_camera = new Camera();
  22. m_camera->SetView(mat4(1.f));
  23. m_camera->SetProjection(mat4::ortho(0.f, 640.f, 0.f, 480.f, -100.f, 100.f));
  24. g_scene->PushCamera(m_camera);
  25. Ticker::Ref(m_camera);
  26. m_tileset = Tiler::Register("06_sprite.png");
  27. for (int i = 0; i < FRAME_COUNT; ++i)
  28. m_tileset->AddTile(ibox2(i * 24, 376, 24 + i * 24, 24 + 376));
  29. for (int i = 0; i < SPRITE_COUNT; ++i)
  30. {
  31. m_sprites.Push(vec3(rand(-96, 640), rand(-96, 480), 0),
  32. rand(0.f, 1.f));
  33. }
  34. m_ready = false;
  35. }
  36. ~SpriteTutorial()
  37. {
  38. Tiler::Deregister(m_tileset);
  39. g_scene->PopCamera(m_camera);
  40. Ticker::Unref(m_camera);
  41. }
  42. virtual void TickGame(float seconds)
  43. {
  44. for (int i = 0; i < SPRITE_COUNT; ++i)
  45. {
  46. m_sprites[i].m1.y += 50.f * seconds;
  47. m_sprites[i].m2 = lol::fmod(m_sprites[i].m2 + seconds, 1.f);
  48. if (m_sprites[i].m1.y > 480 + 48)
  49. m_sprites[i].m1.y = rand(-96, -48);
  50. }
  51. WorldEntity::TickGame(seconds);
  52. }
  53. virtual void TickDraw(float seconds, Scene &scene)
  54. {
  55. WorldEntity::TickDraw(seconds, scene);
  56. if (!m_ready)
  57. {
  58. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  59. m_ready = true;
  60. }
  61. for (int i = 0; i < SPRITE_COUNT; ++i)
  62. {
  63. int frame = (int)(m_sprites[i].m2 * FRAME_COUNT);
  64. // m_sprites[i].m1.z = frame;
  65. scene.AddTile(m_tileset, frame,
  66. m_sprites[i].m1, 0, vec2(2.f), 0.f);
  67. }
  68. }
  69. private:
  70. Camera *m_camera;
  71. TileSet *m_tileset;
  72. static int const SPRITE_COUNT = 192;
  73. static int const FRAME_COUNT = 16;
  74. array<vec3, float> m_sprites;
  75. bool m_ready;
  76. };
  77. int main(int argc, char **argv)
  78. {
  79. System::Init(argc, argv);
  80. Application app("Tutorial 6: Sprite", ivec2(640, 480), 60.0f);
  81. new SpriteTutorial();
  82. app.Run();
  83. return EXIT_SUCCESS;
  84. }