Free Hero Mesh

Diff
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.

Differences From Artifact [86ecd14102]:

To Artifact [2327f052d7]:


48
49
50
51
52
53
54

55







56
57
58
59
















60
61
62
63
64
65
66
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







+
-
+
+
+
+
+
+
+




+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    ColorFilter color;
    OverlayFilter overlay;
    ShiftFilter shift;
  };
} Filter;

typedef struct {
  union {
  char basename[64];
    char basename[64];
    struct {
      // This nested struct is only used for multidependent pictures.
      char name[10];
      Uint16 size;
    };
  };
  Filter filters[64];
  Uint8 nfilters;
} DependentPicture;

typedef struct {
  char basename[64];
} BaseName;

typedef struct {
  union {
    struct {
      Uint8 nbases;
      Uint8 nchains[3];
    };
    Uint8 nitems[4];
  };
  BaseName bases[63];
  DependentPicture chains[3*63];
} MultiDependent;

typedef struct {
  sqlite3_vtab_cursor super;
  int pos;
} Cursor;

static Uint8 cur_type;
static Uint8 gsizes[16];
283
284
285
286
287
288
289
290

291
292
293
294
295
296
297
306
307
308
309
310
311
312

313
314
315
316
317
318
319
320







-
+








static sqlite3_int64 ask_picture_id(const char*t) {
  sqlite3_stmt*st;
  const char*r=screen_prompt(t);
  int i;
  sqlite3_int64 id=0;
  if(!r || !*r) return 0;
  i=sqlite3_prepare_v2(userdb,"SELECT `ID`, `TYPE` FROM `PICEDIT` WHERE `NAME` = (?1 || '.IMG') OR `NAME` = (?1 || '.DEP');",-1,&st,0);
  i=sqlite3_prepare_v2(userdb,"SELECT `ID`, `TYPE` FROM `PICEDIT` WHERE `NAME`=?1||'.IMG' OR `NAME`=?1||'.DEP' OR (SUBSTR(?1,1,1)='+' AND `NAME`=SUBSTR(?1,2)||'.MUL');",-1,&st,0);
  if(i) {
    screen_message(sqlite3_errmsg(userdb));
    return 0;
  }
  sqlite3_bind_text(st,1,r,-1,0);
  i=sqlite3_step(st);
  if(i==SQLITE_ROW) id=sqlite3_column_int64(st,0),cur_type=sqlite3_column_int(st,1);
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
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







-
+


-
+

+







-
+







        break;
      case SDL_VIDEOEXPOSE:
        goto redraw;
    }
  }
}

static void load_dependent_picture(const Uint8*data,int size,DependentPicture*dp) {
static void load_dependent_picture(const Uint8*data,int size,DependentPicture*dp,int mode) {
  FILE*fp;
  int i,j,k;
  memset(dp,0,sizeof(DependentPicture));
  if(!mode) memset(dp,0,sizeof(DependentPicture));
  if(!size) {
    if(mode) return;
    const char*s=screen_prompt("Name of base picture:");
    if(s) strncpy(dp->basename,s,63);
    return;
  }
  fp=fmemopen((Uint8*)data,size,"r");
  if(!fp) fatal("Error with fmemopen\n");
  i=0;
  for(;;) {
  if(!mode) for(;;) {
    j=fgetc(fp);
    if(j<32) {
      if(j>0) ungetc(j,fp);
      break;
    }
    if(i<63) dp->basename[i++]=j;
  }
1159
1160
1161
1162
1163
1164
1165
1166

1167
1168

1169
1170
1171
1172
1173
1174
1175
1183
1184
1185
1186
1187
1188
1189

1190
1191

1192
1193
1194
1195
1196
1197
1198
1199







-
+

-
+







        dp->filters[i].shift.nshift=k+1;
        break;
    }
  }
  fclose(fp);
}

static void save_dependent_picture(FILE*fp,DependentPicture*dp) {
static void save_dependent_picture(FILE*fp,DependentPicture*dp,int mode) {
  int i,j;
  fwrite(dp->basename,1,strlen(dp->basename)+(dp->filters[0].code<32?0:1),fp);
  if(!mode) fwrite(dp->basename,1,strlen(dp->basename)+(dp->filters[0].code<32?0:1),fp);
  for(i=0;i<dp->nfilters;i++) {
    fputc(dp->filters[i].code,fp);
    switch(dp->filters[i].code) {
      case 8 ... 10:
        fputc(dp->filters[i].color.ncolors,fp);
        fwrite(dp->filters[i].color.colors,1,dp->filters[i].color.ncolors,fp);
        break;
1367
1368
1369
1370
1371
1372
1373

1374
1375


1376
1377
1378
1379








1380
1381
1382
1383
1384
1385
1386
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







+
-
-
+
+
-
-
-
-
+
+
+
+
+
+
+
+







  SDL_Rect r;
  SDL_Event ev;
  set_cursor(XC_arrow);
  redraw:
  r.x=r.y=0; r.w=screen->w; r.h=screen->h;
  SDL_LockSurface(screen);
  SDL_FillRect(screen,&r,0xF0);
  if(name) {
  draw_text(0,0,"Dependent Image:",0xF0,0xF7);
  draw_text(136,0,name,0xF0,0xF5);
    draw_text(0,0,"Dependent Image:",0xF0,0xF7);
    draw_text(136,0,name,0xF0,0xF5);
  draw_text(0,8,"<ESC> Exit  <\x18/\x19> Cursor  <SP> Edit  <INS> Insert  <DEL> Delete",0xF0,0xFB);
  draw_text(0,16,c<0?"\x1A":"\xFA",0xF0,c<0?0xFA:0xF2);
  draw_text(16,16,"Base:",0xF0,0xF7);
  draw_text(64,16,dp->basename,0xF0,0xFE);
    draw_text(16,16,"Base:",0xF0,0xF7);
    draw_text(64,16,dp->basename,0xF0,0xFE);
  } else {
    draw_text(0,0,"Filter Chain",0xF0,0xF7);
    draw_text(16,16,"<Base>",0xF0,0xF8);
  }
  draw_text(0,8,"<ESC> Exit  <\x18/\x19> Cursor  <SP> Edit  <INS> Insert  <DEL> Delete",0xF0,0xFB);
  draw_text(0,16,c<0?"\x1A":"\xFA",0xF0,c<0?0xFA:0xF2);
  for(i=0,y=24;i<dp->nfilters;i++) {
    draw_text(0,y,c==i?"\x1A":"\xFA",0xF0,c==i?0xFA:0xF2);
    switch(j=dp->filters[i].code) {
      case 0 ... 7:
        draw_text(16,y,txt[j],0xF0,0xF7);
        y+=8;
        break;
1429
1430
1431
1432
1433
1434
1435

1436
1437
1438
1439
1440
1441
1442
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472







+







          case SDLK_ESCAPE: return;
          case SDLK_UP: case SDLK_KP8: case SDLK_a: case SDLK_k: if(c>=0) --c; break;
          case SDLK_DOWN: case SDLK_KP2: case SDLK_z: case SDLK_j: ++c; break;
          case SDLK_INSERT: case SDLK_KP0:
            if(add_filter(dp,txt,c)) // maybe fallthrough
          case SDLK_SPACE:
            if(c==-1) {
              if(!name) break;
              s=screen_prompt("Name of base picture:");
              if(s && *s) strncpy(dp->basename,s,63);
            } else if(c<dp->nfilters) {
              switch(dp->filters[c].code) {
                case 8 ... 10:
                  edit_color_filter(&dp->filters[c].color);
                  break;
1460
1461
1462
1463
1464
1465
1466
1467



















































































































































1468
1469
1470
1471

1472
1473
1474
1475
1476
1477
1478
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








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




+







        }
        goto redraw;
      case SDL_VIDEOEXPOSE:
        goto redraw;
    }
  }
}

static void load_multidependent(const Uint8*data,int size,MultiDependent*mp) {
  Uint32 at=4;
  int i,j,k;
  memset(mp,0,sizeof(MultiDependent));
  if(size<4) return;
  memcpy(mp->nitems,data,4);
  for(i=0;i<(mp->nbases&63);i++) {
    for(k=0;k<63 && at<size && data[at];k++) mp->bases[i].basename[k]=data[at++];
    at++;
  }
  for(j=0;j<3;j++) for(i=0;i<(mp->nchains[j]&63) && at<size-2;i++) {
    k=data[at++]; k|=data[at++]<<8;
    mp->chains[j*63+i].size=k&0x07FF;
    mp->chains[j*63+i].name[0]="\x00-_."[k>>14];
    k>>=11;
    if(at+(k&7)>size) break;
    memcpy(mp->chains[j*63+i].name+(k&~7?1:0),data+at,k&7);
    at+=k&7;
  }
  for(j=0;j<3;j++) for(i=0;i<(mp->nchains[j]&63) && at+mp->chains[j*63+i].size<=size;i++) {
    load_dependent_picture(data+at,mp->chains[j*63+i].size,mp->chains+j*63+i,1);
    at+=mp->chains[j*63+i].size;
  }
}

static void save_multidependent(FILE*fp,MultiDependent*mp) {
  Uint8*buf=0;
  size_t size=0;
  FILE*f=open_memstream((char**)&buf,&size);
  Uint32 at;
  int i,j,k,c;
  if(!f) fatal("Cannot open memory stream\n");
  fwrite(mp->nitems,1,4,fp);
  for(i=0;i<(mp->nbases&63);i++) fwrite(mp->bases[i].basename,1,strlen(mp->bases[i].basename)+1,fp);
  for(j=0;j<3;j++) for(i=0;i<(mp->nchains[j]&63);i++) {
    at=ftell(f);
    save_dependent_picture(f,mp->chains+j*63+i,1);
    //TODO: This should check if it is too long, but unfortunately it does not do that.
    k=(ftell(f)-at)&0x07FF;
    c=mp->chains[j*63+i].name[0];
    if(c=='-') k|=0x4000;
    if(c=='_') k|=0x8000;
    if(c=='.') k|=0xC000;
    k|=strlen(mp->chains[j*63+i].name+(k>>14?1:0))<<11;
    fputc(k,fp); fputc(k>>8,fp);
    if((k>>11)&7) fwrite(mp->chains[j*63+i].name+(k>>14?1:0),1,(k>>11)&7,fp);
  }
  fputc(0,f);
  fclose(f);
  if(!buf || !size) fatal("Allocation failed\n");
  if(size) fwrite(buf,1,size-1,fp);
  free(buf);
}

static void edit_multidependent(MultiDependent*mp,const char*name) {
  const char*s;
  char row=0;
  char col=0;
  int i,j;
  SDL_Rect r;
  SDL_Event ev;
  set_cursor(XC_arrow);
  redraw:
  r.x=r.y=0; r.w=screen->w; r.h=screen->h;
  SDL_LockSurface(screen);
  SDL_FillRect(screen,&r,0xF0);
  draw_text(0,0,"Multidependent:",0xF0,0xF7);
  draw_text(136,0,name,0xF0,0xF5);
  draw_text(0,8,"<ESC> Exit  <\x18/\x19/\x1A/\x1B> Cursor  <SP> Edit  <TAB> Name  <BKSP> Toggle Null  <INS> Add  <DEL> Delete",0xF0,0xFB);
  draw_text(4,20,"BASE ",col==0?0xF2:0xF0,0xFE);
  if(mp->nbases&0x80) draw_text(4,28,"<Null>",0xF0,0xF7);
  draw_text(4+8*64,20,"I   ",col==1?0xF2:0xF0,0xFE);
  if(mp->nchains[0]&0x80) draw_text(4+8*64,28,"<Null>",0xF0,0xF7);
  draw_text(4+8*74,20,"II  ",col==2?0xF2:0xF0,0xFE);
  if(mp->nchains[1]&0x80) draw_text(4+8*74,28,"<Null>",0xF0,0xF7);
  draw_text(4+8*84,20,"III ",col==3?0xF2:0xF0,0xFE);
  if(mp->nchains[2]&0x80) draw_text(4+8*84,28,"<Null>",0xF0,0xF7);
  if(row>=(mp->nitems[col]&63)) row=(mp->nitems[col]&63?:1)-1;
  for(i=0;i<(mp->nbases&63);i++) draw_text(4,8*i+36,mp->bases[i].basename,(col==0 && row==i)?0xF1:0xF0,0xF7);
  for(j=0;j<3;j++) for(i=0;i<(mp->nchains[j]&63);i++) draw_text(4+8*(10*j+64),8*i+36,mp->chains[j*63+i].name,(col==j+1 && row==i)?0xF1:0xF0,0xF7);
  SDL_UnlockSurface(screen);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&ev)) {
    switch(ev.type) {
      case SDL_QUIT:
        exit(0);
        break;
      case SDL_KEYDOWN:
        switch(ev.key.keysym.sym) {
          case SDLK_ESCAPE: case SDLK_q: return;
          case SDLK_BACKSPACE: case SDLK_t: mp->nitems[col]^=0x80; break;
          case SDLK_LEFT: case SDLK_KP4: case SDLK_h: if(col) --col; break;
          case SDLK_RIGHT: case SDLK_KP6: case SDLK_l: if(col<3) ++col; break;
          case SDLK_UP: case SDLK_KP8: case SDLK_k: if(row) --row; break;
          case SDLK_DOWN: case SDLK_KP2: case SDLK_j: ++row; break;
          case SDLK_HOME: case SDLK_KP7: row=0; break;
          case SDLK_END: case SDLK_KP1: row=64; break;
          case SDLK_INSERT: case SDLK_KP0: case SDLK_a:
            if((mp->nitems[col]&63)==63) break;
            row=mp->nitems[col]&63;
            s=screen_prompt(col?"Add filter chain:":"Add base item:");
            if(!s || !*s) break;
            if(col) {
              strncpy(mp->chains[(col-1)*63+row].name,s,8);
              if(*s!='.' && *s!='_' && *s!='-') mp->chains[(col-1)*63+row].name[8]=0;
              mp->chains[(col-1)*63+row].nfilters=0;
              ++mp->nitems[col];
              goto edit;
            } else {
              strncpy(mp->bases[row].basename,s,63);
              ++mp->nbases;
            }
            break;
          case SDLK_SPACE: case SDLK_e: edit:
            if(!mp->nitems[col]) break;
            if(!col) goto rename;
            edit_dependent_picture(mp->chains+(col-1)*63+row,0);
            break;
          case SDLK_DELETE: case SDLK_KP_PERIOD: case SDLK_d:
            if(!mp->nitems[col]) break;
            --mp->nitems[col];
            if(col) {
              for(i=row;i<mp->nitems[col];i++) mp->chains[(col-1)*63+i]=mp->chains[(col-1)*63+i+1];
            } else {
              for(i=row;i<mp->nbases;i++) mp->bases[i]=mp->bases[i+1];
            }
            break;
          case SDLK_TAB: case SDLK_n: rename:
            if(!mp->nitems[col]) break;
            s=screen_prompt("Rename:");
            if(!s || !*s) break;
            if(col) {
              strncpy(mp->chains[(col-1)*63+row].name,s,8);
              if(*s!='.' && *s!='_' && *s!='-') mp->chains[(col-1)*63+row].name[8]=0;
              mp->chains[(col-1)*63+row].nfilters=0;
            } else {
              strncpy(mp->bases[row].basename,s,63);
            }
            break;
        }
        goto redraw;
      case SDL_VIDEOEXPOSE:
        goto redraw;
    }
  }
}

static void edit_picture(sqlite3_int64 id) {
  sqlite3_stmt*st;
  Picture*pict[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  DependentPicture*dpict;
  MultiDependent*mpict;
  char*name;
  const unsigned char*data;
  Uint8*buf=0;
  size_t size=0;
  FILE*fp;
  int i,n;
  if(i=sqlite3_prepare_v2(userdb,"SELECT SUBSTR(`NAME`,1,LENGTH(`NAME`)-4),`DATA`,`TYPE` FROM `PICEDIT` WHERE `ID`=?1;",-1,&st,0)) {
1516
1517
1518
1519
1520
1521
1522
1523

1524
1525
1526
1527












1528
1529
1530
1531
1532
1533
1534
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







-
+



-
+
+
+
+
+
+
+
+
+
+
+
+







    if(!buf || !size) fatal("Allocation failed\n");
    *buf|=pict[0]->meth<<4;
    for(i=1;i<n;i++) buf[n+1+((i-1)>>1)]|=pict[i]->meth<<(i&1?0:4);
    for(i=0;i<16;i++) free(pict[i]);
  } else if(i==2) {
    dpict=malloc(sizeof(DependentPicture));
    if(!dpict) fatal("Allocation failed\n");
    load_dependent_picture(data,n,dpict);
    load_dependent_picture(data,n,dpict,0);
    fp=open_memstream((char**)&buf,&size);
    if(!fp) fatal("Cannot open memory stream\n");
    edit_dependent_picture(dpict,name);
    save_dependent_picture(fp,dpict);
    save_dependent_picture(fp,dpict,0);
    free(dpict);
    fclose(fp);
    if(!buf || !size) fatal("Allocation failed\n");
  } else if(i==3) {
    mpict=malloc(sizeof(MultiDependent));
    if(!mpict) fatal("Allocation failed\n");
    load_multidependent(data,n,mpict);
    fp=open_memstream((char**)&buf,&size);
    if(!fp) fatal("Cannot open memory stream\n");
    edit_multidependent(mpict,name);
    save_multidependent(fp,mpict);
    free(dpict);
    fclose(fp);
    if(!buf || !size) fatal("Allocation failed\n");
  }
  free(name);
  if(i=sqlite3_prepare_v2(userdb,"UPDATE `PICEDIT` SET `DATA`=?2 WHERE ID=?1;",-1,&st,0)) {
    screen_message(sqlite3_errmsg(userdb));