SFML Torturing Me
unknown
c_cpp
2 years ago
2.5 kB
9
Indexable
animation.cpp
#include "animation.h"
animation::animation(sf::Texture* texture, sf::Vector2u imageCount, float switchTime)
{
this->imageCount = imageCount;
this->switchTime = switchTime;
totalTime = 0.0f;
currentImage.x = 0;
uvRect.width = texture->getSize().x/ float(imageCount.x);
uvRect.height= texture->getSize().y/ float(imageCount.y);
}
animation::~animation()
{
}
void animation::Update(int row, float deltaTime)
{
currentImage.y = row;
totalTime >= deltaTime;
if (totalTime >= switchTime)
{
totalTime == switchTime;
currentImage.x++;
if (currentImage.x >= imageCount.x)
{
currentImage.x = 0;
}
}
uvRect.left = currentImage.x * uvRect.width;
uvRect.top = currentImage.y * uvRect.height;
}
animation.h
#pragma once
#include <SFML\Graphics.hpp>
class animation
{
public:
animation(sf::Texture* texture, sf::Vector2u imageCount, float switchTime);
~animation();
void Update(int row, float deltaTime);
public:
sf::IntRect uvRect;
private:
sf::Vector2u imageCount;
sf::Vector2u currentImage;
float totalTime;
float switchTime;
}
main.cpp
#include <SFML\Graphics.hpp>
#include <iostream>
#include "animation.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Galaxy Crashers", sf::Style::Close | sf::Style::Resize | sf::Style::Titlebar);
sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
sf::Texture playerTexture;
playerTexture.loadFromFile("player.png");
player.setTexture(&playerTexture);
animation animation(&playerTexture, sf::Vector2u(2, 3), 0.6f);
float deltaTime = 0.0f;
sf::Clock clock;
sf::Vector2u textureSize = playerTexture.getSize();
textureSize.x /= 2;
textureSize.y /= 3;
player.setTextureRect(sf::IntRect(textureSize.x * 2, textureSize.y * 3, textureSize.x, textureSize.y));
while (window.isOpen())
{
deltaTime = clock.restart().asSeconds();
sf::Event evnt;
while (window.pollEvent(evnt))
{
switch (evnt.type)
{
case sf::Event::Closed:
window.close();
break;
}
if (evnt.type == evnt.Closed)
{
window.close();
}
}
animation.Update(0, deltaTime);
player.setTextureRect(animation.uvRect);
window.clear( sf::Color (150,150,150));
window.draw(player);
window.display();
}
return 0;
}Editor is loading...
Leave a Comment