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

Author Topic: Dedicating a class to drawing [SOLVED]  (Read 1670 times)

0 Members and 1 Guest are viewing this topic.

Kiatro

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Dedicating a class to drawing [SOLVED]
« on: December 07, 2013, 12:03:21 am »
I am trying to make a class ("render") that will handle all drawing within my program, so that if I had a sprite I wanted to draw I would send it to a function such as:
void draw(sf::Sprite);

Now so far it is working but I have run into trouble whether or not to declare the main RenderWindow in this class, e.g:
private:
sf::RenderWindow window(sf::VideoMode(800, 800), "A Game That Loves You!");
With this I would only be able to access it within this class but also make an accessory function to access it outside. Now here is my issue:

I have a couple of errors, one of them has this error message:
..\..\..\Assets\SFML-2.1\include\SFML\System\NonCopyable.hpp|67|error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private|

and the other error is:
F:\Programming\C++ SFML\SFML Development\render.cpp|16|note: synthesized method 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' first required here |



This is the overall code I have at the moment:
render.h:
#ifndef RENDER_H
#define RENDER_H
#include <SFML/Graphics.hpp>


class render
{
    public:
        render();
        void draw(sf::Sprite);
        sf::RenderWindow getWindow();
    protected:
    private:

        sf::RenderWindow window;
};

#endif // RENDER_H
 

render.cpp:
#include "render.h"
#include <SFML/Graphics.hpp>
#include <iostream>

render::render()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "A Game That Loves You!");
}

void render::draw(sf::Sprite sprite)
{
    //empty!
}

sf::RenderWindow render::getWindow()
{
    return window;
}
 



If anyone could explain or give a solution to these errors I would be very grateful, thanks. (:
« Last Edit: December 07, 2013, 01:02:36 pm by Kiatro »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Dedicating a class to drawing
« Reply #1 on: December 07, 2013, 05:34:10 am »
Pass by reference instead of value  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Kiatro

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Dedicating a class to drawing
« Reply #2 on: December 07, 2013, 01:02:15 pm »
Wow, thanks - didn't think it was such a silly mistake!  :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Dedicating a class to drawing [SOLVED]
« Reply #3 on: December 07, 2013, 03:03:36 pm »
Another mistake lies here:
render::render()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "A Game That Loves You!");
}

You're declaring a local variable in the constructor. It won't outlive the function call, so it's completely useless. What you should do instead is using the constructor initializer list to initialize member variables:
render::render()
: window(sf::VideoMode(800, 800), "A Game That Loves You!")
{
}

By the way, "render" (as a verb) is not an ideal name for a class, consider "renderer". And you should think about encapsulation, returning the internal window is a rather bad idea.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything