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.


Topics - Mooks

Pages: [1]
1
General / [SOLVED]class variables reset value after function loop
« on: June 26, 2017, 12:01:40 am »
SOLUTION: It seems I was being really rigid in my idea of how to declare and define the objects I was working with. I was declaring and defining the objects generally in the same scope. The solution I found was to declare the objects outside the scope of the function that I wanted to change the objects' value and then make calls to that objects member functions (thus defining them, I think) inside the function in question. I hope that explains it.

What it looks like:
overworldTime() previously looked like this:

 

class gameTime {

void overworldTime() {
        overworldLocations cityA;   //objects are declared //
        overworldLocations cityB;
        cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
        // ..and objects are defined right away //
// do stuff //

}; // end gameTime
 

now is looks like this:


overworldLocations cityA;   //objects are declared outside of gameTime //
overworldLocations cityB;

class gameTime {

void overworldTime() {
       
        cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
        // ..and they are still defined in the same place //

// do stuff //

}; // end gameTime
 

/edit


This is more of a general C++ question I believe, but because I'm using SFML to work on this project I figured I'd ask around for help here before going elsewhere.

I have a set of classes and I'd like for their variables to be shared. Easy enough, however I have a function within one of the classes that is meant to incriment the value of the shared variable. In my logger I note that the incriment occurs once and then reverts to the initial value, and this process just repeats itself.

The goal is to get cityFood which is defined in class overworldLocations to be incrimented in the loop in overworldTime() which is a function of class gameTime.

Here are the two classes ( I'm sure there are errors everywhere, but everthing works aside from this one problem) overworldLocations:

#include "text_function.hpp"
#include <memory>

unsigned int cityPosX{(unsigned int)windowSizeX/3}, cityPosY{(unsigned int)windowSizeY/3};
sf::Font font, cityFont;

class overworldLocations {
       
       

public:
        static int cityFood, cityIndustry, cityPopulation;
        friend class gameTime;
       
int resourceSetter(int a, int b, int c) {

        cityFood = a;
        cityIndustry = b;
        cityPopulation = c;
       
}

int resourceGetter() {

        return cityFood, cityIndustry, cityPopulation;
}

void overworldCity(sf::Vector2f cityPos, std::string cityName) {

        float citySizeX{100}, citySizeY{100};

        sf::RectangleShape cityShape(sf::Vector2f(citySizeX, citySizeY));
        cityShape.setPosition(cityPos);
        window.draw(cityShape);
       
        sf::Vector2f cityPosition{cityShape.getPosition()};

        float x = cityPosition.x;
        float y = cityPosition.y;

        sf::Vector2f cityPosVector{cityPosition.x + 25, cityPosition.y + 80};
        // *foodPointer = cityFood;

       
       
        std::string foodString = std::to_string(cityFood), industryString = std::to_string(cityIndustry), populationString = std::to_string(cityPopulation);

        text_function textObject(cityName, cityFont, cityPosVector, sf::Color::Black, 10);

        text_function cityTextObject1("F", cityFont, {cityPosition.x, cityPosition.y - 15}, sf::Color::Green, 12);
        text_function cityResourceText1(foodString, cityFont, {cityPosition.x + 7, cityPosition.y - 15}, sf::Color::Green, 12);

        text_function cityTextObject2("I", cityFont, {cityPosition.x + 40, cityPosition.y - 15}, sf::Color::Red, 12);
        text_function cityResourceText2(industryString, cityFont, {cityPosition.x + 47, cityPosition.y - 15}, sf::Color::Red, 12);

        text_function cityTextObject3("P", cityFont, {cityPosition.x + 80, cityPosition.y - 15}, sf::Color::Yellow, 12);
        text_function cityResourceText3(populationString, cityFont, {cityPosition.x + 87, cityPosition.y - 15}, sf::Color::Yellow, 12);
       
};
};

 int overworldLocations::cityFood{10}, overworldLocations::cityIndustry{10}, overworldLocations::cityPopulation{10};

 

 and gameTime:
#include "overworldLocations.hpp"
#include "drawable_objects.hpp"

sf::Font overworldFont;
bool timePaused = true;


sf::Clock overworldClock;
sf::Text overworldText, overworldCurrentDay;
float overworldObjectPosX{(float)windowSizeX/windowSizeX}, overworldObjectPosY{(float)windowSizeY - 30};
sf::Vector2f overworldCurrentDayObjectPos{(float)windowSizeX/windowSizeX + 50, (float)windowSizeY - 30};

class gameTime : public overworldLocations {

public:

static int currentDay;

friend class overworldLocations;

void overworldTime(int& reference) {

        sf::Time wholeDay = sf::seconds(1.0f);
        sf::Time timePassed = overworldClock.getElapsedTime();
       
        if(timePassed > wholeDay) {

                std::cout
                        << "OVERWORLD TIME "
                        << timePassed.asSeconds()
                        << std::endl;

                overworldClock.restart();

                if(!timePaused) {

                        if(currentDay >= 7) {
                                currentDay = 0;
                                overworldLocations::cityFood += 1;
                        std::cout
                                << "OL CF is "
                                << overworldLocations::cityFood
                                << std::endl;
                        std::cout
                                << "GT CF is "
                                << cityFood
                                << std::endl;
                        std::cout
                                << "reference to cityFood is "
                                << reference
                                << std::endl;
                        }


                        // std::cout
                        //      << "shared_ptr & is "
                        //      << &sharedfoodPointer
                        //      << std::endl;
                        // std::cout
                        //      << "shared_ptr * is "
                        //      << *sharedfoodPointer
                        //      << std::endl;
                        // std::cout
                        //      << "shared_ptr is "
                        //      << sharedfoodPointer
                        //      << std::endl;

                        }

                         
                        currentDay ++;
                        std::cout
                                << "Day "
                                << currentDay
                                << std::endl;

        }
       
overworldLocations::cityFood += 1;
        std::string currentDayString = std::to_string(currentDay);

        text_function overworldTextObject("Day:", overworldFont, {overworldObjectPosX, overworldObjectPosY}, sf::Color::Red, 24);
        text_function overworldTextCurrentDay(currentDayString, overworldFont, overworldCurrentDayObjectPos, sf::Color::Red, 24);

};


};

int gameTime::currentDay{1};

 

and the main.cpp:
#include <SFML/System.hpp>
#include <SFML/WIndow.hpp>
#include <SFML/Graphics.hpp>
#include "include/window.hpp"
#include <iostream>
#include <thread>
#include <string>
#include "include/eventPoll.hpp"


int main() {

if (!cityFont.loadFromFile("impact.ttf")) {

                std::cout
                        << "error loading font";

                }
if (!overworldFont.loadFromFile("impact.ttf")) {

                std::cout
                        << "error loading font";

                }
if (!font.loadFromFile("impact.ttf")) {

                std::cout
                        << "error loading font";

                }
 
   
        while(true) {

                window.clear(black);

                gameTime gameClock;
                gameClock.overworldTime(overworldLocations::cityFood);

                character();

                drawable_objects();

               

                eventPoll();

                window.display();
               
                if(isClosed == true) {

                        break;
                }
       
        }

        return 0;

} //main()  

 

There is more I can put here if its neccesary but I believe my problem lies within these bits of code.

I can achieve the desired effect by making cityFood global - but alot of information suggests that is bad practice, and theres obviously something fundemental I'm missing so I'd like to learn how to get the results I want without using globals. The last thing I tried was passing by reference, as you can see in the overworldTime() function but I did not notice any change.

Any help would be appreciated, I hope I provided enough information.

EDIT: I forgot to add the code where I make instances of the overworldLocations class. I have those objects in a function called drawable_objects which is called in main :
void drawable_objects() {

        overworldLocations cityA, cityB;
        cityA.resourceSetter(10,10,10);
        cityB.resourceSetter(10,10,10);
    cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");

}

 

2
Graphics / "Undefined reference to sf::..."
« on: April 19, 2017, 06:41:15 pm »
EDIT: I've managed to get the 64 bit compiler for my configuration working properly! Details in the posts below  :D


Hi all

Apologies in advance if this question seems a bit simple.  I wouldn't resort to forums unless I felt that I exhausted the abilities of my google-ing. I made an account just to ask about these errors and I really try to avoid doing this because google has usually gotten me through any problems I have with programming.
(Also sorry if my forum etiquette is lacking)

Yesterday I installed SFML for the first time and attempted to use MinGW to compile the tutorial example from the official SFML documentation(the example specifically for MinGW and CodeBlocks):

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

However I consistently return these errors:

C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x7c): undefined reference to `sf::String::String(char const*, std::locale const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0xa0): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0xda): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x10f): undefined reference to `sf::CircleShape::CircleShape(float, unsigned int)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x11f): undefined reference to `sf::Color::Green'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x126): undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x136): undefined reference to `sf::Window::isOpen() const'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x154): undefined reference to `sf::Window::pollEvent(sf::Event&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x172): undefined reference to `sf::Window::close()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x19d): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1b6): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1ca): undefined reference to `sf::RenderStates::Default'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1da): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1ea): undefined reference to `sf::Window::display()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x20e): undefined reference to `sf::RenderWindow::~RenderWindow()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x256): undefined reference to `sf::RenderWindow::~RenderWindow()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0xa): undefined reference to `vtable for sf::CircleShape'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x14): undefined reference to `vtable for sf::CircleShape'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x24): undefined reference to `sf::Shape::~Shape()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.4s with exit code 1]

I am using this to attempt to compile (in Sublime Text 3):

{
    "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-DSFML_STATIC", "-LC:/Coding/mingw32/mingw32/lib", "-IC:/Coding/mingw32/mingw32/include", "-lsfml-window", "-lsfml-graphics", "-lsfml-system", "-static-libgcc", "-static-libstdc++", "${file}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.cpp, source.c++",
    "path": "C:/Coding/mingw32/mingw32/bin",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-static-libgcc", "-static-libstdc++", "*.cpp", "&", "${file_path}/${file_base_name}.exe"]
        }
    ]
}

This build was something I found while googling about the setup of SFML with MinGW. I've tailored it to suit my setup and outside of any attempts to create links to SFML libraries I have not had any issues compiling any other code.

I tried a few different installations of both SFML and MinGW both in 32 and 64bit. I experimented for several hours attempting to configure this via CMake using both the downloads available from the SFML site and the Git source with various installs of MinGW as well, to no avail. These attempts with CMake usually gave me even more errors on various different configurations and setups than without it.

I've tried various things with the linking order and commands in the build path as googling this issue pretty much suggested that I either was using incompatible 32 and 64bit versions of libraries and such or I had incorrect link orders. Ive tried the different 32 and 64 bit  variations with and without CMake, nothing really seems to yield any kind of progress. I tried linking every single /extlib dependency (despite the tutorial explaining it wasn't necessary for Windows users)including any necessary libraries from the Windows SDK 8.0 just to see if that was my problem and nothing changed.

My current setup is using the SFML 2.4.2 GCC 6.1.0 MinGW (DW2) - 32-bit from the SFML site with the MinGW version that is linked below it.  The PATH to the MinGW bin is set accordingly.
What I've done at the moment is simply copied the "SFML/include" and SFML's "/lib" to the MinGW installation and they appear to still be valid within the compilation but I cant build any code using actual SFML objects and classes etc.

I still consider myself a beginner at coding so I assume its very likely there's some blatant oversight that's beyond me that letting some more people look at for a few minutes would make apparent so any help will be greatly appreciated!
I know this question has been asked plenty of times before so I apologize if this seems like an unnecessary post but I'm clueless here.

Pages: [1]
anything