… | |
… | |
9 | use SDL; |
9 | use SDL; |
10 | use SDL::App; |
10 | use SDL::App; |
11 | use SDL::Event; |
11 | use SDL::Event; |
12 | use SDL::Surface; |
12 | use SDL::Surface; |
13 | use SDL::OpenGL; |
13 | use SDL::OpenGL; |
14 | use SDL::OpenGL::Constants; |
|
|
15 | |
14 | |
16 | use Crossfire; |
15 | use Crossfire; |
17 | use Crossfire::Client; |
|
|
18 | use Crossfire::Protocol; |
16 | use Crossfire::Protocol; |
19 | |
17 | |
20 | use Crossfire::Client::Widget; |
18 | use CFClient; |
|
|
19 | use CFClient::UI; |
|
|
20 | |
|
|
21 | our $VERSION = '0.1'; |
|
|
22 | |
|
|
23 | my $MAX_FPS = 60; |
|
|
24 | my $MIN_FPS = 5; # unused |
|
|
25 | my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
21 | |
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; |
|
|
31 | our $FAST; # fast, low-quality mode, possibly useful for software-rendering |
30 | |
32 | |
|
|
33 | our @SDL_MODES; |
31 | our $WIDTH; |
34 | our $WIDTH; |
32 | our $HEIGHT; |
35 | our $HEIGHT; |
33 | our $FULLSCREEN; |
36 | our $FULLSCREEN; |
34 | |
37 | |
|
|
38 | our $NOW; |
|
|
39 | |
|
|
40 | our $MAPWIDGET; |
35 | our $FONTSIZE; |
41 | our $FONTSIZE; |
36 | |
42 | |
37 | our $SDL_TIMER; |
43 | our $SDL_TIMER; |
|
|
44 | our $SDL_ACTIVE; |
38 | our $SDL_APP; |
45 | our $SDL_EV; |
39 | our $SDL_EV = new SDL::Event; |
|
|
40 | our %SDL_CB; |
46 | our %SDL_CB; |
41 | |
47 | |
42 | our @GL_INIT; # hooks called on every gl init |
|
|
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
48 | our $ALT_ENTER_MESSAGE; |
45 | |
49 | our $STATUS_LINE; |
46 | our $TOPLEVEL; |
50 | our $DEBUG_STATUS; |
47 | |
|
|
48 | our $tw; # Test widget #d# |
|
|
49 | |
51 | |
50 | my $last_refresh; |
52 | my $last_refresh; |
|
|
53 | my %ANIMATE; |
|
|
54 | my $refresh_handler; |
51 | |
55 | |
52 | sub init_screen { |
56 | sub status { |
53 | $SDL_APP = new SDL::App |
57 | $STATUS_LINE->set_text ($_[0]); |
54 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
58 | my ($w, $h) = $STATUS_LINE->size_request; |
55 | -title => "Crossfire+ Client", |
59 | $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h); |
56 | -width => $WIDTH, |
60 | } |
57 | -height => $HEIGHT, |
|
|
58 | -opengl => 1, |
|
|
59 | -red_size => 8, |
|
|
60 | -green_size => 8, |
|
|
61 | -blue_size => 8, |
|
|
62 | -double_buffer => 1, |
|
|
63 | -fullscreen => $FULLSCREEN, |
|
|
64 | -resizeable => 0; |
|
|
65 | |
61 | |
66 | $last_refresh = SDL::GetTicks; |
62 | sub debug { |
67 | |
63 | $DEBUG_STATUS->set_text ($_[0]); |
68 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
64 | my ($w, $h) = $DEBUG_STATUS->size_request; |
69 | |
65 | $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); |
70 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
71 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
72 | |
|
|
73 | $FONTSIZE = int $HEIGHT / 50; |
|
|
74 | |
|
|
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"; |
|
|
78 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
|
|
79 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
80 | |
|
|
81 | # Test code #d# |
|
|
82 | unless ($tw) { # haha... |
|
|
83 | $tw = new Crossfire::Client::Widget::Animator; |
|
|
84 | my $lbl1 = new Crossfire::Client::Widget::Label |
|
|
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"; |
|
|
88 | |
|
|
89 | my $vb = new Crossfire::Client::Widget::VBox; |
|
|
90 | my $f = new Crossfire::Client::Widget::FancyFrame; |
|
|
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 | } |
|
|
107 | |
|
|
108 | glClearColor 0, 0, 0, 0; |
|
|
109 | |
|
|
110 | glEnable GL_TEXTURE_2D; |
|
|
111 | glEnable GL_COLOR_MATERIAL; |
|
|
112 | glShadeModel GL_FLAT; |
|
|
113 | glDisable GL_DEPTH_TEST; |
|
|
114 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
115 | |
|
|
116 | $_->() for @GL_INIT; |
|
|
117 | } |
66 | } |
118 | |
67 | |
119 | sub start_game { |
68 | sub start_game { |
120 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
69 | status "logging in..."; |
121 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
122 | while $SDL_EV->poll; |
|
|
123 | |
70 | |
124 | 1 |
71 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
125 | }; |
|
|
126 | |
|
|
127 | $WIDTH = $CFG->{width}; |
|
|
128 | $HEIGHT = $CFG->{height}; |
|
|
129 | $FULLSCREEN = 0; |
|
|
130 | |
|
|
131 | init_screen; |
|
|
132 | |
72 | |
133 | $CONN = new conn |
73 | $CONN = new conn |
134 | host => $CFG->{host}, |
74 | host => $CFG->{host}, |
135 | port => $CFG->{port}, |
75 | port => $CFG->{port}, |
136 | user => $CFG->{user}, |
76 | user => $CFG->{user}, |
137 | pass => $CFG->{password}, |
77 | pass => $CFG->{password}, |
138 | mapw => 50, |
78 | mapw => $mapsize, |
139 | maph => 37, |
79 | maph => $mapsize, |
140 | ; |
80 | ; |
141 | |
81 | |
|
|
82 | status "login successful"; |
|
|
83 | |
142 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
84 | CFClient::lowdelay fileno $CONN->{fh}; |
143 | } |
85 | } |
144 | |
86 | |
145 | sub stop_game { |
87 | sub stop_game { |
|
|
88 | undef $CONN; |
|
|
89 | } |
|
|
90 | |
|
|
91 | sub config_dialog { |
|
|
92 | my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100, |
|
|
93 | child => (my $vbox = new CFClient::UI::VBox); |
|
|
94 | $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup"); |
|
|
95 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
|
|
96 | |
|
|
97 | $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); |
|
|
98 | $table->add (1, 0, my $hbox = new CFClient::UI::HBox); |
|
|
99 | |
|
|
100 | $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); |
|
|
101 | $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8); |
|
|
102 | |
|
|
103 | $mode_slider->connect (changed => sub { |
|
|
104 | my ($self, $value) = @_; |
|
|
105 | |
|
|
106 | $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value; |
|
|
107 | $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); |
|
|
108 | }); |
|
|
109 | $mode_slider->emit (changed => $mode_slider->{range}[0]); |
|
|
110 | |
|
|
111 | $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen"); |
|
|
112 | $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub { |
|
|
113 | my ($self, $value) = @_; |
|
|
114 | $CFG->{fullscreen} = $value; |
|
|
115 | }); |
|
|
116 | |
|
|
117 | $table->add (1, 2, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { |
|
|
118 | destroy_screen (); |
|
|
119 | init_screen (); |
|
|
120 | }); |
|
|
121 | |
|
|
122 | $vbox->add (new CFClient::UI::Label align => 0, text => "Server"); |
|
|
123 | $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
|
|
124 | $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host"); |
|
|
125 | $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host}); |
|
|
126 | |
|
|
127 | $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port"); |
|
|
128 | $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port}); |
|
|
129 | |
|
|
130 | $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); |
|
|
131 | $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user}); |
|
|
132 | |
|
|
133 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); |
|
|
134 | $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1); |
|
|
135 | |
|
|
136 | $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size"); |
|
|
137 | $table->add (1, 6, new CFClient::UI::Slider |
|
|
138 | req_w => 100, |
|
|
139 | range => [$CFG->{mapsize}, 10, 100 + 1, 1], |
|
|
140 | connect_changed => sub { |
|
|
141 | my ($self, $value) = @_; |
|
|
142 | |
|
|
143 | $CFG->{mapsize} = $self->{range}[0] = $value = int $value; |
|
|
144 | }, |
|
|
145 | ); |
|
|
146 | |
|
|
147 | $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { |
|
|
148 | start_game; |
|
|
149 | }); |
|
|
150 | |
|
|
151 | $vbox->add (my $hbox = new CFClient::UI::HBox); |
|
|
152 | |
|
|
153 | $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub { |
|
|
154 | CFClient::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
155 | status "Configuration Saved"; |
|
|
156 | }); |
|
|
157 | $CFClient::UI::TOPLEVEL->add ($dialog); |
|
|
158 | } |
|
|
159 | |
|
|
160 | sub init_screen { |
|
|
161 | ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; |
|
|
162 | $FULLSCREEN = $CFG->{fullscreen}; |
|
|
163 | |
|
|
164 | SDL::Init SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO |
|
|
165 | and die "SDL::Init failed!\n"; |
|
|
166 | |
|
|
167 | SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; |
|
|
168 | SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5; |
|
|
169 | SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5; |
|
|
170 | SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0; |
|
|
171 | |
|
|
172 | SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0; |
|
|
173 | SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0; |
|
|
174 | SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0; |
|
|
175 | SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0; |
|
|
176 | |
|
|
177 | SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1; |
|
|
178 | SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15; |
|
|
179 | SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0; |
|
|
180 | |
|
|
181 | SDL::SetVideoMode $WIDTH, $HEIGHT, 0, |
|
|
182 | SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF |
|
|
183 | | ($FULLSCREEN ? SDL_FULLSCREEN : 0) |
|
|
184 | or die "SDL::SetVideoMode failed!\n"; |
|
|
185 | |
|
|
186 | SDL::WMSetCaption "Crossfire+ Client", "Crossfire+"; |
|
|
187 | |
|
|
188 | $SDL_EV = new SDL::Event; |
|
|
189 | $SDL_EV->set_unicode (1); |
|
|
190 | |
|
|
191 | $SDL_ACTIVE = 1; |
|
|
192 | |
|
|
193 | $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub { |
|
|
194 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
195 | while $SDL_EV->poll; |
|
|
196 | |
|
|
197 | 1 |
|
|
198 | }; |
|
|
199 | |
|
|
200 | $last_refresh = SDL::GetTicks; |
|
|
201 | |
|
|
202 | CFClient::gl_init; |
|
|
203 | |
|
|
204 | $FONTSIZE = int $HEIGHT / 40; |
|
|
205 | |
|
|
206 | ############################################################################# |
|
|
207 | |
|
|
208 | $DEBUG_STATUS = new CFClient::UI::Label padding => 0; |
|
|
209 | $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); |
|
|
210 | |
|
|
211 | $STATUS_LINE = new CFClient::UI::Label |
|
|
212 | padding => 0, |
|
|
213 | y => $HEIGHT * 49 / 50 - $FONTSIZE; |
|
|
214 | $CFClient::UI::TOPLEVEL->add ($STATUS_LINE); |
|
|
215 | |
|
|
216 | $ALT_ENTER_MESSAGE = new CFClient::UI::Label |
|
|
217 | padding => 0, |
|
|
218 | y => $HEIGHT * 49 / 50, |
|
|
219 | height => $HEIGHT / 50, |
|
|
220 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
221 | $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
222 | |
|
|
223 | $MAPWIDGET = new CFClient::UI::MapWidget; |
|
|
224 | $CFClient::UI::TOPLEVEL->add ($MAPWIDGET); |
|
|
225 | $MAPWIDGET->focus_in; |
|
|
226 | |
|
|
227 | config_dialog; |
|
|
228 | } |
|
|
229 | |
|
|
230 | sub destroy_screen { |
|
|
231 | $CFClient::UI::TOPLEVEL->{children} = []; |
146 | remove Glib::Source $SDL_TIMER; |
232 | remove Glib::Source $SDL_TIMER; |
147 | |
233 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
234 | undef $refresh_handler; |
|
|
235 | undef $SDL_ACTIVE; |
148 | undef $SDL_APP; |
236 | undef $SDL_EV; |
149 | SDL::Quit; |
237 | SDL::Quit; |
150 | } |
238 | } |
151 | |
|
|
152 | |
239 | |
153 | sub force_refresh { |
240 | sub force_refresh { |
154 | glViewport 0, 0, $WIDTH, $HEIGHT; |
241 | glViewport 0, 0, $WIDTH, $HEIGHT; |
155 | |
242 | |
156 | glMatrixMode GL_PROJECTION; |
243 | glMatrixMode GL_PROJECTION; |
157 | glLoadIdentity; |
244 | glLoadIdentity; |
158 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
245 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
159 | glMatrixMode GL_MODELVIEW; |
246 | glMatrixMode GL_MODELVIEW; |
|
|
247 | glLoadIdentity; |
160 | |
248 | |
161 | glClear GL_COLOR_BUFFER_BIT; |
249 | glClear GL_COLOR_BUFFER_BIT; |
162 | |
250 | |
163 | $TOPLEVEL->draw; |
251 | $CFClient::UI::TOPLEVEL->draw; |
164 | |
252 | |
165 | SDL::GLSwapBuffers; |
253 | SDL::GLSwapBuffers; |
166 | } |
254 | } |
167 | |
255 | |
168 | my %ANIMATE; |
256 | my $FPS; |
169 | |
|
|
170 | my $refresh_handler; |
|
|
171 | |
257 | |
172 | sub refresh { |
258 | sub refresh { |
173 | $refresh_handler ||= add Glib::Idle sub { |
259 | $refresh_handler ||= add Glib::Idle sub { |
174 | my $next_refresh = SDL::GetTicks; |
260 | if ($SDL_ACTIVE) { |
|
|
261 | $NOW = SDL::GetTicks; |
|
|
262 | |
|
|
263 | if ($NOW - $last_refresh < $TICKS_PER_FRAME) { |
|
|
264 | SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); |
|
|
265 | $NOW = SDL::GetTicks; |
|
|
266 | } |
|
|
267 | |
175 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
268 | my $interval = ($NOW - $last_refresh) * 0.001; |
176 | $last_refresh = $next_refresh; |
269 | $last_refresh = $NOW; |
177 | |
270 | |
|
|
271 | if ($interval) { |
|
|
272 | $FPS ||= 1 / $interval; |
|
|
273 | $FPS = $FPS * 0.9 + (1 / $interval) * 0.1; |
|
|
274 | debug sprintf "%5.02f", $FPS; |
|
|
275 | } |
|
|
276 | |
178 | force_refresh; |
277 | force_refresh; |
179 | $_->animate ($interval) for grep $_, values %ANIMATE; |
278 | $_->animate ($interval) for grep $_, values %ANIMATE; |
180 | |
279 | |
181 | if (%ANIMATE) { |
280 | if (%ANIMATE) { |
|
|
281 | 1 |
|
|
282 | } else { |
|
|
283 | undef $refresh_handler; |
|
|
284 | 0 |
182 | 1 |
285 | } |
183 | } else { |
286 | } else { |
184 | undef $refresh_handler; |
287 | undef $refresh_handler; |
185 | 0 |
288 | 0 |
186 | } |
289 | } |
187 | }; |
290 | }; |
… | |
… | |
211 | }, |
314 | }, |
212 | SDL_KEYDOWN() => sub { |
315 | SDL_KEYDOWN() => sub { |
213 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
316 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
214 | # alt-enter |
317 | # alt-enter |
215 | $FULLSCREEN = !$FULLSCREEN; |
318 | $FULLSCREEN = !$FULLSCREEN; |
|
|
319 | destroy_screen; |
216 | init_screen; |
320 | init_screen; |
217 | } else { |
321 | } else { |
218 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
322 | CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
219 | } |
323 | } |
220 | }, |
324 | }, |
221 | SDL_KEYUP() => sub { |
325 | SDL_KEYUP() => sub { |
222 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
326 | CFClient::UI::feed_sdl_key_up_event ($SDL_EV); |
223 | }, |
327 | }, |
224 | SDL_MOUSEMOTION() => sub { |
328 | SDL_MOUSEMOTION() => sub { |
225 | warn "sdl motion\n";#d# |
329 | CFClient::UI::feed_sdl_motion_event ($SDL_EV); |
226 | }, |
330 | }, |
227 | SDL_MOUSEBUTTONDOWN() => sub { |
331 | SDL_MOUSEBUTTONDOWN() => sub { |
228 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
332 | CFClient::UI::feed_sdl_button_down_event ($SDL_EV); |
229 | }, |
333 | }, |
230 | SDL_MOUSEBUTTONUP() => sub { |
334 | SDL_MOUSEBUTTONUP() => sub { |
231 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
335 | CFClient::UI::feed_sdl_button_up_event ($SDL_EV); |
232 | }, |
336 | }, |
233 | SDL_ACTIVEEVENT() => sub { |
337 | SDL_ACTIVEEVENT() => sub { |
234 | warn "active\n";#d# |
338 | # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
235 | }, |
339 | }, |
236 | ); |
340 | ); |
237 | |
341 | |
238 | @conn::ISA = Crossfire::Protocol::; |
342 | @conn::ISA = Crossfire::Protocol::; |
239 | |
343 | |
240 | sub conn::map_update { |
344 | sub conn::map_update { |
241 | my ($self, $dirty) = @_; |
345 | my ($self, $dirty) = @_; |
242 | |
346 | |
243 | refresh; |
347 | $MAPWIDGET->update; |
244 | } |
348 | } |
245 | |
349 | |
246 | sub conn::map_scroll { |
350 | sub conn::map_scroll { |
247 | my ($self, $dx, $dy) = @_; |
351 | my ($self, $dx, $dy) = @_; |
248 | |
352 | |
… | |
… | |
264 | sub conn::face_update { |
368 | sub conn::face_update { |
265 | my ($self, $face) = @_; |
369 | my ($self, $face) = @_; |
266 | |
370 | |
267 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
371 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
268 | |
372 | |
269 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
373 | $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
270 | } |
374 | } |
271 | |
375 | |
272 | sub conn::query { |
376 | sub conn::query { |
273 | my ($self, $flags, $prompt) = @_; |
377 | my ($self, $flags, $prompt) = @_; |
274 | |
378 | |
… | |
… | |
286 | } |
390 | } |
287 | $ent->set_text ($value); |
391 | $ent->set_text ($value); |
288 | $ent->signal_connect (changed => sub { |
392 | $ent->signal_connect (changed => sub { |
289 | my ($ent) = @_; |
393 | my ($ent) = @_; |
290 | $cfg->{$key} = $ent->get_text; |
394 | $cfg->{$key} = $ent->get_text; |
|
|
395 | # TODO: Mapsize should be a slider in the game gui |
|
|
396 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
397 | $cfg->{$key} = 100; |
|
|
398 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
399 | $cfg->{$key} = 50; |
|
|
400 | } |
291 | }); |
401 | }); |
292 | } |
402 | } |
293 | |
403 | |
294 | sub run_config_dialog { |
404 | sub run_config_dialog { |
295 | my (%events) = @_; |
405 | my (%events) = @_; |
… | |
… | |
297 | my $w = Gtk2::Window->new; |
407 | my $w = Gtk2::Window->new; |
298 | |
408 | |
299 | my @cfg = ( |
409 | my @cfg = ( |
300 | [qw/Host host/], |
410 | [qw/Host host/], |
301 | [qw/Port port/], |
411 | [qw/Port port/], |
|
|
412 | [qw/Mapsize% mapsize/], |
302 | [qw/Username user/], |
413 | [qw/Username user/], |
303 | [qw/Password password/], |
414 | [qw/Password password/], |
304 | ); |
415 | ); |
305 | |
416 | |
306 | my $cfg = {}; |
417 | my $cfg = {}; |
307 | |
|
|
308 | my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
|
|
309 | my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
|
|
310 | |
418 | |
311 | $w->add (my $vb = Gtk2::VBox->new); |
419 | $w->add (my $vb = Gtk2::VBox->new); |
312 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
420 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
313 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
421 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
314 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
422 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
315 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
423 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
316 | my $i = 0; |
424 | my $i = 0; |
317 | my $act = 0; |
425 | my $act = 0; |
318 | for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
426 | for (map { "$_->[0]x$_->[1]" } @SDL_MODES) { |
319 | if ($_ eq $selmode) { $act = $i } |
427 | if ($_ eq $selmode) { $act = $i } |
320 | $cb->append_text ($_); |
428 | $cb->append_text ($_); |
321 | $i++; |
429 | $i++; |
322 | } |
430 | } |
323 | $cb->set_active ($act); |
431 | $cb->set_active ($act); |
… | |
… | |
340 | $cb->signal_connect (clicked => sub { |
448 | $cb->signal_connect (clicked => sub { |
341 | for (keys %$cfg) { |
449 | for (keys %$cfg) { |
342 | $::CFG->{$_} = $cfg->{$_} |
450 | $::CFG->{$_} = $cfg->{$_} |
343 | if $_ ne '_i'; |
451 | if $_ ne '_i'; |
344 | } |
452 | } |
345 | Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
453 | CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; |
346 | }); |
454 | }); |
347 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
455 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
348 | $cb->signal_connect (clicked => sub { |
456 | $cb->signal_connect (clicked => sub { |
349 | for (keys %$cfg) { |
457 | for (keys %$cfg) { |
350 | $::CFG->{$_} = $cfg->{$_} |
458 | $::CFG->{$_} = $cfg->{$_} |
… | |
… | |
353 | my $cb = $events{login} || sub {}; |
461 | my $cb = $events{login} || sub {}; |
354 | $cb->($::CFG->{user}, $::CFG->{password}); |
462 | $cb->($::CFG->{user}, $::CFG->{password}); |
355 | }); |
463 | }); |
356 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
464 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
357 | $cb->signal_connect (clicked => sub { |
465 | $cb->signal_connect (clicked => sub { |
358 | my $cb = $events{login} || sub {}; |
466 | my $cb = $events{logout} || sub {}; |
359 | $cb->(); |
467 | $cb->(); |
360 | }); |
468 | }); |
361 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
469 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
362 | $cb->signal_connect (clicked => sub { $w->destroy }); |
470 | $cb->signal_connect (clicked => sub { $w->destroy }); |
363 | |
471 | |
… | |
… | |
369 | |
477 | |
370 | ############################################################################# |
478 | ############################################################################# |
371 | |
479 | |
372 | SDL::Init SDL_INIT_EVERYTHING; |
480 | SDL::Init SDL_INIT_EVERYTHING; |
373 | |
481 | |
374 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
482 | @SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)], |
|
|
483 | @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE }; |
375 | |
484 | |
376 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
|
|
377 | |
|
|
378 | $TOPLEVEL->add ($mapwidget); |
|
|
379 | $mapwidget->focus_in; |
|
|
380 | |
|
|
381 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
485 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
382 | |
486 | |
383 | $CFG ||= { |
487 | $CFG ||= { |
384 | width => 640, |
488 | width => 640, |
385 | height => 480, |
489 | height => 480, |
386 | fullscreen => 0, |
490 | fullscreen => 0, |
|
|
491 | sdl_mode => 0, |
|
|
492 | mapsize => 100, |
387 | host => "crossfire.schmorp.de", |
493 | host => "crossfire.schmorp.de", |
388 | port => 13327, |
494 | port => 13327, |
389 | }; |
495 | }; |
390 | |
496 | |
391 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
497 | { |
|
|
498 | my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
|
|
499 | |
|
|
500 | CFClient::add_font $_ for @fonts; |
|
|
501 | CFClient::set_font $fonts[0]; |
|
|
502 | } |
392 | |
503 | |
393 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
504 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
394 | |
505 | |
395 | run_config_dialog |
506 | init_screen; |
396 | login => sub { start_game }, |
|
|
397 | logout => sub { stop_game }; |
|
|
398 | |
507 | |
399 | main Gtk2; |
508 | main Gtk2; |
400 | |
509 | |
401 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |
510 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |
|
|
511 | |