Free Hero Mesh

Check-in [55e7980567]
Login
This is a mirror of the main repository for Free Hero Mesh. New tickets and changes will not be accepted at this mirror.
Overview
Comment:Implement loading dependent pictures.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 55e7980567a45c03c003216bebab8b68c4f4b6ba
User & Date: user on 2021-04-15 03:37:26
Other Links: manifest | tags
Context
2021-04-16
00:19
Update the TODO file. check-in: fb7252b349 user: user tags: trunk
2021-04-15
03:37
Implement loading dependent pictures. check-in: 55e7980567 user: user tags: trunk
2021-04-14
21:51
Add the missing -lm flag for linking check-in: 7355156dce user: user tags: trunk
Changes

Modified picedit.c from [9f72395624] to [9a5c536613].

947
948
949
950
951
952
953
954

955
956
957
958
959
960
961
947
948
949
950
951
952
953

954
955
956
957
958
959
960
961







-
+







        break;
      case 12 ... 15:
        for(k=0;k<64;k++) {
          dp->filters[i].shift.shift[k]=(j=fgetc(fp))&127;
          dp->filters[i].shift.size[k]=fgetc(fp);
          if(j&128) break;
        }
        dp->filters[i].shift.nshift=k;
        dp->filters[i].shift.nshift=k+1;
        break;
    }
  }
  fclose(fp);
}

static void save_dependent_picture(FILE*fp,DependentPicture*dp) {

Modified picture.c from [32631e0ed2] to [4b33185177].

389
390
391
392
393
394
395
396


































































397





































































































398


















































399
400
401
402
403
404
405
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








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

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







        if(meth&2) i=size-i-1;
      }
      pix+=pitch-picture_size;
    }
  }
  SDL_UnlockSurface(picts);
}

static void pic_shift_right(SDL_Rect*r,int sh) {
  Uint8*p;
  int y;
  char buf[256];
  sh%=r->w;
  if(!sh) return;
  SDL_LockSurface(picts);
  p=picts->pixels+r->y*picts->pitch+r->x;
  for(y=0;y<r->h;y++) {
    memcpy(buf,p,r->w);
    memcpy(p,buf+r->w-sh,sh);
    memcpy(p+sh,buf,r->w-sh);
    p+=picts->pitch;
  }
  SDL_UnlockSurface(picts);
}

static void pic_shift_down(SDL_Rect*r,int sh) {
  Uint8*p;
  int y;
  char b1[256];
  char b2[256];
  sh%=r->h;
  SDL_LockSurface(picts);
  // This implementation is probably inefficient.
  while(sh--) {
    p=picts->pixels+r->y*picts->pitch+r->x;
    memcpy(b1,p+picts->pitch*(r->h-1),r->w);
    for(y=0;y<r->h;y++) {
      memcpy(b2,p,r->w);
      memcpy(p,b1,r->w);
      p+=picts->pitch;
      memcpy(b1,b2,r->w);
    }
  }
  SDL_UnlockSurface(picts);
}

static void pic_orientation(SDL_Rect*r,Uint8 m) {
  Uint8*d=malloc(r->w*r->h);
  Uint8*p;
  Uint8*q;
  int i,j;
  if(!d) fatal("Allocation failed\n");
  SDL_LockSurface(picts);
  p=picts->pixels+r->y*picts->pitch+r->x;
  q=d;
  for(i=0;i<r->h;i++) {
    for(j=0;j<r->w;j++) {
      if(m&1) j=r->w-j-1;
      if(m&2) i=r->h-i-1;
      *q++=p[m&4?j*picts->pitch+i:i*picts->pitch+j];
      if(m&1) j=r->w-j-1;
      if(m&2) i=r->h-i-1;
    }
  }
  q=d;
  for(i=0;i<r->h;i++) {
    memcpy(p,q,r->w);
    p+=picts->pitch;
    q+=r->w;
  }
  SDL_UnlockSurface(picts);
  free(d);
}

static void load_dependent_picture(FILE*fp,Uint16 img,int alt) {
  SDL_Rect src={0,0,picture_size,picture_size};
  SDL_Rect dst={(img&15)*picture_size,(img>>4)*picture_size,picture_size,picture_size};
  sqlite3_stmt*st;
  Sint32 sz;
  int c,i,x,y;
  char nam[128];
  Uint8 buf[512];
  Uint8*p;
  sz=fgetc(fp)<<16;
  sz|=fgetc(fp)<<24;
  sz|=fgetc(fp);
  sz|=fgetc(fp)<<8;
  if(sqlite3_prepare_v2(userdb,"SELECT `ID` FROM `PICTURES` WHERE `NAME` = ?1 AND NOT `DEPENDENT`;",-1,&st,0))
   fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
  i=0;
  while(sz) {
    c=fgetc(fp);
    if(c<32) break;
    if(i<127) nam[i++]=c;
    sz--;
  }
  if(sz) ungetc(c,fp);
  sqlite3_bind_text(st,1,nam,i,0);
  i=sqlite3_step(st);
  if(i==SQLITE_DONE) {
    fprintf(stderr,"Cannot find base picture for a dependent picture; ignoring\n");
    sqlite3_finalize(st);
    return;
  } else if(i!=SQLITE_ROW) {
    fatal("SQL error (%d): %s\n",i,sqlite3_errmsg(userdb));
  }
  i=sqlite3_column_int(st,0);
  sqlite3_reset(st);
  src.x=(i&15)*picture_size;
  src.y=(i>>4)*picture_size;
  SDL_SetColorKey(picts,0,0);
  SDL_BlitSurface(picts,&src,picts,&dst);
  while(sz-->0) switch(c=fgetc(fp)) {
    case 0 ... 7: // Flip/rotate
      pic_orientation(&dst,c);
      break;
    case 8: // *->* *->* *->*
      c=fgetc(fp);
      sz-=c+1;
      fread(buf,1,c&255,fp);
      SDL_LockSurface(picts);
      p=picts->pixels+((img&15)+picts->pitch*(img>>4))*picture_size;
      for(y=0;y<picture_size;y++) {
        for(x=0;x<picture_size;x++) {
          for(i=0;i<c;i+=2) {
            if(p[x]==buf[i]) {
              p[x]=buf[i+1];
              break;
            }
          }
        }
        p+=picts->pitch;
      }
      SDL_UnlockSurface(picts);
      break;
    case 9: // *->*->*->*->*
      c=fgetc(fp);
      sz-=c+1;
      fread(buf,1,c&255,fp);
      SDL_LockSurface(picts);
      p=picts->pixels+((img&15)+picts->pitch*(img>>4))*picture_size;
      for(y=0;y<picture_size;y++) {
        for(x=0;x<picture_size;x++) {
          for(i=0;i<c-1;i++) {
            if(p[x]==buf[i]) {
              p[x]=buf[i+1];
              break;
            }
          }
        }
        p+=picts->pitch;
      }
      SDL_UnlockSurface(picts);
      break;
    case 10: // *<->* *<->* *<->*
      c=fgetc(fp);
      sz-=c+1;
      fread(buf,1,c&255,fp);
      SDL_LockSurface(picts);
      p=picts->pixels+((img&15)+picts->pitch*(img>>4))*picture_size;
      for(y=0;y<picture_size;y++) {
        for(x=0;x<picture_size;x++) {
          for(i=0;i<c;i+=2) {
            if(p[x]==buf[i]) {
              p[x]=buf[i+1];
              break;
            } else if(p[x]==buf[i+1]) {
              p[x]=buf[i];
              break;
            }
          }
        }
        p+=picts->pitch;
      }
      SDL_UnlockSurface(picts);
      break;
  //TODO
    case 11: // Overlay
      SDL_SetColorKey(picts,SDL_SRCCOLORKEY,0);
      i=0;
      while(sz>0) {
        c=fgetc(fp);
        if(c<32) break;
        if(i<127) nam[i++]=c;
        sz--;
      }
      if(sz) ungetc(c,fp);
      sqlite3_bind_text(st,1,nam,i,0);
      i=sqlite3_step(st);
      if(i==SQLITE_DONE) {
        fprintf(stderr,"Cannot find overlay for a dependent picture; ignoring\n");
        break;
      } else if(i!=SQLITE_ROW) {
        fatal("SQL error (%d): %s\n",i,sqlite3_errmsg(userdb));
      }
      i=sqlite3_column_int(st,0);
      sqlite3_reset(st);
      src.x=(i&15)*picture_size;
      src.y=(i>>4)*picture_size;
      SDL_BlitSurface(picts,&src,picts,&dst);
      break;
    case 12 ... 15: // Shift (up/down/right/left)
      while(sz>0) {
        x=fgetc(fp);
        y=fgetc(fp);
        sz-=2;
        if(y && !(picture_size%y)) {
          if(c==12) pic_shift_down(&dst,picture_size-((x&127)*picture_size)/y);
          if(c==13) pic_shift_down(&dst,((x&127)*picture_size)/y);
          if(c==14) pic_shift_right(&dst,((x&127)*picture_size)/y);
          if(c==15) pic_shift_right(&dst,picture_size-((x&127)*picture_size)/y);
          break;
        }
        if(x&128) break;
      }
      while(sz>0 && x<128) {
        x=fgetc(fp);
        fgetc(fp);
        sz-=2;
      }
      break;
    default:
      fprintf(stderr,"Unrecognized command in dependent picture (%d)\n",c);
      goto done;
  }
  if(sz<0) fprintf(stderr,"Lump size of dependent picture is too short\n");
  done: sqlite3_finalize(st);
}

void load_pictures(void) {
  sqlite3_stmt*st=0;
  FILE*fp;
  Uint8 wantsize[32];
  Uint8 nwantsize=0;