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.12 by elmex, Fri Apr 7 16:29:19 2006 UTC vs.
Revision 1.13 by root, Fri Apr 7 16:30:23 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4 4
5use Crossfire; 5use Glib;
6use Crossfire::Client; 6use Gtk2 -init;
7use Crossfire::Protocol;
8
9use Client::Util;
10
11package Crossfire::Client; # uh, yeah
12
13use strict;
14
15our $CFG;
16our $VERSION = '0.1';
17 7
18use SDL; 8use SDL;
19use SDL::App; 9use SDL::App;
20use SDL::Event; 10use SDL::Event;
21use SDL::Surface; 11use SDL::Surface;
22use SDL::OpenGL; 12use SDL::OpenGL;
23use SDL::OpenGL::Constants; 13use SDL::OpenGL::Constants;
24 14
25my $conn; 15use Crossfire;
26my $app; 16use Crossfire::Client;
17use Crossfire::Protocol;
27 18
28my $WIDTH = 640; 19use Client::Util;
29my $HEIGHT = 480;
30 20
31sub glinit { 21our $VERSION = '0.1';
22
23our $CFG;
24our $CONN;
25
26our $SDL_TIMER;
27our $SDL_APP;
28our $SDL_EV = new SDL::Event;
29our %SDL_CB;
30
31sub init_screen {
32 # nuke all gl context data 32 # nuke all gl context data
33 33
34 $app = new SDL::App 34 $SDL_APP = new SDL::App
35 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 35 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
36 -title => "Crossfire+ Client", 36 -title => "Crossfire+ Client",
37 -width => $WIDTH, 37 -width => $CFG->{width},
38 -height => $HEIGHT, 38 -height => $CFG->{height},
39 -opengl => 1, 39 -opengl => 1,
40 -red_size => 8, 40 -red_size => 8,
41 -green_size => 8, 41 -green_size => 8,
42 -blue_size => 8, 42 -blue_size => 8,
43 -double_buffer => 1, 43 -double_buffer => 1,
44 -fullscreen => $CFG->{fullscreen},
44 -resizeable => 0; 45 -resizeable => 0;
45 46
46 glEnable GL_TEXTURE_2D; 47 glEnable GL_TEXTURE_2D;
47 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 48 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
48 glShadeModel GL_FLAT; 49 glShadeModel GL_FLAT;
50 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 51 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
51 glEnable GL_BLEND; 52 glEnable GL_BLEND;
52 53
53 glMatrixMode GL_PROJECTION; 54 glMatrixMode GL_PROJECTION;
54 glLoadIdentity; 55 glLoadIdentity;
55 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 56 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1;
56 57
57 # re-bind all textures 58 # re-bind all textures
59}
60
61sub start_game {
62 $SDL_TIMER = add Glib::Timeout 1000/20, sub {
63 while ($SDL_EV->poll) {
64 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->();
65 }
66
67 1
68 };
69
70 init_screen;
71
72 $CONN = new conn
73 host => $CFG->{host},
74 port => $CFG->{port};
75}
76
77sub stop_game {
78 remove Glib::Source $SDL_TIMER;
79
80 undef $SDL_APP;
81 SDL::Quit;
58} 82}
59 83
60sub refresh { 84sub refresh {
61 glClearColor 0, 0, 0, 0; 85 glClearColor 0, 0, 0, 0;
62 glClear GL_COLOR_BUFFER_BIT; 86 glClear GL_COLOR_BUFFER_BIT;
63 87
64 my $map = $conn->{map}; 88 my $map = $CONN->{map};
65 89
66 for my $x (0 .. $conn->{mapw} - 1) { 90 for my $x (0 .. $CONN->{mapw} - 1) {
67 for my $y (0 .. $conn->{maph} - 1) { 91 for my $y (0 .. $CONN->{maph} - 1) {
68 92
69 my $cell = $map->[$x][$y] 93 my $cell = $map->[$x][$y]
70 or next; 94 or next;
71 95
72 my $darkness = $cell->[3] * (1 / 255); 96 my $darkness = $cell->[3] * (1 / 255);
73 my $darkness = 0.8 + 0.2*rand;
74 glColor $darkness, $darkness, $darkness; 97 glColor $darkness, $darkness, $darkness;
75 98
76 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 99 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
77 my $tex = $conn->{face}[$num]{texture} || 0; 100 my $tex = $CONN->{face}[$num]{texture} || 0;
78 101
79 glBindTexture GL_TEXTURE_2D, $tex; 102 glBindTexture GL_TEXTURE_2D, $tex;
80 103
81 glBegin GL_QUADS; 104 glBegin GL_QUADS;
82 glTexCoord 0, 0; glVertex $x, $y; 105 glTexCoord 0, 0; glVertex $x, $y;
89 } 112 }
90 113
91 SDL::GLSwapBuffers; 114 SDL::GLSwapBuffers;
92} 115}
93 116
94my $ev = new SDL::Event; 117%SDL_CB = (
95my %ev_cb; 118 SDL_QUIT() => sub {
96 119 warn "sdl quit\n";#d#
97sub event(&$) {
98 $ev_cb{$_[0]->()} = $_[1];
99}
100
101sub does(&) { shift }
102
103event {SDL_QUIT} does {
104 exit; 120 exit;
105}; 121 },
106 122 SDL_VIDEORESIZE() => sub {
107event {SDL_VIDEORESIZE} does { 123 },
108 print "resize\n"; 124 SDL_VIDEOEXPOSE() => sub {
109};
110
111event {SDL_VIDEOEXPOSE} does {
112 refresh; 125 refresh;
113}; 126 },
114 127 SDL_KEYDOWN() => sub {
115event {SDL_KEYDOWN} does { 128 },
116 print "keypress\n"; 129 SDL_KEYUP() => sub {
117}; 130 },
118 131 SDL_MOUSEMOTION() => sub {
119event {SDL_KEYUP} does { 132 warn "sdl motion\n";#d#
120 print "keyup\n";#d# 133 },
121}; 134 SDL_MOUSEBUTTONDOWN() => sub {
122 135 },
123event {SDL_MOUSEMOTION} does { 136 SDL_MOUSEBUTTONUP() => sub {
124 print "motion\n"; 137 },
125}; 138 SDL_ACTIVEEVENT() => sub {
126
127event {SDL_MOUSEBUTTONDOWN} does {
128 print "button\n";
129};
130
131event {SDL_MOUSEBUTTONUP} does {
132 print "buttup\n";
133};
134
135event {SDL_ACTIVEEVENT} does {
136 print "active\n"; 139 warn "active\n";#d#
137}; 140 },
138 141);
139package Crossfire::Client;
140 142
141@conn::ISA = Crossfire::Protocol::; 143@conn::ISA = Crossfire::Protocol::;
142 144
143sub conn::map_update { 145sub conn::map_update {
144 my ($self, $dirty) = @_; 146 my ($self, $dirty) = @_;
158 refresh; 160 refresh;
159} 161}
160 162
161sub conn::face_update { 163sub conn::face_update {
162 my ($self, $num, $face) = @_; 164 my ($self, $num, $face) = @_;
163
164 warn "up face $self,$num,$face\n";#d#
165 use Gtk2;
166 165
167 my $pb = new Gtk2::Gdk::PixbufLoader; 166 my $pb = new Gtk2::Gdk::PixbufLoader;
168 $pb->write ($face->{image}); 167 $pb->write ($face->{image});
169 $pb->close; 168 $pb->close;
170 169
194 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; 193 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr;
195} 194}
196 195
197############################################################################# 196#############################################################################
198 197
199use Event;
200
201Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; 198Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc";
202 199
200$CFG ||= {
201 width => 640,
202 height => 480,
203 fullscreen => 0,
204 host => "crossfire.schmorp.de",
205 port => 13327,
206};
203 207
204if ($ARGV[0] eq 'config') {
205 Client::Util::run_config_dialog; 208Client::Util::run_config_dialog;
206}
207 209
208glinit; 210main Gtk2;
209 211
210$conn = new conn
211 host => "cf.schmorp.de",
212 port => 13327;
213
214Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
215 while ($ev->poll) {
216 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
217 }
218});
219
220Event::loop;
221

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines