2 * See Licensing and Copyright notice in naev.h
9 * @brief Handles the introduction sequence.
11 * @todo Allow handling of images and other fancy things once we get them.
32 #define INTRO_FONT_SIZE 18. /**< Intro text font size. */
33 #define INTRO_SPEED 30. /**< Speed of text in characters / second. */
37 * Introduction text lines.
39 static char **intro_lines
= NULL
; /**< Introduction text lines. */
40 static int intro_nlines
= 0; /**< Number of introduction text lines. */
41 static int intro_length
= 0; /**< Length of the text. */
42 static glFont intro_font
; /**< Introduction font. */
48 static int intro_load( const char *text
);
49 static void intro_cleanup (void);
53 * @brief Loads the intro stuff.
55 static int intro_load( const char *text
)
63 intro_buf
= ndata_read( text
, &intro_size
);
64 intro_length
= intro_size
; /* Length aproximation. */
66 /* Create intro font. */
67 gl_fontInit( &intro_font
, NULL
, INTRO_FONT_SIZE
);
73 while ((uint32_t)p
< intro_size
) {
75 i
= gl_printWidthForText( &intro_font
, &intro_buf
[p
], SCREEN_W
- 200. );
80 intro_lines
= realloc( intro_lines
, sizeof(char*) * mem
);
82 intro_lines
[n
] = malloc( i
+ 1 );
83 strncpy( intro_lines
[n
], &intro_buf
[p
], i
);
84 intro_lines
[n
][i
] = '\0';
86 p
+= i
+ 1; /* Move pointer. */
99 * @brief Cleans up the intro stuff.
101 static void intro_cleanup (void)
106 for (i
=0; i
<intro_nlines
; i
++)
107 free(intro_lines
[i
]);
109 gl_freeFont(&intro_font
);
118 * @brief Displays the introduction sequence.
120 * @brief text Path of text file to use.
121 * @brief mus Type of music to use (run through music.lua).
122 * @return 0 on success.
124 int intro_display( const char *text
, const char *mus
)
127 unsigned int tcur
, tlast
;
134 /* Load the introduction. */
135 if (intro_load(text
) < 0)
138 /* Calculate velocity. */
139 density
= ((double)intro_length
/ (double)intro_nlines
); /* char / line */
140 density
/= (double)intro_font
.h
; /* char / pixel */
141 vel
= INTRO_SPEED
/ density
; /* (char / s) * (pixel / char) = pixel / s */
143 /* Change music to intro music. */
147 /* We need to clear key repeat to avoid infinite loops. */
150 /* Enable keyrepeat just for the intro. */
151 SDL_EnableKeyRepeat( conf
.repeat_delay
, conf
.repeat_freq
);
153 /* Prepare for intro loop. */
156 tlast
= SDL_GetTicks();
158 max
= intro_nlines
+ SCREEN_H
/ ((intro_font
.h
+ 5.));
162 /* Get delta tick in seconds. */
163 tcur
= SDL_GetTicks();
164 dt
= (double)(tcur
- tlast
) / 1000.;
168 while (SDL_PollEvent(&event
)) {
169 switch (event
.type
) {
172 /* Escape skips directly. */
173 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
) {
178 /* Up arrow decreases speed. */
179 else if (event
.key
.keysym
.sym
== SDLK_UP
) {
184 /* Down arrow increases speed. */
185 else if (event
.key
.keysym
.sym
== SDLK_DOWN
) {
189 /* Pageup and backspace jump up. */
190 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
|| event
.key
.keysym
.sym
== SDLK_BACKSPACE
) {
195 /* Pagedown and space jump down. */
196 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
|| event
.key
.keysym
.sym
== SDLK_SPACE
) {
201 /* User is clearly flailing on keyboard. */
206 /* Purpose fallthrough. */
207 case SDL_JOYBUTTONDOWN
:
214 /* Increment position. */
218 glClear(GL_COLOR_BUFFER_BIT
);
221 i
= (int)(offset
/ (intro_font
.h
+ 5.));
222 if (i
> max
) /* Out of lines. */
224 y
= offset
- (i
+1) * (intro_font
.h
+ 5.);
228 /* Skip in line isn't valid. */
229 if (i
>= intro_nlines
) {
231 y
+= intro_font
.h
+ 5.;
235 gl_print( &intro_font
, x
, y
, &cConsole
, intro_lines
[i
] );
237 /* Increment line and position. */
239 y
+= intro_font
.h
+ 5.;
242 /* Only thing we actually care about updating is music. */
246 SDL_GL_SwapBuffers();
248 SDL_Delay(10); /* No need to burn CPU. */
251 /* Disable intro's key repeat. */
252 SDL_EnableKeyRepeat( 0, 0 );
254 /* Stop music, normal music will start shortly after. */
257 /* Clean up after the introduction. */