29 BitMapPic p(
"/Users/nathanst/hog2/textures/u.bmp");
30 p.
Save(
"/Users/nathanst/hog2/textures/out.bmp");
32 glGenTextures(1, &
wall);
34 glBindTexture(GL_TEXTURE_2D,
wall);
37 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
38 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
39 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
63 corners[0] = corners[1] = corners[2] = 0;
125 for (
int x = 0; x <
width; x++)
126 for (
int y = 0; y <
height; y++)
171 for (
int x = 0; x <
width; x++)
180 newLand =
new Tile *[newWidth];
181 for (
int x = 0; x < newWidth; x++)
183 newLand[x] =
new Tile [newHeight];
184 for (
int y = 0; y < newHeight; y++)
202 for (
int x = 0; x <
width; x++)
delete []
land[x];
214 int MinX =
width-1, MinY =
height-1, MaxX = 0, MaxY = 0;
215 for (
int x = 0; x <
width; x++)
217 for (
int y = 0; y <
height; y++)
229 int newWidth = MaxX-MinX+1;
230 int newHeight = MaxY-MinY+1;
232 newLand =
new Tile *[newWidth];
233 for (
int x = 0; x < newWidth; x++)
235 newLand[x] =
new Tile [newHeight];
236 for (
int y = 0; y < newHeight; y++)
238 newLand[x][y] =
land[x+MinX][y+MinY];
241 for (
int x = 0; x <
width; x++)
delete []
land[x];
261 for (
int x = 0; x <
width; x++)
267 FILE *f = fopen(filename,
"r");
275 printf(
"Error! Can't open file %s\n", filename);
280 for (
int x = 0; x <
width; x++)
296 for (
int x = 0; x <
width; x++)
304 int num = fscanf(f,
"type %s\nheight %d\nwidth %d\nmap\n", format, &
height, &
width);
308 if (strcmp(format,
"octile") == 0)
310 else if (strcmp(format,
"octile-corner") == 0)
312 else if (strcmp(format,
"raw") == 0)
327 printf(
"Unknown map type; aborting load!\n");
346 for (
int x = 0; x < wide; x++)
351 for (
int x = 0; x < wide; x++)
353 fread(&
land[x][0],
sizeof(
Tile), wide, f);
354 for (
int y = 0; y < high; y++)
372 for (
int y = 0; y < high; y++)
374 for (
int x = 0; x < wide; x++)
377 fscanf(f,
"%c", &what);
378 switch (toupper(what))
438 for (
int y = 0; y < high; y++)
440 for (
int x = 0; x < wide; x++)
444 fscanf(f,
"%c", &hc);
446 if ((x > 0) && (y < high-1))
448 if ((x < wide-1) && (y > 0))
450 if ((x > 0) && (y > 0))
452 if ((x < wide-1) && (y < high-1))
457 for (
int y = 0; y < high; y++)
459 for (
int x = 0; x < wide; x++)
462 fscanf(f,
"%c", &what);
463 if ((y == high-1) || (x == wide-1))
467 switch (toupper(what))
525 fread(&h,
sizeof(
header), 1, f);
527 printf(
"MGC: %c%c%c%c\n", h.
magic&0xFF, (h.
magic>>8)&0xFF,
540 if ((
'G' != ((
char)(h.
magic&0xFF))) ||
541 (
'F' != ((
char)(h.
magic>>8)&0xFF)) ||
542 (
'F' != ((
char)(h.
magic>>16)&0xFF)) ||
543 (
' ' != ((
char)(h.
magic>>24)&0xFF)))
545 if (
'G' != ((
char)(h.
magic&0xFF))) printf(
"bad g\n");
546 if (
'F' != ((
char)(h.
magic>>8)&0xFF)) printf(
"bad f1\n");
547 if (
'F' != ((
char)(h.
magic>>16)&0xFF)) printf(
"bad f2\n");
548 if (
' ' != ((
char)(h.
magic>>24)&0xFF)) printf(
"bad <space>\n");
549 printf(
"DAO: bad magic\n");
552 if ((
'V' != ((
char)h.
version&0xFF)) ||
553 (
'4' != ((
char)(h.
version>>8)&0xFF)) ||
554 (
'.' != ((
char)(h.
version>>16)&0xFF)) ||
555 (
'0' != ((
char)(h.
version>>24)&0xFF)))
557 printf(
"DAO: bad version\n");
560 if ((
'P' != ((
char)h.
platform&0xFF)) ||
561 (
'C' != ((
char)(h.
platform>>8)&0xFF)) ||
562 (
' ' != ((
char)(h.
platform>>16)&0xFF)) ||
563 (
' ' != ((
char)(h.
platform>>24)&0xFF)))
565 printf(
"DAO: bad platform\n");
568 if ((
'A' != ((
char)h.
filetype&0xFF)) ||
569 (
'R' != ((
char)(h.
filetype>>8)&0xFF)) ||
570 (
'L' != ((
char)(h.
filetype>>16)&0xFF)) ||
571 (
' ' != ((
char)(h.
filetype>>24)&0xFF)))
573 printf(
"DAO: bad file type\n");
581 printf(
"DAO: bad file version\n");
585 std::vector<fullData> fd;
590 fread(&fd[x].sa,
sizeof(fd[x].sa), 1, f);
600 fd[x].fields.resize(fd[x].sa.fieldCnt);
601 fread(&fd[x].fields[0],
sizeof(
fieldData), fd[x].sa.fieldCnt, f);
607 printf(
"ID: %c%c%c%c\n", fd[x].sa.id&0xFF, (fd[x].sa.id>>8)&0xFF,
608 (fd[x].sa.id>>16)&0xFF, (fd[x].sa.id>>24)&0xFF);
609 for (
unsigned int y = 0; y < fd[x].sa.fieldCnt; y++)
611 printf(
"Field %d of %d\n", y, fd[x].sa.fieldCnt-1);
612 printf(
"LABEL: %d\n TYPE: %d", fd[x].fields[y].label, fd[x].fields[y].fieldType);
613 printf(
"(%s %s %s)\n", fd[x].fields[y].fieldType&0x80?
"list":
"", fd[x].fields[y].fieldType&0x40?
"struct":
"", fd[x].fields[y].fieldType&0x20?
"reference":
"");
614 printf(
"AT: 0x%X (0x%X)\n", fd[x].fields[y].index, fd[x].fields[y].index+h.
dataoffset);
615 fseek(f, fd[x].fields[y].index+h.
dataoffset+0x10, SEEK_SET);
616 if (fd[x].fields[y].label == 3127)
619 fread(&str,
sizeof(
char), 8, f);
621 printf(
"name: %s\n", str);
623 else if ((fd[x].fields[y].label == 3086) ||
624 (fd[x].fields[y].label == 3087) ||
627 (fd[x].fields[y].label == 3092))
630 fread(&
loc,
sizeof(
loc), 1, f);
632 if (fd[x].fields[y].label == 3086)
634 else if (fd[x].fields[y].label == 3087)
636 else if (fd[x].fields[y].label == 3381)
638 else if (fd[x].fields[y].label == 3380)
640 else if (fd[x].fields[y].label == 3092)
643 printf(
"Read data before height/width\n");
646 printf(
"No height/width data\n");
658 fread(&
loc,
sizeof(
loc), 1, f);
660 for (
unsigned int z = 0; z <
loc; z++)
663 fread(&next,
sizeof(next), 1, f);
677 if (fd[x].fields[y].fieldType == 5)
680 fread(&
loc,
sizeof(
loc), 1, f);
681 printf(
"**VALUE: %d\n",
loc);
683 if (fd[x].fields[y].fieldType == 8)
686 fread(&
loc,
sizeof(
loc), 1, f);
687 printf(
"**VALUE: %f\n",
loc);
706 char nextc = fgetc(f);
711 printf(
"illegal stone on row %d, (column pos %d)\n", nrows, currCol);
715 while ((nextc = fgetc(f)) !=
'\n')
723 printf(
"illegal stone on row %d, column pos %d\n", nrows, currCol);
728 ncols =
max(currCol, ncols);
731 printf(
"Dimensions (%d, %d)\n", ncols, nrows);
742 for (
int y = 0; y <
height; y++)
744 bool foundEOL =
false;
745 for (
int x = 0; x <
width; x++)
750 switch (toupper(what))
767 for (; x <
width; x++)
786 for (
int x = 0; x <
width; x++)
788 for (
int y = 0; y <
height; y++)
802 if ((x < 0) || (y < 0) || (x >=
width) || (y >=
height))
819 const int numLegals = 7;
820 char legals[numLegals] = {
' ',
'\n',
'$',
'#',
'@',
'.',
'*'};
821 for (
int x = 0; x < numLegals; x++)
824 printf(
"Found illegal stone: \'%c\'\n", c);
840 FILE *f = fopen(filename,
"w+");
847 printf(
"Error! Couldn't open file to save\n");
865 printf(
"Unable to save to identical map type\n");
877 fprintf(f,
"type octile\nheight %d\nwidth %d\nmap\n",
height,
width);
878 for (
int y = 0; y <
height; y++)
880 for (
int x = 0; x <
width; x++)
884 case kGround: fprintf(f,
".");
break;
885 case kSwamp: fprintf(f,
"S");
break;
886 case kWater: fprintf(f,
"W");
break;
887 case kTrees: fprintf(f,
"T");
break;
888 default: fprintf(f,
"@");
break;
900 fprintf(f,
"type raw\nheight %d\nwidth %d\nmap\n",
height,
width);
901 for (
int x = 0; x <
width; x++)
926 for (
int y = 0; y <
height; y+=2*_scale)
928 for (
int x = 0; x <
width; x+=_scale)
938 switch (
land[x][y].tile1.type) {
941 { printf(
"^");
break; }
942 switch (
land[x][y+1].tile1.type) {
945 default: printf(
"#");
break;
953 { printf(
" ");
break; }
954 switch (
land[x][y+1].tile1.type) {
956 case kGround: printf(
" ");
break;
957 default: printf(
"#");
break;
962 { printf(
",");
break; }
963 switch (
land[x][y+1].tile1.type) {
965 case kGround: printf(
".");
break;
966 case kSwamp: printf(
":");
break;
967 default: printf(
"#");
break;
1031 printf(
"land: %p, land[0] = %p\n",
land,
land[0]);
1050 int32_t x2, int32_t y2,
tTerrain t)
1054 double xdiff = (int)x1-(
int)x2;
1055 double ydiff = (int)y1-(
int)y2;
1056 double dist = sqrt(xdiff*xdiff + ydiff*ydiff);
1058 for (
double c = 0; c < dist; c += 0.5)
1060 double ratio = c/dist;
1061 double newx = (double)x1-xdiff*ratio;
1062 double newy = (double)y1-ydiff*ratio;
1110 if ((x >=
width)||(x<0))
return;
1111 if ((y >=
height)||(x<0))
return;
1409 printf(
"Doing smooth rect between (%ld, %ld) and (%ld, %ld) height %ld\n", x1, y1, x2, y2, h);
1413 for (
int x = x1; x <= x2; x++)
1446 for (
int x = x1; x <= x2; x++)
1479 for (
int y = y1; y <= y2; y++)
1514 for (
int y = y1; y <= y2; y++)
1584 for (
int x = x1; x <= x2; x++)
1586 for (
int y = y1; y <= y2; y++)
1604 if ((x < 0) || (y < 0) || (x >=
width) || (y >=
height))
1663 if ((x < 0) || (y < 0) || (x >=
width) || (y >=
height))
1689 default:
return false;
1696 if ((abs(x1-x2) > 1) || (abs(y1-y2) > 1))
1700 if (x1 == x2 || y1 == y2)
1711 case 0:
return true;
1779 glDisable(GL_LIGHTING);
1786 glDeleteLists(
dList, 1);
1796 printf(
"Drawing land into display list\n");
1797 dList = glGenLists(1);
1798 glNewList(
dList, GL_COMPILE_AND_EXECUTE);
1805 for (
int y = 0; y <
height; y++)
1807 for (
int x = 0; x <
width; x++)
1828 if (_x >=
width)
return false;
1829 if (_y >=
height)
return false;
1830 if ((_x == -1) || (_y == -1))
1832 double _scale, xOffset, yOffset;
1835 _scale = 2.0/(double)(
height);
1840 _scale = 2.0/(double)(
width);
1844 double epsilon = _scale/2.0;
1845 x = -1+_x*_scale+epsilon+xOffset;
1846 y = -1+_y*_scale+epsilon+yOffset;
1863 if (isnan(_x) || isnan(_y))
1896 return (
double)(
height)/2.0;
1897 return (
double)(
width)/2.0;
1903 double _scale, xOffset, yOffset;
1906 _scale = 2.0/(double)(
height);
1911 _scale = 2.0/(double)(
width);
1915 double epsilon = _scale/2.0;
1917 _x = (
loc.
x-epsilon+1-xOffset)/_scale;
1918 _y = (
loc.
y-epsilon+1-yOffset)/_scale;
1928 if ((px < 0) || (py < 0) || (px >=
width) || (py >=
height))
1943 GLdouble xx, yy, zz, rr;
1949 glNormal3d(0, 0, -1);
1964 glColor3f(0.5, 0.5, 0.5);
1975 glEnable(GL_TEXTURE_2D);
1976 glBindTexture(GL_TEXTURE_2D,
wall);
1977 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1991 glDisable(GL_TEXTURE_2D);
2000 glBegin(GL_TRIANGLES);
2003 glBegin(GL_LINE_LOOP);
2040 glNormal3d(0, 0, -1);
2041 glTexCoord2f(0.51,1);
2043 glTexCoord2f(0.51,0.51);
2045 glTexCoord2f(1,0.51);
2093 { glEnd(); glBegin(GL_LINE_LOOP); }
2120 { glEnd(); glBegin(GL_LINE_LOOP); }
2138 glDisable(GL_TEXTURE_2D);
2149 double scaleH = (10.0-vHeight)/10.0;
2160 red = 0;
green = (random()%10)/100.0;
blue = scaleH*1-(random()%10)/100.0;
2165 double r1=0, r2=0, r3=0;
2168 r1 = (random()%10-5)/100.0;
2169 r2 = (random()%10-5)/100.0;
2170 r3 = (random()%5)/100.0;
2173 green = scaleH*.25+r2;
2177 double r1=0, r2=0, r3=0;
2180 r1 = -(random()%8)/100.0;
2181 r2 = -(random()%8)/100.0;
2182 r3 = -(random()%8)/100.0;
2184 red = scaleH*0.9+r1;
2185 green = scaleH*0.9+r2;
2186 blue = scaleH*0.9+r3;
2190 red = (random()%10)/100.0;
2191 green = .45*scaleH-(random()%10)/100.0;
2192 blue = (random()%10)/100.0;
2195 red = (scaleH*.5+(random()%10-5)/100.0)*0.9;
2196 green = (scaleH*.25+(random()%10-5)/100.0)*0.9;
2197 blue = (80+random()%10-5)/100.0;
2200 red = (random()%10)/100.0;
2201 green = scaleH-(random()%10)/100.0;
2202 blue = (random()%10)/100.0;
2205 red = (50+random()%10-5)/100.0;
2206 green = (50+random()%10-5)/100.0;
2207 blue = (50+random()%10-5)/100.0;
2212 if ((darken) && (type !=
kGround))
2214 else if ((darken) && (type ==
kGround))
2246 n.
x = pb.
y * pa.
z - pb.
z * pa.
y;
2247 n.
y = pb.
z * pa.
x - pb.
x * pa.
z;
2248 n.
z = pb.
x * pa.
y - pb.
y * pa.
x;
2251 glNormal3f(n.
x,n.
y,n.
z);
2256 GLdouble xx, yy, zz, rr;
2258 glColor3f(0.5, 0.5, 0.5);
2259 for (
int y = 0; y <
height; y++)
2264 glVertex3f(xx-rr, yy-rr, zz);
2265 glVertex3f(xx-rr, yy+rr, zz);
2267 for (
int x = 1; x <
width; x++)
2274 glVertex3f(xx-rr, yy+rr, zz);
2275 glVertex3f(xx-rr, yy-rr, zz);
2280 glVertex3f(xx-rr, yy-rr, zz);
2281 glVertex3f(xx-rr, yy+rr, zz);
2288 glVertex3f(xx+rr, yy+rr, zz);
2289 glVertex3f(xx+rr, yy-rr, zz);
2321 if ((x < 0) || (y < 0) || (x >=
width) || (y >=
height))
2323 printf(
"ERROR -- trying to set invalid node number!\n");
2341 if ((x < 0) || (y < 0) || (x >=
width) || (y >=
height))
2501 if (x < 0 || y-1 < 0 || x+1 >=
width || y+1 >=
height)
2544 if (x-1 < 0 || y < 0 || x+1 >=
width || y+1 >=
height)