… | |
… | |
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 | our $FOCUS; |
|
|
37 | our $HOVER; |
36 | |
38 | |
37 | our $SDL_TIMER; |
39 | our $SDL_TIMER; |
38 | our $SDL_APP; |
40 | our $SDL_APP; |
39 | our $SDL_EV = new SDL::Event; |
41 | our $SDL_EV = new SDL::Event; |
40 | our %SDL_CB; |
42 | our %SDL_CB; |
41 | |
43 | |
42 | our @GL_INIT; # hooks called on every gl init |
44 | our @GL_INIT; # hooks called on every gl init |
43 | |
45 | |
44 | our $ALT_ENTER_MESSAGE; |
46 | our $ALT_ENTER_MESSAGE; |
|
|
47 | our $STATUS_LINE; |
|
|
48 | |
|
|
49 | our $TOPLEVEL; |
45 | |
50 | |
46 | our $tw; # Test widget #d# |
51 | our $tw; # Test widget #d# |
|
|
52 | |
|
|
53 | my $last_refresh; |
|
|
54 | my %ANIMATE; |
|
|
55 | my $refresh_handler; |
47 | |
56 | |
48 | sub init_screen { |
57 | sub init_screen { |
49 | $SDL_APP = new SDL::App |
58 | $SDL_APP = new SDL::App |
50 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
59 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
51 | -title => "Crossfire+ Client", |
60 | -title => "Crossfire+ Client", |
52 | -width => $WIDTH, |
61 | -width => $WIDTH, |
53 | -height => $HEIGHT, |
62 | -height => $HEIGHT, |
54 | -opengl => 1, |
63 | -opengl => 1, |
55 | -red_size => 8, |
64 | -red_size => 5, |
56 | -green_size => 8, |
65 | -green_size => 5, |
57 | -blue_size => 8, |
66 | -blue_size => 5, |
|
|
67 | -alpha_size => 0, |
58 | -double_buffer => 1, |
68 | -double_buffer => 1, |
59 | -fullscreen => $FULLSCREEN, |
69 | -fullscreen => $FULLSCREEN, |
60 | -resizeable => 0; |
70 | -resizeable => 0; |
61 | |
71 | |
|
|
72 | $last_refresh = SDL::GetTicks; |
|
|
73 | |
62 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
74 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
63 | |
75 | |
64 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
76 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
65 | or warn "WARNING: non-power-of-two opengl extension required"; |
77 | or warn "WARNING: non-power-of-two opengl extension required"; |
66 | |
78 | |
67 | $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 |
79 | $FONTSIZE = int $HEIGHT / 50; |
68 | or die "TTFOpenFont: $!"; |
|
|
69 | |
80 | |
70 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; |
81 | ############################################################################# |
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, $HEIGHT - $HEIGHT / 40, 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, $HEIGHT - 50); |
|
|
80 | $tw->activate; |
|
|
81 | # Test code end #d# |
|
|
82 | |
82 | |
83 | glClearColor 0, 0, 0, 0; |
83 | glClearColor 0, 0, 0, 0; |
84 | |
84 | |
85 | glEnable GL_TEXTURE_2D; |
85 | glEnable GL_TEXTURE_2D; |
|
|
86 | glEnable GL_COLOR_MATERIAL; |
86 | glShadeModel GL_FLAT; |
87 | glShadeModel GL_FLAT; |
87 | glDisable GL_DEPTH_TEST; |
88 | glDisable GL_DEPTH_TEST; |
88 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
89 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
89 | |
90 | |
90 | glMatrixMode GL_PROJECTION; |
|
|
91 | glLoadIdentity; |
|
|
92 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
|
|
93 | |
|
|
94 | glMatrixMode GL_MODELVIEW; |
|
|
95 | |
|
|
96 | $_->() for @GL_INIT; |
91 | $_->() for @GL_INIT; |
|
|
92 | |
|
|
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 | } |
97 | } |
131 | } |
98 | |
132 | |
99 | sub start_game { |
133 | sub start_game { |
100 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
134 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
101 | ($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 })->() |
102 | while $SDL_EV->poll; |
136 | while $SDL_EV->poll; |
103 | |
137 | |
104 | 1 |
138 | 1 |
105 | }; |
139 | }; |
… | |
… | |
107 | $WIDTH = $CFG->{width}; |
141 | $WIDTH = $CFG->{width}; |
108 | $HEIGHT = $CFG->{height}; |
142 | $HEIGHT = $CFG->{height}; |
109 | $FULLSCREEN = 0; |
143 | $FULLSCREEN = 0; |
110 | |
144 | |
111 | init_screen; |
145 | init_screen; |
|
|
146 | |
|
|
147 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
112 | |
148 | |
113 | $CONN = new conn |
149 | $CONN = new conn |
114 | host => $CFG->{host}, |
150 | host => $CFG->{host}, |
115 | port => $CFG->{port}, |
151 | port => $CFG->{port}, |
116 | user => $CFG->{user}, |
152 | user => $CFG->{user}, |
117 | pass => $CFG->{password}; |
153 | pass => $CFG->{password}, |
|
|
154 | mapw => $mapsize, |
|
|
155 | maph => $mapsize, |
|
|
156 | ; |
|
|
157 | |
|
|
158 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
118 | } |
159 | } |
119 | |
160 | |
120 | sub stop_game { |
161 | sub stop_game { |
121 | remove Glib::Source $SDL_TIMER; |
162 | remove Glib::Source $SDL_TIMER; |
|
|
163 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
164 | undef $refresh_handler; |
122 | |
165 | |
123 | undef $SDL_APP; |
166 | undef $SDL_APP; |
|
|
167 | undef $CONN; |
124 | SDL::Quit; |
168 | SDL::Quit; |
125 | } |
169 | } |
126 | |
170 | |
127 | |
171 | |
128 | sub force_refresh { |
172 | sub force_refresh { |
129 | glViewport 0, 0, $WIDTH, $HEIGHT; |
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 | |
130 | glClear GL_COLOR_BUFFER_BIT; |
180 | glClear GL_COLOR_BUFFER_BIT; |
131 | |
181 | |
132 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
182 | $TOPLEVEL->draw; |
133 | |
183 | |
134 | SDL::GLSwapBuffers; |
184 | SDL::GLSwapBuffers; |
135 | } |
185 | } |
136 | |
|
|
137 | my $refresh_handler; |
|
|
138 | |
186 | |
139 | sub refresh { |
187 | sub refresh { |
140 | $refresh_handler ||= add Glib::Idle sub { |
188 | $refresh_handler ||= add Glib::Idle sub { |
|
|
189 | return unless $SDL_APP; |
|
|
190 | |
|
|
191 | my $next_refresh = SDL::GetTicks; |
|
|
192 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
|
|
193 | $last_refresh = $next_refresh; |
|
|
194 | |
141 | force_refresh; |
195 | force_refresh; |
|
|
196 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
197 | |
|
|
198 | if (%ANIMATE) { |
|
|
199 | 1 |
|
|
200 | } else { |
142 | undef $refresh_handler; |
201 | undef $refresh_handler; |
|
|
202 | 0 |
143 | 0 |
203 | } |
144 | }; |
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}; |
145 | } |
218 | } |
146 | |
219 | |
147 | %SDL_CB = ( |
220 | %SDL_CB = ( |
148 | SDL_QUIT() => sub { |
221 | SDL_QUIT() => sub { |
149 | main_quit Gtk2; |
222 | main_quit Gtk2; |
… | |
… | |
164 | }, |
237 | }, |
165 | SDL_KEYUP() => sub { |
238 | SDL_KEYUP() => sub { |
166 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
239 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
167 | }, |
240 | }, |
168 | SDL_MOUSEMOTION() => sub { |
241 | SDL_MOUSEMOTION() => sub { |
169 | 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"; |
170 | }, |
247 | }, |
171 | SDL_MOUSEBUTTONDOWN() => sub { |
248 | SDL_MOUSEBUTTONDOWN() => sub { |
172 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
249 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
173 | }, |
250 | }, |
174 | SDL_MOUSEBUTTONUP() => sub { |
251 | SDL_MOUSEBUTTONUP() => sub { |
… | |
… | |
188 | } |
265 | } |
189 | |
266 | |
190 | sub conn::map_scroll { |
267 | sub conn::map_scroll { |
191 | my ($self, $dx, $dy) = @_; |
268 | my ($self, $dx, $dy) = @_; |
192 | |
269 | |
193 | refresh; |
270 | # refresh; |
194 | } |
271 | } |
195 | |
272 | |
196 | sub conn::map_clear { |
273 | sub conn::map_clear { |
197 | my ($self) = @_; |
274 | my ($self) = @_; |
198 | |
275 | |
199 | refresh; |
276 | # refresh; |
200 | } |
277 | } |
201 | |
278 | |
202 | sub conn::face_find { |
279 | sub conn::face_find { |
203 | my ($self, $face) = @_; |
280 | my ($self, $face) = @_; |
204 | |
281 | |
… | |
… | |
230 | } |
307 | } |
231 | $ent->set_text ($value); |
308 | $ent->set_text ($value); |
232 | $ent->signal_connect (changed => sub { |
309 | $ent->signal_connect (changed => sub { |
233 | my ($ent) = @_; |
310 | my ($ent) = @_; |
234 | $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 | } |
235 | }); |
318 | }); |
236 | } |
319 | } |
237 | |
320 | |
238 | sub run_config_dialog { |
321 | sub run_config_dialog { |
239 | my (%events) = @_; |
322 | my (%events) = @_; |
… | |
… | |
241 | my $w = Gtk2::Window->new; |
324 | my $w = Gtk2::Window->new; |
242 | |
325 | |
243 | my @cfg = ( |
326 | my @cfg = ( |
244 | [qw/Host host/], |
327 | [qw/Host host/], |
245 | [qw/Port port/], |
328 | [qw/Port port/], |
|
|
329 | [qw/Mapsize% mapsize/], |
246 | [qw/Username user/], |
330 | [qw/Username user/], |
247 | [qw/Password password/], |
331 | [qw/Password password/], |
248 | ); |
332 | ); |
249 | |
333 | |
250 | my $cfg = {}; |
334 | my $cfg = {}; |
… | |
… | |
297 | my $cb = $events{login} || sub {}; |
381 | my $cb = $events{login} || sub {}; |
298 | $cb->($::CFG->{user}, $::CFG->{password}); |
382 | $cb->($::CFG->{user}, $::CFG->{password}); |
299 | }); |
383 | }); |
300 | $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); |
301 | $cb->signal_connect (clicked => sub { |
385 | $cb->signal_connect (clicked => sub { |
302 | my $cb = $events{login} || sub {}; |
386 | my $cb = $events{logout} || sub {}; |
303 | $cb->(); |
387 | $cb->(); |
304 | }); |
388 | }); |
305 | $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); |
306 | $cb->signal_connect (clicked => sub { $w->destroy }); |
390 | $cb->signal_connect (clicked => sub { $w->destroy }); |
307 | |
391 | |
… | |
… | |
312 | |
396 | |
313 | |
397 | |
314 | ############################################################################# |
398 | ############################################################################# |
315 | |
399 | |
316 | SDL::Init SDL_INIT_EVERYTHING; |
400 | SDL::Init SDL_INIT_EVERYTHING; |
317 | SDL::TTFInit; |
401 | |
|
|
402 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
318 | |
403 | |
319 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
404 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
320 | |
405 | |
321 | $mapwidget->activate; |
406 | $TOPLEVEL->add ($mapwidget); |
322 | $mapwidget->focus_in; |
407 | $mapwidget->focus_in; |
323 | |
408 | |
324 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
409 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
325 | |
410 | |
326 | $CFG ||= { |
411 | $CFG ||= { |
327 | width => 640, |
412 | width => 640, |
328 | height => 480, |
413 | height => 480, |
|
|
414 | mapsize => 100, |
329 | fullscreen => 0, |
415 | fullscreen => 0, |
330 | host => "crossfire.schmorp.de", |
416 | host => "crossfire.schmorp.de", |
331 | port => 13327, |
417 | port => 13327, |
332 | }; |
418 | }; |
333 | |
419 | |
|
|
420 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
421 | |
334 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
422 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
335 | |
423 | |
336 | run_config_dialog |
424 | run_config_dialog |
337 | login => sub { start_game }, |
425 | login => sub { start_game }, |
338 | logout => sub { stop_game }; |
426 | logout => sub { stop_game }; |