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