Tag Info

Hot answers tagged

35

I have sort of replicated the behaviour of SpriteBatch in deferred mode for a cross-platform engine I'm working on, so here are the steps I have reverse engineered so far: SpriteBatch constructor: creates a DynamicIndexBuffer, DynamicVertexBuffer and array of VertexPositionColorTexture of fixed size (in this case, the maximum batch size - 2048 for sprites ...


9

Just want to point out that the XNA SpriteBatch code has been ported to DirectX and released as OSS by a previous member of the XNA team. So if you want to see exactly how it works in XNA here you go.


9

Here's the problems you are having: Sorting for sprite batches only applies within that batch. At End, everything in the batch gets drawn to screen and becomes pixels. The default depth state for SpriteBatch does not read the depth buffer. State objects (eg: SamplerState) are read-only once they are used. In this case, you're accessing the state that the ...


8

SpriteBatch will change the following render states. Even if you don't specify any in your Begin call - it will use its own defaults: GraphicsDevice.BlendState = BlendState.AlphaBlend; GraphicsDevice.DepthStencilState = DepthStencilState.None; GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; GraphicsDevice.SamplerStates[0] = ...


8

Technically, "batching" is putting multiple actions and their data into one data structure so it all can be executed at once rather than individually. The biggest bottleneck of modern GPUs is not their working power, but the communication between your game running on the CPU and the GPU. Each package of data sent to the GPU has an overhead, but the overhead ...


8

You need to use SpriteFont.MeasureString. Vector2 textSize = mySpriteFont.MeasureString("Hello World"); Vector2 center = textSize / 2; The x component of textSize represents the width of the measured string while the y component represents the height.


7

You can draw a line using sprites. SpriteBatch.Draw(...) helpfully allows us to strech and rotate a sprite(texture). In this code, we take a 1x1 pixel texture, strech it (by defining a rectangle of the correct shape and rotate it so it looks like a line. Texture2D t; //base for the line texture protected override void LoadContent() { ...


6

Yes! Let's have a look at how SpriteBatch works: SpriteBatch draws sprites onto a 2D plane. So if you had wanted to rotate your sprites so that they come "off" this 2D plane, then you'd probably have to find another solution. But, looking at your video, it looks like you want to rotate the entire 2D plane. SpriteBatch's normal mode of operation uses a ...


6

If you don't wish to make the jump to vertex arrays and a VBO just yet, there are some simple changes you can make to get more efficiency from even immediate mode. Yes, immediate mode is still the slowest option, but it's not THAT slow - Quake used immediate mode back in 1996 and despite that it will get you well over 25 fps on modern hardware. So bear ...


5

The relevant part of that stack trace appears to be the message about not being able to find the android.png file. Since you assert the file does exist on-disk, the problem is likely an issue with the working directory of your application at the time you attempt to load the file and the file's location relative to that working directory. I'm not entirely ...


5

The first problem is that you're using SpriteSortMode.BackToFront which to put it simply, is not appropriate for this case because you're not even specifying the depth of your sprites. Just use the default mode instead which draws in the same order as your calls (just write spriteBatch.Begin() with no parameters). Also, it's strange that you're drawing the ...


5

Use whatever feels more practical to you, and don't think too much about it. There's really no best answer to this, because there are so many different ways of doing it, but all of the possibilities work just as well. Personally I don't think it's worth bothering with GameServices, so I prefer either passing my dependencies or making them static public. ...


5

Sprite batch is implemented with 'camera-facing polygons with Z set to zero'. IIRC it even has rotation, and if it doesn't, you can supply your own transformation matrix. Your choices are equivalent. Ask yourself how much work you want to do duplicating spritebatch instead.


5

If you are using XNA 4.0 you can use shaders with any SpriteSortMode. To do so you pass an Effect to one of the overloads of SpriteBatch.Begin that accepts it as a parameter. In XNA 3.1 and earlier Immediate was the fastest mode. In XNA 4.0 it is the slowest. SpriteBatch uses a dynamic vertex buffer internally. It is pretty much the best strategy for doing ...


5

It's not a tightly-defined technical term. Batching is basically any system where you perform multiple operations as a set rather than individually, and often this is done because it's more efficient to do so. The efficiency gains usually come from being able to re-use some or all of the context that the operation requires. So, sprite batching is just any ...


5

Sprite batching is the method of submitting multiple sprites to the GPU with a single draw call. Neither of the other answers point out the real point of doing this: minimizing state changes and GPU command submissions. If you have 100 sprites I draw, each with their own texture, the hardware can only process one sprite at a time. The dozens or even ...


5

You want to set the texture's TextureWrap setting to Repeat. See the documentation for more information, and the texture method. Specifically: setWrap(Repeat, Repeat);


5

This can be done in two simple steps: Pass your custom transformation to SpriteBatch.Begin but... Attach a Matrix.CreateScale(1,1,0) at the end to flatten the quad and ensure it's fully visible. Note however that the transformation matrix is relative to the world origin not the sprite's origin, so it might need some tweaking to get the result in the ...


5

If your interest is in making games and not game engines, I suggest you use a game engine. Game engines are libraries which solve some of the most frequently encountered problems, like the one you are having. There are many, but if I may suggest: Slick2D at http://slick.cokeandcode.com/. It is a very simple engine, but it lets you get started quickly and ...


5

Roy T. suggested to fix this changing the texture filtering settings, On LibGDX this can be found on Texture.setFilter or changing the Texture Params if you are using the AssetLoader. Set the filter params to TextureFilter.Linear and the problem should be fixed. For more on the matter, take a look at ...


4

The way I handled this in my simple textbox UI element was to use a scissor rectangle (as Nicolas Bolas said while I was writing this :P). Here's the basic idea: First off, create a RasterizerState field in whatever class does the drawing of the text like so: RasterizerState _rasterizerState = new RasterizerState() { ScissorTestEnable = true }; ...


4

Depending on what function you call, you can probably resize the image correspondingly to diffrent resolution. This is a common problem, and is often solved with dynamic screen scalings. To just take this problem one step futher, you need to do dynamic correction with your GUI to maintaint the correct layout over all possible screens. and assuming you are ...


4

As your sprite count increases, you will notice that glDrawArrays is relatively slow. Luckily, the GPUs happen to have a huge bandwidth. That's why huge amounts can be uploaded on every frame. When creating your vertex buffer you can even give it GL_DYNAMIC_DRAW or GL_STREAM_DRAW as usage parameter to suggest that the data will be updated all the time. This ...


3

Effective usage is only using one single spirebatch for the game. Simply call Begin() at the start of the frame and End() at the end. However, there are some usage for multible batches. You can set them up only draw the content when you call End(). With this you could use one spritebatch for the game and one for the GUI with having problem with game elements ...


3

Actually, I had a brainwave and it worked out. I used the size (specifically, a Vector2) to scale it instead of crop it. Here's the final code: spriteBatch.Draw(Laser.sprite, Laser.position + new Vector2(0, Laser.sprite.Height / 2), null, new Color(255, 255, 255, (byte)MathHelper.Clamp(Laser.Alpha, 0, 255)), Laser.rotation, new Vector2(Laser.center.X, ...


3

Looks like overdraw due to the lack of depth buffer. You can specify the depth buffer when you create the RenderTarget2D: = new RenderTarget2D(this.GraphicsDevice, 800, 500, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);


3

Short Answer Try to use the same Begin/End block as much as possible in order to maximize the amount of sprites that are batched and reduce the actual number of Draw calls made to the graphics device. In general, you only really need to use a different Begin/End block when you want to change some of the parameters to SpriteBatch.Begin. Long Answer I'll ...


3

I encountered the same problem. (I am using eclipse indigo on ubuntu 10.04). The libgdx project setup tool creates android and desktop projects The asset folder in desktop project is a link to android project assets folder When you add a new file to the assets folder ,it takes some time for eclipse to register that a new file is added,as such ...


3

The real problem to the code you posted above is these three lines: texture = new Texture(Gdx.files.internal("android.png")); region = new TextureRegion(texture, 20, 20, 50, 50); sprite = new Sprite(texture, 20, 20, 50, 50); You are instantiating these things in the render loop which is a really bad idea. @Override the create method and create them in ...


3

If you want to clip things to a certain region, you use the scissor box. It's a property of the GraphicsDevice: GraphicsDevice.ScissorTestEnable and either RenderState.ScissorRectangle pre-XNA 4.0 or RasterizerState.ScissorTestEnable in XNA 4.0. You don't need to measure text for it; just set the scissor box to the area you want to clip to. You can draw all ...



Only top voted, non community-wiki answers of a minimum length are eligible