Skip to content

Commit

Permalink
refactor crazy memory allocations in 3d background code
Browse files Browse the repository at this point in the history
  • Loading branch information
Akaricchi committed Mar 11, 2019
1 parent 53b97d5 commit 82a9a02
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/credits.c
Expand Up @@ -211,7 +211,7 @@ static void credits_towerwall_draw(vec3 pos) {

static void credits_init(void) {
memset(&credits, 0, sizeof(credits));
init_stage3d(&stage_3d_context);
init_stage3d(&stage_3d_context, 16);

add_model(&stage_3d_context, credits_towerwall_draw, stage6_towerwall_pos);

Expand Down
2 changes: 1 addition & 1 deletion src/resource/resource.c
Expand Up @@ -553,7 +553,7 @@ void free_resources(bool all) {
unload_resource(ires);
free(c);

log_debug(
log_info(
"Unloaded %s '%s' (%s)",
type_name(type),
name,
Expand Down
10 changes: 5 additions & 5 deletions src/stages/stage1.c
Expand Up @@ -207,9 +207,9 @@ static void stage1_water_draw(vec3 pos) {
r_state_pop();
}

static vec3 **stage1_bg_pos(vec3 p, float maxrange) {
static uint stage1_bg_pos(Stage3D *s3d, vec3 p, float maxrange) {
vec3 q = {0,0,0};
return single3dpos(p, INFINITY, q);
return single3dpos(s3d, p, INFINITY, q);
}

static void stage1_smoke_draw(vec3 pos) {
Expand All @@ -233,10 +233,10 @@ static void stage1_smoke_draw(vec3 pos) {
r_state_pop();
}

static vec3 **stage1_smoke_pos(vec3 p, float maxrange) {
static uint stage1_smoke_pos(Stage3D *s3d, vec3 p, float maxrange) {
vec3 q = {0,0,-300};
vec3 r = {0,300,0};
return linear3dpos(p, maxrange/2.0, q, r);
return linear3dpos(s3d, p, maxrange/2.0, q, r);
}

static bool stage1_fog(Framebuffer *fb) {
Expand Down Expand Up @@ -309,7 +309,7 @@ static void stage1_waterplants_draw(vec3 pos) {
}

static void stage1_start(void) {
init_stage3d(&stage_3d_context);
init_stage3d(&stage_3d_context, 16);
add_model(&stage_3d_context, stage1_water_draw, stage1_bg_pos);
add_model(&stage_3d_context, stage1_waterplants_draw, stage1_smoke_pos);
add_model(&stage_3d_context, stage1_smoke_draw, stage1_smoke_pos);
Expand Down
14 changes: 7 additions & 7 deletions src/stages/stage2.c
Expand Up @@ -128,25 +128,25 @@ static void stage2_bg_ground_draw(vec3 pos) {
r_mat_mode(MM_MODELVIEW);
}

static vec3 **stage2_bg_pos(vec3 pos, float maxrange) {
static uint stage2_bg_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 0, 0};
vec3 r = {0, 1000, 0};

return linear3dpos(pos, maxrange, p, r);
return linear3dpos(s3d, pos, maxrange, p, r);
}

static vec3 **stage2_bg_grass_pos(vec3 pos, float maxrange) {
static uint stage2_bg_grass_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 0, 0};
vec3 r = {0, 2000, 0};

return linear3dpos(pos, maxrange, p, r);
return linear3dpos(s3d, pos, maxrange, p, r);
}

static vec3 **stage2_bg_grass_pos2(vec3 pos, float maxrange) {
static uint stage2_bg_grass_pos2(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 1234, 40};
vec3 r = {0, 2000, 0};

return linear3dpos(pos, maxrange, p, r);
return linear3dpos(s3d, pos, maxrange, p, r);
}

static bool stage2_fog(Framebuffer *fb) {
Expand All @@ -173,7 +173,7 @@ static bool stage2_bloom(Framebuffer *fb) {
}

static void stage2_start(void) {
init_stage3d(&stage_3d_context);
init_stage3d(&stage_3d_context, 16);
stage_3d_context.cx[2] = 1000;
stage_3d_context.cx[0] = -850;
stage_3d_context.crot[0] = 60;
Expand Down
6 changes: 3 additions & 3 deletions src/stages/stage3.c
Expand Up @@ -64,7 +64,7 @@ static struct {
float tunnel_side;
} stgstate;

static vec3 **stage3_bg_pos(vec3 pos, float maxrange) {
static uint stage3_bg_pos(Stage3D *s3d, vec3 pos, float maxrange) {
//vec3 p = {100 * cos(global.frames / 52.0), 100, 50 * sin(global.frames / 50.0)};
vec3 p = {
stgstate.tunnel_side * cos(global.frames / 52.0),
Expand All @@ -73,7 +73,7 @@ static vec3 **stage3_bg_pos(vec3 pos, float maxrange) {
};
vec3 r = {0, 3000, 0};

return linear3dpos(pos, maxrange, p, r);
return linear3dpos(s3d, pos, maxrange, p, r);
}

static void stage3_bg_tunnel_draw(vec3 pos) {
Expand Down Expand Up @@ -145,7 +145,7 @@ static bool stage3_glitch(Framebuffer *fb) {
}

static void stage3_start(void) {
init_stage3d(&stage_3d_context);
init_stage3d(&stage_3d_context, 16);

stage_3d_context.cx[2] = -10;
stage_3d_context.crot[0] = -95;
Expand Down
54 changes: 17 additions & 37 deletions src/stages/stage4.c
Expand Up @@ -85,20 +85,19 @@ static bool stage4_fog(Framebuffer *fb) {
return true;
}

static vec3 **stage4_fountain_pos(vec3 pos, float maxrange) {
static uint stage4_fountain_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 400, 1500};
vec3 r = {0, 0, 3000};

vec3 **list = linear3dpos(pos, maxrange, p, r);
uint num = linear3dpos(s3d, pos, maxrange, p, r);

int i;

for(i = 0; list[i] != NULL; i++) {
if((*list[i])[2] > 0)
(*list[i])[2] = -9000;
for(uint i = 0; i < num; ++i) {
if(s3d->pos_buffer[i][2] > 0) {
s3d->pos_buffer[i][2] = -9000;
}
}

return list;
return num;
}

static void stage4_fountain_draw(vec3 pos) {
Expand All @@ -114,27 +113,9 @@ static void stage4_fountain_draw(vec3 pos) {
r_mat_pop();
}

static vec3 **stage4_lake_pos(vec3 pos, float maxrange) {
static uint stage4_lake_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 600, 0};
vec3 d;

int i;

for(i = 0; i < 3; i++)
d[i] = p[i] - pos[i];

if(glm_vec3_norm(d) > maxrange) {
return NULL;
} else {
vec3 **list = calloc(2, sizeof(vec3*));

list[0] = malloc(sizeof(vec3));
for(i = 0; i < 3; i++)
(*list[0])[i] = p[i];
list[1] = NULL;

return list;
}
return single3dpos(s3d, pos, maxrange, p);
}

static void stage4_lake_draw(vec3 pos) {
Expand All @@ -153,20 +134,19 @@ static void stage4_lake_draw(vec3 pos) {
r_mat_pop();
}

static vec3 **stage4_corridor_pos(vec3 pos, float maxrange) {
static uint stage4_corridor_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 2400, 50};
vec3 r = {0, 2000, 0};

vec3 **list = linear3dpos(pos, maxrange, p, r);

int i;
uint num = linear3dpos(s3d, pos, maxrange, p, r);

for(i = 0; list[i] != NULL; i++) {
if((*list[i])[1] < p[1])
(*list[i])[1] = -9000;
for(uint i = 0; i < num; ++i) {
if(s3d->pos_buffer[i][1] < p[1]) {
s3d->pos_buffer[i][1] = -9000;
}
}

return list;
return num;
}

static void stage4_corridor_draw(vec3 pos) {
Expand Down Expand Up @@ -229,7 +209,7 @@ static void stage4_corridor_draw(vec3 pos) {
}

static void stage4_start(void) {
init_stage3d(&stage_3d_context);
init_stage3d(&stage_3d_context, 16);

stage_3d_context.cx[2] = -10000;
stage_3d_context.cv[2] = 19.7;
Expand Down
6 changes: 3 additions & 3 deletions src/stages/stage5.c
Expand Up @@ -59,11 +59,11 @@ struct {
float rad;
} stagedata;

static vec3 **stage5_stairs_pos(vec3 pos, float maxrange) {
static uint stage5_stairs_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 0, 0};
vec3 r = {0, 0, 6000};

return linear3dpos(pos, maxrange, p, r);
return linear3dpos(s3d, pos, maxrange, p, r);
}

static void stage5_stairs_draw(vec3 pos) {
Expand Down Expand Up @@ -153,7 +153,7 @@ void iku_spell_bg(Boss *b, int t) {
static void stage5_start(void) {
memset(&stagedata, 0, sizeof(stagedata));

init_stage3d(&stage_3d_context);
init_stage3d(&stage_3d_context, 16);
add_model(&stage_3d_context, stage5_stairs_draw, stage5_stairs_pos);

stage_3d_context.crot[0] = 60;
Expand Down
28 changes: 14 additions & 14 deletions src/stages/stage6.c
Expand Up @@ -82,20 +82,19 @@ static float starpos[3*NUM_STARS];
Framebuffer *baryon_aux_fb;
FBPair baryon_fbpair;

vec3 **stage6_towerwall_pos(vec3 pos, float maxrange) {
uint stage6_towerwall_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 0, -220};
vec3 r = {0, 0, 300};

vec3 **list = linear3dpos(pos, maxrange, p, r);
uint num = linear3dpos(s3d, pos, maxrange, p, r);

int i;

for(i = 0; list[i] != NULL; i++) {
if((*list[i])[2] > 0)
(*list[i])[1] = -90000;
for(uint i = 0; i < num; ++i) {
if(s3d->pos_buffer[i][2] > 0) {
s3d->pos_buffer[i][1] = -90000;
}
}

return list;
return num;
}

void stage6_towerwall_draw(vec3 pos) {
Expand All @@ -111,10 +110,9 @@ void stage6_towerwall_draw(vec3 pos) {
r_shader_standard();
}

static vec3 **stage6_towertop_pos(vec3 pos, float maxrange) {
static uint stage6_towertop_pos(Stage3D *s3d, vec3 pos, float maxrange) {
vec3 p = {0, 0, 70};

return single3dpos(pos, maxrange, p);
return single3dpos(s3d, pos, maxrange, p);
}

static void stage6_towertop_draw(vec3 pos) {
Expand All @@ -127,8 +125,8 @@ static void stage6_towertop_draw(vec3 pos) {
r_mat_pop();
}

static vec3 **stage6_skysphere_pos(vec3 pos, float maxrange) {
return single3dpos(pos, maxrange, stage_3d_context.cx);
static uint stage6_skysphere_pos(Stage3D *s3d, vec3 pos, float maxrange) {
return single3dpos(s3d, pos, maxrange, s3d->cx);
}

static void stage6_skysphere_draw(vec3 pos) {
Expand Down Expand Up @@ -216,7 +214,9 @@ void start_fall_over(void) { //troll
}

static void stage6_start(void) {
init_stage3d(&stage_3d_context);
// TODO: make this background slightly less horribly inefficient

init_stage3d(&stage_3d_context, 128);
fall_over = 0;

add_model(&stage_3d_context, stage6_skysphere_draw, stage6_skysphere_pos);
Expand Down
2 changes: 1 addition & 1 deletion src/stages/stage6.h
Expand Up @@ -62,7 +62,7 @@ extern Framebuffer *baryon_aux_fb;

void start_fall_over(void);

vec3 **stage6_towerwall_pos(vec3 pos, float maxrange);
uint stage6_towerwall_pos(Stage3D *s3d, vec3 pos, float maxrange);
void stage6_towerwall_draw(vec3 pos);

#endif // IGUARD_stages_stage6_h

0 comments on commit 82a9a02

Please sign in to comment.