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