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

Author Topic: struct + sf::Music + malloc = error (?)  (Read 3809 times)

0 Members and 1 Guest are viewing this topic.

virus

  • Newbie
  • *
  • Posts: 13
    • View Profile
struct + sf::Music + malloc = error (?)
« on: September 11, 2010, 12:27:03 am »
Hello!

I am trying to have sf::Music into a dynamic list (struct below):

typedef struct sound{
    sf::Music audio;
    char *name;
    struct sound *next;
}sound;

When I declare:

sound test;
test.audio.OpenFromFile("music.ogg");
test.audio.Play();

it works.

But when I do:

sound *test;
test = (sound*)malloc(sizeof(sound));
test->audio.OpenFromFile("music.ogg");
test->audio.Play();

It doesn't work.

(I know, I am no treating the char stuff, but i just didn't post it here...)

Why doesn't it work?

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
struct + sf::Music + malloc = error (?)
« Reply #1 on: September 11, 2010, 12:57:35 am »
When using C++ you shouldn't use malloc, use new instead.
Code: [Select]
sound *test;
test = new sound;

virus

  • Newbie
  • *
  • Posts: 13
    • View Profile
struct + sf::Music + malloc = error (?)
« Reply #2 on: September 11, 2010, 01:02:31 am »
It worked o.o"

Such an easy solution!

Thanks a LOT! :P

virus

  • Newbie
  • *
  • Posts: 13
    • View Profile
struct + sf::Music + malloc = error (?)
« Reply #3 on: September 11, 2010, 01:17:30 am »
What about playing the same music 2 times at almost the same time without stopping the first?

I mean, i am playing a music, and then I want it to start again, without stopping the first music to play, so it starts again and the same music is playing in 2 different positions.

How can I do that?

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
struct + sf::Music + malloc = error (?)
« Reply #4 on: September 11, 2010, 01:30:29 am »
Have two music objects.

virus

  • Newbie
  • *
  • Posts: 13
    • View Profile
struct + sf::Music + malloc = error (?)
« Reply #5 on: September 11, 2010, 01:39:44 am »
There is no other way? It is too slow and heavy.

Afterall, i cannot know how many times will i have to play the same song simultaneously :/

virus

  • Newbie
  • *
  • Posts: 13
    • View Profile
struct + sf::Music + malloc = error (?)
« Reply #6 on: September 11, 2010, 04:26:01 am »
I did it!

SoundBuffer + Sound

Thanks for helping :)

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
struct + sf::Music + malloc = error (?)
« Reply #7 on: September 11, 2010, 12:05:55 pm »
I think that sf::Music is one of the most light-weight class in the SFML library