#pass class object into function argument

38 messages · Page 1 of 1 (latest)

stuck shoal
#
#include "stdafx.h"
#include "Game.h"
#include "Deck.h"
#include "Hand.h"
#include "Text.h"

#include <iostream>
#include <ctime>

//Privates
void Game::initVariables()
{
    this->window = nullptr;

    Deck basicDeck;
    Hand playersHand;
    Hand dealersHand;
    TextObject startText(consolasFont, this->window, 16, L"Start");

}

void Game::initWindow()
{
    this->videoMode.width = 800;
    this->videoMode.height = 600;
    this->window = new sf::RenderWindow(this->videoMode, "Blackjack in C++", sf::Style::Titlebar | sf::Style::Close);
    this->window->setFramerateLimit(75);
}

void Game::initFonts()
{
    if (!this->consolasFont.loadFromFile("fonts/Consolas.ttf"))
    {
        sf::err() << "ERROR::GAME::INITFONTS:: failed to load font! \n";
    }
}

void Game::initText()
{
}

void Game::initButtons()
{
    /*
    this->button.setFillColor(sf::Color(255, 255, 255));
    this->button.setCharacterSize(16);
    this->button.setFont(this->consolasFont);
    this->button.setPosition((this->window->getSize().x - 20.f), (this->window->getSize().y - 20.f));
    */
}



//Constructors
Game::Game()
{
    this->initVariables();
    this->initWindow();
    this->initFonts();
    this->initText();
}

Game::~Game()
{
    delete this->window;
}

//Accessors 

const bool Game::running() const
{
    return this->window->isOpen();
}

//Functions


void Game::pollEvents()
{
    //Event polling
    while (this->window->pollEvent(this->ev))
    {
        switch (this->ev.type)
        {
        case sf::Event::Closed:
            this->window->close();
            break;
        case sf::Event::KeyPressed:
            if (this->ev.key.code == sf::Keyboard::Escape)
                this->window->close();
            break;
        }
    }
}

void Game::updateMousePos()
{
    this->mousePosWindow = sf::Mouse::getPosition(*this->window);
    this->mousePosView = this->window->mapPixelToCoords(this->mousePosWindow);
}



void Game::update()
{

    this->pollEvents();

    this->updateMousePos();

    this->updateText();
}

void Game::updateText()
{
    
}


void Game::renderText(sf::RenderTarget& target)
{
    target.draw();
}

void Game::render()
{
    this->window->clear(sf::Color(20, 20, 20));

    this->renderText(*this->window);

    //LAST
    this->window->display();
}```



in Game.cpp, in `initVariables()` i create `TextObject startText` how do i pass it as an argument in `renderText` in `target.draw();`?
jovial oarBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

median shuttle
#

can't you change your renderText function to take the text as argument?

stuck shoal
#

okay that could work but i have to fix something else

#
TextObject::TextObject(sf::Font font, sf::RenderWindow* window, int characterSize, std::wstring caption)
{
    stringStream << caption;
    this->uiText.setString(stringStream.str());
    this->uiText.setCharacterSize(characterSize);
    this->uiText.setFont(font);
    centerTextOrigin();
    centerTextPosition(window);


}

void TextObject::centerTextOrigin()
{
    this->uiText.setOrigin(static_cast<int>(this->uiText.getLocalBounds().getSize().x / 2.f + this->uiText.getLocalBounds().getPosition().x),
        static_cast<int> (this->uiText.getLocalBounds().getSize().y / 2.f + this->uiText.getLocalBounds().getPosition().y));
}

void TextObject::centerTextPosition(sf::RenderWindow *window)
{
    this->uiText.setPosition(static_cast<int> (window->getSize().x / 2.f), static_cast<int>(window->getSize().y / 2.f));
}

the window is nullptr

#
#include "stdafx.h"
#pragma once

class TextObject
{
public:
    sf::Text uiText;
    std::wstringstream stringStream;

    TextObject(sf::Font font, sf::RenderWindow* window, int characterSize = 12, std::wstring caption = L"");

    void centerTextOrigin();
    void centerTextPosition(sf::RenderWindow* window);
private:

};```
median shuttle
#

yeah you need to initialize window with an actual window before you can draw anything

#

I'm honestly not sure what initVariables is supposed to do

#

because all local variables of that function are going to be destroyed when you leave the function

stuck shoal
#
void Game::initVariables()
{
    this->window = nullptr;

    Deck basicDeck;
    Hand playersHand;
    Hand dealersHand;
    TextObject startText(consolasFont, this->window, 16, L"Start");

}
#

@median shuttle

median shuttle
#

yeah but what's the purpose of this function?

#

like, I get that you want to have a Deck object, and Hands and so on

#

But they should either be member variables of Game, or you should be creating them as local variables in a function where you actually use them

stuck shoal
#
#include "stdafx.h"
#include "Game.h"
#include "Deck.h"
#include "Hand.h"
#include "Text.h"

#include <iostream>
#include <ctime>

//Privates
void Game::initVariables()
{
    this->window = nullptr;

    Deck basicDeck;
    Hand playersHand;
    Hand dealersHand;
    TextObject startText(consolasFont, this->window, 16, L"Start");

}

void Game::initWindow()
{
    this->videoMode.width = 800;
    this->videoMode.height = 600;
    this->window = new sf::RenderWindow(this->videoMode, "Blackjack in C++", sf::Style::Titlebar | sf::Style::Close);
    this->window->setFramerateLimit(75);
}

void Game::initFonts()
{
    if (!this->consolasFont.loadFromFile("fonts/Consolas.ttf"))
    {
        sf::err() << "ERROR::GAME::INITFONTS:: failed to load font! \n";
    }
}

void Game::initText()
{
}

void Game::initButtons()
{
    /*
    this->button.setFillColor(sf::Color(255, 255, 255));
    this->button.setCharacterSize(16);
    this->button.setFont(this->consolasFont);
    this->button.setPosition((this->window->getSize().x - 20.f), (this->window->getSize().y - 20.f));
    */
}



//Constructors
Game::Game()
{
    this->initVariables();
    this->initWindow();
    this->initFonts();
    this->initText();
}

Game::~Game()
{
    delete this->window;
}

//Accessors 

const bool Game::running() const
{
    return this->window->isOpen();
}

//Functions


void Game::pollEvents()
{
    //Event polling
    while (this->window->pollEvent(this->ev))
    {
        switch (this->ev.type)
        {
        case sf::Event::Closed:
            this->window->close();
            break;
        case sf::Event::KeyPressed:
            if (this->ev.key.code == sf::Keyboard::Escape)
                this->window->close();
            break;
        }
    }
}

void Game::updateMousePos()
{
    this->mousePosWindow = sf::Mouse::getPosition(*this->window);
    this->mousePosView = this->window->mapPixelToCoords(this->mousePosWindow);
}



void Game::update()
{

    this->pollEvents();

    this->updateMousePos();

    this->updateText();
}

void Game::updateText()
{
    
}


void Game::renderText(sf::RenderTarget& target, sf::Text text)
{
    target.draw(text);
}

void Game::render()
{
    this->window->clear(sf::Color(20, 20, 20));

    this->renderText(*this->window, uiText);

    //LAST
    this->window->display();
}
#

i guess the whole thing would be more useful then

#

I'm in the process of moving Text based stuff to it's own class

#

@median shuttle

median shuttle
#

I'm not really sure what you're trying to say, to be honest

stuck shoal
#

hmm

#

before i would declare sf::Text uiText in the 'Game.h' file

#

and that was really simple

#

everything was handled within the game itself

#

but i'm working on making text objects their own thing

#

so that i can easily and quickly define however many textObjects i want

median shuttle
#

ok

#

but what is your question?

stuck shoal
#

well you solved my original issue with the parameters

median shuttle
#

oh nice

stuck shoal
#

now i get an error from CenterTextPosition() not recognizing what 'window' is

median shuttle
#

you mean it doesn't know what a sf::RenderWindow is?

#

that means you're missing includes

stuck shoal
#

no no the includes are fine

#

i just keep mixing up referencing derefrencing and pointers

median shuttle
#

ah gotcha

#

that's just something that comes with practice I guess

stuck shoal
#

i guess i'll just try it myself

#

!solved