I'm trying to use Creature with SFML.
My animation changes opacity of some meshes (like, flash when shooting), but my code always shows all meshes. So, first question: what I need to do for fix it?
And second question: how I can draw without scaling? (pixelart, so I want it was same size as it drawn in texture).
Code: Select all
#include <SFML/Graphics.hpp>
#include "MeshBone.h"
#include "CreatureModule.h"
void Render(CreatureModule::CreatureManager*);
const float delta = 0.001;
const int k = 32; // zoom factor
const int WindowWidth = 1024;
const int WindowHeight = 768;
const char* file_json = "character_data.json";
const char* file_texture = "character_img.png";
int main()
{
// Load json
CreatureModule::CreatureLoadDataPacket json_data;
CreatureModule::LoadCreatureJSONData(file_json, json_data);
// Load texture
sf::Texture texture;
texture.loadFromFile(file_texture);
texture.setSmooth(true);
sf::Sprite sprite;
sprite.setTexture(texture);
// Create manager
auto cur_creature = std::shared_ptr<CreatureModule::Creature>(new CreatureModule::Creature(json_data));
auto creature_manager = new CreatureModule::CreatureManager(cur_creature);
// Create animations
creature_manager->CreateAnimation(json_data, "Shot");
creature_manager->SetActiveAnimationName("Shot");
// Create window
sf::RenderWindow window(sf::VideoMode(WindowWidth, WindowHeight), "Test");
// Get some info
auto creature = creature_manager->GetCreature();
creature->FillRenderColours(255, 255, 255, 255);
creature_manager->SetIsPlaying(true);
creature_manager->SetShouldLoop(true);
auto ptr_pts = creature->GetRenderPts();
auto ptr_col = creature->GetRenderColours();
auto ptr_uvs = creature->GetGlobalUvs();
auto ptr_ind = creature->GetGlobalIndices();
auto ind_num = creature->GetTotalNumIndices();
auto pts_num = creature->GetTotalNumPoints();
sf::VertexArray parts(sf::Triangles, ind_num);
auto tsize = texture.getSize();
while (window.isOpen())
{
// Input
sf::Event event;
while (window.pollEvent(event))
{
if ((event.type == sf::Event::Closed)
|| (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
window.close();
}
// Update
creature_manager->Update(delta);
// Draw
window.clear(sf::Color::Blue);
window.draw(sprite);
for (int i = 0; i < ind_num / 3; i++)
{
auto idx1 = ptr_ind[i * 3];
auto idx2 = ptr_ind[i * 3 + 1];
auto idx3 = ptr_ind[i * 3 + 2];
auto point_a_x = ptr_pts[idx1 * 3];
auto point_a_y = ptr_pts[idx1 * 3 + 1];
auto point_b_x = ptr_pts[idx2 * 3];
auto point_b_y = ptr_pts[idx2 * 3 + 1];
auto point_c_x = ptr_pts[idx3 * 3];
auto point_c_y = ptr_pts[idx3 * 3 + 1];
auto a_u = ptr_uvs[idx1 * 2];
auto a_v = ptr_uvs[idx1 * 2 + 1];
auto b_u = ptr_uvs[idx2 * 2];
auto b_v = ptr_uvs[idx2 * 2 + 1];
auto c_u = ptr_uvs[idx3 * 2];
auto c_v = ptr_uvs[idx3 * 2 + 1];
glm::float32 pt1_x = point_a_x * k + WindowWidth / 2;
glm::float32 pt1_y = WindowHeight / 2 - point_a_y * k;
parts[i * 3].position = sf::Vector2f(pt1_x, pt1_y);
glm::float32 pt2_x = point_b_x * k + WindowWidth / 2;
glm::float32 pt2_y = WindowHeight / 2 - point_b_y * k;
parts[i * 3 + 1].position = sf::Vector2f(pt2_x, pt2_y);
glm::float32 pt3_x = point_c_x * k + WindowWidth / 2;
glm::float32 pt3_y = WindowHeight / 2 - point_c_y * k;
parts[i * 3 + 2].position = sf::Vector2f(pt3_x, pt3_y);
parts[i * 3].texCoords = sf::Vector2f(a_u * tsize.x, a_v * tsize.y);
parts[i * 3 + 1].texCoords = sf::Vector2f(b_u * tsize.x, b_v * tsize.y);
parts[i * 3 + 2].texCoords = sf::Vector2f(c_u * tsize.x, c_v * tsize.y);
parts[i * 3].color = sf::Color(ptr_col[idx1 * 4], ptr_col[idx1 * 4 + 1], ptr_col[idx1 * 4 + 2], ptr_col[idx1 * 4 + 3]);
parts[i * 3 + 1].color = sf::Color(ptr_col[idx2 * 4], ptr_col[idx2 * 4 + 1], ptr_col[idx2 * 4 + 2], ptr_col[idx2 * 4 + 3]);
parts[i * 3 + 2].color = sf::Color(ptr_col[idx3 * 4], ptr_col[idx3 * 4 + 1], ptr_col[idx3 * 4 + 2], ptr_col[idx3 * 4 + 3]);
}
window.draw(parts, &texture);
window.display();
}
return 0;
}