-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.c
More file actions
1451 lines (1316 loc) · 45.2 KB
/
Copy pathapp.c
File metadata and controls
1451 lines (1316 loc) · 45.2 KB
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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <agar/core.h>
#include <agar/gui.h>
#include "mini_file_dlg.h"
#include "color_widget.h"
#include "SDL.h"
#include "SDL_ttf.h"
#include "Dingoo_SDL_keymap.h"
#include "c_vector.h"
#include "codepages.h"
#include "def_code_page.h"
//#define _DINGUX 1
#ifdef _DINGUX
# include "MK_Dingux_keymap.h"
#elif _WIN32
# include "MK_Win_keymap.h"
#endif
#define screen_width 320
#define screen_height 240
static int screen_flags = SDL_ANYFORMAT;//|SDL_FULLSCREEN;
static SDL_Surface *p_screen = NULL;
static SDL_Rect screen_rect = {0, 0, screen_width, screen_height};
static char font_name[] = "FONT.TTF";
static TTF_Font *font;
static SDL_Color font_color = {255, 255, 255};
static SDL_Color chgd_fnt_clr = {255, 255, 255};
static SDL_Color bg_color = {0, 0, 0};
static SDL_Color chgd_bg_clr = {0, 0, 0};
SDLKey key;
SDL_Event event;
int font_size = 14;
/* Back и Front применяются для обозначения направления чтения в файле.
Back - движение в файле в сторону его начала.
Front - в сторону конца.
ToEnd и ToFront применяются для обозначения позиции вставки блока в ленту.
ToEnd - вставка в конец ленты.
ToFront - вставка в начало. */
typedef enum __TNewLineWay {CR, LF, CRLF, UNKNOWN_NL} TNewLineWay;
typedef enum __TDirection {End = 0,Back = 0,Front = 1,ToEnd = 0,ToFront = 1} TDirection;
typedef enum __TEndianType {LittleEndian, BigEndian} TEndianType;
typedef enum __TContentType {Image, Text} TContentType;
typedef struct __TTapeBlock
{
int64_t upper_bound, lower_bound;
uint32_t width, height;
SDL_Surface *p_image;
fpos_t f_upper_pos, f_lower_pos;
} TTapeBlock, *PTTapeBlock;
typedef struct __TTape
{
int64_t screen_upper_bound;
int64_t upper_bound, lower_bound;
c_vector blocks;
} TTape, *PTTape;
typedef struct __TSource
{
FILE *p_file;
TNewLineWay new_line_way;
TCodePage code_page;
TEndianType source_endian, machine_endian;
} TSource, *PTSource;
PTTape p_tape = NULL;
PTSource p_source = NULL;
inline int
fbof(FILE *stream)
{
return !ftell(stream);
}
PTTape
CreateTape()
{
PTTape p_tape = malloc(sizeof(TTape));
c_vector_create(&p_tape->blocks, NULL);
c_vector_reserve(&p_tape->blocks, 20);
p_tape->upper_bound = p_tape->lower_bound = p_tape->screen_upper_bound = 0;
return p_tape;
}
inline void
FreeTapeBlock(PTTapeBlock p_block)
{
if(p_block)
{
SDL_FreeSurface(p_block->p_image);
free(p_block);
}
}
void
ClearTape(PTTape p_tape)
{
c_iterator first;
p_tape->upper_bound = p_tape->lower_bound = p_tape->screen_upper_bound = 0;
first = c_vector_begin(&p_tape->blocks);
while(c_vector_size(&p_tape->blocks))
{
FreeTapeBlock((PTTapeBlock)ITER_REF(first));
c_vector_erase(&p_tape->blocks, first);
}
}
void
FreeTape(PTTape p_tape)
{
ClearTape(p_tape);
c_vector_destroy(&p_tape->blocks);
free(p_tape);
}
PTTapeBlock
AddBlockToTape(PTTape p_tape, PTTapeBlock p_block, TDirection pos)
{
if(!p_tape || !p_block || !p_block->p_image) return NULL;
p_block->width = p_block->p_image->w;
p_block->height = p_block->p_image->h;
if(c_vector_size(&p_tape->blocks))
{
if(pos == ToFront)
{
p_block->lower_bound = ((PTTapeBlock)c_vector_front(&p_tape->blocks))->upper_bound;
p_block->upper_bound = p_block->lower_bound - p_block->height;
c_vector_insert(&p_tape->blocks, c_vector_begin(&p_tape->blocks), p_block);
p_tape->upper_bound = p_block->upper_bound;
}
else
{
p_block->upper_bound = ((PTTapeBlock)c_vector_back(&p_tape->blocks))->lower_bound;
p_block->lower_bound = p_block->upper_bound + p_block->height;
c_vector_insert(&p_tape->blocks, c_vector_end(&p_tape->blocks), p_block);
p_tape->lower_bound = p_block->lower_bound;
}
}
else
{
p_block->upper_bound = 0;
p_block->lower_bound = p_block->height;
c_vector_insert(&p_tape->blocks, c_vector_begin(&p_tape->blocks), p_block);
p_tape->lower_bound = p_block->lower_bound;
}
return p_block;
}
PTTapeBlock
RemoveBlockFromTape(PTTape p_tape, TDirection pos)
{
c_iterator first, last;
if(c_vector_size(&p_tape->blocks))
{
if(pos == Front)
{
first = c_vector_begin(&p_tape->blocks);
FreeTapeBlock((PTTapeBlock)ITER_REF(first));
c_vector_erase(&p_tape->blocks, first);
p_tape->upper_bound = ((PTTapeBlock)ITER_REF(first))->upper_bound;
return c_vector_size(&p_tape->blocks) ? (PTTapeBlock)ITER_REF(first) : NULL;
}
else
{
last = c_vector_end(&p_tape->blocks);
ITER_DEC(last);
FreeTapeBlock((PTTapeBlock)ITER_REF(last));
c_vector_erase(&p_tape->blocks, last);
ITER_DEC(last);
p_tape->lower_bound = ((PTTapeBlock)ITER_REF(last))->lower_bound;
return c_vector_size(&p_tape->blocks) ? (PTTapeBlock)ITER_REF(last) : NULL;
}
}
return NULL;
}
inline void
ScrollTape(PTTape p_tape, int64_t dy)
{
p_tape->screen_upper_bound += dy;
if(p_tape->screen_upper_bound < p_tape->upper_bound)
p_tape->screen_upper_bound = p_tape->upper_bound;
if(p_tape->screen_upper_bound > p_tape->lower_bound)
p_tape->screen_upper_bound = p_tape->lower_bound;
}
inline bool
InSegment(int64_t up, int64_t down, int64_t y)
{
return (y >= up && y <= down) ? true : false;
}
inline int
DrawTape(PTTape p_tape, SDL_Surface *p_screen)
{
if(!c_vector_size(&p_tape->blocks)) return -1;
c_iterator first, last, iter;
SDL_Rect dst_rect, src_rect;
PTTapeBlock p_block;
first = c_vector_begin(&p_tape->blocks);
last = c_vector_end(&p_tape->blocks);
int64_t current_pos,screen_lower_bound=p_tape->screen_upper_bound+screen_height;
SDL_FillRect(p_screen, &screen_rect, SDL_MapRGB(p_screen->format, bg_color.r, bg_color.g,
bg_color.b));
for(iter = first; !ITER_EQUAL(iter, last); ITER_INC(iter))
{
assert(p_block = (PTTapeBlock)ITER_REF(iter));
if(InSegment(p_block->upper_bound, p_block->lower_bound, p_tape->screen_upper_bound))
{
current_pos = 0;
while(current_pos < screen_height &&!ITER_EQUAL(iter, last))
{
src_rect.x = 0;
src_rect.w = p_block->width;
if(p_block->upper_bound < p_tape->screen_upper_bound)
src_rect.y = p_tape->screen_upper_bound - p_block->upper_bound;
else src_rect.y = 0;
if(p_block->lower_bound > screen_lower_bound)
src_rect.h = screen_lower_bound - p_block->upper_bound - src_rect.y;
else src_rect.h = p_block->height - src_rect.y;
dst_rect = src_rect;
dst_rect.y = current_pos;
SDL_BlitSurface(p_block->p_image, &src_rect, p_screen, &dst_rect);
current_pos += src_rect.h;
ITER_INC(iter);
p_block = (PTTapeBlock)ITER_REF(iter);
}
break;
}
}
return 0;
}
TCodePage
FileHasBOM(FILE *stream)
{
unsigned char BOM[4];
fpos_t backup;
TCodePage result = UNKNOWN_CP;
fgetpos(stream, &backup);
rewind(stream);
fread(BOM, 1, 4, stream);
if(BOM[0] == 0xFF && BOM[1] == 0xFE)
{
if(!BOM[2] && !BOM[3]) result = UTF32LE;
else result = UTF16LE;
}
else if(!BOM[0] && !BOM[1] && BOM[2] == 0xFE && BOM[3] == 0xFF)
result = UTF32BE;
else if(BOM[0] == 0xFE && BOM[1] == 0xFF)
result = UTF16BE;
else if(BOM[0] == 0xEF && BOM[1] == 0xBB && BOM[2] == 0xBF)
result = UTF8;
fsetpos(stream, &backup);
return result;
}
TEndianType
MachineEndianType()
{
uint16_t x = 1; /* 0000 0001 */
return *((uint8_t *) &x) == 0 ? BigEndian : LittleEndian;
}
inline int
NextChar(PTSource p_source, TUTF32Char *out, TDirection direction);
TNewLineWay
NewLineWay(PTSource p_source)
{
fpos_t backup;
TUTF32Char bufer_1, bufer_2;
fgetpos(p_source->p_file, &backup);
fseek(p_source->p_file, 0, SEEK_SET);
while(!NextChar(p_source, &bufer_1, Front))
{
if(bufer_1 == 0x0D || bufer_1 == 0x0A)
{
NextChar(p_source, &bufer_2, Front);
fsetpos(p_source->p_file, &backup);
if(bufer_1 == 0x0D && bufer_2 == 0x0A)
{
printf("NEW LINE TYPE: CRLF\n");
return CRLF;
}
if(bufer_1 == 0x0A && bufer_2 == 0x0D)
{
printf("NEW LINE TYPE: CRLF\n");
return CRLF;
}
if(bufer_1 == 0x0D)
{
printf("NEW LINE TYPE: CR\n");
return CR;
}
if(bufer_1 == 0x0A)
{
printf("NEW LINE TYPE: LF\n");
return LF;
}
break;
}
}
fsetpos(p_source->p_file, &backup);
printf("NEW LINE TYPE: UNKNOWN\n");
return UNKNOWN_NL;
}
TContentType
ReadTXTBlock(PTSource p_source, TDirection direction,
TUTF32Char *p_bufer, TUTF32Char **p_out);
TContentType
ReadFB2Block(PTSource p_source, TDirection direction,
TUTF32Char *p_bufer, TUTF32Char **p_out);
TContentType
(*ReadBlockFunc)(PTSource p_source, TDirection direction,
TUTF32Char *p_bufer, TUTF32Char **p_out);
PTSource
CreateSource(const char *file_name, TCodePage CP)
{
FILE *_p_file = fopen(file_name, "rb");
if(!_p_file) return NULL;
PTSource p_source = (PTSource)malloc(sizeof(TSource));
p_source->p_file = _p_file;
if(strstr(file_name, ".fb2"))
{
ReadBlockFunc = ReadFB2Block;
CP = CP1251;
}
else
{
ReadBlockFunc = ReadTXTBlock;
}
p_source->machine_endian = MachineEndianType();
if(p_source->machine_endian == LittleEndian)
printf("MACHINE ENDIAN: LITTLE ENDIAN\n");
else printf("MACHINE ENDIAN: BIG ENDIAN\n");
if(CP != UNKNOWN_CP) p_source->code_page = CP;
else
{
unsigned char bufer[150];
size_t readed = fread(bufer, 1, 150, p_source->p_file);
rewind(p_source->p_file);
p_source->code_page = m_def_code(bufer, readed, 255);
switch(p_source->code_page)
{
case CP866:
printf("CODEPAGE: CP866\n");
break;
case CP1251:
printf("CODEPAGE: CP1251\n");
break;
case KOI8R:
printf("CODEPAGE: KOI8R\n");
break;
case UTF8:
printf("CODEPAGE: UTF8\n");
break;
case UTF16BE:
p_source->code_page = UTF16;
p_source->source_endian = BigEndian;
printf("CODEPAGE: UTF16BE\n");
break;
case UTF16LE:
p_source->code_page = UTF16;
p_source->source_endian = LittleEndian;
printf("CODEPAGE: UTF16LE\n");
break;
case UTF32LE:
p_source->code_page = UTF32;
p_source->source_endian = LittleEndian;
printf("CODEPAGE: UTF32LE\n");
break;
case UTF32BE:
p_source->code_page = UTF32;
p_source->source_endian = BigEndian;
printf("CODEPAGE: UTF32BE\n");
break;
default:
printf("ERROR: UNKNOWN CODEPAGE\n");
break;
}
}
p_source->new_line_way = NewLineWay(p_source);
return p_source;
}
int
FreeSource(PTSource p_source)
{
if(!p_source) return - 1;
int result = fclose(p_source->p_file);
free(p_source);
return result;
}
// А то хрен его знает какой EOF на других платформах
#define BOF EOF-1
#define EOF_FERROR EOF-2
#define FOK 0
inline int
NextCharUTF8(PTSource p_source, TUTF32Char *out, TDirection direction)
{
if(ferror(p_source->p_file)) return EOF_FERROR;
int result = FOK;
if(!direction && fbof(p_source->p_file)) result = BOF;
if(direction && feof(p_source->p_file)) result = EOF;
char utf8[6];
TUTF32Char ch;
fread(utf8, 1, 1, p_source->p_file);
ch = *((unsigned char *)utf8);
if(ch < 0x80)
{
*out = ch;
if(!direction) fseek(p_source->p_file, result==BOF?-1:-2, SEEK_CUR);
return result;
}
if(!direction && (result != BOF))
{
fseek(p_source->p_file, -2, SEEK_CUR);
int i = 0;
while(ch < 0xC0 && i < 6 && !fbof(p_source->p_file))
{
fread(utf8, 1, 1, p_source->p_file);
fseek(p_source->p_file, -2, SEEK_CUR);
ch = *((const unsigned char *)utf8);
i++;
}
if(fbof(p_source->p_file)) return result;
fseek(p_source->p_file, 2, SEEK_CUR);
}
if ( ch >= 0xFC )
{
ch = (Uint32)(utf8[0]&0x01) << 30;
fread(utf8, 1, 4, p_source->p_file);
if(!direction) fseek(p_source->p_file, result==BOF?-6:-7, SEEK_CUR);
ch |= (Uint32)(utf8[0]&0x3F) << 24;
ch |= (Uint32)(utf8[1]&0x3F) << 18;
ch |= (Uint32)(utf8[2]&0x3F) << 12;
ch |= (Uint32)(utf8[3]&0x3F) << 6;
ch |= (Uint32)(utf8[4]&0x3F);
}
else if (ch >= 0xF8)
{
ch = (Uint32)(utf8[0]&0x03) << 24;
fread(utf8, 1, 4, p_source->p_file);
if(!direction) fseek(p_source->p_file, result==BOF?-5:-6, SEEK_CUR);
ch |= (Uint32)(utf8[0]&0x3F) << 18;
ch |= (Uint32)(utf8[1]&0x3F) << 12;
ch |= (Uint32)(utf8[2]&0x3F) << 6;
ch |= (Uint32)(utf8[3]&0x3F);
}
else if (ch >= 0xF0)
{
ch = (TUTF32Char)(utf8[0]&0x07) << 18;
fread(utf8, 1, 3, p_source->p_file);
if(!direction) fseek(p_source->p_file, result==BOF?-4:-5, SEEK_CUR);
ch |= (TUTF32Char)(utf8[0]&0x3F) << 12;
ch |= (TUTF32Char)(utf8[1]&0x3F) << 6;
ch |= (TUTF32Char)(utf8[2]&0x3F);
}
else if (ch >= 0xE0)
{
ch = (TUTF32Char)(utf8[0]&0x0F) << 12;
fread(utf8, 1, 2, p_source->p_file);
if(!direction) fseek(p_source->p_file, result==BOF?-3:-4, SEEK_CUR);
ch |= (TUTF32Char)(utf8[0]&0x3F) << 6;
ch |= (TUTF32Char)(utf8[1]&0x3F);
}
else if (ch >= 0xC0)
{
ch = (TUTF32Char)(utf8[0]&0x1F) << 6;
fread(utf8, 1, 1, p_source->p_file);
if(!direction) fseek(p_source->p_file, result==BOF?-2:-3, SEEK_CUR);
ch |= (TUTF32Char)(utf8[0]&0x3F);
}
*out = ch;
return result;
}
inline int
NextChar(PTSource p_source, TUTF32Char *out, TDirection direction)
{
if(ferror(p_source->p_file)) return EOF_FERROR;
int result = FOK;
if(!direction) if(fbof(p_source->p_file)) result = BOF;
if(direction) if(feof(p_source->p_file)) result = EOF;
static unsigned char bufer[4];
bool swapped = p_source->source_endian != p_source->machine_endian;
switch(p_source->code_page)
{
case UTF8:
return NextCharUTF8(p_source, out, direction);
break;
case UTF16:
if(fread(bufer, 1, 2, p_source->p_file) <= 0) result = EOF_FERROR;
if(!direction) fseek(p_source->p_file, result==BOF?-2:-4, SEEK_CUR);
*out = swapped ? SDL_Swap16(*((TUTF16Char*)bufer)) : *((TUTF16Char*)bufer);
break;
case UTF32:
if(fread(bufer, 1, 4, p_source->p_file) <= 0) result = EOF_FERROR;
if(!direction) fseek(p_source->p_file, result==BOF?-4:-8, SEEK_CUR);
*out = swapped ? SDL_Swap32(*((TUTF32Char*)bufer)) : *((TUTF32Char*)bufer);
break;
case CP1251:
if(fread(bufer, 1, 1, p_source->p_file) <= 0) result = EOF_FERROR;
if(!direction) fseek(p_source->p_file, result==BOF?-1:-2, SEEK_CUR);
*out = CP1251_UNICODE[*bufer];
break;
case KOI8R:
if(fread(bufer, 1, 1, p_source->p_file) <= 0) result = EOF_FERROR;
if(!direction) fseek(p_source->p_file, result==BOF?-1:-2, SEEK_CUR);
*out = KOI8R_UNICODE[*bufer];
break;
case CP866:
if(fread(bufer, 1, 1, p_source->p_file) <= 0) result = EOF_FERROR;
if(!direction) fseek(p_source->p_file, result==BOF?-1:-2, SEEK_CUR);
*out = CP866_UNICODE[*bufer];
break;
case MACCYR:
if(fread(bufer, 1, 1, p_source->p_file) <= 0) result = EOF_FERROR;
if(!direction) fseek(p_source->p_file, result==BOF?-1:-2, SEEK_CUR);
*out = MACCYR_UNICODE[*bufer];
break;
}
return result;
}
#define max_block_size 8192
/* Функция читая блок текста(абзац), убирает лишние переносы каретки. Формат новой
строки - 0x0A. Далее последовательные вызовы функции RenderTXTLine отрисовывают его
построчно. */
TContentType
ReadTXTBlock(PTSource p_source, TDirection direction,
TUTF32Char *p_bufer, TUTF32Char **p_out)
{
TUTF32Char NL, nope;
int i, result, nl_count = 0, from_nl = 0, offset;
if(direction == Front)
{
i = 0;
NL = (p_source->new_line_way == LF) ? 0x0A : 0x0D;
offset = 1;
}
else //direction == Back
{
p_bufer[max_block_size - 1] = 0;
i = max_block_size - 2;
NL = (p_source->new_line_way == CR) ? 0x0D : 0x0A;
offset = -1;
}
do
{
if(direction ? (i>=max_block_size-2) : (i<=0))
{
break;
}
result = NextChar(p_source, &p_bufer[i], direction);
if(p_bufer[i] == NL)
{
if(p_source->new_line_way == CRLF)
result = NextChar(p_source, &nope, direction);
p_bufer[i] = 0x0A;
nl_count++;
}
else if(nl_count)
{
result = NextChar(p_source, &nope, !direction);
i-=offset;
if(nl_count > 1)
{
break;
}
// новый абзац. В начале абзаца, бывают ещё и табы, бро.
if(p_bufer[i+1] == 0x20)
{
break;
}
else
{
p_bufer[i] = 0x20;
} // Заголовок. Cтоит указать половину, примерного
if(from_nl < 20) // кол-ва глифов, влезающих в строку.
{
p_bufer[i] = 0x0A;
}
nl_count = from_nl = 0;
}
if(result == FOK) i+=offset;
from_nl++;
}
while(result == FOK);
if(direction ? (i == 0) : (i == max_block_size - 2))
{
*p_out = NULL;
}
else
{
if(direction) p_bufer[i+1] = 0;
*p_out = direction ? p_bufer : (p_bufer + i);
}
return Text;
}
#define max_tag_size 100
// Заглушка
TContentType
ReadFB2Block(PTSource p_source, TDirection direction,
TUTF32Char *p_bufer, TUTF32Char **p_out)
{
TUTF32Char nope, tag[max_tag_size];
int i, j, result, offset;
static TUTF32Char left_bracket = '<';
static TUTF32Char right_bracket = '>';
static TUTF32Char tag_p[] = {'p', 0};
bool done = false;
TUTF32Char first_bracket, second_bracket;
if(direction == Front)
{
i = 0;
offset = 1;
first_bracket = left_bracket;
second_bracket = right_bracket;
}
else //direction == Back
{
p_bufer[max_block_size - 1] = 0;
tag[max_tag_size - 1] = 0;
i = max_block_size - 2;
offset = -1;
first_bracket = right_bracket;
second_bracket = left_bracket;
}
result = FOK;
while(result == FOK)
{
j = (direction == Front) ? 0 : max_tag_size - 2;
do // <
{
result = NextChar(p_source, &nope, direction);
}
while(result == FOK && nope != first_bracket);
do
{
result = NextChar(p_source, &tag[j], direction);
if(tag[j] != second_bracket) j += offset;
else break;
}
while(result == FOK); // tag>
if(result == FOK)
{
if(direction) tag[j] = 0;
else j -= offset;
if((tag[0] == 'p' && tag[1] == 0) || (tag[j] == '/' && tag[j+1] == 'p' && tag[j+2] == 0))
{
do // text
{
result = NextChar(p_source, &p_bufer[i], direction);
if(p_bufer[i] != first_bracket) i += offset;
else break;
}
while(result == FOK);
if(!direction) i -= offset;
if(direction ? (i == 0) : (i == max_block_size - 2))
{
*p_out = NULL;
}
else
{
if(direction) p_bufer[i] = 0;
*p_out = direction ? p_bufer : (p_bufer + i);
}
return Text;
}
}
}
}
/* Функция рендерит строку до нуля или до 0x0A(LF).Возвращает в p_chars_printed кол-во
считанных байт. Если строка NULL или первый символ в ней 0, возвращается NULL */
SDL_Surface *
RenderTXTLine(TUTF32Char *p_str, int *p_chars_printed)
{
if(!p_str) return NULL;
if(!*p_str) return NULL;
SDL_Surface *textbuf;
TUTF32Char temp;
static TUTF32Char empty_line[] = {0x20, 0x00};
int i = 0, char_width, string_width = 0;
while(p_str[i] != 0)
{
if(string_width >= screen_width - 10)
{
break;
}
if(p_str[i] == 0x0A)
{
if(!i)
{
//textbuf = TTF_RenderUTF32_Blended(font, empty_line, font_color);
textbuf = TTF_RenderUTF32_Shaded(font, empty_line, font_color, bg_color);
*p_chars_printed = 1;
return textbuf;
}
else
{
break;
}
}
TTF_GlyphMetrics(font,p_str[i],NULL,NULL,NULL,NULL,&char_width);
string_width += char_width;
i++;
}
temp = p_str[i];
p_str[i] = 0;
//textbuf = TTF_RenderUTF32_Blended(font, p_str, font_color);
textbuf = TTF_RenderUTF32_Shaded(font, p_str, font_color, bg_color);
p_str[i] = temp;
*p_chars_printed = p_str[i] == 0x0A ? (i+1) : i;
return textbuf;
}
PTTapeBlock
RenderTXTBlock(PTSource p_source, TDirection direction)
{
PTTapeBlock p_block;
static TUTF32Char block[max_block_size], *p_string;
static SDL_Surface *p_surfaces[100];
SDL_Rect dst_rect;
int str_count = 0, i = 0, h;
fpos_t f_upper_pos, f_lower_pos;
fgetpos(p_source->p_file,direction?(&f_upper_pos):&(f_lower_pos));
//ReadTXTBlock(p_source, direction, block, &p_string);
ReadBlockFunc(p_source, direction, block, &p_string);
if(!p_string) return NULL;
if(p_string[0] == 0x0A) p_string++;
fgetpos(p_source->p_file,direction?(&f_lower_pos):(&f_upper_pos));
while(p_surfaces[str_count] = RenderTXTLine(p_string+=i, &i))str_count++;
if(str_count)
{
p_block = malloc(sizeof(TTapeBlock));
if(!p_block) return NULL;
p_block->f_lower_pos = f_lower_pos;
p_block->f_upper_pos = f_upper_pos;
for(i = 0, h = 0; i < str_count; h += p_surfaces[i]->h, i++);
p_block->p_image = SDL_CreateRGBSurface(0, screen_width, h,
p_screen->format->BitsPerPixel,
0, 0, 0, 0);
if(!p_block->p_image) return NULL;
dst_rect.x = dst_rect.y = 0;
dst_rect.w = screen_width;
dst_rect.h = h;
SDL_FillRect(p_block->p_image, &dst_rect, SDL_MapRGB(p_screen->format,
bg_color.r, bg_color.g, bg_color.b));
for(i = 0; i < str_count; i++)
{
SDL_BlitSurface(p_surfaces[i], NULL, p_block->p_image, &dst_rect);
dst_rect.y += p_surfaces[i]->h;
SDL_FreeSurface(p_surfaces[i]);
}
return p_block;
}
return NULL;
}
#define min_preload_range 3 * screen_height;
/* Планируется только прокрутка юзером вручную и переход на часть по проценту.
- С переходом по проценту всё понятно. Перешли на позицию в файле и заполнили
ленту вверх и вниз.
- При перемотке подгружаются блоки текста пока расстояние от верхней границы
экрана до краёв ленты не станет больше, чем min_preload_range. */
void
UpdateTape(PTTape p_tape, PTSource p_source)
{
c_iterator first, last;
PTTapeBlock p_block;
int64_t screen_lower_bound, min_upper_bound, min_lower_bound;
if(!c_vector_size(&p_tape->blocks))
{
p_block = AddBlockToTape(p_tape, RenderTXTBlock(p_source, Front), ToEnd);
}
screen_lower_bound = p_tape->screen_upper_bound + screen_height;
min_upper_bound = p_tape->screen_upper_bound - min_preload_range;
min_lower_bound = screen_lower_bound + min_preload_range;
first = c_vector_begin(&p_tape->blocks);
if(p_tape->upper_bound > min_upper_bound) // М. б. нужно добавить блоки в верх ленты
{
p_block = (PTTapeBlock)ITER_REF(first);
if(p_block)
{
fsetpos(p_source->p_file, &p_block->f_upper_pos);
while(p_block&&!InSegment(p_block->upper_bound,p_block->lower_bound,min_upper_bound))
{
p_block = AddBlockToTape(p_tape, RenderTXTBlock(p_source, Back), ToFront);
}
}
}
else // удалить ненужные блоки вверху
{
p_block = (PTTapeBlock)ITER_REF(first);
while(p_block&&!InSegment(p_block->upper_bound,p_block->lower_bound,min_upper_bound))
{
p_block = RemoveBlockFromTape(p_tape, Front);
}
}
last = c_vector_end(&p_tape->blocks);
ITER_DEC(last);
if(p_tape->lower_bound < min_lower_bound) // Может стоит добавить блоки в низ ленты
{
p_block = (PTTapeBlock)ITER_REF(last);
if(p_block)
{
fsetpos(p_source->p_file, &p_block->f_lower_pos);
while(p_block&&!InSegment(p_block->upper_bound,p_block->lower_bound,min_lower_bound))
{
p_block = AddBlockToTape(p_tape, RenderTXTBlock(p_source, Front), ToEnd);
}
}
}
else
{
p_block = (PTTapeBlock)ITER_REF(last);
while(p_block&&!InSegment(p_block->upper_bound,p_block->lower_bound,min_lower_bound))
{
p_block = RemoveBlockFromTape(p_tape, End);
}
}
}
void
RedrawTape(PTTape p_tape, PTSource p_source)
{
c_iterator first, last, iter;
first = c_vector_begin(&p_tape->blocks);
last = c_vector_end(&p_tape->blocks);
PTTapeBlock p_block;
for(iter = first; !ITER_EQUAL(iter, last); ITER_INC(iter))
{
p_block = (PTTapeBlock)ITER_REF(iter);
if(InSegment(p_block->upper_bound, p_block->lower_bound, p_tape->screen_upper_bound))
{
fsetpos(p_source->p_file, &p_block->f_upper_pos);
ClearTape(p_tape);
UpdateTape(p_tape, p_source);
break;
}
}
}
void
SaveBookPos(PTTape p_tape, char **argv)
{
c_iterator first, last, iter;
first = c_vector_begin(&p_tape->blocks);
last = c_vector_end(&p_tape->blocks);
PTTapeBlock p_block;
for(iter = first; !ITER_EQUAL(iter, last); ITER_INC(iter))
{
p_block = (PTTapeBlock)ITER_REF(iter);
if(InSegment(p_block->upper_bound, p_block->lower_bound, p_tape->screen_upper_bound))
{
char bookmark[FILENAME_MAX];
strcpy(bookmark, argv[1]);
strcat(bookmark, ".bm");
FILE *f_bookmark = fopen(bookmark, "wb");
if(f_bookmark)
{
fpos_t pos = p_block->f_upper_pos;
fwrite(&pos, sizeof(fpos_t), 1, f_bookmark);
fclose(f_bookmark);
}
break;
}
}
}
void
ApplySettings()
{
if(font_size != TTF_FontHeight(font))
{
TTF_SetFontKegel(&font, font_size);
RedrawTape(p_tape, p_source);
DrawTape(p_tape, p_screen);
font_size = TTF_FontHeight(font);
}
if(chgd_fnt_clr.r != font_color.r ||
chgd_fnt_clr.g != font_color.g ||
chgd_fnt_clr.b != font_color.b ||
chgd_bg_clr.r != bg_color.r ||
chgd_bg_clr.g != bg_color.g ||
chgd_bg_clr.b != bg_color.b)
{
font_color = chgd_fnt_clr;
bg_color = chgd_bg_clr;
RedrawTape(p_tape, p_source);
DrawTape(p_tape, p_screen);
}
}
int pressedKey = 0; // Last pressed key
int curFPS = 0; // Measured frame rate
const int nominalFPS = 42; // Nominal frame rate
bool menu_done = false;
bool done = false;
static void
MyEventLoop(void)
{
AG_Window *win;
Uint32 t1, t2;
AG_DriverEvent dev;
t1 = AG_GetTicks();
while(!menu_done)
{
t2 = AG_GetTicks();
if(t2 - t1 >= nominalFPS)
{
// Case 1: Update the video display.
AG_LockVFS(&agDrivers);
// Render the Agar windows
if(agDriverSw)
{
// With single-window drivers (e.g., sdlfb).
AG_BeginRendering(agDriverSw);
AG_FOREACH_WINDOW(win, agDriverSw)
{
AG_ObjectLock(win);
AG_WindowDraw(win);
AG_ObjectUnlock(win);
}
AG_EndRendering(agDriverSw);
}
AG_UnlockVFS(&agDrivers);
t1 = AG_GetTicks();
curFPS = nominalFPS - (t1 - t2);
if (curFPS < 1)
{