diff options
author | Philip J Freeman <elektron@halo.nu> | 2019-12-16 16:15:27 -0800 |
---|---|---|
committer | Philip J Freeman <elektron@halo.nu> | 2019-12-30 14:02:32 -0800 |
commit | ebaacbbb4af1e4445c015f7133b337b698185ba7 (patch) | |
tree | 6d579b008be452ee83dd9908070aa698fa8c071a | |
parent | 036f737f7201cb9f2ae9502088773a066f168303 (diff) |
stars: fix bugs, big refactor for resizing
- use a context not globals
- use floats and a "unit cube" to simulate the starfield
-rw-r--r-- | src/modules/stars/Makefile.am | 2 | ||||
-rw-r--r-- | src/modules/stars/stars.c | 152 | ||||
-rw-r--r-- | src/modules/stars/starslib.c | 133 | ||||
-rw-r--r-- | src/modules/stars/starslib.h | 19 |
4 files changed, 122 insertions, 184 deletions
diff --git a/src/modules/stars/Makefile.am b/src/modules/stars/Makefile.am index e72891e..20ee80e 100644 --- a/src/modules/stars/Makefile.am +++ b/src/modules/stars/Makefile.am @@ -1,3 +1,3 @@ noinst_LIBRARIES = libstars.a -libstars_a_SOURCES = draw.h stars.c starslib.c starslib.h +libstars_a_SOURCES = draw.h stars.c libstars_a_CPPFLAGS = -I@top_srcdir@/src 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 <time.h> #include <sys/types.h> #include <unistd.h> +#include <math.h> #include "draw.h" #include "fb.h" #include "rototiller.h" -#include "starslib.h" -/* Copyright (C) 2017 Philip J. Freeman <elektron@halo.nu> */ +/* Copyright (C) 2017-19 Philip J. Freeman <elektron@halo.nu> */ -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; y<z; y++) { - while (process_point(u, &rp) != 0); - for(x=0; x<rand()%128; x++){ - new_point(u); - } + stars_context_t *ctxt; + float z; + struct points* p_ptr = NULL; + + ctxt = malloc(sizeof(stars_context_t)); + if (!ctxt) + return NULL; + + ctxt->points = NULL; + + //add a bunch of points + for(z=0.01; z<1; z=z+0.01) { + for(int i=0; i<rand()%16; i++){ + p_ptr = malloc(sizeof(struct points)); + if (!p_ptr) + return NULL; + p_ptr->x = 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_x<width && pos_y >0 && 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; x<rand()%128; x++) { - new_point(u); + for(int i=0; i<rand()%16; i++){ + tmp_ptr = malloc(sizeof(struct points)); + if (!tmp_ptr) + break; + tmp_ptr->x = 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", diff --git a/src/modules/stars/starslib.c b/src/modules/stars/starslib.c deleted file mode 100644 index 9d43062..0000000 --- a/src/modules/stars/starslib.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * a starfield simulation library from: https://github.com/ph1l/stars - * Copyright 2014 Philip J. Freeman <elektron@halo.nu> - */ - -#include <stdlib.h> -#ifdef DEBUG -#include <stdio.h> -#endif -#include "starslib.h" - -struct points -{ - int x, y, z; - struct points *next; -}; - -void new_universe( struct universe** u, int width, int height, int depth ) -{ - *u = malloc(sizeof(struct universe)); - - (*u)->width = width; - (*u)->height = height; - (*u)->depth = depth; - - (*u)->iterator = NULL; - (*u)->points = NULL; - #ifdef DEBUG - printf("NEW UNIVERSE: %lx: (%i,%i,%i)\n", (long unsigned int )(*u), (*u)->width, (*u)->height, (*u)->depth); - #endif - - return; -} - -void new_point( struct universe* u ) -{ - - struct points* p_ptr = malloc(sizeof(struct points)); - - p_ptr->x = (rand()%u->width - (u->width/2)) * u->depth; - p_ptr->y = (rand()%u->height - (u->height/2)) * u->depth; - p_ptr->z = u->depth; - - p_ptr->next = u->points; - u->points = p_ptr; - #ifdef DEBUG - printf("NEW POINT: %lx: (%i,%i,%i) next=%lx\n", (long unsigned int )p_ptr, p_ptr->x, p_ptr->y, p_ptr->z, (long unsigned int) p_ptr->next); - #endif - - return; -} - -void kill_point( struct universe* universe, struct points* to_kill ) -{ - - struct points *p_ptr, *last_ptr = NULL; - - - for ( p_ptr = universe->points; p_ptr != NULL; p_ptr = p_ptr->next) - { - if (p_ptr == to_kill) - { - #ifdef DEBUG - printf("KILL POINT: %lx: (%i,%i,%i).\n", (long unsigned int )p_ptr, p_ptr->x, p_ptr->y, p_ptr->z); - #endif - if (last_ptr == NULL) - { - universe->points = p_ptr->next; - } else { - last_ptr->next = p_ptr->next; - } - free(p_ptr); - } else { - last_ptr = p_ptr; - } - } - #ifdef DEBUG - printf("KILL POINT: %lx\n", (long unsigned int )p_ptr); - #endif - return; -} - -int process_point( struct universe *u, struct return_point *rp ) -{ - - if ( u->iterator == NULL ) { - if (u->points == NULL){ - return 0; - } else { - u->iterator = u->points; - } - } - - if ( u->iterator->z == 0 ){ - // Delete point that has reached us. - struct points *tmp = u->iterator; - u->iterator = u->iterator->next; - kill_point( u, tmp ); - return(-1); - } else { - // Plot the point - int x, y; - x = u->iterator->x / u->iterator->z; - y = u->iterator->y / u->iterator->z; - if ( abs(x) >= u->width/2 || abs(y) >= u->height/2 ){ - // Delete point that is off screen - struct points *tmp = u->iterator; - u->iterator = u->iterator->next; - kill_point( u, tmp ); - if ( u->iterator == NULL ) { - return(0); - } else { - return(-1); - } - } else { - int m = OPACITY_MAX*((u->depth-u->iterator->z)*4)/u->depth; - if ( m>OPACITY_MAX ){ m=OPACITY_MAX; } - u->iterator->z = u->iterator->z - 1; - #ifdef DEBUG - printf("RETURN POINT: %lx\n", (long unsigned int )u->iterator); - #endif - u->iterator = u->iterator->next; - rp->x = x; - rp->y = y; - rp->opacity = m; - if ( u->iterator == NULL ) { - return(0); - } else { - return(1); - } - } - } -} diff --git a/src/modules/stars/starslib.h b/src/modules/stars/starslib.h deleted file mode 100644 index 0c125a3..0000000 --- a/src/modules/stars/starslib.h +++ /dev/null @@ -1,19 +0,0 @@ -struct universe -{ - int width, height, depth; - struct points* points; - struct points* iterator; -}; - -void new_universe( struct universe** u, int width, int height, int depth ); -void new_point( struct universe* universe ); - -#define OPACITY_MAX 8 -struct return_point -{ - int x, y; - int opacity; -}; - -int process_point( struct universe *u, struct return_point *rp ); - |