ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.4
Committed: Thu Apr 6 20:34:15 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.3: +14 -8 lines
Log Message:
*** empty log message ***

File Contents

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