Page 1 of 1

Can anybody explain me these script errors?

PostPosted: Fri Apr 24, 2009 4:30 pm
by asmodeus
Well, I haven't used GE for a time now, and I wanted to try some code. I put this into the global code:
Code: Select all
#define WORD unsigned short int
#define DWORD unsigned long int
#define LONG signed long int

#define M7_ERROR_NONE 0
#define M7_ERROR_SIZE 1
#define M7_ERROR_FORMAT 2
#define M7_ERROR_UNKNOWN 3

#define M7_TEX_NUMBER 256
#define M7_TEX_WIDTH 32
#define M7_TEX_HEIGHT 32
#define M7_MAP_WIDTH 256
#define M7_MAP_HEIGHT 256

int tex[M7_TEX_NUMBER][M7_TEX_WIDTH][M7_TEX_HEIGHT];
int map[M7_MAP_WIDTH][M7_MAP_HEIGHT];
int textotal = 0;

int mode7_loadBitmap(char *fname)
{
  FILE *fp = fopen(fname, "r");
  int err = 0, err2 = 0;
  char *bfType;
  fgets(bfType, 2, fp);
  if(strcmp(bfType, "BM")) err = M7_ERROR_FORMAT;
  DWORD bfSize, bfReserved, bfOffBits;
  fread(bfSize, 4, 1, fp);
  if(fread(bfReserved, 4, 1, fp)) err = M7_ERROR_FORMAT;
  fread(bfOffBits, 4, 1, fp);
  if(!err)
  {
    DWORD biSize;
   LONG biWidth, biHeight;
   WORD biPlanes, biBitCount;
   DWORD biCompression, biSizeImage;
   LONG biXPelsPerMeter, biYPelsPerMeter;
   DWORD biCrlUsed, biCrlImportant;
    if(fread(biSize, 4, 1, fp)!=40) err = M7_ERROR_FORMAT;
   fread(biWidth, 4, 1, fp); fread(biHeight, 4, 1, fp);
   if(biWidth!=M7_TEX_WIDTH||biHeight!=M7_TEX_HEIGHT) err2 = M7_ERROR_SIZE;
   fread(biPlanes, 2, 1, fp); fread(biBitCount, 2, 1, fp);
   if(biBitCount!=24) err = M7_ERROR_FORMAT;
   if(biPlanes!=1) err = M7_ERROR_FORMAT;
   fread(biCompression, 4, 1, fp); fread(biSizeImage, 4, 1, fp);
   if(biCompression) err = M7_ERROR_FORMAT;
   fread(biXPelsPerMeter, 4, 1, fp); fread(biYPelsPerMeter, 4, 1, fp);
   fread(biCrlUsed, 4, 1, fp); fread(biCrlImportant, 4, 1, fp);
   if(!err)
   {
     int xi, yi;
     unsigned int col;
     int ind = textotal;
     textotal++;
     for(yi=M7_TEX_HEIGHT-1;yi>=0;yi--) for(xi=0;xi<M7_TEX_WIDTH;xi++)
     {
       if(!feof(fp)) fread(col, 3, 1, fp);
      else break;
      tex[ind][xi][yi] = col;
     }
     if(yi>=0||xi<M7_TEX_WIDTH)
     {
       for(yi=yi;yi>=0;yi--) for(xi=xi;xi<M7_TEX_WIDTH;xi++) tex[ind][xi][yi] = 0;
     }
   }
  }
  fclose(fp);
  if(err) return err;
  else return err2;
}

GE says:
Error

Error line 27: Unexpected declaration err
Warning line 28: Suspicious pointer conversion
Warning line 29: Suspicious pointer conversion
Warning line 30: Suspicious pointer conversion
Warning line 39: Suspicious pointer conversion
Warning line 40: Suspicious pointer conversion
Warning line 42: Suspicious pointer conversion
Warning line 45: Suspicious pointer conversion
Warning line 47: Suspicious pointer conversion
Warning line 48: Suspicious pointer conversion
Warning line 57: Suspicious pointer conversion

Why unexpected declartion on line 27? There isn't any declaration.

Re: Can anybody explain me these script errors?

PostPosted: Fri Apr 24, 2009 6:10 pm
by skydereign
The unexpected error is because you cannot declare a variable there. They have to be declared either at the top of the global script or at the top of the function they are in. After that you get suspicious pointer conversion. I don't know much about fread, but I thought that the first variable it takes was supposed to be a pointer.

Re: Can anybody explain me these script errors?

PostPosted: Fri Apr 24, 2009 6:26 pm
by asmodeus
skydereign wrote:The unexpected error is because you cannot declare a variable there. They have to be declared either at the top of the global script or at the top of the function they are in.

Oh, I forgot about that. I will try to continue now. Btw, does anybody know why variables has to be declared at the top?

Re: Can anybody explain me these script errors?

PostPosted: Fri Apr 24, 2009 7:09 pm
by skydereign
I believe it has to do with compile vs run time. Memory is done at compile time, with some exceptions, so for a function, it has already been prepared. It could get really complicated if the processor dished out memory anytime it sees it.

Re: Can anybody explain me these script errors?

PostPosted: Fri Apr 24, 2009 10:24 pm
by pyrometal
skydereign wrote:Btw, does anybody know why variables has to be declared at the top?


GE seems to be using "ANSI C", which is the first establish set of convention for the language, only allows variables to be declared at the top of a block. The later standards allow you to declare a variable anywhere inside your main or a function.

skydereign wrote: Memory is done at compile time


Actually, the OS assigns memory to a program upon execution. Variables as memory spaces are not compiled, only the calls needed to create them in the alotted space are.

Re: Can anybody explain me these script errors?

PostPosted: Sat Apr 25, 2009 12:08 pm
by asmodeus
All errors are corrected now. This would be the correct code:
Code: Select all
#define WORD unsigned short int
#define DWORD unsigned long int
#define LONG signed long int

#define M7_ERROR_NONE 0
#define M7_ERROR_SIZE 1
#define M7_ERROR_FORMAT 2
#define M7_ERROR_UNKNOWN 3

#define M7_TEX_NUMBER 256
#define M7_TEX_WIDTH 32
#define M7_TEX_HEIGHT 32
#define M7_MAP_WIDTH 256
#define M7_MAP_HEIGHT 256
int tex[M7_TEX_NUMBER][M7_TEX_WIDTH][M7_TEX_HEIGHT];
int map[M7_MAP_WIDTH][M7_MAP_HEIGHT];
int textotal = 0;

int mode7_loadBitmap(char *fname)
{
  FILE *fp = fopen(fname, "r");
  int err = 0, err2 = 0;
  char *bfType;
  int xi, yi;
  unsigned int col;
  int ind = textotal;
  DWORD bfSize, bfReserved, bfOffBits;
  DWORD biSize;
  LONG biWidth, biHeight;
  WORD biPlanes, biBitCount;
  DWORD biCompression, biSizeImage;
  LONG biXPelsPerMeter, biYPelsPerMeter;
  DWORD biCrlUsed, biCrlImportant;
  fread(&bfType, 2, 1, fp);
  if(strcmp(bfType, "BM")) err = M7_ERROR_FORMAT;
  fread(&bfSize, 4, 1, fp);
  if(fread(&bfReserved, 4, 1, fp)) err = M7_ERROR_FORMAT;
  fread(&bfOffBits, 4, 1, fp);
  if(!err)
  {
    if(fread(&biSize, 4, 1, fp)!=40) err = M7_ERROR_FORMAT;
    fread(&biWidth, 4, 1, fp); fread(&biHeight, 4, 1, fp);
    if(biWidth!=M7_TEX_WIDTH||biHeight!=M7_TEX_HEIGHT) err2 = M7_ERROR_SIZE;
    fread(&biPlanes, 2, 1, fp); fread(&biBitCount, 2, 1, fp);
    if(biBitCount!=24) err = M7_ERROR_FORMAT;
    if(biPlanes!=1) err = M7_ERROR_FORMAT;
    fread(&biCompression, 4, 1, fp); fread(&biSizeImage, 4, 1, fp);
    if(biCompression) err = M7_ERROR_FORMAT;
    fread(&biXPelsPerMeter, 4, 1, fp); fread(&biYPelsPerMeter, 4, 1, fp);
    fread(&biCrlUsed, 4, 1, fp); fread(&biCrlImportant, 4, 1, fp);
    if(!err)
    {
      textotal++;
      for(yi=M7_TEX_HEIGHT-1;yi>=0;yi--) for(xi=0;xi<M7_TEX_WIDTH;xi++)
      {
        if(!feof(fp)) fread(&col, 3, 1, fp);
        else break;
        tex[ind][xi][yi] = col;
      }
      if(yi>=0||xi<M7_TEX_WIDTH)
      {
        for(yi=yi;yi>=0;yi--) for(xi=xi;xi<M7_TEX_WIDTH;xi++) tex[ind][xi][yi] = 0;
      }
    }
  }
  fclose(fp);
  if(err) return err;
  else return err2;
}

In fread() I had to write something like "&myvar" instead of "myvar".