This module provides a way to render commonly used objects in 2d games like simple sprites and sprite animations.
It also provides colored rectangles which can be great during prototyping. The simple rectangles can easily be replaced by nicer looking textures later.
suggested import:
import Game.TwoD.Render as Render exposing (Renderable)Most functions to render something come in 3 forms:
thing, thingZ, thingWithOptionsThe first is the most common one where you can specify the size, the position in 2d and some more.
The second one is the same as the first, but with a 3d position. The z position goes from -1 to 1, everything outside this will be invisible. This can be used to put something in front or behind regardless of the render order.
The last one gives you all possible options, e.g. the rotation , the pivot point of the rotation (normalized from 0 to 1), etc.
TODO: insert picture to visualize coordinate system.
A colored rectangle, great for prototyping
The same, but with 3d position.
A colored rectangle, that can also be rotated
Textures are Maybe values because you can never have a texture at the start of your game.
You first have to load your textures. In case you pass a Nothing as a value for a texture,
A gray rectangle will be displayed instead.
For loading textures I suggest using the game-resources library.
NOTE: Texture dimensions have to be in a power of 2, e.g. (2^n)x(2^m), like 4x16, 16x16, 512x256, etc. If you try to use a non power of two texture, WebGL will spit out a bunch of warnings and display a black rectangle.
A sprite.
A sprite with 3d position
A sprite with tiling and rotation.
spriteWithOptions {config | tiling = (3,5)}will create a sprite with a texture that repeats itself 3 times horizontally and 5 times vertically. TODO: picture!
An animated sprite. bottomLeft and topRight define a sub area from a texture
where the animation frames are located. It's a normalized coordinate from 0 to 1.
TODO: picture!
The same with 3d position
the same with rotation
Used for scrolling backgrounds. This probably wont satisfy all possible needs for a scrolling background, but it can give you something that looks nice quickly.
Same but with an offset parameter that you can use to position the background.
These are useful if you want to write your own GLSL shaders. When writing your own shaders, you might want to look at Game.TwoD.Shaders and Game.TwoD.Shapes for reusable parts.
This allows you to write your own custom fragment shader. The type signature may look terrifying, but this is still easier than using veryCustom or using WebGL directly. It handles the vertex shader for you, e.g. your object will appear at the expected location once rendered.
For the fragment shader, you have the vec2 varying vcoord; variable available,
which can be used to sample a texture (texture2D(texture, vcoord);)
The MakeUniformsFunc allows you to pass along any additional uniforms you may need.
In practice, this might look something like this:
makeUniforms {cameraProj, transform, time} =
{cameraProj=cameraProj, transform=transform, time=time, myUniform=someVector}
render =
customFragment makeUniforms { fragmentShader=frag, position=p, size=s, rotation=0, pivot=(0,0) }
frag =
[|glsl
precision mediump float;
varying vec2 vcoord;
uniform vec2 myUniform;
void main () {
gl_FragColor = vcoord.yx + myUniform;
}
|]Don't pass the time along if your shader doesn't need it.
Just an alias for this crazy function, needed when you want to use customFragment
This allows you to specify your own attributes, vertex shader and fragment shader by using the WebGL library directly. If you use this you have to calculate your transformations yourself. (You can use Shaders.makeTransform)
If you need a square as attributes, you can take the one from Game.TwoD.Shapes
veryCustom (\{cameraProj, time} ->
WebGL.entity vert frag Shapes.unitSquare
{ u_crazyFrog = frogTexture
, transform = Shaders.makeTransform (x, y, z) 0 (2, 4) (0, 0)
, camera = cameraProj
}
)This can be used inside veryCustom instead of WebGL.entity.
It's a custamized blend function that works well with textures with alpha.
Converts a Renderable to a WebGL.Entity. You don't need this unless you want to slowely introduce this library in a project that currently uses WebGL directly.
toWebGl time camera (w, h) cameraProj renderable
A representation of something that can be rendered. To actually render a
Renderableonto a web page use theGame.TwoD.*functions