Debian Codegraph @

src/state/SDL_Movie.h

raw
1 #ifndef HEADER_SDLMOVIE_H
2 #define HEADER_SDLMOVIE_H
3 #ifdef HAVE_SMPEG
4
5 #include "SDL.h"
6
7 #include "smpeg/smpeg.h"
8
9 #include "Drawable.h"
10 #include "Log.h"
11
12 /**
13 * Thanks to Drew Benton for the initial code:
14 * http://www.gamedev.net/community/forums/topic.asp?topic_id=348340
15 */
16 class SDL_Movie : public Drawable
17 {
18 private:
19 // Surface for the movie
20 SDL_Surface *movieSurface;
21
22 // Holds the movie information
23 SMPEG_Info movieInfo;
24
25 // Load the movie
26 SMPEG *movie;
27
28 // The max we can scale by
29 int MaxScaleX;
30 int MaxScaleY;
31 int MaxScale;
32
33 public:
34 SDL_Movie()
35 {
36 MaxScaleX = MaxScaleY = MaxScale = 1;
37
38 movieSurface = 0;
39 movie = 0;
40 }
41
42 // Free our movie
43 virtual ~SDL_Movie()
44 {
45 if (movie) {
46 Stop();
47 SMPEG_delete( movie );
48 movie = 0;
49 }
50 if (movieSurface) {
51 SDL_FreeSurface( movieSurface );
52 movieSurface = 0;
53 }
54 }
55
56 void ClearScreen()
57 {
58 SDL_FillRect( movieSurface, 0, 0 );
59 }
60
61 // Set's the volume on a scale of 0 - 100
62 void SetVolume( int vol )
63 {
64 SMPEG_setvolume( movie, vol );
65 }
66
67 // Scale the movie by the desired factors
68 void Scale( int w, int h )
69 {
70 // Prevent a divide by 0
71 if( w == 0 )
72 w = 1;
73 if( h == 0 )
74 h = 1;
75
76 SMPEG_scaleXY( movie, w, h );
77 CheckErrors();
78 }
79
80 // Scale the movie by the desired factor
81 void ScaleBy( int factor )
82 {
83 // Prevent a divide by 0
84 if( factor == 0 )
85 factor = 1;
86 // Make sure we don't scale larger than the surface size
87 if( factor > MaxScale )
88 factor = MaxScale;
89
90 SMPEG_scale( movie, factor );
91 CheckErrors();
92 }
93
94 // Sets the region of the video to be shown
95 void SetDisplayRegion( int x, int y, int w, int h )
96 {
97 SMPEG_setdisplayregion( movie, x, y, w, h );
98 CheckErrors();
99 }
100
101 // Check for any errors
102 void CheckErrors()
103 {
104 char* error = SMPEG_error( movie );
105 if( error ) {
106 LOG_WARNING(ExInfo("SMPEG_error")
107 .addInfo("error", error));
108 }
109 }
110
111 // Load the movie
112 void Load( const char* fileName, int maxscalex = 1, int maxscaley = 1 )
113 {
114 MaxScaleX = maxscalex;
115 MaxScaleY = maxscaley;
116
117 // Limit how much we can scale by
118 MaxScale = (maxscalex > maxscaley ? maxscaley : maxscalex);
119
120 // Load the movie and store the information about it
121 movie = SMPEG_new( fileName, &movieInfo, true );
122 CheckErrors();
123
124 SDL_Surface *screen = SDL_GetVideoSurface();
125 // Create a temporary surface to render the movie to
126 SDL_Surface *tempSurface2 = SDL_CreateRGBSurface( SDL_SWSURFACE, movieInfo.width * MaxScaleX, movieInfo.height * MaxScaleY, 32, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask );
127
128 // Now make a surface optimized for the main screen
129 movieSurface = SDL_DisplayFormat( tempSurface2 );
130
131 // Free the temporary surface
132 SDL_FreeSurface( tempSurface2 );
133
134 // Set the surface to draw to
135 SMPEG_setdisplay( movie, movieSurface, 0, 0 );
136 CheckErrors();
137
138 // Set the display region
139 SMPEG_setdisplayregion( movie, 0, 0, movieInfo.width, movieInfo.height );
140 CheckErrors();
141 }
142
143 // Set the looping of hte movie
144 void SetLoop( int val )
145 {
146 SMPEG_loop( movie, val );
147 CheckErrors();
148 }
149
150 // Play the movie
151 void Play()
152 {
153 SMPEG_play( movie );
154 CheckErrors();
155 }
156
157 // Pause the movie
158 void Pause()
159 {
160 SMPEG_pause( movie );
161 CheckErrors();
162 }
163
164 // Stops the movie, but keeps current position
165 void Stop()
166 {
167 SMPEG_stop( movie );
168 CheckErrors();
169 }
170
171 // Rewind the movie back to 0:00:00
172 void Rewind()
173 {
174 SMPEG_rewind( movie );
175 CheckErrors();
176 }
177
178 // Seek a number of bytes into the movie
179 void Seek( int bytes )
180 {
181 SMPEG_seek( movie, bytes );
182 CheckErrors();
183 }
184
185 // Skip a number of seconds
186 void Skip( float seconds )
187 {
188 SMPEG_skip( movie, seconds );
189 CheckErrors();
190 }
191
192 // Render some frame of the movie
193 void RenderFrame( int frame )
194 {
195 SMPEG_renderFrame( movie, frame );
196 CheckErrors();
197 }
198
199 // Render the final frame of the movie
200 void RenderFinal()
201 {
202 SMPEG_renderFinal( movie, movieSurface, 0, 0 );
203 CheckErrors();
204 }
205
206 // Draw the movie surface to the screen
207 virtual void drawOn(SDL_Surface *screen)
208 {
209 if (movieSurface) {
210 SDL_BlitSurface(movieSurface, NULL, screen, NULL);
211 }
212 }
213
214 // Return the current info for the movie
215 SMPEG_Info GetInfo()
216 {
217 SMPEG_getinfo( movie, &movieInfo );
218 return movieInfo;
219 }
220
221 // Get the current status of the movie, can be SMPEG_ERROR = -1, SMPEG_STOPPED, SMPEG_PLAYING
222 SMPEGstatus GetStatus()
223 {
224 return SMPEG_status(movie);
225 }
226 };
227
228 #endif
229 #endif
230