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

Author Topic: Rendering objects from different classes to one.  (Read 2047 times)

0 Members and 1 Guest are viewing this topic.

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Rendering objects from different classes to one.
« on: April 28, 2017, 10:00:34 pm »
Hi Guys. i got this problem with with drawing drawables from classes in main class, it does not draw anything accept the window and it's Background Color.

[ Tank.cpp ]

void Tank::Render()
{
        t_window.Draw(t_tankSprite);
        HandleInput();
}

[window.cpp]

void window::Create()
{
        t_window.create({ t_windowSize.x, t_windowSize.y, 32 }, t_windowTitle, Style::Fullscreen);
}

void window::BeginDraw()
{
        t_window.clear(Color(158, 154, 117, 255));
}

void window::EndDraw()
{
        t_window.display();
}

void window::Update()
{
        Event t_event;
        if (t_window.pollEvent(t_event))
        {
                if (t_event.type == Event::Closed || (t_event.type == Event::KeyPressed && t_event.key.code == Keyboard::Escape))
                        t_IsDone = true;
        }

}
 

[ Main.cpp ]

#include <Windows.h>
#include "Tank.h"

#ifndef window_h
#define window_h
#include "window.h"
#endif


using namespace sf;


int CALLBACK WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow)
{
        window win;
        win.Setup("Tank Game", Vector2u(1024, 786));
        win.Create();

        Tank tank;

        while (!win.IsDone())
        {
                win.BeginDraw();
                win.Update();

                tank.Render();

                win.EndDraw();
        }
        return 0;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Rendering objects from different classes to one.
« Reply #1 on: April 28, 2017, 10:35:21 pm »
Please use the Help sub foruns for help questions. :)

How do you load the texture for your sprite and how do you pass said texture to your sprite?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Rendering objects from different classes to one.
« Reply #2 on: April 28, 2017, 10:52:14 pm »
For loading texture.

void Tank::setSprites()
{
        t_bulletTexture.loadFromFile("bullet.png");
        t_tankTexture.loadFromFile("tank.png");

        t_tankSprite.setTexture(t_tankTexture);

        t_tankSprite.setPosition(t_window.GetWindowSize().x / 2, t_window.GetWindowSize().y / 2);
        t_tankSprite.setOrigin(t_tankTexture.getSize().x / 2, t_tankTexture.getSize().y / 2);
        t_tankSprite.setRotation(360);
}
 

UroboroStudio

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Rendering objects from different classes to one.
« Reply #3 on: April 28, 2017, 11:12:12 pm »
The class Tank has a "t_window" but you don't pass it to the tank in the "main.cpp".

I suggest this way:

Tank tank;
...
Tank.Render(&win);

And change Render function to:
void Tank::Render(window *win)
{
        win->Draw(t_tankSprite);
        HandleInput();
}