📄 maplist.cpp
字号:
if(strcmp(cp, mr.name.cstring()) == 0){
return &mr;
}
}
return 0;
}
maplist::~maplist(){
//printf("Destroy a maplist\n");
}
const char * maplist::readmapdata(const char *mapname){
FILE *fp = fopen(mapname, "r");
String str;
static char ebuff[512];
if(!fp){
sprintf(ebuff, "Can't open %s", mapname);
return ebuff;
}
while(fgets(ebuff, sizeof ebuff, fp)){
// printf("Read string %s\n", str.cstring());
if(ebuff[0] == '#')
continue;
String str = ebuff;
array_of_String strar, namesize;
strar.getfields(str, ",");
const ne = strar.element_count();
if(ne == 0)
continue;
if(ne != 7 && ne != 9){
sprintf(ebuff, "Bad field count %d on line \"%s\"", ne, str.cstring());
fclose(fp);
return ebuff;
}
// Get the gen - first field contains colon-separated sizes if any
namesize.getfields(strar[0], ":");
mapinfo minf;
minf.name = namesize[0];
minf.osproj = (ne == 7);
if(namesize.element_count() == 1){
QPixmap bm;
if(bm.load(minf.name.cstring())){
minf.pixwidth = bm.width();
minf.pixheight = bm.height();
if(verbose) printf("Map name %s has width %d height %d\n", minf.name.cstring(), bm.width(), bm.height());
}else{
sprintf(ebuff, "File %s: invalid bitmap format\n",strar[0].cstring());
fclose(fp);
return ebuff;
}
}else{
minf.pixwidth = int(namesize[1]);
minf.pixheight = int(namesize[2]);
if(verbose) printf("Map name %s has preset width %d height %d\n", minf.name.cstring(), minf.pixwidth, minf.pixheight);
}
unsigned long x1, y1, x2, y2;
double lat1, long1, lat2, long2;
long east1, north1, east2, north2;
for(int i = 1; i < ne; i++){
const String &csr = strar[i];
const char *cp = csr.cstring();
// printf("Field %d, %s\n", i, cp);
while(isspace(*cp))
cp++;
// Now process each field
switch(i){
case 1:
x1 = strtol(cp, 0, 10);
break;
case 2:
y1 = strtol(cp, 0, 10);
break;
case 3:
if(ne == 7){
cvtngrstr(cp, &east1, &north1, &lat1, &long1);
}else{
lat1 = getlatitude(cp);
}
break;
case 4:
if(ne == 7){
x2 = strtol(cp, 0, 10);
}else{
long1 = getlongitude(cp);
double e, n;
String dummy;
cvtongr(lat1, long1, dummy, e, n);
east1 = (long)e; north1 = (long)n;
}
break;
case 5:
(ne == 7 ? y2 : x2) = strtol(cp, 0, 10);
break;
case 6:
if(ne == 7){
cvtngrstr(cp, &east2, &north2, &lat2, &long2);
}else{
y2 = strtol(cp, 0, 10);
}
break;
case 7:
lat2 = getlatitude(cp);
break;
case 8:{
long2 = getlongitude(cp);
double e, n;
String dummy;
cvtongr(lat2, long2, dummy, e, n);
east2 = (long)e; north2 = (long)n;
break;
}
}
}
// printf("Result is\te1 %ld, n1 %ld, %f:%f\n\te2 %ld, n2 %ld, %f:%f\n", east1, north1, lat1, long1, east2, north2, lat2, long2);
long xdiff = labs(long(x2) - long(x1));
double longitdiff = fabs(long2-long1);
long eastdiff = labs(east2-east1);
// printf("xdiff %ld, longitdiff %g, eastdiff %ld\n", xdiff, longitdiff, eastdiff);
long ydiff = labs(long(y2) - long(y1));
double latdiff = fabs(lat2-lat1);
long northdiff = labs(north2-north1);
// printf("ydiff %ld, latdiff %g, northdiff %ld\n", ydiff, latdiff, northdiff);
double xscale = longitdiff/xdiff, yscale = latdiff/ydiff;
double escale = double(eastdiff)/xdiff, nscale = double(northdiff)/ydiff;
minf.pixperdegwidth = 1/xscale;
minf.pixperdegheight = 1/yscale;
minf.latmax = lat1+y1*yscale;
minf.latmin = lat2-(minf.pixheight-y2)*yscale;
minf.longmax = long2+(minf.pixwidth-x2)*xscale;
minf.longmin = long1-x1*xscale;
minf.eastmin = east1-x1*escale;
minf.eastmax = east2+(minf.pixwidth-x2)*escale;
minf.northmax = north1+y1*nscale;
minf.northmin = north2-(minf.pixheight-y2)*nscale;
minf.pixpermetrewidth = 1/escale;
minf.pixpermetreheight = 1/nscale;
// minf.print();
*this += minf;
//const double deg2rad = atan2(1,1) / 45;
//double latrad = (lat2 + (lat1-lat2)/2)*deg2rad;
//double coslat = cos(latrad);
// printf("deg/pixel: Xscale %g, Yscale %g\n", xscale, yscale);
// printf("metres/pixel: escale %g , nscale %g\n", escale, nscale);
//printf("coslat %g, Expect Xscale to be approx %g\n", coslat, yscale/coslat);
//printf("Scale error is %g%%\n", fabs(xscale-yscale/coslat)*100/xscale);
}
fclose(fp);
return 0;
}
#ifdef BITMAP
bitmap::~bitmap(){
if(bmdp){GlobalFreePtr(bmdp); bmdp=0;}
if(hbitmap)DeleteObject(hbitmap);
}
bitmap::bitmap() : vf(0), bmdp(0), hbitmap(0), bits(0){
}
bitmap::bitmap(const char *fname, howget get) : vf(0), bmdp(0), hbitmap(0), bits(0){
doload(fname, get);
}
void bitmap::doload(const char *fname, howget get){
if(bmdp){GlobalFreePtr(bmdp); bmdp=0;}
if(hbitmap){DeleteObject(hbitmap); hbitmap=0;}
vf = 1;
bits = 0;
// Read a .bmp file
int fdes = _lopen(fname, READ);
// printf("File open returns %d\n", fdes);
if(fdes < 0){
vf = 0;
return;
}
char buff[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
//BITMAPFILEHEADER bmfh;
//BITMAPINFOHEADER bmih;
unsigned rres = _lread(fdes, buff, sizeof buff);
_lclose(fdes);
fdes = -1;
// printf("Read returns %u\n", rres);
if(rres == sizeof buff){
BITMAPFILEHEADER *bmfhp = (BITMAPFILEHEADER *)buff;
// printf("type %.2s, size %lu, offset %lu\n", &(bmfhp->bfType), bmfhp->bfSize, bmfhp->bfOffBits);
BITMAPINFOHEADER *bihp = (BITMAPINFOHEADER *)(buff + sizeof *bmfhp);
unsigned long bihsize;
bihsize = bihp->biSize;
if(bihsize == (unsigned long)sizeof (BITMAPINFOHEADER)){
// It's the right size, so pick up the other bits
dibpixwidth = bihp->biWidth;
dibpixheight = bihp->biHeight;
int bitcount = bihp->biBitCount;
unsigned long ncolours = bihp->biClrUsed;
if(ncolours == 0 && bitcount != 24)
ncolours = 1L << bitcount;
// printf("w %lu, h %lu, bits %u, nc %lu\n", dibpixwidth, dibpixheight, bitcount, ncolours);
unsigned long coltabsize = ncolours*sizeof(RGBQUAD);
// printf("colour table size %lu\n", coltabsize);
bits = coltabsize+bihsize;
assert(bits == bmfhp->bfOffBits - sizeof *bmfhp);
}else{
vf = 0;
}
}else{
vf = 0;
}
if(get == justinfo || vf == 0) return;
/////////////////////////////////////////////////////////////
// Get the full monty
fdes = _lopen(fname, READ);
// printf("File open returns %d\n", fdes);
if(fdes < 0){
vf = 0;
return;
}
BITMAPFILEHEADER *bmfhp = (BITMAPFILEHEADER *)buff;
_lread(fdes, buff, sizeof(*bmfhp)); // skip the unwanted bit
unsigned long bmsz = bmfhp->bfSize-sizeof(*bmfhp);
bmdp = GlobalAllocPtr(GMEM_MOVEABLE, bmsz);
if(bmdp == 0 || _hread(fdes, bmdp, bmsz) != bmsz){
vf = 0;
}
_lclose(fdes);
if(vf == 0 && bmdp){GlobalFreePtr(bmdp); bmdp=0;}
}
int bitmap::drawon(int hwnd, int bmx, int bmy, int windx, int windy, int width, int height){
HDC hdc = GetDC(hwnd);
int rval = 0;
if(hdc){
RECT r;
GetClientRect(hwnd, &r);
if(hbitmap == 0){
if(bmdp == 0)
return 0;
hbitmap = CreateDIBitmap( hdc,
(LPBITMAPINFOHEADER)bmdp,
CBM_INIT,
bmdp+bits,
(LPBITMAPINFO)bmdp,
DIB_RGB_COLORS);
}
if(hbitmap){
GlobalFreePtr(bmdp); bmdp=0;
lDrawBitMapsz(hdc, hbitmap,
windx, windy, // dest x, y
bmx, bmy, // src x, y
width, height, // width, height
SRCCOPY);
rval = 1;
}else{
rval = 0;
}
ReleaseDC(hwnd,hdc);
}
return rval;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -