Embeding a XNA Game in a Windows Form User Control - c#

I have a simple XNA 4.0 Game written. I want to make a Windows Form User Control that will render this game, and provide the necessary interaction feedback (keyboard and mouse) back to XNA.
I have tried the following:
In XNA:
Redirect XNA's Game.GraphicsDevice to a RenderTarget2D.
Emit an event sending an object with KeyboardState and MouseState to be filled in by Windows Forms, at the beginning of Update()
In WinForms:
Capture the event, filling in KeyboardState and MouseState with data obtained by the usual keyboard and mouse events in Windows Forms.
On the OnPaint, call Game.RunOnFrame()
Get the RenderTarget2D from the game (as a texture).
Lock the texture's data, and try to paint it pixel by pixel in my user control's Graphics.
Another idea was just calling Game.Run() (o a new Thread) and emit an event in Game sending the RenderTarget2D.
I have found the following problems:
If I call Game.Run() I have no way to hide the game's window (which appears black, because I'm redirecting the render)
Game.Run() must be called on a different thread because it starts a new event loop (it calls Application methods), and then I'm painting in my user control from a different thread that it was created (bad, bad)
Locking a RenderTarget2D with Color, Vector4, and even Rgba32 doesn't appear to work for me (it says 'wrong structure size')
Any ideas?
Thanks in advance.

There is a great code sample on xna's site about embedding xna in a winform. It takes you through all the steps. I make games for xbox, but I use this for the level editor we've made for our engine.
http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1

Related

Synchronize CefSharp render and WPF render

I'm using CefSharp in WPF application in offscreen-mode.
Inside browser WebGL 3D model. At some points of model must be "billboard" but it must be WPF object in 2D space of WPF.
At each frame render I calculate projection of 3D point to 2D screen coords and send callback to WPF with 2D coords.
Visually 2D object of WPF moved on 1-2 frame faster than 3D model. It's strange because I'm catching onRender event of CEF and set billboards coords after browser render.
How I can synchronize render of 3D model and 2D billboards in WPF?
The culprit may be the fact that a WPF application does not have a predictable refresh rate. It redraws the UI based on under-the-hood heuristics influenced by your hardware and the current state of the OS. WPF cannot be forced to redraw at will unless you work around it with DirectDraw. Read in more detail here: https://rhnatiuk.wordpress.com/2008/12/21/wpf-video-playback-problems/
If you have continuous motion rendering within a WPF application, such as video, animation effects, or a browser with a WebGL-rendered moving object, you cannot reliably frame-sync it for screenshot purposes. I tried to run video in a WPF CefSharp app and the performance (perceived and measured) was abysmal on every (even high-end) computer I could find.
You can try to port your application into WinForms to benefit from constant frame rates. In WinForms, you would have to grab screenshots of the form or of the browser control which is messy but should be doable.

Best way to draw moving sphere in a smooth way in C#

Hello: I am trying to create an app which will display a moving sphere. App will vary speed and direction. I've tried Adobe Flash but cannot get it smooth. Smoothness is essential in this case. So I am trying C#.
Initially, I can see that this can be implemented by:
1) Creating a PictureBox of a sphere, and using a Timer, change its coordinates. or
2) Using the this.paint function to draw a filled circle, and somehow, with a timer, erasing and redrawing it.
Can someone recommend the best path to take? I'll have a main menu where the user will chose speed/direction/how many etc... and then simply show the "game window" with the moving spheres. Any guidance would be much appreciated.
This is to be displayed on a PC only.
Thanks
-Ed
I just answered a similar question here.
NOTE: Depending on your needs, it is possible to achieve smooth animations under winforms (under certain conditions) though you are responsible for everything. wpf provides an animation framework but wpf is perhaps a milestone harder.
It probably does not matter should you pursue winforms first or WPF. You arguably could learn the basics under winforms then move over to wpf. wpf may require you to learn quite a bit before you can do anything.
Summary
Essentially what this does is to create an offscreen bitmap that we will draw into first. It is the same size as the UserControl. The control's OnPaint calls DrawOffscreen passing in the Graphics that is attached to the offscreen bitmap. Here we loop around just rendering the tiles/sky that are visible and ignoring others so as to improve performance.
Once it's all done we zap the entire offscreen bitmap to the display in one operation. This serves to eliminate:
Flicker
Tearing effects (typically associated with lateral movement)
There is a Timer that is scheduled to update the positions of all the tiles based on the time since the last update. This allows for a more realistic movement and avoids speed-ups and slow-downs under load. Tiles are moved in the OnUpdate method.
If you note in the code for Timer1OnTick I call Invalidate(Bounds); after animating everything. This does not cause an immediate paint rather Windows will queue a paint operation to be done at a later time. Consecutive pending operations will be fused into one. This means that we can be animating positions more frequently than painting during heavy load. Animation mechanic is independent of paint. That's a good thing, you don't want to be waiting for paints to occur. xna does a similar thing
Please refer to my full SO answer complete with sample code
Here are a few hints to get you going:
First you will need to come to a decision about which platform to target: WPF or Winforms.
Then you should know what to move across what; a nice Bitmap or just a circle across an empty background or a Bitmap or a Form with controls on it.
In Winforms both your approaches will work, esp. if you set a circular region see here for an example of that. (The part in the fun comment!)
And yes, a Timer is the way to animate the sphere. Btw, a Panel or even a Label can display an Bitmap just as well as a PictureBox.
For smooth movements make sure to set the Form.Doublebuffered=true, if you move across a Form. If you move across any other control (except a PictureBox or a Label) you will need to subclass it to get access to the DoubleBuffered property!
It is often also a good idea to keep the Location of a moving item in a variable as a PointF and use floats for its speed because this way you can fine grain the speed and Location changes and also the Timer Intervals!

How to Draw simple objects using OpenGL with a Mouse

I am trying to design a small application that could draw simple objects like lines,ellipses,triangles, etc... with a mouse using OpenGL. There should be a GUI which user can interact with. I thought of design the UI with C#. But I have problems getting it to work with OpenGL library since I need to get mouse interaction to this.
Most of the available codes write the code and directly produces the output to a window when the program is run.
Is there any one that could give me a tip on how to achieve this?
With OpenGL, all the stuff to draw are outputed each frame. So if you plan to modify coordinates of an object, you have to use variables as coordinate while calling vertex3f(x,y,z). Update the values in your mouse event handler, and next frames, new values will be used.

Timer to appear on Game Screen

I have made a countdown timer using C# on basic form window and i want it to appear on game screen i tried to set form properties but it wont work on game screen.
how would it be done? Any Idea?
If you are looking to draw a timer into an existing DirectX or OpenGL backed game then I suggest you take a look at the following question which offers information about hooking and the ability to intercept calls along with working with the applications draw calls. However I believe this is above the level of complexity you would wish to encounter for adding a timer.
LINK : OpenGL/DirectX Hook - Similar to FRAPS
As you don't indicate if you are using OpenGL, XNA or what I will give you a generic response.
If it is your game
Your splash screen should be part of the game loop. This means that will have an update() and a draw().
In the update method you have to calculate the remaining time and store it in a variable or property of an object.
In the draw method you should paint the remaining time.
My recommendation is that you create a TimeCounter class because seems you are also going to reuse it during game.
If it is not your game
Most games will switch to full-screen direct-render mode. It is not impossible but pretty hard.
If the game is web, then you can wrap it in IFrame or similar and put the counter

Windows form rotation

When you create a form in .Net it appears as a dialog box in a portrait layout.
No one normally likes to read sideways, or upside down, but I have a very valid reason to rotate the form.
Anyone knows how to do it on Windows Vista with C#?
Does it have to be in WinForms? This is very easy to do in WPF, using rotation transforms. Unfortunately, the WindowsFormsHost integration with WPF does not allow rotation transforms.
EDIT
I understand, now, that the form in question is out of the control of the poster. Writing the control in WPF won't fix the problem.
This would be a bit of extra work, but if you mainly just need the contents of the form to be rotated (and not the entire window including title bar, window controls etc., which I've never seen before), you could instead make an entirely owner-drawn usercontrol that was rotated 90 degrees, and drop it on an ordinary form. You wouldn't even have to adjust your drawing of everything, since you could do a RotateTransform on your Graphics object and then draw everything normally.
Or if you need the entire form rotated, you could make the form borderless and then do basically the same thing, drawing the title bar and windows controls yourself also.
Update: here's a link to an MSDN article that shows how to rotate the entire screen in C#:
http://msdn.microsoft.com/en-us/library/ms812499.aspx
This is for regular Windows (not Windows Mobile), so it should work for your porpoises, although it will rotate all of Windows and not just your application's form. Depending on how fast this works and your overall needs, you could rotate the screen 90 degrees when your application gets the focus, and then rotate it back to normal when your app loses focus.
Update 2: I just reread your question and comments. You're talking about rotating the window of a separate application in a separate process, so WPF will definitely not help you here. The MSDN link might be what you need. In your application, you would rotate the screen 90 degrees, then start the other application in a separate process. This would work best if you could force the separate application's window to be maximized, which you can do by P/Invoking the FindWindow and SendMessage APIs (you could also make the window always on top, which would put your computer into a sort of kiosk mode for this application). There's a version of the Process code that basically makes starting another application a blocking call, which means your app will wait for the shelled application to close before resuming. Once the app closes, you can put the screen back to its normal orientation.

Categories