Article View: comp.lang.c
Article #1897Re: fir's bag visualisation
From: fir
Date: Fri, 27 Nov 2026 17:47
Date: Fri, 27 Nov 2026 17:47
166 lines
5355 bytes
5355 bytes
fir pisze:
> maybe this group recently lacks a post of strict hard c so this is
> strict hard c
>
> to use in lean fire shots i wrote and ad-hoc container named bag
>
> note i was not thinking wrting it becouse if im in creative mode
> even 5 or 30 minutes of slowdown matters (and later i also not lookd on
> that) so maybe or even possibly this have some errors
>
> but i find it as a good interesting topic on clc so the question is:
> how to improve that?
>
>
>
> enum {MAX_WORLDS = 30};
>
> int bag_i_tab[MAX_WORLDS];
> int bag_max_tab[MAX_WORLDS];
> Object3D *bag_tab[MAX_WORLDS];
>
>
> int bag_i=0;
> int bag_max=0;
> Object3D *bag=NULL;
>
> SwitchBag(int from, int to)
> {
>
> bag_i_tab[from] = bag_i ;
> bag_max_tab[from] = bag_max;
> bag_tab[from] = bag;
>
>
> bag_i = bag_i_tab[to];
> bag_max = bag_max_tab[to];
> bag = bag_tab[to];
> }
>
>
>
> inline int countOcupationInBag()
> {
> int counter=0;
> for(int i=0;i<bag_max;i++) if(bag[i].enabled) counter++;
> return counter;
> }
>
>
> inline void ResizeBag(int new_size)
> {
>
> int old_size=bag_max;
> Object3D *new_bag=(Object3D*) realloc(bag,new_size*sizeof(Object3D));
> if(!new_bag) return;
>
> bag=new_bag;
> for(int i=old_size;i<new_size;i++) memset(&bag[i],0,sizeof(Object3D));
>
> bag_max=new_size;
> if(bag_i>=bag_max) bag_i=0;
> }
>
>
> inline int FindEmptySlotInBag()
> {
> if(bag_max==0) ResizeBag(3000);
> if(bag_max==0) return -1;
>
> if(bag_i>=bag_max) bag_i=0;
>
> for(int i=0;i<bag_max;i++)
> {
> if(!bag[bag_i].enabled) return bag_i;
> bag_i++;
> if(bag_i>=bag_max) bag_i=0;
> }
>
> int old_size=bag_max;
> ResizeBag(bag_max*2);
> if(bag_max==old_size) return -1;
>
> return FindEmptySlotInBag();
> }
>
>
> void ResetBag()
> {
> for(int i=0;i<bag_max;i++) bag[i].enabled=0;
> bag_i=0;
> }
>
> void VisualizeBag()
> {
>
> for(int i=0; i<bag_max; i++)
> {
> int x = i%200;
> int y = i/200;
>
> if(x>=frame_size_x) continue;
> if(y>=frame_size_y) continue;
>
> int color = 0x00c0c0;
> if(!bag[i].enabled) color = 0x003010;
> else
> {
>
> if(bag[i].type=='smo') color = 0x808080;
> if(bag[i].type=='deb') color = 0xc00000;
> if(bag[i].type=='bull') color = 0xffff00;
>
> }
>
> for(int q=0; q<3; q++)
> for(int p=0; p<3; p++)
> SetPixelUnsafe(10+x*3+p,10+ y*3+q,color);
> }
> }
>
>
> i also made this function above to visualise it in runtime
> this video shows it at worrk
>
> https://www.youtube.com/watch?v=DN9kmveSYTs
>
> note those grreen blocks are for empty slots,
> gray are for smokes (those trails) yellow for bullets and
> red are for debris (this what is generated in explosions)
> - normally its not easy to overflow initial 3000
> size but i specially shooted many rockets to make
> unusual amount of debris to create and bag growed
> (which is seen)
>
> hovever this growing may be a problem to potential errors as as
> i once wrote i just reallock the bag in loops that iterate over it
> (but as for a moment i was not checking for those errors
>
>
>
i should add yet method of addition
int BagAdd(Position *p,int type,int ttl)
{
int i=FindEmptySlotInBag();
if(i<0) return -1;
memset(&bag[i],0,sizeof(Object3D));
bag[i].enabled=1;
bag[i].p=*p;
bag[i].type=type;
bag[i].ttl=ttl;
//..... more fields to initialise
bag_i=i+1; if(bag_i>=bag_max) bag_i=0;
return i;
}
Technical Details
Message-ID:
<11ecfnl$1rqsu$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11ecdnp$1qvjl$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: fir <profesor.fir@gmail.com> Newsgroups: comp.lang.c Subject: Re: fir's bag visualisation Date: Fri, 27 Nov 2026 18:47:31 +0100 Organization: A noiseless patient Spider Lines: 166 Message-ID: <11ecfnl$1rqsu$1@dont-email.me> References: <11ecdnp$1qvjl$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 27 Nov 2026 17:47:34 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1960862"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Yu06wXG7VeyMtECY/G0VOfBFLmCSJ8A0="; posting-host="c7a520b7ed792b31a196e960bdbc916b" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 SeaMonkey/2.53.24 Cancel-Lock: sha1:1h7everb8x49UKJRy+TTg2zhRRs= sha256:jjJQ1jdKWW7NqpLZzspF++ltOY5nr9lOq9emkg21+n4= sha1:p10cyQXFnqSoR2J/5Q8xiBPNovY= sha256:bc39+Tyzw5WDmFT/NlWaDfm3MRlxPoaaC82GRzc7LwA= In-Reply-To: <11ecdnp$1qvjl$1@dont-email.me>