forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoteField.cpp
3475 lines (3312 loc) · 97.8 KB
/
NoteField.cpp
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 "global.h"
#include "ActorUtil.h"
#include "BackgroundUtil.h"
#include "EnumHelper.h"
#include "Game.h"
#include "GameManager.h"
#include "GameState.h"
#include "LuaBinding.h"
#include "NoteField.h"
#include "NoteSkinManager.h"
#include "RageDisplay.h"
#include "RageLog.h"
#include "RageMath.h"
#include "RageUtil.h"
#include "ScreenDimensions.h"
#include "Song.h"
#include "Sprite.h"
#include "Steps.h"
#include "Style.h"
#include "ThemeManager.h"
using std::max;
using std::string;
using std::unordered_set;
using std::vector;
static const double note_size= 64.0;
static const double z_bias_per_thing= .01;
static const double lift_fade_dist_recip= 1.0 / 64.0;
static const int field_layer_column_index= -1;
static const int beat_bars_column_index= -2;
static const int holds_child_index= -1;
static const int lifts_child_index= -2;
static const int taps_child_index= -3;
static const int beat_bars_child_index= -4;
static const int selection_child_index= -5;
static const int draw_order_types= 4;
static const int non_board_draw_order= 0;
static const int beat_bars_draw_order= 0;
static const int selection_draw_order= 0;
static const int holds_draw_order= 200;
static const int lifts_draw_order= 200;
static const int taps_draw_order= 300;
static ThemeMetric<Rage::Color> AREA_HIGHLIGHT_COLOR("NoteField", "AreaHighlightColor");
static const char* FieldLayerFadeTypeNames[]= {
"Receptor",
"Note",
"Explosion",
"None",
};
XToString(FieldLayerFadeType);
LuaXType(FieldLayerFadeType);
static const char* FieldLayerTransformTypeNames[]= {
"Full",
"PosOnly",
"None",
};
XToString(FieldLayerTransformType);
LuaXType(FieldLayerTransformType);
REGISTER_ACTOR_CLASS(NoteFieldColumn);
#define HOLD_COUNTS_AS_ACTIVE(tn) (tn.HoldResult.bActive && tn.HoldResult.fLife > 0.0f)
static void apply_render_info_to_layer(Actor* layer,
FieldLayerRenderInfo& info, Rage::transform const& trans,
double receptor_alpha, double receptor_glow,
double explosion_alpha, double explosion_glow, double beat, double second,
ModifiableValue& note_alpha, ModifiableValue& note_glow)
{
switch(info.fade_type)
{
case FLFT_Receptor:
layer->SetDiffuseAlpha(receptor_alpha);
layer->SetGlowAlpha(receptor_glow);
break;
case FLFT_Explosion:
layer->SetDiffuseAlpha(explosion_alpha);
layer->SetGlowAlpha(explosion_glow);
break;
case FLFT_Note:
{
mod_val_inputs input(beat, second);
double alpha= note_alpha.evaluate(input);
double glow= note_glow.evaluate(input);
layer->SetDiffuseAlpha(alpha);
layer->SetGlowAlpha(glow);
}
break;
default:
break;
}
switch(info.transform_type)
{
case FLTT_Full:
layer->set_transform(trans);
break;
case FLTT_PosOnly:
layer->set_transform_pos(trans);
break;
default:
break;
}
}
bool operator<(NoteField::field_draw_entry const& rhs, NoteField::field_draw_entry const& lhs)
{
return rhs.draw_order < lhs.draw_order;
}
NoteFieldColumn::NoteFieldColumn()
:m_show_unjudgable_notes(true),
m_speed_segments_enabled(true), m_scroll_segments_enabled(true),
m_holds_skewed_by_mods(true),
m_twirl_holds(true), m_use_moddable_hold_normal(false),
m_time_offset(&m_mod_manager, 0.0),
m_quantization_multiplier(&m_mod_manager, 1.0),
m_quantization_offset(&m_mod_manager, 0.0),
m_speed_mod(&m_mod_manager, 0.0),
m_lift_pretrail_length(&m_mod_manager, 0.25),
m_y_offset_vec_mod(&m_mod_manager, 0.0, 1.0, 0.0),
m_reverse_offset_pixels(&m_mod_manager, 240.0 - note_size),
m_reverse_scale(&m_mod_manager, 1.0),
m_center_percent(&m_mod_manager, 0.0),
m_note_mod(&m_mod_manager), m_column_mod(&m_mod_manager),
m_hold_normal_mod(&m_mod_manager, 0.0),
m_note_alpha(&m_mod_manager, 1.0), m_note_glow(&m_mod_manager, 0.0),
m_receptor_alpha(&m_mod_manager, 1.0), m_receptor_glow(&m_mod_manager, 0.0),
m_explosion_alpha(&m_mod_manager, 1.0), m_explosion_glow(&m_mod_manager, 0.0),
m_selection_start(-1.0), m_selection_end(-1.0),
m_curr_beat(0.0f), m_curr_second(0.0), m_prev_curr_second(-1000.0),
m_pixels_visible_before_beat(128.0f),
m_pixels_visible_after_beat(1024.0f),
m_upcoming_time(2.0),
m_playerize_mode(NPM_Off),
m_newskin(nullptr), m_player_colors(nullptr), m_field(nullptr),
m_defective_mods(nullptr), m_in_defective_mode(false),
m_note_data(nullptr),
m_timing_data(nullptr),
m_gameplay_zoom(1.0),
reverse_scale_sign(1.0),
pressed(false)
{
DeleteChildrenWhenDone(true);
m_area_highlight.SetEffectGlowShift(2, Rage::Color(0.f, 0.f, 0.f, 0.f),
Rage::Color(.75f, .75f, .75f, 1.0f));
}
NoteFieldColumn::~NoteFieldColumn()
{}
void NoteFieldColumn::AddChild(Actor* act)
{
// The actors have to be wrapped inside of frames so that mod transforms
// can be applied without stomping the rotation the noteskin supplies.
ActorFrame* frame= new ActorFrame;
frame->WrapAroundChild(act);
ActorFrame::AddChild(frame);
m_layer_render_info.push_back({FLFT_None, FLTT_Full});
act->PlayCommand("On");
m_field->add_draw_entry(static_cast<int>(m_column), GetNumChildren()-1, act->GetDrawOrder());
if(act->HasCommand("WidthSet"))
{
Message width_msg("WidthSet");
lua_State* L= LUA->Get();
PushSelf(L);
width_msg.SetParamFromStack(L, "column");
LUA->Release(L);
width_msg.SetParam("column_id", get_mod_col());
width_msg.SetParam("width", m_newskin->get_width());
width_msg.SetParam("padding", m_newskin->get_padding());
act->HandleMessage(width_msg);
}
}
void NoteFieldColumn::RemoveChild(Actor* act)
{
size_t index= FindChildID(act);
if(index < m_SubActors.size())
{
m_field->remove_draw_entry(m_column, index);
m_layer_render_info.erase(m_layer_render_info.begin() + index);
}
ActorFrame::RemoveChild(act);
}
void NoteFieldColumn::ChildChangedDrawOrder(Actor* child)
{
size_t index= FindChildID(child);
if(index < m_SubActors.size())
{
m_field->change_draw_entry(static_cast<int>(m_column),
static_cast<int>(index), child->GetDrawOrder());
}
ActorFrame::ChildChangedDrawOrder(child);
}
void NoteFieldColumn::add_children_from_layers(size_t column,
vector<NoteSkinLayer>& layers)
{
for(size_t i= 0; i < layers.size(); ++i)
{
AddChild(layers[i].m_actors[column]);
}
}
void NoteFieldColumn::set_note_data(size_t column, const NoteData* note_data,
const TimingData* timing_data)
{
m_note_data= note_data;
m_timing_data= timing_data;
note_row_closest_to_current_time= -1;
for(auto&& moddable : {&m_time_offset, &m_quantization_multiplier,
&m_quantization_offset, &m_speed_mod, &m_lift_pretrail_length,
&m_reverse_offset_pixels, &m_reverse_scale,
&m_center_percent, &m_note_alpha, &m_note_glow, &m_receptor_alpha,
&m_receptor_glow, &m_explosion_alpha, &m_explosion_glow})
{
moddable->set_timing(timing_data);
moddable->set_column(column);
}
for(auto&& moddable : {&m_note_mod, &m_column_mod})
{
moddable->set_timing(timing_data);
moddable->set_column(column);
}
for(auto&& moddable : {&m_y_offset_vec_mod, &m_hold_normal_mod})
{
moddable->set_timing(timing_data);
moddable->set_column(column);
}
}
void NoteFieldColumn::set_column_info(NoteField* field, size_t column,
NoteSkinColumn* newskin, ArrowDefects* defects,
NoteSkinData& skin_data, std::vector<Rage::Color>* player_colors,
const NoteData* note_data, const TimingData* timing_data, double x)
{
m_field= field;
m_column= column;
m_newskin= newskin;
m_defective_mods= defects;
m_newskin->set_timing_source(&m_timing_source);
set_note_data(column, note_data, timing_data);
m_column_mod.pos_mod.x_mod.add_simple_mod("base_value", "number", x);
m_use_game_music_beat= true;
m_player_colors= player_colors;
m_mod_manager.column= column;
std::vector<Actor*> layers;
ActorUtil::MakeActorSet(THEME->GetPathG("NoteColumn", "layers", true), layers);
for(auto&& act : layers)
{
AddChild(act);
}
add_children_from_layers(column, skin_data.m_layers);
}
void NoteFieldColumn::take_over_mods(NoteFieldColumn& other)
{
#define CPY(name) name= other.name;
CPY(m_use_game_music_beat);
CPY(m_show_unjudgable_notes);
CPY(m_speed_segments_enabled);
CPY(m_scroll_segments_enabled);
CPY(m_holds_skewed_by_mods);
CPY(m_twirl_holds);
CPY(m_use_moddable_hold_normal);
CPY(m_pixels_visible_before_beat);
CPY(m_pixels_visible_after_beat);
CPY(m_upcoming_time);
#define CPY_MODS(name) name.take_over_mods(other.name);
CPY_MODS(m_time_offset);
CPY_MODS(m_quantization_multiplier);
CPY_MODS(m_quantization_offset);
CPY_MODS(m_speed_mod);
CPY_MODS(m_lift_pretrail_length);
CPY_MODS(m_y_offset_vec_mod);
CPY_MODS(m_reverse_offset_pixels);
CPY_MODS(m_reverse_scale);
CPY_MODS(m_center_percent);
CPY_MODS(m_note_mod);
CPY_MODS(m_column_mod);
CPY_MODS(m_hold_normal_mod);
CPY_MODS(m_note_alpha);
CPY_MODS(m_note_glow);
CPY_MODS(m_receptor_alpha);
CPY_MODS(m_receptor_glow);
CPY_MODS(m_explosion_alpha);
CPY_MODS(m_explosion_glow);
#undef CPY_MODS
#undef CPY
}
void NoteFieldColumn::set_defective_mode(bool mode)
{
if(!m_defective_mods->safe())
{
m_in_defective_mode= false;
return;
}
m_in_defective_mode= mode;
if(m_in_defective_mode)
{
SetXY(0.0, 0.0);
SetZ(0.0);
SetZoom(1.0);
SetRotationX(0.0);
SetRotationY(0.0);
SetRotationZ(0.0);
}
}
bool NoteFieldColumn::get_defective_mode()
{
return m_in_defective_mode;
}
void NoteFieldColumn::set_speed(float time_spacing,
float max_scroll_bpm, float scroll_speed, float scroll_bpm, float read_bpm,
float music_rate)
{
if(time_spacing == 0.f)
{
if(max_scroll_bpm != 0.f)
{
m_speed_mod.add_simple_mod("speed", "dist_beat",
max_scroll_bpm / read_bpm / music_rate);
}
else
{
m_speed_mod.add_simple_mod("speed", "dist_beat",
scroll_speed);
}
}
else
{
m_speed_mod.add_simple_mod("speed", "dist_second",
scroll_bpm / 60.f / music_rate);
}
}
double NoteFieldColumn::get_beat_from_second(double second)
{
return m_timing_data->GetBeatFromElapsedTime(static_cast<float>(second));
}
double NoteFieldColumn::get_second_from_beat(double beat)
{
return m_timing_data->GetElapsedTimeFromBeat(static_cast<float>(beat));
}
void NoteFieldColumn::set_displayed_time(double beat, double second)
{
m_timing_source.beat_delta= beat - m_curr_beat;
m_timing_source.second_delta= second - m_curr_second;
m_timing_source.curr_second= second;
m_newskin->update_taps();
m_curr_beat= beat;
m_curr_second= second;
m_mod_manager.update(beat, second);
build_render_lists();
}
void NoteFieldColumn::update_displayed_time(double beat, double second)
{
if(m_use_game_music_beat)
{
if(!m_time_offset.empty())
{
mod_val_inputs input(beat, second);
double offset= m_time_offset.evaluate(input);
second+= offset;
beat= m_timing_data->GetBeatFromElapsedTime(static_cast<float>(second));
}
set_displayed_time(beat, second);
}
}
void NoteFieldColumn::set_displayed_beat(double beat)
{
set_displayed_time(beat, m_timing_data->GetElapsedTimeFromBeat(static_cast<float>(beat)));
}
void NoteFieldColumn::set_displayed_second(double second)
{
set_displayed_time(m_timing_data->GetBeatFromElapsedTime(static_cast<float>(second)), second);
}
double NoteFieldColumn::calc_y_offset(mod_val_inputs& input)
{
if(!m_in_defective_mode)
{
input.music_beat= m_curr_displayed_beat;
double original_beat= input.eval_beat;
if(m_scroll_segments_enabled)
{
if(original_beat == m_curr_beat)
{
input.change_eval_beat(m_curr_displayed_beat);
}
else
{
input.change_eval_beat(m_timing_data->GetDisplayedBeat(static_cast<float>(input.eval_beat)));
}
}
double ret= note_size * m_speed_mod.evaluate(input);
if(m_speed_segments_enabled)
{
ret*= m_timing_data->GetDisplayedSpeedPercent(static_cast<float>(m_curr_beat), static_cast<float>(m_curr_second));
}
input.music_beat= m_curr_beat;
input.change_eval_beat(original_beat);
return ret;
}
else
{
return m_defective_mods->get_y_offset(input.eval_beat, input.eval_second, m_column);
}
return 0.0;
}
double NoteFieldColumn::calc_y_offset(double beat, double second)
{
mod_val_inputs input(beat, second, m_curr_beat, m_curr_second);
return calc_y_offset(input);
}
void NoteFieldColumn::calc_transform(mod_val_inputs& input,
Rage::transform& trans)
{
if(!m_in_defective_mode)
{
m_note_mod.evaluate(input, trans);
}
else
{
m_defective_mods->get_transform(input.eval_beat, input.y_offset,
apply_reverse_shift(input.y_offset), m_column, trans);
}
}
void NoteFieldColumn::calc_transform_with_glow_alpha(mod_val_inputs& input,
Rage::transform& trans)
{
if(!m_in_defective_mode)
{
m_note_mod.evaluate(input, trans);
trans.alpha= m_note_alpha.evaluate(input);
trans.glow= m_note_glow.evaluate(input);
}
else
{
m_defective_mods->get_transform_with_glow_alpha(input.eval_beat,
input.y_offset, apply_reverse_shift(input.y_offset), m_column, trans);
}
}
void NoteFieldColumn::calc_pos_only(mod_val_inputs& input, Rage::Vector3& out)
{
if(!m_in_defective_mode)
{
m_note_mod.pos_mod.evaluate(input, out);
}
else
{
// TODO: Care. calc_pos_only is only used for positioning the selection
// area highlight. -Kyz
}
}
void NoteFieldColumn::hold_render_transform(mod_val_inputs& input,
Rage::transform& trans, bool do_rot)
{
if(!m_in_defective_mode)
{
m_note_mod.hold_render_eval(input, trans, do_rot);
trans.alpha= m_note_alpha.evaluate(input);
trans.glow= m_note_glow.evaluate(input);
}
else
{
m_defective_mods->hold_render_transform(input.y_offset, m_column, trans);
}
}
void NoteFieldColumn::calc_reverse_shift()
{
double reverse_offset= 0.0;
double center_percent= 0.0;
if(!m_in_defective_mode)
{
mod_val_inputs input(m_curr_beat, m_curr_second);
reverse_offset= m_reverse_offset_pixels.evaluate(input) * m_gameplay_zoom;
center_percent= m_center_percent.evaluate(input);
reverse_scale= m_reverse_scale.evaluate(input);
}
else
{
reverse_offset= m_defective_mods->get_reverse_offset();
center_percent= m_defective_mods->get_center_percent();
reverse_scale= m_defective_mods->get_reverse_scale(m_column);
}
reverse_shift= Rage::scale(reverse_scale, 1.0, -1.0, -reverse_offset, reverse_offset);
reverse_shift= Rage::scale(center_percent, 0.0, 1.0, reverse_shift, 0.0);
double old_scale_sign= reverse_scale_sign;
if(reverse_scale < 0.0)
{
m_status.in_reverse= true;
reverse_scale_sign= -1.0;
}
else
{
m_status.in_reverse= false;
reverse_scale_sign= 1.0;
}
if(old_scale_sign != reverse_scale_sign)
{
Message revmsg("ReverseChanged");
revmsg.SetParam("sign", reverse_scale_sign);
pass_message_to_heads(revmsg);
if(m_column == 0 && m_field != nullptr)
{
m_field->HandleMessage(revmsg);
}
}
static double const min_visible_scale= 0.1;
double visible_scale= fabs(reverse_scale);
if(visible_scale < min_visible_scale)
{
visible_scale= min_visible_scale;
}
first_y_offset_visible= -m_pixels_visible_before_beat / visible_scale;
last_y_offset_visible= m_pixels_visible_after_beat / visible_scale;
}
double NoteFieldColumn::apply_reverse_shift(double y_offset)
{
return (y_offset * reverse_scale) + reverse_shift;
}
void NoteFieldColumn::apply_yoffset_to_pos(mod_val_inputs& input,
Rage::Vector3& pos)
{
Rage::Vector3 yoff;
m_y_offset_vec_mod.evaluate(input, yoff);
for(int i= 0; i < 3; ++i)
{
pos[i]+= static_cast<float>(
(reverse_scale * (yoff[i] * input.y_offset)) +
(reverse_shift * yoff[i]));
}
}
void NoteFieldColumn::apply_column_mods_to_actor(Actor* act)
{
mod_val_inputs input(m_curr_beat, m_curr_second);
Rage::transform trans;
m_column_mod.evaluate(input, trans);
act->set_transform(trans);
}
void NoteFieldColumn::apply_note_mods_to_actor(Actor* act, double beat,
double second, double y_offset, bool use_alpha, bool use_glow)
{
mod_val_inputs mod_input(beat, second, m_curr_beat, m_curr_beat, y_offset);
if(use_alpha)
{
act->SetDiffuseAlpha(static_cast<float>(m_note_alpha.evaluate(mod_input)));
}
if(use_glow)
{
act->SetGlow(Rage::Color(1, 1, 1, static_cast<float>(m_note_glow.evaluate(mod_input))));
}
Rage::transform trans;
calc_transform(mod_input, trans);
apply_yoffset_to_pos(mod_input, trans.pos);
act->set_transform(trans);
}
float NoteFieldColumn::get_selection_glow()
{
if(m_field != nullptr)
{
return m_field->selection_glow;
}
return 0.f;
}
void NoteFieldColumn::UpdateInternal(float delta)
{
calc_reverse_shift();
if(m_selection_start != -1.0)
{
m_area_highlight.Update(delta);
}
ActorFrame::UpdateInternal(delta);
}
void NoteFieldColumn::set_gameplay_zoom(double zoom)
{
m_gameplay_zoom= 1.0 / zoom;
}
Rage::Color NoteFieldColumn::get_player_color(size_t pn)
{
if(m_player_colors->empty())
{
return Rage::Color(0, 0, 0, 0);
}
return (*m_player_colors)[pn % m_player_colors->size()];
}
struct strip_buffer
{
enum { size= 512 };
Rage::SpriteVertex* buf;
Rage::SpriteVertex* v;
// Hold rendering requires two passes, the normal pass and the glow pass.
// Recalculating all the vert positions for the second pass would be
// expensive, so the glow color for each vert is stored in glow_buf.
Rage::VColor* glow_buf;
Rage::VColor* glow_v;
strip_buffer()
{
buf= (Rage::SpriteVertex*) malloc(size * sizeof(Rage::SpriteVertex));
glow_buf= (Rage::VColor*) malloc(size * sizeof(Rage::VColor));
init();
}
~strip_buffer()
{
free(buf);
}
void init()
{
v= buf;
glow_v= glow_buf;
}
void rollback()
{
// The buffer is full and has just been drawn, and more verts need to be
// added to draw. Move the last three verts to the beginning of the
// buffer so that the vert calculating loop doesn't have to redo the work
// for them.
if(used() > 2)
{
buf[0]= v[-2];
buf[1]= v[-1];
v= buf + 2;
glow_buf[0]= glow_v[-2];
glow_buf[1]= glow_v[-1];
glow_v= glow_buf + 2;
}
}
void draw()
{
DISPLAY->DrawQuadStrip(buf, v-buf);
}
void swap_glow()
{
int verts_used= v - buf;
for(int i= 0; i < verts_used; ++i)
{
Rage::VColor temp= buf[i].c;
buf[i].c= glow_buf[i];
glow_buf[i]= temp;
}
}
int used() const { return v - buf; }
int avail() const { return size - used(); }
void add_vert(Rage::Vector3 const& pos, Rage::Color const& color, Rage::Color const& glow, Rage::Vector2 const& texcoord)
{
v->p= pos; v->c= color; v->t= texcoord;
v+= 1;
(*glow_v)= glow;
glow_v+= 1;
}
};
enum hold_tex_phase
{
HTP_Top,
HTP_Body,
HTP_Bottom,
HTP_Done
};
struct hold_texture_handler
{
// Things that would be const if the calculations didn't force them to be
// non-const.
double tex_top;
double tex_bottom;
double tex_rect_h;
double tex_body_height;
double tex_top_end;
double tex_body_end;
double tex_per_y;
double start_y;
double body_start_y;
double body_end_y;
double end_y;
// Things that will be changed/updated each time.
double prev_bodies_left;
int prev_phase;
bool started_bottom;
hold_texture_handler(double const note_size, double const head_y,
double const tail_y, double const tex_t, double const tex_b,
QuantizedHoldRenderData const& data)
{
double pix_h_recip= 1.0 / (data.part_lengths.head_pixs +
data.part_lengths.body_pixs + data.part_lengths.body_pixs +
data.part_lengths.tail_pixs);
double head_pct= data.part_lengths.head_pixs * pix_h_recip;
double body_pct= data.part_lengths.body_pixs * pix_h_recip;
double tail_pct= data.part_lengths.tail_pixs * pix_h_recip;
tex_top= tex_t;
tex_bottom= tex_b;
tex_rect_h= tex_bottom - tex_top;
tex_per_y= tex_rect_h * pix_h_recip;
double tex_top_height= tex_rect_h * head_pct;
tex_body_height= tex_rect_h * body_pct;
double tex_bottom_height= tex_rect_h * tail_pct;
tex_top_end= tex_top + tex_top_height;
tex_body_end= tex_bottom - tex_bottom_height;
start_y= head_y + (note_size * data.part_lengths.start_note_offset);
body_start_y= head_y;
end_y= tail_y + (note_size * data.part_lengths.end_note_offset);
body_end_y= end_y - data.part_lengths.tail_pixs;
// constants go above this line.
prev_bodies_left= 1.0;
prev_phase= HTP_Top;
started_bottom= false;
}
// curr_y will be modified on the transition to HTP_Bottom to make sure the
// entire bottom is drawn.
// The hold is drawn in several phases. Each phase must be drawn in full,
// so when transitioning from one phase to the next, two texture coords are
// calculated, one with the previous phase and one with the current. This
// compresses the seam between phases to zero, making it invisible.
// The texture coords are bottom aligned so that the end of the last body
// lines up with the start of the bottom cap.
int calc_tex_y(double& curr_y, vector<double>& ret_texc)
{
int phase= HTP_Top;
if(curr_y >= end_y)
{
curr_y= end_y;
ret_texc.push_back(tex_bottom);
phase= HTP_Done;
}
else if(curr_y >= body_end_y)
{
if(started_bottom)
{
phase= HTP_Bottom;
}
else
{
curr_y= body_end_y;
phase= HTP_Bottom;
started_bottom= true;
}
}
else if(curr_y >= body_start_y)
{
phase= HTP_Body;
}
if(phase != HTP_Done)
{
if(phase != prev_phase)
{
internal_calc_tex_y(prev_phase, curr_y, ret_texc);
}
internal_calc_tex_y(phase, curr_y, ret_texc);
prev_phase= phase;
}
return phase;
}
private:
void internal_calc_tex_y(int phase, double& curr_y, vector<double>& ret_texc)
{
switch(phase)
{
case HTP_Top:
ret_texc.push_back(tex_top + ((curr_y - start_y) * tex_per_y));
break;
case HTP_Body:
// In the body phase, the first half of the body section of the
// texture is repeated over the length of the hold.
{
double const tex_distance= (body_end_y - curr_y) * tex_per_y;
// bodies_left decreases as more of the hold is drawn.
double bodies_left= tex_distance / tex_body_height;
double const floor_left= floor(bodies_left);
bodies_left= (bodies_left - floor_left) + 1.0;
double curr_tex_y= tex_body_end - (bodies_left * tex_body_height);
if(bodies_left > prev_bodies_left)
{
ret_texc.push_back(curr_tex_y + tex_body_height);
}
ret_texc.push_back(curr_tex_y);
prev_bodies_left= bodies_left;
}
break;
case HTP_Bottom:
ret_texc.push_back(((curr_y-body_end_y) * tex_per_y) + tex_body_end);
break;
}
}
};
struct hold_time_lerper
{
double start_y_off;
double y_off_len;
double start_time;
double time_len;
hold_time_lerper(double sy, double yl, double st, double tl)
:start_y_off(sy), y_off_len(yl), start_time(st), time_len(tl)
{}
double lerp(double y_off)
{
return (((y_off - start_y_off) * time_len) / y_off_len) + start_time;
}
};
static void add_vert_strip(float const tex_y, strip_buffer& verts_to_draw,
Rage::Vector3 const& left,
Rage::Vector3 const& right, Rage::Color const& color, Rage::Color const& glow_color,
float const tex_left, float const tex_right)
{
verts_to_draw.add_vert(left, color, glow_color, Rage::Vector2(tex_left, tex_y));
verts_to_draw.add_vert(right, color, glow_color, Rage::Vector2(tex_right, tex_y));
}
struct hold_vert_step_state
{
double y;
double beat;
double second;
Rage::transform trans;
vector<double> tex_coords;
bool calc(NoteFieldColumn& col, double curr_y, double end_y,
hold_time_lerper& beat_lerp, hold_time_lerper& second_lerp,
hold_texture_handler& tex_handler,
int& phase, bool is_lift, NoteFieldColumn::render_note& renderable)
{
tex_coords.clear();
bool last_vert_set= false;
y= curr_y;
if(curr_y >= end_y)
{
// Different from the end check in hold_texture_handler because this
// clips the hold off at the end of the notefield. That clips the hold
// at the end of the hold.
y= end_y;
last_vert_set= true;
}
// It's important to call the tex_handler before the lerpers because the
// tex_handler changes y in certain conditions.
phase= tex_handler.calc_tex_y(y, tex_coords);
if(phase == HTP_Done)
{
last_vert_set= true;
}
beat= beat_lerp.lerp(y);
second= second_lerp.lerp(y);
renderable.input.change_eval_time(beat, second);
renderable.input.y_offset= y;
col.hold_render_transform(renderable.input, trans, col.m_twirl_holds);
// FIXME: Hold caps need to not be squished by the reverse_scale.
// And they need to be affected by the y zoom.
col.apply_yoffset_to_pos(renderable.input, trans.pos);
if(beat <= col.m_selection_end && beat >= col.m_selection_start)
{
trans.glow= col.get_selection_glow();
}
if(is_lift)
{
double along= (y - tex_handler.start_y) * lift_fade_dist_recip;
if(along < 1.0)
{
trans.alpha*= along;
trans.glow*= along;
}
}
return last_vert_set;
}
};
void NoteFieldColumn::draw_hold(QuantizedHoldRenderData& data,
render_note& note, double head_beat, double head_second,
double tail_beat, double tail_second, bool is_lift)
{
double const original_beat= note.input.eval_beat;
double const original_second= note.input.eval_second;
// pos_z_vec will be used later to orient the hold. Read below. -Kyz
static const Rage::Vector3 pos_z_vec(0.0f, 0.0f, 1.0f);
static const Rage::Vector3 neg_y_vec(0.0f, -1.0f, 0.0f);
static strip_buffer verts_to_draw;
verts_to_draw.init();
static const double y_step= 4.0;
double tex_top= data.rect->top;
double tex_bottom= data.rect->bottom;
double tex_left= data.rect->left;
double tex_right= data.rect->right;
switch(data.flip)
{
case TCFM_X:
std::swap(tex_left, tex_right);
break;
case TCFM_XY:
std::swap(tex_left, tex_right);
std::swap(tex_top, tex_bottom);
break;
case TCFM_Y:
std::swap(tex_top, tex_bottom);
break;
default:
break;
}
double head_y_offset= note.y_offset;
double tail_y_offset= note.tail_y_offset;
if(tail_y_offset < head_y_offset)
{
// The speed mod is negative.
std::swap(head_y_offset, tail_y_offset);
}
double y_off_len= tail_y_offset - head_y_offset;
hold_time_lerper beat_lerper(head_y_offset, y_off_len, head_beat, tail_beat - head_beat);
hold_time_lerper second_lerper(head_y_offset, y_off_len, head_second, tail_second - head_second);
hold_texture_handler tex_handler(note_size, head_y_offset, tail_y_offset, tex_top, tex_bottom, data);
float const color_scale= Rage::scale(note.note_iter->second.HoldResult.fLife, 0.f, 1.f, m_newskin->get_hold_gray_percent(), 1.f);
DISPLAY->ClearAllTextures();
DISPLAY->SetZTestMode(ZTEST_WRITE_ON_PASS);
DISPLAY->SetZWrite(true);
DISPLAY->SetTextureFiltering(TextureUnit_1, data.texture_filtering);
DISPLAY->SetTextureFiltering(TextureUnit_2, data.texture_filtering);
if(data.mask != nullptr)
{
Rage::Color player_color= get_player_color(note.note_iter->second.pn);
DISPLAY->set_color_key_shader(player_color, data.mask->GetTexHandle());
}
bool last_vert_set= false;
bool next_last_vert_set= false;
// Set a start and end y so that the hold can be clipped to the start and
// end of the field.
double start_y= max(tex_handler.start_y, first_y_offset_visible);
double end_y= std::min(tex_handler.end_y, last_y_offset_visible);
// next_step exists so that the forward vector of a hold can be calculated
// and used to make the hold turn and maintain constant width, instead of
// being skewed. Toggle the holds_skewed_by_mods flag with lua to see the
// difference.
int phase= HTP_Top;
int next_phase= HTP_Top;
hold_vert_step_state next_step; // The OS of the future.
next_step.calc(*this, start_y, end_y, beat_lerper, second_lerper,
tex_handler, next_phase, is_lift, note);
bool need_glow_pass= false;
for(double curr_y= start_y; !last_vert_set; curr_y+= y_step)
{
hold_vert_step_state curr_step= next_step;
if(curr_step.trans.glow > .01)
{
need_glow_pass= true;
}
phase= next_phase;
last_vert_set= next_last_vert_set;
next_last_vert_set= next_step.calc(*this, curr_y + y_step, end_y,
beat_lerper, second_lerper, tex_handler,
phase, is_lift, note);
Rage::Vector3 render_forward(0.0, 1.0, 0.0);
if(!m_holds_skewed_by_mods)
{
render_forward.x= next_step.trans.pos.x - curr_step.trans.pos.x;
render_forward.y= next_step.trans.pos.y - curr_step.trans.pos.y;
render_forward.z= next_step.trans.pos.z - curr_step.trans.pos.z;
render_forward= render_forward.GetNormalized();
}
Rage::Vector3 render_left;
if(m_use_moddable_hold_normal)
{
Rage::Vector3 normal;
m_hold_normal_mod.evaluate(note.input, normal);
render_left= Rage::CrossProduct(normal, render_forward);
}
else
{
if(std::abs(render_forward.z) > 0.9f) // 0.9 arbitrariliy picked.
{
render_left= Rage::CrossProduct(neg_y_vec, render_forward);
}
else
{