1 |
#!/opt/bin/perl |
2 |
|
3 |
use strict; |
4 |
|
5 |
use Crossfire::Client; |
6 |
use Crossfire::Protocol; |
7 |
|
8 |
package Crossfire::Client; # uh, yeah |
9 |
|
10 |
use strict; |
11 |
|
12 |
use SDL; |
13 |
use SDL::App; |
14 |
use SDL::Event; |
15 |
use SDL::Surface; |
16 |
use SDL::OpenGL; |
17 |
use SDL::OpenGL::Constants; |
18 |
|
19 |
my $conn; |
20 |
my $app; |
21 |
|
22 |
my $WIDTH = 640; |
23 |
my $HEIGHT = 480; |
24 |
|
25 |
sub glinit { |
26 |
# nuke all gl context data |
27 |
|
28 |
$app = new SDL::App |
29 |
-title => "Crossfire+ Client", |
30 |
-width => $WIDTH, |
31 |
-height => $HEIGHT, |
32 |
-depth => 24, |
33 |
-opengl => 1, |
34 |
-double_buffer => 1, |
35 |
-resizeable => 0; |
36 |
|
37 |
glEnable GL_TEXTURE_2D; |
38 |
glShadeModel GL_FLAT; |
39 |
glDisable GL_DEPTH_TEST; |
40 |
glMatrixMode GL_PROJECTION; |
41 |
|
42 |
#glViewport 0, 0, $WIDTH, $HEIGHT; |
43 |
# re-bind all textures |
44 |
} |
45 |
|
46 |
sub refresh { |
47 |
glClearColor 0.5, 0.5, 0.7, 0; |
48 |
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; |
49 |
|
50 |
glLoadIdentity; |
51 |
glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; |
52 |
|
53 |
my $map = $conn->{map}; |
54 |
|
55 |
for my $x (0 .. $conn->{mapw} - 1) { |
56 |
for my $y (0 .. $conn->{maph} - 1) { |
57 |
|
58 |
my $cell = $map->[$x][$y] |
59 |
or next; |
60 |
|
61 |
for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
62 |
my $tex = $conn->{face}[$num]{texture} ||= do { |
63 |
$conn->send ("askface $num") unless $conn->{face}[$num]{askface}++; |
64 |
|
65 |
0 |
66 |
|
67 |
}; |
68 |
|
69 |
glBindTexture GL_TEXTURE_2D, $tex; |
70 |
|
71 |
glBegin GL_QUADS; |
72 |
glTexCoord 0, 0; glVertex $x, $y; |
73 |
glTexCoord 1, 0; glVertex $x + 1, $y; |
74 |
glTexCoord 1, 1; glVertex $x + 1, $y + 1; |
75 |
glTexCoord 0, 1; glVertex $x, $y + 1; |
76 |
glEnd; |
77 |
} |
78 |
} |
79 |
} |
80 |
|
81 |
SDL::GLSwapBuffers; |
82 |
} |
83 |
|
84 |
my $ev = new SDL::Event; |
85 |
my %ev_cb; |
86 |
|
87 |
sub event(&$) { |
88 |
$ev_cb{$_[0]->()} = $_[1]; |
89 |
} |
90 |
|
91 |
sub does(&) { shift } |
92 |
|
93 |
event {SDL_QUIT} does { |
94 |
exit; |
95 |
}; |
96 |
|
97 |
event {SDL_VIDEORESIZE} does { |
98 |
print "resize\n"; |
99 |
}; |
100 |
|
101 |
event {SDL_KEYDOWN} does { |
102 |
print "keypress\n"; |
103 |
}; |
104 |
|
105 |
event {SDL_KEYUP} does { |
106 |
print "keyup\n";#d# |
107 |
}; |
108 |
|
109 |
event {SDL_MOUSEMOTION} does { |
110 |
print "motion\n"; |
111 |
}; |
112 |
|
113 |
event {SDL_MOUSEBUTTONDOWN} does { |
114 |
print "button\n"; |
115 |
}; |
116 |
|
117 |
event {SDL_MOUSEBUTTONUP} does { |
118 |
print "buttup\n"; |
119 |
}; |
120 |
|
121 |
event {SDL_ACTIVEEVENT} does { |
122 |
print "active\n"; |
123 |
}; |
124 |
|
125 |
package Crossfire::Client; |
126 |
|
127 |
@conn::ISA = Crossfire::Protocol::; |
128 |
|
129 |
sub conn::map_update { |
130 |
my ($self, $dirty) = @_; |
131 |
|
132 |
refresh; |
133 |
} |
134 |
|
135 |
sub conn::map_scroll { |
136 |
my ($self, $dx, $dy) = @_; |
137 |
|
138 |
refresh; |
139 |
} |
140 |
|
141 |
sub conn::map_clear { |
142 |
my ($self) = @_; |
143 |
|
144 |
refresh; |
145 |
} |
146 |
|
147 |
sub conn::face_update { |
148 |
my ($self, $num, $face) = @_; |
149 |
|
150 |
warn "up face $self,$num,$face\n";#d# |
151 |
#TODO |
152 |
open my $fh, ">:raw", "/tmp/x~"; |
153 |
syswrite $fh, $face->{image}; |
154 |
close $fh; |
155 |
|
156 |
my $surface = new SDL::Surface -name => "/tmp/x~"; |
157 |
|
158 |
unlink "/tmp/x~"; |
159 |
|
160 |
my ($tex) = @{glGenTextures 1}; |
161 |
glGetError(); |
162 |
|
163 |
$face->{texture} = $tex; |
164 |
|
165 |
glBindTexture GL_TEXTURE_2D, $tex; |
166 |
my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; |
167 |
|
168 |
# glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
169 |
# glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
170 |
|
171 |
$surface->rgba; |
172 |
|
173 |
glTexImage2D GL_TEXTURE_2D, 0, |
174 |
4, # components |
175 |
$surface->width, $surface->height, |
176 |
0, |
177 |
GL_RGBA, |
178 |
GL_UNSIGNED_BYTE, |
179 |
$surface->pixels; |
180 |
my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; |
181 |
} |
182 |
|
183 |
############################################################################# |
184 |
|
185 |
use Event; |
186 |
|
187 |
glinit; |
188 |
|
189 |
$conn = new conn |
190 |
host => "cf.schmorp.de", |
191 |
port => 13327; |
192 |
|
193 |
Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { |
194 |
while ($ev->poll) { |
195 |
($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); |
196 |
} |
197 |
}); |
198 |
|
199 |
Event::loop; |
200 |
|
201 |
|