gl_dev.c
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <gl/gl.h>
#include <gl/device.h>
#include <gl/image.h>
#include "graphic.h"
#include "macros.h"
#include "image.h"
static int repl; /* pixel replication factor */
/*---------------------------------------------*/
open_display( xsize, ysize, zoom)
int xsize, ysize, zoom;
{
repl = (zoom < 1) ? 1 : zoom;
/*
* set window style preferences
*/
noborder();
prefsize( xsize*repl, ysize*repl);
prefposition(0, xsize*repl-1, 0, ysize*repl-1);
/*
* open and configure a RGB or CI window
* window mapped to screen space, (0,0) = lower left of window
*/
winopen("test");
RGBmode(); /* for CI, cmode(); */
gconfig();
}
/*---------------------------------------------*/
clear_display( color)
int color;
{
cpack( color);
clear();
}
/*---------------------------------------------*/
idle_display()
{
while (!getbutton(ESCKEY))
;
while (getbutton(ESCKEY))
;
}
/*---------------------------------------------*/
put_pixel( x, y, color)
int x, y, color;
{
cpack(color);
if (repl <= 1)
pnt2i( x, y);
else {
x *= repl;
y *= repl;
rectfi( x, y, x + repl - 1, y + repl - 1);
}
}
/*---------------------------------------------*/
get_pixel( x, y, color)
int x, y;
unsigned int *color;
{
if (repl > 1) {
x *= repl;
y *= repl;
}
lrectread( x, y, x, y, color);
}
/*---------------------------------------------*/
make_movie(name, xsize, ysize)
char *name;
int xsize, ysize;
{
static int num = 1;
char filename[80];
IMAGE *img;
unsigned long *lb;
unsigned short *cb;
int x, y;
sprintf(filename, "%s%03d", name, num++);
img = iopen(filename, "w", RLE(1), 3, xsize, ysize, 3);
if (img == (IMAGE *)0) {
fprintf(stderr, "error in creating image %s\n", filename);
return;
}
lb = (unsigned long *)malloc(sizeof(unsigned long) * xsize);
cb = (unsigned short *)malloc(sizeof(unsigned short) * xsize);
for (y=0; y<ysize; y++) {
lrectread(0, y, xsize-1, y, lb);
for (x=0; x<xsize; x++) cb[x] = lb[x] & 0xff;
putrow(img, cb, y, 0);
for (x=0; x<xsize; x++) cb[x] = (lb[x] >> 8) & 0xff;
putrow(img, cb, y, 1);
for (x=0; x<xsize; x++) cb[x] = (lb[x] >> 16) & 0xff;
putrow(img, cb, y, 2);
}
free(lb);
free(cb);
iclose(img);
}
/*---------------------------------------------*/
struct Image *load_texfile(char *file, int type)
{
struct Image *texture;
IMAGE *img;
int x, y;
unsigned short *cp, *sp;
int avg_red=0, avg_grn=0, avg_blu=0, avg_alp=0;
if ((img = iopen(file, "r")) == (IMAGE *)NULL) {
fprintf(stderr, "can't open texture file %s\n", file);
exit(EXIT_FAILURE);
}
texture = (Image *)malloc(sizeof(Image));
texture->xsize = img->xsize;
texture->ysize = img->ysize;
texture->type = type;
texture->lsize = texture->xsize * IM_BYTE(texture->type);
texture->base = (ubyte *)malloc(texture->ysize * texture->lsize);
bzero(texture->base, texture->lsize * texture->ysize);
cp = (unsigned short *)malloc(texture->xsize * sizeof(unsigned short));
sp = (unsigned short *)texture->base;
/*
* XXX for now, 4444 4 component textures
*/
for (y=0; y<texture->ysize; y++) {
getrow(img, cp, y, 0);
for (x=0; x<texture->xsize; x++) sp[x] = (cp[x] & 0xf0) >> 4;
for (x=0; x<texture->xsize; x++) avg_red += cp[x];
getrow(img, cp, y, 1);
for (x=0; x<texture->xsize; x++) sp[x] |= (cp[x] & 0xf0);
for (x=0; x<texture->xsize; x++) avg_grn += cp[x];
getrow(img, cp, y, 2);
for (x=0; x<texture->xsize; x++) sp[x] |= (cp[x] & 0xf0) << 4;
for (x=0; x<texture->xsize; x++) avg_blu += cp[x];
/* XXXX hack to test antialiasing on black background */
if (En_antialias) {
for (x=0; x<texture->xsize; x++)
if (sp[x] == 0) { sp[x] = 0x444;
avg_red += 0x40;
avg_grn += 0x40;
avg_blu += 0x40;
}
}
if (img->zsize > 3) {
getrow(img, cp, y, 3);
for (x=0; x<texture->xsize; x++) sp[x] |= (cp[x] & 0xf0) << 8;
for (x=0; x<texture->xsize; x++) avg_alp += cp[x];
}
else {
for (x=0; x<texture->xsize; x++)
if (sp[x]) sp[x] |= 0xf0 << 8;
for (x=0; x<texture->xsize; x++) avg_alp += 0xf0;
}
sp += texture->xsize;
}
avg_red = (int)((float)avg_red/(texture->xsize*texture->ysize));
avg_grn = (int)((float)avg_grn/(texture->xsize*texture->ysize));
avg_blu = (int)((float)avg_blu/(texture->xsize*texture->ysize));
avg_alp = (int)((float)avg_alp/(texture->xsize*texture->ysize));
texture->avg_color =
pack_colora( avg_red, avg_grn, avg_blu, avg_alp);
free(cp);
iclose(img);
return (texture);
}