Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using the Winforms sample on the app hub and everything is working fine except my spritebatch won't draw anything unless I call Invalidate in the Draw method. I have this in my initialize method:

Application.Idle += delegate { Invalidate(); }; 

I used a breakpoint and it is indeed invalidating my program and it is calling my draw method. I get no errors with the spritebatch and all the textures are loaded I just don't see anything on the screen. Here's the code I have:

    protected override void Draw()
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        tileSheet.Draw(spriteBatch);
        foreach (Image img in selector)
            img.Draw(spriteBatch);
        spriteBatch.End();
    }

But when I do this:

    protected override void Draw()
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        tileSheet.Draw(spriteBatch);
        foreach (Image img in selector)
            img.Draw(spriteBatch);
        spriteBatch.End();
        Invalidate();
    }

then all of a sudden the drawing starts to work! but the problem is that it freezes everything else and only that control gets updated. What can I do to fix this? It's really frustrating.

share|improve this question
1  
Please don't double post. If your question hasn't been answered, try editing it to include more information. Your alternative is to ask somewhere else. (You are getting free help after all, don't demand too much). –  Byte56 Sep 20 '13 at 13:27
    
It seems that your program never runs the line: Application.Idle += delegate { Invalidate(); }; . Did you check this with your debugger? –  user27811 Sep 24 '13 at 10:55
add comment

1 Answer

There is a very good post by Shawn Hargreaves about XNA with WinForms you should definitly check it out.

In addition here are two tutorials aout XNA in WinForms and WPF, with a bit more complex event handling (both are in german)

Maybe try to set up a timer and rebuild XNAs fixed time step. This will queue the timer in your UI thread processing and update the drawing. Invalidate will be called whenever a new frame is prepared and ready.

timer = new Timer();
    timer.Interval = (int)TargetElapsedTime.TotalMilliseconds;
    timer.Tick += Tick;
    timer.Start();

    Stopwatch stopWatch = Stopwatch.StartNew();

    readonly TimeSpan TargetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 60);
    readonly TimeSpan MaxElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 10);

    TimeSpan accumulatedTime;
    TimeSpan lastTime;


    void Tick(object sender, EventArgs e)
    {
        TimeSpan currentTime = stopWatch.Elapsed;
        TimeSpan elapsedTime = currentTime - lastTime;
        lastTime = currentTime;

        if (elapsedTime > MaxElapsedTime)
        {
            elapsedTime = MaxElapsedTime;
        }

        accumulatedTime += elapsedTime;

        bool updated = false;

        while (accumulatedTime >= TargetElapsedTime)
        {
            Update();
            Draw();

            accumulatedTime -= TargetElapsedTime;
            updated = true;
        }

        if (updated)
        {
            Invalidate();
        }
    }

I hope this gets you startet!

share|improve this answer
    
Answers should address the issue directly, not just supply related information. Maybe make this a comment instead. –  Byte56 Sep 20 '13 at 13:28
    
Added a proposal, which might solve the problem –  floAr Sep 20 '13 at 14:04
1  
The timer of the .net framework has a limite frequency. I have never get it to work above 40 fps. –  Emir Lima Nov 19 '13 at 16:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.