aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/SMpeg/USmpeg.pas
blob: c7f756db7bc8a45792c8f0bbcb7533636970e25a (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
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
unit USmpeg;

interface
uses SDL, smpeg, OpenGL12, SysUtils, UIni;

procedure OpenSmpeg(FileName: string);
procedure SkipSmpeg(Time: single);
procedure PlaySmpeg;
procedure PauseSmpeg; //PauseMod
procedure UpdateSmpeg;
procedure CloseSmpeg;
function glmovie_init(Width : GLuint; Height : TGLuint ) : TGLenum;
procedure glmpeg_update(surface: PSDL_Surface; x: Sint32; y: Sint32; w: Uint32; h: Uint32); cdecl;
procedure DrawSmpeg(frame: PGLubyte);
procedure glmovie_resize( width : GLuint; height : GLuint );
procedure glmovie_quit;

var
  mpeg:       PSMPEG;
  mpeg_info:  TSMPEG_Info;
  surface:    PSDL_Surface;

type
  { Some data is redundant at this stage. }
  PGLMovieTexture = ^TGLMovieTexture;
  TGLMovieTexture = record
    id : TGLuint; (* OpenGL texture id. *)
    poly_width : TGLuint; (* Quad width for tile. *)
    poly_height : TGLuint; (* Quad height for tile. *)
    movie_width : TGLuint; (* Width of movie inside tile. *)
    movie_height : TGLuint; (* Height of movie inside tile. *)
    skip_rows : TGLuint; (* Number of rows of movie to skip *)
    skip_pixels : TGLuint; (* Number of columns of movie to skip *)
    row : TGLuint; (* Row number of tile in scheme. *)
    col : TGLuint; (* Column number of tile in scheme. *)
  end;

type
  TGLuintArray = array of TGLuint;
  PGLuintArray = ^TGLuintArray;
  TGLMovieTextureArray = array of TGLMovieTexture;
  PGLMovieTextureArray = ^TGLMovieTextureArray;

var
  (* Our evil maximum texture size. Boo 3Dfxnot  *)
  texture_size : TGLuint = 1024;//512;
  texture_ids : TGLuint;
  textures:     TGLMovieTexture;
  tiled_width : TGLuint = 0;
  tiled_height : TGLuint = 0;
  movie_width : TGLuint = 0;
  movie_height : TGLuint = 0;  

implementation

procedure OpenSmpeg(FileName: string);
begin
  mpeg := SMPEG_new(PChar(FileName), @mpeg_info, 0); // audio
  if ( mpeg = nil ) then begin
    SDL_Quit;
    Exit;
  end;

//  SMPEG_setvolume(mpeg, 50);
  SMPEG_enableaudio(mpeg, 0);

  (* Everything needs to be in RGB for GL, but needs to be 32-bit for SMPEG. *)
  surface := SDL_AllocSurface( SDL_SWSURFACE,
    mpeg_info.width,
    mpeg_info.height,
    32,
    $000000FF,
    $0000FF00,
    $00FF0000,
    $FF000000 );

  if ( surface = nil ) then begin
    SDL_Quit;
    Exit;
  end;

  (* *Initialize* with mpeg size. *)
  if (glmovie_init( mpeg_info.width, mpeg_info.height ) <> GL_NO_ERROR ) then begin
    SDL_Quit;
    Exit;
  end;

  SMPEG_setdisplay(mpeg, surface, nil, @glmpeg_update);
end;

procedure SkipSmpeg(Time: single);
begin
  SMPEG_skip(mpeg, Time);
end;

procedure PlaySmpeg;
begin
  SMPEG_play(mpeg);
end;

//Pause Mod
procedure PauseSmpeg;
begin
  SMPEG_pause(mpeg);
end;

procedure UpdateSmpeg;
begin
//  glmpeg_update(surface,0,0,0,0);
  DrawSmpeg( PGLubyte( surface.pixels ) );
end;

procedure CloseSmpeg;
begin
//  glmovie_quit;
  SMPEG_delete(mpeg);
end;

function glmovie_init( Width : GLuint; Height : TGLuint ) : TGLenum;
type
  PGLubyteArray = ^TGLubyteArray;
  TGLubyteArray = array of TGLubyte;
var
  (* Initial black texels. *)
  pixels : TGLubyteArray;
  (* Absolute offsets from within tiled frame. *)
  //offset_x: GLuint;
  //offset_y: GLuint;
  skip_rows : GLuint;
  skip_pixels : GLuint;
  i, j, current : GLuint;
begin
  skip_rows := 0;
  current := 0;
  (* Save original movie dimensions. *)
  movie_width := width;
  movie_height := height;

  (* Get the power of 2 dimensions. *)
  tiled_width := 1024{512};
  tiled_height := 1024{512};

  texture_size := 1024{512};

  (* Time for fun with data type = record *)
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glEnable(GL_TEXTURE_2D);
  glEnable(GL_DITHER);

  glGenTextures(1, @texture_ids);

      current := 0;
      (* Setup texture. *)
      textures.id := texture_ids;
      textures.poly_width := texture_size;
      textures.poly_height := texture_size;
      textures.movie_width := movie_width - 2;
      textures.movie_height := movie_height - 2;
      textures.row := i;
      textures.col := j;
      textures.skip_pixels := skip_pixels;
      textures.skip_rows := skip_rows;

      SetLength( pixels, textures.poly_width * textures.poly_height * 4 );
      if ( pixels = nil ) then
      begin
        glDeleteTextures(1, @texture_ids);
        result := GL_OUT_OF_MEMORY;
        exit;
      end;
      //FillChar( pixels^, textures[ current ].poly_width * textures[ current ].poly_height * 4, 0 );

      (* Do all of our useful binding. *)
      glBindTexture(GL_TEXTURE_2D, texture_ids);
//      glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
      (* Specify our 256x256 black texture. *)
      glTexImage2D( GL_TEXTURE_2D,
        0,
        GL_RGB,
        1024{512},//textures.poly_width,
        1024{512},//textures.poly_height,
        0,
        GL_RGBA,
        GL_UNSIGNED_BYTE,
        @pixels[0] );
        SetLength( pixels, 0 );


  (* Simple state setup at the end. *)
  result := glGetError( );
end;

procedure glmpeg_update( surface : PSDL_Surface; x : Sint32; y : Sint32; w : Uint32;
  h : Uint32 ); cdecl;
var
  error : TGLenum;
begin
  glClear( GL_COLOR_BUFFER_BIT );
  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity;
  DrawSmpeg( PGLubyte( surface.pixels ) );
  error := glGetError( );
  if ( error <> GL_NO_ERROR ) then Exit;
  SDL_GL_SwapBuffers;
end;

procedure DrawSmpeg(frame: PGLubyte);
var
  Shift:    TGLdouble;
  CropT:    real;
  CropB:    real;
  TexT:     real;
  TexB:     real;
  TexL:     real;
  TexR:     real;
  Wide:     boolean;
begin
  (* full screen mpeg *)
{  CropT := 0;
  CropB := 600;
  TexT := 0;
  TexB := 1;
  TexL := 0;
  TexW := 1;}

  // set movie texture crop
  Wide := false;
  if (textures.movie_width = 720-2) and (textures.movie_height = 344-2) then begin
    TexT := 0;
    TexB := 342/1024;
    Wide := true;
  end;
  if textures.movie_height = 304-2 then begin
    TexT := 0;
    TexB := 304/1024;
    Wide := true;
  end;
  if textures.movie_height = 152-2 then begin
    TexT := 0;
    TexB := 152/1024;
    Wide := true;
  end;

  CropT := 110; // (110/800 = 13,75% max crop)
  CropB := 490; // (110/800 = 13,75% max crop)

  if (textures.movie_height <> 304-2) and (textures.movie_height <> 152-2) and (textures.movie_height <> 344-2) then begin
    TexT := 110 / 600 * (textures.movie_height / 1024{512});
    TexB := 490 / 600 * (textures.movie_height / 1024{512});

    if Ini.MovieSize = 1 then begin
      // full screen size
      CropT := 0;
      CropB := 600;
      TexT := 0;
      TexB := textures.movie_height / 1024{512};
    end;
  end;

  TexL := {10}0/600 * (textures.movie_width / 1024{512});
  TexR := {590}600/600 * (textures.movie_width / 1024{512});

  glEnable(GL_TEXTURE_2D);
  glDisable(GL_BLEND);
  glColor3f(1, 1, 1);
  glBindTexture( GL_TEXTURE_2D, texture_ids );
//  glPixelStorei( GL_UNPACK_ROW_LENGTH, movie_width );
  glPixelStorei( GL_UNPACK_SKIP_ROWS, 0);
  glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0);
  glTexSubImage2D( GL_TEXTURE_2D, 0, 0, (* offset_x *) 0, (* offset_y *) textures.movie_width + 2, textures.movie_height + 2, GL_RGBA, GL_UNSIGNED_BYTE, frame );

  // draw
  glBegin( GL_QUADS );
    glTexCoord2f(TexL, TexT); glVertex2f(0, CropT);
    glTexCoord2f(TexL, TexB); glVertex2f(0, CropB);
    glTexCoord2f(TexR, TexB); glVertex2f(800, CropB);
    glTexCoord2f(TexR, TexT); glVertex2f(800, CropT);
  glEnd;
  glDisable(GL_TEXTURE_2D);
end;

procedure glmovie_resize( width : GLuint; height : GLuint );
begin
  glViewport(0, 0, width, height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity;
  gluOrtho2D(0, 800, 600, 0);
end;

procedure glmovie_quit;
begin
  glDeleteTextures(1, @texture_ids);
end;

end.