Primitive Tutorial

This tutorial will teach you how to get primitives rendering on the screen.


Preparations

Set up the Development Environment, download zenilib, and learn how to build everything if you haven't already.

Colored Triangle

  1. Open zenilib/jni/application/bootstrap.cpp.
  2. class Play_State is currently using the default render() function, as defined in Gamestate_Base. To override it, we can simply add the following code to Play_State:
      void render() {
      }
    
    I recommend making render() private until you have a reason to make it more publically available. The basic maxim is to restrict possible code execution paths whenever possible.
  3. You can get references to the Video and Color singletons as follows, at the beginning of render():
        Video &vr = get_Video();
        Colors &cr = get_Colors();
    
  4. To create a colored Vertex for our objects, we simply declare three Vertex2f_Color to form a triangle. We declare them in counter-clockwise order, assuming that the x-axis is pointing right and the y-axis is pointing down:
        Vertex2f_Color p0(Point2f(0.0f, 0.0f), cr["red"]);
        Vertex2f_Color p1(Point2f(0.0f, 300.0f), cr["green"]);
        Vertex2f_Color p2(Point2f(400.0f, 0.0f), cr["blue"]);
    
    This triangle will have vertices that are red, green, and blue (as defined in 'zenilib/config/color.xml'). Between those vertices, colors will be interpolated.
  5. Now we actually make a Triangle using these vertices. Triangle is a C++ template, so we need to tell it what type of Vertex to expect:
        Triangle<Vertex2f_Color> triangle(p0, p1, p2);
    
  6. Finally, we simply tell our Video device to render our Triangle:
        vr.render(triangle);
    

If you build and run this, you should have a colored triangle rendering on the screen. Now try to render a colored Quadrilateral.