Re: weird problem



Hmm, i thought i had replied to this. Probably clicked on discard. Ok,
one more time:
The GL variables or functions are specific for glut/opengl, you can
discard these. I only provided the complete functions so you can see
them into context.

ng.h:

/* Grid options */
#define SECTOR_SIZE 1.0
#define GRID_SIZE 35.0
#define GRID_START -10.0
#define GRID_OBJECT 2.0

/* Main database structure for holding objects */
typedef struct objects {
/* Host names */
char *hostname;
char *ip_addr;
/* Host status */
char *status;
/* Host coordinates */
char *x;
char *y;
char *z;
/* SNMPv2-MIB */
char *sysDescr;
char *sysUpTime;
char *sysContact;
char *sysName;
char *sysLocation;
/* IP-MIB */
char *ipForwarding;
char *ipAdEntNetMask;
/* IF-MIB */
char *ifNumber;
struct objects *next;
struct objects *prev;
} objects;

/* Variables */
extern GLint collisions;
extern GLfloat sector_size;
extern GLfloat grid_size;
extern GLfloat grid_start;
extern GLfloat grid_object;
extern objects *o_head,*o_tail;
extern events *e_head,*e_tail;
extern GLint object_count,event_count;
extern GLint x_event,y_event;
extern GLfloat x,y,z;
extern GLfloat a,b,c,d,e,f;
extern GLsizei width,heigth;
extern GLfloat fps;
extern GLint dl_display_grid;

GLvoid display_grid(GLvoid);
GLvoid assign_coordinate(objects *tmp);

display_grid.c:

#include "ng.h"

GLfloat x,y,z;

GLfloat sector_size=SECTOR_SIZE;
GLfloat grid_size;
GLfloat grid_start=GRID_START;
GLfloat grid_object=GRID_OBJECT;

GLvoid display_grid(GLvoid)
{
objects *tmp=o_head;


/* Set initial value */
if(!object_count)
grid_size=GRID_SIZE;

/* Check here if we need to resize the grid */
if(((grid_size*grid_size)/object_count)<=2) {
grid_size+=1.0;
/* Reset collisions */
collisions=0; }

if(object_count!=0&&object_count!=1) {
if((grid_size*grid_size)/object_count>=2) {
if(collisions) {
if((grid_size*grid_size)/collisions>=2) {
grid_size-=1.0;
while(tmp!=NULL) {
free(tmp->x);
free(tmp->y);
free(tmp->z);
assign_coordinate(tmp);
tmp=tmp->next;}
}
}
else if(!collisions) {
grid_size-=1.0;
while(tmp!=NULL) {
free(tmp->x);
free(tmp->y);
free(tmp->z);
assign_coordinate(tmp);
tmp=tmp->next;}
}
}
}

glBegin(GL_QUADS);
for(x=grid_start;x<=grid_size;x+=sector_size) {
for(y=grid_start;y<=grid_size;y+=sector_size) {
/* Set color for QUADS */
glColor4f(0.0f,0.0f,0.2,1.0f);
/* Draw QUADS */
glVertex3f(x,y,0); /* Left bottom */
glVertex3f(x+sector_size,y,0); /* Right bottom */
glVertex3f(x+sector_size,y+sector_size,0); /* Right upper */
glVertex3f(x,y+sector_size,0); /* Left upper */
}
}
glEnd();

glBegin(GL_LINES);
for(x=grid_start;x<=grid_size;x+=sector_size) {
for(y=grid_start;y<=grid_size;y+=sector_size) {
/* Set color for LINES */
glColor4f(0.0f,0.2f,0.0,0.5f);
/* Draw lines */
glVertex3f(x,y,0);
glVertex3f(x+sector_size,y,0);
glVertex3f(x+sector_size,y,0);
glVertex3f(x+sector_size,y+sector_size,0);
glVertex3f(x+sector_size,y+sector_size,0);
glVertex3f(x,y+sector_size,0);
glVertex3f(x,y+sector_size,0);
glVertex3f(x,y,0);
}
}
glEnd();

}

and assign_coordinate.c:

#include "ng.h"

GLint collisions;

GLvoid assign_coordinate(objects *tmp)
{
objects *search=o_head;

if(!tmp->prev&&object_count==1)
srand((unsigned int)time(NULL));

/* Assign coordinate planes */
if((tmp->x=malloc(sizeof(float)+7))==NULL) {
fprintf(stderr,"assign_coordinate: Could not allocate memory for x
plane\n");
perror("malloc");
exit(1);}
snprintf(o_tail->x,sizeof(float)+7,"%.0f",((grid_size-grid_start)*rand())/RAND_MAX+grid_start);
if((tmp->y=malloc(sizeof(float)+7))==NULL) {
fprintf(stderr,"assign_coordinate: Could not allocate memory for y
plane\n");
perror("malloc");
exit(1);}
snprintf(o_tail->y,sizeof(float)+7,"%.0f",grid_object);
if((tmp->z=malloc(sizeof(float)+7))==NULL) {
fprintf(stderr,"assign_coordinate: Could not allocate memory for z
plane\n");
perror("malloc");
exit(1);}
snprintf(o_tail->z,sizeof(float)+7,"%.0f",((grid_size-grid_start)*rand())/RAND_MAX+grid_start);

/* Check for possible collision */
while(search!=NULL) {
if(collisions>0&&collisions==((grid_size-grid_start)*(grid_size-grid_start)))
{
fprintf(stderr,"assign_coordinate: Sector collisions 100%%.
Collisions: %d - Objects: %d - Grid size: %.0f [%.0f^2 sectors]\n",
collisions,object_count,(grid_size-grid_start)*(grid_size-grid_start),grid_size-grid_start);
perror("assign_coordinate");
exit(1);}
if(tmp!=search&&
!strcmp(tmp->x,search->x)&&
!strcmp(tmp->y,search->y)&&
!strcmp(tmp->z,search->z)) {
collisions++;
free(tmp->x);
free(tmp->y);
free(tmp->z);
assign_coordinate(tmp);}
search=search->next;}
}

.