src/gfx/sprite.c
/*
 * JIN_gfx_sprite_draw
 *
 * @desc
 *   Batch draw all entities with a sprite
 *   and position component
 *   Uses a default shader and a default
 *   texture
 * @return
 *   Success
 */
JIN_gfx_sprite_draw
@desc
Batch draw all entities with a sprite
and position component
Uses a default shader and a default
texture
@return
Success
JIN_gfx_sprite_draw @desc Batch draw all entities with a sprite and position component Uses a default shader and a default texture @return Success
static int prepare_buffer(void *buffer, int x, int y)
{
  struct JEL_Query q;
  JEL_QUERY(q, Position, Sprite);
  unsigned int sprite_num = 0;
  for (unsigned int t = 0; t < q.count; ++t) {
    struct PositionIt pos;
    struct SpriteIt sprite;
    JEL_IT(pos, q.tables[t], Position);
    JEL_IT(sprite, q.tables[t], Sprite);
    if (sprite_num > MAX_SPRITES)
      break;
    for (JEL_EntityInt i = 1; i < q.tables[t]->count; ++i) {
      if (pos.x[i] + sprite.w[i] < x || pos.x[i] > x + WINDOW_WIDTH ||
          pos.y[i] + sprite.h[i] < y || pos.y[i] > y + WINDOW_HEIGHT)
        continue;
      #define SPRITE_BUFFER_IDX(index) ((float *) buffer)[(sprite_num * VERTEX_ATTRIBS * 4) + index]
      SPRITE_BUFFER_IDX( 0) = pos.x[i] + sprite.w[i] - x;
      SPRITE_BUFFER_IDX( 1) = pos.y[i]               - y;
      SPRITE_BUFFER_IDX( 2) = sprite.z[i];
      SPRITE_BUFFER_IDX( 3) = sprite.tx[i] + sprite.tw[i] - sprite.tw[i] * sprite.dir[i];
      SPRITE_BUFFER_IDX( 4) = sprite.ty[i];
      SPRITE_BUFFER_IDX( 5) = pos.x[i] + sprite.w[i] - x;
      SPRITE_BUFFER_IDX( 6) = pos.y[i] + sprite.h[i] - y;
      SPRITE_BUFFER_IDX( 7) = sprite.z[i];
      SPRITE_BUFFER_IDX( 8) = sprite.tx[i] + sprite.tw[i] - sprite.tw[i] * sprite.dir[i];
      SPRITE_BUFFER_IDX( 9) = sprite.ty[i] + sprite.th[i];
      SPRITE_BUFFER_IDX(10) = pos.x[i]               - x;
      SPRITE_BUFFER_IDX(11) = pos.y[i] + sprite.h[i] - y;
      SPRITE_BUFFER_IDX(12) = sprite.z[i];
      SPRITE_BUFFER_IDX(13) = sprite.tx[i] + sprite.tw[i] * sprite.dir[i];
      SPRITE_BUFFER_IDX(14) = sprite.ty[i] + sprite.th[i];
      SPRITE_BUFFER_IDX(15) = pos.x[i] - x;
      SPRITE_BUFFER_IDX(16) = pos.y[i] - y;
      SPRITE_BUFFER_IDX(17) = sprite.z[i];
      SPRITE_BUFFER_IDX(18) = sprite.tx[i] + sprite.tw[i] * sprite.dir[i];
      SPRITE_BUFFER_IDX(19) = sprite.ty[i];
      if (++sprite_num > MAX_SPRITES)
        break;
    }
  }
  JEL_query_destroy(&q);
  return sprite_num;
}
static int prepare_buffer ( void * buffer , int x , int y ) { struct JEL_Query q ; JEL_QUERY ( q , Position , Sprite ) ; unsigned int sprite_num = 0 ; for ( unsigned int t = 0 ; t < q . count ; ++ t ) { struct PositionIt pos ; struct SpriteIt sprite ; JEL_IT ( pos , q . tables [ t ] , Position ) ; JEL_IT ( sprite , q . tables [ t ] , Sprite ) ; if ( sprite_num > MAX_SPRITES ) break ; for ( JEL_EntityInt i = 1 ; i < q . tables [ t ] -> count ; ++ i ) { if ( pos . x [ i ] + sprite . w [ i ] < x || pos . x [ i ] > x + WINDOW_WIDTH || pos . y [ i ] + sprite . h [ i ] < y || pos . y [ i ] > y + WINDOW_HEIGHT ) continue ; #define SPRITE_BUFFER_IDX ( index )  ((float *) buffer)[(sprite_num * VERTEX_ATTRIBS * 4) + index] SPRITE_BUFFER_IDX ( 0 ) = pos . x [ i ] + sprite . w [ i ] - x ; SPRITE_BUFFER_IDX ( 1 ) = pos . y [ i ] - y ; SPRITE_BUFFER_IDX ( 2 ) = sprite . z [ i ] ; SPRITE_BUFFER_IDX ( 3 ) = sprite . tx [ i ] + sprite . tw [ i ] - sprite . tw [ i ] * sprite . dir [ i ] ; SPRITE_BUFFER_IDX ( 4 ) = sprite . ty [ i ] ; SPRITE_BUFFER_IDX ( 5 ) = pos . x [ i ] + sprite . w [ i ] - x ; SPRITE_BUFFER_IDX ( 6 ) = pos . y [ i ] + sprite . h [ i ] - y ; SPRITE_BUFFER_IDX ( 7 ) = sprite . z [ i ] ; SPRITE_BUFFER_IDX ( 8 ) = sprite . tx [ i ] + sprite . tw [ i ] - sprite . tw [ i ] * sprite . dir [ i ] ; SPRITE_BUFFER_IDX ( 9 ) = sprite . ty [ i ] + sprite . th [ i ] ; SPRITE_BUFFER_IDX ( 10 ) = pos . x [ i ] - x ; SPRITE_BUFFER_IDX ( 11 ) = pos . y [ i ] + sprite . h [ i ] - y ; SPRITE_BUFFER_IDX ( 12 ) = sprite . z [ i ] ; SPRITE_BUFFER_IDX ( 13 ) = sprite . tx [ i ] + sprite . tw [ i ] * sprite . dir [ i ] ; SPRITE_BUFFER_IDX ( 14 ) = sprite . ty [ i ] + sprite . th [ i ] ; SPRITE_BUFFER_IDX ( 15 ) = pos . x [ i ] - x ; SPRITE_BUFFER_IDX ( 16 ) = pos . y [ i ] - y ; SPRITE_BUFFER_IDX ( 17 ) = sprite . z [ i ] ; SPRITE_BUFFER_IDX ( 18 ) = sprite . tx [ i ] + sprite . tw [ i ] * sprite . dir [ i ] ; SPRITE_BUFFER_IDX ( 19 ) = sprite . ty [ i ] ; if ( ++ sprite_num > MAX_SPRITES ) break ; } } JEL_query_destroy ( & q ) ; return sprite_num ; }
JIN_gfx_sprite_draw
 @desc
   Batch draw all entities with a sprite
   and position component
   Uses a default shader and a default
   texture
 @return
   Success
JIN_gfx_sprite_draw @desc Batch draw all entities with a sprite and position component Uses a default shader and a default texture @return Success
/* Prepare the vbo */ // TODO: Loop is not parallelized // TODO: Skip sprites outside of the screen /* Top right */ /* Bottom right */ /* Bottom left */ /* Top left */ // TODO: Loop is not parallelized