1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
|
// This file is part of Golly.
// See docs/License.html for the copyright notice.
/* -------------------- Some notes on Golly's display code ---------------------
The rectangular area used to display patterns is called the viewport.
It's represented by a window of class PatternView defined in wxview.h.
The global viewptr points to a PatternView window which is created in
MainFrame::MainFrame() in wxmain.cpp.
All drawing in the viewport is done in this module using OpenGL 1.
The main rendering routine is DrawView() -- see the end of this module.
DrawView() is called from PatternView::OnPaint(), the update event handler
for the viewport window. Update events are created automatically by the
wxWidgets event dispatcher, or they can be created manually by calling
Refresh().
DrawView() does the following tasks:
- Fills the entire viewport with the state 0 color.
- Calls currlayer->algo->draw() to draw the current pattern. It passes
in renderer, an instance of golly_render (derived from liferender) which
has these methods:
- pixblit() draws a pixmap containing at least one live cell.
- getcolors() provides access to the current layer's color arrays.
Note that currlayer->algo->draw() does all the hard work of figuring
out which parts of the viewport are dead and building all the pixmaps
for the live parts. The pixmaps contain suitably shrunken images
when the scale is < 1:1 (ie. mag < 0).
Each lifealgo needs to implement its own draw() method; for example,
hlifealgo::draw() in hlifedraw.cpp.
- Calls DrawGridLines() to overlay grid lines if they are visible.
- Calls DrawGridBorder() to draw border around a bounded universe.
- Calls DrawSelection() to overlay a translucent selection rectangle
if a selection exists and any part of it is visible.
- Calls DrawStackedLayers() to overlay multiple layers using the current
layer's scale and location.
- If the user is doing a paste, DrawPasteImage() creates a suitable
viewport and draws the paste pattern (stored in pastelayer).
- Calls DrawControls() if the translucent controls need to be drawn.
- Calls DrawOverlay() if the current overlay needs to be drawn.
----------------------------------------------------------------------------- */
#include <string.h> // for memcpy
#include "wx/wxprec.h" // for compilers that support precompilation
#ifndef WX_PRECOMP
#include "wx/wx.h" // for all others include the necessary headers
#endif
#include "wx/rawbmp.h" // for wxAlphaPixelData
#include "bigint.h"
#include "lifealgo.h"
#include "viewport.h"
#include "wxgolly.h" // for viewptr, bigview, statusptr
#include "wxutils.h" // for Warning
#include "wxprefs.h" // for showgridlines, mingridmag, swapcolors, etc
#include "wxstatus.h" // for statusptr->...
#include "wxview.h" // for viewptr->...
#include "wxlayer.h" // currlayer, GetLayer, etc
#include "wxoverlay.h" // curroverlay, overlay_position
#include "wxrender.h"
// -----------------------------------------------------------------------------
// local data used in golly_render routines
int currwd, currht; // current width and height of viewport, in pixels
int scalefactor; // current scale factor (1, 2, 4, 8 or 16)
unsigned char dead_alpha = 255; // alpha value for dead pixels
unsigned char live_alpha = 255; // alpha value for live pixels
GLuint rgbatexture = 0; // texture name for drawing RGBA bitmaps
GLuint icontexture = 0; // texture name for drawing icons
GLuint celltexture = 0; // texture name for drawing magnified cells
GLuint tiletexture = 0; // texture name for tiled drawing
unsigned char* iconatlas = NULL; // pointer to texture atlas for current set of icons
unsigned char* cellatlas = NULL; // pointer to texture atlas for current set of magnified cells
unsigned char* itemgrid = NULL; // pointer to buffer for 16x16 cell/icon grid
const int itemgridwidth = 512; // item grid width in pixels
const int itemgridheight = 512; // item grid height in pixels
const int itemgridrows = 16; // number of item rows
const int itemgridcols = 16; // number of item columns
float iconscalew = 1.0;
float iconscaleh = 1.0;
unsigned char* cellatlasPOT = NULL; // pointer to power of 2 texture atlas for magnified cells
unsigned char* tilebuffer = NULL; // pointer to tile texture buffer (will be power of 2 in size)
float cellscalew = 1.0;
float cellscaleh = 1.0;
// cellatlas needs to be rebuilt if any of these parameters change
int prevnum = 0; // previous number of live states
int prevsize = 0; // previous cell size
unsigned char prevalpha; // previous alpha component
unsigned char prevr[256]; // previous red component of each state
unsigned char prevg[256]; // previous green component of each state
unsigned char prevb[256]; // previous blue component of each state
bool prevborders = false; // previous cell borders setting
// fixed texture coordinates used by glTexCoordPointer
static const GLshort texture_coordinates[] = { 0,0, 1,0, 0,1, 1,1 };
// for drawing paste pattern
Layer* pastelayer; // layer containing the paste pattern
wxRect pastebbox; // bounding box in cell coords (not necessarily minimal)
unsigned char* modedata[4] = {NULL}; // RGBA data for drawing current paste mode (And, Copy, Or, Xor)
const int modewd = 32; // width of each modedata
const int modeht = 16; // height of each modedata
// for drawing translucent controls
unsigned char* ctrlsbitmap = NULL; // RGBA data for controls bitmap
unsigned char* darkbutt = NULL; // RGBA data for darkening one button
int controlswd; // width of ctrlsbitmap
int controlsht; // height of ctrlsbitmap
// include controls_xpm (XPM data for controls bitmap)
#include "bitmaps/controls.xpm"
// these constants must match image dimensions in bitmaps/controls.xpm
const int buttborder = 6; // size of outer border
const int buttsize = 22; // size of each button
const int buttsperrow = 3; // # of buttons in each row
const int numbutts = 15; // # of buttons
const int rowgap = 4; // vertical gap after first 2 rows
// currently clicked control
control_id currcontrol = NO_CONTROL;
// dynamic vertex buffer for grid lines
// start size is enough for 4096x4096 screen resolution at zoom 4
long vertexsize = 8192 * sizeof(GLfloat);
GLfloat *vertexbuffer = (GLfloat *)malloc(vertexsize);
// fixed vertex and texture coordinate buffers for drawing magnified cells or icons
// for speed cells are drawn in blocks of 4096
// size is enough for 4096 cells and must be a multiple of 12
long magbuffersize = 4096 * 12 * sizeof(GLfloat);
GLfloat *magvertexbuffer = (GLfloat *)malloc(magbuffersize);
GLfloat *magtextcoordbuffer = (GLfloat *)malloc(magbuffersize);
#ifdef __WXMSW__
// this constant isn't defined in the OpenGL headers on Windows (XP at least)
#define GL_CLAMP_TO_EDGE 0x812F
#endif
// -----------------------------------------------------------------------------
static void SetColor(int r, int g, int b, int a)
{
glColor4ub(r, g, b, a);
}
// -----------------------------------------------------------------------------
static void FillRect(int x, int y, int wd, int ht)
{
GLfloat rect[] = {
(float)x, (float)y+ht, // left, bottom
(float)x+wd, (float)y+ht, // right, bottom
(float)x+wd, (float)y, // right, top
(float)x, (float)y, // left, top
};
glVertexPointer(2, GL_FLOAT, 0, rect);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
// -----------------------------------------------------------------------------
static void EnableTextures()
{
if (!glIsEnabled(GL_TEXTURE_2D)) {
// restore texture color and enable textures
SetColor(255, 255, 255, 255);
glEnable(GL_TEXTURE_2D);
}
}
// -----------------------------------------------------------------------------
static void DisableTextures()
{
if (glIsEnabled(GL_TEXTURE_2D)) {
glDisable(GL_TEXTURE_2D);
};
}
// -----------------------------------------------------------------------------
void CreateTranslucentControls()
{
wxImage image(controls_xpm);
controlswd = image.GetWidth();
controlsht = image.GetHeight();
// create ctrlsbitmap and initialize its RGBA data based on pixels in image
ctrlsbitmap = (unsigned char*) malloc(controlswd * controlsht * 4);
if (ctrlsbitmap) {
int p = 0;
for ( int y = 0; y < controlsht; y++ ) {
for ( int x = 0; x < controlswd; x++ ) {
unsigned char r = image.GetRed(x,y);
unsigned char g = image.GetGreen(x,y);
unsigned char b = image.GetBlue(x,y);
if (r == 0 && g == 0 && b == 0) {
// make black pixel 100% transparent
ctrlsbitmap[p++] = 0;
ctrlsbitmap[p++] = 0;
ctrlsbitmap[p++] = 0;
ctrlsbitmap[p++] = 0;
} else {
// make all non-black pixels translucent
ctrlsbitmap[p++] = r;
ctrlsbitmap[p++] = g;
ctrlsbitmap[p++] = b;
ctrlsbitmap[p++] = 192; // 75% opaque
}
}
}
}
// allocate bitmap for darkening a clicked button
darkbutt = (unsigned char*) malloc(buttsize * buttsize * 4);
// create bitmaps for drawing each paste mode
paste_mode savemode = pmode;
for (int i = 0; i < 4; i++) {
pmode = (paste_mode) i;
wxString pmodestr = wxString(GetPasteMode(),wxConvLocal); // uses pmode
wxBitmap modemap(modewd, modeht, 32);
wxMemoryDC dc;
dc.SelectObject(modemap);
wxRect r(0, 0, modewd, modeht);
wxBrush brush(*wxWHITE);
FillRect(dc, r, brush);
dc.SetFont(*statusptr->GetStatusFont());
dc.SetBackgroundMode(wxSOLID);
dc.SetTextBackground(*wxWHITE);
dc.SetTextForeground(*wxBLACK);
dc.SetPen(*wxBLACK);
int textwd, textht;
dc.GetTextExtent(pmodestr, &textwd, &textht);
textwd += 4;
dc.DrawText(pmodestr, 2, modeht - statusptr->GetTextAscent() - 4);
dc.SelectObject(wxNullBitmap);
// now convert modemap data into RGBA data suitable for passing into DrawRGBAData
modedata[i] = (unsigned char*) malloc(modewd * modeht * 4);
if (modedata[i]) {
int j = 0;
unsigned char* m = modedata[i];
wxAlphaPixelData data(modemap);
if (data) {
wxAlphaPixelData::Iterator p(data);
for (int y = 0; y < modeht; y++) {
wxAlphaPixelData::Iterator rowstart = p;
for (int x = 0; x < modewd; x++) {
if (x > textwd) {
m[j++] = 0;
m[j++] = 0;
m[j++] = 0;
m[j++] = 0;
} else {
m[j++] = p.Red();
m[j++] = p.Green();
m[j++] = p.Blue();
m[j++] = 255;
}
p++;
}
p = rowstart;
p.OffsetY(data, 1);
}
}
}
}
pmode = savemode;
}
// -----------------------------------------------------------------------------
void DestroyDrawingData()
{
if (cellatlas) free(cellatlas);
if (cellatlasPOT) {
free(cellatlasPOT);
cellatlasPOT = NULL;
}
if (itemgrid) {
free(itemgrid);
itemgrid = NULL;
}
if (tilebuffer) {
free(tilebuffer);
tilebuffer = NULL;
}
if (ctrlsbitmap) free(ctrlsbitmap);
if (darkbutt) free(darkbutt);
for (int i = 0; i < 4; i++) {
if (modedata[i]) free(modedata[i]);
}
}
// -----------------------------------------------------------------------------
unsigned char* GetTexture(unsigned char *data, int *w, int *h, GLuint *texturename, float *scalew, float *scaleh)
{
// enable textures
EnableTextures();
// create the texture name once
if (*texturename == 0) glGenTextures(1, texturename);
glBindTexture(GL_TEXTURE_2D, *texturename);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// initially assume texture width and height same as data
int texturew = *w;
int textureh = *h;
unsigned char* pow2data = NULL;
// set scale to 1 if specified
if (scalew) *scalew = 1.0;
if (scaleh) *scaleh = 1.0;
// OpenGL versions earlier than 2.0 require textures to be a power of 2 in size (POT)
if (glMajor < 2) {
// check if width is a POT
if (texturew & (texturew - 1)) {
texturew = 1;
while (texturew < *w) texturew <<= 1;
}
// check if height is a POT
if (textureh & (textureh - 1)) {
textureh = 1;
while (textureh < *h) textureh <<= 1;
}
// check if texture size is now different than the data size
if (*w != texturew || *h != textureh) {
// allocate memory for POT texture using calloc so pixels outside supplied data will be transparent
pow2data = (unsigned char*)calloc(texturew * textureh, 4);
if (!pow2data) Fatal(_("Cound not allocate POT buffer!"));
// copy the data to the top left of the POT texture
unsigned char* srcptr = data;
unsigned char* dstptr = pow2data;
for (int y = 0; y < *h; y++) {
memcpy(dstptr, srcptr, *w * 4);
srcptr += *w * 4;
dstptr += texturew * 4;
}
// compute the width and height scale factors for the potentially resized texture
if (scalew) *scalew = (float)*w / texturew;
if (scaleh) *scaleh = (float)*h / textureh;
// return the new size
*w = texturew;
*h = textureh;
// use the POT buffer for the texture
data = pow2data;
}
}
// create the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texturew, textureh, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
// return the POT buffer (or NULL if not needed/allocated)
return pow2data;
}
// -----------------------------------------------------------------------------
void DrawRGBAData(unsigned char* rgbadata, int x, int y, int w, int h)
{
// check if data fits in single texture
if (w <= glMaxTextureSize && h <= glMaxTextureSize) {
// convert texture to POT if needed
int texturew = w;
int textureh = h;
unsigned char* pow2data = GetTexture(rgbadata, &texturew, &textureh, &rgbatexture, NULL, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // avoids edge effects when scaling
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // ditto
glTexCoordPointer(2, GL_SHORT, 0, texture_coordinates);
GLfloat vertices[] = {
(float)x, (float)y,
(float)x + texturew, (float)y,
(float)x, (float)y + textureh,
(float)x + texturew, (float)y + textureh,
};
glVertexPointer(2, GL_FLOAT, 0, vertices);
// draw the texture
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// free the POT buffer if allocated
if (pow2data) {
free(pow2data);
}
}
else {
// use the maximum texture size as the tile size
int tilesize = glMaxTextureSize;
// compute number of tiles in the x and y direction
int tilex = ((w - 1) / tilesize) + 1;
int tiley = ((h - 1) / tilesize) + 1;
// create the buffer for the tile once (this will be a power of 2)
if (tilebuffer == 0) tilebuffer = (unsigned char*)calloc(tilesize * tilesize, 4);
if (!tilebuffer) Fatal(_("Could not allocate tile buffer!"));
// enable textures
EnableTextures();
// create the texture name and texture once
if (tiletexture == 0) {
// generate the texture name
glGenTextures(1, &tiletexture);
// make the named texture current
glBindTexture(GL_TEXTURE_2D, tiletexture);
// setup the texture paramters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // avoids edge effects when scaling
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // ditto
// create the texture and have OpenGL allocate the memory
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tilesize, tilesize, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
}
else {
// make the named texture current
glBindTexture(GL_TEXTURE_2D, tiletexture);
}
// draw each row
for (int iy = 0; iy < tiley; iy++) {
// compute the tile height for tiles in this row
int tileh = h - (iy * tilesize);
if (tileh > tilesize) tileh = tilesize;
// draw each tile in the row
for (int ix = 0; ix < tilex ; ix++) {
// compute the tile width
int tilew = w - (ix * tilesize);
if (tilew > tilesize) tilew = tilesize;
// copy the relevant data top left into the texture buffer
// no need to clear unused portion since it isn't rendered below
unsigned char* srcptr = rgbadata + (ix * tilesize * 4) + (iy * tilesize * w * 4);
unsigned char* dstptr = tilebuffer;
for (int r = 0; r < tileh; r++) {
// copy data
memcpy(dstptr, srcptr, tilew * 4);
// next row
srcptr += w * 4;
dstptr += tilew * 4;
}
// update the texture with the tile data
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tilew, tileh, GL_RGBA, GL_UNSIGNED_BYTE, tilebuffer);
// set the vertices for the texture position
GLfloat vertices[] = {
(float)(x + (ix * tilesize)), (float)(y + (iy * tilesize)),
(float)(x + (ix * tilesize) + tilew), (float)(y + (iy * tilesize)),
(float)(x + (ix * tilesize)), (float)(y + (iy * tilesize) + tileh),
(float)(x + (ix * tilesize) + tilew), (float)(y + (iy * tilesize) + tileh),
};
glVertexPointer(2, GL_FLOAT, 0, vertices);
// set the part of the texture to draw
GLfloat xscale = (double)tilew / tilesize;
GLfloat yscale = (double)tileh / tilesize;
GLfloat coordinates[] = { 0, 0, xscale, 0, 0, yscale, xscale, yscale };
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
// draw the tile
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
}
}
}
// -----------------------------------------------------------------------------
void Create16x16Grid(unsigned char* itemrow, int itemsize, int numitems) {
// item width in bytes
int itemwidth = itemsize * 4;
// source pixel row size in bytes
int sourcerow = itemwidth * numitems;
// destination pixel row size in bytes
int destrow = itemgridwidth * 4;
// destination item row size in bytes
int destitemrow = destrow * itemsize;
// allocate the 16x16 item grid if not already allocated
if (!itemgrid) {
// maximum item size is 32x32 RGBA pixels
itemgrid = (unsigned char*)malloc(itemgridrows * itemgridcols * 4 * 32 * 32);
}
// copy the items into the 16x16 buffer
for (int i = 0; i < numitems; i++) {
// get the source item
unsigned char* source = itemrow + i * itemwidth;
// get the destination position
unsigned char* dest = itemgrid + (i / itemgridcols) * destitemrow + (i & (itemgridcols - 1)) * itemwidth;
// copy item into destination
for (int y = 0; y < itemsize; y++) {
memcpy(dest, source, itemwidth);
source += sourcerow;
dest += destrow;
}
}
}
// -----------------------------------------------------------------------------
static void LoadIconAtlas(int iconsize, int numicons)
{
// convert row to 2D grid of icons
Create16x16Grid(iconatlas, iconsize, numicons);
// enable textures
EnableTextures();
// create the texture name once
if (icontexture == 0) glGenTextures(1, &icontexture);
glBindTexture(GL_TEXTURE_2D, icontexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// put the icons in the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, itemgridwidth, itemgridheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, itemgrid);
}
// -----------------------------------------------------------------------------
void DrawOneIcon(wxDC& dc, int x, int y, wxBitmap* icon,
unsigned char deadr, unsigned char deadg, unsigned char deadb,
unsigned char liver, unsigned char liveg, unsigned char liveb,
bool multicolor)
{
// draw a single icon (either multi-color or grayscale) outside the viewport,
// so we don't use OpenGL calls in here
int wd = icon->GetWidth();
int ht = icon->GetHeight();
wxBitmap pixmap(wd, ht, 32);
wxAlphaPixelData pxldata(pixmap);
if (pxldata) {
#if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0)
pxldata.UseAlpha();
#endif
wxAlphaPixelData::Iterator p(pxldata);
wxAlphaPixelData icondata(*icon);
if (icondata) {
wxAlphaPixelData::Iterator iconpxl(icondata);
for (int i = 0; i < ht; i++) {
wxAlphaPixelData::Iterator pixmaprow = p;
wxAlphaPixelData::Iterator iconrow = iconpxl;
for (int j = 0; j < wd; j++) {
if (iconpxl.Red() || iconpxl.Green() || iconpxl.Blue()) {
if (multicolor) {
// use non-black pixel in multi-colored icon
if (swapcolors) {
p.Red() = 255 - iconpxl.Red();
p.Green() = 255 - iconpxl.Green();
p.Blue() = 255 - iconpxl.Blue();
} else {
p.Red() = iconpxl.Red();
p.Green() = iconpxl.Green();
p.Blue() = iconpxl.Blue();
}
} else {
// grayscale icon
if (iconpxl.Red() == 255) {
// replace white pixel with live cell color
p.Red() = liver;
p.Green() = liveg;
p.Blue() = liveb;
} else {
// replace gray pixel with appropriate shade between
// live and dead cell colors
float frac = (float)(iconpxl.Red()) / 255.0;
p.Red() = (int)(deadr + frac * (liver - deadr) + 0.5);
p.Green() = (int)(deadg + frac * (liveg - deadg) + 0.5);
p.Blue() = (int)(deadb + frac * (liveb - deadb) + 0.5);
}
}
} else {
// replace black pixel with dead cell color
p.Red() = deadr;
p.Green() = deadg;
p.Blue() = deadb;
}
#if defined(__WXGTK__) || wxCHECK_VERSION(2,9,0)
p.Alpha() = 255;
#endif
p++;
iconpxl++;
}
// move to next row of pixmap
p = pixmaprow;
p.OffsetY(pxldata, 1);
// move to next row of icon bitmap
iconpxl = iconrow;
iconpxl.OffsetY(icondata, 1);
}
}
}
dc.DrawBitmap(pixmap, x, y);
}
// -----------------------------------------------------------------------------
static bool ChangeCellAtlas(int cellsize, int numcells, unsigned char alpha, bool borders)
{
if (numcells != prevnum) return true;
if (cellsize != prevsize) return true;
if (alpha != prevalpha) return true;
if (borders != prevborders) return true;
for (int state = 1; state <= numcells; state++) {
if (currlayer->cellr[state] != prevr[state]) return true;
if (currlayer->cellg[state] != prevg[state]) return true;
if (currlayer->cellb[state] != prevb[state]) return true;
}
return false; // no need to change cellatlas
}
// -----------------------------------------------------------------------------
static void LoadCellAtlas(int cellsize, int numcells, unsigned char alpha)
{
// cellatlas might need to be (re)created
if (ChangeCellAtlas(cellsize, numcells, alpha, cellborders)) {
prevnum = numcells;
prevsize = cellsize;
prevalpha = alpha;
prevborders = cellborders;
for (int state = 1; state <= numcells; state++) {
prevr[state] = currlayer->cellr[state];
prevg[state] = currlayer->cellg[state];
prevb[state] = currlayer->cellb[state];
}
if (cellatlas) free(cellatlas);
// allocate enough memory for texture atlas to store RGBA pixels for a row of cells
// (note that we use calloc so all alpha bytes are initially 0)
int rowbytes = numcells * cellsize * 4;
cellatlas = (unsigned char*) calloc(rowbytes * cellsize, 1);
if (cellatlas) {
// determine whether zoomed cells have a border
bool haveborder = (cellborders && cellsize > 2);
// set pixels in top row
int tpos = 0;
for (int state = 1; state <= numcells; state++) {
unsigned char r = currlayer->cellr[state];
unsigned char g = currlayer->cellg[state];
unsigned char b = currlayer->cellb[state];
// if the cell size is > 2 and cellborders are on then leave a 1 pixel gap at right
// and bottom edge of each cell
int cellwd = cellsize - (haveborder ? 1 : 0);
for (int i = 0; i < cellwd; i++) {
cellatlas[tpos++] = r;
cellatlas[tpos++] = g;
cellatlas[tpos++] = b;
cellatlas[tpos++] = alpha;
}
if (haveborder) tpos += 4; // skip transparent pixel at right edge of cell
}
// copy top row to remaining rows
int remrows = cellsize - (haveborder ? 2 : 1);
for (int i = 1; i <= remrows; i++) {
memcpy(&cellatlas[i * rowbytes], cellatlas, rowbytes);
}
}
}
// convert row to 2D grid of icons
Create16x16Grid(cellatlas, cellsize, numcells);
// enable textures
EnableTextures();
// create the texture name once
if (celltexture == 0) glGenTextures(1, &celltexture);
glBindTexture(GL_TEXTURE_2D, celltexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// put the cells in the texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, itemgridwidth, itemgridheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, itemgrid);
}
// -----------------------------------------------------------------------------
void DrawCells(unsigned char* statedata, int x, int y, int w, int h, int pmscale, int stride, int numstates, GLuint texture)
{
// called from golly_render::pixblit to draw cells magnified by pmscale (2, 4, ... 2^MAX_MAG)
// uses the supplied texture to draw solid cells or icons
//
// one cell = 2 triangles = 6 vertices = 12 coordinates for GL_TRIANGLES
int v = 0;
int t = 0;
int max = magbuffersize / sizeof(*magvertexbuffer);
float tscale = (float)pmscale / itemgridwidth;
EnableTextures();
glBindTexture(GL_TEXTURE_2D, texture);
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
unsigned char state = statedata[row*stride + col];
if (state > 0) {
// add cell to buffer
int xpos = x + col * pmscale;
int ypos = y + row * pmscale;
magvertexbuffer[v++] = xpos;
magvertexbuffer[v++] = ypos;
magvertexbuffer[v++] = xpos + pmscale;
magvertexbuffer[v++] = ypos;
magvertexbuffer[v++] = xpos;
magvertexbuffer[v++] = ypos + pmscale;
magvertexbuffer[v++] = xpos + pmscale;
magvertexbuffer[v++] = ypos;
magvertexbuffer[v++] = xpos;
magvertexbuffer[v++] = ypos + pmscale;
magvertexbuffer[v++] = xpos + pmscale;
magvertexbuffer[v++] = ypos + pmscale;
int tx = (state - 1) & (itemgridcols - 1);
int ty = (state - 1) / itemgridcols;
magtextcoordbuffer[t++] = (float)tx * tscale;
magtextcoordbuffer[t++] = (float)ty * tscale;
magtextcoordbuffer[t++] = (float)(tx + 1) * tscale;
magtextcoordbuffer[t++] = (float)ty * tscale;
magtextcoordbuffer[t++] = (float)tx * tscale;
magtextcoordbuffer[t++] = (float)(ty + 1) * tscale;
magtextcoordbuffer[t++] = (float)(tx + 1) * tscale;
magtextcoordbuffer[t++] = (float)ty * tscale;
magtextcoordbuffer[t++] = (float)tx * tscale;
magtextcoordbuffer[t++] = (float)(ty + 1) * tscale;
magtextcoordbuffer[t++] = (float)(tx + 1) * tscale;
magtextcoordbuffer[t++] = (float)(ty + 1) * tscale;
// check if buffer is full
if (v == max) {
// draw cells in buffer
glTexCoordPointer(2, GL_FLOAT, 0, magtextcoordbuffer);
glVertexPointer(2, GL_FLOAT, 0, magvertexbuffer);
glDrawArrays(GL_TRIANGLES, 0, v / 2);
// reset buffer
v = 0;
t = 0;
}
}
}
}
// draw any remaining cells in buffer
if (v > 0) {
glTexCoordPointer(2, GL_FLOAT, 0, magtextcoordbuffer);
glVertexPointer(2, GL_FLOAT, 0, magvertexbuffer);
glDrawArrays(GL_TRIANGLES, 0, v / 2);
}
}
// -----------------------------------------------------------------------------
class golly_render : public liferender
{
public:
golly_render() {}
virtual ~golly_render() {}
virtual void pixblit(int x, int y, int w, int h, unsigned char* pm, int pmscale);
virtual void getcolors(unsigned char** r, unsigned char** g, unsigned char** b,
unsigned char* deada, unsigned char* livea);
};
golly_render renderer; // create instance
// -----------------------------------------------------------------------------
void golly_render::pixblit(int x, int y, int w, int h, unsigned char* pmdata, int pmscale)
{
if (x >= currwd || y >= currht) return;
if (x + w <= 0 || y + h <= 0) return;
// stride is the horizontal pixel width of the image data
int stride = w/pmscale;
// clip data outside viewport
if (pmscale > 1) {
// pmdata contains 1 byte per `pmscale' pixels, so we must be careful
// and adjust x, y, w and h by multiples of `pmscale' only.
if (x < 0) {
int dx = -x/pmscale*pmscale;
pmdata += dx/pmscale;
w -= dx;
x += dx;
}
if (y < 0) {
int dy = -y/pmscale*pmscale;
pmdata += dy/pmscale*stride;
h -= dy;
y += dy;
}
if (x + w >= currwd + pmscale) w = (currwd - x + pmscale - 1)/pmscale*pmscale;
if (y + h >= currht + pmscale) h = (currht - y + pmscale - 1)/pmscale*pmscale;
}
if (pmscale == 1) {
// draw RGBA pixel data at scale 1:1
DrawRGBAData(pmdata, x, y, w, h);
} else if (showicons && pmscale > 4 && iconatlas) {
// draw icons at scales 1:8 and above
DrawCells(pmdata, x, y, w/pmscale, h/pmscale, pmscale, stride, currlayer->numicons, icontexture);
} else {
// draw magnified cells, assuming pmdata contains (w/pmscale)*(h/pmscale) bytes
// where each byte contains a cell state
DrawCells(pmdata, x, y, w/pmscale, h/pmscale, pmscale, stride, currlayer->numicons, celltexture);
}
}
// -----------------------------------------------------------------------------
void golly_render::getcolors(unsigned char** r, unsigned char** g, unsigned char** b,
unsigned char* deada, unsigned char* livea)
{
*r = currlayer->cellr;
*g = currlayer->cellg;
*b = currlayer->cellb;
*deada = dead_alpha;
*livea = live_alpha;
}
// -----------------------------------------------------------------------------
void DrawSelection(wxRect& rect, bool active)
{
// draw semi-transparent rectangle
DisableTextures();
if (active) {
SetColor(selectrgb->Red(), selectrgb->Green(), selectrgb->Blue(), 128);
} else {
// use light gray to indicate an inactive selection
SetColor(160, 160, 160, 128);
}
FillRect(rect.x, rect.y, rect.width, rect.height);
}
// -----------------------------------------------------------------------------
void InitPaste(Layer* player, wxRect& bbox)
{
// set globals used in DrawPasteImage
pastelayer = player;
pastebbox = bbox;
}
// -----------------------------------------------------------------------------
int PixelsToCells(int pixels, int mag) {
// convert given # of screen pixels to corresponding # of cells
if (mag >= 0) {
int cellsize = 1 << mag;
return (pixels + cellsize - 1) / cellsize;
} else {
// mag < 0; no need to worry about overflow
return pixels << -mag;
}
}
// -----------------------------------------------------------------------------
void DrawPasteImage()
{
int pastemag = currlayer->view->getmag();
// note that viewptr->pasterect.width > 0
int prectwd = viewptr->pasterect.width;
int prectht = viewptr->pasterect.height;
// calculate size of paste image; we could just set it to pasterect size
// but that would be slow and wasteful for large pasterects, so we use
// the following code (the only tricky bit is when plocation = Middle)
int pastewd = prectwd;
int pasteht = prectht;
wxRect cellbox = pastebbox;
if (pastewd > currlayer->view->getwidth() || pasteht > currlayer->view->getheight()) {
if (plocation == Middle) {
// temporary viewport may need to be TWICE size of current viewport
if (pastewd > 2 * currlayer->view->getwidth())
pastewd = 2 * currlayer->view->getwidth();
if (pasteht > 2 * currlayer->view->getheight())
pasteht = 2 * currlayer->view->getheight();
if (pastemag > 0) {
// make sure pastewd/ht don't have partial cells
int cellsize = 1 << pastemag;
if ((pastewd + 1) % cellsize > 0)
pastewd += cellsize - ((pastewd + 1) % cellsize);
if ((pasteht + 1) % cellsize != 0)
pasteht += cellsize - ((pasteht + 1) % cellsize);
}
if (prectwd > pastewd) {
// make sure prectwd - pastewd is an even number of *cells*
if (pastemag > 0) {
int cellsize = 1 << pastemag;
int celldiff = (prectwd - pastewd) / cellsize;
if (celldiff & 1) pastewd += cellsize;
} else {
if ((prectwd - pastewd) & 1) pastewd++;
}
}
if (prectht > pasteht) {
// make sure prectht - pasteht is an even number of *cells*
if (pastemag > 0) {
int cellsize = 1 << pastemag;
int celldiff = (prectht - pasteht) / cellsize;
if (celldiff & 1) pasteht += cellsize;
} else {
if ((prectht - pasteht) & 1) pasteht++;
}
}
} else {
// plocation is at a corner of pasterect so temporary viewport
// may need to be size of current viewport
if (pastewd > currlayer->view->getwidth())
pastewd = currlayer->view->getwidth();
if (pasteht > currlayer->view->getheight())
pasteht = currlayer->view->getheight();
if (pastemag > 0) {
// make sure pastewd/ht don't have partial cells
int cellsize = 1 << pastemag;
int gap = 1; // gap between cells
if (pastemag == 1) gap = 0; // no gap at scale 1:2
if ((pastewd + gap) % cellsize > 0)
pastewd += cellsize - ((pastewd + gap) % cellsize);
if ((pasteht + gap) % cellsize != 0)
pasteht += cellsize - ((pasteht + gap) % cellsize);
}
cellbox.width = PixelsToCells(pastewd, pastemag);
cellbox.height = PixelsToCells(pasteht, pastemag);
if (plocation == TopLeft) {
// show top left corner of pasterect
cellbox.x = pastebbox.x;
cellbox.y = pastebbox.y;
} else if (plocation == TopRight) {
// show top right corner of pasterect
cellbox.x = pastebbox.x + pastebbox.width - cellbox.width;
cellbox.y = pastebbox.y;
} else if (plocation == BottomRight) {
// show bottom right corner of pasterect
cellbox.x = pastebbox.x + pastebbox.width - cellbox.width;
cellbox.y = pastebbox.y + pastebbox.height - cellbox.height;
} else { // plocation == BottomLeft
// show bottom left corner of pasterect
cellbox.x = pastebbox.x;
cellbox.y = pastebbox.y + pastebbox.height - cellbox.height;
}
}
}
wxRect r = viewptr->pasterect;
if (r.width > pastewd || r.height > pasteht) {
// paste image is smaller than pasterect (which can't fit in viewport)
// so shift image depending on plocation
switch (plocation) {
case TopLeft:
// no need to do any shifting
break;
case TopRight:
// shift image to top right corner of pasterect
r.x += r.width - pastewd;
break;
case BottomRight:
// shift image to bottom right corner of pasterect
r.x += r.width - pastewd;
r.y += r.height - pasteht;
break;
case BottomLeft:
// shift image to bottom left corner of pasterect
r.y += r.height - pasteht;
break;
case Middle:
// shift image to middle of pasterect; note that above code
// has ensured (r.width - pastewd) and (r.height - pasteht)
// are an even number of *cells* if pastemag > 0
r.x += (r.width - pastewd) / 2;
r.y += (r.height - pasteht) / 2;
break;
}
}
// set up viewport for drawing paste pattern
pastelayer->view->resize(pastewd, pasteht);
int midx, midy;
if (pastemag > 1) {
// allow for gap between cells
midx = cellbox.x + (cellbox.width - 1) / 2;
midy = cellbox.y + (cellbox.height - 1) / 2;
} else {
midx = cellbox.x + cellbox.width / 2;
midy = cellbox.y + cellbox.height / 2;
}
pastelayer->view->setpositionmag(midx, midy, pastemag);
// temporarily turn off grid lines
bool saveshow = showgridlines;
showgridlines = false;
// dead pixels will be 100% transparent and live pixels 100% opaque
dead_alpha = 0;
live_alpha = 255;
currwd = pastelayer->view->getwidth();
currht = pastelayer->view->getheight();
glTranslatef(r.x, r.y, 0);
// temporarily set currlayer to pastelayer so golly_render routines
// will use the paste pattern's color and icons
Layer* savelayer = currlayer;
currlayer = pastelayer;
if (showicons && pastemag > 2) {
// only show icons at scales 1:8 and above
if (pastemag == 3) {
iconatlas = currlayer->atlas7x7;
LoadIconAtlas(8, currlayer->numicons);
} else if (pastemag == 4) {
iconatlas = currlayer->atlas15x15;
LoadIconAtlas(16, currlayer->numicons);
} else {
iconatlas = currlayer->atlas31x31;
LoadIconAtlas(32, currlayer->numicons);
}
} else if (pastemag > 0) {
LoadCellAtlas(1 << pastemag, currlayer->numicons, 255);
}
if (scalefactor > 1) {
// change scale to 1:1 and increase its size by scalefactor
pastelayer->view->setmag(0);
currwd = currwd * scalefactor;
currht = currht * scalefactor;
pastelayer->view->resize(currwd, currht);
glPushMatrix();
glScalef(1.0/scalefactor, 1.0/scalefactor, 1.0);
pastelayer->algo->draw(*pastelayer->view, renderer);
// restore viewport settings
currwd = currwd / scalefactor;
currht = currht / scalefactor;
// restore OpenGL scale
glPopMatrix();
} else {
// no scaling
pastelayer->algo->draw(*pastelayer->view, renderer);
}
currlayer = savelayer;
showgridlines = saveshow;
glTranslatef(-r.x, -r.y, 0);
// overlay translucent rect to show paste area
DisableTextures();
SetColor(pastergb->Red(), pastergb->Green(), pastergb->Blue(), 64);
r = viewptr->pasterect;
FillRect(r.x, r.y, r.width, r.height);
// show current paste mode
if (r.y > 0 && modedata[(int)pmode]) {
DrawRGBAData(modedata[(int)pmode], r.x, r.y - modeht - 1, modewd, modeht);
}
}
// -----------------------------------------------------------------------------
control_id WhichControl(int x, int y)
{
// determine which button is at x,y in controls bitmap
int col, row;
x -= buttborder;
y -= buttborder;
if (x < 0 || y < 0) return NO_CONTROL;
// allow for vertical gap after first 2 rows
if (y < (buttsize + rowgap)) {
if (y > buttsize) return NO_CONTROL; // in 1st gap
row = 1;
} else if (y < 2*(buttsize + rowgap)) {
if (y > 2*buttsize + rowgap) return NO_CONTROL; // in 2nd gap
row = 2;
} else {
row = 3 + (y - 2*(buttsize + rowgap)) / buttsize;
}
col = 1 + x / buttsize;
if (col < 1 || col > buttsperrow) return NO_CONTROL;
if (row < 1 || row > numbutts/buttsperrow) return NO_CONTROL;
int control = (row - 1) * buttsperrow + col;
return (control_id) control;
}
// -----------------------------------------------------------------------------
void DrawControls(wxRect& rect)
{
if (ctrlsbitmap) {
DrawRGBAData(ctrlsbitmap, rect.x, rect.y, controlswd, controlsht);
if (currcontrol > NO_CONTROL && darkbutt) {
// show clicked control
int i = (int)currcontrol - 1;
int x = buttborder + (i % buttsperrow) * buttsize;
int y = buttborder + (i / buttsperrow) * buttsize;
// allow for vertical gap after first 2 rows
if (i < buttsperrow) {
// y is correct
} else if (i < 2*buttsperrow) {
y += rowgap;
} else {
y += 2*rowgap;
}
// draw one darkened button
int p = 0;
for ( int row = 0; row < buttsize; row++ ) {
for ( int col = 0; col < buttsize; col++ ) {
unsigned char alpha = ctrlsbitmap[((row + y) * controlswd + col + x) * 4 + 3];
if (alpha == 0) {
// pixel is transparent
darkbutt[p++] = 0;
darkbutt[p++] = 0;
darkbutt[p++] = 0;
darkbutt[p++] = 0;
} else {
// pixel is part of button so use a very dark gray
darkbutt[p++] = 20;
darkbutt[p++] = 20;
darkbutt[p++] = 20;
darkbutt[p++] = 128; // 50% opaque
}
}
}
DrawRGBAData(darkbutt, rect.x + x, rect.y + y, buttsize, buttsize);
}
}
}
// -----------------------------------------------------------------------------
void DrawGridLines(int wd, int ht)
{
int cellsize = 1 << currlayer->view->getmag();
int h, v, i, topbold, leftbold;
// compute the vertex buffer size
long neededBufferSize = ((((wd + ht) * 4) / cellsize) + 1) * sizeof(*vertexbuffer);
// check if the current buffer is big enough
if (neededBufferSize > vertexsize) {
// grow the buffer to the required size
vertexbuffer = (GLfloat *)realloc(vertexbuffer, neededBufferSize);
vertexsize = neededBufferSize;
}
// get the vertex buffer
GLfloat *points = vertexbuffer;
int numPoints = 0;
if (showboldlines) {
// ensure that origin cell stays next to bold lines;
// ie. bold lines scroll when pattern is scrolled
pair<bigint, bigint> lefttop = currlayer->view->at(0, 0);
leftbold = lefttop.first.mod_smallint(boldspacing);
topbold = lefttop.second.mod_smallint(boldspacing);
if (currlayer->originx != bigint::zero) {
leftbold -= currlayer->originx.mod_smallint(boldspacing);
}
if (currlayer->originy != bigint::zero) {
topbold -= currlayer->originy.mod_smallint(boldspacing);
}
if (mathcoords) topbold--; // show origin cell above bold line
} else {
// avoid gcc warning
topbold = leftbold = 0;
}
DisableTextures();
glLineWidth(1.0);
// set the stroke color depending on current bg color
int r = currlayer->cellr[0];
int g = currlayer->cellg[0];
int b = currlayer->cellb[0];
int gray = (int) ((r + g + b) / 3.0);
if (gray > 127) {
// darker lines
SetColor(r > 32 ? r - 32 : 0,
g > 32 ? g - 32 : 0,
b > 32 ? b - 32 : 0, 255);
} else {
// lighter lines
SetColor(r + 32 < 256 ? r + 32 : 255,
g + 32 < 256 ? g + 32 : 255,
b + 32 < 256 ? b + 32 : 255, 255);
}
// draw all plain lines first;
// note that we need to add/subtract 0.5 from coordinates to avoid uneven spacing
i = showboldlines ? topbold : 1;
v = 0;
while (true) {
v += cellsize;
if (v > ht) break;
if (showboldlines) i++;
if (i % boldspacing != 0) {
points[numPoints++] = -0.5f;
points[numPoints++] = (float)v - 0.5f;
points[numPoints++] = (float)wd + 0.5f;
points[numPoints++] = (float)v - 0.5f;
}
}
i = showboldlines ? leftbold : 1;
h = 0;
while (true) {
h += cellsize;
if (h > wd) break;
if (showboldlines) i++;
if (i % boldspacing != 0) {
points[numPoints++] = (float)h - 0.5f;
points[numPoints++] = -0.5f;
points[numPoints++] = (float)h - 0.5f;
points[numPoints++] = (float)ht + 0.5;
}
}
// draw all plain lines
glVertexPointer(2, GL_FLOAT, 0, points);
glDrawArrays(GL_LINES, 0, numPoints / 2);
if (showboldlines) {
// draw bold lines in slightly darker/lighter color
numPoints = 0;
if (gray > 127) {
// darker lines
SetColor(r > 64 ? r - 64 : 0,
g > 64 ? g - 64 : 0,
b > 64 ? b - 64 : 0, 255);
} else {
// lighter lines
SetColor(r + 64 < 256 ? r + 64 : 255,
g + 64 < 256 ? g + 64 : 255,
b + 64 < 256 ? b + 64 : 255, 255);
}
i = topbold;
v = 0;
while (true) {
v += cellsize;
if (v > ht) break;
i++;
if (i % boldspacing == 0) {
points[numPoints++] = -0.5f;
points[numPoints++] = (float)v - 0.5f;
points[numPoints++] = (float)wd + 0.5f;
points[numPoints++] = (float)v - 0.5f;
}
}
i = leftbold;
h = 0;
while (true) {
h += cellsize;
if (h > wd) break;
i++;
if (i % boldspacing == 0) {
points[numPoints++] = (float)h - 0.5f;
points[numPoints++] = -0.5f;
points[numPoints++] = (float)h - 0.5f;
points[numPoints++] = (float)ht + 0.5f;
}
}
// draw all bold lines
glVertexPointer(2, GL_FLOAT, 0, points);
glDrawArrays(GL_LINES, 0, numPoints / 2);
}
}
// -----------------------------------------------------------------------------
void DrawGridBorder(int wd, int ht)
{
// universe is bounded so draw any visible border regions
pair<int,int> ltpxl = currlayer->view->screenPosOf(currlayer->algo->gridleft,
currlayer->algo->gridtop,
currlayer->algo);
pair<int,int> rbpxl = currlayer->view->screenPosOf(currlayer->algo->gridright,
currlayer->algo->gridbottom,
currlayer->algo);
int left = ltpxl.first;
int top = ltpxl.second;
int right = rbpxl.first;
int bottom = rbpxl.second;
if (currlayer->algo->gridwd == 0) {
left = 0;
right = wd-1;
}
if (currlayer->algo->gridht == 0) {
top = 0;
bottom = ht-1;
}
// note that right and/or bottom might be INT_MAX so avoid adding to cause overflow
if (currlayer->view->getmag() > 0) {
// move to bottom right pixel of cell at gridright,gridbottom
if (right < wd) right += (1 << currlayer->view->getmag()) - 1;
if (bottom < ht) bottom += (1 << currlayer->view->getmag()) - 1;
if (currlayer->view->getmag() == 1) {
// there are no gaps at scale 1:2
if (right < wd) right++;
if (bottom < ht) bottom++;
}
} else {
if (right < wd) right++;
if (bottom < ht) bottom++;
}
if (left < 0 && right >= wd && top < 0 && bottom >= ht) {
// border isn't visible (ie. grid fills viewport)
return;
}
DisableTextures();
SetColor(borderrgb->Red(), borderrgb->Green(), borderrgb->Blue(), 255);
if (left >= wd || right < 0 || top >= ht || bottom < 0) {
// no part of grid is visible so fill viewport with border
FillRect(0, 0, wd, ht);
return;
}
// avoid drawing overlapping rects below
int rtop = 0;
int rheight = ht;
if (currlayer->algo->gridht > 0) {
if (top > 0) {
// top border is visible
FillRect(0, 0, wd, top);
// reduce size of rect below
rtop = top;
rheight -= top;
}
if (bottom < ht) {
// bottom border is visible
FillRect(0, bottom, wd, ht - bottom);
// reduce size of rect below
rheight -= ht - bottom;
}
}
if (currlayer->algo->gridwd > 0) {
if (left > 0) {
// left border is visible
FillRect(0, rtop, left, rheight);
}
if (right < wd) {
// right border is visible
FillRect(right, rtop, wd - right, rheight);
}
}
}
// -----------------------------------------------------------------------------
void ReplaceAlpha(int iconsize, int numicons, unsigned char oldalpha, unsigned char newalpha)
{
if (iconatlas) {
int numbytes = numicons * iconsize * iconsize * 4;
int i = 3;
while (i < numbytes) {
if (iconatlas[i] == oldalpha) iconatlas[i] = newalpha;
i += 4;
}
}
}
// -----------------------------------------------------------------------------
void DrawOneLayer()
{
// dead pixels will be 100% transparent, and live pixels will use opacity setting
dead_alpha = 0;
live_alpha = int(2.55 * opacity);
int iconsize = 0;
int currmag = currlayer->view->getmag();
if (showicons && currmag > 2) {
// only show icons at scales 1:8 and above
if (currmag == 3) {
iconatlas = currlayer->atlas7x7;
iconsize = 8;
} else if (currmag == 4) {
iconatlas = currlayer->atlas15x15;
iconsize = 16;
} else {
iconatlas = currlayer->atlas31x31;
iconsize = 32;
}
// DANGER: we're making assumptions about what CreateIconAtlas does in wxlayer.cpp
if (live_alpha < 255) {
// this is ugly, but we need to replace the alpha 255 values in iconatlas
// with live_alpha so that LoadIconAtlas will load translucent icons
ReplaceAlpha(iconsize, currlayer->numicons, 255, live_alpha);
}
// load iconatlas for use by DrawCells
LoadIconAtlas(iconsize, currlayer->numicons);
} else if (currmag > 0) {
LoadCellAtlas(1 << currmag, currlayer->numicons, live_alpha);
}
if (scalefactor > 1) {
// temporarily change viewport scale to 1:1 and increase its size by scalefactor
currlayer->view->setmag(0);
currwd = currwd * scalefactor;
currht = currht * scalefactor;
currlayer->view->resize(currwd, currht);
glPushMatrix();
glScalef(1.0/scalefactor, 1.0/scalefactor, 1.0);
currlayer->algo->draw(*currlayer->view, renderer);
// restore viewport settings
currwd = currwd / scalefactor;
currht = currht / scalefactor;
currlayer->view->resize(currwd, currht);
currlayer->view->setmag(currmag);
// restore OpenGL scale
glPopMatrix();
} else {
currlayer->algo->draw(*currlayer->view, renderer);
}
if (showicons && currmag > 2 && live_alpha < 255) {
// restore alpha values in iconatlas
ReplaceAlpha(iconsize, currlayer->numicons, live_alpha, 255);
}
}
// -----------------------------------------------------------------------------
void DrawStackedLayers()
{
// temporarily turn off grid lines
bool saveshow = showgridlines;
showgridlines = false;
// overlay patterns in layers 1..numlayers-1
for ( int i = 1; i < numlayers; i++ ) {
Layer* savelayer = currlayer;
currlayer = GetLayer(i);
// use real current layer's viewport
viewport* saveview = currlayer->view;
currlayer->view = savelayer->view;
if ( !currlayer->algo->isEmpty() ) {
DrawOneLayer();
}
// draw this layer's selection if necessary
wxRect r;
if ( currlayer->currsel.Visible(&r) ) {
DrawSelection(r, i == currindex);
}
// restore viewport and currlayer
currlayer->view = saveview;
currlayer = savelayer;
}
showgridlines = saveshow;
}
// -----------------------------------------------------------------------------
void DrawTileFrame(wxRect& trect, int wd)
{
trect.Inflate(wd);
wxRect r = trect;
r.height = wd;
FillRect(r.x, r.y, r.width, r.height); // top edge
r.y += trect.height - wd;
FillRect(r.x, r.y, r.width, r.height); // bottom edge
r = trect;
r.width = wd;
FillRect(r.x, r.y, r.width, r.height); // left edge
r.x += trect.width - wd;
FillRect(r.x, r.y, r.width, r.height); // right edge
}
// -----------------------------------------------------------------------------
void DrawTileBorders()
{
if (tileborder <= 0) return; // no borders
// draw tile borders in bigview window
int wd, ht;
bigview->GetClientSize(&wd, &ht);
if (wd < 1 || ht < 1) return;
// most people will choose either a very light or very dark color for dead cells,
// so draw mid gray border around non-current tiles
DisableTextures();
SetColor(144, 144, 144, 255);
wxRect trect;
for ( int i = 0; i < numlayers; i++ ) {
if (i != currindex) {
trect = GetLayer(i)->tilerect;
DrawTileFrame(trect, tileborder);
}
}
// draw green border around current tile
trect = GetLayer(currindex)->tilerect;
SetColor(0, 255, 0, 255);
DrawTileFrame(trect, tileborder);
}
// -----------------------------------------------------------------------------
void DrawOverlay()
{
unsigned char* overlaydata = curroverlay->GetOverlayData();
if (overlaydata) {
int wd = curroverlay->GetOverlayWidth();
int ht = curroverlay->GetOverlayHeight();
int x = 0;
int y = 0;
switch (curroverlay->GetOverlayPosition()) {
case topleft:
break;
case topright:
x = currwd - wd;
break;
case bottomright:
x = currwd - wd;
y = currht - ht;
break;
case bottomleft:
y = currht - ht;
break;
case middle:
x = (currwd - wd) / 2;
y = (currht - ht) / 2;
break;
}
// render the overlay
DrawRGBAData(overlaydata, x, y, wd, ht);
// the cursor might need to be changed
curroverlay->CheckCursor();
}
}
// -----------------------------------------------------------------------------
static bool firstview = true;
void DrawView(int tileindex)
{
wxRect r;
Layer* savelayer = NULL;
viewport* saveview0 = NULL;
int colorindex;
int currmag = currlayer->view->getmag();
if (firstview) {
firstview = false;
// draw a tiny pixmap before calling DrawGridLines to avoid crash in NVIDIA driver
// (this probably only happens on Windows but safer to do it on all platforms)
unsigned char data[] = "RGBARGBARGBARGBA";
DrawRGBAData(data, 0, 0, 2, 2); // data has 4 pixels
}
if (curroverlay->OnlyDrawOverlay()) {
DrawOverlay();
return;
}
// if grid is bounded then ensure viewport's central cell is not outside grid edges
if ( currlayer->algo->gridwd > 0) {
if ( currlayer->view->x < currlayer->algo->gridleft )
currlayer->view->setpositionmag(currlayer->algo->gridleft,
currlayer->view->y,
currmag);
else if ( currlayer->view->x > currlayer->algo->gridright )
currlayer->view->setpositionmag(currlayer->algo->gridright,
currlayer->view->y,
currmag);
}
if ( currlayer->algo->gridht > 0) {
if ( currlayer->view->y < currlayer->algo->gridtop )
currlayer->view->setpositionmag(currlayer->view->x,
currlayer->algo->gridtop,
currmag);
else if ( currlayer->view->y > currlayer->algo->gridbottom )
currlayer->view->setpositionmag(currlayer->view->x,
currlayer->algo->gridbottom,
currmag);
}
if ( viewptr->nopattupdate ) {
// don't draw incomplete pattern, just fill background
glClearColor(currlayer->cellr[0]/255.0,
currlayer->cellg[0]/255.0,
currlayer->cellb[0]/255.0,
1.0);
glClear(GL_COLOR_BUFFER_BIT);
// might as well draw grid lines and border
currwd = currlayer->view->getwidth();
currht = currlayer->view->getheight();
if ( viewptr->GridVisible() ) {
DrawGridLines(currwd, currht);
}
if ( currlayer->algo->gridwd > 0 || currlayer->algo->gridht > 0 ) {
DrawGridBorder(currwd, currht);
}
return;
}
if ( numlayers > 1 && tilelayers ) {
if ( tileindex < 0 ) {
DrawTileBorders();
// there's no need to fill bigview's background
return;
}
// tileindex >= 0 so temporarily change some globals to draw this tile
if ( syncviews && tileindex != currindex ) {
// make sure this layer uses same location and scale as current layer
GetLayer(tileindex)->view->setpositionmag(currlayer->view->x,
currlayer->view->y,
currmag);
}
savelayer = currlayer;
currlayer = GetLayer(tileindex);
currmag = currlayer->view->getmag(); // possibly changed if not syncviews
viewptr = currlayer->tilewin;
colorindex = tileindex;
} else if ( numlayers > 1 && stacklayers ) {
// draw all layers starting with layer 0 but using current layer's viewport
savelayer = currlayer;
if ( currindex != 0 ) {
// change currlayer to layer 0
currlayer = GetLayer(0);
saveview0 = currlayer->view;
currlayer->view = savelayer->view;
}
colorindex = 0;
} else {
// just draw the current layer
colorindex = currindex;
}
// fill the background with the current state 0 color
// (note that currlayer might have changed)
glClearColor(currlayer->cellr[0]/255.0,
currlayer->cellg[0]/255.0,
currlayer->cellb[0]/255.0,
1.0);
glClear(GL_COLOR_BUFFER_BIT);
if (showicons && currmag > 2) {
// only show icons at scales 1:8 and above
if (currmag == 3) {
iconatlas = currlayer->atlas7x7;
LoadIconAtlas(8, currlayer->numicons);
} else if (currmag == 4) {
iconatlas = currlayer->atlas15x15;
LoadIconAtlas(16, currlayer->numicons);
} else {
iconatlas = currlayer->atlas31x31;
LoadIconAtlas(32, currlayer->numicons);
}
} else if (currmag > 0) {
LoadCellAtlas(1 << currmag, currlayer->numicons, 255);
}
currwd = currlayer->view->getwidth();
currht = currlayer->view->getheight();
// all pixels are initially opaque
dead_alpha = 255;
live_alpha = 255;
// draw pattern using a sequence of pixblit calls
if (smartscale && currmag <= -1 && currmag >= -4) {
// current scale is from 2^1:1 to 2^4:1
scalefactor = 1 << (-currmag);
// temporarily change viewport scale to 1:1 and increase its size by scalefactor
currlayer->view->setmag(0);
currwd = currwd * scalefactor;
currht = currht * scalefactor;
currlayer->view->resize(currwd, currht);
glPushMatrix();
glScalef(1.0/scalefactor, 1.0/scalefactor, 1.0);
currlayer->algo->draw(*currlayer->view, renderer);
// restore viewport settings
currwd = currwd / scalefactor;
currht = currht / scalefactor;
currlayer->view->resize(currwd, currht);
currlayer->view->setmag(currmag);
// restore OpenGL scale
glPopMatrix();
} else {
// no scaling
scalefactor = 1;
currlayer->algo->draw(*currlayer->view, renderer);
}
if ( viewptr->GridVisible() ) {
DrawGridLines(currwd, currht);
}
// if universe is bounded then draw border regions (if visible)
if ( currlayer->algo->gridwd > 0 || currlayer->algo->gridht > 0 ) {
DrawGridBorder(currwd, currht);
}
if ( currlayer->currsel.Visible(&r) ) {
DrawSelection(r, colorindex == currindex);
}
if ( numlayers > 1 && stacklayers ) {
// must restore currlayer before we call DrawStackedLayers
currlayer = savelayer;
if ( saveview0 ) {
// restore layer 0's viewport
GetLayer(0)->view = saveview0;
}
// draw layers 1, 2, ... numlayers-1
DrawStackedLayers();
}
if ( viewptr->waitingforclick && viewptr->pasterect.width > 0 ) {
DrawPasteImage();
}
if (viewptr->showcontrols) {
DrawControls(viewptr->controlsrect);
}
if (showoverlay) {
if (numlayers > 1 && tilelayers) {
// only display overlay above current tile
if (tileindex == currindex) DrawOverlay();
} else {
DrawOverlay();
}
}
if ( numlayers > 1 && tilelayers ) {
// restore globals changed above
currlayer = savelayer;
viewptr = currlayer->tilewin;
}
}
|