… | |
… | |
15 | |
15 | |
16 | use Crossfire; |
16 | use Crossfire; |
17 | use Crossfire::Client; |
17 | use Crossfire::Client; |
18 | use Crossfire::Protocol; |
18 | use Crossfire::Protocol; |
19 | |
19 | |
20 | use Crossfire::Client::Util; |
|
|
21 | use Crossfire::Client::Widget; |
20 | use Crossfire::Client::Widget; |
22 | |
21 | |
23 | our $FACECACHE; |
22 | our $FACECACHE; |
24 | |
23 | |
25 | our $VERSION = '0.1'; |
24 | our $VERSION = '0.1'; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
26 | |
27 | |
27 | our $CFG; |
28 | our $CFG; |
28 | our $CONN; |
29 | our $CONN; |
29 | |
30 | |
|
|
31 | our $WIDTH; |
|
|
32 | our $HEIGHT; |
|
|
33 | our $FULLSCREEN; |
|
|
34 | |
30 | our $UIFONT; |
35 | our $FONTSIZE; |
31 | |
36 | |
32 | our $SDL_TIMER; |
37 | our $SDL_TIMER; |
33 | our $SDL_APP; |
38 | our $SDL_APP; |
34 | our $SDL_EV = new SDL::Event; |
39 | our $SDL_EV = new SDL::Event; |
35 | our %SDL_CB; |
40 | our %SDL_CB; |
36 | |
41 | |
37 | our @GL_INIT; # hooks called on every gl init |
42 | our @GL_INIT; # hooks called on every gl init |
38 | |
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
|
|
45 | our $STATUS_LINE; |
|
|
46 | |
|
|
47 | our $TOPLEVEL; |
|
|
48 | |
|
|
49 | our $tw; # Test widget #d# |
|
|
50 | |
|
|
51 | my $last_refresh; |
|
|
52 | my %ANIMATE; |
|
|
53 | my $refresh_handler; |
|
|
54 | |
39 | sub init_screen { |
55 | sub init_screen { |
40 | $SDL_APP = new SDL::App |
56 | $SDL_APP = new SDL::App |
41 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
57 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
42 | -title => "Crossfire+ Client", |
58 | -title => "Crossfire+ Client", |
43 | -width => $CFG->{width}, |
59 | -width => $WIDTH, |
44 | -height => $CFG->{height}, |
60 | -height => $HEIGHT, |
45 | -opengl => 1, |
61 | -opengl => 1, |
46 | -red_size => 8, |
62 | -red_size => 5, |
47 | -green_size => 8, |
63 | -green_size => 5, |
48 | -blue_size => 8, |
64 | -blue_size => 5, |
|
|
65 | -alpha_size => 0, |
49 | -double_buffer => 1, |
66 | -double_buffer => 1, |
50 | -fullscreen => $CFG->{fullscreen}, |
67 | -fullscreen => $FULLSCREEN, |
51 | -resizeable => 0; |
68 | -resizeable => 0; |
52 | |
69 | |
|
|
70 | $last_refresh = SDL::GetTicks; |
|
|
71 | |
|
|
72 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
|
|
73 | |
|
|
74 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
75 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
76 | |
|
|
77 | $FONTSIZE = int $HEIGHT / 50; |
|
|
78 | |
|
|
79 | ############################################################################# |
|
|
80 | |
53 | glClearColor 0, 0, 0, 0; |
81 | glClearColor 0, 0, 0, 0; |
54 | |
82 | |
55 | glEnable GL_TEXTURE_2D; |
83 | glEnable GL_TEXTURE_2D; |
|
|
84 | glEnable GL_COLOR_MATERIAL; |
56 | glShadeModel GL_FLAT; |
85 | glShadeModel GL_FLAT; |
57 | glDisable GL_DEPTH_TEST; |
86 | glDisable GL_DEPTH_TEST; |
58 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
87 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
59 | |
88 | |
|
|
89 | $_->() for @GL_INIT; |
|
|
90 | |
|
|
91 | ############################################################################# |
|
|
92 | |
|
|
93 | $STATUS_LINE = new Crossfire::Client::Widget::Label |
|
|
94 | 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
|
|
95 | ""; |
|
|
96 | $TOPLEVEL->add ($STATUS_LINE); |
|
|
97 | |
|
|
98 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
|
|
99 | 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, |
|
|
100 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
101 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
102 | |
|
|
103 | # Test code #d# |
|
|
104 | unless ($tw) { # haha... |
|
|
105 | $tw = new Crossfire::Client::Widget::Animator; |
|
|
106 | my $lbl1 = new Crossfire::Client::Widget::Label |
|
|
107 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
|
|
108 | my $lbl2 = new Crossfire::Client::Widget::Label |
|
|
109 | 0, 0, 10, $FONTSIZE, "LBL2"; |
|
|
110 | |
|
|
111 | my $vb = new Crossfire::Client::Widget::VBox; |
|
|
112 | my $f = new Crossfire::Client::Widget::FancyFrame; |
|
|
113 | my $f2 = new Crossfire::Client::Widget::FancyFrame; |
|
|
114 | $f->add ($lbl1); |
|
|
115 | $f2->add ($lbl2); |
|
|
116 | $vb->add ($f); |
|
|
117 | $vb->add ($f2, 1); |
|
|
118 | |
|
|
119 | $tw->add ($vb); |
|
|
120 | $tw->w (400); |
|
|
121 | $tw->h (300); |
|
|
122 | $tw->move ($WIDTH - 200, 0); |
|
|
123 | $tw->moveto (0, 0); |
|
|
124 | $TOPLEVEL->add ($tw); |
|
|
125 | |
|
|
126 | # $f->move ($WIDTH - 200, 0); |
|
|
127 | # $TOPLEVEL->add ($f); |
|
|
128 | } |
|
|
129 | } |
|
|
130 | |
|
|
131 | sub start_game { |
|
|
132 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
|
|
133 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
134 | while $SDL_EV->poll; |
|
|
135 | |
|
|
136 | 1 |
|
|
137 | }; |
|
|
138 | |
|
|
139 | $WIDTH = $CFG->{width}; |
|
|
140 | $HEIGHT = $CFG->{height}; |
|
|
141 | $FULLSCREEN = 0; |
|
|
142 | |
|
|
143 | init_screen; |
|
|
144 | |
|
|
145 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
|
|
146 | |
|
|
147 | $CONN = new conn |
|
|
148 | host => $CFG->{host}, |
|
|
149 | port => $CFG->{port}, |
|
|
150 | user => $CFG->{user}, |
|
|
151 | pass => $CFG->{password}, |
|
|
152 | mapw => $mapsize, |
|
|
153 | maph => $mapsize, |
|
|
154 | ; |
|
|
155 | |
|
|
156 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
|
|
157 | } |
|
|
158 | |
|
|
159 | sub stop_game { |
|
|
160 | remove Glib::Source $SDL_TIMER; |
|
|
161 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
162 | undef $refresh_handler; |
|
|
163 | |
|
|
164 | undef $SDL_APP; |
|
|
165 | undef $CONN; |
|
|
166 | SDL::Quit; |
|
|
167 | } |
|
|
168 | |
|
|
169 | |
|
|
170 | sub force_refresh { |
|
|
171 | glViewport 0, 0, $WIDTH, $HEIGHT; |
|
|
172 | |
60 | glMatrixMode GL_PROJECTION; |
173 | glMatrixMode GL_PROJECTION; |
61 | glLoadIdentity; |
174 | glLoadIdentity; |
62 | glOrtho 0, $CFG->{width}, $CFG->{height}, 0, -100 , 100; |
175 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
63 | |
|
|
64 | glMatrixMode GL_MODELVIEW; |
176 | glMatrixMode GL_MODELVIEW; |
65 | |
177 | |
66 | $_->() for @GL_INIT; |
178 | glClear GL_COLOR_BUFFER_BIT; |
67 | } |
|
|
68 | |
179 | |
69 | my $label;#d# |
180 | $TOPLEVEL->draw; |
70 | |
181 | |
71 | sub start_game { |
182 | SDL::GLSwapBuffers; |
72 | $SDL_TIMER = add Glib::Timeout 1000/20, sub { |
183 | } |
73 | while ($SDL_EV->poll) { |
184 | |
74 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); |
185 | sub refresh { |
|
|
186 | $refresh_handler ||= add Glib::Idle sub { |
|
|
187 | return unless $SDL_APP; |
|
|
188 | |
|
|
189 | my $next_refresh = SDL::GetTicks; |
|
|
190 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
|
|
191 | $last_refresh = $next_refresh; |
|
|
192 | |
|
|
193 | force_refresh; |
|
|
194 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
195 | |
|
|
196 | if (%ANIMATE) { |
|
|
197 | 1 |
|
|
198 | } else { |
|
|
199 | undef $refresh_handler; |
|
|
200 | 0 |
75 | } |
201 | } |
76 | |
|
|
77 | 1 |
|
|
78 | }; |
202 | }; |
79 | |
|
|
80 | init_screen; |
|
|
81 | |
|
|
82 | $label = new Crossfire::Client::Widget::Label 500, 10, 1, $UIFONT, "Testü[]"; |
|
|
83 | $label->activate; |
|
|
84 | |
|
|
85 | $CONN = new conn |
|
|
86 | host => $CFG->{host}, |
|
|
87 | port => $CFG->{port}; |
|
|
88 | } |
203 | } |
89 | |
204 | |
90 | sub stop_game { |
205 | sub animation_start { |
91 | remove Glib::Source $SDL_TIMER; |
206 | my ($widget) = @_; |
|
|
207 | $ANIMATE{$widget} = $widget; |
|
|
208 | Scalar::Util::weaken $ANIMATE{$widget}; |
92 | |
209 | |
93 | undef $SDL_APP; |
210 | refresh; |
94 | SDL::Quit; |
|
|
95 | } |
211 | } |
96 | |
212 | |
97 | sub refresh { |
213 | sub animation_stop { |
98 | glClear GL_COLOR_BUFFER_BIT; |
214 | my ($widget) = @_; |
99 | |
215 | delete $ANIMATE{$widget}; |
100 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
|
|
101 | |
|
|
102 | SDL::GLSwapBuffers; |
|
|
103 | } |
216 | } |
104 | |
217 | |
105 | %SDL_CB = ( |
218 | %SDL_CB = ( |
106 | SDL_QUIT() => sub { |
219 | SDL_QUIT() => sub { |
107 | main_quit Gtk2; |
220 | main_quit Gtk2; |
… | |
… | |
112 | refresh; |
225 | refresh; |
113 | }, |
226 | }, |
114 | SDL_KEYDOWN() => sub { |
227 | SDL_KEYDOWN() => sub { |
115 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
228 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
116 | # alt-enter |
229 | # alt-enter |
117 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
230 | $FULLSCREEN = !$FULLSCREEN; |
118 | init_screen; |
231 | init_screen; |
119 | } else { |
232 | } else { |
120 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
233 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
121 | } |
234 | } |
122 | }, |
235 | }, |
123 | SDL_KEYUP() => sub { |
236 | SDL_KEYUP() => sub { |
124 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
237 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
125 | }, |
238 | }, |
126 | SDL_MOUSEMOTION() => sub { |
239 | SDL_MOUSEMOTION() => sub { |
127 | warn "sdl motion\n";#d# |
240 | my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); |
|
|
241 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
|
|
242 | |
|
|
243 | warn "mouse $x, $y = $widget\n"; |
128 | }, |
244 | }, |
129 | SDL_MOUSEBUTTONDOWN() => sub { |
245 | SDL_MOUSEBUTTONDOWN() => sub { |
130 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
246 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
131 | }, |
247 | }, |
132 | SDL_MOUSEBUTTONUP() => sub { |
248 | SDL_MOUSEBUTTONUP() => sub { |
… | |
… | |
146 | } |
262 | } |
147 | |
263 | |
148 | sub conn::map_scroll { |
264 | sub conn::map_scroll { |
149 | my ($self, $dx, $dy) = @_; |
265 | my ($self, $dx, $dy) = @_; |
150 | |
266 | |
151 | refresh; |
267 | # refresh; |
152 | } |
268 | } |
153 | |
269 | |
154 | sub conn::map_clear { |
270 | sub conn::map_clear { |
155 | my ($self) = @_; |
271 | my ($self) = @_; |
156 | |
272 | |
157 | refresh; |
273 | # refresh; |
158 | } |
274 | } |
159 | |
275 | |
160 | sub conn::face_find { |
276 | sub conn::face_find { |
161 | my ($self, $face) = @_; |
277 | my ($self, $face) = @_; |
162 | |
278 | |
… | |
… | |
167 | my ($self, $face) = @_; |
283 | my ($self, $face) = @_; |
168 | |
284 | |
169 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
285 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
170 | |
286 | |
171 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
287 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
|
|
288 | } |
|
|
289 | |
|
|
290 | sub conn::query { |
|
|
291 | my ($self, $flags, $prompt) = @_; |
|
|
292 | |
|
|
293 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
172 | } |
294 | } |
173 | |
295 | |
174 | sub gtk_add_cfg_field { |
296 | sub gtk_add_cfg_field { |
175 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
297 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
176 | my $i = $cfg->{_i}++; |
298 | my $i = $cfg->{_i}++; |
… | |
… | |
182 | } |
304 | } |
183 | $ent->set_text ($value); |
305 | $ent->set_text ($value); |
184 | $ent->signal_connect (changed => sub { |
306 | $ent->signal_connect (changed => sub { |
185 | my ($ent) = @_; |
307 | my ($ent) = @_; |
186 | $cfg->{$key} = $ent->get_text; |
308 | $cfg->{$key} = $ent->get_text; |
|
|
309 | # TODO: Mapsize should be a slider in the game gui |
|
|
310 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
311 | $cfg->{$key} = 100; |
|
|
312 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
313 | $cfg->{$key} = 50; |
|
|
314 | } |
187 | }); |
315 | }); |
188 | } |
316 | } |
189 | |
317 | |
190 | sub run_config_dialog { |
318 | sub run_config_dialog { |
191 | my (%events) = @_; |
319 | my (%events) = @_; |
… | |
… | |
193 | my $w = Gtk2::Window->new; |
321 | my $w = Gtk2::Window->new; |
194 | |
322 | |
195 | my @cfg = ( |
323 | my @cfg = ( |
196 | [qw/Host host/], |
324 | [qw/Host host/], |
197 | [qw/Port port/], |
325 | [qw/Port port/], |
|
|
326 | [qw/Mapsize% mapsize/], |
198 | [qw/Username user/], |
327 | [qw/Username user/], |
199 | [qw/Password password/], |
328 | [qw/Password password/], |
200 | ); |
329 | ); |
201 | |
330 | |
202 | my $cfg = {}; |
331 | my $cfg = {}; |
… | |
… | |
249 | my $cb = $events{login} || sub {}; |
378 | my $cb = $events{login} || sub {}; |
250 | $cb->($::CFG->{user}, $::CFG->{password}); |
379 | $cb->($::CFG->{user}, $::CFG->{password}); |
251 | }); |
380 | }); |
252 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
381 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
253 | $cb->signal_connect (clicked => sub { |
382 | $cb->signal_connect (clicked => sub { |
254 | my $cb = $events{login} || sub {}; |
383 | my $cb = $events{logout} || sub {}; |
255 | $cb->(); |
384 | $cb->(); |
256 | }); |
385 | }); |
257 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
386 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
258 | $cb->signal_connect (clicked => sub { $w->destroy }); |
387 | $cb->signal_connect (clicked => sub { $w->destroy }); |
259 | |
388 | |
… | |
… | |
265 | |
394 | |
266 | ############################################################################# |
395 | ############################################################################# |
267 | |
396 | |
268 | SDL::Init SDL_INIT_EVERYTHING; |
397 | SDL::Init SDL_INIT_EVERYTHING; |
269 | |
398 | |
|
|
399 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
|
|
400 | |
270 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
401 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
271 | |
402 | |
272 | $mapwidget->activate; |
403 | $TOPLEVEL->add ($mapwidget); |
273 | $mapwidget->focus_in; |
404 | $mapwidget->focus_in; |
274 | |
405 | |
275 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
406 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
276 | |
407 | |
277 | $CFG ||= { |
408 | $CFG ||= { |
278 | width => 640, |
409 | width => 640, |
279 | height => 480, |
410 | height => 480, |
|
|
411 | mapsize => 100, |
280 | fullscreen => 0, |
412 | fullscreen => 0, |
281 | host => "crossfire.schmorp.de", |
413 | host => "crossfire.schmorp.de", |
282 | port => 13327, |
414 | port => 13327, |
283 | }; |
415 | }; |
284 | |
416 | |
|
|
417 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
418 | |
285 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
419 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
286 | |
|
|
287 | SDL::TTFInit; |
|
|
288 | |
|
|
289 | $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", 24 |
|
|
290 | or die "TTFOpenFont: $!"; |
|
|
291 | |
420 | |
292 | run_config_dialog |
421 | run_config_dialog |
293 | login => sub { start_game }, |
422 | login => sub { start_game }, |
294 | logout => sub { stop_game }; |
423 | logout => sub { stop_game }; |
295 | |
424 | |