|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2025, Kazankov Nikolay |
| 3 | + * <nik.kazankov.05@mail.ru> |
| 4 | + */ |
| 5 | + |
| 6 | +#include "animations.hpp" |
| 7 | + |
| 8 | +#if (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS) |
| 9 | + |
| 10 | +#include "loader/loader.hpp" |
| 11 | +#include "../exceptions.hpp" |
| 12 | + |
| 13 | + |
| 14 | +AnimationsData::AnimationsData() { |
| 15 | + // Resetting texture array |
| 16 | + #if (CHECK_CORRECTION) |
| 17 | + for (unsigned i=0; i < unsigned(Animations::Count); ++i) { |
| 18 | + animations[i] = nullptr; |
| 19 | + } |
| 20 | + #endif |
| 21 | + |
| 22 | + // Loading all needed textures |
| 23 | + for (unsigned i=0; i < unsigned(Animations::Count); ++i) { |
| 24 | + loadAnimation(Animations(i), animationsFilesNames[i]); |
| 25 | + } |
| 26 | + |
| 27 | + // Checking massive on loading correction |
| 28 | + #if (CHECK_CORRECTION) |
| 29 | + for (unsigned i=0; i < unsigned(Animations::Count); ++i) { |
| 30 | + if (animations[i] == nullptr) { |
| 31 | + throw DataLoadException(animationsFilesNames[i]); |
| 32 | + } |
| 33 | + } |
| 34 | + logAdditional("Animations loaded corretly"); |
| 35 | + #endif |
| 36 | +} |
| 37 | + |
| 38 | +AnimationsData::~AnimationsData() { |
| 39 | + // Closing all animations |
| 40 | + for (unsigned i=0; i < unsigned(Animations::Count); ++i) { |
| 41 | + IMG_FreeAnimation(animations[i]); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void AnimationsData::loadAnimation(Animations _index, const char* _fileName) { |
| 46 | + // Load data of current texture |
| 47 | + SDL_IOStream* data = dataLoader.load(_fileName); |
| 48 | + |
| 49 | + // Creating texture |
| 50 | + animations[unsigned(_index)] = IMG_LoadAnimation_IO(data, true); |
| 51 | + |
| 52 | + // Checking correction of loaded texture |
| 53 | + #if (CHECK_CORRECTION) |
| 54 | + if (animations[unsigned(_index)] == nullptr) { |
| 55 | + throw DataLoadException(_fileName); |
| 56 | + } |
| 57 | + #endif |
| 58 | +} |
| 59 | + |
| 60 | +IMG_Animation* AnimationsData::operator[] (Animations _index) const { |
| 61 | + return animations[unsigned(_index)]; |
| 62 | +} |
| 63 | + |
| 64 | +#endif // (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS) |
0 commit comments