I'm working on an application that uses the XNA framework to do it's 3D rendering. I now want to load a texture from file. I've found two methods so far:
Texture2D.FromStream(GraphicsDevice, Stream) The problem with this approach is that it only loads gif, png and jpg and I also need support for tga images.
Create a ContentManager object. The problem with this approach is that it seems like all the textures need to be statically added to the project, from the documentation: "Before a ContentManager can load an asset, you need to add the asset to your game project". The program in question is a level editor and which textures are needed isn't known in advance.
Is there any other easy way to load the texture, I'm thinking about using some other class to load the image (although I don't know which, I'm not very familiar with C#) and then perhaps use the Texture2D.SetData method?
Is there any other easy way to achieve what I'm trying to achieve?
There are a few ways to achieve what you want:
You could invoke the content pipeline from within your editor, dynamically creating your content project. How to do this is described in the WinForms Series 2 Sample. This is probably the "best" way, because it allows you to keep using the content pipeline.
You could, as you say, decode the TGA file yourself and use SetData. There are lots of results for C# TGA readers on Google. This is the first one.
I've used Texture2D.FromFile(device, path) before, and it works well. However occasionally I'll encounter problems and will also have to specify TextureCreationParameters and pass them in. Keep in mind that you'll need to dispose the loaded Texture2D manually.
Related
I'm looking to add ebook support (.pdf .txt .epub .mobi .rtf Support) to a game I'm making in Unity using C#. Thing is I really do not know where to start when it comes to this and most of my google searches have gotten me nothing but Ebooks about programming or game development. So I'm hoping someone here would have a good idea where I could start and/or information that would help set me in the right direction.
So just to summarize my comments on the OP:
With the least amount of work, embedding a web browser control like Webkit is probably the best option. It should properly read all the common filetypes you mentioned, save for .epub and .mobi. A separate library or control will need to be obtained or coded for those to work. Additionally, if the user already has a default program set up to open those filetypes, you can open them outside of the game with Process.Start(...) which is part of the System.Diagnostics namespace.
If it comes down to you having to code this yourself, a PDF is just drawn graphics on a canvas, txt is just raw text data, and an rtf is text data with some markup to get the formatting right. Coding a component that opens those for you should not be abnormally difficult.
I have a number of web controls, which are made up of png images. The simplest is a button.
I need to be able to generate these controls with different colours depending on the colour selected by the client.
The images are .PSD files, layered before exporting to png.
My idea was to allow the client to pick one colour and use a layer filter in the psd to change the overall colour of the image and programmatically export the .PSD to PNG on the server. I looked into using the Photoshop CS Interface via COM, but haven't got my head around it, has anyone else used it for a similar task?
Alternatively I could read the png into memory and perform colour replacement, but this seems really complex for what reads like a simple(ish) task.
Many thanks in advance
.PSD is quite complicated and poor documented file format, that is constantly receiving new features from Adobe, so editing them is no way an easy task.
One way is to use Photoshop batch processing, which means photoshop installed on server, but as long you you wished to make that through COM, it should not be a problem.
One of the starting points may be: http://www.webdesignerdepot.com/2008/11/photoshop-droplets-and-imagemagick/
Another way would be to try composite layers using c#, that means you would have some layers ready (textures/borders/etc), some would be created at runtime and all those layers would be merged at runtime using c#.
I asked a similair question earilier, but it was not generic enough, so here's another attempt.
I currently have a whole bunch of 3d .obj and .jpg files which I want to display in my WPF application. However, I want to do this at runtime, so converting them using Blend is not an option.
I don't mind having to convert to other types of 3d files, as long as this can be done at runtime (either programmaticly or by using a command line tool).
So I guess my question is: Is there any way to import a 3d file in WPF at runtime?
Thanks!
I don't think there's a built in way. You'll most likely have to write an importer for your own specific needs. You can find some file format information here> http://en.wikipedia.org/wiki/Wavefront_.obj_file
Thanks to #MattDavey
I managed to get The HelixToolkit (http://helixtoolkit.codeplex.com/) to work and got what I wanted using the ModelImporter.Load method.
I have been trying to create a decoder that will stream through a pcx file and display it on screen as a bitmap. I have managed to get the information from the image header by using a binary reader, but I have now reached the part that seems to take the least amount of code, yet is also the hardest: creating an array of pixels.
I understand that i may need to add two embeddded for loops to process the data. I have looked at some C and C++ examples, but struggle to understand them. I also need to get the array to display it. if you need more code then I will share it.
I have searched far and wide and read the spec, but I don't know how to approach this. If anyone could help me, I would be very grateful.
Regards.
The .NET does not support PCX images natively, you have two choices. Read the specification and decode the image by yourself or use some library.
As suggested on bytes.com you can use Dot Net Fireball (a Free Image wrapper) and load the image like this:
Fireball.Drawing.FreeImage freeImage = new FreeImage(#"c:\test.pcx");
Image image = freeImage.GetBitmap();
http://magick.codeplex.com/
a nice wrapper working with http://imagemagick.codeplex.com/
easy to setup and get going, see samples at the bottom of the page here:
http://magick.codeplex.com/documentation
I am planning on building a 2D game library for XNA, and one of the components I want to include is a simple text drawer for debug purposes. Now, to draw text with SpriteBatch you need a .spritefont file, which is an xml format file, and these seem to need to compile into a separate folder. I would prefer not to have to copy that around with the dll, so here is my question:
can i build some sort of text renderer for XNA that
A: does not require me to carry around external files with the dll (if you can embed the sprite font into a dll then that works) and
B: does not force me to rewrite a fair amount of underlying (is it managed directx? a different part of xna?) code that makes the SpriteBatch.DrawString code work.
Could you not just require that a SpriteFont be passed into your library, so that whoever is using the library has to supply that component? That would seem to be the best solution, since then they could use whatever font they wanted. Or you could write in a component that generates the spritefont xml file based on a given font name, because it's not all that complicated of a file. Disregard. I forgot that XNA compiles its resources.
You can precompile the project and then grab the xnb file for the spritefont. Then add the file to the project Content folder as a content file. It should then be deployed together with the library. However u will need to build a separate xnb file for each platform you wish to support (xbox360, windows, zune) and deploy the correct file.
Or why don't u just create a XNA Content Library project?