25 static inline void swap(
int& a,
int& b) {
int tmp=a; a=b; b=tmp; }
26 static inline void sort(
int& a,
int& b) {
if (a>b) swap(a,b); }
27 static inline void swap(
double& a,
double& b) {
double tmp=a; a=b; b=tmp; }
28 static inline void sort(
double& a,
double& b) {
if (a>b) swap(a,b); }
29 static void draw_strip(BYTE* dst,
int pitch,
int pixsize,
int x,
int y,
int len,
Color color);
30 static void draw_vline(BYTE* dst,
int pitch,
int pixsize,
int x,
int y,
int len,
Color color);
36 void plot(
int x,
int y, DWORD val,
int exch=0);
53 BYTE* dst = s + y*pitch + x*pixsize;
56 case 1: *dst = (BYTE) val;
break;
57 case 2: { LPWORD dst2 = (LPWORD) dst; *dst2 = (WORD) val; }
break;
58 case 4: { LPDWORD dst4 = (LPDWORD) dst; *dst4 = (DWORD) val; }
break;
65 : type(BMP_SOLID), width(0), height(0),
66 ownpix(false), alpha_loaded(false), texture(false),
67 pix(0), hipix(0), mapsize(0),
74 : type(t), width(w), height(h),
75 ownpix(false), alpha_loaded(false), texture(false),
76 pix(p), hipix(0), mapsize(w*h),
79 sprintf_s(
filename,
"Bitmap(%d, %d, index, type=%d)", w, h, (
int) t);
83 : type(t), width(w), height(h),
84 ownpix(false), alpha_loaded(false), texture(false),
85 pix(0), hipix(p), mapsize(w*h),
88 sprintf_s(
filename,
"Bitmap(%d, %d, hicolor, type=%d)", w, h, (
int) t);
125 return sizeof(
Color);
138 return (BYTE*)
hipix;
154 if (sx < 0 || sy < 0 || sx >= srcBmp.
Width() || sy >= srcBmp.
Height())
160 int spitch = srcBmp.
Width();
161 int rowlen = w *
sizeof(
Color);
166 for (
int i = 0; i < h; i++) {
167 memcpy(dst, src, rowlen);
173 for (
int i = 0; i < h; i++) {
177 for (
int n = 0; n < w; n++) {
192 int spitch = srcBmp.
Width();
197 for (
int j = 0; j < h; j++) {
198 for (
int i = 0; i < w; i++) {
199 dst[i].
Set(src[i].Formatted());
207 for (
int i = 0; i < h; i++) {
211 for (
int n = 0; n < w; n++) {
228 int spitch = srcBmp.
Width();
233 for (
int i = 0; i < h; i++) {
234 #pragma warning(suppress: 28183)
235 memcpy(dst, src, rowlen);
403 for (
int i = 0; i <
mapsize; i++) {
423 for (
int i = 0; i <
mapsize; i++) {
446 for (
int i = 0; i <
mapsize; i++) {
447 if ((p->
Value() & Color::RGBMask) == m)
468 BYTE index = c.
Index();
470 for (
int i = 0; i <
mapsize; i++)
476 DWORD value = c.
Value();
478 for (
int i = 0; i <
mapsize; i++) {
495 double dx = (double)
width / (
double) w;
496 double dy = (double)
height / (
double) h;
502 Color* buf =
new(__FILE__,__LINE__)
Color[w*h];
510 for (
int y = 0; y < h; y++) {
511 int y_offset = (int) (y * dy);
512 for (
int x = 0; x < w; x++) {
513 int x_offset = (int) (x * dx);
537 for (
int y = 0; y < h; y++) {
538 int y_offset = (int) (y * dy);
539 for (
int x = 0; x < w; x++) {
540 int x_offset = (int) (x * dx);
541 src =
pix + (y_offset *
width) + x_offset;
575 for (
int y = 0; y <
height; y++) {
576 for (
int x = 0; x <
width; x++) {
577 *dst++ = src->
Index();
605 for (
int y = 0; y <
height; y++) {
606 for (
int x = 0; x <
width; x++) {
607 *dst++ = src->
Index();
622 static int FindBestTexSize(
int n,
int max_size)
627 for (
int i = 0; i < 12; i++) {
633 int dx = abs(n-size);
673 int best_width = FindBestTexSize(
width, max_tex_size);
674 int best_height = FindBestTexSize(
height, max_tex_size);
678 if (best_width > best_height) {
679 aspect = best_width / best_height;
681 if (aspect > max_tex_aspect)
682 best_height = best_width / max_tex_aspect;
686 aspect = best_height / best_width;
688 if (aspect > max_tex_aspect)
689 best_width = best_height / max_tex_aspect;
694 ScaleTo(best_width, best_width);
730 result = (
pix + y*
width + x)->Index();
813 for (
int y = 0; y < 64; y++) {
814 for (
int x = 0; x < 64; x++) {
815 double distance = sqrt((x-32.0)*(x-32.0) + (y-32.0)*(y-32.0));
816 if (distance > 31.0) distance = 31.0;
817 BYTE color = 24 + (BYTE) distance;
819 if (x == 0 || y == 0) color = 255;
820 else if (x == 32 || y == 32) color = 251;
838 for (
int i = 0; i < bitmap_cache.
size(); i++) {
839 if (bitmap_cache[i]->
Handle() == bmp_id) {
840 return bitmap_cache[i];
850 for (
int i = 0; i < bitmap_cache.
size(); i++) {
851 if (!_stricmp(bitmap_cache[i]->
GetFilename(), filename)) {
852 return bitmap_cache[i];
868 for (
int i = 0; i < bitmap_cache.
size(); i++) {
869 Bitmap* bmp = bitmap_cache[i];
885 DWORD result =
sizeof(bitmap_cache);
886 result += bitmap_cache.
size() *
sizeof(
Bitmap*);
888 for (
int i = 0; i < bitmap_cache.
size(); i++) {
889 Bitmap* bmp = bitmap_cache[i];
910 if (x1 < 0 || x1 >=
width)
return false;
920 if (y1 < 0 || y1 >=
height)
return false;
934 double m = (double)(y2-y1) / (double)(x2-x1);
935 double b = (double) y1 - (m * x1);
938 if (x1 < 0) { x1 = 0; y1 = (int) b; }
939 if (x1 >=
width)
return false;
940 if (x2 < 0)
return false;
941 if (x2 >
width-1) { x2 =
width-1; y2 = (int) (m * x2 + b); }
943 if (y1 < 0 && y2 < 0)
return false;
946 if (y1 < 0) { y1 = 0; x1 = (int) (-b/m); }
947 if (y1 >=
height) { y1 =
height-1; x1 = (int) ((y1-b)/m); }
948 if (y2 < 0) { y2 = 0; x2 = (int) (-b/m); }
949 if (y2 >=
height) { y2 =
height-1; x2 = (int) ((y2-b)/m); }
955 goto clip_horizontal;
969 if (x1 < 0 || x1 >=
width)
return false;
979 if (y1 < 0 || y1 >=
height)
return false;
993 double m = (double)(y2-y1) / (double)(x2-x1);
994 double b = (double) y1 - (m * x1);
997 if (x1 < 0) { x1 = 0; y1 = b; }
998 if (x1 >=
width)
return false;
999 if (x2 < 0)
return false;
1000 if (x2 >
width-1) { x2 =
width-1; y2 = (m * x2 + b); }
1002 if (y1 < 0 && y2 < 0)
return false;
1005 if (y1 < 0) { y1 = 0; x1 = (-b/m); }
1007 if (y2 < 0) { y2 = 0; x2 = (-b/m); }
1014 goto clip_horizontal;
1035 if (x1 < 0 || x1 >=
width)
return;
1048 if (y1 < 0 || y1 >=
height)
return;
1064 double m = (double)(y2-y1) / (double)(x2-x1);
1065 double b = (double) y1 - (m * x1);
1068 if (x1 < 0) { x1 = 0; y1 = (int) b; }
1069 if (x1 >=
width)
return;
1071 if (x2 >
width-1) { x2 =
width-1; y2 = (int) (m * x2 + b); }
1073 if (y1 < 0 && y2 < 0)
return;
1076 if (y1 < 0) { y1 = 0; x1 = (int) (-b/m); }
1077 if (y1 >=
height) { y1 =
height-1; x1 = (int) ((y1-b)/m); }
1078 if (y2 < 0) { y2 = 0; x2 = (int) (-b/m); }
1079 if (y2 >=
height) { y2 =
height-1; x2 = (int) ((y2-b)/m); }
1088 goto draw_horizontal;
1100 int sign_x=1, sign_y=1, step, reflect;
1101 int i, inc1, inc2, c, D, x_end, pixleft;
1115 if (sign_x == sign_y)
1136 x_end = (dx - 1) / 4;
1137 pixleft = (dx - 1) % 4;
1139 plotter.
plot(x1, y1, pix, reflect);
1140 plotter.
plot(x2, y2, pix, reflect);
1142 inc2 = 4 * dy - 2 * dx;
1148 for (i = 0; i < x_end; i++) {
1153 plotter.
plot(x1, y1, pix, reflect);
1154 plotter.
plot(++x1, y1, pix, reflect);
1156 plotter.
plot(x2, y2, pix, reflect);
1157 plotter.
plot(--x2, y2, pix, reflect);
1163 plotter.
plot(x1, y1, pix, reflect);
1164 plotter.
plot(++x1, y1 += step, pix, reflect);
1166 plotter.
plot(x2, y2, pix, reflect);
1167 plotter.
plot(--x2, y2 -= step, pix, reflect);
1171 plotter.
plot(x1, y1 += step, pix, reflect);
1172 plotter.
plot(++x1, y1, pix, reflect);
1174 plotter.
plot(x2, y2 -= step, pix, reflect);
1175 plotter.
plot(--x2, y2, pix, reflect);
1184 plotter.
plot(++x1, y1, pix, reflect);
1186 plotter.
plot(++x1, y1, pix, reflect);
1188 plotter.
plot(--x2, y2, pix, reflect);
1192 plotter.
plot(++x1, y1, pix, reflect);
1194 plotter.
plot(++x1, y1 += step, pix, reflect);
1196 plotter.
plot(--x2, y2, pix, reflect);
1200 plotter.
plot(++x1, y1 += step, pix, reflect);
1202 plotter.
plot(++x1, y1, pix, reflect);
1204 plotter.
plot(--x2, y2 -= step, pix, reflect);
1214 for (i = 0; i < x_end; i++) {
1219 plotter.
plot(x1, y1 += step, pix, reflect);
1220 plotter.
plot(++x1, y1 += step, pix, reflect);
1222 plotter.
plot(x2, y2 -= step, pix, reflect);
1223 plotter.
plot(--x2, y2 -= step, pix, reflect);
1228 plotter.
plot(x1, y1, pix, reflect);
1229 plotter.
plot(++x1, y1 += step, pix, reflect);
1232 plotter.
plot(x2, y2, pix, reflect);
1233 plotter.
plot(--x2, y2 -= step, pix, reflect);
1236 plotter.
plot(x1, y1 += step, pix, reflect);
1237 plotter.
plot(++x1, y1, pix, reflect);
1239 plotter.
plot(x2, y2 -= step, pix, reflect);
1240 plotter.
plot(--x2, y2, pix, reflect);
1248 plotter.
plot(++x1, y1 += step, pix, reflect);
1250 plotter.
plot(++x1, y1 += step, pix, reflect);
1252 plotter.
plot(--x2, y2 -= step, pix, reflect);
1255 plotter.
plot(++x1, y1, pix, reflect);
1257 plotter.
plot(++x1, y1 += step, pix, reflect);
1259 plotter.
plot(--x2, y2, pix, reflect);
1262 plotter.
plot(++x1, y1 += step, pix, reflect);
1264 plotter.
plot(++x1, y1, pix, reflect);
1267 plotter.
plot(--x2, y2 -= step, pix, reflect);
1269 plotter.
plot(--x2, y2, pix, reflect);
1288 if (fw == 0 || fh == 0)
return;
1291 int left = (x1 >= 0);
1292 int right = (x2 <=
width);
1293 int top = (y1 >= 0);
1294 int bottom = (y2 <=
height);
1298 int pitch =
Pitch();
1301 if (left) draw_vline(s, pitch, pixsize, x1, y1, fh, color);
1302 if (right) draw_vline(s, pitch, pixsize, x2, y1, fh, color);
1303 if (top) draw_strip(s, pitch, pixsize, x1, y1, fw, color);
1304 if (bottom) draw_strip(s, pitch, pixsize, x1, y2, fw, color);
1314 if (r.
w == 0 || r.
h == 0)
return;
1322 int left = (x1 >= 0);
1323 int right = (x2 <=
width);
1324 int top = (y1 >= 0);
1325 int bottom = (y2 <=
height);
1329 int pitch =
Pitch();
1332 if (left) draw_vline(s, pitch, pixsize, x1, y1, r.
h, color);
1333 if (right) draw_vline(s, pitch, pixsize, x2, y1, r.
h, color);
1334 if (top) draw_strip(s, pitch, pixsize, x1, y1, r.
w, color);
1335 if (bottom) draw_strip(s, pitch, pixsize, x1, y2, r.
w, color);
1354 if (fw == 0 || fh == 0)
return;
1358 int pitch =
Pitch();
1361 for (
int i = 0; i < fh; i++)
1362 draw_strip(s, pitch, pixsize, x1, y1+i, fw, color);
1386 if (fw == 0 || fh == 0)
return;
1390 int pitch =
Pitch();
1393 for (
int i = 0; i < fh; i++)
1394 draw_strip(s, pitch, pixsize, x1, y1+i, fw, color);
1415 if (fw < 1 || fh < 1)
return;
1418 if (x1 >=
width || x2 < 0)
return;
1419 if (y1 >=
height || y2 < 0)
return;
1421 double a = fw / 2.0;
1422 double b = fh / 2.0;
1432 if (x1 < 0 && y1 < 0 && x2 >
width && y2 >
height) {
1433 double r2 = (a2<b2) ? a2 : b2;
1438 double ul = (x1-x0)*(x1-x0) + (y1-y0)*(y1-y0);
1439 double ur = (x2-x0)*(x2-x0) + (y1-y0)*(y1-y0);
1440 double ll = (x1-x0)*(x1-x0) + (y2-y0)*(y2-y0);
1441 double lr = (x2-x0)*(x2-x0) + (y2-y0)*(y2-y0);
1443 if (ul > r2 && ur > r2 && ll > r2 && lr > r2)
1450 double d1 = (b2)-(a2*b)+(0.25*a2);
1451 while ((a2)*(y-0.5) > (b2)*(x+1)) {
1455 d1 += b2*(2*x+3) + a2*(-2*y+2);
1464 double d2 = b2*(x+0.5)*(x+0.5) + a2*(y-1)*(y-1) - a2*b2;
1467 d2 += b2*(2*x+2) + a2*(-2*y+3);
1487 int pitch =
Pitch();
1493 int bottom = y0+y+1;
1496 if (left >=
width || right < 0)
return;
1497 if (top >=
height || bottom < 0)
return;
1500 DWORD cf = c.
Value();
1502 if (left >= 0 && top >= 0 && quad&1) {
1503 dst = s + top*pitch + left*pixsize;
1506 case 1: *dst = (BYTE) cf;
break;
1507 case 2: { LPWORD sw = (LPWORD) dst; *sw = (WORD) cf; }
break;
1508 case 4: { LPDWORD sd = (LPDWORD) dst; *sd = (DWORD) cf; }
break;
1512 if (right < width && top >= 0 && quad&2) {
1513 dst = s + top*pitch + right*pixsize;
1516 case 1: *dst = (BYTE) cf;
break;
1517 case 2: { LPWORD sw = (LPWORD) dst; *sw = (WORD) cf; }
break;
1518 case 4: { LPDWORD sd = (LPDWORD) dst; *sd = (DWORD) cf; }
break;
1522 if (left >= 0 && bottom <
height && quad&4) {
1523 dst = s + bottom*pitch + left*pixsize;
1526 case 1: *dst = (BYTE) cf;
break;
1527 case 2: { LPWORD sw = (LPWORD) dst; *sw = (WORD) cf; }
break;
1528 case 4: { LPDWORD sd = (LPDWORD) dst; *sd = (DWORD) cf; }
break;
1533 dst = s + bottom*pitch + right*pixsize;
1536 case 1: *dst = (BYTE) cf;
break;
1537 case 2: { LPWORD sw = (LPWORD) dst; *sw = (WORD) cf; }
break;
1538 case 4: { LPDWORD sd = (LPDWORD) dst; *sd = (DWORD) cf; }
break;
1545 static void draw_strip(BYTE* s,
int pitch,
int pixsize,
int x,
int y,
int len,
Color color)
1548 s += y*pitch + x*pixsize;
1555 memset(s, (BYTE) value, len);
1560 LPWORD sw = (LPWORD) s;
1561 for (
int x = 0; x < len; x++) {
1562 *sw++ = (WORD) value;
1569 for (
int x = 0; x < len; x++) {
1579 static void draw_vline(BYTE* s,
int pitch,
int pixsize,
int x,
int y,
int len,
Color color)
1582 s += (y)*pitch + (x)*pixsize;
1588 for (
int y = 0; y < len; y++) {
1596 LPWORD sw = (LPWORD) s;
1599 for (
int y = 0; y < len; y++) {
1610 for (
int y = 0; y < len; y++) {