Subversion Repositories eduke32

Rev

Rev 4262 | Rev 4308 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4262 Rev 4301
Line 16... Line 16...
16
decl[[
16
decl[[
17
int32_t getclosestcol(int32_t r, int32_t g, int32_t b);
17
int32_t getclosestcol(int32_t r, int32_t g, int32_t b);
18
char *palookup[256];  // MAXPALOOKUPS
18
char *palookup[256];  // MAXPALOOKUPS
19
uint8_t palette[768];
19
uint8_t palette[768];
20
20
-
 
21
const char *getblendtab(int32_t blend);
-
 
22
void setblendtab(int32_t blend, const char *tab);
-
 
23
21
int32_t setpalookup(int32_t palnum, const uint8_t *shtab);
24
int32_t setpalookup(int32_t palnum, const uint8_t *shtab);
22
]]
25
]]
23
26
24
----------
27
----------
25
28
Line 28... Line 31...
28
local engine = {}
31
local engine = {}
29
32
30
33
31
local shtab_t  -- forward-decl
34
local shtab_t  -- forward-decl
32
35
33
local function cast_u8ptr(pal256)
36
local function cast_u8ptr(sth)
34
    return ffi.cast("uint8_t *", pal256)
37
    return ffi.cast("uint8_t *", sth)
35
end
38
end
36
39
37
local shtab_methods = {
40
local shtab_methods = {
38
    -- Remap consecutive blocks of 16 color indices and return this new shade
41
    -- Remap consecutive blocks of 16 color indices and return this new shade
39
    -- table.
42
    -- table.
Line 71... Line 74...
71
    if (method) then
74
    if (method) then
72
        return method
75
        return method
73
    end
76
    end
74
end
77
end
75
78
76
local pal256_t = bcarray.new("uint8_t", 256, "shade table 256-tuple")
79
local pal256_t = bcarray.new("uint8_t", 256, "color index 256-tuple")
77
-- The shade table type, effectively a bound-checked uint8_t [32][256]:
80
-- The shade table type, effectively a bound-checked uint8_t [32][256]:
78
shtab_t = bcarray.new(pal256_t, 32, "shade table", nil, nil, { __index = shtab_mt__index })
81
shtab_t = bcarray.new(pal256_t, 32, "shade table", nil, nil, { __index = shtab_mt__index })
79
local SIZEOF_SHTAB = ffi.sizeof(shtab_t)
82
local SIZEOF_SHTAB = ffi.sizeof(shtab_t)
80
83
-
 
84
local blendtab_t = bcarray.new(pal256_t, 256, "blending table")
-
 
85
local SIZEOF_BLENDTAB = ffi.sizeof(blendtab_t)
-
 
86
81
local RESERVEDPALS = 8  -- KEEPINSYNC build.h: assure that ours is >= theirs
87
local RESERVEDPALS = 8  -- KEEPINSYNC build.h: assure that ours is >= theirs
82
engine.RESERVEDPALS = RESERVEDPALS
88
engine.RESERVEDPALS = RESERVEDPALS
83
89
-
 
90
local MAXBLENDTABS = 256  -- KEEPINSYNC build.h
-
 
91
84
local function check_palidx(i)
92
local function check_palidx(i)
85
    if (type(i) ~= "number" or not (i >= 0 and i <= 255-RESERVEDPALS)) then
93
    if (type(i) ~= "number" or not (i >= 0 and i <= 255-RESERVEDPALS)) then
86
        error("invalid argument #1: palette swap index must be in the range [0 .. "..255-RESERVEDPALS.."]", 3)
94
        error("invalid argument #1: palette swap index must be in the range [0 .. "..255-RESERVEDPALS.."]", 3)
87
    end
95
    end
88
end
96
end
89
97
-
 
98
local function check_blendidx(i)
-
 
99
    if (type(i) ~= "number" or not (i >= 0 and i <= MAXBLENDTABS-1)) then
-
 
100
        error("invalid argument #1: blending table index must be in the range [0 .. ".. MAXBLENDTABS-1 .."]", 3)
-
 
101
    end
-
 
102
end
-
 
103
90
local function err_uncommon_shade_table(ret)
104
local function err_uncommon_shade_table(ret)
91
    if (ret == -1) then
105
    if (ret == -1) then
92
        error("loaded engine shade tables don't have 32 gradients of shade", 3)
106
        error("loaded engine shade tables don't have 32 gradients of shade", 3)
93
    end
107
    end
94
end
108
end
Line 99... Line 113...
99
113
100
function engine.shadetab()
114
function engine.shadetab()
101
    return shtab_t()
115
    return shtab_t()
102
end
116
end
103
117
-
 
118
function engine.blendtab()
-
 
119
    return blendtab_t()
-
 
120
end
-
 
121
104
function engine.getshadetab(palidx)
122
function engine.getshadetab(palidx)
105
    check_palidx(palidx)
123
    check_palidx(palidx)
106
    if (palookup_isdefault(palidx)) then
124
    if (palookup_isdefault(palidx)) then
107
        return nil
125
        return nil
108
    end
126
    end
Line 113... Line 131...
113
    local sht = shtab_t()
131
    local sht = shtab_t()
114
    ffi.copy(sht, C.palookup[palidx], SIZEOF_SHTAB)
132
    ffi.copy(sht, C.palookup[palidx], SIZEOF_SHTAB)
115
    return sht
133
    return sht
116
end
134
end
117
135
118
function engine.setshadetab(palidx, shtab)
136
function engine.getblendtab(blendidx)
-
 
137
    check_blendidx(blendidx)
-
 
138
-
 
139
    local ptr = C.getblendtab(blendidx)
-
 
140
    if (ptr == nil) then
-
 
141
        return nil
-
 
142
    end
-
 
143
-
 
144
    local tab = blendtab_t()
-
 
145
    ffi.copy(tab, ptr, SIZEOF_BLENDTAB)
-
 
146
    return tab
-
 
147
end
-
 
148
-
 
149
-
 
150
local function check_first_time()
119
    if (not ismapster32 and C.g_elFirstTime == 0) then
151
    if (not ismapster32 and C.g_elFirstTime == 0) then
120
        error("setshadetab() may be run only while LUNATIC_FIRST_TIME is true", 2)
152
        error("may be called only while LUNATIC_FIRST_TIME is true", 3)
-
 
153
    end
121
    end
154
end
122
155
-
 
156
function engine.setshadetab(palidx, shtab)
-
 
157
    check_first_time()
123
    check_palidx(palidx)
158
    check_palidx(palidx)
-
 
159
124
    if (not ffi.istype(shtab_t, shtab_t)) then
160
    if (not ffi.istype(shtab_t, shtab)) then
125
        error("invalid argument #2: must be a shade table obtained by shadetab()", 2)
161
        error("invalid argument #2: must be a shade table obtained by shadetab()", 2)
126
    end
162
    end
127
163
128
    if (not ismapster32 and not palookup_isdefault(palidx)) then
164
    if (not ismapster32 and not palookup_isdefault(palidx)) then
129
        error("attempt to override already defined shade table", 2)
165
        error("attempt to override already defined shade table", 2)
130
    end
166
    end
131
167
132
    local ret = C.setpalookup(palidx, ffi.cast("uint8_t *", shtab))
168
    local ret = C.setpalookup(palidx, cast_u8ptr(shtab))
133
    err_uncommon_shade_table(ret)
169
    err_uncommon_shade_table(ret)
134
end
170
end
135
171
-
 
172
function engine.setblendtab(blendidx, tab)
-
 
173
    check_first_time()
-
 
174
    check_blendidx(blendidx)
-
 
175
-
 
176
    if (not ffi.istype(blendtab_t, tab)) then
-
 
177
        error("invalid argument #2: must be a blending table obtained by blendtab()", 2)
-
 
178
    end
-
 
179
-
 
180
    if (not ismapster32 and C.getblendtab(blendidx) ~= nil) then
-
 
181
        error("attempt to override already defined blending table", 2)
-
 
182
    end
-
 
183
-
 
184
    C.setblendtab(blendidx, cast_u8ptr(tab))
-
 
185
end
-
 
186
136
187
137
local function check_colcomp(a)
188
local function check_colcomp(a)
138
    if (type(a) ~= "number" or not (a >= 0 and a <= 63)) then
189
    if (type(a) ~= "number" or not (a >= 0 and a <= 63)) then
139
        error("color component must be in the range [0 .. 63]", 3)
190
        error("color component must be in the range [0 .. 63]", 3)
140
    end
191
    end
Line 145... Line 196...
145
function engine.getrgb(colidx)
196
function engine.getrgb(colidx)
146
    if (type(colidx) ~= "number" or not (colidx >= 0 and colidx <= 255)) then
197
    if (type(colidx) ~= "number" or not (colidx >= 0 and colidx <= 255)) then
147
        error("color index must be in the range [0 .. 255]", 2)
198
        error("color index must be in the range [0 .. 255]", 2)
148
    end
199
    end
149
200
-
 
201
    -- NOTE: In the game, palette[255*{0..2}] is set to 0 in
-
 
202
    -- G_LoadExtraPalettes() via G_Startup(). However, that's after Lua state
-
 
203
    -- initialization (i.e. when LUNATIC_FIRST_TIME would be true), and in the
-
 
204
    -- editor, it's never changed from the purple color. Therefore, I think
-
 
205
    -- it's more useful to always return the fully black color here.
-
 
206
    if (colidx == 255) then
-
 
207
        return 0, 0, 0
-
 
208
    end
-
 
209
150
    local rgbptr = C.palette + 3*colidx
210
    local rgbptr = C.palette + 3*colidx
151
    return rgbptr[0], rgbptr[1], rgbptr[2]
211
    return rgbptr[0], rgbptr[1], rgbptr[2]
152
end
212
end
153
213
154
-- TODO: flag whether fullbrights are OK
214
-- TODO: flag whether fullbrights are OK