-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimporter.c
116 lines (102 loc) · 2.74 KB
/
importer.c
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
/*****************************************************************************/
/*
* PiSHi LE (Lite edition) - Fundamentals of the King's Crook graphics engine.
*
* by EMMIR 2018-2022
*
* YouTube: https://www.youtube.com/c/LMP88
*
* This software is released into the public domain.
*/
/*****************************************************************************/
#include "pl.h"
/* importer.c
*
* Code for importing DMDL files used in King's Crook.
* DMDL files can be generated by making models in the
* Freeform Model Editor (mdlrfree)
*
*/
#include <stdio.h>
#include <stdlib.h>
static char *cur_fn = NULL;
static char g_buf[1024];
static void
read_line(FILE *out, char *buf, int len)
{
if (!fgets(buf, len, out)) {
fprintf(stderr, "invalid dmdl file: %s\n", cur_fn);
fclose(out);
getchar();
exit(0);
}
}
static void
read_polygon(FILE *out, struct PL_POLY *dst)
{
int tmp; /* texture ID (ignored for this simple importer) */
int i, nv;
int ctr = 0;
read_line(out, g_buf, 16);
sscanf(g_buf, "%d", &tmp);
dst->tex = NULL;
read_line(out, g_buf, 16);
sscanf(g_buf, "%d", &dst->color);
read_line(out, g_buf, 16);
read_line(out, g_buf, 16);
sscanf(g_buf, "%d", &dst->n_verts);
dst->n_verts &= 0xf;
nv = ((dst->n_verts & 0xf) + 1) * 4;
for (i = 0; i < nv; i++) {
read_line(out, g_buf, 16);
if ((i % 4) != 1) {
sscanf(g_buf, "%d\n", &dst->verts[ctr]);
ctr++;
}
}
}
extern int
import_dmdl(char *name, struct PL_OBJ **o)
{
struct PL_OBJ *dst;
FILE *out;
int i, tmp;
int x, y, z;
int *dv;
snprintf(g_buf, sizeof(g_buf), "%s.dmdl", name);
cur_fn = name;
if (!(out = fopen(g_buf, "rb"))) {
fprintf(stderr, "failed to read dmdl file: %s\n", g_buf);
return 0;
}
*o = EXT_calloc(1, sizeof(struct PL_OBJ));
dst = *o;
read_line(out, g_buf, 256);
sscanf(g_buf, "%d", &dst->n_verts);
dst->verts = EXT_calloc(1, dst->n_verts * PL_VLEN * sizeof(int));
if (dst->verts == NULL) {
EXT_error(PL_ERR_NO_MEM, "importer", "no memory");
return 0;
}
for (i = 0; i < dst->n_verts; i++) {
read_line(out, g_buf, 256);
dv = dst->verts;
x = i * PL_VLEN + 0;
y = i * PL_VLEN + 1;
z = i * PL_VLEN + 2;
/* w coord is really unused */
sscanf(g_buf, "%d %d %d %d", &dv[x], &dv[y], &dv[z], &tmp);
}
read_line(out, g_buf, 256);
sscanf(g_buf, "%d", &dst->n_polys);
dst->polys = EXT_calloc(1, dst->n_polys * sizeof(struct PL_POLY));
if (dst->polys == NULL) {
EXT_error(PL_ERR_NO_MEM, "importer", "no memory");
return 0;
}
for (i = 0; i < dst->n_polys; i++) {
read_polygon(out, &dst->polys[i]);
}
fclose(out);
return 1;
}