im sorry if im writing in weird ways. pretty new to programming, going the first year so I figured i could use some help..
im trying to get an "End screen" in my group and i have no idea really how to do it.
we have three levels and after the third and last level a screen should pop up like, "Do you want to play again/Exit?"
here's to my problem, how do I begin with just simply starting? I have tried myself and created a SpriteFont named "EndScreen" under Objects.
now later down in "draw(GameTime)" i did this:
" // Draws the Ending screen of game
switch (CurrentGameState)
{
case Gamestate.EndScreen:
{
spriteBatch.Draw(Content.Load<Texture2D>("Sprites/Endscreen"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
btnPlay.Draw(spriteBatch);
break; "
now i get the error: "unreachable code detected"
I would really appreciate If you could give me some step through step.
sorry if it looks bad and some type errors, I live in sweden and new to programming and this site! I also wonder if i did the coding right and putting the code on the right places, im very insecure about programming
you have an extra " after the break
Since break will jump out of the switch statement, the code path will never be able to reach this part of the code. Simply removing will fix this
void Update(GameTime g)
{
CurrentGameState = GameState.EndScreen;
}
I'm not going to give you the answer to the question you asked (#sayse already did that perfectly fine), but rather the question your code asked.
When you're drawing out your image you call the following code:
spriteBatch.Draw(Content.Load<Texture2D>("Sprites/Endscreen"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
This means every frame that your game draws that image to the screen it is attempting to load in a new instance of the texture at "Sprites/Endscreen." While XNA is able to realize not to load it, the process of checking if it already has been loaded is somewhat slow. You may not notce it immediately, but if you get enough images on a screen drawing like that you'll notice significant "lag" or drops in framerate.
A good solution to this problem would be to make a field (class member variable) at the top of your class that this code is being called in. Make it a Texture2D and call it something relevant like endScreen. Then in LoadContent load the texture into your endScreen object. Lastly changed your call to spriteBatch.Draw() to use that Texture2D. Below I have included an example of what you might want to do.
//Fields
Texture2D endScreen;
//Load Content
endScreen = Content.Load<Texture2D>("Sprites/Endscreen");
//All Your Class Code
//Draw
spriteBatch.Draw(endScreen, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
Related
Afternoon, Recently while trying to develop the more; Err Graphical part of my 'Editor Extension', Through towards unity3d's Audience I've been looking at creating more of a dynamic window for screen sizes!
Throughout my testing I've noticed many hard-core glitches that I cannot seem to persuade, Currently I've tried "doubles, int, float, etc.. etc..", Though my issue might just be with unity, I'm sure someone here has had this event occur while attempting to scale their "Editor Application", Here are some visual representations of the variable issues i've been having.
Error && Success Image Collection!
Upon my Journey of Dynamic Screen Fitting Here is some of my current attempted code that I have within my project for the window as of now.
private Rect Topic_Selection_Rect_Window = new Rect(5, 5, Screen.width / 6, 350);
static void Init()
{
EditorWindow Toolbox = GetWindowWithRect(typeof(Toolbox), new Rect(0, 0, 1250, 900));
Toolbox.Show();
}
private void OnGUI()
{
BeginWindows();
GUILayout.Window(0, Topic_Selection_Rect_Window, Topic, "Please Select A Listed Topic!");
EndWindows();
}
private void Topic(int id)
{
GUILayout.TextField("pre-text");
}
Now with the code shown above, We Have on the "Price rect topic_selection_rect_window", We have the idea that 'Oh, we can just use the API for 'Screen.width', Though initially i've noticed that the idea of that is knowing the users screen - information, Though the reality of it is I'm thinking that initally it's all wonky for inital load to get user-information which I need to entail an idea on how to attempt to get that upon init, Though i've tried hasn't seemed to work accordingly, Though once I load and it's all wonky and I exit and load again it's less wonky and one last time and it's fine; I've had some issues for some time such as "Screen.height" never seems to work and give me a good ## or Screen %% availability and just tends to not work with my monitor or what; I'm fairly new to programming and I'm looking to figure out how this API works, etc thanks for any
comments / answers!
-- DrEncompass --
After-Hours of tinkering, I'd have given up while messing around I later had an issue with my window not being able to have de-bugging and I had to re-boot Unity3d Every-time I had crashed my Editor-Window, So after some messing around I figured I could just directly call the window with the [.show();], And so far it's appearing to be a strong solution!
I currently work on a project in Unity 5. I am trying to apply a shader to one of my cameras using Camera.RenderWithShader, and after that read and save the image. Here is the code:
Texture2D screenshot = new Texture2D(this.screenWidth, this.screenHeight, TextureFormat.RGB24, false);
this.mainCamera.RenderWithShader(this.myShader,"RenderType");
screenshot.ReadPixels(new Rect(0, 0, this.cameraWidth, this.cameraHeight), 0, 0);
The problem is that, after I save the screenshot texture as a Bitmap, the shader is not applied on the entire image.
But if I use Camera.Render() and apply the shader using OnRenderImage(RenderTexture,RenderTexture), it works.
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination, this.disparityMaterial);
}
So, my question is: What is the difference between these two approaches and how can I make the Camera.RenderWithShader function work properly?
RenderWithShader and OnRenderImage are two completely different things and have nothing to do with each other.Read the linked manual pages for details and a better understand, but long story short:Prior is applying a shader to all (game)objects the camera can see without any image filters applied so basically it's about using a different shader for the same objects/prefabs/materials to alter something the way you want for the viewer (in your case, GOs should also have their tag set to "RenderType", otherwise the shader will not be applied on them),Latter one however is a "post processing" feature, applying filters only on images already rendered. I.e. an image effect feature.So a good use to prior one is e.g. nightvision on/off, or remove cloth from chicks with that special glasses the player can put hands on (mmmmm), etc while the latter one is clearly just image effects, e.g. a secret agent takes photos while one-finger-kills enemies, but when he gets hit, his equipment is more and more damaged, so photos taken as intel are getting more and more blurry, broken up and such - if that makes sense.
I working on a live stream App that receives JPEG image as arrays of bytes and displays it on the screen with UI.Image. It works fine but I am making optimization and have few questions. Currently, the code I have below converts arrays of bytes to Texture2D then creates a Sprite from the Texture2D then assign that Sprite to UI.Iamge to display on the screen.
Texture2D camTexture;
Image screenDisplay;
public byte[] JPEG_VIDEO_STREAM;
bool updateScreen = false;
//Initializing
JPEG_VIDEO_STREAM = new byte[20000];
camTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
//Main Code that runs in the Update function
if(updateScreen){
camTexture.LoadImage(JPEG_VIDEO_STREAM);
Sprite tempSprite = Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0);
screenDisplay.sprite = tempSprite;
updateScreen = false;
}
The code above currently perform 3 steps just to display image to screen.
byte array -> Texture2D -> Sprite -> UI.Image.
but I want it to look like byte array -> Texture2D-> UI.Image.
I want to write Texture2D directly to UI.Image without creating new Sprite because I believe that Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0); allocates new memory each time Sprite.Create called. I looked at the Unity Documentation and couldn't find any other way to do this.
My questions are:
How can I assign camTexture(Texture2D) to the screen screenDisplay(UI.Image) without converting camTexture(Texture2D) to Sprite first?
Does Sprite.Create allocate new memory when called?
If there is a solution to this, is that solution better than what I currently have in terms of performance and memory management?
Note: I have no plans on using OnGUI to draw Texture2D. I want to do this with the new Unity UI. Thanks.
Edit:
With Joe's answer of RawImage, the final code looks like this:
RawImage screenDisplay;
if(updateScreen){
camTexture.LoadImage(JPEG_VIDEO_STREAM);
screenDisplay.texture = camTexture;
updateScreen = false;
}
No more Sprite needed.
I think that by specifically using a RawImage rather than Image, one can do this.
I use RawImage extensively, because, we have to "display PNGs" and it's easier.
Consider the very handy trick:
just start with a trivial gray PNG which you have imported .. and then modify that .. rather than try to build from scratch?
An interesting curiosity I found is: normally to mirror an image, you just simply scale of x or y to -1. Unless it's been fixed, Unity has a problem where this won't work for RawImage.
// currently in Unity, the ONLY way to mirror a RAW image is by fooling with
// the uvRect. changing the scale is completely broken.
if ( shouldWeMirror )
rawImage.uvRect = new Rect(1,0,-1,1); // means mirror
else
rawImage.uvRect = new Rect(0,0,1,1); // means no flip
Another interesting factor. For this reason, many Unity projects still use (even 2017) the superlative 2dToolkit. It instantly solves issues such as this.
I'm working on some kind of mod for Terraria (written in C# and using XNA), in which I need to use some blend modes. I didn't have any troubles getting additive blending to work, but subtractive one causes me some problems.
I managed to display stuff with subtractive blending, but it doesn't really want to return to the standard mode. SpriteBatch.End and Begin doesn't help at all.
This is my custom BlendState:
public readonly static BlendState
bsSubtract = new BlendState{
ColorSourceBlend = Blend.SourceAlpha,
ColorDestinationBlend = Blend.One,
ColorBlendFunction = BlendFunction.ReverseSubtract,
AlphaSourceBlend = Blend.SourceAlpha,
AlphaDestinationBlend = Blend.One,
AlphaBlendFunction = BlendFunction.ReverseSubtract
},
Drawing code:
sb.End();
sb.Begin(SpriteSortMode.Immediate,bsSubtract);
(...drawing drawing blah...)
sb.End();
sb.Begin(SpriteSortMode.Immediate,BlendState.Additive);
The problem is, everything that is drawn after this code seems to still use some old options (half-transparent, bland). What am I doing wrong?
I even tried calling just sb.End() and sb.Begin() before setting the blend state back, or using another custom blend state which was a standard additive one, just with BlendFunctions set to Add, to no avail.
EDIT: Seems like setting ANY custom BlendState makes it do that...
EDIT2: Seems like the problem was me splitting the drawing to 3 separate places: one for item slots, one for tiles and one for world in general. And in one of these (items) I forgot to set the SpriteBatch before using and reset it afterwards. I should have spent more time looking at my code. Still, thanks for trying to help!
(can't close the question just yet, gonna close it after StackOverflow lets me do it)
The default blending mode is BlendState.AlphaBlend.
Try replacing BlendState.Additive with BlendState.AlphaBlend in your code. Or possibly NonPremultiplied, depending on what Terraria is actually using.
Better yet, you could read out exactly the blend state that Terraria was using, as SpriteBatch sets it on the graphics card and simply leaves it there. Here is some untested code that should do exactly that:
sb.End(); // Sets blend state
BlendState previousState = GraphicsDevice.BlendState; // Retrieve it
sb.Begin(SpriteSortMode.Immediate, bsSubtract);
// (...drawing drawing blah...)
sb.End();
sb.Begin(SpriteSortMode.Immediate, previousState); // Re-use it
Seems like the problem was me splitting the drawing to 3 separate places: one for item slots, one for tiles and one for world in general. And in one of these (items) I forgot to set the SpriteBatch before using and reset it afterwards. I should have spent more time looking at my code. Still, thanks for trying to help!
I'm trying to use the Sprite Class in Microsoft.DirectX.Direct3D to draw some sprites to my device.
GameObject go = gameDevice.Objects[x];
SpriteDraw.Draw2D(go.ObjectTexture,
go.CenterPoint,
go.DegreeToRadian(go.Rotation),
go.Position,
Color.White);
GameObject is a class i wrote, in which all the basic information required of a game object is stored (like graphics, current game position, rotation, etc)
My dice with Sprite.Draw2D is the Position parameter (satisfied here with go.Position)
if i pass go.Position, the sprite draws at 0,0 regardless of the Position value of the Object.
i tested hard-coding in "new Point(100, 100)" and all objects drew at 100,100.
I cant figure out why the variable doesnt correctly satisfy the parameter.
I've done some googling, and many people have said MDX's Sprite.Draw2D is buggy and unstable, but i didnt find a solution.
Thus i call upon Stack Overflow to hopefully shed some light on this problem!
Fixed
Yes sprite.Draw2D some time gives problem. Have u tried sprite.Draw its working fine for me.
Here is the sample for Sprite.Draw.
GameObject go = gameDevice.Objects[x];
SpriteDraw.Draw2D(go.ObjectTexture,
new Vector3(go.CenterPoint.X,go.CenterPoint.Y,O),
new Vector3(go.Position.X,go.Position.Y,O),
Color.White); and for rotation u can use matrix transform of sprite.