diff --git a/.gitignore b/.gitignore index 91203af4..e7893320 100644 --- a/.gitignore +++ b/.gitignore @@ -85,7 +85,9 @@ tutorial/02_cube tutorial/03_noise tutorial/04_texture tutorial/05_easymesh +tutorial/06_sprite tutorial/08_fbo tutorial/11_fractal +tutorial/12_voronoi # Our data doc/doxygen.cfg diff --git a/tutorial/06_sprite.cpp b/tutorial/06_sprite.cpp new file mode 100644 index 00000000..f57887cc --- /dev/null +++ b/tutorial/06_sprite.cpp @@ -0,0 +1,106 @@ +// +// Lol Engine - Sprite tutorial +// +// Copyright: (c) 2011-2013 Sam Hocevar +// (c) 2012 Daniel Stephens (artwork) +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include "core.h" + +using namespace std; +using namespace lol; + +class SpriteTutorial : public WorldEntity +{ +public: + SpriteTutorial() + { + m_camera = new Camera(); + m_camera->SetView(mat4(1.f)); + m_camera->SetProjection(mat4::ortho(0.f, 640.f, 0.f, 480.f, -100.f, 100.f)); + Scene::GetDefault()->PushCamera(m_camera); + Ticker::Ref(m_camera); + + m_tileset = Tiler::Register("06_sprite.png"); + for (int i = 0; i < FRAME_COUNT; ++i) + m_tileset->AddTile(ibox2(i * 24, 376, 24 + i * 24, 24 + 376)); + + for (int i = 0; i < SPRITE_COUNT; ++i) + { + m_sprites.Push(ivec3(rand(-96, 640), rand(-96, 480), 0), + rand(0.f, 1.f)); + } + + m_ready = false; + } + + ~SpriteTutorial() + { + Tiler::Deregister(m_tileset); + + Scene::GetDefault()->PopCamera(m_camera); + Ticker::Unref(m_camera); + } + + virtual void TickGame(float seconds) + { + for (int i = 0; i < SPRITE_COUNT; ++i) + { + m_sprites[i].m1.y += 50.f * seconds; + m_sprites[i].m2 = lol::fmod(m_sprites[i].m2 + seconds, 1.f); + if (m_sprites[i].m1.y > 480 + 48) + m_sprites[i].m1.y = rand(-96, -48); + } + + WorldEntity::TickGame(seconds); + } + + virtual void TickDraw(float seconds) + { + WorldEntity::TickDraw(seconds); + + if (!m_ready) + { + Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f)); + m_ready = true; + } + + for (int i = 0; i < SPRITE_COUNT; ++i) + { + int frame = (int)(m_sprites[i].m2 * FRAME_COUNT); +// m_sprites[i].m1.z = frame; + Scene::GetDefault()->AddTile(m_tileset, frame, + (ivec3)m_sprites[i].m1, 0, vec2(2.f)); + } + } + +private: + Camera *m_camera; + TileSet *m_tileset; + + static int const SPRITE_COUNT = 192; + static int const FRAME_COUNT = 16; + Array m_sprites; + + bool m_ready; +}; + +int main(int argc, char **argv) +{ + System::Init(argc, argv); + + Application app("Tutorial 6: Sprite", ivec2(640, 480), 60.0f); + new SpriteTutorial(); + app.Run(); + + return EXIT_SUCCESS; +} + diff --git a/tutorial/06_sprite.png b/tutorial/06_sprite.png new file mode 100644 index 00000000..e0bdabbb Binary files /dev/null and b/tutorial/06_sprite.png differ diff --git a/tutorial/06_sprite.vcxproj b/tutorial/06_sprite.vcxproj new file mode 100644 index 00000000..d7221b91 --- /dev/null +++ b/tutorial/06_sprite.vcxproj @@ -0,0 +1,68 @@ + + + + + Debug + PS3 + + + Debug + Win32 + + + Debug + x64 + + + Debug + Xbox 360 + + + Release + PS3 + + + Release + Win32 + + + Release + x64 + + + Release + Xbox 360 + + + + + + + + {9e62f2fe-3408-4eae-8238-fd84238ceeda} + + + {83d3b207-c601-4025-8f41-01dedc354661} + + + + {1c5b8702-290c-42da-aa9e-671348f5b747} + Application + Win32Proj + + + + + + + + + + + + + + + + + diff --git a/tutorial/Makefile.am b/tutorial/Makefile.am index a10a821c..dfb0a477 100644 --- a/tutorial/Makefile.am +++ b/tutorial/Makefile.am @@ -2,7 +2,7 @@ include $(top_srcdir)/build/autotools/common.am noinst_PROGRAMS = 01_triangle 02_cube 03_noise 04_texture 05_easymesh \ - 08_fbo 11_fractal \ + 06_sprite 08_fbo 11_fractal \ 12_voronoi 01_triangle_SOURCES = 01_triangle.cpp 01_triangle.lolfx @@ -25,6 +25,14 @@ noinst_PROGRAMS = 01_triangle 02_cube 03_noise 04_texture 05_easymesh \ 05_easymesh_CPPFLAGS = $(AM_CPPFLAGS) 05_easymesh_DEPENDENCIES = @LOL_DEPS@ +06_sprite_SOURCES = 06_sprite.cpp 06_sprite.png +06_sprite_CPPFLAGS = $(AM_CPPFLAGS) +06_sprite_DEPENDENCIES = @LOL_DEPS@ +06_sprite_LDFLAGS = $(AM_LDFLAGS) +if USE_EMSCRIPTEN +06_sprite_LDFLAGS += --preload-file 06_sprite.png +endif + 08_fbo_SOURCES = 08_fbo.cpp 08_fbo.lolfx 08_fbo_CPPFLAGS = $(AM_CPPFLAGS) 08_fbo_DEPENDENCIES = @LOL_DEPS@