|
-
December 17th, 2007, 03:55 AM
#1
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.
-
December 17th, 2007, 04:57 AM
#2
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.
If this post was helpful please rate it
If it wasn't for C, we would be using BASI, PASAL and OBOL.
-
December 17th, 2007, 05:30 AM
#3
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);
}
}
}
Last edited by 2k1Toaster; December 17th, 2007 at 05:33 AM.
-
December 17th, 2007, 05:42 AM
#4
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?
-
December 17th, 2007, 07:21 AM
#5
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
-
December 17th, 2007, 01:12 PM
#6
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
 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.
 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.
-
December 17th, 2007, 08:55 PM
#7
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
 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 
@2k1Toaster: You need to complete the steps that kasracer mentioned in his post above!
If this post was helpful please rate it
If it wasn't for C, we would be using BASI, PASAL and OBOL.
-
December 17th, 2007, 09:19 PM
#8
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
 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 
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).
-
December 18th, 2007, 03:15 AM
#9
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
 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
@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.
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
December 18th, 2007, 09:21 AM
#10
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
 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)
-
December 18th, 2007, 09:23 AM
#11
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
-
December 19th, 2007, 08:27 AM
#12
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
 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.
-
December 19th, 2007, 09:04 AM
#13
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.
-
December 19th, 2007, 09:55 AM
#14
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
Woohoo! 
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?
-
December 19th, 2007, 10:04 AM
#15
Re: C# 2D Images, GDI too slow, XNA/DirectX/OpenGL???
 Originally Posted by 2k1Toaster
Woohoo!
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|