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

Author Topic: Reference to render window not working.  (Read 929 times)

0 Members and 1 Guest are viewing this topic.

zexanima

  • Newbie
  • *
  • Posts: 3
    • View Profile
Reference to render window not working.
« on: September 07, 2015, 06:30:17 am »
So, after just laying out a bunch of code to test things out I decided to organize some things into a class. I created a class to manage the creation and drawing of the interface (at this stage just a tool bar). I pass a reference of my RenderWindow to that class so it can handle the creation and drawing of the interface all in one place, then I just call userInterface.draw() However, the view or the RectangleShape I setup seem not to be working with the reference of RenderWindow I passed, at least, I'm assuming that's why it's not displaying anything from UserInterface class when I try to call it's draw() function. Why is my UserInterface::draw() function not actually drawing anything to the RenderWindow or displaying the view I made?

UserInterface.h
#include <SFML/Graphics.hpp>

class UserInterface
{

public:
        UserInterface(float window_w, float window_h, sf::RenderWindow &w);
        ~UserInterface();
        void draw();

private:
        void createToolbar();
        sf::View toolbarView;
        sf::RectangleShape toolbarBackground;
        float window_width;
        float window_height;
        sf::RenderWindow& window;
};

UserInterface.cpp
#include "UserInterface.h"


UserInterface::UserInterface(float window_w, float window_h, sf::RenderWindow &w) : window(w), window_width(window_w), window_height(window_h)
{
        createToolbar();
}


UserInterface::~UserInterface()
{
}

void UserInterface::createToolbar()
{
        toolbarView.setSize(window_width, window_height / 10);
        toolbarView.setViewport(sf::FloatRect(0, 0, 1.0f, 0.05f));

        toolbarBackground.setSize(sf::Vector2f(window_width, window_height / 10));
        toolbarBackground.setFillColor(sf::Color(100, 100, 100, 255));
        toolbarBackground.setOutlineThickness(-3);
        toolbarBackground.setOutlineColor(sf::Color(150, 150, 150, 255));
}

void UserInterface::draw()
{
        window.setView(toolbarView);
        window.draw(toolbarBackground);
};
 

main.cpp
#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")  
#endif // SFML_STATIC

#define WINDOW_WIDTH 1024
#define WINDOW_HEIGHT 512
#define TILE_SIZE 32

#include <iostream>
#include <SFML/Graphics.hpp>
#include "Tilemap.h"
#include "UserInterface.h"




int main()
{
        sf::Vector2i levelSize;

        sf::RenderWindow window(sf::VideoMode(1280, 720, 32), "Spellsword Map Editor");

        sf::View mapView(sf::FloatRect(0,0,WINDOW_WIDTH, WINDOW_HEIGHT));
        mapView.setViewport(sf::FloatRect(0,0.05f,1.0f,0.95f));

        UserInterface userInterface(WINDOW_WIDTH, WINDOW_HEIGHT, window);
        const int level[] =
        {//I removed all this for sake of space in my post

        };
        levelSize.x = (sizeof(level)/sizeof(*level))/TILE_SIZE;
        levelSize.y = (sizeof(level)/sizeof(*level))/TILE_SIZE;
        Tilemap map;
        if (!map.load("Textures/tilemap.png", sf::Vector2u(TILE_SIZE, TILE_SIZE), level, levelSize.x, levelSize.y))
                return -1;

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::Down)
                                {
                                        mapView.move(0, 32);
                                        window.setView(mapView);
                                }
                                if (event.key.code == sf::Keyboard::Up)
                                {
                                        mapView.move(0, -32);
                                        window.setView(mapView);
                                }
                                if (event.key.code == sf::Keyboard::Left)
                                {
                                        mapView.move(-32, 0);
                                        window.setView(mapView);
                                }
                                if (event.key.code == sf::Keyboard::Right)
                                {
                                        mapView.move(32, 0);
                                        window.setView(mapView);
                                }
                        }
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();

                window.setView(mapView);
                window.draw(map);

                userInterface.draw();

                window.display();
        }

        return 0;
}

zexanima

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Reference to render window not working.
« Reply #1 on: September 07, 2015, 08:50:53 am »
Durp, I forgot to center the view.  :-X

 

anything