1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
|
|
4 | use utf8; |
4 | |
5 | |
5 | use Glib; |
6 | use Glib; |
6 | use Gtk2 -init; |
7 | use Gtk2 -init; |
7 | |
8 | |
8 | use SDL; |
9 | use SDL; |
… | |
… | |
14 | |
15 | |
15 | use Crossfire; |
16 | use Crossfire; |
16 | use Crossfire::Client; |
17 | use Crossfire::Client; |
17 | use Crossfire::Protocol; |
18 | use Crossfire::Protocol; |
18 | |
19 | |
19 | use Client::Util; |
20 | use Crossfire::Client::Widget; |
|
|
21 | |
|
|
22 | our $FACECACHE; |
20 | |
23 | |
21 | our $VERSION = '0.1'; |
24 | our $VERSION = '0.1'; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
22 | |
27 | |
23 | our $CFG; |
28 | our $CFG; |
24 | our $CONN; |
29 | our $CONN; |
|
|
30 | |
|
|
31 | our $WIDTH; |
|
|
32 | our $HEIGHT; |
|
|
33 | our $FULLSCREEN; |
|
|
34 | |
|
|
35 | our $UIFONT; |
25 | |
36 | |
26 | our $SDL_TIMER; |
37 | our $SDL_TIMER; |
27 | our $SDL_APP; |
38 | our $SDL_APP; |
28 | our $SDL_EV = new SDL::Event; |
39 | our $SDL_EV = new SDL::Event; |
29 | our %SDL_CB; |
40 | our %SDL_CB; |
30 | |
41 | |
|
|
42 | our @GL_INIT; # hooks called on every gl init |
|
|
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
|
|
45 | |
|
|
46 | our $tw; |
|
|
47 | |
31 | sub init_screen { |
48 | sub init_screen { |
32 | # nuke all gl context data |
|
|
33 | |
|
|
34 | $SDL_APP = new SDL::App |
49 | $SDL_APP = new SDL::App |
35 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
50 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
36 | -title => "Crossfire+ Client", |
51 | -title => "Crossfire+ Client", |
37 | -width => $CFG->{width}, |
52 | -width => $WIDTH, |
38 | -height => $CFG->{height}, |
53 | -height => $HEIGHT, |
39 | -opengl => 1, |
54 | -opengl => 1, |
40 | -red_size => 8, |
55 | -red_size => 8, |
41 | -green_size => 8, |
56 | -green_size => 8, |
42 | -blue_size => 8, |
57 | -blue_size => 8, |
43 | -double_buffer => 1, |
58 | -double_buffer => 1, |
44 | -fullscreen => $CFG->{fullscreen}, |
59 | -fullscreen => $FULLSCREEN, |
45 | -resizeable => 0; |
60 | -resizeable => 0; |
46 | |
61 | |
|
|
62 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
|
|
63 | |
|
|
64 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
65 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
66 | |
|
|
67 | $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $HEIGHT / 40 |
|
|
68 | or die "TTFOpenFont: $!"; |
|
|
69 | |
|
|
70 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Alt-Enter toggles fullscreen mode"; |
|
|
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 | |
|
|
81 | glClearColor 0, 0, 0, 0; |
|
|
82 | |
47 | glEnable GL_TEXTURE_2D; |
83 | glEnable GL_TEXTURE_2D; |
48 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
49 | glShadeModel GL_FLAT; |
84 | glShadeModel GL_FLAT; |
50 | glDisable GL_DEPTH_TEST; |
85 | glDisable GL_DEPTH_TEST; |
51 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
86 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
52 | glEnable GL_BLEND; |
|
|
53 | |
87 | |
54 | glMatrixMode GL_PROJECTION; |
88 | glMatrixMode GL_PROJECTION; |
55 | glLoadIdentity; |
89 | glLoadIdentity; |
56 | glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1; |
90 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
57 | |
91 | |
58 | # re-bind all textures |
92 | glMatrixMode GL_MODELVIEW; |
|
|
93 | |
|
|
94 | $_->() for @GL_INIT; |
59 | } |
95 | } |
60 | |
96 | |
61 | sub start_game { |
97 | sub start_game { |
62 | $SDL_TIMER = add Glib::Timeout 1000/20, sub { |
98 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
63 | while ($SDL_EV->poll) { |
|
|
64 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); |
99 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
65 | } |
100 | while $SDL_EV->poll; |
66 | |
101 | |
67 | 1 |
102 | 1 |
68 | }; |
103 | }; |
69 | |
104 | |
|
|
105 | $WIDTH = $CFG->{width}; |
|
|
106 | $HEIGHT = $CFG->{height}; |
|
|
107 | $FULLSCREEN = 0; |
|
|
108 | |
70 | init_screen; |
109 | init_screen; |
71 | |
110 | |
72 | $CONN = new conn |
111 | $CONN = new conn |
73 | host => $CFG->{host}, |
112 | host => $CFG->{host}, |
74 | port => $CFG->{port}; |
113 | port => $CFG->{port}, |
|
|
114 | user => $CFG->{user}, |
|
|
115 | pass => $CFG->{password}; |
75 | } |
116 | } |
76 | |
117 | |
77 | sub stop_game { |
118 | sub stop_game { |
78 | remove Glib::Source $SDL_TIMER; |
119 | remove Glib::Source $SDL_TIMER; |
79 | |
120 | |
80 | undef $SDL_APP; |
121 | undef $SDL_APP; |
81 | SDL::Quit; |
122 | SDL::Quit; |
82 | } |
123 | } |
83 | |
124 | |
|
|
125 | |
|
|
126 | sub force_refresh { |
|
|
127 | glViewport 0, 0, $WIDTH, $HEIGHT; |
|
|
128 | glClear GL_COLOR_BUFFER_BIT; |
|
|
129 | |
|
|
130 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
|
|
131 | |
|
|
132 | SDL::GLSwapBuffers; |
|
|
133 | } |
|
|
134 | |
|
|
135 | my $refresh_handler; |
|
|
136 | |
84 | sub refresh { |
137 | sub refresh { |
85 | glClearColor 0, 0, 0, 0; |
138 | $refresh_handler ||= add Glib::Idle sub { |
86 | glClear GL_COLOR_BUFFER_BIT; |
139 | force_refresh; |
87 | |
140 | undef $refresh_handler; |
88 | my $map = $CONN->{map}; |
|
|
89 | |
|
|
90 | for my $x (0 .. $CONN->{mapw} - 1) { |
|
|
91 | for my $y (0 .. $CONN->{maph} - 1) { |
|
|
92 | |
|
|
93 | my $cell = $map->[$x][$y] |
|
|
94 | or next; |
|
|
95 | |
|
|
96 | my $darkness = $cell->[3] * (1 / 255); |
|
|
97 | glColor $darkness, $darkness, $darkness; |
|
|
98 | |
|
|
99 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
|
|
100 | my $tex = $CONN->{face}[$num]{texture} || 0; |
|
|
101 | |
|
|
102 | glBindTexture GL_TEXTURE_2D, $tex; |
|
|
103 | |
|
|
104 | glBegin GL_QUADS; |
|
|
105 | glTexCoord 0, 0; glVertex $x, $y; |
|
|
106 | glTexCoord 0, 1; glVertex $x, $y + 1; |
|
|
107 | glTexCoord 1, 1; glVertex $x + 1, $y + 1; |
|
|
108 | glTexCoord 1, 0; glVertex $x + 1, $y; |
|
|
109 | glEnd; |
|
|
110 | } |
|
|
111 | } |
141 | 0 |
112 | } |
142 | }; |
113 | |
|
|
114 | SDL::GLSwapBuffers; |
|
|
115 | } |
143 | } |
116 | |
144 | |
117 | %SDL_CB = ( |
145 | %SDL_CB = ( |
118 | SDL_QUIT() => sub { |
146 | SDL_QUIT() => sub { |
119 | warn "sdl quit\n";#d# |
147 | main_quit Gtk2; |
120 | exit; |
|
|
121 | }, |
148 | }, |
122 | SDL_VIDEORESIZE() => sub { |
149 | SDL_VIDEORESIZE() => sub { |
123 | }, |
150 | }, |
124 | SDL_VIDEOEXPOSE() => sub { |
151 | SDL_VIDEOEXPOSE() => sub { |
125 | refresh; |
152 | refresh; |
126 | }, |
153 | }, |
127 | SDL_KEYDOWN() => sub { |
154 | SDL_KEYDOWN() => sub { |
|
|
155 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
|
|
156 | # alt-enter |
|
|
157 | $FULLSCREEN = !$FULLSCREEN; |
|
|
158 | init_screen; |
|
|
159 | } else { |
|
|
160 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
|
|
161 | } |
128 | }, |
162 | }, |
129 | SDL_KEYUP() => sub { |
163 | SDL_KEYUP() => sub { |
|
|
164 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
130 | }, |
165 | }, |
131 | SDL_MOUSEMOTION() => sub { |
166 | SDL_MOUSEMOTION() => sub { |
132 | warn "sdl motion\n";#d# |
167 | warn "sdl motion\n";#d# |
133 | }, |
168 | }, |
134 | SDL_MOUSEBUTTONDOWN() => sub { |
169 | SDL_MOUSEBUTTONDOWN() => sub { |
|
|
170 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
135 | }, |
171 | }, |
136 | SDL_MOUSEBUTTONUP() => sub { |
172 | SDL_MOUSEBUTTONUP() => sub { |
|
|
173 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
137 | }, |
174 | }, |
138 | SDL_ACTIVEEVENT() => sub { |
175 | SDL_ACTIVEEVENT() => sub { |
139 | warn "active\n";#d# |
176 | warn "active\n";#d# |
140 | }, |
177 | }, |
141 | ); |
178 | ); |
… | |
… | |
158 | my ($self) = @_; |
195 | my ($self) = @_; |
159 | |
196 | |
160 | refresh; |
197 | refresh; |
161 | } |
198 | } |
162 | |
199 | |
|
|
200 | sub conn::face_find { |
|
|
201 | my ($self, $face) = @_; |
|
|
202 | |
|
|
203 | $FACECACHE->{"$face->{chksum},$face->{name}"} |
|
|
204 | } |
|
|
205 | |
163 | sub conn::face_update { |
206 | sub conn::face_update { |
164 | my ($self, $num, $face) = @_; |
207 | my ($self, $face) = @_; |
165 | |
208 | |
166 | my $pb = new Gtk2::Gdk::PixbufLoader; |
209 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
167 | $pb->write ($face->{image}); |
|
|
168 | $pb->close; |
|
|
169 | |
210 | |
170 | $pb = $pb->get_pixbuf; |
211 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
171 | $pb = $pb->add_alpha (0, 0, 0, 0); |
212 | } |
172 | |
213 | |
173 | glGetError(); |
214 | sub conn::query { |
174 | my ($tex) = @{glGenTextures 1}; |
215 | my ($self, $flags, $prompt) = @_; |
175 | |
216 | |
176 | $face->{texture} = $tex; |
217 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
|
|
218 | } |
|
|
219 | |
|
|
220 | sub gtk_add_cfg_field { |
|
|
221 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
|
|
222 | my $i = $cfg->{_i}++; |
|
|
223 | $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
|
|
224 | $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
|
|
225 | if ($key eq 'password') { |
|
|
226 | $ent->set_invisible_char ("*"); |
|
|
227 | $ent->set (visibility => 0) |
|
|
228 | } |
|
|
229 | $ent->set_text ($value); |
|
|
230 | $ent->signal_connect (changed => sub { |
|
|
231 | my ($ent) = @_; |
|
|
232 | $cfg->{$key} = $ent->get_text; |
|
|
233 | }); |
|
|
234 | } |
|
|
235 | |
|
|
236 | sub run_config_dialog { |
|
|
237 | my (%events) = @_; |
|
|
238 | |
|
|
239 | my $w = Gtk2::Window->new; |
|
|
240 | |
|
|
241 | my @cfg = ( |
|
|
242 | [qw/Host host/], |
|
|
243 | [qw/Port port/], |
|
|
244 | [qw/Username user/], |
|
|
245 | [qw/Password password/], |
177 | |
246 | ); |
178 | glBindTexture GL_TEXTURE_2D, $tex; |
247 | |
179 | my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; |
248 | my $cfg = {}; |
|
|
249 | |
|
|
250 | my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
|
|
251 | my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
|
|
252 | |
|
|
253 | $w->add (my $vb = Gtk2::VBox->new); |
|
|
254 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
|
|
255 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
|
|
256 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
|
|
257 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
|
|
258 | my $i = 0; |
|
|
259 | my $act = 0; |
|
|
260 | for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
|
|
261 | if ($_ eq $selmode) { $act = $i } |
|
|
262 | $cb->append_text ($_); |
|
|
263 | $i++; |
|
|
264 | } |
|
|
265 | $cb->set_active ($act); |
|
|
266 | $cb->signal_connect (changed => sub { |
|
|
267 | my ($cb) = @_; |
|
|
268 | my $txt = $cb->get_active_text; |
|
|
269 | if ($txt =~ m/(\d+)x(\d+)/) { |
|
|
270 | $::CFG->{width} = $1; |
|
|
271 | $::CFG->{height} = $2; |
180 | |
272 | } |
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
273 | }); |
182 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
274 | |
183 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
275 | $cfg->{_i} = 1; |
184 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
276 | for (@cfg) { |
185 | |
277 | gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
186 | glTexImage2D GL_TEXTURE_2D, 0, |
278 | } |
187 | GL_RGBA8, |
279 | |
188 | $pb->get_width, $pb->get_height, |
280 | $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
|
|
281 | $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
|
|
282 | $cb->signal_connect (clicked => sub { |
|
|
283 | for (keys %$cfg) { |
|
|
284 | $::CFG->{$_} = $cfg->{$_} |
|
|
285 | if $_ ne '_i'; |
189 | 0, |
286 | } |
|
|
287 | Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
288 | }); |
|
|
289 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
|
|
290 | $cb->signal_connect (clicked => sub { |
|
|
291 | for (keys %$cfg) { |
|
|
292 | $::CFG->{$_} = $cfg->{$_} |
|
|
293 | if $_ ne '_i'; |
190 | GL_RGBA, |
294 | } |
191 | GL_UNSIGNED_BYTE, |
295 | my $cb = $events{login} || sub {}; |
192 | $pb->get_pixels; |
296 | $cb->($::CFG->{user}, $::CFG->{password}); |
193 | my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; |
297 | }); |
|
|
298 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
|
|
299 | $cb->signal_connect (clicked => sub { |
|
|
300 | my $cb = $events{login} || sub {}; |
|
|
301 | $cb->(); |
|
|
302 | }); |
|
|
303 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
|
|
304 | $cb->signal_connect (clicked => sub { $w->destroy }); |
|
|
305 | |
|
|
306 | $w->show_all; |
|
|
307 | |
|
|
308 | $w->signal_connect (destroy => sub { Gtk2->main_quit }); |
194 | } |
309 | } |
|
|
310 | |
195 | |
311 | |
196 | ############################################################################# |
312 | ############################################################################# |
197 | |
313 | |
|
|
314 | SDL::Init SDL_INIT_EVERYTHING; |
|
|
315 | SDL::TTFInit; |
|
|
316 | |
|
|
317 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
|
|
318 | |
|
|
319 | $mapwidget->activate; |
|
|
320 | $mapwidget->focus_in; |
|
|
321 | |
198 | Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; |
322 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
199 | |
323 | |
200 | $CFG ||= { |
324 | $CFG ||= { |
201 | width => 640, |
325 | width => 640, |
202 | height => 480, |
326 | height => 480, |
203 | fullscreen => 0, |
327 | fullscreen => 0, |
204 | host => "crossfire.schmorp.de", |
328 | host => "crossfire.schmorp.de", |
205 | port => 13327, |
329 | port => 13327, |
206 | }; |
330 | }; |
207 | |
331 | |
208 | Client::Util::run_config_dialog |
332 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
209 | login => sub { print "Login $_[0] with $_[1]\n" }, |
333 | |
210 | logout => sub { print "Logout" }; |
334 | run_config_dialog |
|
|
335 | login => sub { start_game }, |
|
|
336 | logout => sub { stop_game }; |
211 | |
337 | |
212 | main Gtk2; |
338 | main Gtk2; |
213 | |
339 | |
|
|
340 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |