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

Author Topic: Access violation on certain attributes of sf::Text [C++]  (Read 1716 times)

0 Members and 1 Guest are viewing this topic.

MrSnappingTurtle

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Access violation on certain attributes of sf::Text [C++]
« on: December 24, 2014, 03:30:46 am »
Hey,

So this time hopefully my issue isn't as stupid as me adding an extra semi-colon, but when I try to get the global bounds of an sf::Text object, I get an access violation:

First-chance exception at 0x0FD2E41F (sfml-graphics-d-2.dll) in Survive.exe: 0xC0000005: Access violation reading location 0xCCCCCD24.

The violation happens in the if statement when I use the .contains method(). I know it happens because of the .getGlobalBounds() method because if I try std::cout'ing that value's width, height, etc, it will crash there instead.

Here are my .h and .cpp files. The relevent .h portion is the  //------Menu---- and //=====Main Screen==== section. As for the .cpp, the menu constructor is the one that loads. As an added note, even though I change the character size, Visual Studio is reporting it as '0' in the debug window at the time of the crash. Thanks!

.h

#ifndef GUIMANAGER_H
#define GUIMANAGER_H
#include <SFML/Graphics.hpp>
#include "Player.h"
class GUIManager
{
public:
        GUIManager(sf::RenderWindow*, Player*);
        GUIManager(sf::RenderWindow*);
        GUIManager();
        void update(const sf::Time&);

        enum menuType
        {
                main,
                settings,
                gameplay,
                audio,
                controls
        };

        //Getters
        sf::RectangleShape getHealthOutOf() const;
        sf::RectangleShape getHealthCurrent() const;
        sf::RectangleShape getReloadOutOf() const;
        sf::RectangleShape getReloadCurrent() const;
        sf::Text getTotalAmmo() const;
        bool isMenu() const;
        menuType getCurrentMenu() const;
        sf::RectangleShape getSelectionRect() const;
        sf::Text getSurviveTitleText() const;
        sf::Text getPlayText() const;
        sf::Text getSettingsText() const;
        sf::Text getQuitText() const;

       


private:
        sf::RenderWindow* pWindow_;
        sf::Font font_;
        bool menu_;
       

        //-----------------------------------MENU------------------------------------
        //The type of menu that is currently visible
        menuType currentMenu_ = main;
        //Rectangle that appears below selected button/text
        sf::RectangleShape selectionRect_;
        //Text that is shown at the bottom to describe what the setting does
        sf::Text descriptionText_;
        //The sizes of selectable buttons and title texts in percent
        int titleSize_ = 0.3f;
        int buttonSize_ = 0.1f;

        float titleOffset_ = 0.15f;
        //==================================Main Screen===================================

        //Text
        sf::Text surviveTitleText_;
        sf::Text playText_;
        sf::Text settingsText_;
        sf::Text quitText_;

        //====================================Settings====================================

        sf::Text settingsTitleText_;
        sf::Text audioText_;
        sf::Text gameplayText_;
        sf::Text fullscreenText_;

        //====================================GAMEPLAY====================================
        sf::Text gameplayTitleText_;
        sf::Text mapSizeText_;
        sf::Text difficultyMultiplierText_;



        //--------------------------------------Game--------------------------------------
        Player* pPlayer_;

        //Health
        sf::RectangleShape healthOutOf_;
        sf::RectangleShape healthCurrent_;
        sf::Vector2f healthOffset_ = sf::Vector2f(0.01f, 0.01f); //In percent of window size
        sf::Vector2f healthSize_ = sf::Vector2f(0.075f, 0.01f); //In percent of window size

        //Weapon
        sf::Text ammo_;
        sf::Vector2f ammoOffset_ = sf::Vector2f(0.015f, 0.015f);
        float ammoSize_ = 0.035f; //In percent

        //Reload
        sf::RectangleShape reloadOutOf_;
        sf::RectangleShape reloadCurrent_;
        sf::Vector2f reloadOffset_ = sf::Vector2f(0.01f, 0.01f);
        sf::Vector2f reloadSize_ = sf::Vector2f(0.075f, 0.01f);

};

#endif 

.cpp

#include "GUIManager.h"
#include <iostream>
#include "Humanoid.h"
#include "Player.h"
#include "Gun.h"

//HUD constructor
GUIManager::GUIManager(sf::RenderWindow* pWindow, Player* pPlayer)
        :pWindow_(pWindow), pPlayer_(pPlayer)
{


        if (!font_.loadFromFile("arial.ttf"))
                std::cout << "Failed to load font." << std::endl;

        //Health
        healthOutOf_.setFillColor(sf::Color(214, 34, 34));
        healthCurrent_.setFillColor(sf::Color(78, 213, 116));
        healthOutOf_.setSize(sf::Vector2f(3.0f, 3.0f));
        healthCurrent_.setSize(sf::Vector2f(3.0f, 3.0f));
        healthOutOf_.setOrigin(0.0f, 3.0f);
        healthCurrent_.setOrigin(0.0f, 3.0f);

        //Ammo
        ammo_.setFont(font_);
        ammo_.setColor(sf::Color::Black);

        //Reload
        reloadOutOf_.setFillColor(sf::Color(23, 24, 25));
        reloadCurrent_.setFillColor(sf::Color(141, 191, 215));
        reloadCurrent_.setSize(sf::Vector2f(3.0f, 3.0f));
        reloadOutOf_.setSize(sf::Vector2f(3.0f, 3.0f));
        reloadCurrent_.setOrigin(sf::Vector2f(3.0f, 3.0f));
        reloadOutOf_.setOrigin(sf::Vector2f(3.0f, 3.0f));
}

//Menu constructor
GUIManager::GUIManager(sf::RenderWindow* pWindow)
        :menu_(true), pWindow_(pWindow)
{


        if (!font_.loadFromFile("arial.ttf"))
                std::cout << "Failed to load font." << std::endl;
        selectionRect_.setFillColor(sf::Color(200, 200, 200, 100));
        selectionRect_.setPosition(sf::Vector2f(-10000.0f, -10000.0f));

        surviveTitleText_.setFont(font_);
        playText_.setFont(font_);
        settingsText_.setFont(font_);
        quitText_.setFont(font_);

        //Assigns all the strings to the texts
        surviveTitleText_.setString("Survive");
        playText_.setString("Play");
        settingsText_.setString("Settings");
        quitText_.setString("Quit");
        settingsTitleText_.setString("Settings");
        audioText_.setString("Audio");
        gameplayText_.setString("Gameplay");
        fullscreenText_.setString("Fullscreen: No");
        gameplayTitleText_.setString("Gameplay");
        mapSizeText_.setString("Map Size: 257");
        difficultyMultiplierText_.setString("Difficulty: Easy");

       
}

//Default - do not use
GUIManager::GUIManager()
{
}

void GUIManager::update(const sf::Time& dT)
{
        sf::Vector2f windowSize = pWindow_->getView().getSize();

        if (menu_)
        {
                if (currentMenu_ == main)
                {
                        surviveTitleText_.setCharacterSize((int)(titleSize_ * windowSize.y));
                        playText_.setCharacterSize((int)(buttonSize_ * windowSize.y));
                        settingsText_.setCharacterSize((int)(buttonSize_ * windowSize.y));
                        quitText_.setCharacterSize((int)(buttonSize_ * windowSize.y));
                        std::cout << settingsText_.getPosition().x;

                        surviveTitleText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_);
                        playText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_ + windowSize.y * 0.15f * 2);
                        settingsText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_ + windowSize.y * 0.15f * 3);
                        quitText_.setPosition(windowSize.x / 2.0f, windowSize.y * titleOffset_ + windowSize.y * 0.15f * 4);

               
                        if (playText_.getGlobalBounds().contains((sf::Vector2f)sf::Mouse::getPosition(*pWindow_)))
                        {
                                selectionRect_.setSize(sf::Vector2f(playText_.getGlobalBounds().width, playText_.getGlobalBounds().height));
                                selectionRect_.setPosition(playText_.getPosition());
                        }
                        else if (settingsText_.getGlobalBounds().contains((sf::Vector2f)sf::Mouse::getPosition(*pWindow_)))
                        {
                                selectionRect_.setSize(sf::Vector2f(settingsText_.getGlobalBounds().width, settingsText_.getGlobalBounds().height));
                                selectionRect_.setPosition(settingsText_.getPosition());
                        }
                        else if (quitText_.getGlobalBounds().contains((sf::Vector2f)sf::Mouse::getPosition(*pWindow_)))
                        {
                                selectionRect_.setSize(sf::Vector2f(quitText_.getGlobalBounds().width, quitText_.getGlobalBounds().height));
                                selectionRect_.setPosition(quitText_.getPosition());
                        }
                }

        }
        else
        {
                sf::Vector2f currentWindowPos(pWindow_->getView().getCenter().x - windowSize.x / 2.0f, pWindow_->getView().getCenter().y - windowSize.y / 2.);
                //Health
                healthOutOf_.setScale(windowSize.x * healthSize_.x, windowSize.y * healthSize_.y);
                healthCurrent_.setScale(windowSize.x * healthSize_.x * (pPlayer_->getHealth() / 100.0f), windowSize.y * healthSize_.y);
                healthOutOf_.setPosition(currentWindowPos.x + windowSize.x * healthOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * healthOffset_.y);
                healthCurrent_.setPosition(currentWindowPos.x + windowSize.x * healthOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * healthOffset_.y);

                //Reload
                Gun gun = pPlayer_->getGuns().at(pPlayer_->getCurrentGunIndex());
                if (gun.isReloading())
                        reloadCurrent_.setScale(windowSize.x * reloadSize_.x * (gun.getCurrentReloadTime().asSeconds() / gun.getReloadTime()), windowSize.y * reloadSize_.y);
                else
                        reloadCurrent_.setScale(windowSize.x * reloadSize_.x * (gun.getCurrentBullets() / (float)gun.getBulletsPerMag()), windowSize.y * reloadSize_.y);

                reloadOutOf_.setScale(windowSize.x * reloadSize_.x, windowSize.y * reloadSize_.y);
                reloadCurrent_.setPosition(currentWindowPos.x + windowSize.x - windowSize.x * reloadOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * reloadOffset_.y);
                reloadOutOf_.setPosition(currentWindowPos.x + windowSize.x - windowSize.x * reloadOffset_.x, currentWindowPos.y + windowSize.y - windowSize.y * reloadOffset_.y);

                //Ammo
                ammo_.setString(std::to_string(pPlayer_->getGuns().at(pPlayer_->getCurrentGunIndex()).getCurrentBullets()) + " / " + std::to_string(pPlayer_->getGuns().at(pPlayer_->getCurrentGunIndex()).getTotalAmmo()));
                ammo_.setOrigin(ammo_.getLocalBounds().width / 2, ammo_.getLocalBounds().height / 2.0f);
                ammo_.setCharacterSize(0.75f * reloadOutOf_.getGlobalBounds().height);
                ammo_.setPosition(reloadOutOf_.getPosition().x - reloadOutOf_.getGlobalBounds().width / 2.0f, reloadOutOf_.getPosition().y - reloadOutOf_.getGlobalBounds().height / 2.0f);
        }
}

//Getters
sf::RectangleShape GUIManager::getHealthOutOf() const { return healthOutOf_; }
sf::RectangleShape GUIManager::getHealthCurrent() const { return healthCurrent_; }
sf::RectangleShape GUIManager::getReloadOutOf() const { return reloadOutOf_; }
sf::RectangleShape GUIManager::getReloadCurrent() const { return reloadCurrent_; }
sf::Text GUIManager::getTotalAmmo() const { return ammo_; }
bool GUIManager::isMenu() const { return menu_; }
sf::RectangleShape GUIManager::getSelectionRect() const { return selectionRect_; }
sf::Text GUIManager::getSurviveTitleText() const { return surviveTitleText_; }
sf::Text GUIManager::getPlayText() const { return playText_; }
sf::Text GUIManager::getSettingsText() const { return settingsText_; }
sf::Text GUIManager::getQuitText() const { return quitText_; }

MrSnappingTurtle

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: Access violation on certain attributes of sf::Text [C++]
« Reply #1 on: December 24, 2014, 06:11:30 am »
Nevermind. I didn't find a solution but the whole reason this was even a problem is because I tried to refit this for something it wasn't. I reverted to a commit earlier today. Hope someone wasn't typing the solution right as I post this...

 

anything