ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.8
Committed: Fri Apr 7 08:42:56 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.7: +10 -6 lines
Log Message:
*** empty log message ***

File Contents

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