Skip to content

Commit

Permalink
Интеграция ImGui
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo506 committed Jan 27, 2022
1 parent fc5871c commit 64e09b8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ AnimDemo
*.cbp
*.layout
*.depend
obj/
bin/
7 changes: 4 additions & 3 deletions animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace animation
{
AnimationClip::AnimationClip(std::string path, sf::Sprite* sprite, sf::IntRect& rect, int countOfSprites) : m_lostTime(0), m_current(0), m_animSpeed(1)
AnimationClip::AnimationClip(std::string path, sf::Sprite* sprite, sf::IntRect& rect, int countOfSprites) : m_lostTime(0), m_current(0), m_animSpeed(1), m_pause(false)
{
m_numberOfSprites = countOfSprites;

Expand Down Expand Up @@ -42,6 +42,7 @@ namespace animation
m_numberOfSprites = 0;
m_animSpeed = 1;
m_currentSprite = sprite;
m_pause = false;
}


Expand Down Expand Up @@ -123,11 +124,11 @@ namespace animation

void AnimationClip::Update(int delta)
{
if (m_animTimes.size() != 0)
if (m_animTimes.size() != 0 && !m_pause)
{

m_lostTime += delta;
if (m_lostTime >= m_animTimes[m_current])
if (m_lostTime >= m_animTimes[m_current] / m_animSpeed)
{
m_current++;
if (m_current >= m_numberOfSprites)
Expand Down
6 changes: 5 additions & 1 deletion animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace animation
std::vector<long int> m_animTimes; // Время смены спрайтов
int m_numberOfSprites; // Количество спрайтов в анимации
float m_animSpeed; // Скорость анимации
bool m_pause; // Состояние анимации (на паузе или нет)

void UpdateAnimTimes(); // Обновляет время смены анимации при изменении скорости

Expand Down Expand Up @@ -54,13 +55,16 @@ namespace animation
void SetNumberOfSprites(int value) { m_numberOfSprites = value; }

// Установка скорости анимации
void SetAnimSpeed(float speed) { m_animSpeed = speed; UpdateAnimTimes();}
void SetAnimSpeed(float speed) { m_animSpeed = speed; }

// Возвращает кол-во спрайтов в анимации
const int NumberOfSprites() const { return m_numberOfSprites; }

// Обновить анимацию
void Update(int delta);

// Пауза анимации
void Pause(bool pause) { m_pause = pause; }
};
}

Expand Down
15 changes: 15 additions & 0 deletions imgui.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0

[Window][Sample window]
Pos=60,60
Size=405,127
Collapsed=0

[Window][Animation Controller]
Pos=332,45
Size=324,151
Collapsed=0

48 changes: 28 additions & 20 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
#include <SFML/System/Time.hpp>
#include <SFML/System/Clock.hpp>
#include "animation.h"
#include "imgui.h"
#include "imgui-SFML.h"


int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "AnimationDemo");
ImGui::SFML::Init(window);

sf::Sprite sprite1, sprite2; // Два спрайта для отображения
float speed = 1;

sf::Sprite sprite1;

// Создание анимации 1-ым способом
sf::IntRect rect1(0, 0, 32, 32);
Expand All @@ -18,44 +23,47 @@ int main()
clip1.SetAnimTime(70 * (i+1), i);
clip1.SetAnimSpeed(2);

// Создание анимации 2-ым способом
animation::AnimationClip clip2(&sprite2);
clip2.SetNumberOfSprites(4);
sf::Texture texture;
for (int i = 0; i < 4; i++)
{
sf::IntRect rect2(0 + 32 * i, 32, 32, 32);
texture.loadFromFile("Assets/Sprites/2.png", rect2);
clip2.SetTexture(texture, i);
clip2.SetAnimTime(70 * (i + 1), i);
}

sprite1.setScale(3.0f, 3.0f);
sprite2.setScale(3.0f, 3.0f);

sprite1.setPosition(100, 252);
sprite2.setPosition(604, 252);

sf::Clock deltaClock;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(event);
if (event.type == sf::Event::Closed)
window.close();
}

sf::Time dt = deltaClock.restart();

ImGui::SFML::Update(window, dt);

ImGui::Begin("Animation Controller");

ImGui::Text("Speed of animation");
ImGui::SliderFloat("speed", &speed, 0.0f, 5.0f);
if (ImGui::Button("Set speed"))
clip1.SetAnimSpeed(speed);
if (ImGui::Button("Pause"))
clip1.Pause(true);
if (ImGui::Button("Unpause"))
clip1.Pause(false);
ImGui::End();

clip1.Update(dt.asMilliseconds());

window.clear();
window.draw(sprite1);
window.draw(sprite2);
ImGui::SFML::Render(window);
window.display();

sf::Time dt = deltaClock.restart();
clip1.Update(dt.asMilliseconds());
clip2.Update(dt.asMilliseconds());

}

ImGui::SFML::Shutdown();

return 0;
}

0 comments on commit 64e09b8

Please sign in to comment.