Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6012471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:24:34+00:00 2026-05-23T02:24:34+00:00

I’m trying to figure out how to manage the whole game loop manually in

  • 0

I’m trying to figure out how to manage the whole game loop manually in a Windows game, without using the regular Game Microsoft.Xna.Framework.Game class.

The reason for this is using the regular Game class causes some stuttering in my game. Not much, but because of the specific nature of the game it is still quite visible.

After trying a bunch of different settings (vsync, fixedtimestep, various framerates etc.) I decided to try write my own Game class to have full control of the timing. I am not sure that will fix it, but at least this way I have full control.

Basically I need to:

  1. Set up the game window
  2. In a loop: Do all rendering as usual, and then flush the result to the screen, manage backbuffers etc.

Anyone knows how to do this? It sounds quite easy in fact, but could not find any documentation on how to do it.


Not sure what I am doing wrong, but I have the following code (just for testing, timing will be handled differently), and the loop will run for a little while then stop. Once I pass my mousepointer over the window the loop will run for a little while again.

    private void Application_Idle(object pSender, EventArgs pEventArgs)
    {
        Thread.Sleep(500);
        //Message message;
        //while (!PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
        {
            gametime.update();
            Update(gametime);
            Draw(gametime);
            GraphicsDevice.Present();
        }
    }

If enabling the “while PeekMessage”, the loop will run continuously, but ignoring the sleep and also stopping when the mouse is moving over the window. Not sure what is going on here…

I think optimally I would just want to do something simple like this in the main render loop:

    while (alive)
    {
      Thread.Sleep(100);
      gametime.update();
      Update(gametime);
      Draw(gametime);
      GraphicsDevice.Present();
    }

But in this case the window remains blank, as it seems the window is not actually being redrawn with the new content. I tried a form.Refresh(), but still no go… Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:24:35+00:00Added an answer on May 23, 2026 at 2:24 am

    (added xbox information)

    for windows you Basically need to create a Form and Show it, then store its handle and the form itself.
    Using this handle you can create a GraphicsDevice.
    Then you hook Application.Idle to your own function that calls your update and render.
    For example

    public class MyGame
    {
    public Form form;
    public GraphicsDevice GraphicsDevice;
    
    public MyGame()
    {
        form = new Form();
        form.ClientSize = new Size(1280, 1024);
        form.MainMenuStrip = null;
    
        form.Show();
    }
    
    public void Run()
    {      
        PresentationParameters pp = new PresentationParameters();
        pp.DeviceWindowHandle = form.Handle;
    
        pp.BackBufferFormat = SurfaceFormat.Color;
        pp.BackBufferWidth = 1280;
        pp.BackBufferHeight = 1024;
        pp.RenderTargetUsage = RenderTargetUsage.DiscardContents; 
        pp.IsFullScreen = false; 
    
        pp.MultiSampleCount = 16;
    
        pp.DepthStencilFormat = DepthFormat.Depth24Stencil8;
    
        GraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
                                                  GraphicsProfile.HiDef,
                                                  pp);
        Application.Idle += new EventHandler(Application_Idle);
        Application.Run(form);
    }
    
     private void Application_Idle(object pSender, EventArgs pEventArgs)
     {
        Message message;
        while (!PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
        {
            /* Your logic goes here
             Custom timing and so on
            Update();
            Render();
            */
        }
    
     }
    
     void Render()
     {
          GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target, Color.Black, 1, 0);
          //Your logic here.
         GraphicsDevice.Present();
     }
        [StructLayout(LayoutKind.Sequential)]
        private struct Message
        {
            public IntPtr hWnd;
            public int msg;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint time;
            public Point p;
        }
    
        [return: MarshalAs(UnmanagedType.Bool)]
        [SuppressUnmanagedCodeSecurity, DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint
            messageFilterMin, uint messageFilterMax, uint flags);
    }
    

    EDIT 1

    For xbox you may just be able to place your own custom run function with your game loop in a throttled while true loop. Inside that run outside the top of the while true you will probably have to do the graphics device initialization and verification with IntPtr.Zero as your handle

    EDIT 2
    i use something like this ( got from http://www.koonsolo.com/news/dewitters-gameloop/ )

            private long nextGameTick;
    
            private Stopwatch stopwatch;
    
            const int ticksPerSecond = 60;
            const int skipTicks = 1000 / ticksPerSecond;
            private const int maxSkip = 10;
            `constructor 
             stopwatch = Stopwatch.StartNew();
    
            nextGameTick = stopwatch.ElapsedMilliseconds; 
    
            `loop 
            int loops = 0;
            long currentTick = stopwatch.ElapsedMilliseconds;
            while ( (ulong)(currentTick - nextGameTick) > skipTicks && loops < maxSkip)
            {
                Update(16.667f);
                nextGameTick += skipTicks;
                loops++;
    
            }
    
            PreRender();
            Render();
            PostRender();
    

    EDIT 3

    Creating a content manager was a little more work, but still managable. You need to create a class that implements IServiceProvider. This class takes a GraphicsDevice in its constructor in order to create the next class the implements IGraphicsDeviceProvider. in addition I implement GetService like this

        //in implementer of IServiceProvider
        public object GetService ( Type serviceType )
        {
            if ( serviceType == typeof ( IGraphicsDeviceService ) )
            {
                return myGraphicsService;
            }
    
            return null;
        }
    

    For convenience i also add a method to the class to create and return managers

        //in implementer of IServiceProvider
        public ContentManager CreateContentManager( string sPath )
        {
    
            ContentManager content = new ContentManager(this);
    
            content.RootDirectory = sPath;
    
            return content;
    
        }
    

    In addition i create a class that implements IGraphicsDeviceService and takes a reference to my GraphicsDevice. then I create a property and field in it like so

        //in implementer of IGraphicsDeviceService 
        private GraphicsDevice graphicsDevice;
        public GraphicsDevice GraphicsDevice
        {
            get
            {
                return graphicsDevice;
            }
        }
    

    So the call ends up being somehting like

    MyServiceProvider m = new MyServiceProvider(graphicsDevice);
    ContentManager content = m.CreateContentManager("Content");
    

    where

    MyServiceProvider(GraphicsDevice graphicsDevice)
    {
          myGraphicsService = new MyGraphicsDeviceService(graphicsDevice);
    }
    
    MyGraphicsDeviceService(GraphicsDevice gfxDevice)
    {
         graphicsDevice = gfxDevice;
    }
    

    -Sorry for fragmenting the code around but its not something i wrote too recently so im having difficulty remembering parts.

    EDIT 4

    i had an odd case with my custom game i just remembered when i new the Form for it i
    had to bind

        private void IgnoreAlt(object pSender, KeyEventArgs pEventArgs)
        {
            if (pEventArgs.Alt && pEventArgs.KeyCode != Keys.F4)
                pEventArgs.Handled = true;
    
        }
    

    to

        form.KeyUp += IgnoreAlt;
        form.KeyDown += IgnoreAlt;
    

    otherwise i got some horrible stalls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to loop through a bunch of documents I have to put
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.