gba_fade.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. (*
  2. gba_fade.pas 18/06/2006 4.19.14
  3. ------------------------------------------------------------------------------
  4. Part of this file is a raw porting of libgba library for gba (you can find it
  5. at http://www.devkitpro.org).
  6. As this is a direct port from c, I'm pretty sure that something could not work
  7. as you expect. I am even more sure that this code could be written better, so
  8. if you think that I have made some mistakes or you have some better
  9. implemented functions, let me know [francky74 (at) gmail (dot) com]
  10. Enjoy!
  11. Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  12. (http://www.freepascal.org)
  13. Copyright (C) 2006 Francesco Lombardi
  14. This library is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU Lesser General Public
  16. License as published by the Free Software Foundation; either
  17. version 2.1 of the License, or (at your option) any later version.
  18. This library is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. Lesser General Public License for more details.
  22. You should have received a copy of the GNU Lesser General Public
  23. License along with this library; if not, write to the Free Software
  24. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. ------------------------------------------------------------------------------
  26. *)
  27. unit gba_fade;
  28. {$i def.inc}
  29. interface
  30. uses
  31. gba_types, gba_regs, gba_video, gba_core;
  32. var
  33. CurrentPalette: array [0..511] of word;
  34. FadeTable: array [0..(512*3*2)-1] of smallint;
  35. procedure GetCurrentPalette();
  36. procedure SetPalette(Palette: pword);
  37. procedure DoFade(FadeCount: dword);
  38. procedure FadeToGrayScale(FrameCount: dword);
  39. procedure FadeToPalette(const NewPalette: pword; FrameCount: dword);
  40. procedure FadeToGray(gray: word; FrameCount: dword);
  41. implementation
  42. procedure GetCurrentPalette();
  43. var
  44. i: dword;
  45. Src: ^word;
  46. Dest: ^word;
  47. begin
  48. Src := BG_COLORS;
  49. Dest := CurrentPalette;
  50. for i := 0 to 511 do
  51. Dest[i] := dword(Src[i]);
  52. end;
  53. procedure SetPalette(Palette: pword);
  54. var
  55. i: dword;
  56. Src, Dest: ^word;
  57. begin
  58. Src := Palette;
  59. Dest := BG_COLORS;
  60. for i := 0 to 511 do
  61. Dest[i] := dword(Src[i]);
  62. end;
  63. procedure DoFade(FadeCount: dword);
  64. var
  65. r, g, b: word;
  66. i, count, color: dword;
  67. src: ^smallint;
  68. dest: ^word;
  69. begin
  70. for count := 0 to FadeCount - 1 do
  71. begin
  72. src := FadeTable;
  73. dest := CurrentPalette;
  74. i := 0;
  75. while i < 512 do
  76. begin
  77. r := Src[(i*6)+1];
  78. r := r + Src[(i*6)];
  79. Src[(i*6)+1] := r;
  80. g := Src[(i*6)+3];
  81. g := g + Src[(i*6)+2];
  82. Src[(i*6)+3] := g;
  83. b := Src[(i*6)+5];
  84. b := b + Src[(i*6)+4];
  85. Src[(i*6)+5] := b;
  86. color := (r shr 8) or ((g shr 8) shl 5) or ((b shr 8) shl 10);
  87. Dest[i] := color;
  88. inc(i);
  89. end;
  90. WaitForVBlank();
  91. SetPalette(CurrentPalette);
  92. end;
  93. end;
  94. procedure FadeToGray(gray: word; FrameCount: dword);
  95. var
  96. index, r, g, b, color: dword;
  97. src: ^word;
  98. table: ^smallint;
  99. begin
  100. GetCurrentPalette();
  101. src := CurrentPalette;
  102. for index :=0 to 511 do
  103. begin
  104. color := src[index];
  105. r := (color and $1f) shl 8;
  106. g := ((color shr 5) and $1f) shl 8;
  107. b := ((color shr 10) and $1f) shl 8;
  108. FadeTable[(index*6)] := ((gray shl 8)-r) div FrameCount;
  109. FadeTable[(index*6)+1] := r;
  110. FadeTable[(index*6)+2] := ((gray shl 8)-g) div FrameCount;
  111. FadeTable[(index*6)+3] := g;
  112. FadeTable[(index*6)+4] := ((gray shl 8)-b) div FrameCount;
  113. FadeTable[(index*6)+5] := b;
  114. end;
  115. DoFade( FrameCount);
  116. end;
  117. procedure FadeToPalette(const NewPalette: pword; FrameCount: dword);
  118. var
  119. index: dword;
  120. color: word;
  121. r1, r2, g1, g2, b1, b2: smallint;
  122. Src: ^word;
  123. Dest: ^word;
  124. Table: ^smallint;
  125. begin
  126. GetCurrentPalette();
  127. Src := CurrentPalette;
  128. Dest := NewPalette;
  129. Table := FadeTable;
  130. for index := 0 to 511 do
  131. begin
  132. color := Src[index];
  133. r1 := (color and $1f) shl 8;
  134. g1 := ((color shr 5) and $1f) shl 8;
  135. b1 := ((color shr 10) and $1f) shl 8;
  136. color := Dest[index];
  137. r2 := (color and $1f) shl 8;
  138. g2 := ((color shr 5) and $1f) shl 8;
  139. b2 := ((color shr 10) and $1f) shl 8;
  140. Table[(index*6)] := (r2 - r1) div FrameCount;
  141. Table[(index*6)+1] := r1;
  142. Table[(index*6)+2] := (g2 - g1) div FrameCount;
  143. Table[(index*6)+3] := g1;
  144. Table[(index*6)+4] := (b2 - b1) div FrameCount;
  145. Table[(index*6)+5] := b1;
  146. end;
  147. DoFade(FrameCount);
  148. end;
  149. procedure FadeToGrayScale(FrameCount: dword);
  150. var
  151. index: dword;
  152. gray: word;
  153. r,g,b: smallint;
  154. Src: ^word;
  155. Dest: ^word;
  156. Table: ^smallint;
  157. GrayScalePalette: array [0..511] of word;
  158. begin
  159. GetCurrentPalette();
  160. Src := CurrentPalette;
  161. Table := FadeTable;
  162. for index := 0 to 511 do
  163. begin
  164. r := (Src[index] and $1f);
  165. g := ((Src[index] shr 5) and $1f);
  166. b := ((Src[index] shr 10) and $1f);
  167. Gray := (r shr 2) + (r shr 4) + (g shr 1) + (g shr 4) + (b shr 3);
  168. GrayScalePalette[index] := RGB(gray, gray, gray);
  169. end;
  170. FadeToPalette(GrayScalePalette, FrameCount);
  171. end;
  172. end.