File: rubysdl_smpeg.c

package info (click to toggle)
libsdl-ruby 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,428 kB
  • ctags: 1,252
  • sloc: ansic: 4,610; ruby: 1,868; makefile: 18
file content (343 lines) | stat: -rw-r--r-- 9,455 bytes parent folder | download
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
/*
  Ruby/SDL   Ruby extension library for SDL

  Copyright (C) 2001-2007 Ohbayashi Ippei
  
  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
#ifdef HAVE_SMPEG
#include "rubysdl.h"
#include "smpeg/smpeg.h"

#ifdef HAVE_SDL_MIXER
#include "SDL_mixer.h"
#endif

static SMPEG_Filter* filters[3];
#define NULL_FILTER 0
#define BILINEAR_FILTER 1
#define DEBLOCKING_FILTER 2
#define NUM_FILTERS 3

static void setInfoToSMPEGInfo(VALUE obj,SMPEG_Info info)
{
  rb_iv_set(obj,"@has_audio",BOOL(info.has_audio));
  rb_iv_set(obj,"@has_video",BOOL(info.has_video));
  rb_iv_set(obj,"@width",INT2NUM(info.width));
  rb_iv_set(obj,"@height",INT2NUM(info.height));
  rb_iv_set(obj,"@current_frame",INT2NUM(info.current_frame));
  rb_iv_set(obj,"@current_fps",INT2NUM(info.current_fps));
  rb_iv_set(obj,"@audio_string",rb_str_new2(info.audio_string));
  rb_iv_set(obj,"@audio_current_frame",INT2NUM(info.audio_current_frame));
  rb_iv_set(obj,"@current_offset",UINT2NUM(info.current_offset));
  rb_iv_set(obj,"@total_size",UINT2NUM(info.total_size));
  rb_iv_set(obj,"@current_time",UINT2NUM(info.current_time));
  rb_iv_set(obj,"@total_time",UINT2NUM(info.total_time));
}

static VALUE smpeg_load(VALUE class,VALUE filename)
{
  SMPEG *mpeg;
  VALUE obj;
  char error_msg[2048];
    
  mpeg = SMPEG_new(GETCSTR(filename),NULL,0);
  if( SMPEG_error(mpeg) ){
    snprintf(error_msg,sizeof(error_msg),"Couldn't load %s: %s",
	     GETCSTR(filename),SMPEG_error(mpeg));
    SMPEG_delete(mpeg);
    rb_raise(eSDLError,"%s",error_msg);
  }

  obj = Data_Wrap_Struct(cMPEG,0,SMPEG_delete,mpeg);
  rb_iv_set(obj,"enable_audio",Qtrue);
  return obj;
}

static VALUE smpeg_getInfo(VALUE obj,VALUE infoObj)
{
  SMPEG *mpeg;
  SMPEG_Info info;
  
  if( !rb_obj_is_kind_of(infoObj,cMPEGInfo) )
    rb_raise(rb_eArgError,"type mismatch(expect SDL::MPEG::Info)");
  Data_Get_Struct(obj,SMPEG,mpeg);

  SMPEG_getinfo(mpeg,&info);
  setInfoToSMPEGInfo(infoObj,info);
  
  return Qnil;
}

static VALUE smpeg_enableAudio(VALUE obj,VALUE enable)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  rb_iv_set(obj,"enable_audio",enable);
  return Qnil;
}

static VALUE smpeg_enableVideo(VALUE obj,VALUE enable)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_enablevideo(mpeg,RTEST(enable));
  return Qnil;
}

static VALUE smpeg_status(VALUE obj)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  return INT2FIX(SMPEG_status(mpeg));
}

static VALUE smpeg_setVolume(VALUE obj,VALUE volume)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_setvolume(mpeg,NUM2INT(volume));
  return Qnil;
}

static VALUE smpeg_setDisplay(VALUE obj,VALUE dst)
{
  SMPEG *mpeg;
  SDL_Surface *surface;
  if( !rb_obj_is_kind_of(dst,cSurface) )
    rb_raise(rb_eArgError,"type mismatchi(expect Surface)");
  
  Data_Get_Struct(obj,SMPEG,mpeg);
  Data_Get_Struct(dst,SDL_Surface,surface);

  SMPEG_setdisplay(mpeg,surface,NULL,NULL);
  rb_iv_set(obj,"surface",dst);
  return Qnil;
}

static VALUE smpeg_setLoop(VALUE obj,VALUE repeat)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_loop(mpeg,RTEST(repeat));
  return Qnil;
}

static VALUE smpeg_scaleXY(VALUE obj,VALUE w,VALUE h)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_scaleXY(mpeg,NUM2INT(w),NUM2INT(h));
  return Qnil;
}

static VALUE smpeg_scale(VALUE obj,VALUE scale)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_scale(mpeg,NUM2INT(scale));
  return Qnil;
}

static VALUE smpeg_move(VALUE obj,VALUE x,VALUE y)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_move(mpeg,NUM2INT(x),NUM2INT(y));
  return Qnil;
}

static VALUE smpeg_setDisplayRegion(VALUE obj,VALUE x,VALUE y,VALUE w,
				    VALUE h)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_setdisplayregion(mpeg,NUM2INT(x),NUM2INT(y),NUM2INT(w),NUM2INT(h));
  return Qnil;
}

static VALUE smpeg_play(VALUE obj)
{
  SMPEG *mpeg;
  int use_audio;
  
  Data_Get_Struct(obj,SMPEG,mpeg);

#ifdef HAVE_SDL_MIXER
  use_audio = RTEST(rb_iv_get(obj,"enable_audio")) &&
    Mix_QuerySpec( NULL, NULL, NULL );

  if( use_audio ){
    SDL_AudioSpec audiofmt;
    Uint16 format;
    int freq, channels;
    
    SMPEG_enableaudio(mpeg, 0);
    /* Tell SMPEG what the audio format is */
    Mix_QuerySpec(&freq, &format, &channels);
    audiofmt.format = format;
    audiofmt.freq = freq;
    audiofmt.channels = channels;
    SMPEG_actualSpec(mpeg, &audiofmt);

    /* Hook in the MPEG music mixer */
    Mix_HookMusic(NULL,NULL);
    Mix_HookMusic(SMPEG_playAudioSDL, mpeg);
    SMPEG_enableaudio(mpeg, 1);
  }
#else
  SMPEG_enableaudio(mpeg, RTEST(rb_iv_get(obj,"enable_audio")));
#endif
  
  SMPEG_play(mpeg);
  return Qnil;
}

static VALUE smpeg_pause(VALUE obj)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_pause(mpeg);
  return Qnil;
}

static VALUE smpeg_stop(VALUE obj)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_stop(mpeg);
#ifdef HAVE_SDL_MIXER
  Mix_HookMusic(NULL,NULL);
#endif
  return Qnil;
}

static VALUE smpeg_rewind(VALUE obj)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_rewind(mpeg);
  return Qnil;
}

static VALUE smpeg_seek(VALUE obj,VALUE bytes)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_seek(mpeg,NUM2INT(bytes));
  return Qnil;
}

static VALUE smpeg_skip(VALUE obj,VALUE seconds)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_skip(mpeg,NUM2DBL(seconds));
  return Qnil;
}

static VALUE smpeg_renderFrame(VALUE obj,VALUE framenum)
{
  SMPEG *mpeg;
  Data_Get_Struct(obj,SMPEG,mpeg);
  SMPEG_renderFrame(mpeg,NUM2INT(framenum));
  return Qnil;
}

static VALUE smpeg_renderFinal(VALUE obj,VALUE dst, VALUE x, VALUE y)
{
  SMPEG *mpeg;
  SDL_Surface *surface;
  if( !rb_obj_is_kind_of(dst,cSurface) )
    rb_raise(rb_eArgError,"type mismatchi(expect Surface)");
  
  Data_Get_Struct(obj,SMPEG,mpeg);
  Data_Get_Struct(dst,SDL_Surface,surface);
  
  SMPEG_renderFinal(mpeg, surface, NUM2INT(x), NUM2INT(y));
  return Qnil;
}
  
static VALUE smpeg_setFilter(VALUE obj,VALUE filter)
{
  SMPEG *mpeg;
  
  Data_Get_Struct(obj,SMPEG,mpeg);
  if( (NUM2INT(filter)<0) || (NUM2INT(filter)>=NUM_FILTERS) )
    rb_raise(eSDLError,"There isn't that filter");
  SMPEG_filter(mpeg,filters[NUM2INT(filter)]);
  return Qnil;
}

static void defineConstForSMPEG()
{
  rb_define_const(cMPEG,"ERROR",INT2FIX(SMPEG_ERROR));
  rb_define_const(cMPEG,"STOPPED",INT2FIX(SMPEG_STOPPED));
  rb_define_const(cMPEG,"PLAYING",INT2FIX(SMPEG_PLAYING));
  rb_define_const(cMPEG,"NULL_FILTER",INT2FIX(NULL_FILTER));
  rb_define_const(cMPEG,"BILINEAR_FILTER",INT2FIX(BILINEAR_FILTER));
  rb_define_const(cMPEG,"DEBLOCKING_FILTER",INT2FIX(DEBLOCKING_FILTER));
}

void init_smpeg()
{
  cMPEG = rb_define_class_under(mSDL,"MPEG",rb_cObject);
  cMPEGInfo = rb_define_class_under(cMPEG,"Info",rb_cObject);

  filters[NULL_FILTER] = SMPEGfilter_null();
  filters[BILINEAR_FILTER] = SMPEGfilter_bilinear();
  filters[DEBLOCKING_FILTER] = SMPEGfilter_deblocking();

  defineConstForSMPEG();
  
  rb_define_attr(cMPEGInfo,"has_audio",1,0);
  rb_define_attr(cMPEGInfo,"has_video",1,0);
  rb_define_attr(cMPEGInfo,"width",1,0);
  rb_define_attr(cMPEGInfo,"height",1,0);
  rb_define_attr(cMPEGInfo,"current_frame",1,0);
  rb_define_attr(cMPEGInfo,"current_fps",1,0);
  rb_define_attr(cMPEGInfo,"audio_string",1,0);
  rb_define_attr(cMPEGInfo,"audio_current_frame",1,0);
  rb_define_attr(cMPEGInfo,"current_offset",1,0);
  rb_define_attr(cMPEGInfo,"total_size",1,0);
  rb_define_attr(cMPEGInfo,"current_time",1,0);
  rb_define_attr(cMPEGInfo,"total_time",1,0);

  rb_define_singleton_method(cMPEG,"load",smpeg_load,1);
  rb_define_singleton_method(cMPEG,"new",smpeg_load,1);

  rb_define_method(cMPEG,"info",smpeg_getInfo,1);
  rb_define_method(cMPEG,"enableAudio",smpeg_enableAudio,1);
  rb_define_method(cMPEG,"enableVideo",smpeg_enableVideo,1);
  rb_define_method(cMPEG,"status",smpeg_status,0);
  rb_define_method(cMPEG,"setVolume",smpeg_setVolume,1);
  rb_define_method(cMPEG,"setDisplay",smpeg_setDisplay,1);
  rb_define_method(cMPEG,"setLoop",smpeg_setLoop,1);
  rb_define_method(cMPEG,"scaleXY",smpeg_scaleXY,2);
  rb_define_method(cMPEG,"scale",smpeg_scale,1);
  rb_define_method(cMPEG,"move",smpeg_move,1);
  rb_define_method(cMPEG,"setDisplayRegion",smpeg_setDisplayRegion,4);
  rb_define_method(cMPEG,"play",smpeg_play,0);
  rb_define_method(cMPEG,"pause",smpeg_pause,0);
  rb_define_method(cMPEG,"stop",smpeg_stop,0);
  rb_define_method(cMPEG,"rewind",smpeg_rewind,0);
  rb_define_method(cMPEG,"seek",smpeg_seek,1);
  rb_define_method(cMPEG,"skip",smpeg_skip,1);
  rb_define_method(cMPEG,"renderFrame",smpeg_renderFrame,1);
  rb_define_method(cMPEG,"renderFinal",smpeg_renderFinal,3);
  rb_define_method(cMPEG,"setFilter",smpeg_setFilter,1);
  
}
#endif /* HAVE_SMPEG */