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