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