… | |
… | |
12 | use SDL::Surface; |
12 | use SDL::Surface; |
13 | use SDL::OpenGL; |
13 | use SDL::OpenGL; |
14 | use SDL::OpenGL::Constants; |
14 | use SDL::OpenGL::Constants; |
15 | |
15 | |
16 | use Crossfire; |
16 | use Crossfire; |
|
|
17 | use Crossfire::Protocol; |
|
|
18 | |
17 | use Crossfire::Client; |
19 | use Crossfire::Client; |
18 | use Crossfire::Protocol; |
|
|
19 | |
|
|
20 | use Crossfire::Client::Widget; |
20 | use Crossfire::Client::Widget; |
21 | |
21 | |
|
|
22 | our $VERSION = '0.1'; |
|
|
23 | |
|
|
24 | my $MAX_FPS = 30; |
|
|
25 | my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
|
|
26 | |
22 | our $FACECACHE; |
27 | our $FACECACHE; |
23 | |
|
|
24 | our $VERSION = '0.1'; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
|
|
27 | |
28 | |
28 | our $CFG; |
29 | our $CFG; |
29 | our $CONN; |
30 | our $CONN; |
30 | |
31 | |
31 | our $WIDTH; |
32 | our $WIDTH; |
32 | our $HEIGHT; |
33 | our $HEIGHT; |
33 | our $FULLSCREEN; |
34 | our $FULLSCREEN; |
34 | |
35 | |
35 | our $UIFONT; |
36 | our $FONTSIZE; |
36 | |
37 | |
37 | our $SDL_TIMER; |
38 | our $SDL_TIMER; |
38 | our $SDL_APP; |
39 | our $SDL_APP; |
39 | our $SDL_EV = new SDL::Event; |
40 | our $SDL_EV; |
40 | our %SDL_CB; |
41 | our %SDL_CB; |
41 | |
42 | |
42 | our @GL_INIT; # hooks called on every gl init |
|
|
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
43 | our $ALT_ENTER_MESSAGE; |
|
|
44 | our $STATUS_LINE; |
45 | |
45 | |
|
|
46 | my $last_refresh; |
|
|
47 | my %ANIMATE; |
|
|
48 | my $refresh_handler; |
|
|
49 | |
46 | our $tw; # Test widget #d# |
50 | our ($tw, $te); # Test widget #d# |
47 | |
51 | |
48 | sub init_screen { |
52 | sub init_screen { |
49 | $SDL_APP = new SDL::App |
53 | $SDL_APP = new SDL::App |
50 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
54 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
51 | -title => "Crossfire+ Client", |
55 | -title => "Crossfire+ Client", |
52 | -width => $WIDTH, |
56 | -width => $WIDTH, |
53 | -height => $HEIGHT, |
57 | -height => $HEIGHT, |
54 | -opengl => 1, |
58 | -opengl => 1, |
55 | -red_size => 8, |
59 | -red_size => 5, |
56 | -green_size => 8, |
60 | -green_size => 5, |
57 | -blue_size => 8, |
61 | -blue_size => 5, |
|
|
62 | -alpha_size => 0, |
58 | -double_buffer => 1, |
63 | -double_buffer => 1, |
59 | -fullscreen => $FULLSCREEN, |
64 | -fullscreen => $FULLSCREEN, |
60 | -resizeable => 0; |
65 | -resizeable => 0; |
61 | |
66 | |
62 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
67 | $SDL_EV = new SDL::Event; |
|
|
68 | $SDL_EV->set_unicode (1); |
63 | |
69 | |
64 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
70 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
65 | or warn "WARNING: non-power-of-two opengl extension required"; |
71 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
72 | while $SDL_EV->poll; |
66 | |
73 | |
67 | $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 |
74 | 1 |
68 | or die "TTFOpenFont: $!"; |
75 | }; |
69 | |
76 | |
70 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; |
77 | $last_refresh = SDL::GetTicks; |
71 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
|
|
72 | $ALT_ENTER_MESSAGE->activate; |
|
|
73 | |
78 | |
74 | # Test code #d# |
79 | Crossfire::Client::gl_init; |
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 | |
80 | |
79 | $tw->move (0, 0); |
81 | $FONTSIZE = int $HEIGHT / 50; |
80 | $tw->activate; |
|
|
81 | # Test code end #d# |
|
|
82 | |
82 | |
|
|
83 | ############################################################################# |
|
|
84 | |
83 | glClearColor 0, 0, 0, 0; |
85 | glClearColor 0.45, 0.45, 0.45, 1; |
84 | |
86 | |
85 | glEnable GL_TEXTURE_2D; |
87 | glEnable GL_TEXTURE_2D; |
|
|
88 | glEnable GL_COLOR_MATERIAL; |
86 | glShadeModel GL_FLAT; |
89 | glShadeModel GL_FLAT; |
87 | glDisable GL_DEPTH_TEST; |
90 | glDisable GL_DEPTH_TEST; |
88 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
91 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
89 | |
92 | |
90 | $_->() for @GL_INIT; |
93 | ############################################################################# |
|
|
94 | |
|
|
95 | $STATUS_LINE = new Crossfire::Client::Widget::Label |
|
|
96 | 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
|
|
97 | ""; |
|
|
98 | $Crossfire::Client::Widget::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 | $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
104 | |
|
|
105 | # Test code #d# |
|
|
106 | unless ($tw) { # haha... |
|
|
107 | $te = new Crossfire::Client::Widget::FancyFrame; |
|
|
108 | $te->add (new Crossfire::Client::Widget::Entry); |
|
|
109 | $te->move (300, 0, 2); |
|
|
110 | $Crossfire::Client::Widget::TOPLEVEL->add ($te); |
|
|
111 | |
|
|
112 | $tw = new Crossfire::Client::Widget::Animator; |
|
|
113 | my $lbl1 = new Crossfire::Client::Widget::Label |
|
|
114 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
|
|
115 | my $lbl2 = new Crossfire::Client::Widget::Label |
|
|
116 | 0, 0, 10, $FONTSIZE, "LBL2"; |
|
|
117 | |
|
|
118 | my $vb = new Crossfire::Client::Widget::VBox; |
|
|
119 | my $f = new Crossfire::Client::Widget::FancyFrame; |
|
|
120 | my $f2 = new Crossfire::Client::Widget::FancyFrame; |
|
|
121 | $f->add ($lbl1); |
|
|
122 | $f2->add ($lbl2); |
|
|
123 | $vb->add ($f); |
|
|
124 | $vb->add ($f2, 1); |
|
|
125 | |
|
|
126 | $tw->add ($vb); |
|
|
127 | $tw->w (400); |
|
|
128 | $tw->h (300); |
|
|
129 | $tw->move ($WIDTH - 200, 0); |
|
|
130 | $tw->moveto (0, 0); |
|
|
131 | $Crossfire::Client::Widget::TOPLEVEL->add ($tw); |
|
|
132 | |
|
|
133 | # $f->move ($WIDTH - 200, 0); |
|
|
134 | # $Crossfire::Client::Widget::TOPLEVEL->add ($f); |
|
|
135 | } |
|
|
136 | } |
|
|
137 | |
|
|
138 | sub destroy_screen { |
|
|
139 | remove Glib::Source $SDL_TIMER; |
|
|
140 | undef $SDL_APP; |
|
|
141 | undef $SDL_EV; |
|
|
142 | SDL::Quit; |
91 | } |
143 | } |
92 | |
144 | |
93 | sub start_game { |
145 | sub start_game { |
94 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
|
|
95 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
96 | while $SDL_EV->poll; |
|
|
97 | |
|
|
98 | 1 |
|
|
99 | }; |
|
|
100 | |
|
|
101 | $WIDTH = $CFG->{width}; |
146 | $WIDTH = $CFG->{width}; |
102 | $HEIGHT = $CFG->{height}; |
147 | $HEIGHT = $CFG->{height}; |
103 | $FULLSCREEN = 0; |
148 | $FULLSCREEN = 0; |
104 | |
149 | |
105 | init_screen; |
150 | init_screen; |
|
|
151 | |
|
|
152 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
106 | |
153 | |
107 | $CONN = new conn |
154 | $CONN = new conn |
108 | host => $CFG->{host}, |
155 | host => $CFG->{host}, |
109 | port => $CFG->{port}, |
156 | port => $CFG->{port}, |
110 | user => $CFG->{user}, |
157 | user => $CFG->{user}, |
111 | pass => $CFG->{password}; |
158 | pass => $CFG->{password}, |
|
|
159 | mapw => $mapsize, |
|
|
160 | maph => $mapsize, |
|
|
161 | ; |
|
|
162 | |
|
|
163 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
112 | } |
164 | } |
113 | |
165 | |
114 | sub stop_game { |
166 | sub stop_game { |
115 | remove Glib::Source $SDL_TIMER; |
167 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
168 | undef $refresh_handler; |
116 | |
169 | |
117 | undef $SDL_APP; |
170 | undef $CONN; |
118 | SDL::Quit; |
171 | destroy_screen; |
119 | } |
172 | } |
120 | |
|
|
121 | |
173 | |
122 | sub force_refresh { |
174 | sub force_refresh { |
123 | glViewport 0, 0, $WIDTH, $HEIGHT; |
175 | glViewport 0, 0, $WIDTH, $HEIGHT; |
124 | |
176 | |
125 | glMatrixMode GL_PROJECTION; |
177 | glMatrixMode GL_PROJECTION; |
126 | glLoadIdentity; |
178 | glLoadIdentity; |
127 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
179 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
128 | glMatrixMode GL_MODELVIEW; |
180 | glMatrixMode GL_MODELVIEW; |
129 | |
181 | |
130 | glClear GL_COLOR_BUFFER_BIT; |
182 | glClear GL_COLOR_BUFFER_BIT; |
131 | |
183 | |
132 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
184 | $Crossfire::Client::Widget::TOPLEVEL->draw; |
133 | |
185 | |
134 | SDL::GLSwapBuffers; |
186 | SDL::GLSwapBuffers; |
135 | } |
187 | } |
136 | |
|
|
137 | my $refresh_handler; |
|
|
138 | |
188 | |
139 | sub refresh { |
189 | sub refresh { |
140 | $refresh_handler ||= add Glib::Idle sub { |
190 | $refresh_handler ||= add Glib::Idle sub { |
|
|
191 | if ($SDL_APP) { |
|
|
192 | my $next_refresh = SDL::GetTicks; |
|
|
193 | |
|
|
194 | if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) { |
|
|
195 | SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh); |
|
|
196 | $next_refresh = SDL::GetTicks; |
|
|
197 | } |
|
|
198 | |
|
|
199 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
|
|
200 | $last_refresh = $next_refresh; |
|
|
201 | |
141 | force_refresh; |
202 | force_refresh; |
|
|
203 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
204 | |
|
|
205 | if (%ANIMATE) { |
|
|
206 | 1 |
|
|
207 | } else { |
|
|
208 | undef $refresh_handler; |
|
|
209 | 0 |
|
|
210 | } |
|
|
211 | } else { |
142 | undef $refresh_handler; |
212 | undef $refresh_handler; |
|
|
213 | 0 |
143 | 0 |
214 | } |
144 | }; |
215 | }; |
|
|
216 | } |
|
|
217 | |
|
|
218 | sub animation_start { |
|
|
219 | my ($widget) = @_; |
|
|
220 | $ANIMATE{$widget} = $widget; |
|
|
221 | Scalar::Util::weaken $ANIMATE{$widget}; |
|
|
222 | |
|
|
223 | refresh; |
|
|
224 | } |
|
|
225 | |
|
|
226 | sub animation_stop { |
|
|
227 | my ($widget) = @_; |
|
|
228 | delete $ANIMATE{$widget}; |
145 | } |
229 | } |
146 | |
230 | |
147 | %SDL_CB = ( |
231 | %SDL_CB = ( |
148 | SDL_QUIT() => sub { |
232 | SDL_QUIT() => sub { |
149 | main_quit Gtk2; |
233 | main_quit Gtk2; |
… | |
… | |
164 | }, |
248 | }, |
165 | SDL_KEYUP() => sub { |
249 | SDL_KEYUP() => sub { |
166 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
250 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
167 | }, |
251 | }, |
168 | SDL_MOUSEMOTION() => sub { |
252 | SDL_MOUSEMOTION() => sub { |
169 | warn "sdl motion\n";#d# |
253 | Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV); |
170 | }, |
254 | }, |
171 | SDL_MOUSEBUTTONDOWN() => sub { |
255 | SDL_MOUSEBUTTONDOWN() => sub { |
172 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
256 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
173 | }, |
257 | }, |
174 | SDL_MOUSEBUTTONUP() => sub { |
258 | SDL_MOUSEBUTTONUP() => sub { |
175 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
259 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
176 | }, |
260 | }, |
177 | SDL_ACTIVEEVENT() => sub { |
261 | SDL_ACTIVEEVENT() => sub { |
178 | warn "active\n";#d# |
262 | printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
179 | }, |
263 | }, |
180 | ); |
264 | ); |
181 | |
265 | |
182 | @conn::ISA = Crossfire::Protocol::; |
266 | @conn::ISA = Crossfire::Protocol::; |
183 | |
267 | |
… | |
… | |
188 | } |
272 | } |
189 | |
273 | |
190 | sub conn::map_scroll { |
274 | sub conn::map_scroll { |
191 | my ($self, $dx, $dy) = @_; |
275 | my ($self, $dx, $dy) = @_; |
192 | |
276 | |
193 | refresh; |
277 | # refresh; |
194 | } |
278 | } |
195 | |
279 | |
196 | sub conn::map_clear { |
280 | sub conn::map_clear { |
197 | my ($self) = @_; |
281 | my ($self) = @_; |
198 | |
282 | |
199 | refresh; |
283 | # refresh; |
200 | } |
284 | } |
201 | |
285 | |
202 | sub conn::face_find { |
286 | sub conn::face_find { |
203 | my ($self, $face) = @_; |
287 | my ($self, $face) = @_; |
204 | |
288 | |
… | |
… | |
230 | } |
314 | } |
231 | $ent->set_text ($value); |
315 | $ent->set_text ($value); |
232 | $ent->signal_connect (changed => sub { |
316 | $ent->signal_connect (changed => sub { |
233 | my ($ent) = @_; |
317 | my ($ent) = @_; |
234 | $cfg->{$key} = $ent->get_text; |
318 | $cfg->{$key} = $ent->get_text; |
|
|
319 | # TODO: Mapsize should be a slider in the game gui |
|
|
320 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
321 | $cfg->{$key} = 100; |
|
|
322 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
323 | $cfg->{$key} = 50; |
|
|
324 | } |
235 | }); |
325 | }); |
236 | } |
326 | } |
237 | |
327 | |
238 | sub run_config_dialog { |
328 | sub run_config_dialog { |
239 | my (%events) = @_; |
329 | my (%events) = @_; |
… | |
… | |
241 | my $w = Gtk2::Window->new; |
331 | my $w = Gtk2::Window->new; |
242 | |
332 | |
243 | my @cfg = ( |
333 | my @cfg = ( |
244 | [qw/Host host/], |
334 | [qw/Host host/], |
245 | [qw/Port port/], |
335 | [qw/Port port/], |
|
|
336 | [qw/Mapsize% mapsize/], |
246 | [qw/Username user/], |
337 | [qw/Username user/], |
247 | [qw/Password password/], |
338 | [qw/Password password/], |
248 | ); |
339 | ); |
249 | |
340 | |
250 | my $cfg = {}; |
341 | my $cfg = {}; |
… | |
… | |
297 | my $cb = $events{login} || sub {}; |
388 | my $cb = $events{login} || sub {}; |
298 | $cb->($::CFG->{user}, $::CFG->{password}); |
389 | $cb->($::CFG->{user}, $::CFG->{password}); |
299 | }); |
390 | }); |
300 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
391 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
301 | $cb->signal_connect (clicked => sub { |
392 | $cb->signal_connect (clicked => sub { |
302 | my $cb = $events{login} || sub {}; |
393 | my $cb = $events{logout} || sub {}; |
303 | $cb->(); |
394 | $cb->(); |
304 | }); |
395 | }); |
305 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
396 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
306 | $cb->signal_connect (clicked => sub { $w->destroy }); |
397 | $cb->signal_connect (clicked => sub { $w->destroy }); |
307 | |
398 | |
… | |
… | |
312 | |
403 | |
313 | |
404 | |
314 | ############################################################################# |
405 | ############################################################################# |
315 | |
406 | |
316 | SDL::Init SDL_INIT_EVERYTHING; |
407 | SDL::Init SDL_INIT_EVERYTHING; |
317 | SDL::TTFInit; |
|
|
318 | |
408 | |
319 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
409 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
320 | |
410 | |
321 | $mapwidget->activate; |
411 | $Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget); |
322 | $mapwidget->focus_in; |
412 | $mapwidget->focus_in; |
323 | |
413 | |
324 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
414 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
325 | |
415 | |
326 | $CFG ||= { |
416 | $CFG ||= { |
327 | width => 640, |
417 | width => 640, |
328 | height => 480, |
418 | height => 480, |
|
|
419 | mapsize => 100, |
329 | fullscreen => 0, |
420 | fullscreen => 0, |
330 | host => "crossfire.schmorp.de", |
421 | host => "crossfire.schmorp.de", |
331 | port => 13327, |
422 | port => 13327, |
332 | }; |
423 | }; |
333 | |
424 | |
|
|
425 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
426 | |
334 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
427 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
335 | |
428 | |
336 | run_config_dialog |
429 | run_config_dialog |
337 | login => sub { start_game }, |
430 | login => sub { start_game }, |
338 | logout => sub { stop_game }; |
431 | logout => sub { stop_game }; |