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 - Elektro

Pages: [1]
1
Graphics / Re: Why does this code not show anything?
« on: January 04, 2017, 05:26:37 pm »
Sorry about not replying earlier.

 That was very helpful and made me look up what the new keyword actually does, which is something i'll definitely need to know in the future, thanks!

2
Graphics / Why does this code not show anything?
« on: December 27, 2016, 02:52:15 pm »
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"

int main()
{
        sf::RenderWindow window(sf::VideoMode(512, 512), "SFML testing", sf::Style::Close | sf::Style::Resize);
        Player player = Player();

        while (window.isOpen())
        {
                sf::Event evnt;
                while (window.pollEvent(evnt))
                {
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::TextEntered:
                                if (evnt.text.unicode < 128)
                                {
                                        printf("%c", evnt.text.unicode);
                                }
                        }
                }
                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {
                        sf::Vector2i mousepos = sf::Mouse::getPosition(window);

                        player.setPosition(sf::Vector2f(static_cast<float>(mousepos.x), static_cast<float>(mousepos.y)));
                }

                window.clear();
                player.render(window);
                window.display();
        }
       

        return 0;
}

#pragma once
#include <SFML/Graphics.hpp>

class Player
{
public:
        Player();
        ~Player();
        void setPosition(sf::Vector2f position);
        sf::RectangleShape sprite_;
        void render(sf::RenderWindow &window);

};
 

#include "Player.h"



Player::Player()
{
        sf::RectangleShape sprite_(sf::Vector2f(100.f, 100.f));
        sprite_.setFillColor(sf::Color::Cyan);
        sprite_.setOrigin(sf::Vector2f(50.f, 50.f));
}


Player::~Player()
{
}

void Player::setPosition(sf::Vector2f position)
{
        sprite_.setPosition(position);
}

void Player::render(sf::RenderWindow &window)
{
        window.draw(sprite_);
}

 

Basically i'm trying to create a Player object with a cyan rectangle as a sprite that moves to my mousepos when i press the left mouse button. To show the rectangle in the window, i'm trying to pass my window to the render function in the player object. Unfortunately i just get a black window and nothing happens when i left click it. There are no error messages or anything, it runs fine but doesn't do anything. I'm guessing there is probably something wrong with the constructor and the sprite_ is not initialized correctly.

Now obviously i'm pretty inexperienced with C++ and need to pick up a book to learn the language properly, but i'd really like to solve this now, i'm sure it's a simple fix and it would help me immensely if someone could point me in the right direction.

Pages: [1]