From ebaacbbb4af1e4445c015f7133b337b698185ba7 Mon Sep 17 00:00:00 2001 From: Philip J Freeman Date: Mon, 16 Dec 2019 16:15:27 -0800 Subject: stars: fix bugs, big refactor for resizing - use a context not globals - use floats and a "unit cube" to simulate the starfield --- src/modules/stars/stars.c | 152 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 121 insertions(+), 31 deletions(-) (limited to 'src/modules/stars/stars.c') diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index 63ff73c..9c1de5b 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -5,56 +5,146 @@ #include #include #include +#include #include "draw.h" #include "fb.h" #include "rototiller.h" -#include "starslib.h" -/* Copyright (C) 2017 Philip J. Freeman */ +/* Copyright (C) 2017-19 Philip J. Freeman */ -static void stars_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) +struct points +{ + float x, y, z; + struct points *next; +}; + + +typedef struct stars_context_t { + struct points* points; +} stars_context_t; + + +float get_random_unit_coord() { + return (((float)rand()/(float)RAND_MAX)*2.0)-1.0; +} + + +static void * stars_create_context(unsigned num_cpus) { - static int initialized, z; - static struct universe* u; - - struct return_point rp; - int x, y, width = fragment->width, height = fragment->height; - - if (!initialized) { - z = 128; - srand(time(NULL) + getpid()); - - // Initialize the stars lib (and pre-add a bunch of stars) - new_universe(&u, width, height, z); - for(y=0; ypoints = NULL; + + //add a bunch of points + for(z=0.01; z<1; z=z+0.01) { + for(int i=0; ix = get_random_unit_coord(); + p_ptr->y = get_random_unit_coord(); + p_ptr->z = z; + p_ptr->next = ctxt->points; + ctxt->points = p_ptr; } - initialized = 1; } + return ctxt; +} + +static void stars_destroy_context(void *context) +{ + stars_context_t *ctxt = context; + struct points* p_ptr; + struct points* last_ptr=NULL; + + for ( p_ptr=ctxt->points; p_ptr != NULL; p_ptr = p_ptr->next) + { + if (last_ptr!=NULL) + free(last_ptr); + + last_ptr=p_ptr; + } + + free(last_ptr); + + free(context); +} + + +static void stars_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) +{ + stars_context_t *ctxt = context; + struct points* iterator; + struct points* tmp_ptr; + struct points* last_ptr=NULL; + float x, y, pos_x, pos_y, opacity; + int width = fragment->width, height = fragment->height; + - // draw space (or blank the frame, if you prefer) fb_fragment_zero(fragment); - // draw stars - for (;;) { - int ret = process_point( u, &rp ); - if (ret==0) break; - if (ret==1) fb_fragment_put_pixel_unchecked(fragment, rp.x+(width/2), rp.y+(height/2), - makergb(0xFF, 0xFF, 0xFF, (float)rp.opacity/OPACITY_MAX) - ); + iterator=ctxt->points; + for(;;) + { + if(iterator == NULL) + break; + + if(iterator->z >= 1) { + if(last_ptr == NULL) + ctxt->points = iterator->next; + else + last_ptr->next = iterator->next; + tmp_ptr = iterator; + iterator = iterator->next; + free(tmp_ptr); + continue; + } + + x = iterator->x / (1.f - iterator->z); + y = iterator->y / (1.f - iterator->z); + + pos_x = ((x+1.f)*.5f)*(float)width; + pos_y = ((y+1.f)*.5f)*(float)height; + + if (pos_x>0 && pos_x0 && pos_y < height) { + if(iterator->z<0.1) + opacity = iterator->z*10; + else + opacity = 1; + + fb_fragment_put_pixel_unchecked(fragment, pos_x, pos_y, + makergb(0xFF, 0xFF, 0xFF, opacity)); + + } + + iterator->z += 0.01; + last_ptr=iterator; + iterator=iterator->next; } // add stars at horizon - for (x=0; xx = get_random_unit_coord(); + tmp_ptr->y = get_random_unit_coord(); + tmp_ptr->z = 0.01; + tmp_ptr->next = ctxt->points; + ctxt->points = tmp_ptr; } } rototiller_module_t stars_module = { + .create_context = stars_create_context, + .destroy_context = stars_destroy_context, .render_fragment = stars_render_fragment, .name = "stars", .description = "Basic starfield", -- cgit v1.2.1