-
Notifications
You must be signed in to change notification settings - Fork 4
/
ui.c
1501 lines (1246 loc) · 44.4 KB
/
ui.c
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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#define NES_UI_C
#include "sys.h"
#include "nes.h"
#include "sound.h"
#include "vid.h"
#include "config.h"
#include "nespal.h"
#include "font.h"
#include "filesystem.h"
#include "ui.h"
#include <SDL_image.h>
void (*menu) (struct inputctx *) = NULL;
/*** Freetype glue ***/
FT_Library ftlibrary;
FT_Face face_sans;
int freetype_init = 0;
int ensure_freetype (void)
{
if (!freetype_init) {
freetype_init = -1;
int error = FT_Init_FreeType(&ftlibrary);
if (error) {
fprintf(stderr, "Unable to initialize freetype.\n");
return -1;
}
char *filename = "DejaVuSans.ttf";
error = FT_New_Face(ftlibrary, asset(filename), 0, &face_sans);
if (error) {
error = FT_New_Face(ftlibrary, localasset(filename), 0, &face_sans);
}
if (error) {
fprintf(stderr, "Error opening %s\n", filename);
return -1;
}
freetype_init = 1;
}
return freetype_init;
}
image_t sans_label (Uint32 color, unsigned text_height, const char *string)
{
static byte *rtmp = NULL;
int rwidth = window_surface->w;
static unsigned tmpheight = 0;
static unsigned rheight = 0;
int baseline = text_height + 4;
int error;
int first_char = 1;
int min_x = 0;
int min_y = baseline;
int max_y = baseline + 1;
int max_x = 1;
assert(text_height > 0);
if (text_height > tmpheight) {
if (rtmp) free(rtmp);
// This is what you might call a hack.
rheight = (text_height*2 + 4);
rtmp = calloc(1, rwidth * rheight);
assert(rtmp != NULL);
tmpheight = text_height;
} else memset(rtmp, 0, rwidth * rheight);
FT_Set_Pixel_Sizes(face_sans, 0, text_height);
int pen_x = 0;
FT_UInt last_glyph_index = 0;
const char *str = string;
for (char c = *str++; c; c=*str++) {
// Beg Freetype to render the glyph
FT_UInt glyph_index = FT_Get_Char_Index(face_sans, c);
if (first_char) first_char = 0;
else {
FT_Vector delta;
FT_Get_Kerning(face_sans, last_glyph_index, glyph_index,
FT_KERNING_DEFAULT, &delta);
pen_x += delta.x >> 6;
last_glyph_index = glyph_index;
}
error = FT_Load_Glyph(face_sans, glyph_index,
FT_LOAD_RENDER |
FT_LOAD_NO_HINTING |
FT_LOAD_TARGET_LIGHT);
if (error) continue;
FT_Bitmap *bmp = &face_sans->glyph->bitmap;
/*
printf(" x=%3i: Char %c: %ix%i + %i + %i advance=%f\n",
pen_x, c, bmp->width, bmp->rows,
face_sans->glyph->bitmap_left,
face_sans->glyph->bitmap_top,
((float)face_sans->glyph->advance.x) / 64.0);
*/
/* Transfer the glyph to the temporary buffer.. */
int ox0 = pen_x + face_sans->glyph->bitmap_left;
int ix0 = 0;
int ox1 = ox0 + bmp->width;
// Clip to left edge
if (ox0 < 0) {
//printf("PRECLIP %s\n", string);
ix0 -= ox0;
ox0 -= ox0;
}
if (ox0 >= rwidth) break;
// Clip to right edge
int overflow = max(0, ox1 - rwidth);
//if (overflow) printf("POSTCLIP\n");
ox1 -= overflow;
int width = ox1 - ox0;
//printf("Compute width \"%s\" char '%c' -- %i - %i = %i\n", string, c, ox1, ox0, width);
assert(width >= 0);
assert(ix0 >= 0);
int oy0 = baseline - face_sans->glyph->bitmap_top;
int height = bmp->rows;
// Update bounding rectangle
//min_x = min(min_x, cx0);
max_x = max(max_x, ox1);
min_y = max(0, min(min_y, oy0));
max_y = min(rheight, max(max_y, oy0+height));
// Ick?
unsigned char *input = bmp->buffer;
int input_pitch = bmp->pitch;
for (int y=0 ; y<height; y++) {
int y_out = oy0 + y;
if ((y_out >= 0) && (y_out < rheight)) {
byte *ptr = rtmp + y_out*rwidth;
unsigned char *row = input + ix0 + y*input_pitch;
for (int xoff=0; xoff<(ox1-ox0); xoff++) {
int tmp = ptr[ox0+xoff];
tmp += row[xoff];
ptr[ox0+xoff] = clamp_byte(tmp);
}
} else printf(" fixme at %i\n", y);
}
pen_x += face_sans->glyph->advance.x >> 6;
}
Uint32 *data = malloc(4*(max_x - min_x)*(max_y - min_y));
if (data == NULL) {
printf("Can't allocate final memory for size %i label \"%s\"\n", text_height, string);
printf("Final bounds: %i %i %i %i baseline=%i\n", 0, min_y, max_x, max_y, baseline);
return NULL;
} else {
image_t img = malloc(sizeof(*img));
assert(img);
int w = max_x - min_x;
for (int y=min_y; y<max_y; y++)
for (int x=min_x; x<max_x; x++)
data[(y-min_y)*w+(x-min_x)] = color | ((unsigned)rtmp[y*rwidth+x] << 24);
img->w = w;
img->h = max_y - min_y;
img->x_origin = 0;
img->y_origin = baseline - min_y;
img->freeptr = data;
/*
img->x_origin = -((img->w*align_x[ALIGN_MAX]) >> align_x[ALIGN_MAX_SHIFT]);
img->y_origin = -((img->h*align_y[ALIGN_MAX]) >> align_y[ALIGN_MAX_SHIFT])
- (min_y - baseline)*align_y[ALIGN_AXIS];
*/
img->_sdl = SDL_CreateRGBSurfaceFrom(data, img->w, img->h, 32, img->w*4,
0xFF0000, 0xFF00, 0xFF, 0xFF000000);
assert(img->_sdl);
return img;
}
}
/*** UI Utilities ***/
unsigned cursor_base[2] = {7,20};
unsigned cursor[2] = {0,0};
void setcursor (int col, int row)
{
cursor[0] = col * text_width + cursor_base[0];
cursor[1] = (row+1) * text_height + cursor_base[1];
}
const Uint32 color00 = 0xFFFFFF;
const Uint32 color01 = 0x888888;
const Uint32 color10 = 0xFFBB88;
const Uint32 color20 = 0xCCFFAA;
Uint32 cursor_color = 0xFFFFFF;
void setcolor (Uint32 color) { cursor_color = color; }
SDL_Rect print (char *fmt, ...)
{
va_list args;
va_start(args, fmt);
char buf[8192];
int xmin = cursor[0], xmax = cursor[0];
int ymin = cursor[1], ymax = cursor[1];
vsnprintf(buf, sizeof(buf), fmt, args);
buf[8191] = 0;
char *ptr = buf;
while (*ptr) {
char *next = strchr(ptr, '\n');
if (next) *next = 0;
cursor[0] = drop_string(cursor[0], cursor[1], ptr, cursor_color);
xmin = min(xmin, cursor[0]);
xmax = max(xmax, cursor[0]);
ymin = min(ymin, cursor[1] - text_ascent);
ymax = max(ymax, cursor[1] + text_descent);
if (next) {
cursor[0] = cursor_base[0];
cursor[1] += text_height;
ptr = next+1;
} else break;
}
va_end(args);
return (SDL_Rect){xmax-xmin, ymax-ymin, xmin, ymin};
}
void dim_to_y (int y)
{
for (int i=0; i<min(y, window_surface->h); i++) {
Uint32 *ptr = display_ptr(0, i);
unsigned width = window_surface->w;
for (int x=0; x<width; x++) ptr[x] = (ptr[x] >> 2) & 0x3F3F3F;
}
}
float dim_y = 0.0;
float dim_y_target = 0.0;
void dim_background (void)
{
float rate = 0.13;
dim_y = dim_y * (1.0-rate) + rate*dim_y_target;
dim_to_y(floor(dim_y));
}
//typedef SDL_Surface *image_t;
char *asset (char *name)
{
static char buf[512];
snprintf(buf, sizeof(buf), "%s/share/tenes/%s", PREFIX, name);
buf[511] = 0;
return buf;
}
char *localasset (char *name)
{
static char buf[512];
snprintf(buf, sizeof(buf), "media/%s", name);
buf[511] = 0;
return buf;
}
char *nth_name (char *name, int n)
{
static char buf[512];
snprintf(buf, sizeof(buf), "%s_%i.png", name, n);
buf[511] = 0;
return buf;
}
image_t loaddecal (char *name)
{
image_t img = NULL;
SDL_Surface *ptr = IMG_Load(asset(name));
if (ptr == NULL) ptr = IMG_Load(localasset(name));
if (ptr) {
SDL_SetAlpha(ptr, SDL_SRCALPHA, 128);
/* Naughty! Twiddle image into ARGB format. */
ptr->format->Amask = 0xFF000000;
ptr->format->Ashift = 24;
img = malloc(sizeof(*img));
assert(img);
img->w = ptr->w;
img->h = ptr->h;
img->x_origin = 0;
img->y_origin = 0;
img->_sdl = ptr;
img->freeptr = NULL;
} else printf("Unable to load %s!\n", asset(name));
return img;
}
//image_t pad600 = NULL;
image_t drop_roundbutton = NULL;
image_t power_lo[2] = { NULL, NULL };
image_t reset_lo[2] = { NULL, NULL };
image_t cableseg = NULL;
image_t nesport[2] = { NULL, NULL };
image_t nesport_shadow = NULL;
image_t photo = NULL;
image_t photo_shadow_left = NULL;
image_t photo_shadow_bottom = NULL;
image_t square_shadow = NULL;
image_t camera[2] = { NULL, NULL };
image_t eject[2] = { NULL, NULL };
image_t sound[2] = { NULL, NULL };
image_t mute[2] = { NULL, NULL };
image_t gamepak_top = NULL;
image_t border_top_1 = NULL;
image_t scroll_outer_top = NULL;
image_t scroll_outer_bottom = NULL;
image_t scroll_inner_top = NULL;
image_t scroll_inner_bottom = NULL;
image_t device_bar_bg = NULL;
image_t rcol_top = NULL;
image_t rcol_middle = NULL;
image_t rcol_bottom = NULL;
image_t checkmark = NULL;
#define decal(name) if (!name) name = loaddecal(#name".png");
#define decals(name) \
{ for (int i=0; i<(sizeof(name)/sizeof(name[0])); i++) \
name[i] = loaddecal(nth_name(#name, i)); }
void load_menu_media (void)
{
// decal(pad600);
decals(power_lo);
decals(reset_lo);
decal(drop_roundbutton);
decal(cableseg);
decals(nesport);
decal(nesport_shadow);
decal(photo);
decal(photo_shadow_left);
decal(photo_shadow_bottom);
decal(square_shadow);
decals(camera);
decals(eject);
decals(sound);
decals(mute);
decal(gamepak_top);
decal(border_top_1);
decal(scroll_outer_top);
decal(scroll_outer_bottom);
decal(scroll_inner_top);
decal(scroll_inner_bottom);
decal(device_bar_bg);
decal(rcol_top);
decal(rcol_middle);
decal(rcol_bottom);
decal(checkmark);
}
const int ALIGN_MAX = 0;
const int ALIGN_MAX_SHIFT = 1;
const int ALIGN_AXIS = 2;
alignmode left = { 0, 0, 0};
alignmode right = { -1, 0, 0};
alignmode center = { -1, 1, 0};
alignmode top = { 0, 0, 0};
alignmode bottom = { -1, 0, 0};
alignmode baseline = { 0, 0,-1};
SDL_Rect drawimage (image_t image, int x, int y,
alignmode align_x,
alignmode align_y)
{
if (image) {
SDL_Rect r = { x + align_x[ALIGN_MAX]*(image->w >> align_x[ALIGN_MAX_SHIFT])
+ align_x[ALIGN_AXIS]*image->x_origin,
y + align_y[ALIGN_MAX]*(image->h >> align_y[ALIGN_MAX_SHIFT])
+ align_y[ALIGN_AXIS]*image->y_origin,
0, 0 };
SDL_BlitSurface(image->_sdl, NULL, window_surface, &r);
return r;
}
return (SDL_Rect){0, 0, 0, 0};
}
int mouseover (struct inputctx *input, SDL_Rect rect) {
return ((input->mx >= rect.x) &&
(input->my >= rect.y) &&
(input->mx < rect.x+rect.w) &&
(input->my < rect.y+rect.h));
}
SDL_Rect grow_rect (int n, SDL_Rect rect)
{
return (SDL_Rect){rect.x - n, rect.y - n, rect.w + 2*n, rect.h + 2*n};
}
float lerpf (float x, float arg, float y)
{
return x*arg + y*(1.0-arg);
}
/* UI Hint */
char current_hint[2048] = "";
image_t current_hint_image = NULL;
const int HINT_HEIGHT = 24;
void display_hint (const char *text)
{
if (!text)
{
current_hint[0] = 0;
// also free image
return;
}
if (!strncmp(text, current_hint, sizeof(current_hint)-1))
{
// Image is unchanged
}
else
{
strncpy(current_hint, text, sizeof(current_hint));
current_hint[sizeof(current_hint)-1] = 0;
//printf("Set hint to '%s'\n", text);
image_free(current_hint_image);
current_hint_image = sans_label(0xFFFFFF, HINT_HEIGHT, text);
}
drawimage(current_hint_image, window_surface->w/2, window_surface->h - 10, center, baseline);
}
/* Floaty Buttons */
struct floatybutt {
float offset;
int hover;
};
const float floaty_rate = 0.25;
const float floaty_height = 10;
const float floaty_linear = 0.1; // Hmm. Don't like this.
int run_floatybutt (
struct inputctx *input, struct floatybutt *this,
image_t faces[2], image_t shadow, int x, int y,
const char *hint)
{
this->hover = mouseover(input, drawimage(shadow, x, y, left, bottom));
// if (this->hover && (input->buttons & 1)) this->offset = lerpf(0, floaty_rate, this->offset);
// else this->offset = lerpf(floaty_height, floaty_rate, this->offset);
if (this->hover && (input->buttons & 1)) this->offset = approach(0, floaty_linear, floaty_rate, this->offset);
else this->offset = approach(floaty_height, floaty_linear, floaty_rate, this->offset);
drawimage(faces[this->hover], x+this->offset, y-this->offset, left, bottom);
if (hint && this->hover)
{
display_hint(hint);
}
return (this->hover && (input->released & 1));
}
float normsq (float x, float y)
{
return x*x + y*y;
}
void draw_cable (float px, float py, float destx, float desty)
{
float dx=0, dy=8.0;
int segments=0;
while ((segments < 1000) && (normsq(px-destx, py-desty) > 1.0)) {
drawimage(cableseg, px, py, center, center);
float vx = destx - px, vy = desty - py;
float rate = 1.0 / sqrt(normsq(vx, vy));
dx += vx*rate;
dy += vy*rate;
//printf("%i: %5f,%4f %5f,%4f \n", segments, px, dx, py, dy);
float ns = normsq(dx,dy);
if (ns > 6.0) {
dx /= 2.0;
dy /= 2.0;
}
dy += 0.5;
px += dx;
py += dy;
segments++;
}
}
void age_pixels (Uint32 *ptr, size_t n)
{
for (int i=0; i<n; i++) {
Uint32 px = ptr[i];
px &= 0xFCFCFC;
int level = ((px + (px >> 8) + (px >> 16)) >> 2) & 0xFF;
level += rand()&31;
level += 31;
ptr[i] = rgbi_clamp(level+30, level+30, level);
}
swizzle_pixels(ptr,n);
}
/*** Menu dispatch ***/
void run_menu (struct inputctx *input)
{
cursor_base[0] = 10;
cursor_base[1] = 40;
setcursor(0,0);
setcolor(color00);
menu(input);
}
void open_menu (void)
{
load_menu_media();
menu = run_main_menu;
//menu = run_input_menu;
SDL_ShowCursor(SDL_ENABLE);
}
void close_menu (void)
{
menu = 0;
SDL_ShowCursor(SDL_DISABLE);
dim_y_target = 0;
}
int menu_process_key_event (SDL_KeyboardEvent *key)
{
if (key->type == SDL_KEYDOWN) return 0;
if (key->keysym.sym == SDLK_ESCAPE) {
close_menu();
return 1;
}
return 0;
}
/*** Main menu screen ***/
char last_state_filename[512] = "";
time_t last_state_mtime = 0;
byte state_screencap[2][SCREEN_HEIGHT][SCREEN_WIDTH];
Uint32 rgb_screencap[SCREEN_HEIGHT][SCREEN_WIDTH];
Uint32 aged_screencap[SCREEN_HEIGHT][SCREEN_WIDTH];
int have_screencap = 0;
void update_state_image (void)
{
const char *filename = state_filename(&nes.rom, 1);
time_t mtime = file_write_date(filename);
char buf[512];
if (!strcmp(filename, last_state_filename) &&
(mtime == last_state_mtime)) return;
last_state_mtime = mtime;
strncpy(last_state_filename, filename, sizeof(last_state_filename)-1);
last_state_filename[sizeof(last_state_filename)-1] = 0;
snprintf(buf, sizeof(buf), "%s.screen", filename);
have_screencap = load_binary_data(buf, state_screencap, sizeof(state_screencap));
if (have_screencap) {
for (int i=0; i<SCREEN_HEIGHT; i++) {
emit_unscaled(&rgb_screencap[i][0],
&state_screencap[0][i][0],
&state_screencap[1][i][0],
SCREEN_WIDTH) ;
}
memcpy(aged_screencap, rgb_screencap, sizeof(rgb_screencap));
age_pixels(&aged_screencap[0][0], SCREEN_WIDTH*SCREEN_HEIGHT);
}
printf("Updated screencap %s\n", buf);
}
static Uint32 getbyte (Uint32 x, unsigned shift)
{
return (x >> shift) & 0xFF;
}
static Uint32 blendbyte (Uint32 x, Uint32 y, int fade, int notfade)
{
return (x * fade + y * notfade) >> 8;
}
static Uint32 blend (Uint32 x, Uint32 y, int fade)
{
int notfade = 256 - fade;
return blendbyte(getbyte(x,0), getbyte(y,0), fade, notfade)
| blendbyte(getbyte(x,8), getbyte(y,8), fade, notfade) << 8
| blendbyte(getbyte(x,16), getbyte(y,16), fade, notfade) << 16
| blendbyte(getbyte(x,24), getbyte(y,24), fade, notfade) << 24;
}
void render_restorestate (struct inputctx *input)
{
const int photo_border = 10;
const int overscan = 8;
static int fade = 255;
int cx = vid_width - 255 - 27;
int cy = 10 + photo->h + 10;
const float button_height = 7;
static float offset = 7; /* const is dumb */
const int actual_width = SCREEN_WIDTH - 2*overscan;
const int actual_height = SCREEN_HEIGHT-2*overscan;
int px = cx + 3;
int py = cy - 3;
int vx = px + roundf(offset) + photo_border;
int vy = py - roundf(offset) - photo->h + photo_border + 1;
if (probe_file(state_filename(&nes.rom, 1))) {
update_state_image();
drawimage(photo_shadow_left, cx, cy, left, bottom);
drawimage(photo_shadow_bottom, cx+photo_shadow_left->w, cy, left, bottom);
int hover = mouseover(input, grow_rect(1.4*ceilf(button_height-offset), drawimage(photo, px + roundf(offset), py-roundf(offset), left, bottom)));
float target = (hover && (input->buttons & 1))? 0.0 : button_height;
float d_offset = 0.05 + 0.5 * fabs(target - offset);
if (hover && (input->buttons & 1)) d_offset *= -1.0;
offset = min(button_height, max(0, offset + d_offset));
int d_fade = hover? -10: 10;
fade = clamp_byte(fade + d_fade);
if (have_screencap)
{
for (int i=0; i<actual_height; i++)
{
Uint32 *ptr = display_ptr(vx, vy+i);
Uint32 *aged = &aged_screencap[i+overscan][overscan];
Uint32 *rgb = &rgb_screencap[i+overscan][overscan];
if (fade==255)
{
memcpy(ptr, aged, 4*actual_width);
}
else
{
for (int x = 0; x<actual_width; x++)
{
ptr[x] = blend(aged[x], rgb[x], fade);
}
}
}
}
vy += actual_height;
vy += 8;
char buf[64];
struct tm *ts;
ts = localtime(&last_state_mtime);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S", ts);
// It used to change the caption on the photo to "Restore
//State", but it's redundant now that I've put the hint line
//at the bottom of the screen. if (hover) strcpy(buf,
//"Restore State");
outlined_string(vx + actual_width/2 - strlen(buf)*text_width/2,
vy + text_ascent, buf, 0x79786d /*0x65645b*/, 0xbab7a7);
vy += text_height + 2;
dim_y_target = max(dim_y_target, vy);
if (hover)
{
display_hint("Restore Saved State");
if (input->released & 1)
{
if (!restore_state_from_disk(NULL))
{
hard_reset_nes(&nes);
}
}
}
} else {
vy += actual_height + 8 + text_height + 2;
}
}
void run_nsf_ui (struct inputctx *input, int bx, int by)
{
struct nsf_header *h = nes.rom.nsf_header;
cursor_base[0] = bx;
cursor_base[1] = by + 20;
setcursor(0,0);
setcolor(color00);
print(" Name: ");
setcolor(color10);
print("\"%s\"\n", h->name);
setcolor(color00);
print(" Artist: ");
setcolor(color10);
print("\"%s\"\n", h->artist);
setcolor(color00);
print(" Copyright: ");
setcolor(color10);
print("\"%s\"\n", h->copyright);
setcolor(color00);
print(" Song: ");
setcolor(color10);
print("%i of %i\n", nes.nsf_current_song + 1, h->total_songs);
print("\n(Use arrow keys to change songs)\n");
}
void run_main_menu (struct inputctx *input)
{
//drawimage(pad600, 0, 0, left, top);
//drawimage(mascot, window_surface->w, window_surface->h - 80, right, bottom);
if (input->released & SDL_BUTTON(3)) close_menu();
cursor_base[0] = 92;
cursor_base[1] = 270;
setcursor(0,0);
setcolor(color00);
print("\n");
setcolor(color10);
print("Playing: %s\n", nes.rom.title);
print("PRG: %i KB\n", nes.rom.prg_size/1024);
print("CHR: %i KB\n", nes.rom.chr_size/1024);
print("Mapper %i: %s\n", nes.rom.mapper, nes.rom.mapper_info? nes.rom.mapper_info->name : "Unknown");
if (mapper->describe)
{
print("%s\n", mapper->describe());
}
// if (sound_globalenabled) print("Sound %s\n", sound_muted? "muted" : "enabled");
if (cfg_disable_joysticks) print("Joysticks disabled.\n");
if (cfg_disable_keyboard) print("Keyboard input disabled.\n");
if (movie_input_filename && movie_input) print("Playing movie from %s\n", movie_input_filename);
if (movie_output_filename && movie_output) print("Recording movie to %s\n", movie_output_filename);
if (cfg_mount_fs) print("FUSE filesystem mounted at \"%s\"\n", cfg_mountpoint);
/*
setcolor(color00);
print("\nInput Devices:\n");
setcolor(color20);
print(" Bitchin' Keyboard %c\n", 1);
for (int i=0; i<numsticks; i++) {
print(" %s w/ %i bodacious buttons and %i awesome axes\n",
SDL_JoystickName(i),
SDL_JoystickNumButtons(joystick[i]),
SDL_JoystickNumAxes(joystick[i]));
}
*/
render_restorestate(input);
int icon_pad = 12;
int icon_x = 0;
int icon_y = 85; //window_surface->h - icon_pad;
float vert_x = icon_x;
float vert_y = icon_y;
static struct floatybutt power = {0, 0};
if (run_floatybutt(input, &power, power_lo, drop_roundbutton, icon_x, icon_y,
"Exit the program"))
{
running = 0;
}
icon_x += icon_pad + power_lo[0]->w;
vert_y += icon_pad + power_lo[0]->h;
static struct floatybutt reset = {0, 0};
if (run_floatybutt(input, &reset, reset_lo, drop_roundbutton, icon_x, icon_y,
"Reset the NES"))
{
soft_reset_nes(&nes);
}
icon_x += icon_pad + reset_lo[0]->w;
icon_x += icon_pad;
static struct floatybutt eject_button = {0, 0};
if (run_floatybutt(input, &eject_button, eject, square_shadow, vert_x, vert_y,
"Load a new rom image"))
{
menu = run_game_browser;
}
vert_y += icon_pad + eject[0]->h;
static struct floatybutt camera_button = {0, 0};
if (run_floatybutt(input, &camera_button, camera, square_shadow, vert_x, vert_y,
"Save Current State"))
{
save_state_to_disk(NULL);
}
vert_y += icon_pad + camera[0]->h;
static struct floatybutt mute_button = {0, 0};
if (sound_globalenabled) {
if (run_floatybutt(input, &mute_button, sound_muted? mute : sound,
square_shadow,
vert_x, vert_y,
sound_muted? "Unmute the audio" : "Mute the audio"
))
{
sound_muted = !sound_muted;
}
vert_y += icon_pad + camera[0]->h;
}
//drawimage(keyboard_140, input->mx, input->my, right, bottom);
int port_y = window_surface->h - icon_pad - 6;
static struct floatybutt port_button = {0,0};
if (run_floatybutt(input, &port_button, nesport, nesport_shadow,
window_surface->w - 29 - nesport[0]->w, port_y,
"Configure Controllers"))
{
menu = run_input_menu;
}
//drawimage(nesport, window_surface->w - 8, port_y, right, bottom);
//drawimage(nesport, window_surface->w - 10 - nesport->w, port_y, right, bottom);
// Clever stunt:
/*
static float destx = 500, desty= 65;
if (input->pressed == 2) {
destx = input->mx;
desty = input->my;
}
draw_cable(input->mx, input->my, destx, desty);
drawimage(keyboard_140, destx, desty, center, center);
*/
//dim_y_target = max(dim_y_target, cursor[1] + text_height);
if (nes.machine_type == NSF_PLAYER) run_nsf_ui(input, 100, 92);
dim_y_target = vid_height;
}
/*** Game Browser ***/
struct gbent
{
char *title;
char *name;
char *path;
image_t background; /* Not freed! */
image_t label;
image_t highlight;
float xoffset, xtarget;
unsigned y;
int hover;
};
struct gbent **browser_ents;
unsigned browser_num_ents = 0;
unsigned browser_max_ents = 0;
char browser_cwd[1024] = "";
image_t browser_dir_label = NULL;
image_t browser_dir_shadow = NULL;
void free_gbent (struct gbent *ent) {
assert(ent != NULL);
free(ent->name);
free(ent->path);
free(ent->title);
if (ent->label) image_free(ent->label);
if (ent->highlight) image_free(ent->highlight);
free(ent);
}
struct gbent *browser_dirs[1024];
unsigned browser_num_dirs = 0;
int game_filename_p (char *filename)
{
int len = strlen(filename);
if (len < 5) return 0;
return !strcasecmp(filename+len-4, ".nes") ||
!strcasecmp(filename+len-4, ".nsf");
}
char *filename_to_title (char *filename)
{
char *out = strdup(filename);
char *tmp = strrchr(out, '.');
if (tmp) *tmp = 0;
return out;
}
int browser_ent_title_relation (struct gbent **a, struct gbent **b)
{
return strcasecmp((*a)->title, (*b)->title);
}
int trailing_slash_p (char *str)
{
return strlen(str) && (str[strlen(str)-1] == '/');
}
const char *dirsep (char *path)
{
return trailing_slash_p(path)? "" : "/";
}
struct breadcrumb {
image_t label, highlight, shadow;
char name[PATH_MAX];
char path[1024];
struct breadcrumb *next;
int use_highlight;
};
struct breadcrumb *browser_breadcrumbs;
void free_breadcrumb (struct breadcrumb *b)
{
if (b->label) image_free(b->label);
if (b->highlight) image_free(b->highlight);
if (b->next) free_breadcrumb(b->next);
free(b);
}
struct breadcrumb *build_breadcrumbs (char *path)
{
struct breadcrumb *root = calloc(1, sizeof(*root));
assert(root != NULL);
strcpy(root->name, "Viewing /");
strcpy(root->path, "/");
char *ptr = path + strspn(path, "/");
struct breadcrumb *tail = root;
while (*ptr) {
struct breadcrumb *next = calloc(1, sizeof(*next));
assert(next != NULL);
char *sep = strchr(ptr, '/');
if (!sep) sep = ptr + strlen(ptr);
if (*sep == '/') sep++;
memcpy(next->name, ptr, min(sizeof(next->name)-1, sep-ptr));
memcpy(next->path, path, min(sizeof(next->path)-1, sep-path));
ptr = sep + strspn(sep, "/");
tail->next = next;
tail = next;
}