Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

06_sprite.cpp 2.8 KiB

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