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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - redding

Pages: [1]
1
Graphics / Re: Problem with drawing properties of objects in arrays
« on: May 05, 2015, 07:36:35 pm »
Ok, thank you for the advice - I'll be sure to do so.

2
Graphics / Re: Problem with drawing properties of objects in arrays
« on: May 05, 2015, 06:20:43 pm »
Thank you very much for the quick reply! This explains a ton of the questions I've been having about c++ in general. I replaced that line with

buttonArr[0] = *new Button(texture, sf::Vector2f(0, 0));

and everything is working! Thanks!

3
Graphics / Problem with drawing properties of objects in arrays
« on: May 05, 2015, 04:34:26 pm »
I have created a button class that just contains a texture and sprite. However, when I create an array of these Buttons, window.draw(buttons[0].sprite) doesn't draw the sprite.

Here is the class "Button":


#include <stdio.h>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"

class Button {
public:
    Button();
    Button(sf::Texture myTexture, sf::Vector2f pos);
    sf::Sprite sprite;
    sf::Texture texture;
};

...

#include "Button.h"

Button::Button() {
}

Button::Button(sf::Texture myTexture, sf::Vector2f pos) {
    texture = myTexture;
    if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
        return EXIT_FAILURE;
    }
    sprite = sf::Sprite(texture);
    sprite.setPosition(pos);
}

 

An here is my main():

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"
#include "Button.h"

int main(int, char const**)
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
   
    sf::Texture texture;
    if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
        return EXIT_FAILURE;
    }
   
    Button *buttonArr = new Button[5];
    buttonArr[0] = Button(texture, sf::Vector2f(0, 0));
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
        window.clear();
        window.draw(buttonArr[0].sprite);
        window.display();
    }
    return EXIT_SUCCESS;
}
 

A window opens, but the screen remains white. Interestingly, if I keep everything the same, but make buttons a single button rather than an array, everything works fine.

Any help here would be much appreciated. :)

Pages: [1]