473,382 Members | 1,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Texture not displaying in my XNA game app.

kiseitai2
Today, I got back into coding my first video game. I had started this project a year ago, but took a break soon after to think more clearly about my design approach. However, while fixing some stuff and cleaning my code, I stumble upon a critical runtime bug. The problem is that the XNA framework, which is the library set I'm using, can draw the textures of my game objects when using the Draw() function inside the Game class. This is not true when I tried using a reference to the SpriteBatch object inside the Game class and begin drawing textures from an external class. As a result, I now have a 2D world without the characters. I can still trigger some functions, but it is as if it was aghost character. Here's a snippet:
Expand|Select|Wrap|Line Numbers
  1. public void Draw(SpriteBatch Spritebatch)
  2.         {
  3.             Spritebatch.Begin();
  4.             Spritebatch.Draw(herO, heror2, heror, Color .White );
  5.             Spritebatch.Draw(smoke, smoker2, smoker, Color.White);
  6.             Spritebatch .End ();
  7.         }
The above is the Draw function inside the hero class, which is called in the Draw function of the Game class.
Furthermore, there are no compiler or runtime errors detected by the debugger.
Expand|Select|Wrap|Line Numbers
  1. public void LoadContent(ContentManager Content)
  2.         {
  3.             herO = Content.Load<Texture2D>("Graphics\\char\\FlameAbuser\\Fireb");
  4.             smoke = Content.Load<Texture2D>("Graphics\\objs\\particles\\smoke");
  5.         }
The above is the code I used to load the textures for the hero class (this function is within the hero class). Thanks for your time and I hope you can help.
Dec 21 '10 #1
25 4257
GaryTexmo
1,501 Expert 1GB
Hmm this is strange, I do what you are trying to do all the time. Pass the sprite batch along to another class and use it to draw.

Can you please verify the texture itself is valid? Maybe something happened and it's not loading correctly? You'd think you'd get an exception, but maybe it's drawing and just getting a blank image? Actually you did say it worked in your game class...

Can you please post some more complete code? I know this works because I do it all the time, so maybe something else funny is going on. I just need to see your source to help you debug it.
Dec 21 '10 #2
Thanks for the quick reply. I will double check that the target texture can be drawn in the Game class. The particular texture I will be testing could have gotten messy when I applied some new compression settings today. I'll post back as soon as possible.
Dec 21 '10 #3
The texture actually draws when done inside Game. I included the two files in question, but could not include the whole project since it was bigger than the maximum upload space.
Attached Files
File Type: zip partial_source.zip (8.6 KB, 96 views)
Dec 21 '10 #4
I tried making the texture in hero public so I could draw it from Game and the same problem is happening. This tells me the textures are not being loaded when the LoadContent function in the hero class is called.
Dec 23 '10 #5
It seems that Content (a ContentManager instance) is null, but it is the same object that XNA 2.0 provides when a new project is created. If it's null how can it even load content in Game? This problem is giving me headaches. It actually violates any logic I come up with.
Dec 23 '10 #6
GaryTexmo
1,501 Expert 1GB
Sorry for the late replay, I wasn't checking the board over Christmas. Speaking of which, I hope you had a great one and are still keeping an eye on this thread ;)

Before I take a look at anything (and don't worry, I'm quite happy to), lets try a different approach first.

Can you please create a new project and try to get this working on a smaller scale? When I encounter problems like these I tend to like to step back and reduce the problem to just the problem. If I can reproduce it on the smaller scale, I've got a much smaller problem space to work in which goes a long way towards debugging. If I can't reproduce it, the odds are good that something has gone funny in the main program, which may give me a few ideas on where to look.

As for what you've described, that's kind of strange. You've verified that in your main program, this.Content is null? I find that rather strange... that's supposed to get initialized as a part of the Game class.
Dec 29 '10 #7
I took a break from the project, but will try your suggestion. In respect to the Content Manager, it gave me this exception when I played around with the code to try getting the textures loaded. Since I gave up a few days ago, I cannot remember what was it I did (I undo changes quickly when a new idea isn't working) then. I did confirm that the Content instance is not being passed to the other class and that the textures are not loading. It's kinda like if the debugger does not through exceptions for parameters in this case (really weird). I had a nice warm Christmas and will keep you updated as soon as I single out the problem in a test solution.
Dec 30 '10 #8
I did what you suggested and the test project actually draws the texture. However, the texture's main color was changed for the background color I specified in the spriteBatch.Draw() method. Nonetheless, this result tells me that the missing texture in my project does exists and it is probably hidden by a background texture. (I the background textures I made have contrasting colors so I don't think the problem has to do with color swapping when drawing the target texture. Thanks for the help and I'll post back when I play with the code to bring the texture to the front.
Dec 30 '10 #9
I'm back to square one.
Dec 30 '10 #10
GaryTexmo
1,501 Expert 1GB
I didn't know you could draw a background colour on a texture, did they add that? I know you can mask it, but that's it...

Actually I just checked the overloads on Draw and I have no idea what you're talking about.

Can you post the source for your demo project and include the sample textures? Make sure you clearly let me know what you're expecting to happen and what is happening.

I need something a bit more visual to see this I think :)

*Edit: I'm looking at the other source code you posted... you are passing it along so yea, I'm not sure why that's not working. When you post the sample project I'll play around and see what I can find out.
Dec 30 '10 #11
I guess it is a mask. It's the Color.Color part of the Draw function. I cleaned up the project from some files that are not important so that I could make it up-loadable. Also, when you try to compile it, you may get a lot of warnings about old variables that I am not using anymore, but still left in case I were to use them in the future. Just ignore them. Thank you for the time you have put into helping me.

P.S.-> if the code was working right, you would expect to see a fire ball in the center of the first screen. Also. if you move for a few minutes to the right, you will trigger the create new game screen.
Attached Files
File Type: zip Nameless.zip (2.14 MB, 99 views)
Dec 30 '10 #12
GaryTexmo
1,501 Expert 1GB
Sorry, that's your whole source, I was just looking for your test project where you were able to reproduce the strange behaviour. I can't actually open your source file, I don't have the right version of VS I guess and mine won't convert it.

So I'm supposed to see a fire ball on the screen, and it sounds like it does that, but you were saying the texture doesn't display? I'm confused...

Also, just so I'm clear, the general idea is you want to pass a ContentManager object to another class and have it load an image, right?
Dec 30 '10 #13
1) Well, I'm using VS 2005 Express (this is so I don't have to reference manually to .Net framework 2.0 and xna 2.0).

2) The main character starts as a fireball, which is what I used at the title screen in the regular project. In the test project. it just displays the whole sprite sheet. Also, what I meant with the texture not displaying is that the fireball should have appeared in the center of the title screen in the regular project. I know this, because I used to have the code in the main class, but the problem appeared after I moved the code into its own separate class, which I did to make the whole project more organized.

3)Yes I want to pass a ContentManager object to load the textures, because that is the only way I know of initializing a Texture2D variable. I also want to pass a SpriteBatch object afterwards to enable drawing of the loaded textures from the other class (not Game1/ main class)

I attached the test project, which has same structural pattern but actually loads and draws textures from classes outside Game1/main.

Run the compiled exes in this sample. One will show the fireball, while the other does not. The one that doesn't (which should have) is the current build of my regular project.
Attached Files
File Type: zip BugCatcher.zip (969.8 KB, 87 views)
Dec 31 '10 #14
GaryTexmo
1,501 Expert 1GB
Thanks for posting this, I'll take a look in a few days or so. Sorry for the delay, but I'm on holidays again and busy for the next few days.

Since you're running in vs2005 with .net/xna 2.0, I'll probably have to do some forward converting. Just so you know, both .net and xna are up to 4.0 and you can get a free copy of visual studio express 2010 :)
Dec 31 '10 #15
Don't worry. Take as much time as you like.I know the current versions, but I feel comfortable working with .Net/xna 2.0 and I do it for increased compatibility. Enjoy your holidays.
Jan 1 '11 #16
GaryTexmo
1,501 Expert 1GB
Ok, I'm back...

I'm unable to run the compiled version, it crashes for me. I'm going to guess it's because I don't have the runtimes installed for 2.0; however, I think it's safe to say that since your test project works for me and for you, you've done something funny in your main project.

As we suspected, it's just fine to pass the ContentManager along to another class and have it load something, so we've got something else going on. My usual process here is to start commenting out code in the main project to direct the flow and boil it down to something simpler. See if you can isolate the program flow that generates this problem and identify any issues.

Check to make sure the texture is being loaded into your Content and ensure the name is correct. You said it will work if you load it from the main project and the issue only happens when you load from the Test class?
Jan 6 '11 #17
Let me make a diagram to illustrate how so far the problem has occurred:

main proj = Main
test proj = Test

Main
| ************ |
Game1 ****** hero
| ************* |
Draw()<--------- Draw()= fail

///////////////////////////////////

Main
(all code in the same beginning class)
|
Game1
|
Draw()= works
////////////////////////////////////

Test
| ************ |
Game1 ******* hero
| ************ |
Draw()<---------Draw()= works.
///////////////////////////////////

I'm going to try commenting the code out.Also, I'm not sure if I gave you an outdated piece of code, because I had to comment out a part that required a test video to load around the time when I uploaded the code sample. I uploaded an updated version that should run an show you what is displayed so you have an idea.
Attached Files
File Type: zip Nameless.zip (2.10 MB, 114 views)
Jan 9 '11 #18
GaryTexmo
1,501 Expert 1GB
I still can't run your code, and I don't really want to rebuild your project to update it for the latest XNA.You should really do this as most people will will be running .NET 4.0 and XNA 4.0 anyway. It's generally best to keep up with the latest versions where you can.

Anyway, your version of XNA isn't really the problem here.

Though honestly, I'm having a hard time understanding what is. I'm also having trouble keeping track of what the problem is. When we boiled it down to the test scenario, everything worked fine so it's definitely something within your project's code. What that is, I think you're going to need to find on your own. We've shown that the general idea of what you're trying to do is sound, so it's just something you've done in there.

What confuses me is what's happening. Earlier you said the image just didn't draw, then later you said you were getting an error on the texture load. Are you changing what your Texture2D object refers to at any point? I'm reasonably sure that the Draw method will throw an exception if it's null, so if you're not getting anything out of your Draw it must be sending an empty image.
Jan 10 '11 #19
I'm thinking along those lines too, for I tried your suggestion of commenting out some of the code and the output is the same. So I don't think the other draw calls are interfering. Is xna 4.0 radically different from 2.0? I'm thinking of updating to that and checking out how it behaves. Maybe by moving the code to a newer version of the framework and modifying the code to fit it, I will unknowingly fix whatever was giving me trouble. Thanks for everything.
Jan 11 '11 #20
GaryTexmo
1,501 Expert 1GB
From the standpoint of a simple 2D game I don't think it's that different. There might be a few changes in the Draw overloads, but nothing that's difficult to deal with.

I'm out of ideas for how to fix your problem, but maybe a general suggestion?

I notice you're letting your hero class load its own textures in itself by giving it the content manager... this is a different approach than I usually take. I'm not saying my method is better, but I'll describe it so you can consider it.

Basically, I create a sprite manager class and load all my game's sprites into that. It's usually stored in a hash table where the key is a string name for what I'm looking for and the value is the Texture2D object. Then, to use it, I might do one of two things...

1) If I'm concerned about performance, I'll pass the sprite table to the object in the constructor and copy references to local objects.

2) If speed isn't an issue (and keep in mind, for this kind of lookup in a 2D game it usually isn't), I'll either give the object a reference to the sprite table (again, via the constructor), or pass the reference to the object's draw method.

That way I load all my sprites in one place where I can find them if i need to do any modifications.
Jan 11 '11 #21
Rabbit
12,516 Expert Mod 8TB
I'm jumping in out of the blue but shouldn't this
Expand|Select|Wrap|Line Numbers
  1. Spritebatch.Draw(herO, heror2, heror, Color .White );
actually be this?
Expand|Select|Wrap|Line Numbers
  1. Spritebatch.Draw(herO, heror2, heror, Color.White );
Note the spacing issue.
Jan 11 '11 #22
Rabbit
12,516 Expert Mod 8TB
Actually, that wasn't the only occurence of that. I don't know C# syntax but you have multiple occurences where the object and its method is separated by a space. There were about 5 such occurences in the code.
Jan 11 '11 #23
GaryTexmo
1,501 Expert 1GB
I saw that but thought it was a typo and didn't give it any thought. I didn't think to look in the code and see if it was there.

I don't know if you can do that either, I thought it would generate a compile error which is why it went out of my head immediately afterward... I'll be amused if that's the issue.

Nice spot, Rabbit!
Jan 11 '11 #24
Rabbit
12,516 Expert Mod 8TB
I thought so too. I don't think the code would compile as is. So I don't know where the disconnect is between what he's posted and how he was able to get it to compile. Perhaps C# handles white space differently and you're allowed to do that, or the code he attached is different from the code he's compiling, or it's some setting in the compiler to ignore errors.
Jan 11 '11 #25
That's in my code alright. But when using the code browser that appears when accessing functions and variables of classes, the code ends up with the extra space. I sometimes delete the extra spaces, but if I'm focused on figuring out why something is not working as intended or how to make a function more general for the program I totally ignore them. Furthermore, I don't get compiler errors and I thought they are normal, because in C++ the compiler would ignore most white spaces.
Jan 13 '11 #26

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mark L | last post by:
I am trying to create a graphics engine using OpenGL and PHP. I am currently trying to create a completly white texture to test out the texturing capabilities. My idea is to create an array...
2
by: Maarten | last post by:
Does anyone can help with this? For our goose game, we have to create the game board in a spiral. It may be a square instead of an oval or circle. Does anyone know a simple formula to do this...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
8
by: cody | last post by:
Is here anybody who has experience with GC in games or selfrunning demos? Is the GC pausing the game noticeable? If yes, is it possible to preallocate almost everything so that GC only needs to run...
3
by: Xpyder | last post by:
hey - i am developing a game editor in C++/DX and have encountered a problem. when i create map sturcture array (like below) to represent tile textures: o,1,o, 1,1,o, 1,1,1, 1,1,o,
1
by: Myk Quayce | last post by:
I have a four-sided polygon that rotates and zooms as the user moves the mouse. Is there a way to incorporate a texture-mapped image without DirectX? The GDI+ doesn't seem to support this. -- ...
3
by: x | last post by:
Still fairly new at this, I have been trying to find out how to compile and effect with multiple samplers. Been able to find loads of examples that show the HSL code once the samplers are...
2
by: =?ISO-8859-2?Q?Rafa=B3?= Maj | last post by:
Say we have a class Model (a 3d model) and class Texture (representing an Texture). Model have a texture, Model can be Drawed. Texture have method that returns data needed for drawing. class...
0
by: ...:::JA:::... | last post by:
Hello, Is there any real easy example for loading an texture in your directpython window??? For example this is my code: # loading directpython modules import d3dx import d3d from d3dc...
0
by: brixton | last post by:
Hello, I've got the following code that creates a texture from a .RAW file: GLuint MyGLCanvas::LoadTextureRAW( const char * filename, int wrap ) { GLuint texture; int width, height; ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.