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

Author Topic: [SOLVED]Help needed with music  (Read 3300 times)

0 Members and 1 Guest are viewing this topic.

Santa-Claus

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
[SOLVED]Help needed with music
« on: March 23, 2014, 04:12:52 pm »
Hello everyone,

So i'm having problems with my music it's playing and stuff but my window kinda starts bugging.
If i set the music in my gameloop then the window isn't usable anymore.
I haven't tested to draw anything on it so that could still be possible but the close open and minimize button no longer work and when i overlay another program over it, then i can no longer open it.

static void Main()
        {
            // initialize the form
            System.Windows.Forms.Form form = new System.Windows.Forms.Form(); // create our form
            form.Text = Variables.General.GameName;
            System.Drawing.Icon ico = new System.Drawing.Icon("icon.ico");
            form.Icon = ico;
            form.Size = new System.Drawing.Size(Variables.GameScreen.ScreenWidth, Variables.GameScreen.ScreenHeight); // set form size to 600 width & 600 height
            form.Show(); // show our form
            DrawingSurface rendersurface = new DrawingSurface(); // our control for SFML to draw on
            rendersurface.Size = new System.Drawing.Size(Variables.GameScreen.ScreenWidth, Variables.GameScreen.ScreenHeight); // set our SFML surface control size to be 500 width & 500 height
            form.Controls.Add(rendersurface); // add the SFML surface control to our form
            rendersurface.Location = new System.Drawing.Point(0, 0); // center our control on the form

            // initialize sfml
            SFML.Graphics.RenderWindow renderwindow = new SFML.Graphics.RenderWindow(rendersurface.Handle); // creates our SFML RenderWindow on our surface control

            // drawing loop
            while (form.Visible) // loop while the window is open
            {
                System.Windows.Forms.Application.DoEvents(); // handle form events
                renderwindow.DispatchEvents(); // handle SFML events - NOTE this is still required when SFML is hosted in another window
                renderwindow.Clear(SFML.Graphics.Color.Black); // clear our SFML RenderWindow
                renderwindow.Display(); // display what SFML has drawn to the screen
                GameLoop.Running();
            }
        }
 

/// <summary>
        /// Play a music
        /// </summary>
        public static void PlayMusic(string path, bool loop)
        {
            // Load an ogg music file
            Music music = new Music(path);

            // Play it
            music.Volume = 50;
            music.Play();
            music.Loop = loop;

            // Loop while the music is playing
            while (music.Status == SoundStatus.Playing)
            {
                // Leave some CPU time for other processes
                Thread.Sleep(100);
            }
        }
 

class GameLoop
    {  
        public static void Running()
        {
            SoundEngine.PlayMusic("DataFiles/Music/Main.wav", true);
        }
    }
 

Kind regards santa,
« Last Edit: March 24, 2014, 11:45:48 am by Santa-Claus »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Help needed with music
« Reply #1 on: March 23, 2014, 08:27:38 pm »
While music is playing, you're not processing events, thus the close event has no effect.

The main loop and GameLoop don't run in parallel (and they shouldn't), thus you need to include the event processing (and drawing) in the GameLoop or let the GameLoop return to the main loop each frame. ;)
« Last Edit: March 24, 2014, 11:56:27 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Santa-Claus

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Help needed with music
« Reply #2 on: March 24, 2014, 11:45:35 am »
Ok fixed it.
« Last Edit: March 24, 2014, 03:58:53 pm by Santa-Claus »

Deathbeam

  • Jr. Member
  • **
  • Posts: 82
  • VB6, VB.NET, C#, HTML, PHP, CSS, JavaScript nerd.
    • View Profile
    • My portfolio
    • Email
Re: [SOLVED]Help needed with music
« Reply #3 on: March 26, 2014, 08:57:18 am »
Why you even using Thread.Sleep(100)? It is unnecessary for audio...
Spooker Framework - Open source gaming library
My portfolio
Indie Armory - Small community of a game developers. Everyone is welcome. Bring your friends, family, pets...