Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

108 righe
2.7 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 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. Scene& scene = Scene::GetScene();
  25. scene.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(vec3((float)rand(-96, 640), (float)rand(-96, 480), 0.f),
  33. rand(0.f, 1.f));
  34. }
  35. m_ready = false;
  36. }
  37. ~SpriteTutorial()
  38. {
  39. Tiler::Deregister(m_tileset);
  40. Scene& scene = Scene::GetScene();
  41. scene.PopCamera(m_camera);
  42. Ticker::Unref(m_camera);
  43. }
  44. virtual void TickGame(float seconds)
  45. {
  46. for (int i = 0; i < SPRITE_COUNT; ++i)
  47. {
  48. m_sprites[i].m1.y += 50.f * seconds;
  49. m_sprites[i].m2 = lol::fmod(m_sprites[i].m2 + seconds, 1.f);
  50. if (m_sprites[i].m1.y > 480 + 48)
  51. m_sprites[i].m1.y = (float)rand(-96, -48);
  52. }
  53. WorldEntity::TickGame(seconds);
  54. }
  55. virtual void TickDraw(float seconds, Scene &scene)
  56. {
  57. WorldEntity::TickDraw(seconds, scene);
  58. if (!m_ready)
  59. {
  60. Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  61. m_ready = true;
  62. }
  63. for (int i = 0; i < SPRITE_COUNT; ++i)
  64. {
  65. int frame = (int)(m_sprites[i].m2 * FRAME_COUNT);
  66. // m_sprites[i].m1.z = frame;
  67. scene.AddTile(m_tileset, frame,
  68. m_sprites[i].m1, 0, vec2(2.f), 0.f);
  69. }
  70. }
  71. private:
  72. Camera *m_camera;
  73. TileSet *m_tileset;
  74. static int const SPRITE_COUNT = 192;
  75. static int const FRAME_COUNT = 16;
  76. array<vec3, float> m_sprites;
  77. bool m_ready;
  78. };
  79. int main(int argc, char **argv)
  80. {
  81. System::Init(argc, argv);
  82. Application app("Tutorial 6: Sprite", ivec2(640, 480), 60.0f);
  83. new SpriteTutorial();
  84. app.Run();
  85. return EXIT_SUCCESS;
  86. }