Skip to content

Tutorial: primitives

theZiz edited this page May 15, 2013 · 3 revisions

Welcome to the Tutorial about sparrowPrimitives. Probably this tutorial will be one of the longest, but afterwards you should be able to start writing your first game! (Or at least unterstanding some of the example code ;) )

Table of Contents

Principles

The basic principle of the whole sparrowPrimitives part and every bottom-up part like sparrowRenderer or sparrowFont is a state machine, which determines in which surface and how every drawing call works. The most important setting you have to do is the render target. The render target can be any SDL_Surface. Set it with spSelectRenderTarget. Two things happen: First the target is set as we want. The second thing is, that a z-Buffer is created for this specific target. Every new target gets a new z-Buffer. However, the old buffer is cached for speeding things up. So, if you use only a few or only 1 render target, not much allocation magic is done. Most of the time the render target will be your screen surface.

z Buffer magic

Two other settings to set up are the z-set and the z-test. Even if sparrowPrimitives is about drawing in 2D space, every drawing function has the possibility to set a z value into the z-Buffer. The z-Buffer is as large as the target-surface, but instead of drawing a color in it, the z-value is saved. So if z-set is enabled (with spSetZSet you "draw" twice: First to your target surface, then to its z-Buffer.

That makes only much sense, if you activate z-test (with spSetZTest), too. In that case before drawing or setting a z value the z-value of your pixel to draw and the already existing z-value in the z-buffer are compared and if the new pixel has a smaller z-value, it will be drawn, otherwise not. That is very important for 3D rendering, but may be interesting for 2D stuff, too. However, if you really want to stay two dimensional and don't need the z-buffer neither, deactivate both settings, because they make your whole program slower (about 50%!).

alpha and pattern magic

But the z-value is not the only test every single pixel can have. sparrowPrimitives provides also an (optional) test for the alpha color SP_ALPHA_COLOR, which is pink with R=255, G=0 and B=255. If alpha test is enabled with spSetAlphaTest every pixel with this color will not be drawn.

As you may notice now: sparrow3d doesn't provide real alpha blending! Just because it is slow. But it has something else: a pattern test. Think of a pattern of 4x4 binary pixel, which lays repeatedly on your target. Everywhere, where the pattern says 1, the pixel is drawn, elsewhere not. With this you can get some kind of... discrete alpha blending. ;) Set the Alpha Pattern with the here described pattern functions.

Culling

A feature of sparrow3d is culling. That means, that every primitive (primitives are triangles or quads), that is defined clockwise will not be drawn. That may look useless for 2d graphic, so deactivate it with spSetCulling if you don't need it, but it can speed up 3d applications up to 100%, because you don't have to draw the "inside" of a manifold object. ;)

Further preparations

If you use the z-Buffer you have to "clear" it with the farest away z-value with spResetZBuffer every frame. Furthermore most modern graphic applications first clear the whole screen and then draw on it. If you know, that you will draw on every pixel on the screen, this isn't necessary, but if not, call spClearTarget every frame, too.

That's it! Let's start with the draw functions.

Drawing functions