summaryrefslogtreecommitdiff
path: root/packages/gbaunits/src/gba_fade.pas
blob: 465fbc1b48ad6b38b0556cd45a48dc3bf52b6800 (plain)
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
(*
  gba_fade.pas  18/06/2006 4.19.14
  ------------------------------------------------------------------------------
  Part of this file is a raw porting of libgba library for gba (you can find it 
  at http://www.devkitpro.org).
  
  As this is a direct port from c, I'm pretty sure that something could not work
  as you expect. I am even more sure that this code could be written better, so 
  if you think that I have made some mistakes or you have some better 
  implemented functions, let me know [francky74 (at) gmail (dot) com]
  Enjoy!

  Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  (http://www.freepascal.org)
  
  Copyright (C) 2006  Francesco Lombardi
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
  ------------------------------------------------------------------------------
*)
 
unit gba_fade;
{$i def.inc}
interface

uses 
  gba_types, gba_regs, gba_video, gba_core;

var
  CurrentPalette: array [0..511] of word;
  FadeTable: array [0..(512*3*2)-1] of smallint;
  
procedure GetCurrentPalette();
procedure SetPalette(Palette: pword);
procedure DoFade(FadeCount: dword);
procedure FadeToGrayScale(FrameCount: dword);
procedure FadeToPalette(const NewPalette: pword; FrameCount: dword);
procedure FadeToGray(gray: word; FrameCount: dword);

implementation

procedure GetCurrentPalette();
var
  i: dword;
  Src: ^word;
  Dest: ^word;
begin
  Src := BG_COLORS;
  Dest := CurrentPalette;
	for i := 0 to 511 do
	  Dest[i] := dword(Src[i]);
end;


procedure SetPalette(Palette: pword);
var
  i: dword;
  Src, Dest: ^word;
begin	
	Src := Palette;
	Dest := BG_COLORS;
	for i := 0 to 511 do
		Dest[i] := dword(Src[i]);
end;

procedure DoFade(FadeCount: dword);
var
  r, g, b: word;
	i, count, color: dword;
	src: ^smallint;
  dest: ^word;
begin
  for count := 0 to FadeCount - 1 do
  begin
    src := FadeTable;
    dest := CurrentPalette;

    i := 0;
    while i < 512 do
    begin
      r := Src[(i*6)+1];
      r := r + Src[(i*6)];
      Src[(i*6)+1] := r;
      
      g := Src[(i*6)+3];
      g := g + Src[(i*6)+2];
      Src[(i*6)+3] := g;
      
      b := Src[(i*6)+5];
      b := b + Src[(i*6)+4];
      Src[(i*6)+5] := b;
      
      color := (r shr 8) or ((g shr 8) shl 5) or ((b shr 8) shl 10);
      Dest[i] := color;
      inc(i);
    end;
    WaitForVBlank();
    SetPalette(CurrentPalette);
	end;
end;

procedure FadeToGray(gray: word; FrameCount: dword);
var
	index, r, g, b, color: dword;
 	src: ^word;
	table: ^smallint; 
begin
  GetCurrentPalette();
  src := CurrentPalette;
  
  for index :=0  to 511 do 
  begin
    color := src[index];
    r := (color and $1f) shl 8;
    g := ((color shr 5) and $1f) shl 8;
    b := ((color shr 10) and $1f) shl 8;
    
    FadeTable[(index*6)] := ((gray shl 8)-r) div FrameCount;
    FadeTable[(index*6)+1] := r;
    
    FadeTable[(index*6)+2] := ((gray shl 8)-g) div FrameCount;
    FadeTable[(index*6)+3] := g;
    
    FadeTable[(index*6)+4] := ((gray shl 8)-b) div FrameCount;
    FadeTable[(index*6)+5] := b;
  end;
  DoFade( FrameCount);
end;

procedure FadeToPalette(const NewPalette: pword; FrameCount: dword);
var
  index: dword;
  color: word;
  r1, r2, g1, g2, b1, b2: smallint;
  Src: ^word;
  Dest: ^word;
  Table: ^smallint;
begin
  GetCurrentPalette();
  Src := CurrentPalette;
  Dest := NewPalette;
  Table := FadeTable;
  
  for index := 0 to 511 do
  begin
    color := Src[index];
    r1 := (color and $1f) shl 8;
    g1 := ((color shr 5) and $1f) shl 8;
    b1 := ((color shr 10) and $1f) shl 8;
    
    color := Dest[index];
    r2 := (color and $1f) shl 8;
    g2 := ((color shr 5) and $1f) shl 8;
    b2 := ((color shr 10) and $1f) shl 8;
    
    Table[(index*6)] := (r2 - r1) div FrameCount;
    Table[(index*6)+1] := r1;
    Table[(index*6)+2] := (g2 - g1) div FrameCount;
    Table[(index*6)+3] := g1;
    Table[(index*6)+4] := (b2 - b1) div FrameCount;
    Table[(index*6)+5] := b1;
  end;
  DoFade(FrameCount);
end;

procedure FadeToGrayScale(FrameCount: dword);
var
  index: dword;
  gray: word;
  r,g,b: smallint;
  Src: ^word;
  Dest: ^word;
  Table: ^smallint;
  GrayScalePalette: array [0..511] of word; 
begin
  GetCurrentPalette();
  Src := CurrentPalette;
  Table := FadeTable;

  for index := 0 to 511 do
  begin
    r := (Src[index] and $1f);
    g := ((Src[index] shr 5) and $1f);
    b := ((Src[index] shr 10) and $1f);
    Gray := (r shr 2) + (r shr 4) + (g shr 1) + (g shr 4) + (b shr 3); 
    GrayScalePalette[index] := RGB(gray, gray, gray);
  end;
  FadeToPalette(GrayScalePalette, FrameCount);
end;

end.