… | |
… | |
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; |
45 | |
48 | |
46 | our $tw; |
49 | our $TOPLEVEL; |
|
|
50 | |
|
|
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 | $tw = new Crossfire::Client::Widget::Window; |
|
|
75 | $tw->add (my $lbl = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!"); |
|
|
76 | # $tw = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!"; |
|
|
77 | |
|
|
78 | $tw->move (0, $HEIGHT - 50); |
|
|
79 | $tw->activate; |
|
|
80 | |
82 | |
81 | glClearColor 0, 0, 0, 0; |
83 | glClearColor 0, 0, 0, 0; |
82 | |
84 | |
83 | glEnable GL_TEXTURE_2D; |
85 | glEnable GL_TEXTURE_2D; |
|
|
86 | glEnable GL_COLOR_MATERIAL; |
84 | glShadeModel GL_FLAT; |
87 | glShadeModel GL_FLAT; |
85 | glDisable GL_DEPTH_TEST; |
88 | glDisable GL_DEPTH_TEST; |
86 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
89 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
87 | |
90 | |
88 | glMatrixMode GL_PROJECTION; |
|
|
89 | glLoadIdentity; |
|
|
90 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
|
|
91 | |
|
|
92 | glMatrixMode GL_MODELVIEW; |
|
|
93 | |
|
|
94 | $_->() 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 | } |
95 | } |
131 | } |
96 | |
132 | |
97 | sub start_game { |
133 | sub start_game { |
98 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
134 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
99 | ($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 })->() |
100 | while $SDL_EV->poll; |
136 | while $SDL_EV->poll; |
101 | |
137 | |
102 | 1 |
138 | 1 |
103 | }; |
139 | }; |
… | |
… | |
105 | $WIDTH = $CFG->{width}; |
141 | $WIDTH = $CFG->{width}; |
106 | $HEIGHT = $CFG->{height}; |
142 | $HEIGHT = $CFG->{height}; |
107 | $FULLSCREEN = 0; |
143 | $FULLSCREEN = 0; |
108 | |
144 | |
109 | init_screen; |
145 | init_screen; |
|
|
146 | |
|
|
147 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
110 | |
148 | |
111 | $CONN = new conn |
149 | $CONN = new conn |
112 | host => $CFG->{host}, |
150 | host => $CFG->{host}, |
113 | port => $CFG->{port}, |
151 | port => $CFG->{port}, |
114 | user => $CFG->{user}, |
152 | user => $CFG->{user}, |
115 | pass => $CFG->{password}; |
153 | pass => $CFG->{password}, |
|
|
154 | mapw => $mapsize, |
|
|
155 | maph => $mapsize, |
|
|
156 | ; |
|
|
157 | |
|
|
158 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
116 | } |
159 | } |
117 | |
160 | |
118 | sub stop_game { |
161 | sub stop_game { |
119 | remove Glib::Source $SDL_TIMER; |
162 | remove Glib::Source $SDL_TIMER; |
|
|
163 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
164 | undef $refresh_handler; |
120 | |
165 | |
121 | undef $SDL_APP; |
166 | undef $SDL_APP; |
|
|
167 | undef $CONN; |
122 | SDL::Quit; |
168 | SDL::Quit; |
123 | } |
169 | } |
124 | |
170 | |
125 | |
171 | |
126 | sub force_refresh { |
172 | sub force_refresh { |
127 | 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 | |
128 | glClear GL_COLOR_BUFFER_BIT; |
180 | glClear GL_COLOR_BUFFER_BIT; |
129 | |
181 | |
130 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
182 | $TOPLEVEL->draw; |
131 | |
183 | |
132 | SDL::GLSwapBuffers; |
184 | SDL::GLSwapBuffers; |
133 | } |
185 | } |
134 | |
|
|
135 | my $refresh_handler; |
|
|
136 | |
186 | |
137 | sub refresh { |
187 | sub refresh { |
138 | $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 | |
139 | force_refresh; |
195 | force_refresh; |
|
|
196 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
197 | |
|
|
198 | if (%ANIMATE) { |
|
|
199 | 1 |
|
|
200 | } else { |
140 | undef $refresh_handler; |
201 | undef $refresh_handler; |
|
|
202 | 0 |
141 | 0 |
203 | } |
142 | }; |
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}; |
143 | } |
218 | } |
144 | |
219 | |
145 | %SDL_CB = ( |
220 | %SDL_CB = ( |
146 | SDL_QUIT() => sub { |
221 | SDL_QUIT() => sub { |
147 | main_quit Gtk2; |
222 | main_quit Gtk2; |
… | |
… | |
162 | }, |
237 | }, |
163 | SDL_KEYUP() => sub { |
238 | SDL_KEYUP() => sub { |
164 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
239 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
165 | }, |
240 | }, |
166 | SDL_MOUSEMOTION() => sub { |
241 | SDL_MOUSEMOTION() => sub { |
167 | 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 | ::refresh; |
|
|
245 | |
|
|
246 | warn "mouse $x, $y = $HOVER\n"; |
168 | }, |
247 | }, |
169 | SDL_MOUSEBUTTONDOWN() => sub { |
248 | SDL_MOUSEBUTTONDOWN() => sub { |
170 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
249 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
171 | }, |
250 | }, |
172 | SDL_MOUSEBUTTONUP() => sub { |
251 | SDL_MOUSEBUTTONUP() => sub { |
… | |
… | |
186 | } |
265 | } |
187 | |
266 | |
188 | sub conn::map_scroll { |
267 | sub conn::map_scroll { |
189 | my ($self, $dx, $dy) = @_; |
268 | my ($self, $dx, $dy) = @_; |
190 | |
269 | |
191 | refresh; |
270 | # refresh; |
192 | } |
271 | } |
193 | |
272 | |
194 | sub conn::map_clear { |
273 | sub conn::map_clear { |
195 | my ($self) = @_; |
274 | my ($self) = @_; |
196 | |
275 | |
197 | refresh; |
276 | # refresh; |
198 | } |
277 | } |
199 | |
278 | |
200 | sub conn::face_find { |
279 | sub conn::face_find { |
201 | my ($self, $face) = @_; |
280 | my ($self, $face) = @_; |
202 | |
281 | |
… | |
… | |
228 | } |
307 | } |
229 | $ent->set_text ($value); |
308 | $ent->set_text ($value); |
230 | $ent->signal_connect (changed => sub { |
309 | $ent->signal_connect (changed => sub { |
231 | my ($ent) = @_; |
310 | my ($ent) = @_; |
232 | $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 | } |
233 | }); |
318 | }); |
234 | } |
319 | } |
235 | |
320 | |
236 | sub run_config_dialog { |
321 | sub run_config_dialog { |
237 | my (%events) = @_; |
322 | my (%events) = @_; |
… | |
… | |
239 | my $w = Gtk2::Window->new; |
324 | my $w = Gtk2::Window->new; |
240 | |
325 | |
241 | my @cfg = ( |
326 | my @cfg = ( |
242 | [qw/Host host/], |
327 | [qw/Host host/], |
243 | [qw/Port port/], |
328 | [qw/Port port/], |
|
|
329 | [qw/Mapsize% mapsize/], |
244 | [qw/Username user/], |
330 | [qw/Username user/], |
245 | [qw/Password password/], |
331 | [qw/Password password/], |
246 | ); |
332 | ); |
247 | |
333 | |
248 | my $cfg = {}; |
334 | my $cfg = {}; |
… | |
… | |
295 | my $cb = $events{login} || sub {}; |
381 | my $cb = $events{login} || sub {}; |
296 | $cb->($::CFG->{user}, $::CFG->{password}); |
382 | $cb->($::CFG->{user}, $::CFG->{password}); |
297 | }); |
383 | }); |
298 | $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); |
299 | $cb->signal_connect (clicked => sub { |
385 | $cb->signal_connect (clicked => sub { |
300 | my $cb = $events{login} || sub {}; |
386 | my $cb = $events{logout} || sub {}; |
301 | $cb->(); |
387 | $cb->(); |
302 | }); |
388 | }); |
303 | $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); |
304 | $cb->signal_connect (clicked => sub { $w->destroy }); |
390 | $cb->signal_connect (clicked => sub { $w->destroy }); |
305 | |
391 | |
… | |
… | |
310 | |
396 | |
311 | |
397 | |
312 | ############################################################################# |
398 | ############################################################################# |
313 | |
399 | |
314 | SDL::Init SDL_INIT_EVERYTHING; |
400 | SDL::Init SDL_INIT_EVERYTHING; |
315 | SDL::TTFInit; |
401 | |
|
|
402 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
316 | |
403 | |
317 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
404 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
318 | |
405 | |
319 | $mapwidget->activate; |
406 | $TOPLEVEL->add ($mapwidget); |
320 | $mapwidget->focus_in; |
407 | $mapwidget->focus_in; |
321 | |
408 | |
322 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
409 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
323 | |
410 | |
324 | $CFG ||= { |
411 | $CFG ||= { |
325 | width => 640, |
412 | width => 640, |
326 | height => 480, |
413 | height => 480, |
|
|
414 | mapsize => 100, |
327 | fullscreen => 0, |
415 | fullscreen => 0, |
328 | host => "crossfire.schmorp.de", |
416 | host => "crossfire.schmorp.de", |
329 | port => 13327, |
417 | port => 13327, |
330 | }; |
418 | }; |
331 | |
419 | |
|
|
420 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
421 | |
332 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
422 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
333 | |
423 | |
334 | run_config_dialog |
424 | run_config_dialog |
335 | login => sub { start_game }, |
425 | login => sub { start_game }, |
336 | logout => sub { stop_game }; |
426 | logout => sub { stop_game }; |