polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

slab_geom.c (1303B)


      1 
      2 #include "slab_geom.h"
      3 #include <stdlib.h>
      4 #include <assert.h>
      5 
      6 struct geometry*
      7 slab_alloc_arrays(const struct slab *slab,
      8                   struct geometry *geom,
      9                   void* (*_realloc)(void*, size_t)) {
     10   _realloc = _realloc ? _realloc : realloc;
     11   float *verts, *normals;
     12   u32 *indices;
     13   const int num_cubes = slab_size(slab);
     14   const int num_sides = 6;
     15   const int vert_per_side = 4;
     16   const int wut_factor = 0;
     17   const int num_verts = (num_cubes * num_sides * vert_per_side) + wut_factor;
     18   const size_t verts_allocd  = (size_t)num_verts * 3 * sizeof(float);
     19   printf("stuff ncubes(%d) num_verts(%d) num_sides(%d) vert_per_side(%d) allocd(%zu)\n",
     20          num_cubes,
     21          num_verts,
     22          num_sides,
     23          vert_per_side,
     24          verts_allocd 
     25          );
     26   verts = malloc(verts_allocd);
     27   normals = malloc(verts_allocd);
     28   indices = malloc((size_t)num_verts * sizeof(u32));
     29 
     30   assert(verts);
     31   assert(normals);
     32   assert(indices);
     33 
     34   geom->vertices = verts;
     35   geom->indices = indices;
     36   geom->normals = normals;
     37 
     38   geom->num_verts = num_verts - wut_factor;
     39 
     40   return geom;
     41 }
     42 
     43 
     44 void
     45 slab_free_arrays(struct geometry *geom,
     46                  void (*_free)(void*)) {
     47   _free = _free ? _free : free;
     48   _free(geom->vertices);
     49   _free(geom->indices);
     50   _free(geom->normals);
     51 }