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 Client::Util; |
|
|
19 | use 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; |
|
|
30 | |
|
|
31 | our $WIDTH; |
|
|
32 | our $HEIGHT; |
|
|
33 | our $FULLSCREEN; |
|
|
34 | |
|
|
35 | our $FONTSIZE; |
27 | |
36 | |
28 | our $SDL_TIMER; |
37 | our $SDL_TIMER; |
29 | our $SDL_APP; |
38 | our $SDL_APP; |
30 | our $SDL_EV = new SDL::Event; |
39 | our $SDL_EV = new SDL::Event; |
31 | our %SDL_CB; |
40 | our %SDL_CB; |
32 | |
41 | |
33 | our @GL_INIT; # hooks called on every gl init |
42 | our @GL_INIT; # hooks called on every gl init |
34 | |
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
|
|
45 | |
|
|
46 | our $TOPLEVEL; |
|
|
47 | |
|
|
48 | our $tw; # Test widget #d# |
|
|
49 | |
35 | sub init_screen { |
50 | sub init_screen { |
36 | $SDL_APP = new SDL::App |
51 | $SDL_APP = new SDL::App |
37 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
52 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
38 | -title => "Crossfire+ Client", |
53 | -title => "Crossfire+ Client", |
39 | -width => $CFG->{width}, |
54 | -width => $WIDTH, |
40 | -height => $CFG->{height}, |
55 | -height => $HEIGHT, |
41 | -opengl => 1, |
56 | -opengl => 1, |
42 | -red_size => 8, |
57 | -red_size => 8, |
43 | -green_size => 8, |
58 | -green_size => 8, |
44 | -blue_size => 8, |
59 | -blue_size => 8, |
45 | -double_buffer => 1, |
60 | -double_buffer => 1, |
46 | -fullscreen => $CFG->{fullscreen}, |
61 | -fullscreen => $FULLSCREEN, |
47 | -resizeable => 0; |
62 | -resizeable => 0; |
48 | |
63 | |
|
|
64 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
|
|
65 | |
|
|
66 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
67 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
68 | |
|
|
69 | $FONTSIZE = int $HEIGHT / 50; |
|
|
70 | |
|
|
71 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
|
|
72 | 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, |
|
|
73 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
74 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
|
|
75 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
76 | |
|
|
77 | # Test code #d# |
|
|
78 | { |
|
|
79 | my $frm = new Crossfire::Client::Widget::Frame; |
|
|
80 | $tw = new Crossfire::Client::Widget::Window; |
|
|
81 | $frm->add (my $te = new Crossfire::Client::Widget::TextEntry 0, 0, 10, $FONTSIZE, "Foo in the garden!"); |
|
|
82 | $tw->add ($frm); |
|
|
83 | |
|
|
84 | $tw->move (0, $HEIGHT - 120); |
|
|
85 | $TOPLEVEL->add ($tw); |
|
|
86 | # $te->focus_in; |
|
|
87 | } |
|
|
88 | |
49 | glClearColor 0, 0, 0, 0; |
89 | glClearColor 0, 0, 0, 0; |
50 | |
90 | |
51 | glEnable GL_TEXTURE_2D; |
91 | glEnable GL_TEXTURE_2D; |
52 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
92 | glEnable GL_COLOR_MATERIAL; |
53 | glShadeModel GL_FLAT; |
93 | glShadeModel GL_FLAT; |
54 | glDisable GL_DEPTH_TEST; |
94 | glDisable GL_DEPTH_TEST; |
55 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
95 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
56 | glEnable GL_BLEND; |
96 | |
|
|
97 | $_->() for @GL_INIT; |
|
|
98 | } |
|
|
99 | |
|
|
100 | sub start_game { |
|
|
101 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
|
|
102 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
103 | while $SDL_EV->poll; |
|
|
104 | |
|
|
105 | 1 |
|
|
106 | }; |
|
|
107 | |
|
|
108 | $WIDTH = $CFG->{width}; |
|
|
109 | $HEIGHT = $CFG->{height}; |
|
|
110 | $FULLSCREEN = 0; |
|
|
111 | |
|
|
112 | init_screen; |
|
|
113 | |
|
|
114 | $CONN = new conn |
|
|
115 | host => $CFG->{host}, |
|
|
116 | port => $CFG->{port}, |
|
|
117 | user => $CFG->{user}, |
|
|
118 | pass => $CFG->{password}, |
|
|
119 | mapw => 50, |
|
|
120 | maph => 37, |
|
|
121 | ; |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub stop_game { |
|
|
125 | remove Glib::Source $SDL_TIMER; |
|
|
126 | |
|
|
127 | undef $SDL_APP; |
|
|
128 | SDL::Quit; |
|
|
129 | } |
|
|
130 | |
|
|
131 | |
|
|
132 | sub force_refresh { |
|
|
133 | glViewport 0, 0, $WIDTH, $HEIGHT; |
57 | |
134 | |
58 | glMatrixMode GL_PROJECTION; |
135 | glMatrixMode GL_PROJECTION; |
59 | glLoadIdentity; |
136 | glLoadIdentity; |
60 | glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100; |
137 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
61 | |
|
|
62 | glMatrixMode GL_MODELVIEW; |
138 | glMatrixMode GL_MODELVIEW; |
63 | |
139 | |
64 | $_->() for @GL_INIT; |
140 | glClear GL_COLOR_BUFFER_BIT; |
65 | } |
|
|
66 | |
141 | |
67 | sub start_game { |
142 | $TOPLEVEL->draw; |
68 | $SDL_TIMER = add Glib::Timeout 1000/20, sub { |
143 | |
69 | while ($SDL_EV->poll) { |
144 | SDL::GLSwapBuffers; |
70 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); |
145 | } |
|
|
146 | |
|
|
147 | my $refresh_handler; |
|
|
148 | |
|
|
149 | sub refresh { |
|
|
150 | $refresh_handler ||= add Glib::Idle sub { |
|
|
151 | force_refresh; |
|
|
152 | undef $refresh_handler; |
71 | } |
153 | 0 |
72 | |
|
|
73 | 1 |
|
|
74 | }; |
154 | }; |
75 | |
|
|
76 | init_screen; |
|
|
77 | |
|
|
78 | $CONN = new conn |
|
|
79 | host => $CFG->{host}, |
|
|
80 | port => $CFG->{port}; |
|
|
81 | } |
|
|
82 | |
|
|
83 | sub stop_game { |
|
|
84 | remove Glib::Source $SDL_TIMER; |
|
|
85 | |
|
|
86 | undef $SDL_APP; |
|
|
87 | SDL::Quit; |
|
|
88 | } |
|
|
89 | |
|
|
90 | sub refresh { |
|
|
91 | glClear GL_COLOR_BUFFER_BIT; |
|
|
92 | |
|
|
93 | for (values %Client::Widget::ACTIVE_WIDGETS) { |
|
|
94 | $_->draw |
|
|
95 | } |
|
|
96 | |
|
|
97 | SDL::GLSwapBuffers; |
|
|
98 | } |
155 | } |
99 | |
156 | |
100 | %SDL_CB = ( |
157 | %SDL_CB = ( |
101 | SDL_QUIT() => sub { |
158 | SDL_QUIT() => sub { |
102 | warn "sdl quit\n";#d# |
|
|
103 | main_quit Gtk2; |
159 | main_quit Gtk2; |
104 | }, |
160 | }, |
105 | SDL_VIDEORESIZE() => sub { |
161 | SDL_VIDEORESIZE() => sub { |
106 | }, |
162 | }, |
107 | SDL_VIDEOEXPOSE() => sub { |
163 | SDL_VIDEOEXPOSE() => sub { |
108 | refresh; |
164 | refresh; |
109 | }, |
165 | }, |
110 | SDL_KEYDOWN() => sub { |
166 | SDL_KEYDOWN() => sub { |
111 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
167 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
112 | # alt-enter |
168 | # alt-enter |
113 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
169 | $FULLSCREEN = !$FULLSCREEN; |
114 | init_screen; |
170 | init_screen; |
115 | } else { |
171 | } else { |
116 | Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
172 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
117 | } |
173 | } |
118 | }, |
174 | }, |
119 | SDL_KEYUP() => sub { |
175 | SDL_KEYUP() => sub { |
120 | Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
176 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
121 | }, |
177 | }, |
122 | SDL_MOUSEMOTION() => sub { |
178 | SDL_MOUSEMOTION() => sub { |
123 | warn "sdl motion\n";#d# |
179 | warn "sdl motion\n";#d# |
124 | }, |
180 | }, |
125 | SDL_MOUSEBUTTONDOWN() => sub { |
181 | SDL_MOUSEBUTTONDOWN() => sub { |
126 | Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
182 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
127 | }, |
183 | }, |
128 | SDL_MOUSEBUTTONUP() => sub { |
184 | SDL_MOUSEBUTTONUP() => sub { |
129 | Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
185 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
130 | }, |
186 | }, |
131 | SDL_ACTIVEEVENT() => sub { |
187 | SDL_ACTIVEEVENT() => sub { |
132 | warn "active\n";#d# |
188 | warn "active\n";#d# |
133 | }, |
189 | }, |
134 | ); |
190 | ); |
… | |
… | |
165 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
221 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
166 | |
222 | |
167 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
223 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
168 | } |
224 | } |
169 | |
225 | |
|
|
226 | sub conn::query { |
|
|
227 | my ($self, $flags, $prompt) = @_; |
|
|
228 | |
|
|
229 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
|
|
230 | } |
|
|
231 | |
|
|
232 | sub gtk_add_cfg_field { |
|
|
233 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
|
|
234 | my $i = $cfg->{_i}++; |
|
|
235 | $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
|
|
236 | $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
|
|
237 | if ($key eq 'password') { |
|
|
238 | $ent->set_invisible_char ("*"); |
|
|
239 | $ent->set (visibility => 0) |
|
|
240 | } |
|
|
241 | $ent->set_text ($value); |
|
|
242 | $ent->signal_connect (changed => sub { |
|
|
243 | my ($ent) = @_; |
|
|
244 | $cfg->{$key} = $ent->get_text; |
|
|
245 | }); |
|
|
246 | } |
|
|
247 | |
|
|
248 | sub run_config_dialog { |
|
|
249 | my (%events) = @_; |
|
|
250 | |
|
|
251 | my $w = Gtk2::Window->new; |
|
|
252 | |
|
|
253 | my @cfg = ( |
|
|
254 | [qw/Host host/], |
|
|
255 | [qw/Port port/], |
|
|
256 | [qw/Username user/], |
|
|
257 | [qw/Password password/], |
|
|
258 | ); |
|
|
259 | |
|
|
260 | my $cfg = {}; |
|
|
261 | |
|
|
262 | my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
|
|
263 | my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
|
|
264 | |
|
|
265 | $w->add (my $vb = Gtk2::VBox->new); |
|
|
266 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
|
|
267 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
|
|
268 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
|
|
269 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
|
|
270 | my $i = 0; |
|
|
271 | my $act = 0; |
|
|
272 | for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
|
|
273 | if ($_ eq $selmode) { $act = $i } |
|
|
274 | $cb->append_text ($_); |
|
|
275 | $i++; |
|
|
276 | } |
|
|
277 | $cb->set_active ($act); |
|
|
278 | $cb->signal_connect (changed => sub { |
|
|
279 | my ($cb) = @_; |
|
|
280 | my $txt = $cb->get_active_text; |
|
|
281 | if ($txt =~ m/(\d+)x(\d+)/) { |
|
|
282 | $::CFG->{width} = $1; |
|
|
283 | $::CFG->{height} = $2; |
|
|
284 | } |
|
|
285 | }); |
|
|
286 | |
|
|
287 | $cfg->{_i} = 1; |
|
|
288 | for (@cfg) { |
|
|
289 | gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
|
|
290 | } |
|
|
291 | |
|
|
292 | $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
|
|
293 | $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
|
|
294 | $cb->signal_connect (clicked => sub { |
|
|
295 | for (keys %$cfg) { |
|
|
296 | $::CFG->{$_} = $cfg->{$_} |
|
|
297 | if $_ ne '_i'; |
|
|
298 | } |
|
|
299 | Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
300 | }); |
|
|
301 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
|
|
302 | $cb->signal_connect (clicked => sub { |
|
|
303 | for (keys %$cfg) { |
|
|
304 | $::CFG->{$_} = $cfg->{$_} |
|
|
305 | if $_ ne '_i'; |
|
|
306 | } |
|
|
307 | my $cb = $events{login} || sub {}; |
|
|
308 | $cb->($::CFG->{user}, $::CFG->{password}); |
|
|
309 | }); |
|
|
310 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
|
|
311 | $cb->signal_connect (clicked => sub { |
|
|
312 | my $cb = $events{login} || sub {}; |
|
|
313 | $cb->(); |
|
|
314 | }); |
|
|
315 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
|
|
316 | $cb->signal_connect (clicked => sub { $w->destroy }); |
|
|
317 | |
|
|
318 | $w->show_all; |
|
|
319 | |
|
|
320 | $w->signal_connect (destroy => sub { Gtk2->main_quit }); |
|
|
321 | } |
|
|
322 | |
|
|
323 | |
170 | ############################################################################# |
324 | ############################################################################# |
171 | |
325 | |
|
|
326 | SDL::Init SDL_INIT_EVERYTHING; |
|
|
327 | |
|
|
328 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
|
|
329 | |
172 | my $mapwidget = Client::MapWidget->new; |
330 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
173 | |
331 | |
174 | $mapwidget->activate; |
332 | $TOPLEVEL->add ($mapwidget); |
175 | $mapwidget->focus_in; |
333 | $mapwidget->focus_in; |
176 | |
334 | |
177 | Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; |
335 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
178 | |
|
|
179 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
|
|
180 | |
336 | |
181 | $CFG ||= { |
337 | $CFG ||= { |
182 | width => 640, |
338 | width => 640, |
183 | height => 480, |
339 | height => 480, |
184 | fullscreen => 0, |
340 | fullscreen => 0, |
185 | host => "crossfire.schmorp.de", |
341 | host => "crossfire.schmorp.de", |
186 | port => 13327, |
342 | port => 13327, |
187 | }; |
343 | }; |
188 | |
344 | |
189 | Client::Util::run_config_dialog |
345 | Crossfire::Client::add_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
346 | Crossfire::Client::set_font "[Bitstream Vera Sans Mono]"; |
|
|
347 | |
|
|
348 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
|
|
349 | |
|
|
350 | run_config_dialog |
190 | login => sub { start_game }, |
351 | login => sub { start_game }, |
191 | logout => sub { stop_game }; |
352 | logout => sub { stop_game }; |
192 | |
353 | |
193 | main Gtk2; |
354 | main Gtk2; |
194 | |
355 | |