Article View: comp.lang.c
Article #1902Re: fir's bag visualisation
From: fir
Date: Fri, 27 Nov 2026 20:00
Date: Fri, 27 Nov 2026 20:00
173 lines
4671 bytes
4671 bytes
i tested yet thoce no order compactor... by the eye its hard to see
difference in time frames etc (becouse overally scene is dynamic, by my
eye not lurking to much its no clearly visible difference but logically
i think smaller loops are better and no order compactor probably better)
so fial code as for now for the bag would be
enum {MAX_WORLDS = 30};
int bag_i_tab[MAX_WORLDS];
int bag_max_tab[MAX_WORLDS];
int bag_capacity_tab[MAX_WORLDS];
Object3D *bag_tab[MAX_WORLDS];
int bag_i=0;
int bag_max=0;
int bag_capacity=0;
Object3D *bag=NULL;
SwitchBag(int from, int to)
{
bag_i_tab[from] = bag_i ;
bag_max_tab[from] = bag_max;
bag_capacity_tab[from] = bag_capacity;
bag_tab[from] = bag;
bag_i = bag_i_tab[to];
bag_max = bag_max_tab[to];
bag_capacity = bag_capacity_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)
{
if(new_size>bag_capacity)
{
Object3D *new_bag=(Object3D*)realloc(bag,new_size*sizeof(Object3D));
if(!new_bag && new_size>0) { ERROR_("realloc failed in bag
resize"); return; }
bag=new_bag;
if(new_size>bag_capacity)
memset(&bag[bag_capacity],0,(new_size-bag_capacity)*sizeof(Object3D));
bag_capacity=new_size;
}
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) { ERROR_("unexpected state in FindEmptySlotInBag");
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*1.5);
if(bag_max==old_size) { ERROR_("unexpected state in
FindEmptySlotInBag"); 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);
}
}
void CompactBag_KeepOrder()
{
int dst=0;
for(int src=0;src<bag_max;src++)
{
if(!bag[src].enabled) continue;
if(dst!=src) bag[dst]=bag[src];
dst++;
}
for(int i=dst;i<bag_max;i++) bag[i].enabled=0;
bag_i=dst;
bag_max=dst+200;
ResizeBag(bag_max);
}
void CompactBag_NoOrder()
{
int dst=0;
int src=bag_max-1;
while(dst<src)
{
if(bag[dst].enabled) { dst++; continue; }
if(!bag[src].enabled) { src--; continue; }
bag[dst]=bag[src];
bag[src].enabled=0;
dst++;
src--;
}
bag_i=dst;
bag_max=dst+200;
ResizeBag(bag_max);
}
fir
Technical Details
Message-ID:
<11ecng8$1uu5l$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.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> <11ecerh$1reea$1@dont-email.me> <11ecl7q$1u08b$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.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 21:00:07 +0100 Organization: A noiseless patient Spider Lines: 173 Message-ID: <11ecng8$1uu5l$1@dont-email.me> References: <11ecdnp$1qvjl$1@dont-email.me> <11ecerh$1reea$1@dont-email.me> <11ecl7q$1u08b$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 27 Nov 2026 20:00:09 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2062517"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LK9bU5q5fQgvcLUzVzV4n8RVK5OibGHA="; 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:cJYELp7v5DL4cYDoqHpCBzm8eS0= sha256:HJTAsSsI6wuxS5T+oQE05FWyLTfEBc7JiNlixTIkXdg= sha1:mE9QMBhActl1Rg96MAlRpmmEPxg= sha256:eOMYjdq1LpU6ixI+st23B9P+6/GIJTZ9UjLWygqUn3Q= In-Reply-To: <11ecl7q$1u08b$1@dont-email.me>