-
C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Hello all.
I have been programming for a while, all self taught. I usually learn via examples, so any sample code you may have will help tremendously.
I have an application that I am developing in C# (using .NET 2.0 and Visual Studio 2005 Pro, I have the free Express C# installed as well) and it is getting to the complexity level where GDI cannot keep up. It is 2D but lots of transparency. Because C# didn't support true transparency, it just drew the background alpha blended to the image, not the blend of all images below, I took things into my own hands and made it work. :) Thing is now it takes between 15ms and 400ms depending on how much stuff there is to refresh the image. It is double buffered as well by my own doing.
I think I need to move up to a better graphical interface, and I have never messed with anything like this. I do not know if I should use XNA, DirectX, or OpenGL. I am not attached to any of them.
From what I have read, DirectX is dead, and being replaced by XNA? I am not sure if "managed DirectX 10" is different somehow.
I dont need any 3D capabilities, so my choices are rather open I suppose. I want the application to support XP and Vista. So I think that rules out DirectX 10 correct? Is DirectX 9 still a viable option?
Also when I do "using Microsoft" then the "." and the predictive popup displays with the options, I do not have "DirectX', "XNA" or any of the others I have seen moentioned in places scattered around the internet. I am guessing I need to download a library and place it in the appropriate folder (what, where, and where to???).
So anyone have suggestions on a good start for 2D Graphics that are faster? Also how does this work as a whole? I have controls such as buttons in my C# form, I want this image to display in the back, so will this "voodoo magic directX/XNA/OpenGL" function kick back a bitmap/image that I can set as the background or something, or is this a whole takeover of the application where my regular controls will not draw themselves or work?
Thanks in advance for the answers.
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
You will have to add the references manually (Add Reference --> .NET Tab [should see them]) but before we go further with that lets clear everything up and decide which one you will be using ;)
Now if you want to use OpenGL you will be looking at using C++ though you can use it in C# but you will need to create a bridge class to the OpenGL DLL files. But what benefits does OpenGL? Well it can run on many different platforms and it is pretty fast. It is used more often in 3D Modeling than actual Game Development.
As for DirectX it can be easily adapted to use with either C# or C++ and it is very powerful. Though it won't run on every platform. But honestly you don't want to be developing games for Linux since it already runs awfully slow in full graphical mode. The syntax of DirectX is a lot easier to so it will be much easier for you to become competent in DirectX than OpenGL.
Now as for XNA it is basically one big DirectX wrapper that makes it even easier to use than it would be to directly use DirectX. But XNA is only supported by the C# IDE (which it seems like what you want to use :)) XNA also makes it a lot easier to develop for the XBox360.
So basically i would go with DirectX but XNA does look really promising.
DirectX 10 is supported on both Vista and XP i believe. I know Vista for sure but XP i am not 100% certain. And yes DirectX 9 is a very good option it is still the industries standard. Only until Vista becomes standard on majority of home computers will DirectX 10 be the mainstream.
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Thankyou for the quick reply. I actually realized how to add the reference, and then I started searching for more tutorials. I just made my first, DirectX9 Application. :) It takes over my screen and flashes random rgb colours with the .ColorFill.
Do you know if there is a way to get actual function descriptions from the "predictive popup" when you type for these DirectX9 functions? The only message for all of them that shows up is "This method is depreciated. Depreciated components of Microsoft DirectX 9.0 for managed code are considered obsolete. While these components are still supported in this release of Direct X 9.0 for managed code, they may be removed in the future. When writing new applications you should avoid using these depreciated components. When modifiying existing applications you are strongly encouraged to remove any dependancy on these components. Depreciated."
Sort of harsh language. :)
So should I start to learn what is new now being XNA since I will be doing C# mostly? I just need 2D support. Draw a few images to a back buffer, flip it, and that is about it. Not a game, just an application that needs fast redraw rates that GDI cannot provide. Also how does this effect the painitng of exisitng controls?
And on a side note, the code I am using works, but I am not entirely sure why. I tried changing a property, and I get an error at runtime.
"MyDevice.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive);" with "MyDevice" being the Microsoft.DirectX.DirectDraw.Device, and "this" being the current form, it works fine. But when I change the CooperativeLevelFlags to "Normal", it craps out on me when I try to initialize my Primary (front) surface with "PrimarySurface = new Surface(MySurfaceDescription, MyDevice);" with a "NoExclusiveMode Exception"
I am just guessing Normal is what I want, as I have no way to know what each of the options do, because all I get is the "blah blah depreciated, blah blah" thing. I dont want it to be FullScreen. And by exclusive, does that mean it is not allowing any other applications access to the video card?
As soon as I ran it, my laptop flashed the screen like 3 times completely on/off, then did what I programmed it to. As soon as I "alt+tab"'ed, I got an unhandled exception error (I havent protected the code at all obviously). So since the OS needed the card, and it was exclusive, it died correct. I suppose if I wanted fullscreen, I would do a test to see if the form had focus before each redraw, but I dont want it full screen.
Again any help would be appreciated.
And if it is helpful, my code is below:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
namespace DirectX_Test
{
public partial class MainForm : Form
{
public Microsoft.DirectX.DirectDraw.Device MyDevice = null;
public Microsoft.DirectX.DirectDraw.Surface PrimarySurface;
public Microsoft.DirectX.DirectDraw.Surface SecondarySurface;
public Microsoft.DirectX.DirectDraw.Clipper MyClipper = null;
int rgb_r = 0, rgb_g = 0, rgb_b = 0;
Random rgb_Random = new Random();
public MainForm()
{
InitializeComponent();
MyDevice = new Device();
MyDevice.SetCooperativeLevel(this, CooperativeLevelFlags.FullscreenExclusive);
InitializeSurfaces();
DX_Timer.Enabled = true;
DX_Timer.Tick += new EventHandler(DX_Timer_Tick);
this.FormClosed += new FormClosedEventHandler(MainForm_FormClosed);
}
public void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
//Kill the DirectX stuff...
MyDevice.Dispose();
PrimarySurface.Dispose();
SecondarySurface.Dispose();
}
public void DX_Timer_Tick(object sender, EventArgs e)
{
//Draw Here...
SecondarySurface.ColorFill(Color.FromArgb(rgb_r, rgb_g, rgb_b));
PrimarySurface.Flip(SecondarySurface, FlipFlags.Wait);
// Randomize new colour
rgb_r += rgb_Random.Next();
rgb_r %= 255;
rgb_g += rgb_Random.Next();
rgb_g %= 255;
rgb_b += rgb_Random.Next();
rgb_b %= 255;
}
public void InitializeSurfaces()
{
PrimarySurface = null;
SecondarySurface = null;
SurfaceDescription MySurfaceDescription = new SurfaceDescription();
Clipper clippy = new Clipper(MyDevice);
clippy.Window = this;
MySurfaceDescription.SurfaceCaps.PrimarySurface = true;
MySurfaceDescription.SurfaceCaps.Flip = true;
MySurfaceDescription.SurfaceCaps.Complex = true;
MySurfaceDescription.BackBufferCount = 1;
PrimarySurface = new Surface(MySurfaceDescription, MyDevice);
PrimarySurface.Clipper = clippy;
MySurfaceDescription.Clear();
MySurfaceDescription.SurfaceCaps.BackBuffer = true;
SecondarySurface = PrimarySurface.GetAttachedSurface(MySurfaceDescription.SurfaceCaps);
}
}
}
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Doing some more reading and apparently DirectDraw has been dead for a while, and people say to use Direct3D but in 2D mode somehow... And Direct3D has much improved hardware acceleration compared to DirectDraw from what I have read.
I would rather not learn a dead routine.
Also I am trying to get the XNA reference added to Visual Studio, but it does not show up in my list. I have downloaded the XNA SDK, and installed it. But I am not seeing the XNA reference listed. Is there an intermediary step I am missing you suppose?
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
directdraw was killed and replaced by
setting z to 0 and having a fixed camera in direct3d
as far as i know ms stop'd developing on managed directx
unless thats changed with .net3.5
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by ~Paul~
But honestly you don't want to be developing games for Linux since it already runs awfully slow in full graphical mode.
Just wanted to point out that this is very wrong. While I do prefer Windows, Linux has the ability to run games just as well as Windows. In fact, there are many games that run great on Linux (UT 2004 I believe is one) and even through Wine games like Half-Life 2 I heard can run pretty well.
Quote:
Originally Posted by 2k1Toaster
Also I am trying to get the XNA reference added to Visual Studio, but it does not show up in my list. I have downloaded the XNA SDK, and installed it. But I am not seeing the XNA reference listed. Is there an intermediary step I am missing you suppose?
For XNA you have to use Game Studio Express 1.0 rather than Visual Studio to get it to work (though there are some hacky ways around it). You could also install Game Studio 2.0 which allows you to use Visual Studio 2005 or C# Express to create XNA projects.
I think XNA may work well for you as it's very easy to learn and it provides a very friendly wrapper to DirectX. Though be aware there is an additional runtime required to running XNA games. Thankfully it's only 1MB but it still exists.
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by kasracer
Just wanted to point out that this is very wrong. While I do prefer Windows, Linux has the ability to run games just as well as Windows. In fact, there are many games that run great on Linux (UT 2004 I believe is one) and even through Wine games like Half-Life 2 I heard can run pretty well.
The vast majority of people i know don't run Linux in full graphical mode because it is worse off than Vista. Thats why Linux is primarily used for servers and for those who like using a command line interface (which is silly) since it takes 10 minutes to do a task in which someone on windows could complete in 2 minutes :D
@2k1Toaster: You need to complete the steps that kasracer mentioned in his post above!
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by ~Paul~
The vast majority of people i know don't run Linux in full graphical mode because it is worse off than Vista. Thats why Linux is primarily used for servers and for those who like using a command line interface (which is silly) since it takes 10 minutes to do a task in which someone on windows could complete in 2 minutes :D
I have never heard of any issues with the Linux UI and slowness. X Server is pretty snappy and OpenGL works well (check out compiz fusion or UT 2004).
The main reason Linux is used as a server OS is because it's customizable enough so that you can remove the overhead of a GUI. Windows Server 2008 will actually have this ability when it's released (and it's not because it can't handle the GUI).
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by ~Paul~
The vast majority of people i know don't run Linux in full graphical mode because it is worse off than Vista. Thats why Linux is primarily used for servers and for those who like using a command line interface (which is silly) since it takes 10 minutes to do a task in which someone on windows could complete in 2 minutes :D
@2k1Toaster: You need to complete the steps that kasracer mentioned in his post above!
That post is the most horribly inaccurate statement i've ever seen.
1) I think 99% of people who use linux on a home computer use a graphical interface.
2) If someone wants to use the commandline, it's because they can do things *faster* in commandline rather than in a GUI. The reason being is that linux treats the commandline as a 1st class citizen, not a 4th cousin twice removed.
3) Linux is not as popular as macos or windows, but it is a desktop OS and it is making headway (slowly).
4) DirectX 10 is not and will never be supported on WinXP. It's Vista (and above) only.
5) OpenGL is not 'more often used in 3D modelling rather than game development'.
http://en.wikipedia.org/wiki/OpenGL#OpenGL_Games
6) DirectX only runs on windows, OpenGL runs on everything. If you want to use openGL, you can use the Tao bindings. http://www.taoframework.com
Other than that, managed directx is dead. It's not complete and never will be. XNA replaced it. If you want crossplatform games, OpenGL is the way to go. If you don't care, then OpenGL or DirectX are both good options. It's really personal preference.
If you want to use XNA, you don't need Visual Game Studio - or whatever it is microsoft calls it. All you need are the XNA libs and just add them as references to your project in whatever IDE (or emacs ;) ) you use. But i think the only way to get those DLLs are by installing the Game Studio.
XNA is the best choice if you're not good at native interop and don't want to worry about memory management. It hides the nastiness away from you.
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by Mutant_Fruit
If you want to use XNA, you don't need Visual Game Studio - or whatever it is microsoft calls it. All you need are the XNA libs and just add them as references to your project in whatever IDE (or emacs ;) ) you use. But i think the only way to get those DLLs are by installing the Game Studio.
XNA GameStudio 2.0 automatically installs the libraries so you can use them in Visual Studio 2005 (rather than using the express verison) :)
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
linux's gui can be set to more eyecandy then vista with a much lower videocard and cpu requirements
http://www.youtube.com/watch?v=i9JC5NQ7G0o
2 reasons why games often run worse on linux then windows
is
1 well if they use wine then the wrapper layer will always slow things down compared to running dx directly
2 the 2 big video card makers ati and nvidia spend ALOT! of time optimizing their drivers for windows
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by kasracer
XNA GameStudio 2.0 automatically installs the libraries so you can use them in Visual Studio 2005 (rather than using the express verison) :)
Yup, I had downloaded 1.0 by mistake, and it didn't do anything really, uninstalled, and redownloaded v2.0 and right away it detected Visual Studio (Both the Express and Pro versions actually) and now I can code "Games" in Visual Studio.
It is definately difficult. Not like what I am used to at all. I think I am starting to get the hang of the very very basics.
As of now, I can initialize the device, setup an event handler for initialization, check for PixelShader hardware support, then if/else it into using either software or hardware support, finish initializing, and then using a pre-defined effects file make a triangle with faded colours. Also can make the background flash random colours using colourFill again, but that is sort of useless. :)
I am pretty confident that the gradient blending is something the effects file does. The tutorial I was using basically said, these are hard, download this and use it, the end. Well that is nice for drawing gradients between vertecies, but I need to but a *.png between verticies!
Does anyone have a bit of sample code for adding an image bmp, jpg, gif, png, to the same plane using XNA? I just need a fixed camera, with a 2D look. XNA is obviously way more capable, but I dont need that capability right now, just its speed of hardware access.
I read somewhere that DirectX doesn't like compressed images like a jpg... Is this true of XNA as well? So how does one make it support them? Most importantly transparency such as png's.
Thanks for all the help so far.
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Jpegs and Pngs "just work" in XNA (that includes transparency in Pngs) :)
I'm not too sure of some of your questions but http://creators.xna.com/ is the best site for Xna information. There are camera tutorials there as well.
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Woohoo! :D
I got a red circle loaded from a png, to display with the correct transparency and all.
I am happy.
Now I need to find a way to convert from any filetype to *.xnb at runtime... I will have a folder of images, that change all the time, so I will need to load in every image, and compile to xnb format. I know there is a way, because Visual Studio does it as soon as you add them to your project. I need to capture that function wherever it may be...
Any ideas?
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by 2k1Toaster
Woohoo! :D
I got a red circle loaded from a png, to display with the correct transparency and all.
I am happy.
Now I need to find a way to convert from any filetype to *.xnb at runtime... I will have a folder of images, that change all the time, so I will need to load in every image, and compile to xnb format. I know there is a way, because Visual Studio does it as soon as you add them to your project. I need to capture that function wherever it may be...
Any ideas?
Nevermind, I sort of solved it.
For a while the Texture2D.FromFile() would not work. It is because the default content loader directory is not where I put the images, and then I tried to change the folder as seen in 1.0 examples, but this is now only read only I guess? Anyways, changing all FromFile calls from "blah.png" to "Content\\MyFolder\\blah.png" works to load in a file from scratch.
Now a bit different of a problem, the only image that stays, is the last image loaded. This says to me I am overloading something. Probably the spritebatch... more research is needed, but I feel I am getting close.
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Ok, I have images drawing in the game projects, and everything works.
Now I need to take this functionality and put it in a regular C# project (The kind that extends Form, not Game).
It seems like the hard part is initializing, the graphicsdevice. Any ideas, or tips?
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Well I gave with getting it to work in a :Form instead of a :Game, and am porting my app over to it.
Anyways, is there a way to speed up mouse response? I assume it is something in my code. Is there a standard way of doing it? I click a button, and it takes about 1seconds to reflect a change. The screen is being updated in 1ms at most and quite often. So I think it is the actual mouse event capturing.
I am creating a system.windows.form.form from the handle pointer of the game class. From there I can change the formborderstyle, and such. So I setup .mousedown and .mousemove events on that form, but they seem to have a delay.
Bettery ways?
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Why not manually handle where the mouse is on the screen? Xna doesn't really have events but in the update method you could check where the mouse is and if the button is down.
Also, I thought I heard someone from the Xna team discussing a way to put a game onto a form. Try searching for it as it may be a new feature of Game Studio 2.0
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
It is definately not "supported" at all! You can create a graphics device and it seems to work, but nothing displays...
Perhaps I didnt override the Onpaint command correctly.
Anyways, there are no actual buttons as defined by system.windows.form. They are images of buttons as Texture2D painted onto the XNA layer. Then I use the mouse to check the bounds of the button and the position of the mouse.
It just seems much slower that I thought it would be. Everything is really fast, but the button changing visual appearance to reflect being clicked is slow... really slow.
If I try to draw a real button on the xna device with a transparent back colour, it just shows black, not the XNA background. So this is why I need to do everything in the XNA area.
Also regarding text, is the only way to do it via images? Is there really no way to draw text?
-
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Quote:
Originally Posted by 2k1Toaster
Also regarding text, is the only way to do it via images? Is there really no way to draw text?
Drawing text is very simple. In the early betas, there was only TextureFont so you had to use images but you can use a SpriteFont instead.
Just add a SpriteFont to your project, open it and adjust the font name, size, and style. Then you import it with the content and you can use the DrawString method of a Spritebatch.