… | |
… | |
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 | $STATUS_LINE = new Crossfire::Client::Widget::Label |
|
|
80 | 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
|
|
81 | ""; |
|
|
82 | $TOPLEVEL->add ($STATUS_LINE); |
|
|
83 | |
|
|
84 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
|
|
85 | 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, |
|
|
86 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
87 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
88 | |
|
|
89 | # Test code #d# |
|
|
90 | unless ($tw) { # haha... |
|
|
91 | $tw = new Crossfire::Client::Widget::Animator; |
|
|
92 | my $lbl1 = new Crossfire::Client::Widget::Label |
|
|
93 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
|
|
94 | my $lbl2 = new Crossfire::Client::Widget::Label |
|
|
95 | 0, 0, 10, $FONTSIZE, "LBL2"; |
|
|
96 | |
|
|
97 | my $vb = new Crossfire::Client::Widget::VBox; |
|
|
98 | my $f = new Crossfire::Client::Widget::FancyFrame; |
|
|
99 | my $f2 = new Crossfire::Client::Widget::FancyFrame; |
|
|
100 | $f->add ($lbl1); |
|
|
101 | $f2->add ($lbl2); |
|
|
102 | $vb->add ($f); |
|
|
103 | $vb->add ($f2, 1); |
|
|
104 | |
|
|
105 | $tw->add ($vb); |
|
|
106 | $tw->w (400); |
|
|
107 | $tw->h (300); |
|
|
108 | $tw->move ($WIDTH - 200, 0); |
|
|
109 | $tw->moveto (0, 0); |
|
|
110 | $TOPLEVEL->add ($tw); |
|
|
111 | |
|
|
112 | # $f->move ($WIDTH - 200, 0); |
|
|
113 | # $TOPLEVEL->add ($f); |
|
|
114 | } |
|
|
115 | |
53 | glClearColor 0, 0, 0, 0; |
116 | glClearColor 0, 0, 0, 0; |
54 | |
117 | |
55 | glEnable GL_TEXTURE_2D; |
118 | glEnable GL_TEXTURE_2D; |
|
|
119 | glEnable GL_COLOR_MATERIAL; |
56 | glShadeModel GL_FLAT; |
120 | glShadeModel GL_FLAT; |
57 | glDisable GL_DEPTH_TEST; |
121 | glDisable GL_DEPTH_TEST; |
58 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
122 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
59 | |
123 | |
|
|
124 | $_->() for @GL_INIT; |
|
|
125 | } |
|
|
126 | |
|
|
127 | sub start_game { |
|
|
128 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
|
|
129 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
130 | while $SDL_EV->poll; |
|
|
131 | |
|
|
132 | 1 |
|
|
133 | }; |
|
|
134 | |
|
|
135 | $WIDTH = $CFG->{width}; |
|
|
136 | $HEIGHT = $CFG->{height}; |
|
|
137 | $FULLSCREEN = 0; |
|
|
138 | |
|
|
139 | init_screen; |
|
|
140 | |
|
|
141 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
|
|
142 | |
|
|
143 | $CONN = new conn |
|
|
144 | host => $CFG->{host}, |
|
|
145 | port => $CFG->{port}, |
|
|
146 | user => $CFG->{user}, |
|
|
147 | pass => $CFG->{password}, |
|
|
148 | mapw => $mapsize, |
|
|
149 | maph => $mapsize, |
|
|
150 | ; |
|
|
151 | |
|
|
152 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
|
|
153 | } |
|
|
154 | |
|
|
155 | sub stop_game { |
|
|
156 | remove Glib::Source $SDL_TIMER; |
|
|
157 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
158 | undef $refresh_handler; |
|
|
159 | |
|
|
160 | undef $SDL_APP; |
|
|
161 | undef $CONN; |
|
|
162 | SDL::Quit; |
|
|
163 | } |
|
|
164 | |
|
|
165 | |
|
|
166 | sub force_refresh { |
|
|
167 | glViewport 0, 0, $WIDTH, $HEIGHT; |
|
|
168 | |
60 | glMatrixMode GL_PROJECTION; |
169 | glMatrixMode GL_PROJECTION; |
61 | glLoadIdentity; |
170 | glLoadIdentity; |
62 | glOrtho 0, $CFG->{width}, $CFG->{height}, 0, -100 , 100; |
171 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
63 | |
|
|
64 | glMatrixMode GL_MODELVIEW; |
172 | glMatrixMode GL_MODELVIEW; |
65 | |
173 | |
66 | $_->() for @GL_INIT; |
174 | glClear GL_COLOR_BUFFER_BIT; |
67 | } |
|
|
68 | |
175 | |
69 | my $label;#d# |
176 | $TOPLEVEL->draw; |
70 | |
177 | |
71 | sub start_game { |
178 | SDL::GLSwapBuffers; |
72 | $SDL_TIMER = add Glib::Timeout 1000/20, sub { |
179 | } |
73 | while ($SDL_EV->poll) { |
180 | |
74 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); |
181 | sub refresh { |
|
|
182 | $refresh_handler ||= add Glib::Idle sub { |
|
|
183 | return unless $SDL_APP; |
|
|
184 | |
|
|
185 | my $next_refresh = SDL::GetTicks; |
|
|
186 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
|
|
187 | $last_refresh = $next_refresh; |
|
|
188 | |
|
|
189 | force_refresh; |
|
|
190 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
191 | |
|
|
192 | if (%ANIMATE) { |
|
|
193 | 1 |
|
|
194 | } else { |
|
|
195 | undef $refresh_handler; |
|
|
196 | 0 |
75 | } |
197 | } |
76 | |
|
|
77 | 1 |
|
|
78 | }; |
198 | }; |
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 | } |
199 | } |
89 | |
200 | |
90 | sub stop_game { |
201 | sub animation_start { |
91 | remove Glib::Source $SDL_TIMER; |
202 | my ($widget) = @_; |
|
|
203 | $ANIMATE{$widget} = $widget; |
|
|
204 | Scalar::Util::weaken $ANIMATE{$widget}; |
92 | |
205 | |
93 | undef $SDL_APP; |
206 | refresh; |
94 | SDL::Quit; |
|
|
95 | } |
207 | } |
96 | |
208 | |
97 | sub refresh { |
209 | sub animation_stop { |
98 | glClear GL_COLOR_BUFFER_BIT; |
210 | my ($widget) = @_; |
99 | |
211 | delete $ANIMATE{$widget}; |
100 | $_->draw for values %Crossfire::Client::Widget::ACTIVE_WIDGETS; |
|
|
101 | |
|
|
102 | SDL::GLSwapBuffers; |
|
|
103 | } |
212 | } |
104 | |
213 | |
105 | %SDL_CB = ( |
214 | %SDL_CB = ( |
106 | SDL_QUIT() => sub { |
215 | SDL_QUIT() => sub { |
107 | main_quit Gtk2; |
216 | main_quit Gtk2; |
… | |
… | |
112 | refresh; |
221 | refresh; |
113 | }, |
222 | }, |
114 | SDL_KEYDOWN() => sub { |
223 | SDL_KEYDOWN() => sub { |
115 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
224 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
116 | # alt-enter |
225 | # alt-enter |
117 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
226 | $FULLSCREEN = !$FULLSCREEN; |
118 | init_screen; |
227 | init_screen; |
119 | } else { |
228 | } else { |
120 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
229 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
121 | } |
230 | } |
122 | }, |
231 | }, |
… | |
… | |
146 | } |
255 | } |
147 | |
256 | |
148 | sub conn::map_scroll { |
257 | sub conn::map_scroll { |
149 | my ($self, $dx, $dy) = @_; |
258 | my ($self, $dx, $dy) = @_; |
150 | |
259 | |
151 | refresh; |
260 | # refresh; |
152 | } |
261 | } |
153 | |
262 | |
154 | sub conn::map_clear { |
263 | sub conn::map_clear { |
155 | my ($self) = @_; |
264 | my ($self) = @_; |
156 | |
265 | |
157 | refresh; |
266 | # refresh; |
158 | } |
267 | } |
159 | |
268 | |
160 | sub conn::face_find { |
269 | sub conn::face_find { |
161 | my ($self, $face) = @_; |
270 | my ($self, $face) = @_; |
162 | |
271 | |
… | |
… | |
167 | my ($self, $face) = @_; |
276 | my ($self, $face) = @_; |
168 | |
277 | |
169 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
278 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
170 | |
279 | |
171 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
280 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
|
|
281 | } |
|
|
282 | |
|
|
283 | sub conn::query { |
|
|
284 | my ($self, $flags, $prompt) = @_; |
|
|
285 | |
|
|
286 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
172 | } |
287 | } |
173 | |
288 | |
174 | sub gtk_add_cfg_field { |
289 | sub gtk_add_cfg_field { |
175 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
290 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
176 | my $i = $cfg->{_i}++; |
291 | my $i = $cfg->{_i}++; |
… | |
… | |
182 | } |
297 | } |
183 | $ent->set_text ($value); |
298 | $ent->set_text ($value); |
184 | $ent->signal_connect (changed => sub { |
299 | $ent->signal_connect (changed => sub { |
185 | my ($ent) = @_; |
300 | my ($ent) = @_; |
186 | $cfg->{$key} = $ent->get_text; |
301 | $cfg->{$key} = $ent->get_text; |
|
|
302 | # TODO: Mapsize should be a slider in the game gui |
|
|
303 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
304 | $cfg->{$key} = 100; |
|
|
305 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
306 | $cfg->{$key} = 50; |
|
|
307 | } |
187 | }); |
308 | }); |
188 | } |
309 | } |
189 | |
310 | |
190 | sub run_config_dialog { |
311 | sub run_config_dialog { |
191 | my (%events) = @_; |
312 | my (%events) = @_; |
… | |
… | |
193 | my $w = Gtk2::Window->new; |
314 | my $w = Gtk2::Window->new; |
194 | |
315 | |
195 | my @cfg = ( |
316 | my @cfg = ( |
196 | [qw/Host host/], |
317 | [qw/Host host/], |
197 | [qw/Port port/], |
318 | [qw/Port port/], |
|
|
319 | [qw/Mapsize% mapsize/], |
198 | [qw/Username user/], |
320 | [qw/Username user/], |
199 | [qw/Password password/], |
321 | [qw/Password password/], |
200 | ); |
322 | ); |
201 | |
323 | |
202 | my $cfg = {}; |
324 | my $cfg = {}; |
… | |
… | |
249 | my $cb = $events{login} || sub {}; |
371 | my $cb = $events{login} || sub {}; |
250 | $cb->($::CFG->{user}, $::CFG->{password}); |
372 | $cb->($::CFG->{user}, $::CFG->{password}); |
251 | }); |
373 | }); |
252 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
374 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
253 | $cb->signal_connect (clicked => sub { |
375 | $cb->signal_connect (clicked => sub { |
254 | my $cb = $events{login} || sub {}; |
376 | my $cb = $events{logout} || sub {}; |
255 | $cb->(); |
377 | $cb->(); |
256 | }); |
378 | }); |
257 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
379 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
258 | $cb->signal_connect (clicked => sub { $w->destroy }); |
380 | $cb->signal_connect (clicked => sub { $w->destroy }); |
259 | |
381 | |
… | |
… | |
265 | |
387 | |
266 | ############################################################################# |
388 | ############################################################################# |
267 | |
389 | |
268 | SDL::Init SDL_INIT_EVERYTHING; |
390 | SDL::Init SDL_INIT_EVERYTHING; |
269 | |
391 | |
|
|
392 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
|
|
393 | |
270 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
394 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
271 | |
395 | |
272 | $mapwidget->activate; |
396 | $TOPLEVEL->add ($mapwidget); |
273 | $mapwidget->focus_in; |
397 | $mapwidget->focus_in; |
274 | |
398 | |
275 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
399 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
276 | |
400 | |
277 | $CFG ||= { |
401 | $CFG ||= { |
278 | width => 640, |
402 | width => 640, |
279 | height => 480, |
403 | height => 480, |
|
|
404 | mapsize => 100, |
280 | fullscreen => 0, |
405 | fullscreen => 0, |
281 | host => "crossfire.schmorp.de", |
406 | host => "crossfire.schmorp.de", |
282 | port => 13327, |
407 | port => 13327, |
283 | }; |
408 | }; |
284 | |
409 | |
|
|
410 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
411 | |
285 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
412 | $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 | |
413 | |
292 | run_config_dialog |
414 | run_config_dialog |
293 | login => sub { start_game }, |
415 | login => sub { start_game }, |
294 | logout => sub { stop_game }; |
416 | logout => sub { stop_game }; |
295 | |
417 | |