… | |
… | |
9 | use SDL; |
9 | use SDL; |
10 | use SDL::App; |
10 | use SDL::App; |
11 | use SDL::Event; |
11 | use SDL::Event; |
12 | use SDL::Surface; |
12 | use SDL::Surface; |
13 | use SDL::OpenGL; |
13 | use SDL::OpenGL; |
14 | use SDL::OpenGL::Constants; |
|
|
15 | |
14 | |
16 | use Crossfire; |
15 | use Crossfire; |
17 | use Crossfire::Client; |
|
|
18 | use Crossfire::Protocol; |
16 | use Crossfire::Protocol; |
19 | |
17 | |
20 | use Crossfire::Client::Widget; |
18 | use CFClient; |
|
|
19 | use CFClient::UI; |
|
|
20 | |
|
|
21 | our $VERSION = '0.1'; |
|
|
22 | |
|
|
23 | my $MAX_FPS = 60; |
|
|
24 | my $MIN_FPS = 5; # unused |
|
|
25 | my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
21 | |
26 | |
22 | our $FACECACHE; |
27 | our $FACECACHE; |
23 | |
|
|
24 | our $VERSION = '0.1'; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
|
|
27 | |
28 | |
28 | our $CFG; |
29 | our $CFG; |
29 | our $CONN; |
30 | our $CONN; |
|
|
31 | our $FAST; # fast, low-quality mode, possibly useful for software-rendering |
30 | |
32 | |
|
|
33 | our @SDL_MODES; |
31 | our $WIDTH; |
34 | our $WIDTH; |
32 | our $HEIGHT; |
35 | our $HEIGHT; |
33 | our $FULLSCREEN; |
36 | our $FULLSCREEN; |
34 | |
37 | |
|
|
38 | our $NOW; |
|
|
39 | |
|
|
40 | our $MAPWIDGET; |
35 | our $UIFONT; |
41 | our $FONTSIZE; |
36 | |
42 | |
37 | our $SDL_TIMER; |
43 | our $SDL_TIMER; |
|
|
44 | our $SDL_ACTIVE; |
38 | our $SDL_APP; |
45 | our $SDL_EV; |
39 | our $SDL_EV = new SDL::Event; |
|
|
40 | our %SDL_CB; |
46 | our %SDL_CB; |
41 | |
47 | |
42 | our @GL_INIT; # hooks called on every gl init |
|
|
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
48 | our $ALT_ENTER_MESSAGE; |
|
|
49 | our $STATUS_LINE; |
|
|
50 | our $DEBUG_STATUS; |
45 | |
51 | |
46 | sub init_screen { |
52 | my $last_refresh; |
47 | $SDL_APP = new SDL::App |
53 | my %ANIMATE; |
48 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
54 | my $refresh_handler; |
49 | -title => "Crossfire+ Client", |
|
|
50 | -width => $WIDTH, |
|
|
51 | -height => $HEIGHT, |
|
|
52 | -opengl => 1, |
|
|
53 | -red_size => 8, |
|
|
54 | -green_size => 8, |
|
|
55 | -blue_size => 8, |
|
|
56 | -double_buffer => 1, |
|
|
57 | -fullscreen => $FULLSCREEN, |
|
|
58 | -resizeable => 0; |
|
|
59 | |
55 | |
60 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
56 | sub status { |
|
|
57 | $STATUS_LINE->set_text ($_[0]); |
|
|
58 | my ($w, $h) = $STATUS_LINE->size_request; |
|
|
59 | $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); |
|
|
60 | } |
61 | |
61 | |
62 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
62 | sub debug { |
63 | or warn "WARNING: non-power-of-two opengl extension required"; |
63 | $DEBUG_STATUS->set_text ($_[0]); |
64 | |
64 | my ($w, $h) = $DEBUG_STATUS->size_request; |
65 | $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 |
65 | $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); |
66 | or die "TTFOpenFont: $!"; |
|
|
67 | |
|
|
68 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; |
|
|
69 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
|
|
70 | $ALT_ENTER_MESSAGE->activate; |
|
|
71 | |
|
|
72 | glClearColor 0, 0, 0, 0; |
|
|
73 | |
|
|
74 | glEnable GL_TEXTURE_2D; |
|
|
75 | glShadeModel GL_FLAT; |
|
|
76 | glDisable GL_DEPTH_TEST; |
|
|
77 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
78 | |
|
|
79 | glMatrixMode GL_PROJECTION; |
|
|
80 | glLoadIdentity; |
|
|
81 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
|
|
82 | |
|
|
83 | glMatrixMode GL_MODELVIEW; |
|
|
84 | |
|
|
85 | $_->() for @GL_INIT; |
|
|
86 | } |
66 | } |
87 | |
67 | |
88 | sub start_game { |
68 | sub start_game { |
89 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
69 | status "logging in..."; |
90 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
91 | while $SDL_EV->poll; |
|
|
92 | |
70 | |
93 | 1 |
71 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
94 | }; |
|
|
95 | |
|
|
96 | $WIDTH = $CFG->{width}; |
|
|
97 | $HEIGHT = $CFG->{height}; |
|
|
98 | $FULLSCREEN = 0; |
|
|
99 | |
|
|
100 | init_screen; |
|
|
101 | |
72 | |
102 | $CONN = new conn |
73 | $CONN = new conn |
103 | host => $CFG->{host}, |
74 | host => $CFG->{host}, |
104 | port => $CFG->{port}, |
75 | port => $CFG->{port}, |
105 | user => $CFG->{user}, |
76 | user => $CFG->{user}, |
106 | pass => $CFG->{password}; |
77 | pass => $CFG->{password}, |
|
|
78 | mapw => $mapsize, |
|
|
79 | maph => $mapsize, |
|
|
80 | ; |
|
|
81 | |
|
|
82 | status "login successful"; |
|
|
83 | |
|
|
84 | CFClient::lowdelay fileno $CONN->{fh}; |
107 | } |
85 | } |
108 | |
86 | |
109 | sub stop_game { |
87 | sub stop_game { |
|
|
88 | undef $CONN; |
|
|
89 | } |
|
|
90 | |
|
|
91 | sub config_dialog { |
|
|
92 | my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100, |
|
|
93 | child => (my $vbox = new CFClient::UI::VBox); |
|
|
94 | $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup"); |
|
|
95 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
|
|
96 | |
|
|
97 | $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); |
|
|
98 | $table->add (1, 0, my $hbox = new CFClient::UI::HBox); |
|
|
99 | |
|
|
100 | $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); |
|
|
101 | $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8); |
|
|
102 | |
|
|
103 | $mode_slider->connect (changed => sub { |
|
|
104 | my ($self, $value) = @_; |
|
|
105 | |
|
|
106 | $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value; |
|
|
107 | $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); |
|
|
108 | }); |
|
|
109 | $mode_slider->emit (changed => $mode_slider->{range}[0]); |
|
|
110 | |
|
|
111 | $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen"); |
|
|
112 | $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub { |
|
|
113 | my ($self, $value) = @_; |
|
|
114 | $CFG->{fullscreen} = $value; |
|
|
115 | }); |
|
|
116 | |
|
|
117 | $table->add (1, 2, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { |
|
|
118 | destroy_screen (); |
|
|
119 | init_screen (); |
|
|
120 | }); |
|
|
121 | |
|
|
122 | $vbox->add (new CFClient::UI::Label align => 0, text => "Server"); |
|
|
123 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
|
|
124 | $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host"); |
|
|
125 | $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host}); |
|
|
126 | |
|
|
127 | $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port"); |
|
|
128 | $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port}); |
|
|
129 | |
|
|
130 | $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); |
|
|
131 | $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user}); |
|
|
132 | |
|
|
133 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); |
|
|
134 | $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1); |
|
|
135 | |
|
|
136 | $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size"); |
|
|
137 | $table->add (1, 6, new CFClient::UI::Slider |
|
|
138 | req_w => 100, |
|
|
139 | range => [$CFG->{mapsize}, 10, 100 + 1, 1], |
|
|
140 | connect_changed => sub { |
|
|
141 | my ($self, $value) = @_; |
|
|
142 | |
|
|
143 | $CFG->{mapsize} = $self->{range}[0] = $value = int $value; |
|
|
144 | }, |
|
|
145 | ); |
|
|
146 | |
|
|
147 | $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { |
|
|
148 | start_game; |
|
|
149 | }); |
|
|
150 | |
|
|
151 | $vbox->add (my $hbox = new CFClient::UI::HBox); |
|
|
152 | |
|
|
153 | $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub { |
|
|
154 | CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
155 | status "Configuration Saved"; |
|
|
156 | }); |
|
|
157 | $CFClient::UI::TOPLEVEL->add ($dialog); |
|
|
158 | } |
|
|
159 | |
|
|
160 | sub init_screen { |
|
|
161 | ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; |
|
|
162 | $FULLSCREEN = $CFG->{fullscreen}; |
|
|
163 | |
|
|
164 | SDL::Init SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO |
|
|
165 | and die "SDL::Init failed!\n"; |
|
|
166 | |
|
|
167 | SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; |
|
|
168 | SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; |
|
|
169 | SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; |
|
|
170 | SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; |
|
|
171 | |
|
|
172 | SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; |
|
|
173 | SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; |
|
|
174 | SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; |
|
|
175 | SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; |
|
|
176 | |
|
|
177 | SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1; |
|
|
178 | SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15; |
|
|
179 | SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0; |
|
|
180 | |
|
|
181 | SDL::SetVideoMode $WIDTH, $HEIGHT, 0, |
|
|
182 | SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF |
|
|
183 | | ($FULLSCREEN ? SDL_FULLSCREEN : 0) |
|
|
184 | or die "SDL::SetVideoMode failed!\n"; |
|
|
185 | |
|
|
186 | SDL::WMSetCaption "Crossfire+ Client", "Crossfire+"; |
|
|
187 | |
|
|
188 | $SDL_EV = new SDL::Event; |
|
|
189 | $SDL_EV->set_unicode (1); |
|
|
190 | |
|
|
191 | $SDL_ACTIVE = 1; |
|
|
192 | |
|
|
193 | $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub { |
|
|
194 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
195 | while $SDL_EV->poll; |
|
|
196 | |
|
|
197 | 1 |
|
|
198 | }; |
|
|
199 | |
|
|
200 | $last_refresh = SDL::GetTicks; |
|
|
201 | |
|
|
202 | CFClient::gl_init; |
|
|
203 | |
|
|
204 | $FONTSIZE = int $HEIGHT / 40; |
|
|
205 | |
|
|
206 | ############################################################################# |
|
|
207 | |
|
|
208 | $DEBUG_STATUS = new CFClient::UI::Label padding => 0; |
|
|
209 | $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); |
|
|
210 | |
|
|
211 | $STATUS_LINE = new CFClient::UI::Label |
|
|
212 | padding => 0, |
|
|
213 | y => $HEIGHT * 49 / 50 - $FONTSIZE; |
|
|
214 | $CFClient::UI::TOPLEVEL->add ($STATUS_LINE); |
|
|
215 | |
|
|
216 | $ALT_ENTER_MESSAGE = new CFClient::UI::Label |
|
|
217 | padding => 0, |
|
|
218 | y => $HEIGHT * 49 / 50, |
|
|
219 | height => $HEIGHT / 50, |
|
|
220 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
221 | $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
222 | |
|
|
223 | $MAPWIDGET = new CFClient::UI::MapWidget; |
|
|
224 | $CFClient::UI::TOPLEVEL->add ($MAPWIDGET); |
|
|
225 | $MAPWIDGET->focus_in; |
|
|
226 | |
|
|
227 | config_dialog; |
|
|
228 | } |
|
|
229 | |
|
|
230 | sub destroy_screen { |
|
|
231 | $CFClient::UI::TOPLEVEL->{children} = []; |
110 | remove Glib::Source $SDL_TIMER; |
232 | remove Glib::Source $SDL_TIMER; |
111 | |
233 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
234 | undef $refresh_handler; |
|
|
235 | undef $SDL_ACTIVE; |
112 | undef $SDL_APP; |
236 | undef $SDL_EV; |
113 | SDL::Quit; |
237 | SDL::Quit; |
114 | } |
238 | } |
115 | |
|
|
116 | |
239 | |
117 | sub force_refresh { |
240 | sub force_refresh { |
118 | glViewport 0, 0, $WIDTH, $HEIGHT; |
241 | glViewport 0, 0, $WIDTH, $HEIGHT; |
|
|
242 | |
|
|
243 | glMatrixMode GL_PROJECTION; |
|
|
244 | glLoadIdentity; |
|
|
245 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
|
|
246 | glMatrixMode GL_MODELVIEW; |
|
|
247 | glLoadIdentity; |
|
|
248 | |
119 | glClear GL_COLOR_BUFFER_BIT; |
249 | glClear GL_COLOR_BUFFER_BIT; |
120 | |
250 | |
121 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
251 | $CFClient::UI::TOPLEVEL->draw; |
122 | |
252 | |
123 | SDL::GLSwapBuffers; |
253 | SDL::GLSwapBuffers; |
124 | } |
254 | } |
125 | |
255 | |
126 | my $refresh_handler; |
256 | my $FPS; |
127 | |
257 | |
128 | sub refresh { |
258 | sub refresh { |
129 | $refresh_handler ||= add Glib::Idle sub { |
259 | $refresh_handler ||= add Glib::Idle sub { |
|
|
260 | if ($SDL_ACTIVE) { |
|
|
261 | $NOW = SDL::GetTicks; |
|
|
262 | |
|
|
263 | if ($NOW - $last_refresh < $TICKS_PER_FRAME) { |
|
|
264 | SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); |
|
|
265 | $NOW = SDL::GetTicks; |
|
|
266 | } |
|
|
267 | |
|
|
268 | my $interval = ($NOW - $last_refresh) * 0.001; |
|
|
269 | $last_refresh = $NOW; |
|
|
270 | |
|
|
271 | if ($interval) { |
|
|
272 | $FPS ||= 1 / $interval; |
|
|
273 | $FPS = $FPS * 0.9 + (1 / $interval) * 0.1; |
|
|
274 | debug sprintf "%5.02f", $FPS; |
|
|
275 | } |
|
|
276 | |
130 | force_refresh; |
277 | force_refresh; |
|
|
278 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
279 | |
|
|
280 | if (%ANIMATE) { |
|
|
281 | 1 |
|
|
282 | } else { |
|
|
283 | undef $refresh_handler; |
|
|
284 | 0 |
|
|
285 | } |
|
|
286 | } else { |
131 | undef $refresh_handler; |
287 | undef $refresh_handler; |
|
|
288 | 0 |
132 | 0 |
289 | } |
133 | }; |
290 | }; |
|
|
291 | } |
|
|
292 | |
|
|
293 | sub animation_start { |
|
|
294 | my ($widget) = @_; |
|
|
295 | $ANIMATE{$widget} = $widget; |
|
|
296 | Scalar::Util::weaken $ANIMATE{$widget}; |
|
|
297 | |
|
|
298 | refresh; |
|
|
299 | } |
|
|
300 | |
|
|
301 | sub animation_stop { |
|
|
302 | my ($widget) = @_; |
|
|
303 | delete $ANIMATE{$widget}; |
134 | } |
304 | } |
135 | |
305 | |
136 | %SDL_CB = ( |
306 | %SDL_CB = ( |
137 | SDL_QUIT() => sub { |
307 | SDL_QUIT() => sub { |
138 | main_quit Gtk2; |
308 | main_quit Gtk2; |
… | |
… | |
144 | }, |
314 | }, |
145 | SDL_KEYDOWN() => sub { |
315 | SDL_KEYDOWN() => sub { |
146 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
316 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
147 | # alt-enter |
317 | # alt-enter |
148 | $FULLSCREEN = !$FULLSCREEN; |
318 | $FULLSCREEN = !$FULLSCREEN; |
|
|
319 | destroy_screen; |
149 | init_screen; |
320 | init_screen; |
150 | } else { |
321 | } else { |
151 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
322 | CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
152 | } |
323 | } |
153 | }, |
324 | }, |
154 | SDL_KEYUP() => sub { |
325 | SDL_KEYUP() => sub { |
155 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
326 | CFClient::UI::feed_sdl_key_up_event ($SDL_EV); |
156 | }, |
327 | }, |
157 | SDL_MOUSEMOTION() => sub { |
328 | SDL_MOUSEMOTION() => sub { |
158 | warn "sdl motion\n";#d# |
329 | CFClient::UI::feed_sdl_motion_event ($SDL_EV); |
159 | }, |
330 | }, |
160 | SDL_MOUSEBUTTONDOWN() => sub { |
331 | SDL_MOUSEBUTTONDOWN() => sub { |
161 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
332 | CFClient::UI::feed_sdl_button_down_event ($SDL_EV); |
162 | }, |
333 | }, |
163 | SDL_MOUSEBUTTONUP() => sub { |
334 | SDL_MOUSEBUTTONUP() => sub { |
164 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
335 | CFClient::UI::feed_sdl_button_up_event ($SDL_EV); |
165 | }, |
336 | }, |
166 | SDL_ACTIVEEVENT() => sub { |
337 | SDL_ACTIVEEVENT() => sub { |
167 | warn "active\n";#d# |
338 | # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
168 | }, |
339 | }, |
169 | ); |
340 | ); |
170 | |
341 | |
171 | @conn::ISA = Crossfire::Protocol::; |
342 | @conn::ISA = Crossfire::Protocol::; |
172 | |
343 | |
173 | sub conn::map_update { |
344 | sub conn::map_update { |
174 | my ($self, $dirty) = @_; |
345 | my ($self, $dirty) = @_; |
175 | |
346 | |
176 | refresh; |
347 | $MAPWIDGET->update; |
177 | } |
348 | } |
178 | |
349 | |
179 | sub conn::map_scroll { |
350 | sub conn::map_scroll { |
180 | my ($self, $dx, $dy) = @_; |
351 | my ($self, $dx, $dy) = @_; |
181 | |
352 | |
182 | refresh; |
353 | # refresh; |
183 | } |
354 | } |
184 | |
355 | |
185 | sub conn::map_clear { |
356 | sub conn::map_clear { |
186 | my ($self) = @_; |
357 | my ($self) = @_; |
187 | |
358 | |
188 | refresh; |
359 | # refresh; |
189 | } |
360 | } |
190 | |
361 | |
191 | sub conn::face_find { |
362 | sub conn::face_find { |
192 | my ($self, $face) = @_; |
363 | my ($self, $face) = @_; |
193 | |
364 | |
… | |
… | |
197 | sub conn::face_update { |
368 | sub conn::face_update { |
198 | my ($self, $face) = @_; |
369 | my ($self, $face) = @_; |
199 | |
370 | |
200 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
371 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
201 | |
372 | |
202 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
373 | $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
|
|
374 | } |
|
|
375 | |
|
|
376 | sub conn::query { |
|
|
377 | my ($self, $flags, $prompt) = @_; |
|
|
378 | |
|
|
379 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
203 | } |
380 | } |
204 | |
381 | |
205 | sub gtk_add_cfg_field { |
382 | sub gtk_add_cfg_field { |
206 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
383 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
207 | my $i = $cfg->{_i}++; |
384 | my $i = $cfg->{_i}++; |
… | |
… | |
213 | } |
390 | } |
214 | $ent->set_text ($value); |
391 | $ent->set_text ($value); |
215 | $ent->signal_connect (changed => sub { |
392 | $ent->signal_connect (changed => sub { |
216 | my ($ent) = @_; |
393 | my ($ent) = @_; |
217 | $cfg->{$key} = $ent->get_text; |
394 | $cfg->{$key} = $ent->get_text; |
|
|
395 | # TODO: Mapsize should be a slider in the game gui |
|
|
396 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
397 | $cfg->{$key} = 100; |
|
|
398 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
399 | $cfg->{$key} = 50; |
|
|
400 | } |
218 | }); |
401 | }); |
219 | } |
402 | } |
220 | |
403 | |
221 | sub run_config_dialog { |
404 | sub run_config_dialog { |
222 | my (%events) = @_; |
405 | my (%events) = @_; |
… | |
… | |
224 | my $w = Gtk2::Window->new; |
407 | my $w = Gtk2::Window->new; |
225 | |
408 | |
226 | my @cfg = ( |
409 | my @cfg = ( |
227 | [qw/Host host/], |
410 | [qw/Host host/], |
228 | [qw/Port port/], |
411 | [qw/Port port/], |
|
|
412 | [qw/Mapsize% mapsize/], |
229 | [qw/Username user/], |
413 | [qw/Username user/], |
230 | [qw/Password password/], |
414 | [qw/Password password/], |
231 | ); |
415 | ); |
232 | |
416 | |
233 | my $cfg = {}; |
417 | my $cfg = {}; |
234 | |
|
|
235 | my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
|
|
236 | my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
|
|
237 | |
418 | |
238 | $w->add (my $vb = Gtk2::VBox->new); |
419 | $w->add (my $vb = Gtk2::VBox->new); |
239 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
420 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
240 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
421 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
241 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
422 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
242 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
423 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
243 | my $i = 0; |
424 | my $i = 0; |
244 | my $act = 0; |
425 | my $act = 0; |
245 | for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
426 | for (map { "$_->[0]x$_->[1]" } @SDL_MODES) { |
246 | if ($_ eq $selmode) { $act = $i } |
427 | if ($_ eq $selmode) { $act = $i } |
247 | $cb->append_text ($_); |
428 | $cb->append_text ($_); |
248 | $i++; |
429 | $i++; |
249 | } |
430 | } |
250 | $cb->set_active ($act); |
431 | $cb->set_active ($act); |
… | |
… | |
267 | $cb->signal_connect (clicked => sub { |
448 | $cb->signal_connect (clicked => sub { |
268 | for (keys %$cfg) { |
449 | for (keys %$cfg) { |
269 | $::CFG->{$_} = $cfg->{$_} |
450 | $::CFG->{$_} = $cfg->{$_} |
270 | if $_ ne '_i'; |
451 | if $_ ne '_i'; |
271 | } |
452 | } |
272 | Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
453 | CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; |
273 | }); |
454 | }); |
274 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
455 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
275 | $cb->signal_connect (clicked => sub { |
456 | $cb->signal_connect (clicked => sub { |
276 | for (keys %$cfg) { |
457 | for (keys %$cfg) { |
277 | $::CFG->{$_} = $cfg->{$_} |
458 | $::CFG->{$_} = $cfg->{$_} |
… | |
… | |
280 | my $cb = $events{login} || sub {}; |
461 | my $cb = $events{login} || sub {}; |
281 | $cb->($::CFG->{user}, $::CFG->{password}); |
462 | $cb->($::CFG->{user}, $::CFG->{password}); |
282 | }); |
463 | }); |
283 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
464 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
284 | $cb->signal_connect (clicked => sub { |
465 | $cb->signal_connect (clicked => sub { |
285 | my $cb = $events{login} || sub {}; |
466 | my $cb = $events{logout} || sub {}; |
286 | $cb->(); |
467 | $cb->(); |
287 | }); |
468 | }); |
288 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
469 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
289 | $cb->signal_connect (clicked => sub { $w->destroy }); |
470 | $cb->signal_connect (clicked => sub { $w->destroy }); |
290 | |
471 | |
… | |
… | |
295 | |
476 | |
296 | |
477 | |
297 | ############################################################################# |
478 | ############################################################################# |
298 | |
479 | |
299 | SDL::Init SDL_INIT_EVERYTHING; |
480 | SDL::Init SDL_INIT_EVERYTHING; |
300 | SDL::TTFInit; |
|
|
301 | |
481 | |
302 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
482 | @SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)], |
|
|
483 | @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE }; |
303 | |
484 | |
304 | $mapwidget->activate; |
|
|
305 | $mapwidget->focus_in; |
|
|
306 | |
|
|
307 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
485 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
308 | |
486 | |
309 | $CFG ||= { |
487 | $CFG ||= { |
310 | width => 640, |
488 | width => 640, |
311 | height => 480, |
489 | height => 480, |
312 | fullscreen => 0, |
490 | fullscreen => 0, |
|
|
491 | sdl_mode => 0, |
|
|
492 | mapsize => 100, |
313 | host => "crossfire.schmorp.de", |
493 | host => "crossfire.schmorp.de", |
314 | port => 13327, |
494 | port => 13327, |
315 | }; |
495 | }; |
316 | |
496 | |
|
|
497 | { |
|
|
498 | my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
|
|
499 | |
|
|
500 | CFClient::add_font $_ for @fonts; |
|
|
501 | CFClient::set_font $fonts[0]; |
|
|
502 | } |
|
|
503 | |
317 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
504 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
318 | |
505 | |
319 | run_config_dialog |
506 | init_screen; |
320 | login => sub { start_game }, |
|
|
321 | logout => sub { stop_game }; |
|
|
322 | |
507 | |
323 | main Gtk2; |
508 | main Gtk2; |
324 | |
509 | |
325 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |
510 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |
|
|
511 | |