1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
|
|
3 | use strict; |
2 | use strict; |
4 | |
3 | |
5 | use Crossfire::Client; |
4 | use Glib; |
6 | use Crossfire::Protocol; |
5 | use Gtk2 -init; |
7 | |
|
|
8 | use Client::Util; |
|
|
9 | |
|
|
10 | package Crossfire::Client; # uh, yeah |
|
|
11 | |
|
|
12 | use strict; |
|
|
13 | |
|
|
14 | our $CFG; |
|
|
15 | our $VERSION = '0.1'; |
|
|
16 | |
6 | |
17 | use SDL; |
7 | use SDL; |
18 | use SDL::App; |
8 | use SDL::App; |
19 | use SDL::Event; |
9 | use SDL::Event; |
20 | use SDL::Surface; |
10 | use SDL::Surface; |
21 | use SDL::OpenGL; |
11 | use SDL::OpenGL; |
22 | use SDL::OpenGL::Constants; |
12 | use SDL::OpenGL::Constants; |
23 | |
13 | |
24 | my $conn; |
14 | use Crossfire; |
25 | my $app; |
15 | use Crossfire::Client; |
|
|
16 | use Crossfire::Protocol; |
26 | |
17 | |
27 | my $WIDTH = 640; |
18 | use Crossfire::Client::Util; |
28 | my $HEIGHT = 480; |
19 | use Crossfire::Client::Widget; |
29 | |
20 | |
30 | sub glinit { |
21 | our $FACECACHE; |
31 | # nuke all gl context data |
22 | |
32 | |
23 | our $VERSION = '0.1'; |
|
|
24 | |
|
|
25 | our $CFG; |
|
|
26 | our $CONN; |
|
|
27 | |
|
|
28 | our $SDL_TIMER; |
|
|
29 | our $SDL_APP; |
|
|
30 | our $SDL_EV = new SDL::Event; |
|
|
31 | our %SDL_CB; |
|
|
32 | |
|
|
33 | our @GL_INIT; # hooks called on every gl init |
|
|
34 | |
|
|
35 | sub init_screen { |
33 | $app = new SDL::App |
36 | $SDL_APP = new SDL::App |
34 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
37 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
35 | -title => "Crossfire+ Client", |
38 | -title => "Crossfire+ Client", |
36 | -width => $WIDTH, |
39 | -width => $CFG->{width}, |
37 | -height => $HEIGHT, |
40 | -height => $CFG->{height}, |
38 | -opengl => 1, |
41 | -opengl => 1, |
39 | -red_size => 8, |
42 | -red_size => 8, |
40 | -green_size => 8, |
43 | -green_size => 8, |
41 | -blue_size => 8, |
44 | -blue_size => 8, |
42 | -double_buffer => 1, |
45 | -double_buffer => 1, |
|
|
46 | -fullscreen => $CFG->{fullscreen}, |
43 | -resizeable => 0; |
47 | -resizeable => 0; |
|
|
48 | |
|
|
49 | glClearColor 0, 0, 0, 0; |
44 | |
50 | |
45 | glEnable GL_TEXTURE_2D; |
51 | glEnable GL_TEXTURE_2D; |
46 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
52 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
47 | glShadeModel GL_FLAT; |
53 | glShadeModel GL_FLAT; |
48 | glDisable GL_DEPTH_TEST; |
54 | glDisable GL_DEPTH_TEST; |
49 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
55 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
50 | glEnable GL_BLEND; |
56 | glEnable GL_BLEND; |
51 | |
57 | |
52 | glMatrixMode GL_PROJECTION; |
58 | glMatrixMode GL_PROJECTION; |
53 | glLoadIdentity; |
59 | glLoadIdentity; |
54 | glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; |
60 | glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100; |
55 | |
61 | |
56 | # re-bind all textures |
62 | glMatrixMode GL_MODELVIEW; |
|
|
63 | |
|
|
64 | $_->() for @GL_INIT; |
|
|
65 | } |
|
|
66 | |
|
|
67 | sub start_game { |
|
|
68 | $SDL_TIMER = add Glib::Timeout 1000/20, sub { |
|
|
69 | while ($SDL_EV->poll) { |
|
|
70 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); |
|
|
71 | } |
|
|
72 | |
|
|
73 | 1 |
|
|
74 | }; |
|
|
75 | |
|
|
76 | init_screen; |
|
|
77 | |
|
|
78 | $CONN = new conn |
|
|
79 | host => $CFG->{host}, |
|
|
80 | port => $CFG->{port}; |
|
|
81 | } |
|
|
82 | |
|
|
83 | sub stop_game { |
|
|
84 | remove Glib::Source $SDL_TIMER; |
|
|
85 | |
|
|
86 | undef $SDL_APP; |
|
|
87 | SDL::Quit; |
57 | } |
88 | } |
58 | |
89 | |
59 | sub refresh { |
90 | sub refresh { |
60 | glClearColor 0, 0, 0, 0; |
|
|
61 | glClear GL_COLOR_BUFFER_BIT; |
91 | glClear GL_COLOR_BUFFER_BIT; |
62 | |
92 | |
63 | my $map = $conn->{map}; |
93 | for (values %Client::Widget::ACTIVE_WIDGETS) { |
64 | |
94 | $_->draw |
65 | for my $x (0 .. $conn->{mapw} - 1) { |
|
|
66 | for my $y (0 .. $conn->{maph} - 1) { |
|
|
67 | |
|
|
68 | my $cell = $map->[$x][$y] |
|
|
69 | or next; |
|
|
70 | |
|
|
71 | my $darkness = $cell->[3] * (1 / 255); |
|
|
72 | my $darkness = 0.8 + 0.2*rand; |
|
|
73 | glColor $darkness, $darkness, $darkness; |
|
|
74 | |
|
|
75 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
|
|
76 | my $tex = $conn->{face}[$num]{texture} || 0; |
|
|
77 | |
|
|
78 | glBindTexture GL_TEXTURE_2D, $tex; |
|
|
79 | |
|
|
80 | glBegin GL_QUADS; |
|
|
81 | glTexCoord 0, 0; glVertex $x, $y; |
|
|
82 | glTexCoord 0, 1; glVertex $x, $y + 1; |
|
|
83 | glTexCoord 1, 1; glVertex $x + 1, $y + 1; |
|
|
84 | glTexCoord 1, 0; glVertex $x + 1, $y; |
|
|
85 | glEnd; |
|
|
86 | } |
|
|
87 | } |
|
|
88 | } |
95 | } |
89 | |
96 | |
90 | SDL::GLSwapBuffers; |
97 | SDL::GLSwapBuffers; |
91 | } |
98 | } |
92 | |
99 | |
93 | my $ev = new SDL::Event; |
100 | %SDL_CB = ( |
94 | my %ev_cb; |
101 | SDL_QUIT() => sub { |
95 | |
102 | warn "sdl quit\n";#d# |
96 | sub event(&$) { |
103 | main_quit Gtk2; |
97 | $ev_cb{$_[0]->()} = $_[1]; |
104 | }, |
98 | } |
105 | SDL_VIDEORESIZE() => sub { |
99 | |
106 | }, |
100 | sub does(&) { shift } |
107 | SDL_VIDEOEXPOSE() => sub { |
101 | |
|
|
102 | event {SDL_QUIT} does { |
|
|
103 | exit; |
|
|
104 | }; |
|
|
105 | |
|
|
106 | event {SDL_VIDEORESIZE} does { |
|
|
107 | print "resize\n"; |
|
|
108 | }; |
|
|
109 | |
|
|
110 | event {SDL_VIDEOEXPOSE} does { |
|
|
111 | refresh; |
108 | refresh; |
112 | }; |
109 | }, |
113 | |
110 | SDL_KEYDOWN() => sub { |
114 | event {SDL_KEYDOWN} does { |
111 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
115 | print "keypress\n"; |
112 | # alt-enter |
116 | }; |
113 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
117 | |
114 | init_screen; |
118 | event {SDL_KEYUP} does { |
115 | } else { |
119 | print "keyup\n";#d# |
116 | Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
120 | }; |
117 | } |
121 | |
118 | }, |
122 | event {SDL_MOUSEMOTION} does { |
119 | SDL_KEYUP() => sub { |
123 | print "motion\n"; |
120 | Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
124 | }; |
121 | }, |
125 | |
122 | SDL_MOUSEMOTION() => sub { |
126 | event {SDL_MOUSEBUTTONDOWN} does { |
123 | warn "sdl motion\n";#d# |
127 | print "button\n"; |
124 | }, |
128 | }; |
125 | SDL_MOUSEBUTTONDOWN() => sub { |
129 | |
126 | Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
130 | event {SDL_MOUSEBUTTONUP} does { |
127 | }, |
131 | print "buttup\n"; |
128 | SDL_MOUSEBUTTONUP() => sub { |
132 | }; |
129 | Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
133 | |
130 | }, |
134 | event {SDL_ACTIVEEVENT} does { |
131 | SDL_ACTIVEEVENT() => sub { |
135 | print "active\n"; |
132 | warn "active\n";#d# |
136 | }; |
133 | }, |
137 | |
134 | ); |
138 | package Crossfire::Client; |
|
|
139 | |
135 | |
140 | @conn::ISA = Crossfire::Protocol::; |
136 | @conn::ISA = Crossfire::Protocol::; |
141 | |
137 | |
142 | sub conn::map_update { |
138 | sub conn::map_update { |
143 | my ($self, $dirty) = @_; |
139 | my ($self, $dirty) = @_; |
… | |
… | |
155 | my ($self) = @_; |
151 | my ($self) = @_; |
156 | |
152 | |
157 | refresh; |
153 | refresh; |
158 | } |
154 | } |
159 | |
155 | |
|
|
156 | sub conn::face_find { |
|
|
157 | my ($self, $face) = @_; |
|
|
158 | |
|
|
159 | $FACECACHE->{"$face->{chksum},$face->{name}"} |
|
|
160 | } |
|
|
161 | |
160 | sub conn::face_update { |
162 | sub conn::face_update { |
161 | my ($self, $num, $face) = @_; |
163 | my ($self, $face) = @_; |
162 | |
164 | |
163 | warn "up face $self,$num,$face\n";#d# |
165 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
164 | use Gtk2; |
|
|
165 | |
166 | |
166 | my $pb = new Gtk2::Gdk::PixbufLoader; |
167 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
167 | $pb->write ($face->{image}); |
|
|
168 | $pb->close; |
|
|
169 | |
|
|
170 | $pb = $pb->get_pixbuf; |
|
|
171 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
172 | |
|
|
173 | glGetError(); |
|
|
174 | my ($tex) = @{glGenTextures 1}; |
|
|
175 | |
|
|
176 | $face->{texture} = $tex; |
|
|
177 | |
|
|
178 | glBindTexture GL_TEXTURE_2D, $tex; |
|
|
179 | my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; |
|
|
180 | |
|
|
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
182 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
|
|
183 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
|
|
184 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
185 | |
|
|
186 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
187 | GL_RGBA8, |
|
|
188 | $pb->get_width, $pb->get_height, |
|
|
189 | 0, |
|
|
190 | GL_RGBA, |
|
|
191 | GL_UNSIGNED_BYTE, |
|
|
192 | $pb->get_pixels; |
|
|
193 | my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; |
|
|
194 | } |
168 | } |
195 | |
169 | |
196 | ############################################################################# |
170 | ############################################################################# |
197 | |
171 | |
198 | use Event; |
172 | SDL::Init(SDL_INIT_EVERYTHING()); |
199 | |
173 | |
200 | Client::Util::read_cfg "$Crossfire::VARDIR/pclient"; |
174 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
201 | |
175 | |
202 | glinit; |
176 | #find_rcfile "uifont.ttf"; |
203 | |
177 | |
204 | $conn = new conn |
178 | $mapwidget->activate; |
205 | host => "cf.schmorp.de", |
179 | $mapwidget->focus_in; |
206 | port => 13327; |
|
|
207 | |
180 | |
208 | Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { |
181 | read_cfg "$Crossfire::VARDIR/pclientrc"; |
209 | while ($ev->poll) { |
|
|
210 | ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); |
|
|
211 | } |
|
|
212 | }); |
|
|
213 | |
182 | |
214 | Event::loop; |
183 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
215 | |
184 | |
|
|
185 | $CFG ||= { |
|
|
186 | width => 640, |
|
|
187 | height => 480, |
|
|
188 | fullscreen => 0, |
|
|
189 | host => "crossfire.schmorp.de", |
|
|
190 | port => 13327, |
|
|
191 | }; |
|
|
192 | |
|
|
193 | Crossfire::Client::Util::run_config_dialog |
|
|
194 | login => sub { start_game }, |
|
|
195 | logout => sub { stop_game }; |
|
|
196 | |
|
|
197 | main Gtk2; |
|
|
198 | |
|
|
199 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |