Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with sprites drawing when resizing RenderTexture in my Game Engine  (Read 5779 times)

0 Members and 2 Guests are viewing this topic.

orella

  • Newbie
  • *
  • Posts: 10
    • View Profile
Hi!
I'm creating a game engine similar to unity but just for 2D games and right now I'm trying to finish the scene view but I'm having problems when resizing.

This is how Unity scene view looks and is what I want to achieve:



And this is what I have right now:



Here is the code used. I'm using ImGui:

Quote
// Function drawing the GameObjects to RenderTexture. window = RenderTexture
bool ModuleSceneWindow::PostUpdate()
{
   window->clear(sf::Color(100, 100, 100, 255));

   for (int j = 0; j < drawableObjects.size(); j++) {
      if (drawableObjects[j]->isActive()) {
         window->draw(*drawableObjects[j]->gameObjectSprite);
      }
   }
   window->display();

   return true;
}

// Function drawing the RenderTexture as the scene view
void PanelScene::DrawPanel()
{
   if (ImGui::BeginDock(panelName.c_str(), ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse)) {
      sf::Vector2u size = ImGui::GetContentRegionAvail();
      ImGui::Image((void*)engine->sceneWindow->window->getTexture().getNativeHandle(), size, ImVec2(0, 1), ImVec2(1, 0), sf::Color::White, sf::Color::Transparent);
   }
}

As you see, the sprite is stretching with the screen.

Any idea to know how to do it?

I know scene in Unity is 3D but uses a 2D view. Should i use openGL to achieve this result? If it's the case, I'll need extra info because I don't know openGL.

If you need more info, tell  me.

Thanks.
« Last Edit: June 09, 2017, 01:29:52 pm by orella »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
You'd have to adjust the view accordingly. Not sure whether his falls into the responsibility of ImGui or if you can change that.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

orella

  • Newbie
  • *
  • Posts: 10
    • View Profile
You'd have to adjust the view accordingly. Not sure whether his falls into the responsibility of ImGui or if you can change that.

I have adjusted the view and this is result:



Looks better but still not the desired result. And now if the window is big the sprite is a bit pixelated:



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
I'm not quite sure, but maybe if you play around with the viewport you can get something similar. It's certainly not something that just works fully automatically, as you can see in the unity gif, it only starts to scale at a certain point and doesn't get any larger after a given point.

As for the pixelation, you may need to ensure that your view and sprite position 1:1 match the pixels on your screen. If they don't fully match, then OpenGL's rasterizer will interpolate partial pixel coloring.
« Last Edit: June 09, 2017, 08:02:38 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

orella

  • Newbie
  • *
  • Posts: 10
    • View Profile
I'm not quite sure, but maybe if you play around with the viewport you can get something similar. It's certainly not something that just works fully automatically, as you can see in the unity gif, it only starts to scale at a certain point and doesn't get any larger after a given point.

As for the pixelation, you may need to ensure that your view and sprite position 1:1 match the pixels on your screen. If they don't fully match, then OpenGL's rasterizer will interpolate partial pixel coloring.

I have managed a similar result changing the view size and using the zoom.

For the pixelation how can I ensure that my view and sprite position 1:1 math the pixels in screen?

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
For the pixelation how can I ensure that my view and sprite position 1:1 math the pixels in screen?

Just set your view to the same size as the actual render area. E.g. if that section of the window is 640x480, set your view to the 640x480.