NOTE: THIS IS OLD VERSION OF THE SITE. THE NEW ONE IS AT: HERE.

GameApi Library

GameApi is a library for writing games. It is already pretty optimized, normal games take only 300Mb of memory in initialization phase, and about 56Mb inside frame loop. This has been tested with world example code.

Platforms supported are: mingw/windows, clang/windows, emscripten/(chrome/firefox/edge/android browser), linux/g++, linux/clang

License: LGPL

  • Features supported:

    The library

    #include <GameApi.hh>
    

    Env

    class Env
    {
    public:
      Env();
      void free_memory(); // this should be called before entering frame loop!
      ~Env(); // this destructor deletes all resources in the lib
    private:
      Env(const Env &);
      void operator=(const Env &);
    private:
      void *envimpl;
      friend struct EnvImpl;
    };
    

    Handles

      struct BM { int id; }; // bitmap
      struct SP { int id; }; // space
      struct PT { int id; }; // point
      struct P { int id; }; // 3d object via polygons
    
    These are just for passing data structures around inside your game. They are for communication between classes used in the library.

    EveryApi

    struct EveryApi
    {
      EveryApi(Env &e);
    
      MainLoopApi mainloop_api;
      PointApi point_api;
      VectorApi vector_api;
      SpriteApi sprite_api;
      GridApi grid_api;
      BitmapApi bitmap_api;
      PolygonApi polygon_api;
      BoolBitmapApi bool_bitmap_api;
      FloatBitmapApi float_bitmap_api;
      FontApi font_api;
      AnimApi anim_api;
      EventApi event_api;
      //CurveApi curve_api;
      FunctionApi function_api;
      VolumeApi volume_api;
      ShaderApi shader_api;
      StateChangeApi state_change_api;
      TextureApi texture_api;
    };
    

    MainLoop

    class MainLoopApi
    {
    public:
      MainLoopApi(Env &e);
      ~MainLoopApi();
      void init_window(int screen_width = 800, int screen_height=600);
      void init(SH sh,int screen_width = 800, int screen_height = 600);
      void init_3d(SH sh, int screen_width = 800, int screen_heigth = 600);
      void clear();
      void clear_3d();
      void switch_to_3d(bool b);
      void alpha(bool enable);
      void antialias(bool enabled);
      float get_time();
      int get_framenum();
      void swapbuffers();
      BM screenshot();
      void fpscounter();
      void delay(int ms);
    
      struct Event
      {
        int type;
        int ch;
        PT cursor_pos;
        int button;
      };
      Event get_event();
      void waittof();
      SP screenspace();
    };
    
    Example:
    void mainlooptest()
    {
      Env e;
      MainLoop loop(e);
      loop.init();
      while(1)
        {
          loop.clear();
          loop.swapbuffers();
          MainLoop::Event e = loop.get_event();
          if (e.ch==27) break;
       }
    }
    

    Bitmaps

    class BitmapApi
    {
    public:
      BitmapApi(Env &e);
      ~BitmapApi();
      BM newbitmap(int sx, int sy);
      BM function(std::function<unsigned int (int,int)> f, int sx, int sy);
      BM newtilebitmap(int sx, int sy, int tile_sx, int tile_sy);
      BM loadbitmap(std::string filename);
      BM loadtilebitmap(std::string filename, int tile_sx, int tile_sy);
      BM loadposbitmap(std::string filename);
      BM findtile(BM tile_bitmap, int x, int y);
      BM subbitmap(BM orig, int x, int y, int width, int height);
      BM growbitmap(BM small_orig_bitmap, int l, int t, int r, int b);
      BM blitbitmap(BM bg, BM orig, int x, int y);
      BM anim_array(BM *array, int size);
      BM modify_bitmap(BM orig, BM bm, int x, int y);
      BM interpolatebitmap(BM orig1, BM orig2, float x); // x=[0..1]
      BM repeat_bitmap(BM orig, int xcount, int ycount);
      BM sample_bitmap(BM orig, float xmult, float ymult, float x, float y);
      BM flip_x(BM orig);
      BM flip_y(BM orig);
      BM alpha_color(BM orig, unsigned int color_key);
      BM gradient(PT pos_1, PT pos_2, 
                  unsigned int color_1, unsigned int color_2, 
                  int sx, int sy);
       BM chessboard(int tile_sx, int tile_sy, 
                     int count_x, int count_y, 
                     unsigned int c1, unsigned int c2);
      BM memoize(BM orig); // use memoize before you plan to use same BM several time
      BM memoize_all(BM orig);
    
      BM mandelbrot(bool julia,
    		float start_x, float end_x, // [-2..1]
    		float start_y, float end_y, // [-1,1]
    		float xx, float yy, // [0,0]
    		int sx, int sy,
    		int count);
      SP space(BM bm);
      BM addsubrects(BM orig); // use subbitmap with orig bitmap.
      void savebitmap(BM orig, std::string filename);
      unsigned int colorvalue(BM bm, int x, int y);
      int size_x(BM bm);
      int size_y(BM bm);
    };
    

    Sprites

    class SpriteApi
    {
    public:
      SpriteApi(Env &e);
      ~SpriteApi();
      void spritepos(BM bm, float x, float y);
    
      VA create_vertex_array(BM bm);
      void render_sprite_vertex_array(VA va);
    
      void preparesprite(BM bm, int bbm_choose=-1);
      void rendersprite(BM bm, SH sh, float x, float y, float mult_x=1.0, float mult_y=1.0);
      void rendersprite(BM bm, SH sh, PT pos);
      void rendersprite(BM bm, int bm_choose, SH sh, float x, float y, float mult_x, float mult_y);
      void rendersprite(BM bm, int bm_choose, SH sh, PT pos);
      void rendersprite(BM bm, int bm_choose, SH sh, SP move_space, SP sprite_space, float x, float y);
      void rendersprite(BM bm, int bm_choose, SH sh, SP move_space, SP sprite_space, PT pos);
      SP spritespace(BM bm);
      PT pixelpos(BM bm, int x, int y);
    };
    
    Example:
    void spritetest()
    {
      Env e;
      MainLoopApi loop(e);
      BitmapApi bm(e);
      SpriteApi sp(e);
      loop.init();
      BM b = bm.loadbitmap("sprite1.xpm");
      sp.preparesprite(b);
      
      while(1)
        {
          loop.clear();
          float time = loop.get_time();
          sp.rendersprite(b, time, 0.0);
          loop.swapbuffers();
          MainLoop::Event e = loop.get_event();
          if (e.ch==27) break;
       }   
    }
    

    Bool Bitmap

    class BoolBitmapApi
    {
    public:
      BoolBitmapApi(Env &e);
      ~BoolBitmapApi();
      BB empty(int sx, int sy);
      BB function(std::function<bool (int,int)> f, int sx, int sy);
      BB from_bitmaps_color(BM bm, int r, int g, int b);
      BB from_bitmaps_color_area(BM bm, std::function<bool(int r, int g, int b, int a)> f);
    
      BB circle(BB bg, float center_x, float center_y, float radius); 
      BB rectangle(BB bg, float x, float y, float width, float height); // for moving  
      
      BB not_bitmap(BB b);
      BB or_bitmap(BB b1, BB b2);
      BB andnot_bitmap(BB b1, BB not_b2);
      BB xor_bitmap(BB b1, BB flip_b2);
      BM choose_bitmap(BB bools, BM true_bitmap, BM false_bitmap);
    
      BM to_bitmap(BB bools, 
    	       int true_r, int true_g, int true_b, int true_a,
    	       int false_r, int false_g, int false_b, int false_a);
      BM texture(BM bg, 
    	     BB bools1, int l1, int t1,
    	     BM texturebitmap2, int l2, int t2);
      int size_x(BB bm);
      int size_y(BB bm);
      bool boolvalue(BB bb, int x, int y);
    private:
      Env &e;
    };
    

    Float bitmap

    class FloatBitmapApi
    {
    public: // values are [0.0..1.0]
      FloatBitmapApi(Env &e);
      ~FloatBitmapApi();
      FB empty(int sx, int sy);
      FB function(std::function<float (int,int)> f, int sx, int sy);
      FB from_bool_bitmap(BB bm, int csx, int csy);
      FB grayscale(BM color_bm);
      FB from_red(BM color_bm);
      FB from_green(BM color_bm);
      FB from_blue(BM color_bm);
      FB from_alpha(BM color_bm);
    
      FB min_fb(FB fb1, FB fb2);
      FB max_fb(FB fb1, FB fb2);
    
      FB mix_fb(FB fb1, FB fb2, float val);
      
      BM to_grayscale(FB fb);
      BM to_grayscale_color(FB fb, 
    			int r, int g, int b, int a,
    			int r2, int g2, int b2, int a2);
      BM to_color(FB r, FB g, FB b, FB a);
      BM choose_bitmap(FB fb, BM bm1, BM bm2);
      BM subfloatbitmap(FB fb, float range_start, float range_end, 
                               unsigned int true_color, unsigned int false_color);
      FB from_bool(BB b, float val_true, float val_false);
      BB to_bool(FB f, float true_range_start, float true_range_end);
    
      int size_x(FB bm);
      int size_y(FB bm);
      float floatvalue(FB bm, int x, int y);
    private:
      Env &e;
    };
    

    ContinuousBitmap

    class ContinuousBitmapApi
    { // RxR->RGB
    public:
      ContinuousBitmapApi(Env &e);
      CBM empty(float x, float y);
      CBM constant(unsigned int color, float x, float y);
      CBM function(std::function<unsigned int (float,float)> f, float sx, float sy);
      BM sample(CBM c_bitmap, int sx, int sy); 
      CBM from_bitmap(BM bm, float xsize, float ysize);
      BM to_bitmap(CBM bm, int sx, int sy);
    
      CBM rotate(CBM bm, float center_x, float center_y, float angle);
    };
    

    Animations

    class AnimApi
    {
    public:
      AnimApi(Env &e);
      ~AnimApi();
      IS single(int val, float duration);
      IS single(PT point, float duration);
      IS single(float val, float duration);
      IS line(int val1, int val2, float duration);
      IS line(PT p1, PT p2, float duration);
      IS line(float val1, float val2, float duration);
      IS line_obj(P p, P p2, float duration);
    
      IS repeat(IS i, int count);
      IS repeat_infinite(IS i);
    
      int timed_value(IS i, float time);  
      PT timed_value_point(IS i, float time);
      int timed_value_repeat_num(IS i, float time);
      float timed_value_float(IS i, float time);
      P timeline_obj(IS i, float time);
        
      IS seq_line(IS *array, int size);
      IS par_line(IS *array, int arraysize);
      int timeline_int(IS l, int choose_par, float time);
      float timeline_float(IS i, int choose_par, float time);
      PT timeline_point(IS l, int choose_par, float time);
      P timeline_obj(IS i, int choose_par, float time);
    };
    

    Shader modules

    lass ShaderModuleApi
    {
    public:
      ShaderModuleApi(Env &e) : e(e) { }
      SFO function(std::function f, 
                   std::string function_name, 
                   std::vector param_names = std::vector(), 
                   std::vector arg_values = std::vector());
      SFO color_function(SFO orig, 
                         std::function function, 
                         std::string function_name);
      SFO sphere(); // vec3 center, float radius
      SFO sphere(PT center, float radius); // ()
      SFO cube(); // vec3 tl, vec3 br
      SFO cube(float start_x, float end_x,
    	   float start_y, float end_y,
    	   float start_z, float end_z); // ()
      SFO rounded_cube(float start_x, float end_x,
    		   float start_y, float end_y,
    		   float start_z, float end_z,
    		   float r);
      SFO line(); // vec3 tl, vec3 br, float line_width1, float line_width2
      SFO line(float start_x, float start_y, float start_z,
    	   float end_x, float end_y, float end_z,
    	   float line_width1, float line_width2);
      SFO plane(PT center, V u_x, V u_y);
      SFO torus(float radius_1, float radius_2);
      SFO color(SFO obj, float r, float g, float b);
      SFO rot_x(SFO obj, float angle);
      SFO rot_y(SFO obj, float angle); // float angle
      SFO rot_z(SFO obj, float angle);
      SFO rot_x(SFO obj, float angle, PT center);
      SFO rot_y(SFO obj, float angle, PT center);
      SFO rot_z(SFO obj, float angle, PT center);
      SFO mod_x(SFO obj, float dx);
      SFO mod_y(SFO obj, float dy);
      SFO mod_z(SFO obj, float dz);
      SFO and_not(SFO obj, SFO not_obj);
      SFO or_elem(SFO obj1, SFO obj2);
      SFO blend(SFO obj1, SFO obj2);
      SFO trans(SFO obj);
      SFO trans(SFO obj, float dx, float dy, float dz);
      SFO from_points(PTS p, SFO obj);
      SFO from_lines(LI li, SFO obj);
      SFO bind_arg(SFO obj, std::string name, std::string value);
      SFO color_from_normal(SFO obj);
      SFO stop_generation(SFO obj);
      SFO mix_color(SFO col1, SFO col2, float t); // kills obj side
      SFO grayscale(SFO obj);
      SFO noise(SFO obj, float strength);
      SFO ambient_occulsion(SFO obj, float d, float i);
      SFO soft_shadow(SFO scene, V light_dir, float mint, float maxt, float k, float strong);
      SFO render(SFO obj);
    };
    

    Grids

    class GridApi
    {
    public:
      GridApi(Env &e);
      ~GridApi();
      void preparegrid(BM tile_bitmap, int tile_choose=-1);
      void rendergrid(BM grid, int grid_choose, float top_x, float top_y);
    };
    

    Games

    class GamesApi
    {
    public:
      GamesApi();
      ~GamesApi();
      void register_game(int game_id, void (*fptr)(EveryApi &e));
      void modify_map(int event, int game_id);
      void start_game(int event);
    };
    

    Polygons

    class PolygonApi
    {
    public:
      PolygonApi(Env &e);
      ~PolygonApi();
    
      P empty();
      P load_model(std::string filename, int obj_num);
      P line(PT p1, PT p2);
      P triangle(PT p1, PT p2, PT p3);
      P quad(PT p1, PT p2, PT p3, PT p4);
    
      P polygon(PT *array, int size);
      P cube(float start_x, float end_x, 
    	 float start_y, float end_y,
    	 float start_z, float end_z);
      P cube(PT *p); // 8 points needed
      P sphere(PT center, float radius, int numfaces1, int numfaces2);
      P cone(int numfaces, PT p1, PT p2, float rad1, float rad2);
      P ring(float sx, float sy, float x, int steps); // use RingEffect::Init() to implement
      P heightmap(BM bm, HeightMapType t,
    	      float min_x, float max_x, 
    	      float min_y, float max_y,
    	      float min_z, float max_z);
    
      P color(P orig, unsigned int color);
      P sprite_bind(P p, Q bm);
      P texture(P orig, BM bm, int bm_choose=-1); // all quads
      P texcoord_cube(P orig, 
    		  PT o, PT u_x, PT u_y, PT u_z,  // these are 3d
    		  PT tex_o, PT tex_x, PT tex_y, PT tex_z); // tex_* are 2d
      P color_cube(P orig,
    	       PT o, PT u_x, PT u_y, PT u_z,
    	       unsigned int color_o, unsigned int color_x, unsigned int color_y, unsigned int color_z);
      P color_faces(P orig,
    		unsigned int color_1, unsigned int color_2,
    		unsigned int color_3, unsigned int color_4);
    
    
      P texcoord_poly(P orig, int facenum, PT *array, int size);
      P color_poly(P orig, int facenum, unsigned int *array, int size);
    
      P or_elem(P p1, P p2);
      P or_array(P *array, int size); 
    
      P shadow(P orig, PT pos, V u_x, V u_y, V light_vec);
    
      P translate(P orig, float delta_x, float delta_y, float delta_z);
      P rotatex(P orig, float angle);
      P rotatey(P orig, float angle);
      P rotatez(P orig, float angle);
      P scale(P orig, float sx, float sy, float sz);
    
      P splitquads(P orig, int x_count, int y_count);
      P recalculate_normals(P orig);
      P change_positions(P orig, std::function<PT (PT p, int face, int point)> f);
    
      VA create_vertex_array(P p); // slow
      void render_vertex_array(VA va); // fast
    
      P counts(P p1, int numfaces);
      P count_function(P p1, std::function<int (int face)> f);
      P point_function(P p1, std::function<PT (int face, int point)> f);
      P color_function(P p1, std::function<unsigned int (int face, int point)> f);
      P texcoord_function(P p1, std::function<PT (int face, int point)> f);
      P normal_function(P p1, std::function<V (int face, int point)> f);
      P attrib_function(P p1, std::function<float (int face, int point, int idx)> f, int idx);
      P attribi_function(P p1, std::function<int (int face, int point, int idx)> f, int idx);
    
    
      P tri_vertex_array(float *v_array, int v_size,
    		     float *n_array, int n_size,
    		     unsigned int *c_array, int c_size,
    		     float *tex_array, int tex_size,
    		     float **attrib_array, int a_size1, int a_size2);
      int get_tri_vertex_array_frames(P p);
      int get_tri_vertex_array_rows(P p);
      void get_tri_vertex_array(P p, int choose, int row,
    			    int *v_size, float **v_array,
    			    int *n_size, float **n_array,
    			    int *c_size, unsigned int **c_array,
    			    int *tex_size, float **tex_array,
    			    int *attrib_size1, int *attrib_size2, float ***attrib_array);
    
      P world_from_bitmap(std::function<P (int c)> f, BM int_bm, float dx, float dy);
      P world_from_voxel(std::function<P (unsigned int c)> f, VX voxel, float dx, float dy, float dz);
      P from_points(PTS p, std::function<P (int i, float x,float y,float z, unsigned int color)> f);
      P from_lines(LI li, std::function<P (int i, float sx, float sy, float sz, float ex, float ey, float ez, unsigned int scolor, unsigned int ecolor)> f);
      P from_polygon(P p, std::function<P (int face, 
    					      float p1_x, float p1_y, float p1_z,
    					      float p2_x, float p2_y, float p2_z,
    					      float p3_x, float p3_y, float p3_z,
    					      float p4_x, float p4_y, float p4_z)> f); 
    
      P memoize(P orig);
      P memoize_all(P orig);
    };
    

    Plane

    class PlaneApi
    {
    public:
       PlaneApi(Env &e);
       PL function(PT (*fptr)(EveryApi &e, int idx, void *data), int num_points, float sx, float sy, void *data);
    
       PL flip_y(PL pl);
       PL move(PL pl, float dx, float dy);   
       PL or_plane(PL p1, PL p2);
    
       PL render_p(P p, M proj_matrix, float sx, float sy);
    
       PLA prepare(GameApi::PL pl);
       void render(PLA pl, float x, float y, float mult_x, float mult_y);
    };
    

    Lines

    class LinesApi
    {
    public:
      LinesApi(Env &e) : e(e) { }
      LI function(std::function<PT (int linenum, bool id)> f,
    	      int numlines);
      LI from_points(PC points, bool loops);
      LI from_polygon(P poly);
      LI border_from_bool_bitmap(BB b, float start_x, float end_x,
    			     float start_y, float end_y, float z);
      LLA prepare(LI l);
      void render(LLA array);
    };
    

    Texture

    class TextureApi
    {
    public:
      TextureApi(Env &e);
      TX tex_plane(int sx, int sy);
      TX tex_bitmap(BM bm);
      int unique_id();
      TX tex_assign(TX tx, int id, int x, int y, BM bm);
      TX tex_coord(TX tx, int id, int x, int y, int width, int height);
      Q get_tex_coord(TX tx, int id);
      TXID prepare(TX tx);
      void use(TXID tx);
      void unuse(TXID tx);
    private:
      Env &e;
      int count;
    };
    

    StateChange

    class StateChangeApi
    {
    public:
      StateChangeApi(Env &e, ShaderApi &api);
      TR init(int paths);
      TR linear(TR s, int path_num, std::function<P (float val, void *cb)> f, float start_v, float end_v, float duration);
      VV prepare(TR sc);
      void render(VV sc, float time, SH shadero);
    };
    

    Volume

    class VolumeApi
    {
    public:
      VolumeApi(Env &e);
      ~VolumeApi();
      O boolfunction(std::function<bool (float x, float y, float z)> f);
      O sphere(PT center, float radius);
      O cube(float start_x, float end_x, 
    	 float start_y, float end_y,
    	 float start_z, float end_z);
      O cone(PT p1, PT p2, float rad1, float rad2);
      O torus(PT center, PT u_x, PT u_y, float dist1, float dist2);
    
      O colour(O object, int r, int g, int b, int a);
      O reflect(O object, float val); // val = [0..1]
      O cubetexture(O object, PT origo, PT u_x, PT u_y, PT u_z, BM bm);
      O spheretexture(O object, PT center, BM texture, float multx, float multy);
    
      O move(O object, PT pos);
      O rotatex(O object, float angle);
      O rotatey(O object, float angle);
      O rotatez(O object, float angle);
      O scale(O object, float sx, float sy, float sz);
    
      O not_op(O object1);
      O min_op(O object1, O object2);
      O max_op(O object1, O object2);
      O andnot_op(O object1, O object2);
      BM render(O object, int sx, int sy, PT ray_0, PT ray_x, PT ray_y, PT ray_z);
      typedef std::function<P (float start_x, float end_x, 
    			float start_y, float end_y, 
    			float start_z, float end_z, 
    			unsigned int color)> fptrtype;
      P rendercubes(O object,
    		fptrtype fptr,
    		int size,
    		float wholesize); // marching cubes algo
    		
      void find_surface(O object, PT p1, PT p2, PT *res1, PT *res2, int level);
    };
    

    ColorVolume

    class ColorVolumeApi
    {
    public:
      ColorVolumeApi(Env &e) : e(e) { }
      COV function(std::function<unsigned int(float x, float y, float z)> f);
      COV from_float_volume(FO obj, unsigned int col0, unsigned int col1);
      COV from_volume(O obj, unsigned int col_true, unsigned int col_false);
    
      COV mix(COV p1, COV p2, float value); // value=[0..1]
      COV or_cov(COV p1, COV p2);
      COV phong(VO normal, PT light_pos, CO i_s, CO i_d, CO i_a, float k_s, float k_d, float k_a, float alfa);
      COV directcolor(VO normal);
      P texture(P obj, COV colors);
      BM texture_bm(P obj, COV colors, int face, int sx, int sy);
    };
    

    VectorVolume

    class VectorVolumeApi
    {
    public:
      VectorVolumeApi(Env &e) : e(e) { }
      VO function(std::function<V(float x, float y, float z)> f);
      VO normal(FD fd);
    private:
      Env &e;
    };
    

    FloatVolume

    class FloatVolumeApi
    {
    public:
      FloatVolumeApi(Env &e) : e(e) { }
      FO function(std::function<float(float x, float y, float z)> f);
      FO from_volume(O o, float false_val, float true_val);
      FO from_float_bitmap(FB bm,
    		       float start_x, float end_x, 
    		       float start_y, float end_y, 
    		       float start_z, float end_z);
      FO distance();
      FO torusdistance(PT center, V u_x, V u_y, float radius);
      FO move(FO f1, float dx, float dy, float dz);
      FO minimum(FO f1, FO f2);
      FO maximum(FO f1, FO f2);
    
      FO shadow(FD fd, V light_dir, float mint, float maxt, float k);
    
      //FO plus(FO f1, FO f2);
      BM raytrace(FO object, int sx, int sy,
    	      PT ray_0, PT ray_x, PT ray_y, PT ray_z, float surface_value);
      FOA prepare(FO object, int numpoints,
    	      float start_x, float start_y, float start_z, 
    	      float end_x, float end_y, float end_z);
      void render(FOA array);
    };
    

    DistanceFloatVolume

    class DistanceFloatVolumeApi
    {
    public:
      DistanceFloatVolumeApi(Env &e) : e(e) { }
      FD function(std::function<float(float x, float y, float z)> f);
      FD sphere(PT center, float radius);
      FD cube(float start_x, float end_x,
              float start_y, float end_y,
    	  float start_z, float end_z);
      FD line(PT start, PT end, float dist);
    
      FD min(FD a1, FD a2);
      FD and_not(FD a1, FD a2);
      BM render(FD obj, COV color, PT pos, V u_x, V u_y, V u_z, int sx, int sy);
    };
    

    Color

    class ColorApi
    {
    public:
      ColorApi(Env &e);
      CO u_color(unsigned int color); // argb
      CO rgb_color(int r, int g, int b, int a); // r,g,b,a [0..255]
      CO rgbf_color(float r, float g, float b, float a);
    private:
      Env &e;
    };
    

    Point

    class PointApi
    {
    public:
      PointApi(Env &e);
      PT origo();
      PT point(float x, float y, float z=0.0);
      PT move(PT p1, float delta_x, float delta_y, float delta_z=0.0);
      PT move(PT p1, V vec);
    
      PT mix(PT p1, PT p2, float val); // val=[0.0..1.0]
    
      PT from_angle(float radius, float angle);
      PT from_angle(PT center, float radius, float angle);
    
      float pt_x(PT p);
      float pt_y(PT p);
      float pt_z(PT p);
      float dist3d(PT p, PT p2);
      float dist2d(PT p, PT p2);
      V minus(PT p1, PT p2);
    private:
      Env &e;
    };
    

    Vector

    class VectorApi
    {
    public:
      VectorApi(Env &e);
      V null_vector();
      V vector(float delta_x, float delta_y, float delta_z);
      V sum(V v1, V v2);
      V mul(V v1, float scalar);
      V dot(V v1, V v2);
      V cross(V v1, V v2);
      float projection_length(V u, V u_x);
      V projection_1(V u, V u_x);
      V projection_2(V u, V u_x);
      V neg(V v);
      float dist3d(V v);
      float dist2d(V v);
    private:
      Env &e;
    };
    

    Text

    class TextApi
    {
    public:
      TextApi(Bitmap &bm, Sprite &sp) : bm(bm), sp(sp), priv(0) { }
      ~TextApi();
      void load_font(std::string filename, int sx, int sy, int x, int y, char start_char, char end_char);
      void draw_text(std::string text, int x, int y);
    };
    

    Font

    class FontApi
    {
    public:
      FontApi(Env &e);
      ~FontApi();
      Ft newfont(const char *ttf_filename, int sx, int sy);
      BM glyph(Ft font, long idx);
      LI glyph_outline(Ft font, long idx, float sx, float sy);
      PL glyph_plane(Ft font, long idx, float sx, float sy);
      BM font_string(Ft font, const char *text, int x_gap); // framerate problems in this call
      FB glyph_fb(Ft font, long idx);
      BB glyph_bb(Ft font, long idx);
    };
    

    Event

    (NOTE, EventApi might not yet be usable yet since run_game is not implemented and it might still limit too much whatk kind of games can be implemented.)
    class EventApi
    {
    public:
      EventApi(Env &e);
      ~EventApi();
      E root_event();
      E timer(E activation_event, float time);
      E key_down_event(int key, E start_time, E end_time);
      E key_up_event(int key, E start_time, E end_time);
      E activate_mouse_plane(E activation_event, MP m);
      E deactivate_mouse_plane(E deactivation_event, MP m);
      E pair(E e1, E e2);
      E array(E *array, int size);
      L polygon(E start, E end, P polygon);
      L polygon(E start, E end, P start_polygon, P end_polygon);
      L bitmap(E start, E end, BM bitmap);
      L bitmap(E start, E end, BM start_bitmap, BM end_bitmap);
      L array(L *array, int size);
      M mouse_plane();
      M mouse_rectangle(MP m, int x, int y, int width, int height);
      M array(M *array, int size);
      MV point(float x, float y, float z);
      MV line(E start, E end, MV start_mv, MV end_mv);
      LL link(L obj, MV point);
      LL link(L obj, ID pointnum, MV point);
      LL array(LL *array, int size);
      ST states(int count_states);
      ST mouse_plane(ST states, int state, M mouse);
      ST activate_event(ST states, int state, E event);
      ST enable_obj(ST states, int state, LL link);
      ST disable_obj(ST states, int state, LL link);
      E state_change(E event, int old_state, int new_state);
      //void run_game(ST states, int start_state);
      std::string Serialize(ST states, int start_state);
      //ST UnSerialize(std::string s);
    };
    

    Shaders

    class ShaderApi
    {
    public:
      ShaderApi(Env &e);
      ~ShaderApi();
      // the file needs to have comments like 
      // //V: vertexname 
      // //F: fragmentname
      // //G: geometryname
      // for vertex, fragment and geometry shaders
      void load(std::string filename);
      // v_format = "vertexname1:vertexname2"
      // f_format = "fragmentname1:fragmentname2"
      // g_format = "geometryname1:geometryname2"
      SH get_shader(std::string v_format, std::string f_format, std::string g_format);
      void use(SH shader);
      void unuse(SH shader);
    private:
      void *priv;
      Env &e;
    };
    
    Example:
    void GameTest8(EveryApi &e)
    {
      e.mainloop_api.init_window();
      e.shader_api.load("Shader.txt");
      SH sh = e.shader_api.get_shader("empty", "empty", "");
    
      e.shader_api.link(sh);
      
      e.shader_api.use(sh);
      e.mainloop_api.init_3d(sh );
      e.shader_api.bind_attrib(sh, 0, "in_Position");
      e.shader_api.use(sh);
      e.shader_api.set_default_projection(sh, "in_P");
      /* ... */
    }
    

    Object-Oriented API

    SpriteObj

     class SpriteObj : public RenderObject, public MoveScaleObject2d
      {
      public:
        SpriteObj(SpriteApi &sp, BM bm_, SH sh);
        SpriteObj(EveryApi &ev, BM bm_, SH sh);
        SpriteObj(EveryApi &ev, std::vector<BM> anim, SH sh);
        void prepare();
        void render(); 
        void set_pos(float p_x, float p_y);
        void set_scale(float m_x, float m_y);
        void set_anim_frame(int id);
        };
    

    PolygonObj

    class PolygonObj : public RenderObject, public MoveScaleObject3d
      {
      public:
        PolygonObj(EveryApi &ev, P p, SH sh);
        PolygonObj(EveryApi &ev, std::vector<P> anim_p, SH sh);
        PolygonObj(PolygonApi &api, ShaderApi &shapi, MatrixApi &mat, TextureApi &tex,P p, SH sh);
        void prepare();
        void render();
        void set_pos(float pos_x, float pos_y, float pos_z);
        void set_scale(float mult_x, float mult_y, float mult_z);
        void bind_texture(int anim_id, TXID id_);
        void set_anim_frame(int id);
    };
    

    PointsObj

    class PointsObj : public RenderObject, public MoveScaleObject3d
      {
      public:
        PointsObj(EveryApi &ev, FO fo, SH sh)
        void set_numpoints(int count);
        void set_bounds(float s_x, float s_y, float s_z,
    		    float e_x, float e_y, float e_z);
        void prepare();
        void render();
        void set_pos(float pos_x, float pos_y, float pos_z);
        void set_scale(float mult_x, float mult_y, float mult_z);
    };
    

    LinesObj

      class LinesObj : public RenderObject, public MoveScaleObject3d
      {
      public:
        LinesObj(EveryApi &ev, LI li, SH sh) 
        LinesObj(LinesApi &lines, MatrixApi &mat, ShaderApi &shapi, LI li, SH sh);
        void prepare();
        void render();
        void set_pos(float pos_x, float pos_y, float pos_z);
        void set_scale(float mult_x, float mult_y, float mult_z);
      };
    

    ArrayObj3d

     class ArrayObj3d : public RenderObject, public MoveScaleObject3d
      {
      public:
        ArrayObj3d();
        void push_back(RenderObject *obj, MoveScaleObject3d *obj2);
        void prepare();
        void render();
        int size() const;
        void set_child_pos(int i, float cpos_x, float cpos_y, float cpos_z);
        void set_child_scale(int i, float mult_x, float mult_y, float mult_z);
        void set_pos(float pos_x, float pos_y, float pos_z);
        void set_scale(float mult_x, float mult_y, float mult_z);
      };
    

    WorldObj

      class WorldObj : public RenderObject, public MoveScaleObject3d
      {
      public:
         WorldObj(EveryApi &ev, std::function<P(int)> f, int numvalues, BM bm, int dx, int dy, SH sh);
         ~WorldObj();
         void set_block(int x, int y, int c);
         void read_block(int x, int y) const;
         void prepare();
         void set_range(int x, int y, int sx, int sy);
         void render();
         void set_pos(float pos_x, float pox_y, float pos_z);
         void set_scale(float mult_x, float mult_y, float mult_z);
         void set_rotation_y(float angle);
         void set_rotation_matrix(M m);
         void set_rotation_matrix2(M m);
         void bind_texture(int num_id, TXID id_);
      };
    

    Extending the library

    struct K { std::vector < BM > vec; };
    
    class OwnApi
    {
    public:
      OwnApi(BitmapApi &bm, SpriteApi &sp) : bm(bm), sp(sp) { }
      K create(int a, int b);
      void prepare(K k);
      void render(K k);
    private:
      BitmapApi &bm;
      SpriteApi &sp;
    };
    
    void ownapitest()
    {
      Env e;
      MainLoopApi loop(e);
      BitmapApi bm(e);
      SpriteApi sp(e);
      OwnApi s(bm, sp);
    
      K k = s.create(10,20);
      s.prepare(k);
      while(1) {
        loop.clear();
        s.render(k);
        loop.swapbuffers();
      }
    }
    
    Download:

    WIN32 MINGW: GameApi-win32.zip

    Coding convention: pure.html

    std::function documentation: std::function>

    Example code:

  • Example3d.zip. Example3d.cpp.

    Full scale example code: (a game)

  • Game
  • Chess(web)
  • World example(web)

    Github pages:

  • GameApi github page

    Google+

    Email me if you want to know more: terop@kotiposti.net.