-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterrain.c
More file actions
137 lines (125 loc) · 2.75 KB
/
terrain.c
File metadata and controls
137 lines (125 loc) · 2.75 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
long read_long (FILE *fp);
void write_long (FILE *fp, unsigned long x);
long file_length (char *path);
void write_short (FILE *fp, unsigned short x);
void write_bmp (char *fname, int width, int height, unsigned char *pal,
char *data,
int red, int green, int blue, int mult);
int main (int argc, char **argv)
{
char *buffer;
FILE *datfp;
FILE *palfp;
char palette[768];
char fname[50];
int i;
palfp = fopen ("main.pal", "rb");
if (!palfp)
{
printf ("Can't open main.pal\n");
exit (1);
}
fread (palette, 768, 1, palfp);
fclose (palfp);
buffer = malloc (557056);
if (!buffer)
{
printf ("Out of memory.\n");
exit (1);
}
for (i=0; i < 4; i++)
{
sprintf (fname, "tmapa00%d.dat", i);
datfp = fopen (fname, "rb");
if (!datfp)
{
printf ("Can't open %s\n", fname);
exit (1);
}
fread (buffer, 557056, 1, datfp);
fclose (datfp);
sprintf (fname, "block%d.bmp", i);
write_bmp (fname, 32*8, 2176, palette, buffer, 0, 1, 2, 4);
}
}
void write_bmp (char *fname, int width, int height,
unsigned char *pal, char *data,
int red, int green, int blue, int mult)
{
long l;
int i, j;
FILE *out;
out = fopen (fname, "wb");
if (!out)
{
printf ("Can't open file %s. Aborting.\n", fname);
exit (1);
}
l = width*height;
fprintf (out, "BM");
write_long (out, l+0x436);
write_long (out, 0);
write_long (out, 0x436);
write_long (out, 40);
write_long (out, width);
write_long (out, height);
write_short (out, 1);
write_short (out, 8);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
write_long (out, 0);
for (i=0; i < 256; i++)
{
fputc (pal[i*3+blue]*mult, out);
fputc (pal[i*3+green]*mult, out);
fputc (pal[i*3+red]*mult, out);
fputc (0, out);
}
for (i=1; i <= height; i++)
{
fwrite (data+(height-i)*width, width, 1, out);
if (width & 3)
for (j=0; j < 4-(width&3); j++)
fputc (0, out);
}
fclose (out);
}
void write_short (FILE *fp, unsigned short x)
{
fputc ((int) (x&255), fp);
fputc ((int) ((x>>8)&255), fp);
}
void write_long (FILE *fp, unsigned long x)
{
fputc ((int) (x&255), fp);
fputc ((int) ((x>>8)&255), fp);
fputc ((int) ((x>>16)&255), fp);
fputc ((int) ((x>>24)&255), fp);
}
long read_long (FILE *fp)
{
long l;
l = fgetc (fp);
l += fgetc (fp)<<8;
l += fgetc (fp)<<16;
l += fgetc (fp)<<24;
return l;
}
long file_length (char *path)
{
FILE *fp;
long ret;
fp = fopen (path, "rb");
if (!fp)
return -1;
fseek (fp, 0, SEEK_END);
ret = ftell (fp);
fclose (fp);
return ret;
}