… | |
… | |
30 | |
30 | |
31 | our $WIDTH; |
31 | our $WIDTH; |
32 | our $HEIGHT; |
32 | our $HEIGHT; |
33 | our $FULLSCREEN; |
33 | our $FULLSCREEN; |
34 | |
34 | |
35 | our $UIFONT; |
35 | our $FONTSIZE; |
36 | |
36 | |
37 | our $SDL_TIMER; |
37 | our $SDL_TIMER; |
38 | our $SDL_APP; |
38 | our $SDL_APP; |
39 | our $SDL_EV = new SDL::Event; |
39 | our $SDL_EV = new SDL::Event; |
40 | our %SDL_CB; |
40 | our %SDL_CB; |
41 | |
41 | |
42 | our @GL_INIT; # hooks called on every gl init |
42 | our @GL_INIT; # hooks called on every gl init |
43 | |
43 | |
44 | our $ALT_ENTER_MESSAGE; |
44 | our $ALT_ENTER_MESSAGE; |
45 | |
45 | |
46 | our $tw; |
46 | our $TOPLEVEL; |
|
|
47 | |
|
|
48 | our $tw; # Test widget #d# |
|
|
49 | |
|
|
50 | my $last_refresh; |
|
|
51 | my %ANIMATE; |
|
|
52 | my $refresh_handler; |
47 | |
53 | |
48 | sub init_screen { |
54 | sub init_screen { |
49 | $SDL_APP = new SDL::App |
55 | $SDL_APP = new SDL::App |
50 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
56 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
51 | -title => "Crossfire+ Client", |
57 | -title => "Crossfire+ Client", |
… | |
… | |
57 | -blue_size => 8, |
63 | -blue_size => 8, |
58 | -double_buffer => 1, |
64 | -double_buffer => 1, |
59 | -fullscreen => $FULLSCREEN, |
65 | -fullscreen => $FULLSCREEN, |
60 | -resizeable => 0; |
66 | -resizeable => 0; |
61 | |
67 | |
|
|
68 | $last_refresh = SDL::GetTicks; |
|
|
69 | |
62 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
70 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
63 | |
71 | |
64 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
72 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
65 | or warn "WARNING: non-power-of-two opengl extension required"; |
73 | or warn "WARNING: non-power-of-two opengl extension required"; |
66 | |
74 | |
67 | $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 |
75 | $FONTSIZE = int $HEIGHT / 50; |
68 | or die "TTFOpenFont: $!"; |
|
|
69 | |
76 | |
70 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; |
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"; |
71 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
80 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
72 | $ALT_ENTER_MESSAGE->activate; |
81 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
73 | |
82 | |
|
|
83 | # Test code #d# |
|
|
84 | unless ($tw) { # haha... |
74 | $tw = new Crossfire::Client::Widget::Window; |
85 | $tw = new Crossfire::Client::Widget::Animator; |
75 | $tw->add (my $lbl = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!"); |
86 | my $lbl1 = new Crossfire::Client::Widget::Label |
76 | # $tw = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!"; |
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"; |
77 | |
90 | |
78 | $tw->move (0, $HEIGHT - 50); |
91 | my $vb = new Crossfire::Client::Widget::VBox; |
79 | $tw->activate; |
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 | } |
80 | |
109 | |
81 | glClearColor 0, 0, 0, 0; |
110 | glClearColor 0, 0, 0, 0; |
82 | |
111 | |
83 | glEnable GL_TEXTURE_2D; |
112 | glEnable GL_TEXTURE_2D; |
|
|
113 | glEnable GL_COLOR_MATERIAL; |
84 | glShadeModel GL_FLAT; |
114 | glShadeModel GL_FLAT; |
85 | glDisable GL_DEPTH_TEST; |
115 | glDisable GL_DEPTH_TEST; |
86 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
116 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
87 | |
117 | |
88 | glMatrixMode GL_PROJECTION; |
|
|
89 | glLoadIdentity; |
|
|
90 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
|
|
91 | |
|
|
92 | glMatrixMode GL_MODELVIEW; |
|
|
93 | |
|
|
94 | $_->() for @GL_INIT; |
118 | $_->() for @GL_INIT; |
95 | } |
119 | } |
96 | |
120 | |
97 | sub start_game { |
121 | sub start_game { |
98 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
122 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
99 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
123 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
100 | while $SDL_EV->poll; |
124 | while $SDL_EV->poll; |
101 | |
125 | |
102 | 1 |
126 | 1 |
103 | }; |
127 | }; |
… | |
… | |
105 | $WIDTH = $CFG->{width}; |
129 | $WIDTH = $CFG->{width}; |
106 | $HEIGHT = $CFG->{height}; |
130 | $HEIGHT = $CFG->{height}; |
107 | $FULLSCREEN = 0; |
131 | $FULLSCREEN = 0; |
108 | |
132 | |
109 | init_screen; |
133 | init_screen; |
|
|
134 | |
|
|
135 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
110 | |
136 | |
111 | $CONN = new conn |
137 | $CONN = new conn |
112 | host => $CFG->{host}, |
138 | host => $CFG->{host}, |
113 | port => $CFG->{port}, |
139 | port => $CFG->{port}, |
114 | user => $CFG->{user}, |
140 | user => $CFG->{user}, |
115 | pass => $CFG->{password}; |
141 | pass => $CFG->{password}, |
|
|
142 | mapw => $mapsize, |
|
|
143 | maph => $mapsize, |
|
|
144 | ; |
|
|
145 | |
|
|
146 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
116 | } |
147 | } |
117 | |
148 | |
118 | sub stop_game { |
149 | sub stop_game { |
119 | remove Glib::Source $SDL_TIMER; |
150 | remove Glib::Source $SDL_TIMER; |
|
|
151 | remove Glib::Source $refresh_handler; |
|
|
152 | undef $refresh_handler; |
120 | |
153 | |
121 | undef $SDL_APP; |
154 | undef $SDL_APP; |
|
|
155 | undef $CONN; |
122 | SDL::Quit; |
156 | SDL::Quit; |
123 | } |
157 | } |
124 | |
158 | |
125 | |
159 | |
126 | sub force_refresh { |
160 | sub force_refresh { |
127 | glViewport 0, 0, $WIDTH, $HEIGHT; |
161 | glViewport 0, 0, $WIDTH, $HEIGHT; |
|
|
162 | |
|
|
163 | glMatrixMode GL_PROJECTION; |
|
|
164 | glLoadIdentity; |
|
|
165 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
|
|
166 | glMatrixMode GL_MODELVIEW; |
|
|
167 | |
128 | glClear GL_COLOR_BUFFER_BIT; |
168 | glClear GL_COLOR_BUFFER_BIT; |
129 | |
169 | |
130 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
170 | $TOPLEVEL->draw; |
131 | |
171 | |
132 | SDL::GLSwapBuffers; |
172 | SDL::GLSwapBuffers; |
133 | } |
173 | } |
134 | |
|
|
135 | my $refresh_handler; |
|
|
136 | |
174 | |
137 | sub refresh { |
175 | sub refresh { |
138 | $refresh_handler ||= add Glib::Idle sub { |
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 | |
139 | force_refresh; |
183 | force_refresh; |
|
|
184 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
185 | |
|
|
186 | if (%ANIMATE) { |
|
|
187 | 1 |
|
|
188 | } else { |
140 | undef $refresh_handler; |
189 | undef $refresh_handler; |
|
|
190 | 0 |
141 | 0 |
191 | } |
142 | }; |
192 | }; |
|
|
193 | } |
|
|
194 | |
|
|
195 | sub animation_start { |
|
|
196 | my ($widget) = @_; |
|
|
197 | $ANIMATE{$widget} = $widget; |
|
|
198 | Scalar::Util::weaken $ANIMATE{$widget}; |
|
|
199 | |
|
|
200 | refresh; |
|
|
201 | } |
|
|
202 | |
|
|
203 | sub animation_stop { |
|
|
204 | my ($widget) = @_; |
|
|
205 | delete $ANIMATE{$widget}; |
143 | } |
206 | } |
144 | |
207 | |
145 | %SDL_CB = ( |
208 | %SDL_CB = ( |
146 | SDL_QUIT() => sub { |
209 | SDL_QUIT() => sub { |
147 | main_quit Gtk2; |
210 | main_quit Gtk2; |
… | |
… | |
186 | } |
249 | } |
187 | |
250 | |
188 | sub conn::map_scroll { |
251 | sub conn::map_scroll { |
189 | my ($self, $dx, $dy) = @_; |
252 | my ($self, $dx, $dy) = @_; |
190 | |
253 | |
191 | refresh; |
254 | # refresh; |
192 | } |
255 | } |
193 | |
256 | |
194 | sub conn::map_clear { |
257 | sub conn::map_clear { |
195 | my ($self) = @_; |
258 | my ($self) = @_; |
196 | |
259 | |
197 | refresh; |
260 | # refresh; |
198 | } |
261 | } |
199 | |
262 | |
200 | sub conn::face_find { |
263 | sub conn::face_find { |
201 | my ($self, $face) = @_; |
264 | my ($self, $face) = @_; |
202 | |
265 | |
… | |
… | |
228 | } |
291 | } |
229 | $ent->set_text ($value); |
292 | $ent->set_text ($value); |
230 | $ent->signal_connect (changed => sub { |
293 | $ent->signal_connect (changed => sub { |
231 | my ($ent) = @_; |
294 | my ($ent) = @_; |
232 | $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 | } |
233 | }); |
302 | }); |
234 | } |
303 | } |
235 | |
304 | |
236 | sub run_config_dialog { |
305 | sub run_config_dialog { |
237 | my (%events) = @_; |
306 | my (%events) = @_; |
… | |
… | |
239 | my $w = Gtk2::Window->new; |
308 | my $w = Gtk2::Window->new; |
240 | |
309 | |
241 | my @cfg = ( |
310 | my @cfg = ( |
242 | [qw/Host host/], |
311 | [qw/Host host/], |
243 | [qw/Port port/], |
312 | [qw/Port port/], |
|
|
313 | [qw/Mapsize% mapsize/], |
244 | [qw/Username user/], |
314 | [qw/Username user/], |
245 | [qw/Password password/], |
315 | [qw/Password password/], |
246 | ); |
316 | ); |
247 | |
317 | |
248 | my $cfg = {}; |
318 | my $cfg = {}; |
… | |
… | |
295 | my $cb = $events{login} || sub {}; |
365 | my $cb = $events{login} || sub {}; |
296 | $cb->($::CFG->{user}, $::CFG->{password}); |
366 | $cb->($::CFG->{user}, $::CFG->{password}); |
297 | }); |
367 | }); |
298 | $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); |
299 | $cb->signal_connect (clicked => sub { |
369 | $cb->signal_connect (clicked => sub { |
300 | my $cb = $events{login} || sub {}; |
370 | my $cb = $events{logout} || sub {}; |
301 | $cb->(); |
371 | $cb->(); |
302 | }); |
372 | }); |
303 | $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); |
304 | $cb->signal_connect (clicked => sub { $w->destroy }); |
374 | $cb->signal_connect (clicked => sub { $w->destroy }); |
305 | |
375 | |
… | |
… | |
310 | |
380 | |
311 | |
381 | |
312 | ############################################################################# |
382 | ############################################################################# |
313 | |
383 | |
314 | SDL::Init SDL_INIT_EVERYTHING; |
384 | SDL::Init SDL_INIT_EVERYTHING; |
315 | SDL::TTFInit; |
385 | |
|
|
386 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
316 | |
387 | |
317 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
388 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
318 | |
389 | |
319 | $mapwidget->activate; |
390 | $TOPLEVEL->add ($mapwidget); |
320 | $mapwidget->focus_in; |
391 | $mapwidget->focus_in; |
321 | |
392 | |
322 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
393 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
323 | |
394 | |
324 | $CFG ||= { |
395 | $CFG ||= { |
325 | width => 640, |
396 | width => 640, |
326 | height => 480, |
397 | height => 480, |
|
|
398 | mapsize => 100, |
327 | fullscreen => 0, |
399 | fullscreen => 0, |
328 | host => "crossfire.schmorp.de", |
400 | host => "crossfire.schmorp.de", |
329 | port => 13327, |
401 | port => 13327, |
330 | }; |
402 | }; |
331 | |
403 | |
|
|
404 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
405 | |
332 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
406 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
333 | |
407 | |
334 | run_config_dialog |
408 | run_config_dialog |
335 | login => sub { start_game }, |
409 | login => sub { start_game }, |
336 | logout => sub { stop_game }; |
410 | logout => sub { stop_game }; |