aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Menu/UDisplay.pas
blob: d7439dc9b62898364281d09abb8ff1a2f3daf34f (plain) (blame)
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
unit UDisplay;

interface

uses Windows, SDL, UMenu, OpenGL12, SysUtils, dialogs;

type
  TDisplay = class
    ActualScreen:     PMenu;
    NextScreen:       PMenu;

    h_DC:     HDC;
    h_RC:     HGLRC;

    Fade: Real;
    // fade-mod
    myfade: integer;
    pTexData : Pointer;
    pTex : glUInt;
    // end

    function Draw: Boolean;
    procedure PrintScreen;
    constructor Create;
    // fade mod
    destructor Destroy;
    // end
    procedure ScreenShot;
  end;

var
  Display:          TDisplay;
//  ActualScreen:     PMenu;
//  NextScreen:       PMenu;

implementation

uses UGraphic, UTime, Graphics, Jpeg, UPliki, UTexture;

constructor TDisplay.Create;
begin
  inherited Create;
  // fade mod
  myfade:=0;
  // generate texture for fading between screens
  GetMem(pTexData, 1024*1024*4);
  glGenTextures(1, pTex);
  glBindTexture(GL_TEXTURE_2D, pTex);

  glTexImage2D(GL_TEXTURE_2D, 0, 3, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, pTexData);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  FreeMem(pTexData);
  // end
end;

// fade mod
destructor TDisplay.Destroy;
begin
  glDeleteTextures(1,@pTex);
  inherited Destroy;
end;
// end

function TDisplay.Draw: Boolean;
var
  S:    integer;
  // fade mod
  myfade2:integer;
  // end
begin
  Result := True;

{  Col := 1;
  if (ParamStr(1) = '-black') or (ParamStr(1) = '-fsblack') then
    Col := 0;}

  glClearColor(0, 0, 0 , 0);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  for S := 1 to Screens do begin
    ScreenAct := S;

//    if Screens = 1 then ScreenX := 0;
//    if (Screens = 2) and (S = 1) then ScreenX := -1;
//    if (Screens = 2) and (S = 2) then ScreenX := 1;
    ScreenX := 0;


    if S = 2 then TimeSkip := 0 else; // it's easier than rewriting code
    glViewPort((S-1) * ScreenW div Screens, 0, ScreenW div Screens, ScreenH);

//    ActualScreen.SetAnimationProgress(1);
    if not assigned (NextScreen) then begin
      Result := ActualScreen.Draw;
      // fade mod
      myfade:=0;
      // end
    end
    else begin
      // fade mod
      //Create Fading texture if we're just starting
      if myfade = 0 then
      begin
        ActualScreen.Draw;
        glBindTexture(GL_TEXTURE_2D, pTex);
        glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 1024, 1024, 0);
        NextScreen.onShow;
      end;

//      LastFade := Fade;   // whatever
//      Fade := Fade -0.999; // start fading out
      myfade:=myfade+1;

//      ActualScreen.ShowFinish := false; // no purpose?

//      ActualScreen.SetAnimationProgress(Fade-1); // nop?

      NextScreen.Draw; // draw next screen

      // and draw old screen over it... slowly fading out
      myfade2:=myfade*myfade;
      glBindTexture(GL_TEXTURE_2D, pTex);
      glColor4f(1, 1, 1, (1000-myfade2)/1000); // strange calculation - alpha gets negative... but looks good this way
      glEnable(GL_TEXTURE_2D);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      glEnable(GL_BLEND);
      glBegin(GL_QUADS);
        glTexCoord2f(0,0);glVertex2f(0-myfade2,   600+myfade2);
        glTexCoord2f(0,ScreenH/1024);glVertex2f(0-myfade2,   0-myfade2);
        glTexCoord2f(ScreenW/1024,ScreenH/1024);glVertex2f(800+myfade2, 0-myfade2);
        glTexCoord2f(ScreenW/1024,0);glVertex2f(800+myfade2, 600+myfade2);
      glEnd;
      glDisable(GL_BLEND);
      glDisable(GL_TEXTURE_2D);
    end;

    if myfade > 50 then begin // fade out complete...
        myfade:=0;
        ActualScreen.onHide; // nop... whatever
        ActualScreen.ShowFinish:=False;
        ActualScreen:=NextScreen;
        NextScreen := nil;
        ActualScreen.onShowFinish; // one more nop...
        ActualScreen.ShowFinish := true; // who wrote this crap?
    // end of fade mod
    end; // if
  end; // for
//  SwapBuffers(h_DC);
end;

{function TDisplay.Fade(FadeIn : Boolean; Steps : UInt8): UInt8;
begin
  Self.FadeIn := FadeIn;
  FadeStep := (SizeOf(FadeStep) * $FF) div Steps;
  ActualStep := $FF;
  Result := $FF div FadeStep;
end;}

procedure TDisplay.PrintScreen;
var
  Bitmap:     TBitmap;
  Jpeg:       TJpegImage;
  X, Y:       integer;
  Num:        integer;
  FileName:   string;
begin
  for Num := 1 to 9999 do begin
    FileName := IntToStr(Num);
    while Length(FileName) < 4 do FileName := '0' + FileName;
    FileName := ScreenshotsPath + 'screenshot' + FileName + '.jpg';
    if not FileExists(FileName) then break
  end;

  glReadPixels(0, 0, ScreenW, ScreenH, GL_RGBA, GL_UNSIGNED_BYTE, @PrintScreenData[0]);
  Bitmap := TBitmap.Create;
  Bitmap.Width := ScreenW;
  Bitmap.Height := ScreenH;

  for Y := 0 to ScreenH-1 do
    for X := 0 to ScreenW-1 do
      Bitmap.Canvas.Pixels[X, Y] := PrintScreenData[(ScreenH-1-Y) * ScreenW + X] and $00FFFFFF;

  Jpeg := TJpegImage.Create;
  Jpeg.Assign(Bitmap);
  Bitmap.Free;
  Jpeg.CompressionQuality := 95;//90;
  ForceDirectories(ScreenshotsPath);
  Jpeg.SaveToFile(FileName);
  Jpeg.Free;
end;

procedure TDisplay.ScreenShot;
 var F : file;
     FileInfo: BITMAPINFOHEADER;
     FileHeader : BITMAPFILEHEADER;
     pPicData:Pointer;
     FileName: String;
     Num: Integer;
begin
  //bilddatei Suchen
  for Num := 1 to 9999 do begin
    FileName := IntToStr(Num);
    while Length(FileName) < 4 do FileName := '0' + FileName;
    FileName := {ScreenshotsPath + }'screenshot' + FileName + '.BMP';
    if not FileExists(FileName) then break
  end;

 //Speicher f�r die Speicherung der Header-Informationen vorbereiten
 ZeroMemory(@FileHeader, SizeOf(BITMAPFILEHEADER));
 ZeroMemory(@FileInfo, SizeOf(BITMAPINFOHEADER));
 
 //Initialisieren der Daten des Headers
 FileHeader.bfType := 19778; //$4D42 = 'BM'
 FileHeader.bfOffBits := SizeOf(BITMAPINFOHEADER)+SizeOf(BITMAPFILEHEADER);
 
 //Schreiben der Bitmap-Informationen
 FileInfo.biSize := SizeOf(BITMAPINFOHEADER);
 FileInfo.biWidth := ScreenH;
 FileInfo.biHeight := ScreenW;
 FileInfo.biPlanes := 1;
 FileInfo.biBitCount := 32;
 FileInfo.biSizeImage := FileInfo.biWidth*FileInfo.biHeight*(FileInfo.biBitCount div 8);
 
 //Gr��enangabe auch in den Header �bernehmen
 FileHeader.bfSize := FileHeader.bfOffBits + FileInfo.biSizeImage;
 
 //Speicher f�r die Bilddaten reservieren
 GetMem(pPicData, FileInfo.biSizeImage);
 try
  //Bilddaten von OpenGL anfordern (siehe oben)
  glReadPixels(0, 0, ScreenW, ScreenH, GL_BGRA, GL_UNSIGNED_BYTE, pPicData);
 
  //Und den ganzen M�ll in die Datei schieben ;-)
  //Moderne Leute nehmen daf�r auch Streams ...
  AssignFile(f, Filename);
  Rewrite( f,1 );
  try
   BlockWrite(F, FileHeader, SizeOf(BITMAPFILEHEADER));
   BlockWrite(F, FileInfo, SizeOf(BITMAPINFOHEADER));
   BlockWrite(F, pPicData^, FileInfo.biSizeImage );
  finally
   CloseFile(f);
  end;
 finally
  //Und den angeforderten Speicher wieder freigeben ...
  FreeMem(pPicData, FileInfo.biSizeImage);
 end;
end;


end.