ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.8 by root, Fri Apr 7 08:42:56 2006 UTC vs.
Revision 1.19 by root, Fri Apr 7 19:33:42 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2
3use strict; 2use strict;
4 3
5use Crossfire::Client; 4use Glib;
6use Crossfire::Protocol; 5use Gtk2 -init;
7
8package Crossfire::Client; # uh, yeah
9
10use strict;
11 6
12use SDL; 7use SDL;
13use SDL::App; 8use SDL::App;
14use SDL::Event; 9use SDL::Event;
15use SDL::Surface; 10use SDL::Surface;
16use SDL::OpenGL; 11use SDL::OpenGL;
17use SDL::OpenGL::Constants; 12use SDL::OpenGL::Constants;
18 13
19my $conn; 14use Crossfire;
20my $app; 15use Crossfire::Client;
16use Crossfire::Protocol;
21 17
22my $WIDTH = 640; 18use Client::Util;
23my $HEIGHT = 480; 19use Client::Widget;
24 20
25sub glinit { 21our $FACECACHE;
26 # nuke all gl context data 22
27 23our $VERSION = '0.1';
24
25our $CFG;
26our $CONN;
27
28our $SDL_TIMER;
29our $SDL_APP;
30our $SDL_EV = new SDL::Event;
31our %SDL_CB;
32
33our @GL_INIT; # hooks called on every gl init
34
35sub init_screen {
28 $app = new SDL::App 36 $SDL_APP = new SDL::App
29 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 37 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
30 -title => "Crossfire+ Client", 38 -title => "Crossfire+ Client",
31 -width => $WIDTH, 39 -width => $CFG->{width},
32 -height => $HEIGHT, 40 -height => $CFG->{height},
33 -opengl => 1, 41 -opengl => 1,
34 -red_size => 8, 42 -red_size => 8,
35 -green_size => 8, 43 -green_size => 8,
36 -blue_size => 8, 44 -blue_size => 8,
37 -double_buffer => 1, 45 -double_buffer => 1,
46 -fullscreen => $CFG->{fullscreen},
38 -resizeable => 0; 47 -resizeable => 0;
39 48
49 glClearColor 0, 0, 0, 0;
50
40 glEnable GL_TEXTURE_2D; 51 glEnable GL_TEXTURE_2D;
41# glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 52 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
42 glShadeModel GL_FLAT; 53 glShadeModel GL_FLAT;
43 glDisable GL_DEPTH_TEST; 54 glDisable GL_DEPTH_TEST;
44 glMatrixMode GL_PROJECTION;
45 glBlendFunc GL_SRC_ALPHA, GL_ZERO; 55 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
46 glEnable GL_BLEND; 56 glEnable GL_BLEND;
47 57
58 glMatrixMode GL_PROJECTION;
48 glLoadIdentity; 59 glLoadIdentity;
49 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 60 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100;
50 61
51 # re-bind all textures 62 glMatrixMode GL_MODELVIEW;
63
64 $_->() for @GL_INIT;
65}
66
67sub 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
83sub stop_game {
84 remove Glib::Source $SDL_TIMER;
85
86 undef $SDL_APP;
87 SDL::Quit;
52} 88}
53 89
54sub refresh { 90sub refresh {
55 glClearColor 0, 0, 0, 0;
56 glClear GL_COLOR_BUFFER_BIT; 91 glClear GL_COLOR_BUFFER_BIT;
57 92
58 my $map = $conn->{map}; 93 for (values %Client::Widget::ACTIVE_WIDGETS) {
59 94 $_->draw
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 } 95 }
83 96
84 SDL::GLSwapBuffers; 97 SDL::GLSwapBuffers;
85} 98}
86 99
87my $ev = new SDL::Event; 100%SDL_CB = (
88my %ev_cb; 101 SDL_QUIT() => sub {
89 102 warn "sdl quit\n";#d#
90sub event(&$) { 103 main_quit Gtk2;
91 $ev_cb{$_[0]->()} = $_[1]; 104 },
92} 105 SDL_VIDEORESIZE() => sub {
93 106 },
94sub does(&) { shift } 107 SDL_VIDEOEXPOSE() => sub {
95
96event {SDL_QUIT} does {
97 exit;
98};
99
100event {SDL_VIDEORESIZE} does {
101 print "resize\n";
102};
103
104event {SDL_VIDEOEXPOSE} does {
105 refresh; 108 refresh;
106}; 109 },
107 110 SDL_KEYDOWN() => sub {
108event {SDL_KEYDOWN} does { 111 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
109 print "keypress\n"; 112 # alt-enter
110}; 113 $CFG->{fullscreen} = !$CFG->{fullscreen};
111 114 init_screen;
112event {SDL_KEYUP} does { 115 } else {
113 print "keyup\n";#d# 116 Client::Widget::feed_sdl_key_down_event ($SDL_EV);
114}; 117 }
115 118 },
116event {SDL_MOUSEMOTION} does { 119 SDL_KEYUP() => sub {
117 print "motion\n"; 120 Client::Widget::feed_sdl_key_up_event ($SDL_EV);
118}; 121 },
119 122 SDL_MOUSEMOTION() => sub {
120event {SDL_MOUSEBUTTONDOWN} does { 123 warn "sdl motion\n";#d#
121 print "button\n"; 124 },
122}; 125 SDL_MOUSEBUTTONDOWN() => sub {
123 126 Client::Widget::feed_sdl_button_down_event ($SDL_EV);
124event {SDL_MOUSEBUTTONUP} does { 127 },
125 print "buttup\n"; 128 SDL_MOUSEBUTTONUP() => sub {
126}; 129 Client::Widget::feed_sdl_button_up_event ($SDL_EV);
127 130 },
128event {SDL_ACTIVEEVENT} does { 131 SDL_ACTIVEEVENT() => sub {
129 print "active\n"; 132 warn "active\n";#d#
130}; 133 },
131 134);
132package Crossfire::Client;
133 135
134@conn::ISA = Crossfire::Protocol::; 136@conn::ISA = Crossfire::Protocol::;
135 137
136sub conn::map_update { 138sub conn::map_update {
137 my ($self, $dirty) = @_; 139 my ($self, $dirty) = @_;
149 my ($self) = @_; 151 my ($self) = @_;
150 152
151 refresh; 153 refresh;
152} 154}
153 155
156sub conn::face_find {
157 my ($self, $face) = @_;
158
159 $FACECACHE->{"$face->{chksum},$face->{name}"}
160}
161
154sub conn::face_update { 162sub conn::face_update {
155 my ($self, $num, $face) = @_; 163 my ($self, $face) = @_;
156 164
157 warn "up face $self,$num,$face\n";#d# 165 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
158 #TODO
159 open my $fh, ">:raw", "/tmp/x~";
160 syswrite $fh, $face->{image};
161 close $fh;
162 166
163 my $surface = new SDL::Surface -name => "/tmp/x~"; 167 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
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} 168}
191 169
192############################################################################# 170#############################################################################
193 171
194use Event; 172my $mapwidget = Client::MapWidget->new;
195 173
196glinit; 174$mapwidget->activate;
175$mapwidget->focus_in;
197 176
198$conn = new conn 177Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc";
199 host => "cf.schmorp.de",
200 port => 13327;
201 178
202Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { 179$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
203 while ($ev->poll) {
204 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
205 }
206});
207 180
208Event::loop; 181$CFG ||= {
182 width => 640,
183 height => 480,
184 fullscreen => 0,
185 host => "crossfire.schmorp.de",
186 port => 13327,
187};
209 188
189Client::Util::run_config_dialog
190 login => sub { start_game },
191 logout => sub { stop_game };
210 192
193main Gtk2;
194
195Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines