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

Author Topic: [SOLVED]Problems with Struct pointers that contain textures  (Read 4466 times)

0 Members and 1 Guest are viewing this topic.

Hipreme

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
[SOLVED]Problems with Struct pointers that contain textures
« on: August 03, 2018, 09:30:35 am »
Hello, I'm having problems with structure pointers, when I declare simply a structure without a pointer, I can freely draw it onto my screen.

Now, when I try to do like where it is commented,  it gives a SIGSEV error after I load the image inside the texture place, at the it[0] assignment, but I don't understand why, as I do always like this when I want to make a dynamic array.
ps: The image is correctly loaded.

This is just a simple example, as my entire class got one realloc function for it, but the problem is inside this struct.


Just another plus question:
Why did I needed to declare a sf::Vector2f for the size? When I tried using without the new variable, eg:
this->spriteView.setScale(width / t.getSize().x, height / t.getSize().y) it wasn't really scaling anything, the getScale function was returning 0 for the values
« Last Edit: August 04, 2018, 07:38:46 am by Hipreme »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Problems with Struct pointers that contain textures
« Reply #1 on: August 03, 2018, 12:55:31 pm »
You can't use C practices and just apply them to C++.
C++ classes/struct can't be created with malloc. That's what the C++ keyword new is for.

I can't teach you C++ here, as such, I recommend you learn some proper C++ before attempting to use SFML. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hipreme

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Problems with Struct pointers that contain textures
« Reply #2 on: August 03, 2018, 11:37:00 pm »
Well, I read it again, and the documentation never said that one can't use malloc instead of new, the only difference is that you will need to call the constructor separately, as malloc just allocates memories.

I tried doing with

wn::Item* it = new wn::Item[3];
it[0] = wn::Item(100, 100);

And it still give errors:

It is giving GL internal error in Texture.cpp 93
(If I remember, there was something about glFlush in the console log)

After my researches about these GL Internal error, it usually the drivers of the person the problem, but I don't see any potential error with my drivers, they are all updated and stuff, anad my graphic card is in one of the latest versions

PS: Saying for people to learn a language instead doing projects is a disservice for people who learn by doing programming, one does not learn how to apply the knowledge without needing to apply, if I started before learning sfml instead studying the language, I would be much better at the API and at the language than today
« Last Edit: August 03, 2018, 11:43:14 pm by Hipreme »

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Problems with Struct pointers that contain textures
« Reply #3 on: August 04, 2018, 12:04:01 am »
Pretty sure if new wasn't used to allocate for and construct an object, you need to use placement new - not just a call to the constructor.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Problems with Struct pointers that contain textures
« Reply #4 on: August 04, 2018, 12:33:35 am »
I have no idea what "documentation" you read, but it's obviously not part of the SFML documentation, as SFML isn't here to teach you C++. Which again is the same reason I suggested to first learn C++. And dabbertorres is right, but at this point you really shouldn't learn what placement new is, but instead learn how to write normal C++ applications, learn how classes work, what scopes are in C++, what the difference is between struct and a class, what a constructor and deconstructor does and many more things.

We're happy to help with any problems related to SFML and as you see, we even try to help with C++ issue sometimes, but then again if you don't even want to take the help people offer you and try and stick to your own wrong interpretation of who knows what exactly, then you shouldn't be surprised when people quickly stop providing help, even at places where general C++ questions are welcome. ;)
Learning something isn't about getting others to explain stuff to you all the time, but it's very well also to put in your time and study books and materials on your own until you understand them.

it[0] = wn::Item(100, 100); this creates a copy which can potentially lead to issues.
If you want help specifically with SFML related issues, then you shouldn't be vague on the error messages. Error messages aren't just decorations, but they do actually communicate what is going on.
There's also a very important tool called the debugger, that you should learn how to use, so you can go step by step through your code and see variables change, etc.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hipreme

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Problems with Struct pointers that contain textures
« Reply #5 on: August 04, 2018, 07:16:04 am »
I really don't believe that making one post in my life on a forum is getting help from people all the time anyway.

And I do really think that I know what a class is and what a struct is, specially, I don't think that not using the new operator gives you the right to say that I don't know how to program, as I said, I'm used to use malloc, and I learn by doing projects anyway.

When tried to call the constructor for the array indexes after the first new call, made a strange behavior and some error like this was appearing:




But I got through this, just made some function which does the same thing as a constructor and called it for the array instead the constructor.

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Problems with Struct pointers that contain textures
« Reply #6 on: August 06, 2018, 02:28:25 pm »
And I do really think that I know what a class is and what a struct is, specially, I don't think that not using the new operator gives you the right to say that I don't know how to program, as I said, I'm used to use malloc, and I learn by doing projects anyway.

It somewhat does because it shows you lack some fundamentals of C++. You don't use malloc in C++ (except in rare case which your situation is not a valid case). If you want to use malloc, program in C, not in C++. If you want help, don't reject it when it does not satisfy you. When we tell you to go read a good C++ book, it's not to insult you on your lack of knowledge, but it's to help you and save us both time. All the points eXpl0it3r said are valid.

The fact you go around the errors with possible hack(not sure what you did with a function instead of the constructor) will just bring you maintenance problem in the future.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED]Problems with Struct pointers that contain textures
« Reply #7 on: August 06, 2018, 02:56:36 pm »
You're explicitly calling the destructor of your sf::Texture member, which you're not supposed to do.

That will fix the OpenGL error, but then all you'll see is white squares instead of textured sprites, because you're making copies of your Item class, and you didn't implement a correct copy constructor (and the one generated by the compiler is not correct either, in this specific case).
Laurent Gomila - SFML developer

 

anything