I will try to keep this brief
My basic C++ (SDL & OpenGL) 2D game engine has an ECS. I have a system called TextSystem, which requires 3 components. TransformComponent, RenderComponent, & TextComponent.
This is a code snippet of the signatures being set for the systems.
ECSHandler::Instance()->Init();
//Register components.
ECSHandler::Instance()->RegisterComponent<TransformComponent>();
ECSHandler::Instance()->RegisterComponent<RenderComponent>();
ECSHandler::Instance()->RegisterComponent<TextComponent>();
//movement signature here
renderSignature.set(ECSHandler::Instance()->GetComponentType<TransformComponent>());
renderSignature.set(ECSHandler::Instance()->GetComponentType<RenderComponent>());
textSignature.set(ECSHandler::Instance()->GetComponentType<TransformComponent>());
textSignature.set(ECSHandler::Instance()->GetComponentType<RenderComponent>());
textSignature.set(ECSHandler::Instance()->GetComponentType<TextComponent>());
//Register systems.
//mpMovementSystem = ECSHandler::Instance()->RegisterSystem<MovementSystem>();
mpRenderSystem = ECSHandler::Instance()->RegisterSystem<RenderSystem>();
mpTextSystem = ECSHandler::Instance()->RegisterSystem<TextSystem>();
//ECSHandler::Instance()->SetSystemSignature<MovementSystem>(signature);
ECSHandler::Instance()->SetSystemSignature<RenderSystem>(renderSignature);
ECSHandler::Instance()->SetSystemSignature<TextSystem>(textSignature);
//mpSystems.insert({ typeid(MovementSystem).name(), mpMovementSystem });
mpSystems.insert({ typeid(RenderSystem).name(), mpRenderSystem });
mpSystems.insert({ typeid(TextSystem).name(), mpTextSystem });
All the TextSystem does is, sets the "texture" variable in the RenderComponent to a texture made from text/font. This is done using a function called LoadTextureFromFont.
Another system which handles rendering, the RenderingSystem, automatically has the entity added if it is already registered to a TextSystem as the RenderingSystem has all the components that a TextSystem needs, the transform and render component.
So, the TextSystem changes the texture of an entity to update it according to the text input (the "output" variable in the TextComponent) and the RenderSystem renders that entity.
void TextSystem::Update()
{
for (auto const& entity : mEntities)
{
auto& render = ECSHandler::Instance()->GetComponent<RenderComponent>(entity);
auto& text = ECSHandler::Instance()->GetComponent<TextComponent>(entity);
render.texture = ResourceManager::LoadTextureFromFont(text.output, true, text.font);
}
}
void RenderSystem::Draw()
{
for (auto const& entity : mEntities)
{
auto& transform = ECSHandler::Instance()->GetComponent<TransformComponent>(entity);
auto& render = ECSHandler::Instance()->GetComponent<RenderComponent>(entity);
...
glBindVertexArray(mQuadVAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
}
}
TextSystem's Update is called before RenderingSystem's Draw.
When re-assigning the texture variable in the render component for an entity registered in TextSystem, the latest texture that is applied (via LoadTextureFromFont) is applied to ALL entities that have a TextComponent. Essentially, the last TextComponent becomes the TextComponent for all entities that have said component. The size of the texture is made according to the original text's size, so texts that are long but have been replaced by the newest text will look dis-proportionate.
ResourceManager::AddText("testText", "Message", glm::vec2(200.f, 200.f), ResourceManager::GetFont("CircularMedium"));
ResourceManager::AddText("testText2", "This is a message.", glm::vec2(300.f, 200.f), ResourceManager::GetFont("CircularMedium"));
This is not happening with any other component, strictly this component (which holds only a string and font object) is seemingly not entity-exclusive.
I've tried all I could to fix this (10-12 hours...) and Googling did not yield desired results.
If anything at all is needed (more code snippets and/or context) please let me know! I am not sure if this question suits Stack Overflow or Game Development.
The "Texture" variable being returned from GetTextureFromFont was a global variable.