Skip to content

Commit a456410

Browse files
committed
Added and tested animations
Reworked and reverted animations, that can be preloaded
1 parent ed5edbc commit a456410

26 files changed

+225
-59
lines changed

ani/menu.gif

624 KB
Loading

src/GUI/animation.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@
88
#if (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS)
99

1010

11-
GUI::Animation::Animation(SDL_Rect _rect, ANI_names _type)
12-
: dest(_dest), type(_type), frame(0), prevTick(0) {}
11+
GUI::Animation::Animation(const Window& _window, float _X, float _Y, float _W, float _H, Animations _type)
12+
: Animation(_window, {_X*window.getWidth(), _Y*window.getHeight(), _W*window.getWidth(), _H*window.getHeight()}, _type) {}
13+
14+
GUI::Animation::Animation(const Window& _window, const SDL_FRect& _dest, Animations _type)
15+
: TextureTemplate(_window),
16+
type(_type),
17+
prevTick(0),
18+
animation(window.getAnimation(_type)) {
19+
rect = _dest;
20+
texture = window.createTexture(animation->frames[0]);
21+
prevTick = getTime() + animation->delays[0];
22+
}
1323

1424
GUI::Animation::~Animation() {
1525
SDL_DestroyTexture(texture);
1626
}
1727

18-
void GUI::Animation::blit() {
28+
void GUI::Animation::update() {
1929
if (SDL_GetTicks() > prevTick) {
20-
static unsigned frame = (frame + 1) % Animations[type]->count;
21-
window.destroy(texture);
22-
texture = window.createTexture(Animations[type]->frames[frame], false);
23-
prevTick = SDL_GetTicks() + Animations[type]->delays[frame] / 2;
30+
frame = (frame + 1) % animation->count;
31+
SDL_DestroyTexture(texture);
32+
texture = window.createTexture(animation->frames[frame]);
33+
prevTick = getTime() + animation->delays[frame];
2434
}
2535
}
2636

src/GUI/baseGUI.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ namespace GUI {
9595
#if (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS)
9696
class Animation : public TextureTemplate {
9797
private:
98-
const Uint8 type;
99-
Uint64 prevTick;
100-
const SDL_FRect dest;
98+
const Animations type;
99+
const IMG_Animation* animation;
100+
unsigned frame = 0;
101+
timer prevTick;
101102

102103
public:
103-
Animation(const Window& window, SDL_FRect destination, ANI_names type);
104+
Animation(const Window& window, float X, float Y, float width, float height, Animations type);
105+
Animation(const Window& window, const SDL_FRect& destination, Animations type);
104106
~Animation();
105-
void blit() const;
107+
void update();
106108
};
107109
#endif
108110

src/animationsNames.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (C) 2024-2025, Kazankov Nikolay
3+
* <nik.kazankov.05@mail.ru>
4+
*/
5+
6+
#include "animationsNames.hpp"
7+
8+
9+
// Check, if can load animations and should preload it
10+
#if (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS)
11+
12+
13+
// File names of the corresponding textures
14+
const char* animationsFilesNames[unsigned(Animations::Count)] = {
15+
"ani/menu.gif",
16+
};
17+
18+
#endif // (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS)

src/animationsNames.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2024-2025, Kazankov Nikolay
3+
* <nik.kazankov.05@mail.ru>
4+
*/
5+
6+
#pragma once
7+
8+
#include "define.hpp"
9+
10+
11+
// Check, if can load animations and should preload it
12+
#if (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS)
13+
14+
15+
// Names of all images with related numbers
16+
enum class Animations : unsigned {
17+
// Base part
18+
Menu,
19+
20+
// Count of all animations
21+
Count,
22+
};
23+
24+
// File names of the corresponding animations
25+
extern const char* animationsFilesNames[unsigned(Animations::Count)];
26+
27+
#endif // (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS)

src/data/exceptions.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
#pragma once
77

8-
#include <SDL3/SDL_log.h>
8+
#include "logger.hpp"
9+
10+
// Custom exceptions
11+
#if (CHECK_CORRECTION)
12+
913
#include <stdexcept>
1014
#include <string>
11-
#include "logger.hpp"
1215

1316

14-
// Custom exceptions
15-
#if CHECK_CORRECTION
1617
// Any error of loading
1718
class LoadException : public std::exception {
1819
protected:
@@ -47,4 +48,5 @@ class DataLoadException : LoadException {
4748
return message;
4849
}
4950
};
50-
#endif
51+
52+
#endif // (CHECK_CORRECTION)

src/data/libraries.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
Libraries::Libraries() {
1414
// Load depend on testing
15-
#if CHECK_CORRECTION
15+
#if (CHECK_CORRECTION)
1616
// Initialasing main library
1717
if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO)) {
1818
throw LibararyLoadException("Main library: " + std::string(SDL_GetError()));
@@ -39,7 +39,7 @@ Libraries::Libraries() {
3939
}
4040
#endif
4141
logAdditional("Libraries load correctly");
42-
#else // CHECK_CORRECTION
42+
#else // (CHECK_CORRECTION)
4343
SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO);
4444
#if USE_SDL_FONT
4545
TTF_Init();
@@ -49,7 +49,7 @@ Libraries::Libraries() {
4949
audioDeviceID = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL);
5050
Mix_OpenAudio(audioDeviceID, NULL);
5151
#endif
52-
#endif // CHECK_CORRECTION
52+
#endif // (CHECK_CORRECTION)
5353
}
5454

5555
Libraries::~Libraries() noexcept {

src/data/logger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// Function for log important information
1313
template <typename ...Args>
1414
void logImportant(const char* text, const Args& ...args) {
15-
#if CHECK_CORRECTION
15+
#if (CHECK_CORRECTION)
1616
// Writing to stdout
1717
SDL_Log(text, args...);
1818
#endif

src/data/preloaded/animations.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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)

src/data/preloaded/animations.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2024-2025, Kazankov Nikolay
3+
* <nik.kazankov.05@mail.ru>
4+
*/
5+
6+
#pragma once
7+
8+
#include "../../animationsNames.hpp"
9+
10+
// Check, if can load animations and should preload it
11+
#if (USE_SDL_IMAGE) && (PRELOAD_ANIMATIONS)
12+
#include <SDL3_image/SDL_image.h>
13+
14+
15+
// Class for load, get and clear preloaded animations
16+
class AnimationsData {
17+
private:
18+
IMG_Animation* animations[unsigned(Animations::Count)];
19+
void loadAnimation(Animations name, const char* fileName);
20+
21+
public:
22+
explicit AnimationsData();
23+
~AnimationsData();
24+
IMG_Animation* operator[] (Animations name) const;
25+
};
26+
27+
#endif // (USE_SDL_IMAGE) && (PRELOAD_TEXTURES)

0 commit comments

Comments
 (0)