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 [57df01a3fa]:

To Artifact [ff76ebbf80]:


17
18
19
20
21
22
23




24
25
26
27
28
29
30
#include "smallxrm.h"
#include "pcfont.h"
#include "quarks.h"
#include "heromesh.h"
#include "cursorshapes.h"
#include "keyicons.xbm"





SDL_Surface*screen;
Uint16 picture_size;
int left_margin;
Uint32 codepage;

static SDL_Surface*picts;
static Uint8*curpic;







>
>
>
>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "smallxrm.h"
#include "pcfont.h"
#include "quarks.h"
#include "heromesh.h"
#include "cursorshapes.h"
#include "keyicons.xbm"

#define PPR 128 // pictures per row
#define PPRM 127 // pictures per row bit mask (one less than pictures per row)
#define PPRS 7 // pictures per row bit shift amount (log2 of pictures per row)

SDL_Surface*screen;
Uint16 picture_size;
int left_margin;
Uint32 codepage;

static SDL_Surface*picts;
static Uint8*curpic;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  if(fp) fclose(fp);
  SDL_SetColors(screen,pal,0,256);
  SDL_SetColors(picts,pal,0,256);
}

void draw_picture(int x,int y,Uint16 img) {
  // To be called only when screen is unlocked!
  SDL_Rect src={(img&15)*picture_size,(img>>4)*picture_size,picture_size,picture_size};
  SDL_Rect dst={x,y,picture_size,picture_size};
  SDL_BlitSurface(picts,&src,screen,&dst);
}

void draw_cell(int x,int y) {
  // To be called only when screen is unlocked!
  Uint32 o;







|







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  if(fp) fclose(fp);
  SDL_SetColors(screen,pal,0,256);
  SDL_SetColors(picts,pal,0,256);
}

void draw_picture(int x,int y,Uint16 img) {
  // To be called only when screen is unlocked!
  SDL_Rect src={(img&PPRM)*picture_size,(img>>PPRS)*picture_size,picture_size,picture_size};
  SDL_Rect dst={x,y,picture_size,picture_size};
  SDL_BlitSurface(picts,&src,screen,&dst);
}

void draw_cell(int x,int y) {
  // To be called only when screen is unlocked!
  Uint32 o;
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
  which=i;
  i=1;
  while(which--) load_one_picture_sub(fp,size=buf[i],meth=(i==1?*buf>>4:buf[(*buf&15)+1+((i-2)>>1)]>>(i&1?4:0))&15),i++;
  if(meth==5 || meth==6) meth^=3;
  if(meth==15) meth=0;
  SDL_LockSurface(picts);
  pitch=picts->pitch;
  pix=picts->pixels+((img&15)+pitch*(img>>4))*picture_size;
  for(i=0;i<size;i++) {
    for(h=0;h<zoom;h++) {
      for(j=0;j<size;j++) {
        if(meth&1) j=size-j-1;
        if(meth&2) i=size-i-1;
        for(k=0;k<zoom;k++) *pix++=curpic[meth&4?j*size+i:i*size+j];
        if(meth&1) j=size-j-1;







|







453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
  which=i;
  i=1;
  while(which--) load_one_picture_sub(fp,size=buf[i],meth=(i==1?*buf>>4:buf[(*buf&15)+1+((i-2)>>1)]>>(i&1?4:0))&15),i++;
  if(meth==5 || meth==6) meth^=3;
  if(meth==15) meth=0;
  SDL_LockSurface(picts);
  pitch=picts->pitch;
  pix=picts->pixels+((img&PPRM)+pitch*(img>>PPRS))*picture_size;
  for(i=0;i<size;i++) {
    for(h=0;h<zoom;h++) {
      for(j=0;j<size;j++) {
        if(meth&1) j=size-j-1;
        if(meth&2) i=size-i-1;
        for(k=0;k<zoom;k++) *pix++=curpic[meth&4?j*size+i:i*size+j];
        if(meth&1) j=size-j-1;
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  }
  SDL_UnlockSurface(picts);
  free(d);
}

static void load_dependent_picture(FILE*fp,Sint32 sz,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;
  int c,i,x,y;
  char nam[128];
  Uint8 buf[512];
  Uint8*p;
  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));







|







537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
  }
  SDL_UnlockSurface(picts);
  free(d);
}

static void load_dependent_picture(FILE*fp,Sint32 sz,Uint16 img,int alt) {
  SDL_Rect src={0,0,picture_size,picture_size};
  SDL_Rect dst={(img&PPRM)*picture_size,(img>>PPRS)*picture_size,picture_size,picture_size};
  sqlite3_stmt*st;
  int c,i,x,y;
  char nam[128];
  Uint8 buf[512];
  Uint8*p;
  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));
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
      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);
  } else {
    SDL_FillRect(picts,&dst,0);
  }
  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]) {







|
|














|


















|


















|







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
      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&PPRM)*picture_size;
    src.y=(i>>PPRS)*picture_size;
    SDL_SetColorKey(picts,0,0);
    SDL_BlitSurface(picts,&src,picts,&dst);
  } else {
    SDL_FillRect(picts,&dst,0);
  }
  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&PPRM)+picts->pitch*(img>>PPRS))*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&PPRM)+picts->pitch*(img>>PPRS))*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&PPRM)+picts->pitch*(img>>PPRS))*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]) {
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
        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;







|
|







656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
        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&PPRM)*picture_size;
      src.y=(i>>PPRS)*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;
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
      }
      break;
    case 16: // Hue/shade
      fread(buf,1,2,fp);
      c=(Sint8)buf[1];
      sz-=2;
      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++) {
          if(p[x] && p[x]<=225) {
            i=(p[x]-1)%15+c;
            p[x]=15*(((p[x]-1)/15+buf[0])%15)+1;
            if(i<0) p[x]=1; else if(i>14) p[x]=15; else p[x]+=i;
          }







|







685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
      }
      break;
    case 16: // Hue/shade
      fread(buf,1,2,fp);
      c=(Sint8)buf[1];
      sz-=2;
      SDL_LockSurface(picts);
      p=picts->pixels+((img&PPRM)+picts->pitch*(img>>PPRS))*picture_size;
      for(y=0;y<picture_size;y++) {
        for(x=0;x<picture_size;x++) {
          if(p[x] && p[x]<=225) {
            i=(p[x]-1)%15+c;
            p[x]=15*(((p[x]-1)/15+buf[0])%15)+1;
            if(i<0) p[x]=1; else if(i>14) p[x]=15; else p[x]+=i;
          }
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
  picture_size=decide_picture_size(nwantsize,wantsize,havesize,n);
  if(main_options['x']) goto done;
  if(sqlite3_prepare_v2(userdb,"SELECT `ID`, `OFFSET` FROM `PICTURES` WHERE NOT `DEPENDENT`;",-1,&st,0))
   fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
  optionquery[1]=Q_screenFlags;
  v=xrm_get_resource(resourcedb,optionquery,optionquery,2);
  i=v&&strchr(v,'h');
  picts=SDL_CreateRGBSurface((i?SDL_HWSURFACE:SDL_SWSURFACE)|SDL_SRCCOLORKEY,picture_size<<4,picture_size*((n+16)>>4),8,0,0,0,0);
  if(!picts) fatal("Error allocating surface for pictures: %s\n",SDL_GetError());
  init_palette();
  for(i=0;i<n;i++) {
    if((j=sqlite3_step(st))!=SQLITE_ROW) {
      if(j==SQLITE_DONE) break;
      fatal("SQL error (%d): %s\n",j,sqlite3_errmsg(userdb));
    }







|







879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
  picture_size=decide_picture_size(nwantsize,wantsize,havesize,n);
  if(main_options['x']) goto done;
  if(sqlite3_prepare_v2(userdb,"SELECT `ID`, `OFFSET` FROM `PICTURES` WHERE NOT `DEPENDENT`;",-1,&st,0))
   fatal("Unable to prepare SQL statement while loading pictures: %s\n",sqlite3_errmsg(userdb));
  optionquery[1]=Q_screenFlags;
  v=xrm_get_resource(resourcedb,optionquery,optionquery,2);
  i=v&&strchr(v,'h');
  picts=SDL_CreateRGBSurface((i?SDL_HWSURFACE:SDL_SWSURFACE)|SDL_SRCCOLORKEY,picture_size<<PPRS,picture_size*((n+PPR)>>PPRS),8,0,0,0,0);
  if(!picts) fatal("Error allocating surface for pictures: %s\n",SDL_GetError());
  init_palette();
  for(i=0;i<n;i++) {
    if((j=sqlite3_step(st))!=SQLITE_ROW) {
      if(j==SQLITE_DONE) break;
      fatal("SQL error (%d): %s\n",j,sqlite3_errmsg(userdb));
    }