1 |
root |
1.1 |
#!/opt/bin/perl |
2 |
|
|
|
3 |
root |
1.2 |
use strict; |
4 |
|
|
|
5 |
elmex |
1.11 |
use Crossfire; |
6 |
root |
1.2 |
use Crossfire::Client; |
7 |
|
|
use Crossfire::Protocol; |
8 |
|
|
|
9 |
elmex |
1.10 |
use Client::Util; |
10 |
|
|
|
11 |
root |
1.2 |
package Crossfire::Client; # uh, yeah |
12 |
|
|
|
13 |
|
|
use strict; |
14 |
|
|
|
15 |
elmex |
1.10 |
our $CFG; |
16 |
|
|
our $VERSION = '0.1'; |
17 |
|
|
|
18 |
root |
1.1 |
use SDL; |
19 |
|
|
use SDL::App; |
20 |
|
|
use SDL::Event; |
21 |
|
|
use SDL::Surface; |
22 |
root |
1.2 |
use SDL::OpenGL; |
23 |
|
|
use SDL::OpenGL::Constants; |
24 |
root |
1.1 |
|
25 |
|
|
my $conn; |
26 |
root |
1.2 |
my $app; |
27 |
|
|
|
28 |
|
|
my $WIDTH = 640; |
29 |
|
|
my $HEIGHT = 480; |
30 |
|
|
|
31 |
|
|
sub glinit { |
32 |
|
|
# nuke all gl context data |
33 |
|
|
|
34 |
|
|
$app = new SDL::App |
35 |
root |
1.5 |
-flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
36 |
|
|
-title => "Crossfire+ Client", |
37 |
|
|
-width => $WIDTH, |
38 |
|
|
-height => $HEIGHT, |
39 |
|
|
-opengl => 1, |
40 |
|
|
-red_size => 8, |
41 |
|
|
-green_size => 8, |
42 |
|
|
-blue_size => 8, |
43 |
root |
1.2 |
-double_buffer => 1, |
44 |
root |
1.5 |
-resizeable => 0; |
45 |
root |
1.2 |
|
46 |
|
|
glEnable GL_TEXTURE_2D; |
47 |
root |
1.9 |
glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
48 |
root |
1.2 |
glShadeModel GL_FLAT; |
49 |
|
|
glDisable GL_DEPTH_TEST; |
50 |
root |
1.9 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
51 |
root |
1.8 |
glEnable GL_BLEND; |
52 |
root |
1.2 |
|
53 |
root |
1.9 |
glMatrixMode GL_PROJECTION; |
54 |
root |
1.4 |
glLoadIdentity; |
55 |
|
|
glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; |
56 |
|
|
|
57 |
root |
1.2 |
# re-bind all textures |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
sub refresh { |
61 |
root |
1.8 |
glClearColor 0, 0, 0, 0; |
62 |
root |
1.5 |
glClear GL_COLOR_BUFFER_BIT; |
63 |
root |
1.2 |
|
64 |
|
|
my $map = $conn->{map}; |
65 |
root |
1.1 |
|
66 |
root |
1.2 |
for my $x (0 .. $conn->{mapw} - 1) { |
67 |
|
|
for my $y (0 .. $conn->{maph} - 1) { |
68 |
root |
1.1 |
|
69 |
root |
1.2 |
my $cell = $map->[$x][$y] |
70 |
|
|
or next; |
71 |
|
|
|
72 |
root |
1.9 |
my $darkness = $cell->[3] * (1 / 255); |
73 |
|
|
my $darkness = 0.8 + 0.2*rand; |
74 |
|
|
glColor $darkness, $darkness, $darkness; |
75 |
root |
1.8 |
|
76 |
root |
1.2 |
for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
77 |
root |
1.3 |
my $tex = $conn->{face}[$num]{texture} || 0; |
78 |
root |
1.2 |
|
79 |
|
|
glBindTexture GL_TEXTURE_2D, $tex; |
80 |
|
|
|
81 |
|
|
glBegin GL_QUADS; |
82 |
|
|
glTexCoord 0, 0; glVertex $x, $y; |
83 |
root |
1.8 |
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 |
root |
1.2 |
glEnd; |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
} |
90 |
root |
1.1 |
|
91 |
root |
1.2 |
SDL::GLSwapBuffers; |
92 |
root |
1.1 |
} |
93 |
|
|
|
94 |
|
|
my $ev = new SDL::Event; |
95 |
|
|
my %ev_cb; |
96 |
|
|
|
97 |
|
|
sub event(&$) { |
98 |
|
|
$ev_cb{$_[0]->()} = $_[1]; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
sub does(&) { shift } |
102 |
|
|
|
103 |
|
|
event {SDL_QUIT} does { |
104 |
|
|
exit; |
105 |
|
|
}; |
106 |
|
|
|
107 |
|
|
event {SDL_VIDEORESIZE} does { |
108 |
|
|
print "resize\n"; |
109 |
|
|
}; |
110 |
|
|
|
111 |
root |
1.4 |
event {SDL_VIDEOEXPOSE} does { |
112 |
|
|
refresh; |
113 |
|
|
}; |
114 |
|
|
|
115 |
root |
1.1 |
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"; |
137 |
|
|
}; |
138 |
|
|
|
139 |
root |
1.2 |
package Crossfire::Client; |
140 |
root |
1.1 |
|
141 |
root |
1.2 |
@conn::ISA = Crossfire::Protocol::; |
142 |
root |
1.1 |
|
143 |
root |
1.2 |
sub conn::map_update { |
144 |
root |
1.1 |
my ($self, $dirty) = @_; |
145 |
|
|
|
146 |
root |
1.2 |
refresh; |
147 |
root |
1.1 |
} |
148 |
|
|
|
149 |
root |
1.2 |
sub conn::map_scroll { |
150 |
root |
1.1 |
my ($self, $dx, $dy) = @_; |
151 |
|
|
|
152 |
root |
1.2 |
refresh; |
153 |
root |
1.1 |
} |
154 |
|
|
|
155 |
root |
1.2 |
sub conn::map_clear { |
156 |
root |
1.1 |
my ($self) = @_; |
157 |
|
|
|
158 |
root |
1.2 |
refresh; |
159 |
root |
1.1 |
} |
160 |
|
|
|
161 |
root |
1.2 |
sub conn::face_update { |
162 |
root |
1.1 |
my ($self, $num, $face) = @_; |
163 |
|
|
|
164 |
|
|
warn "up face $self,$num,$face\n";#d# |
165 |
root |
1.9 |
use Gtk2; |
166 |
root |
1.1 |
|
167 |
root |
1.9 |
my $pb = new Gtk2::Gdk::PixbufLoader; |
168 |
|
|
$pb->write ($face->{image}); |
169 |
|
|
$pb->close; |
170 |
root |
1.1 |
|
171 |
root |
1.9 |
$pb = $pb->get_pixbuf; |
172 |
|
|
$pb = $pb->add_alpha (0, 0, 0, 0); |
173 |
root |
1.1 |
|
174 |
root |
1.9 |
glGetError(); |
175 |
root |
1.2 |
my ($tex) = @{glGenTextures 1}; |
176 |
root |
1.1 |
|
177 |
root |
1.2 |
$face->{texture} = $tex; |
178 |
|
|
|
179 |
|
|
glBindTexture GL_TEXTURE_2D, $tex; |
180 |
|
|
my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; |
181 |
|
|
|
182 |
root |
1.5 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
183 |
elmex |
1.7 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
184 |
root |
1.6 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
185 |
|
|
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
186 |
root |
1.2 |
|
187 |
|
|
glTexImage2D GL_TEXTURE_2D, 0, |
188 |
root |
1.9 |
GL_RGBA8, |
189 |
|
|
$pb->get_width, $pb->get_height, |
190 |
root |
1.2 |
0, |
191 |
|
|
GL_RGBA, |
192 |
|
|
GL_UNSIGNED_BYTE, |
193 |
root |
1.9 |
$pb->get_pixels; |
194 |
root |
1.2 |
my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; |
195 |
root |
1.1 |
} |
196 |
|
|
|
197 |
|
|
############################################################################# |
198 |
|
|
|
199 |
|
|
use Event; |
200 |
|
|
|
201 |
elmex |
1.11 |
Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; |
202 |
elmex |
1.10 |
|
203 |
elmex |
1.12 |
|
204 |
|
|
if ($ARGV[0] eq 'config') { |
205 |
|
|
Client::Util::run_config_dialog; |
206 |
|
|
} |
207 |
|
|
|
208 |
root |
1.2 |
glinit; |
209 |
|
|
|
210 |
root |
1.1 |
$conn = new conn |
211 |
|
|
host => "cf.schmorp.de", |
212 |
|
|
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 |
|
|
}); |
219 |
|
|
|
220 |
|
|
Event::loop; |
221 |
|
|
|