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

Author Topic: Loading texture into class  (Read 6317 times)

0 Members and 4 Guests are viewing this topic.

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Loading texture into class
« on: March 08, 2021, 10:49:09 am »
Hey,I have a problem on loading texture inside custom class. Its probably a pure C++ Problem (not SFML) but i am losing my mind trying to solve this.
#pragma once
#include "Unit.h"
#include <SFML\Graphics.hpp>
#include <string>
#include <iostream>

class Riflemans : public Unit, public sf::Drawable
{
public:
        Riflemans() = delete;
        Riflemans(int at, int pe, int ar, int du, Nation na=Nation::USA, Unittype ty=Unittype::INFANTRY)
        : Unit(at, pe, ar, du, na, ty)
        {
                if (!m_texture.loadFromFile("a.png"))
                {
                        std::cout << "BYE" << std::endl;
                        exit(1);
                }
                m_sprite.setTexture(m_texture);
       
       
        }

        ~Riflemans(){}
private:
        sf::Texture m_texture;
        sf::Sprite m_sprite;
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
                target.draw(m_sprite);
        };

};
 

This is Infantry.h file. Everything is compiling,etc but when executed whole program goes down due"Access violation while reading". VS shows the error
if (!m_texture.loadFromFile("a.png"))
<-here

I was trying to debug and if I'm correct it cannot read m_texture properly of something. I am pretty sure this is a very obvious error in code but i am just stuck :v
TIA

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Loading texture into class
« Reply #1 on: March 08, 2021, 12:18:36 pm »
whats inside Unit.h? I removed all references to it in the code (since i don't know what is looks like) and it worked.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading texture into class
« Reply #2 on: March 08, 2021, 12:50:01 pm »
#pragma once
enum class Nation{USA,RUS};
enum class Unittype{INFANTRY,LIGHT,TANK,HELICOPTER};


class Unit
{
public:
        Unit(int at, int pe, int ar, int du, Nation na, Unittype ty);
        //~Unit();
        int PENATK(int &pen);
        bool takeDMG(int pen, int atk);

private:
        int attack;
        int penetration;
        int armor;
        int durability;
        Nation nationality;
        Unittype type;
};

Unit.h

Unit.cpp
#include "Unit.h"

Unit::Unit(int at, int pe, int ar, int du, Nation na, Unittype ty) {
        attack = at;
        penetration = pe;
        armor = ar;
        durability = du;
        nationality = na;
        type = ty;
}
int Unit::PENATK(int& pen) {
        pen = penetration;
        return attack;
}
bool Unit::takeDMG(int pen, int atk) {
        if (pen > armor)
                durability = durability - atk;

        if (durability <= 0)
                return 1;

        else
                return 0;
}


EDIT:
main.cpp will be useful too i believe:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Infantry.h"
#include "Unit.h"

int main() {
        sf::RenderWindow _window(sf::VideoMode(1024,768), "sfml", sf::Style::Close | sf::Style::Titlebar);
        _window.setVerticalSyncEnabled(true);
        sf::Event evnt;
        Riflemans rf(10,10,10,10);

        while (_window.isOpen()) {
                while (_window.pollEvent(evnt)) {
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                _window.close();
                                break;
                        default:
                                break;
                        }//switch






                }//while
       
       
       
       
       
                _window.clear(sf::Color::Blue);
                _window.draw(rf);
                _window.display();
        }//petlagry

       



        return 0;
}
« Last Edit: March 08, 2021, 01:11:59 pm by HaS3n »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Loading texture into class
« Reply #3 on: March 08, 2021, 03:32:08 pm »
its working for me. isn't your image file corrupted?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading texture into class
« Reply #4 on: March 08, 2021, 05:15:30 pm »
I think no. I did test- I wrote direct path to directory on desktop and tried different images. Still nothing :/
I can make video of it.

Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: Loading texture into class
« Reply #5 on: March 08, 2021, 10:27:14 pm »
Could you provide single-file example to reproduce the behaviour or attach your project file?

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading texture into class
« Reply #6 on: March 08, 2021, 11:05:43 pm »
You want my VS project file? Here it is. (note that I am not fully familiar with English nomenclature,so sometimes i can misunderstand some sentences. I really apologise :v)
Also code is slightly different. I moved texture from class (i realized putting it inside class is not very smart-every object will contain texture and this is waste of memory). texture object is now inside main function and Riflemans class constructor takes pointer to texture object and setting sprite texture. But problem is the same. The exception shows off at line of loading texture.
 

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading texture into class
« Reply #7 on: March 08, 2021, 11:06:22 pm »
CPP FILES:

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Loading texture into class
« Reply #8 on: March 09, 2021, 12:32:49 am »
is it working with a simple common SFML code, like this?

#include <SFML/Graphics.hpp>
int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    sf::Texture texture;
    if (!texture.loadFromFile("button.png"))
        return 1;
    sf::Sprite sprite(texture);
    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(sprite);
        window.display();
    }
    return 0;
}

 
Visit my game site (and hopefully help funding it? )
Website | IndieDB

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading texture into class
« Reply #9 on: March 09, 2021, 08:46:45 am »
No,its not working. No matter if file exists or not. If not program should just stop working in a "peaceful" way, but it's crashing instead.Every time exception shows "Access violation while reading localization*HEX NUMBERS*"

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Loading texture into class
« Reply #10 on: March 09, 2021, 12:42:21 pm »
what are your system specs, sfml version, how did you install it...?

the issue doesn't seem to be in the code
Visit my game site (and hopefully help funding it? )
Website | IndieDB

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading texture into class
« Reply #11 on: March 09, 2021, 01:56:53 pm »
SFML is 2.5.1
I have followed YT tutorial (I forgotten the video :c). Basically I put SFML folder on C drive and then linked everything to VS. I have downloaded the 2017 version (I am using 2019...). Is it making those troubles? (Guy on video did the same...). I am working on Windows 10 64bit (but Im "developing" SFML on x32). (Note that test code from SFML main page run perfectly)
« Last Edit: March 09, 2021, 02:02:17 pm by HaS3n »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Loading texture into class
« Reply #12 on: March 09, 2021, 05:25:28 pm »
what code do you mean? is it simpler than the code i posted (and you said didn't work?)
could you try updating graphic drivers and specially OpenGL? trying different file formats for your textures?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

HaS3n

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading texture into class
« Reply #13 on: March 09, 2021, 06:16:36 pm »
https://www.sfml-dev.org/tutorials/2.5/start-vc.php
Code from here (this code WORKS, your didn't).
Drivers are latest. Results as expected: nothing :(
Tried different file formats: nothing.
are the VS 2017 version compatible to VS 2019? Maybe that's a problem?
Also i can try setting up SFML one more time in a different project, Can you tell me tutorial how to do this in a proper way? (Or i can pick random from internet...)
TIA

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Loading texture into class
« Reply #14 on: March 09, 2021, 08:26:11 pm »
no' i dont believe that's the problem. you have everything working, except for the textures.
i believe that it will, but do you get this error with something simples as that?
#include <SFML/Graphics.hpp>
int main(){
    sf::Texture texture;
    texture.loadFromFile("a.png");
    return 0;
}

 

i'm not a everyday Windows user, but it seems that VS is trying to access files that have restricted access to other programs... maybe the forum pros could give a better idea :/

edit: it was suggested in the Discord channel if you're not mixing debug and release libraries?
« Last Edit: March 09, 2021, 08:44:35 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything