1 /* NetHack may be freely redistributed.  See license for details. */
2 
3 #include "introwin.h"
4 
5 #include <SDL.h>
6 
7 #include "vulture_win.h"
8 #include "vulture_sdl.h"
9 #include "vulture_gra.h"
10 #include "vulture_gfl.h"
11 #include "vulture_txt.h"
12 #include "vulture_mou.h"
13 #include "vulture_tile.h"
14 
introwin(window * p,std::vector<std::string> & imagenames,std::vector<std::vector<std::string>> & subtitles)15 introwin::introwin(window *p, std::vector<std::string> &imagenames, std::vector< std::vector<std::string> > &subtitles) :
16                    window(p), imagenames(imagenames), subtitles(subtitles)
17 {
18 	current_scene = -1;
19 
20 	w = vulture_screen->w;
21 	h = vulture_screen->h;
22 
23 	image = NULL;
24 	image_changed = false;
25 	starttick = 0;
26 	need_redraw = true;
27 	scenetime = 0;
28 }
29 
30 
~introwin()31 introwin::~introwin()
32 {
33 	if (image)
34 		SDL_FreeSurface(image);
35 }
36 
37 
draw()38 bool introwin::draw()
39 {
40 	int j, pos_x, pos_y, img_x, img_y;
41 	int lineheight = vulture_get_lineheight(V_FONT_INTRO);
42 
43 	vulture_set_draw_region(0, 0, w - 1, h - 1);
44 	if (image) {
45 		img_x = (w - image->w) / 2;
46 		img_y = (h - image->h) / 6;
47 
48 		if (image_changed)
49 			vulture_fade_out(0.2);
50 
51 		SDL_FillRect(vulture_screen, NULL, CLR32_BLACK);
52 		vulture_put_img(img_x, img_y, image);
53 		if (image_changed)
54 			vulture_fade_in(0.2);
55 
56 		image_changed = false;
57 
58 		for (j = 0; j < (int)subtitles[current_scene].size(); j++) {
59 			pos_x = (w - vulture_text_length(V_FONT_INTRO, subtitles[current_scene][j])) / 2;
60 			pos_y = 2 * img_y + image->h + lineheight * j;
61 			vulture_put_text(V_FONT_INTRO, subtitles[current_scene][j], vulture_screen,
62 								pos_x, pos_y, V_COLOR_INTRO_TEXT);
63 			scenetime += subtitles[current_scene][j].length() * MSEC_PER_CHAR;
64 		}
65 	} else
66 		SDL_FillRect(vulture_screen, NULL, CLR32_BLACK);
67 
68 	vulture_invalidate_region(0, 0, w, h);
69 	return false;
70 }
71 
72 
handle_timer_event(window * target,void * result,int time)73 eventresult introwin::handle_timer_event(window* target, void* result, int time)
74 {
75 	/* next scene? */
76 	if (SDL_GetTicks() - starttick > scenetime)
77 		return next_scene();
78 	return V_EVENT_HANDLED_NOREDRAW;
79 }
80 
81 
handle_mousemotion_event(window * target,void * result,int xrel,int yrel,int state)82 eventresult introwin::handle_mousemotion_event(window* target, void* result, int xrel,
83                                              int yrel, int state)
84 {
85 	/* hide cursor */
86 	vulture_set_mcursor(V_TILE_NONE);
87 
88 	/* next scene? */
89 	if (SDL_GetTicks() - starttick > scenetime)
90 		return next_scene();
91 	return V_EVENT_HANDLED_NOREDRAW;
92 }
93 
94 
handle_mousebuttonup_event(window * target,void * result,int mouse_x,int mouse_y,int button,int state)95 eventresult introwin::handle_mousebuttonup_event(window* target, void* result,
96                                 int mouse_x, int mouse_y, int button, int state)
97 {
98 	/* cancel intro */
99 	return V_EVENT_HANDLED_FINAL;
100 }
101 
102 
handle_keydown_event(window * target,void * result,int sym,int mod,int unicode)103 eventresult introwin::handle_keydown_event(window* target, void* result, int sym,
104                                            int mod, int unicode)
105 {
106 	/* cancel intro */
107 	return V_EVENT_HANDLED_FINAL;
108 }
109 
110 
handle_resize_event(window * target,void * result,int res_w,int res_h)111 eventresult introwin::handle_resize_event(window* target, void* result, int res_w, int res_h)
112 {
113 	w = res_w;
114 	h = res_h;
115 	return V_EVENT_HANDLED_REDRAW;
116 }
117 
118 
next_scene()119 eventresult introwin::next_scene()
120 {
121 	starttick = SDL_GetTicks();
122 
123 	current_scene++;
124 	scenetime = 0;
125 	if (current_scene >= (int)imagenames.size())
126 		return V_EVENT_HANDLED_FINAL;
127 
128 	if (image)
129 		SDL_FreeSurface(image);
130 
131 	image = vulture_load_graphic(imagenames[current_scene]);
132 	if (current_scene > 0 && imagenames[current_scene - 1] != imagenames[current_scene])
133 		image_changed = true;
134 
135 	need_redraw = true;
136 	return V_EVENT_HANDLED_REDRAW;
137 }
138