… | |
… | |
32 | our $HEIGHT; |
32 | our $HEIGHT; |
33 | our $FULLSCREEN; |
33 | our $FULLSCREEN; |
34 | |
34 | |
35 | our $FONTSIZE; |
35 | our $FONTSIZE; |
36 | |
36 | |
|
|
37 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
|
|
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 $TOPLEVEL; |
49 | our $TOPLEVEL; |
47 | |
50 | |
48 | our $tw; # Test widget #d# |
51 | our $tw; # Test widget #d# |
|
|
52 | |
|
|
53 | my $last_refresh; |
|
|
54 | my %ANIMATE; |
|
|
55 | my $refresh_handler; |
49 | |
56 | |
50 | sub init_screen { |
57 | sub init_screen { |
51 | $SDL_APP = new SDL::App |
58 | $SDL_APP = new SDL::App |
52 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
59 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
53 | -title => "Crossfire+ Client", |
60 | -title => "Crossfire+ Client", |
54 | -width => $WIDTH, |
61 | -width => $WIDTH, |
55 | -height => $HEIGHT, |
62 | -height => $HEIGHT, |
56 | -opengl => 1, |
63 | -opengl => 1, |
57 | -red_size => 8, |
64 | -red_size => 5, |
58 | -green_size => 8, |
65 | -green_size => 5, |
59 | -blue_size => 8, |
66 | -blue_size => 5, |
|
|
67 | -alpha_size => 0, |
60 | -double_buffer => 1, |
68 | -double_buffer => 1, |
61 | -fullscreen => $FULLSCREEN, |
69 | -fullscreen => $FULLSCREEN, |
62 | -resizeable => 0; |
70 | -resizeable => 0; |
63 | |
71 | |
|
|
72 | $last_refresh = SDL::GetTicks; |
|
|
73 | |
64 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
74 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
65 | |
75 | |
66 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
76 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
67 | or warn "WARNING: non-power-of-two opengl extension required"; |
77 | or warn "WARNING: non-power-of-two opengl extension required"; |
68 | |
78 | |
69 | $FONTSIZE = int $HEIGHT / 50; |
79 | $FONTSIZE = int $HEIGHT / 50; |
70 | |
80 | |
71 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
81 | ############################################################################# |
72 | 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, |
|
|
73 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
74 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
|
|
75 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
76 | |
|
|
77 | # Test code #d# |
|
|
78 | unless ($tw) { # haha... |
|
|
79 | my $frm = new Crossfire::Client::Widget::Frame; |
|
|
80 | $tw = new Crossfire::Client::Widget::Window; |
|
|
81 | $frm->add (my $te = new Crossfire::Client::Widget::TextEntry 0, 0, 10, $FONTSIZE, "Foo in the garden!"); |
|
|
82 | $tw->add ($frm); |
|
|
83 | |
|
|
84 | $tw->move (0, $HEIGHT - 120); |
|
|
85 | $TOPLEVEL->add ($tw); |
|
|
86 | $te->focus_in; |
|
|
87 | } |
|
|
88 | |
82 | |
89 | glClearColor 0, 0, 0, 0; |
83 | glClearColor 0, 0, 0, 0; |
90 | |
84 | |
91 | glEnable GL_TEXTURE_2D; |
85 | glEnable GL_TEXTURE_2D; |
92 | glEnable GL_COLOR_MATERIAL; |
86 | glEnable GL_COLOR_MATERIAL; |
93 | glShadeModel GL_FLAT; |
87 | glShadeModel GL_FLAT; |
94 | glDisable GL_DEPTH_TEST; |
88 | glDisable GL_DEPTH_TEST; |
95 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
89 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
96 | |
90 | |
97 | $_->() 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 | } |
98 | } |
131 | } |
99 | |
132 | |
100 | sub start_game { |
133 | sub start_game { |
101 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
134 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
102 | ($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 })->() |
103 | while $SDL_EV->poll; |
136 | while $SDL_EV->poll; |
104 | |
137 | |
105 | 1 |
138 | 1 |
106 | }; |
139 | }; |
… | |
… | |
108 | $WIDTH = $CFG->{width}; |
141 | $WIDTH = $CFG->{width}; |
109 | $HEIGHT = $CFG->{height}; |
142 | $HEIGHT = $CFG->{height}; |
110 | $FULLSCREEN = 0; |
143 | $FULLSCREEN = 0; |
111 | |
144 | |
112 | init_screen; |
145 | init_screen; |
|
|
146 | |
|
|
147 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
113 | |
148 | |
114 | $CONN = new conn |
149 | $CONN = new conn |
115 | host => $CFG->{host}, |
150 | host => $CFG->{host}, |
116 | port => $CFG->{port}, |
151 | port => $CFG->{port}, |
117 | user => $CFG->{user}, |
152 | user => $CFG->{user}, |
118 | pass => $CFG->{password}, |
153 | pass => $CFG->{password}, |
119 | mapw => 50, |
154 | mapw => $mapsize, |
120 | maph => 37, |
155 | maph => $mapsize, |
121 | ; |
156 | ; |
|
|
157 | |
|
|
158 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
122 | } |
159 | } |
123 | |
160 | |
124 | sub stop_game { |
161 | sub stop_game { |
125 | remove Glib::Source $SDL_TIMER; |
162 | remove Glib::Source $SDL_TIMER; |
|
|
163 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
164 | undef $refresh_handler; |
126 | |
165 | |
127 | undef $SDL_APP; |
166 | undef $SDL_APP; |
|
|
167 | undef $CONN; |
128 | SDL::Quit; |
168 | SDL::Quit; |
129 | } |
169 | } |
130 | |
170 | |
131 | |
171 | |
132 | sub force_refresh { |
172 | sub force_refresh { |
133 | glViewport 0, 0, $WIDTH, $HEIGHT; |
173 | glViewport 0, 0, $WIDTH, $HEIGHT; |
134 | |
174 | |
135 | glMatrixMode GL_PROJECTION; |
175 | glMatrixMode GL_PROJECTION; |
136 | glLoadIdentity; |
176 | glLoadIdentity; |
137 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
177 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
138 | glMatrixMode GL_MODELVIEW; |
178 | glMatrixMode GL_MODELVIEW; |
139 | |
179 | |
140 | glClear GL_COLOR_BUFFER_BIT; |
180 | glClear GL_COLOR_BUFFER_BIT; |
141 | |
181 | |
142 | $TOPLEVEL->draw; |
182 | $TOPLEVEL->draw; |
143 | |
183 | |
144 | SDL::GLSwapBuffers; |
184 | SDL::GLSwapBuffers; |
145 | } |
185 | } |
146 | |
|
|
147 | my $refresh_handler; |
|
|
148 | |
186 | |
149 | sub refresh { |
187 | sub refresh { |
150 | $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 | |
151 | force_refresh; |
195 | force_refresh; |
|
|
196 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
197 | |
|
|
198 | if (%ANIMATE) { |
|
|
199 | 1 |
|
|
200 | } else { |
152 | undef $refresh_handler; |
201 | undef $refresh_handler; |
|
|
202 | 0 |
153 | 0 |
203 | } |
154 | }; |
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}; |
155 | } |
218 | } |
156 | |
219 | |
157 | %SDL_CB = ( |
220 | %SDL_CB = ( |
158 | SDL_QUIT() => sub { |
221 | SDL_QUIT() => sub { |
159 | main_quit Gtk2; |
222 | main_quit Gtk2; |
… | |
… | |
174 | }, |
237 | }, |
175 | SDL_KEYUP() => sub { |
238 | SDL_KEYUP() => sub { |
176 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
239 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
177 | }, |
240 | }, |
178 | SDL_MOUSEMOTION() => sub { |
241 | SDL_MOUSEMOTION() => sub { |
179 | warn "sdl motion\n";#d# |
242 | my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); |
|
|
243 | $HOVER = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
244 | ::refresh; |
180 | }, |
245 | }, |
181 | SDL_MOUSEBUTTONDOWN() => sub { |
246 | SDL_MOUSEBUTTONDOWN() => sub { |
182 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
247 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
183 | }, |
248 | }, |
184 | SDL_MOUSEBUTTONUP() => sub { |
249 | SDL_MOUSEBUTTONUP() => sub { |
… | |
… | |
198 | } |
263 | } |
199 | |
264 | |
200 | sub conn::map_scroll { |
265 | sub conn::map_scroll { |
201 | my ($self, $dx, $dy) = @_; |
266 | my ($self, $dx, $dy) = @_; |
202 | |
267 | |
203 | refresh; |
268 | # refresh; |
204 | } |
269 | } |
205 | |
270 | |
206 | sub conn::map_clear { |
271 | sub conn::map_clear { |
207 | my ($self) = @_; |
272 | my ($self) = @_; |
208 | |
273 | |
209 | refresh; |
274 | # refresh; |
210 | } |
275 | } |
211 | |
276 | |
212 | sub conn::face_find { |
277 | sub conn::face_find { |
213 | my ($self, $face) = @_; |
278 | my ($self, $face) = @_; |
214 | |
279 | |
… | |
… | |
240 | } |
305 | } |
241 | $ent->set_text ($value); |
306 | $ent->set_text ($value); |
242 | $ent->signal_connect (changed => sub { |
307 | $ent->signal_connect (changed => sub { |
243 | my ($ent) = @_; |
308 | my ($ent) = @_; |
244 | $cfg->{$key} = $ent->get_text; |
309 | $cfg->{$key} = $ent->get_text; |
|
|
310 | # TODO: Mapsize should be a slider in the game gui |
|
|
311 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
312 | $cfg->{$key} = 100; |
|
|
313 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
314 | $cfg->{$key} = 50; |
|
|
315 | } |
245 | }); |
316 | }); |
246 | } |
317 | } |
247 | |
318 | |
248 | sub run_config_dialog { |
319 | sub run_config_dialog { |
249 | my (%events) = @_; |
320 | my (%events) = @_; |
… | |
… | |
251 | my $w = Gtk2::Window->new; |
322 | my $w = Gtk2::Window->new; |
252 | |
323 | |
253 | my @cfg = ( |
324 | my @cfg = ( |
254 | [qw/Host host/], |
325 | [qw/Host host/], |
255 | [qw/Port port/], |
326 | [qw/Port port/], |
|
|
327 | [qw/Mapsize% mapsize/], |
256 | [qw/Username user/], |
328 | [qw/Username user/], |
257 | [qw/Password password/], |
329 | [qw/Password password/], |
258 | ); |
330 | ); |
259 | |
331 | |
260 | my $cfg = {}; |
332 | my $cfg = {}; |
… | |
… | |
307 | my $cb = $events{login} || sub {}; |
379 | my $cb = $events{login} || sub {}; |
308 | $cb->($::CFG->{user}, $::CFG->{password}); |
380 | $cb->($::CFG->{user}, $::CFG->{password}); |
309 | }); |
381 | }); |
310 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
382 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
311 | $cb->signal_connect (clicked => sub { |
383 | $cb->signal_connect (clicked => sub { |
312 | my $cb = $events{login} || sub {}; |
384 | my $cb = $events{logout} || sub {}; |
313 | $cb->(); |
385 | $cb->(); |
314 | }); |
386 | }); |
315 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
387 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
316 | $cb->signal_connect (clicked => sub { $w->destroy }); |
388 | $cb->signal_connect (clicked => sub { $w->destroy }); |
317 | |
389 | |
… | |
… | |
335 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
407 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
336 | |
408 | |
337 | $CFG ||= { |
409 | $CFG ||= { |
338 | width => 640, |
410 | width => 640, |
339 | height => 480, |
411 | height => 480, |
|
|
412 | mapsize => 100, |
340 | fullscreen => 0, |
413 | fullscreen => 0, |
341 | host => "crossfire.schmorp.de", |
414 | host => "crossfire.schmorp.de", |
342 | port => 13327, |
415 | port => 13327, |
343 | }; |
416 | }; |
344 | |
417 | |