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.6 by root, Thu Apr 6 21:15:11 2006 UTC vs.
Revision 1.21 by root, Fri Apr 7 20:08:57 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 Crossfire::Client::Util;
23my $HEIGHT = 480; 19use Crossfire::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_DECAL; 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;
55 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
56 glEnable GL_BLEND;
57
44 glMatrixMode GL_PROJECTION; 58 glMatrixMode GL_PROJECTION;
59 glLoadIdentity;
60 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100;
45 61
46 glLoadIdentity; 62 glMatrixMode GL_MODELVIEW;
47 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1;
48 63
49 # re-bind all textures 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;
50} 88}
51 89
52sub refresh { 90sub refresh {
53 glClearColor 0.5, 0.5, 0.7, 0;
54 glClear GL_COLOR_BUFFER_BIT; 91 glClear GL_COLOR_BUFFER_BIT;
55 92
56 my $map = $conn->{map}; 93 for (values %Client::Widget::ACTIVE_WIDGETS) {
57 94 $_->draw
58 for my $x (0 .. $conn->{mapw} - 1) {
59 for my $y (0 .. $conn->{maph} - 1) {
60
61 my $cell = $map->[$x][$y]
62 or next;
63
64 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
65 my $tex = $conn->{face}[$num]{texture} || 0;
66
67 glBindTexture GL_TEXTURE_2D, $tex;
68
69 glColor 1,0.2,0.7;
70 glBegin GL_QUADS;
71 glTexCoord 0, 0; glVertex $x, $y;
72 glTexCoord 0, 1; glVertex $x, $y + 0.9;
73 glTexCoord 1, 1; glVertex $x + 0.9, $y + 0.9;
74 glTexCoord 1, 0; glVertex $x + 0.9, $y;
75 glEnd;
76 }
77 }
78 } 95 }
79 96
80 SDL::GLSwapBuffers; 97 SDL::GLSwapBuffers;
81} 98}
82 99
83my $ev = new SDL::Event; 100%SDL_CB = (
84my %ev_cb; 101 SDL_QUIT() => sub {
85 102 main_quit Gtk2;
86sub event(&$) { 103 },
87 $ev_cb{$_[0]->()} = $_[1]; 104 SDL_VIDEORESIZE() => sub {
88} 105 },
89 106 SDL_VIDEOEXPOSE() => sub {
90sub does(&) { shift }
91
92event {SDL_QUIT} does {
93 exit;
94};
95
96event {SDL_VIDEORESIZE} does {
97 print "resize\n";
98};
99
100event {SDL_VIDEOEXPOSE} does {
101 refresh; 107 refresh;
102}; 108 },
103 109 SDL_KEYDOWN() => sub {
104event {SDL_KEYDOWN} does { 110 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
105 print "keypress\n"; 111 # alt-enter
106}; 112 $CFG->{fullscreen} = !$CFG->{fullscreen};
107 113 init_screen;
108event {SDL_KEYUP} does { 114 } else {
109 print "keyup\n";#d# 115 Client::Widget::feed_sdl_key_down_event ($SDL_EV);
110}; 116 }
111 117 },
112event {SDL_MOUSEMOTION} does { 118 SDL_KEYUP() => sub {
113 print "motion\n"; 119 Client::Widget::feed_sdl_key_up_event ($SDL_EV);
114}; 120 },
115 121 SDL_MOUSEMOTION() => sub {
116event {SDL_MOUSEBUTTONDOWN} does { 122 warn "sdl motion\n";#d#
117 print "button\n"; 123 },
118}; 124 SDL_MOUSEBUTTONDOWN() => sub {
119 125 Client::Widget::feed_sdl_button_down_event ($SDL_EV);
120event {SDL_MOUSEBUTTONUP} does { 126 },
121 print "buttup\n"; 127 SDL_MOUSEBUTTONUP() => sub {
122}; 128 Client::Widget::feed_sdl_button_up_event ($SDL_EV);
123 129 },
124event {SDL_ACTIVEEVENT} does { 130 SDL_ACTIVEEVENT() => sub {
125 print "active\n"; 131 warn "active\n";#d#
126}; 132 },
127 133);
128package Crossfire::Client;
129 134
130@conn::ISA = Crossfire::Protocol::; 135@conn::ISA = Crossfire::Protocol::;
131 136
132sub conn::map_update { 137sub conn::map_update {
133 my ($self, $dirty) = @_; 138 my ($self, $dirty) = @_;
145 my ($self) = @_; 150 my ($self) = @_;
146 151
147 refresh; 152 refresh;
148} 153}
149 154
155sub conn::face_find {
156 my ($self, $face) = @_;
157
158 $FACECACHE->{"$face->{chksum},$face->{name}"}
159}
160
150sub conn::face_update { 161sub conn::face_update {
151 my ($self, $num, $face) = @_; 162 my ($self, $face) = @_;
152 163
153 warn "up face $self,$num,$face\n";#d# 164 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
154 #TODO
155 open my $fh, ">:raw", "/tmp/x~";
156 syswrite $fh, $face->{image};
157 close $fh;
158 165
159 my $surface = new SDL::Surface -name => "/tmp/x~"; 166 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
160
161 unlink "/tmp/x~";
162
163 my ($tex) = @{glGenTextures 1};
164 glGetError();
165
166 $face->{texture} = $tex;
167
168 glBindTexture GL_TEXTURE_2D, $tex;
169 my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr;
170
171 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
172 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
173 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
174 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
175
176 $surface->rgba;
177
178 glTexImage2D GL_TEXTURE_2D, 0,
179 4, # components
180 $surface->width, $surface->height,
181 0,
182 GL_RGBA,
183 GL_UNSIGNED_BYTE,
184 $surface->pixels;
185 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr;
186} 167}
187 168
188############################################################################# 169#############################################################################
189 170
190use Event; 171SDL::Init(SDL_INIT_EVERYTHING());
191 172
192glinit; 173my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
193 174
194$conn = new conn 175#find_rcfile "uifont.ttf";
195 host => "cf.schmorp.de",
196 port => 13327;
197 176
198Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { 177$mapwidget->activate;
199 while ($ev->poll) { 178$mapwidget->focus_in;
200 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
201 }
202});
203 179
204Event::loop; 180read_cfg "$Crossfire::VARDIR/pclientrc";
205 181
182$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
206 183
184$CFG ||= {
185 width => 640,
186 height => 480,
187 fullscreen => 0,
188 host => "crossfire.schmorp.de",
189 port => 13327,
190};
191
192Crossfire::Client::Util::run_config_dialog
193 login => sub { start_game },
194 logout => sub { stop_game };
195
196main Gtk2;
197
198Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines