Free Hero Mesh

Check-in [00363b01aa]
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 popup text (partially)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 00363b01aaa1b5020b628daee2e70c46b56b8269
User & Date: user on 2020-12-17 00:28:01
Other Links: manifest | tags
Context
2020-12-17
02:16
Change sql_interactive to use sqlite3_get_table so that it is not executing the SQL codes multiple times check-in: ef1133452e user: user tags: trunk
00:28
Implement popup text (partially) check-in: 00363b01aa user: user tags: trunk
2020-12-16
20:51
Implementing several new instructions and several corrections; implementing Move too, but currently incomplete (and may have some mistakes; more experiments will be needed) check-in: b1126053b5 user: user tags: trunk
Changes

Modified bindings.doc from [8ca36f207e] to [91d6e26158].

55
56
57
58
59
60
61



62
63
64
65
66
67
68
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71







+
+
+








'- ' <count>
  Rewind that many moves.

'= ' <count>
  Restart and replay that many moves.

'^T'
  Display the level title popup.

'^o' <location>
  List objects at the specified coordinates.


=== Editor commands ===

'^c'

Modified exec.c from [3d46b07e14] to [4b3ee71562].

27
28
29
30
31
32
33

34
35
36
37
38
39
40
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41







+







Uint32 firstobj=VOIDLINK;
Uint32 lastobj=VOIDLINK;
Uint32 playfield[64*64]; // bottom-most object per cell
Uint8 pfwidth,pfheight;
Sint8 gameover,key_ignored;
Uint8 generation_number_inc;
Uint32 move_number;
unsigned char*quiz_text;

typedef struct {
  Uint16 msg;
  Uint32 from;
  Value arg1,arg2,arg3;
} MessageVars;

909
910
911
912
913
914
915


















































































































916
917
918
919
920
921
922
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







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







  return send_message(from,v_object(to),msg.u,arg1,arg2,arg3);
}

static inline Value v_send_self(Uint32 from,Value msg,Value arg1,Value arg2,Value arg3) {
  if(msg.t!=TY_MESSAGE) Throw("Type mismatch");
  return send_message(from,from,msg.u,arg1,arg2,arg3);
}

static void v_set_popup(Uint32 from,int argc) {
  const unsigned char*t;
  const unsigned char*u;
  sqlite3_str*s;
  Value v;
  int argi=1;
  if(argc>32 || argc<0) Throw("Too many arguments");
  vstackptr-=argc;
  v=Pop();
  if(quiz_text) return;
  if(argc) {
    argc++;
    if(v.t!=TY_STRING && v.t!=TY_LEVELSTRING) Throw("Type mismatch");
    s=sqlite3_str_new(userdb);
    t=value_string_ptr(v);
    while(*t) {
      if(u=strchr(t,'%')) {
        sqlite3_str_append(s,t,u-t);
        t=u+2;
        switch(u[1]) {
          case 0:
            t=u+1;
            break;
          case 'c':
            if(argi==argc) break;
            v=vstack[vstackptr+argi++];
            if(v.t==TY_NUMBER) {
              sqlite3_str_appendchar(s,1,31);
              sqlite3_str_appendchar(s,1,v.u&255?:255);
            }
            break;
          case 'd':
            if(argi==argc) break;
            v=vstack[vstackptr+argi++];
            if(v.t==TY_NUMBER) sqlite3_str_appendf(s,"%d",(signed int)v.s);
            break;
          case 'i':
            if(argi>=argc-1) break;
            if(vstack[vstackptr+argi].t==TY_CLASS && vstack[vstackptr+argi+1].t==TY_NUMBER) {
              Class*c=classes[vstack[vstackptr+argi].u];
              int n=vstack[vstackptr+argi+1].u&255;
              if(n<c->nimages) sqlite3_str_appendf(s,"\x0E\x07:%d\\",c->images[n]&0x7FFF);
            }
            argi+=2;
            break;
          case 's':
            if(argi==argc) break;
            v=vstack[vstackptr+argi++];
            switch(v.t) {
              case TY_STRING: case TY_LEVELSTRING:
                sqlite3_str_appendf(s,"%s",value_string_ptr(v));
                break;
              case TY_NUMBER:
                sqlite3_str_appendf(s,"%llu",(sqlite3_int64)v.u);
                break;
              case TY_CLASS:
                sqlite3_str_appendf(s,"%s",classes[v.u]->name);
                break;
              default:
                Throw("Type mismatch");
            }
            break;
          case 'u':
            if(argi==argc) break;
            v=vstack[vstackptr+argi++];
            if(v.t==TY_NUMBER) sqlite3_str_appendf(s,"%u",(unsigned int)v.u);
            break;
          case 'x':
            if(argi==argc) break;
            v=vstack[vstackptr+argi++];
            if(v.t==TY_NUMBER) sqlite3_str_appendf(s,"%x",(unsigned int)v.u);
            break;
          case 'X':
            if(argi==argc) break;
            v=vstack[vstackptr+argi++];
            if(v.t==TY_NUMBER) sqlite3_str_appendf(s,"%X",(unsigned int)v.u);
            break;
          case '%':
            sqlite3_str_appendchar(s,1,'%');
            break;
        }
      } else {
        sqlite3_str_appendall(s,t);
        break;
      }
    }
    sqlite3_str_appendchar(s,1,0);
    quiz_text=sqlite3_str_finish(s);
  } else {
    switch(v.t) {
      case TY_STRING: case TY_LEVELSTRING:
        quiz_text=sqlite3_mprintf("%s",value_string_ptr(v));
        break;
      case TY_NUMBER:
        quiz_text=sqlite3_mprintf("%llu",(sqlite3_int64)v.u);
        break;
      case TY_CLASS:
        quiz_text=sqlite3_mprintf("%s",classes[v.u]->name);
        break;
      default:
        Throw("Type mismatch");
    }
  }
  if(!quiz_text) fatal("Allocation failed\n");
  t=quiz_text;
  while(*t) {
    if(*t==16) {
      quiz_obj.t=objects[from]->generation?:1;
      quiz_obj.u=from;
    }
    if(*t==31 && t[1]) t+=2; else t+=1;
  }
}

// Here is where the execution of a Free Hero Mesh bytecode subroutine is executed.
#define NoIgnore() do{ changed=1; }while(0)
#define GetVariableOf(a,b) (i=v_object(Pop()),i==VOIDLINK?NVALUE(0):b(objects[i]->a))
#define GetVariableOrAttributeOf(a,b) (t2=Pop(),t2.t==TY_CLASS?NVALUE(classes[t2.u]->a):(i=v_object(t2),i==VOIDLINK?NVALUE(0):b(objects[i]->a)))
#define Numeric(a) do{ if((a).t!=TY_NUMBER) Throw("Type mismatch"); }while(0)
#define DivideBy(a) do{ Numeric(a); if(!(a).u) Throw("Division by zero"); }while(0)
1154
1155
1156
1157
1158
1159
1160


1161
1162
1163
1164
1165
1166
1167
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284







+
+







    case OP_OBJCLASSAT: StackReq(3,1); t3=Pop(); t2=Pop(); t1=Pop(); Push(v_obj_class_at(t1,t2,t3)); break;
    case OP_OBJDIR: StackReq(1,1); t2=Pop(); Numeric(t2); i=obj_dir(obj,t2.u); Push(OVALUE(i)); break;
    case OP_OBJDIR_C: StackReq(2,1); t2=Pop(); Numeric(t2); i=obj_dir(v_object(Pop()),t2.u); Push(OVALUE(i)); break;
    case OP_OBJLAYERAT: StackReq(3,1); t3=Pop(); Numeric(t3); t2=Pop(); Numeric(t2); t1=Pop(); Numeric(t1); i=obj_layer_at(t1.u,t2.u,t3.u); Push(OVALUE(i)); break;
    case OP_OBJTOPAT: StackReq(2,1); t2=Pop(); Numeric(t2); t1=Pop(); Numeric(t1); i=obj_top_at(t1.u,t2.u); Push(OVALUE(i)); break;
    case OP_PLAYER: StackReq(0,1); if(classes[o->class]->cflags&CF_PLAYER) Push(NVALUE(1)); else Push(NVALUE(0)); break;
    case OP_PLAYER_C: StackReq(1,1); GetClassFlagOf(CF_PLAYER); break;
    case OP_POPUP: StackReq(1,0); v_set_popup(obj,0); break;
    case OP_POPUPARGS: i=code[ptr++]; StackReq(i+1,0); v_set_popup(obj,i); break;
    case OP_RET: return;
    case OP_ROT: StackReq(3,3); t3=Pop(); t2=Pop(); t1=Pop(); Push(t2); Push(t3); Push(t1); break;
    case OP_ROTBACK: StackReq(3,3); t3=Pop(); t2=Pop(); t1=Pop(); Push(t3); Push(t1); Push(t2); break;
    case OP_RSH: StackReq(2,1); t2=Pop(); Numeric(t2); t1=Pop(); Numeric(t1); Push(NVALUE(t2.u&~31?0:t1.u>>t2.u)); break;
    case OP_RSH_C: StackReq(2,1); t2=Pop(); Numeric(t2); t1=Pop(); Numeric(t1); Push(NVALUE(t2.u&~31?(t1.s<0?-1:0):t1.s>>t2.u)); break;
    case OP_SELF: StackReq(0,1); Push(OVALUE(obj)); break;
    case OP_SEND: StackReq(3,1); t4=Pop(); t3=Pop(); t2=Pop(); Push(v_send_self(obj,t2,t3,t4,NVALUE(0))); break;
1194
1195
1196
1197
1198
1199
1200

1201
1202
1203
1204
1205
1206
1207
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325







+







    case OP_STEALTHY_EC: NoIgnore(); StackReq(2,0); SetFlagOf(OF_STEALTHY); break;
    case OP_STRENGTH: StackReq(0,1); Push(NVALUE(o->strength)); break;
    case OP_STRENGTH_C: StackReq(1,1); Push(GetVariableOrAttributeOf(strength,NVALUE)); break;
    case OP_STRENGTH_E: NoIgnore(); StackReq(1,0); t1=Pop(); Numeric(t1); o->strength=t1.u; break;
    case OP_STRENGTH_E16: NoIgnore(); StackReq(1,0); t1=Pop(); Numeric(t1); o->strength=t1.u&0xFFFF; break;
    case OP_STRENGTH_EC: NoIgnore(); StackReq(2,0); t1=Pop(); Numeric(t1); i=v_object(Pop()); if(i!=VOIDLINK) o->strength=t1.u; break;
    case OP_STRENGTH_EC16: NoIgnore(); StackReq(2,0); t1=Pop(); Numeric(t1); i=v_object(Pop()); if(i!=VOIDLINK) o->strength=t1.u; break;
    case OP_STRING: StackReq(0,1); Push(UVALUE(code[ptr++],TY_STRING)); break;
    case OP_SOUND: StackReq(2,0); t2=Pop(); t1=Pop(); break; // Sound not implemented at this time
    case OP_SUB: StackReq(2,1); t2=Pop(); Numeric(t2); t1=Pop(); Numeric(t1); Push(NVALUE(t1.u-t2.u)); break;
    case OP_SWAP: StackReq(2,2); t1=Pop(); t2=Pop(); Push(t1); Push(t2); break;
    case OP_SYNCHRONIZE: StackReq(2,0); t2=Pop(); Numeric(t2); t1=Pop(); Numeric(t1); animate_sync(obj,t1.u,t2.u); break;
    case OP_TEMPERATURE: StackReq(0,1); Push(NVALUE(o->temperature)); break;
    case OP_TEMPERATURE_C: StackReq(1,1); Push(GetVariableOrAttributeOf(temperature,NVALUE)); break;
    case OP_TEMPERATURE_E: NoIgnore(); StackReq(1,0); t1=Pop(); Numeric(t1); o->temperature=t1.u; break;
1337
1338
1339
1340
1341
1342
1343




1344
1345
1346
1347
1348
1349
1350
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472







+
+
+
+







  return t;
}

void annihilate(void) {
  Uint32 i;
  for(i=0;i<64*64;i++) playfield[i]=VOIDLINK;
  firstobj=lastobj=VOIDLINK;
  if(quiz_text) {
    sqlite3_free(quiz_text);
    quiz_text=0;
  }
  if(!objects) return;
  for(i=0;i<nobjects;i++) if(objects[i]) {
    animfree(objects[i]->anim);
    free(objects[i]);
  }
  nobjects=0;
  free(objects);
1391
1392
1393
1394
1395
1396
1397





1398
1399
1400
1401
1402
1403
1404
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531







+
+
+
+
+







  if(key_ignored) {
    quiz_obj=NVALUE(0);
    return changed?"Invalid use of IgnoreKey":0;
  }
  move_number++;
  // Beginning phase
  if(!all_flushed) broadcast(m,0,MSG_BEGIN_TURN,m==VOIDLINK?NVALUE(objects[m]->x):NVALUE(0),m==VOIDLINK?NVALUE(objects[m]->y):NVALUE(0),v,0);
  // Trigger phase
  
  // Ending phase
  
  // Animation phase
  
  // Cleanup phase
  for(n=0;n<nobjects;n++) if(objects[n] && (objects[n]->oflags&OF_DESTROYED)) objtrash(n);
  if(generation_number<=TY_MAXTYPE) return "Too many generations of objects";
  return 0;
}

Modified game.c from [2c811f4938] to [4ed4013de1].

71
72
73
74
75
76
77

78
79
80
81
82
83
84
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85







+







  draw_text(24,32,"x",0xF0,0xF5);
  x=x>=left_margin?(x-left_margin)/picture_size+1:0;
  y=y/picture_size+1;
  if(x>0 && y>0 && x<=pfwidth && y<=pfheight) snprintf(buf,8,"(%2d,%2d)",x,y);
  else strcpy(buf,"       ");
  draw_text(0,40,buf,0xF0,0xF1);
  SDL_UnlockSurface(screen);
  if(quiz_text) draw_popup(quiz_text);
  SDL_Flip(screen);
  set_cursor(XC_arrow);
}

static void continue_animation(void) {
  Uint32 n=firstobj;
  Object*o;
168
169
170
171
172
173
174
175
176


177
178
179
180
181
182
183
184
185
186
169
170
171
172
173
174
175


176
177



178
179
180
181
182
183
184







-
-
+
+
-
-
-







      draw_text(200,y,"$",0xF0,0xFB);
      draw_text(208,y,classes[v.u]->name,0xF0,0xFB);
      break;
    case TY_MESSAGE:
      snprintf(buf,255,"%s%s",v.u<256?"":"#",v.u<256?standard_message_names[v.u]:messages[v.u-256]);
      draw_text(200,y,buf,0xF0,0xFD);
      break;
    case TY_LEVELSTRING:
      
    case TY_LEVELSTRING: case TY_STRING:
      draw_text(200,y,"<String>",0xF0,0xF9);
      break;
    case TY_STRING:
      
      break;
    case TY_SOUND: case TY_USOUND:
      draw_text(200,y,"<Sound>",0xF0,0xF6);
      break;
    default:
      snprintf(buf,80,"<%lu:%lu>",(long)v.u,(long)v.t);
      draw_text(200,y,buf,0xF0,0xFA);
253
254
255
256
257
258
259

260
261
262
263
264
265
266
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265







+







  r.w=screen->w;
  r.h=screen->h;
  SDL_FillRect(screen,&r,0xF0);
  SDL_LockSurface(screen);
  exam_value("Self:",0,OVALUE(n));
  exam_value("Class:",1,CVALUE(o->class));
  exam_value("Image:",2,NVALUE(o->image));
  if(classes[o->class]->cflags&CF_QUIZ) goto quiz;
  exam_value("Dir:",3,NVALUE(o->dir));
  exam_value("Misc1:",4,o->misc1);
  exam_value("Misc2:",5,o->misc2);
  exam_value("Misc3:",6,o->misc3);
  exam_value("Misc4:",7,o->misc4);
  exam_value("Misc5:",8,o->misc5);
  exam_value("Misc6:",9,o->misc6);
281
282
283
284
285
286
287

288
289
290
291
292
293
294
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294







+







  exam_value("Inertia:",24,NVALUE(o->inertia));
  exam_hardsharp("Hardness:",25,o->hard);
  exam_hardsharp("Sharpness:",26,o->sharp);
  while(sqlite3_step(st)==SQLITE_ROW) {
    i=sqlite3_column_int(st,1);
    exam_value(sqlite3_column_text(st,0),i+28,o->uservars[i]);
  }
  quiz:
  sqlite3_reset(st);
  SDL_UnlockSurface(screen);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&ev)) switch(ev.type) {
    case SDL_KEYDOWN:
      switch(ev.key.keysym.sym) {
        case SDLK_ESCAPE: case SDLK_RETURN: case SDLK_KP_ENTER: sqlite3_finalize(st); return;
341
342
343
344
345
346
347
348
349


350
351
352
353
354
355
356
341
342
343
344
345
346
347


348
349
350
351
352
353
354
355
356







-
-
+
+







  snprintf(buf,255," %d objects at (%d,%d): ",count,(xy&63)+1,(xy/64)+1);
  SDL_LockSurface(screen);
  draw_text(0,0,buf,0xF7,0xF0);
  n=t;
  for(i=0;i<scroll && n!=VOIDLINK;i++) n=objects[n]->down;
  for(i=0;i<screen->h/8 && n!=VOIDLINK;i++) {
    o=objects[n];
    snprintf(buf,255," %8d: %-14.14s %3d %s",n,classes[o->class]->name,o->image,dirs[o->dir&7]);
    draw_text(24,r.y,buf,0xF1,o->generation?0xFF:0xF8);
    snprintf(buf,255," %8d: %-14.14s %3d %s",n,classes[o->class]->name,o->image,classes[o->class]->cflags&CF_QUIZ?"":dirs[o->dir&7]);
    draw_text(24,r.y,buf,0xF1,o->generation?(classes[o->class]->cflags&CF_PLAYER?0xFE:0xFF):0xF8);
    n=o->down;
    r.y+=8;
  }
  SDL_UnlockSurface(screen);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&ev)) {
    if(ev.type!=SDL_VIDEOEXPOSE) {
395
396
397
398
399
400
401



402
403
404
405
406
407
408
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411







+
+
+







      }
      inputs[inputs_count++]=number;
      return 0;
    case '^E': // Edit
      return -2;
    case '^Q': // Quit
      return -1;
    case '^T': // Show title
      modal_draw_popup(level_title);
      return prev;
    case '^o': // List objects
      list_objects_at(number-65);
      return prev;
    case 'go': // Select level
      begin_level(number);
      return 1;
    default:
464
465
466
467
468
469
470
471

472
473
474
475
476
477
478
467
468
469
470
471
472
473

474
475
476
477
478
479
480
481







-
+







      case SDL_QUIT:
        exit(0);
        break;
      case SDL_MOUSEMOTION:
        show_mouse_xy(&ev);
        break;
      case SDL_USEREVENT:
        if(!gameover) continue_animation();
        if(!gameover && !quiz_text) continue_animation();
        timerflag=0;
        break;
      case SDL_MOUSEBUTTONDOWN:
        if(ev.button.x<left_margin) {
          
          break;
        } else {

Modified heromesh.h from [6f44ec7cde] to [cae8174e03].

91
92
93
94
95
96
97



98
99
100
101
102
103
104
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107







+
+
+







void draw_cell(int x,int y);
const char*screen_prompt(const char*txt);
int screen_message(const char*txt);
void load_pictures(void);

int scrollbar(int*cur,int page,int max,SDL_Event*ev,SDL_Rect*re);

void draw_popup(const unsigned char*txt);
int modal_draw_popup(const unsigned char*txt);

// == class ==

#define CF_PLAYER 0x01
#define CF_INPUT 0x02
#define CF_COMPATIBLE 0x04
#define CF_QUIZ 0x08
#define CF_GROUP 0x10 // this is a group of classes; you can't create an object of this class
221
222
223
224
225
226
227

228
229
230
231
232
233
234
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238







+







extern Value globals[0x800];
extern Uint32 firstobj,lastobj;
extern Uint32 playfield[64*64];
extern Uint8 pfwidth,pfheight;
extern Sint8 gameover,key_ignored;
extern Uint8 generation_number_inc;
extern Uint32 move_number;
extern unsigned char*quiz_text;

const char*value_string_ptr(Value v);
void pfunlink(Uint32 n);
void pflink(Uint32 n);
Uint32 objalloc(Uint16 c);
void objtrash(Uint32 n);
void annihilate(void);

Modified picture.c from [27a9f01e5e] to [608c347ef7].

543
544
545
546
547
548
549




































































































































































































































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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
  f=(y1-y0)/(double)(max+page);
  *cur=(y-y0+0.5)/f;
  draw_scrollbar(cur,page,max,x0,y0,x1,y1);
  SDL_Flip(screen);
  set_cursor(XC_sb_right_arrow);
  return 1;
}

// Popup text

/*
  \0 to \7 (1 to 8) - Colours
  \b (15) - Horizontal line
  \c (12) - Centre align
  \i (14) - Image of class
  \l (11) - Left align
  \n (10) - Line break
  \q (16) - Quiz button
  \x (31) - Hex escape
*/

typedef struct {
  Uint8 a,h;
  Uint16 w;
} PopLine;

static void pop_bar(int x,int y,PopLine*li,int w) {
  
}

static void pop_char(int x,int y,PopLine*li,Uint8 c,Uint8 v) {
  Uint8*p=screen->pixels;
  Uint16 pitch=screen->pitch;
  int xx,yy;
  const unsigned char*f=fontdata+(v<<3);
  if(li->h>8) y+=(li->h-8)/2;
  if(y+8>=screen->h || x+8>=screen->w) return;
  p+=y*pitch+x;
  for(yy=0;yy<8;yy++) {
    for(xx=0;xx<8;xx++) p[xx]=(*f<<xx)&128?c:0xF7;
    p+=pitch;
    ++f;
  }
}

static void pop_image(int x,int y,PopLine*li,const unsigned char*t) {
  const unsigned char*p=strchr(t,':');
  int i,n;
  if(!p) return;
  n=strtol(p+1,0,10);
  if(li->h>picture_size) y+=(li->h-picture_size)/2;
  if(y+picture_size>=screen->h || x+picture_size>=screen->w) return;
  SDL_UnlockSurface(screen);
  if(p==t+1 && *t==7) {
    // This case is used for %i substitutions
    draw_picture(x,y,n);
  } else if(p) {
    for(i=1;i<0x4000;i++) if(classes[i]) {
      if(strlen(classes[i]->name)==p-t && !memcmp(t,classes[i]->name,p-t)) {
        if(n>=0 && n<classes[i]->nimages) draw_picture(x,y,classes[i]->images[n]&0x7FFF);
        break;
      }
    }
  }
  SDL_LockSurface(screen);
}

static void pop_quiz(int x,int y,PopLine*li,Uint8 c,Uint8 v) {
  
}

void draw_popup(const unsigned char*txt) {
  static colo[8]={1,144,188,173,83,98,218,15};
  static PopLine li[64];
  SDL_Rect r;
  int bx,by,x,y,c;
  int ln=0; // line number
  int lh=8; // height of current line
  int th=0; // total height
  int lw=0; // width of current line
  int tw=0; // total width
  const unsigned char*p=txt;
  li[0].w=li[0].h=li[0].a=0;
  // Figure out size
  while(ln<64 && *p) switch(*p++) {
    case 1 ... 8:
      // needing doing nothing yet
      break;
    case 10:
      th+=lh;
      li[ln].h=lh;
      li[ln].w=lw;
      if(tw<lw) tw=lw;
      if(*p && *p!=15) lh=8; else lh=0;
      lw=0;
      ln++;
      if(ln<64) li[ln].a=li[ln-1].a;
      break;
    case 11: case 12:
      li[ln].a=p[-1]-11;
      break;
    case 14:
      if(lh<picture_size) lh=picture_size;
      lw+=picture_size;
      p=strchr(p,'\\')?:"";
      if(*p) p++;
      break;
    case 15:
      th+=lh+8;
      li[ln].h=lh;
      li[ln].w=lw;
      if(tw<lw) tw=lw;
      lh=8;
      lw=0;
      ln++;
      if(ln<64) li[ln].a=li[ln-1].a;
      break;
    case 16:
      lw+=24;
      if(lh<16) lh=16;
      if(*p) p++;
      break;
    case 31:
      lw+=8;
      if(*p) p++;
      break;
    default:
      lw+=8;
  }
  done:
  if(ln<64) li[ln].h=lh,li[ln].w=lw;
  p=txt;
  th+=lh;
  if(tw<lw) tw=lw;
  if(tw>screen->w-32) tw=screen->w-32;
  if(th>screen->h-32) th=screen->h-32;
  bx=(screen->w-tw-8)/2;
  by=(screen->h-th-8)/2;
  // Draw box
  r.x=bx; r.y=by; r.w=tw+16; r.h=th+16;
  SDL_FillRect(screen,&r,0xF7);
  r.w=tw+16; r.h=1; SDL_FillRect(screen,&r,0x0F);
  r.w=1; r.h=th+16; SDL_FillRect(screen,&r,0x0F);
  r.x++; r.y++;
  r.w=tw+14; r.h=1; SDL_FillRect(screen,&r,0x0D);
  r.w=1; r.h=th+14; SDL_FillRect(screen,&r,0x0D);
  r.x++; r.y++;
  r.w=tw+12; r.h=1; SDL_FillRect(screen,&r,0x0B);
  r.w=1; r.h=th+12; SDL_FillRect(screen,&r,0x0B);
  r.x=bx+tw+15; r.y=by;
  r.w=1; r.h=th+16; SDL_FillRect(screen,&r,0x02);
  r.x--; r.y++; r.h-=2; SDL_FillRect(screen,&r,0x05);
  r.x--; r.y++; r.h-=2; SDL_FillRect(screen,&r,0x08);
  r.x=bx; r.y=by+th+15;
  r.w=tw+16; r.h=1; SDL_FillRect(screen,&r,0x02);
  r.x++; r.y--; r.w-=2; SDL_FillRect(screen,&r,0x05);
  r.x++; r.y--; r.w-=2; SDL_FillRect(screen,&r,0x08);
  // Draw text
  bx+=8; by+=8; tw-=8; th-=8;
  x=bx; y=by;
  c=colo[0];
  ln=0;
  SDL_LockSurface(screen);
  while(*p && ln<64 && y+li[ln].h<screen->h) switch(*p++) {
    case 1 ... 8:
      c=colo[p[-1]-1];
      break;
    case 10:
      y+=li[ln++].h;
      if(ln<64) x=li[ln].a?bx+(tw-li[ln].w)/2:bx;
      break;
    case 11:
      // do nothing
      break;
    case 12:
      if(x==bx) x+=(tw-li[ln].w)/2;
      break;
    case 14:
      pop_image(x,y,li+ln,p);
      x+=picture_size;
      p=strchr(p,'\\')?:"";
      if(*p) p++;
      break;
    case 15:
      y+=li[ln++].h;
      pop_bar(bx,y,li+ln,tw);
      y+=8;
      if(ln<64) x=li[ln].a?bx+(tw-li[ln].w)/2:bx;
      break;
    case 16:
      pop_quiz(x,y,li+ln,c,*p);
      if(*p) p++;
      x+=24;
      break;
    case 31:
      pop_char(x,y,li+ln,c,*p);
      if(*p) p++;
      x+=8;
    default:
      pop_char(x,y,li+ln,c,p[-1]);
      x+=8;
      break;
  }
  SDL_UnlockSurface(screen);
}

int modal_draw_popup(const unsigned char*txt) {
  SDL_Event ev;
  SDL_Rect r;
  r.x=r.y=0;
  r.w=screen->w;
  r.h=4;
  SDL_FillRect(screen,&r,0xFE);
  set_cursor(XC_iron_cross);
  redraw:
  draw_popup(txt);
  SDL_Flip(screen);
  while(SDL_WaitEvent(&ev)) switch(ev.type) {
    case SDL_QUIT:
      SDL_PushEvent(&ev);
      return -1;
    case SDL_KEYDOWN:
      switch(ev.key.keysym.sym) {
        case SDLK_RETURN: case SDLK_KP_ENTER: case SDLK_ESCAPE:
          return 0;
      }
      break;
    case SDL_MOUSEBUTTONDOWN:
      return 0;
    case SDL_VIDEOEXPOSE:
      goto redraw;
  }
  return -1;
}