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