Skip to content

Commit a13ad92

Browse files
committed
Switch to LuaJIT to improve Lua code performance and improve the speed of the tile cluster example by caching the channel extration. The rgbaGetChannel function takes more time to run than one might expect because it has to allocate memory and prepare a new string.
1 parent 387da84 commit a13ad92

20 files changed

+137
-102
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "serpent"]
1414
path = serpent
1515
url = https://github.com/pkulchenko/serpent
16+
[submodule "lua-compat-5.3"]
17+
path = lua-compat-53
18+
url = https://github.com/keplerproject/lua-compat-5.3

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include Makefile.common
66

77
CFLAGS += -march=native -flto=8 -fuse-linker-plugin -s -O3 -pipe -march=native -fomit-frame-pointer
88
CXXFLAGS := $(CFLAGS) -fno-rtti -std=gnu++14
9-
LDFLAGS := -flto=8 -O3 -march=native -fuse-linker-plugin -s -fno-rtti -std=gnu++14 -L/usr/lib/fltk/ -lfltk_images -lfltk -lpng -ljpeg -lXft -lXext -lXinerama -lX11 -lz -s -llua -ldl
9+
LDFLAGS := -flto=8 -O3 -march=native -fuse-linker-plugin -s -fno-rtti -std=gnu++14 -L/usr/lib/fltk/ -lfltk_images -lfltk -lpng -ljpeg -lXft -lXext -lXinerama -lX11 -lz -s -lluajit-5.1 -ldl
1010

1111
EXECUTABLE := RetroGraphicsToolkit
1212

Makefile.common

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Gentoo fix needs include directory set to /usr/include/fltk/ or /usr/include/fltk-1/ for older versions of FLTK if you are using a different distribution then this may not apply to you.
2-
CFLAGS := -isystem /usr/include/fltk/ -Imdcomp/include/ -IautogeneratedBindings/ -Iluaposix/ext/include -Iluaposix_headers -Iiqa/include -I. -c
2+
CFLAGS := -isystem /usr/include/fltk/ -Imdcomp/include/ -IautogeneratedBindings/ -Iluaposix/ext/include -Iluaposix_headers -Iiqa/include -I. -I/usr/include/luajit-2.0 -Ilua-compat-53/c-api -DCOMPAT53_PREFIX=luacompat -c
33
BINDINGS := $(wildcard autogeneratedBindings/*.cpp) $(wildcard luaposix/ext/posix/*.c) $(wildcard luaposix/ext/posix/sys/*.c) \
44
$(wildcard moonfltk/src/*.cc) $(wildcard moonfltk/src/*.c) $(wildcard iqa/source/*.c)
55
BINDINGS := $(filter-out luaposix/ext/posix/posix.c, $(BINDINGS))
6+
BINDINGS := $(filter-out luaposix/ext/posix/stdio.c, $(BINDINGS))
67
BINDINGS := $(filter-out moonfltk/src/open.c, $(BINDINGS))
8+
BINDINGS := $(filter-out moonfltk/src/compat-5.3.c, $(BINDINGS))
79
BINDINGS := lua-zlib/lua_zlib.c $(BINDINGS)
10+
# BINDINGS := lua-compat-5.3/c-api/compat-5.3.c $(BINDINGS)
811
CPPFLAGS += -DPACKAGE='"luaposix"' -DVERSION='"RGT"' -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DHAVE_NET_IF_H=1 -D_DEFAULT_SOURCE -DUSE_IMAGES=1
912
include Makefile.objects

Makefile.objects

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ OBJECTS := project.o main.o callbacks_palette.o callback_tiles.o class_global.o
1919
metasprites.o callbacklua.o dub/dub.o $(BINDINGS:.c=.o) cbHelper.o \
2020
luaChunks.o luaChunk.o luaChunkRow.o luaChunkEntry.o luaLevel.o luaLevelLayers.o \
2121
luaLevelLayer.o luaLevelLayerRow.o luaLevelObjects.o luaTilePixels.o luaTilePixelsRow.o \
22-
luaStringStore.o
22+
luaStringStore.o lua-compat-53/c-api/compat-5.3.o
2323

TMS9918.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function tms9918Graphics1RemapTiles(projectIDX, attrsByTile, forceKeepAllUnique)
143143

144144
extAttrsFinal = {}
145145
for ti = 8, #tilesFinal, 8 do
146-
extAttrsFinal[ti // 8] = string.char(tilesFinal[ti][1])
146+
extAttrsFinal[math.floor(ti / 8)] = string.char(tilesFinal[ti][1])
147147
end
148148
ct.extAttrs = table.concat(extAttrsFinal)
149149

cbHelper.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "runlua.h"
1818
#include "cbHelper.h"
1919
#include <stdexcept>
20+
extern "C" {
21+
#include "compat-5.3.h"
22+
}
23+
2024
void luaWidgetCallbackHelper(Fl_Widget*, void*i) {
2125
struct cbInfo*c = (struct cbInfo*)i;
2226

config.lua

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
You should have received a copy of the GNU General Public License
1414
along with Retro Graphics Toolkit. If not, see <http://www.gnu.org/licenses/>.
15-
Copyright Sega16 (or whatever you wish to call me) (2012-2016)
15+
Copyright Sega16 (or whatever you wish to call me) (2012-2020)
1616
--]]
1717

1818
--[[
@@ -24,6 +24,17 @@ Warning: This file is executed very early. Before the project objects are ready.
2424
--]]
2525

2626
serpent = require("serpent.src.serpent")
27+
-- You must install compat53 and bit32 to use luajit.
28+
29+
-- There are a few ways to go about this. One is to install locally. This is helpful if these libraries cannot be installed with your package manager and you don't want to run as root.
30+
-- luarocks --local --lua-version 5.1 install compat53
31+
-- luarocks --local --lua-version 5.1 install bit32
32+
-- Then prior to running Retro Graphics Toolkit run
33+
-- eval $(luarocks --lua-version 5.1 path)
34+
35+
require("compat53")
36+
bit32 = require('bit32')
37+
2738

2839
Fl.scheme('plastic')
2940
if is_headless == 0 then

filereader.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include "gui.h"
2727
#include "luaconfig.h"
2828
#include "runlua.h"
29+
extern "C" {
30+
#include "compat-5.3.h"
31+
}
32+
2933
filereader::filereader(boost::endian::order endian, unsigned bytesPerElement, const char*title, bool relptr, unsigned offbits, bool be, const char * filename, fileType_t forceType, CompressionType compression) {
3034
char*fname = nullptr;
3135

level.lua

+24-24
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
You should have received a copy of the GNU General Public License
1414
along with Retro Graphics Toolkit. If not, see <http://www.gnu.org/licenses/>.
15-
Copyright Sega16 (or whatever you wish to call me) (2012-2019)
15+
Copyright Sega16 (or whatever you wish to call me) (2012-2020)
1616
--]]
1717
lvlCurLayer = 1
1818
editX,editY=0,0
@@ -22,8 +22,8 @@ function setSizePer()
2222
local p = projects.current
2323
local info = p.level.layers[lvlCurLayer].info
2424

25-
local src = info.src & 3
26-
local plane = (info.src>>2) + 1
25+
local src = bit32.band(info.src, 3)
26+
local plane = bit32.rshift(info.src, 2) + 1
2727

2828
local chunks = p.chunks
2929

@@ -94,7 +94,7 @@ function lvlsetlayer(layerIDX)
9494
editX = math.max(0, math.min(editX, curLayerInfo.w - 1))
9595
editY = math.max(0, math.min(editY, curLayerInfo.h - 1))
9696

97-
local layerSource = curLayerInfo.src & 3
97+
local layerSource = bit32.band(curLayerInfo.src, 3)
9898

9999
lvlSrc:value(lvlSrcOptions[layerSource + 1])
100100
setScrollBounds()
@@ -190,9 +190,9 @@ function selSlideUpdateMax(src)
190190
if src == level.TILES then
191191
selSlide:maximum(#p.tiles - 1)
192192
elseif src == level.BLOCKS then
193-
local plane=(curLayerInfo.src>>2)+1
193+
local plane = bit32.band(curLayerInfo.src, 2) + 1
194194
local tilemap = p.tilemaps[plane]
195-
selSlide:maximum(tilemap.hAll // tilemap.h - 1)
195+
selSlide:maximum(math.floor(tilemap.hAll / tilemap.h) - 1)
196196
elseif src == level.CHUNKS then
197197
selSlide:maximum(#chunks - 1)
198198
else
@@ -211,25 +211,25 @@ function drawLevel()
211211
end
212212
local curLayerInfo = layer.info
213213

214-
xOff = 168 * rgt.w() // 800
215-
yOff = 72 * rgt.h() // 600
214+
xOff = 168 * math.floor(rgt.w() / 800)
215+
yOff = 72 * math.floor(rgt.h() / 600)
216216

217217
setScrollBounds()
218218
local xs = xOff
219219
local ys = yOff
220-
local src = curLayerInfo.src & 3
220+
local src = bit32.band(curLayerInfo.src, 3)
221221
for j = scrollY, curLayerInfo.h - 1 do
222222
local xxs = xs
223223
for i = scrollX, curLayerInfo.w - 1 do
224224
local a = layer[j + 1][i + 1]
225-
local plane = curLayerInfo.src >> 2
225+
local plane = bit32.rshift(curLayerInfo.src, 2)
226226
if src == level.TILES then
227227
local tile = p.tiles[a.id + 1]
228228
if tile ~= nil then
229-
tile:draw(xxs, ys, lvlZoom, (a.dat>>2)&3, a.dat&1, a.dat&2, false, false)
229+
tile:draw(xxs, ys, lvlZoom, bit32.band(bit32.rshift(a.dat, 2), 3), bit32.band(a.dat, 1), bit32.band(a.dat, 2), false, false)
230230
end
231231
elseif src == level.BLOCKS then
232-
p.tilemaps[plane + 1].drawBlock(a.id, xxs, ys, a.dat & 3, lvlZoom)
232+
p.tilemaps[plane + 1].drawBlock(a.id, xxs, ys, bit32.band(a.dat, 3), lvlZoom)
233233
elseif src == level.CHUNKS then
234234
local chunks = p.chunks
235235
if a.id < #chunks then
@@ -360,8 +360,8 @@ function handleLevel(e)
360360
if e==FL.PUSH then
361361
x,y=Fl.event_x()-xOff,Fl.event_y()-yOff
362362
if x>=0 and y>=0 then
363-
x=(x//lvlZoom)+(scrollX*szX)
364-
y=(y//lvlZoom)+(scrollY*szY)
363+
x= math.floor(x / lvlZoom) + (scrollX*szX)
364+
y= math.floor(y / lvlZoom) + (scrollY*szY)
365365
level.appendObj(lvlCurLayer)
366366
spriteSel:maximum(level.objamt[lvlCurLayer+1]-1)
367367
spriteEdit=level.objamt[lvlCurLayer+1]-1
@@ -382,8 +382,8 @@ function handleLevel(e)
382382
local layer
383383
if e == FL.PUSH or e == FL.SHORTCUT then
384384
x,y=Fl.event_x(),Fl.event_y()
385-
x=((x-xOff)//(szX*lvlZoom))+scrollX
386-
y=((y-yOff)//(szY*lvlZoom))+scrollY
385+
x = math.floor((x-xOff) / (szX*lvlZoom)) + scrollX
386+
y = math.floor((y-yOff) / (szY*lvlZoom)) + scrollY
387387
if x < 0 or y < 0 then
388388
return
389389
end
@@ -463,17 +463,17 @@ end
463463
function setLayerSrc(valStr)
464464
local p = projects.current
465465
local curLayerInfo = p.level.layers[lvlCurLayer].info
466-
local plane = (curLayerInfo.src >> 2) + 1
466+
local plane = bit32.rshift(curLayerInfo.src, 2) + 1
467467

468468
local val = lvlSrcLUT[valStr]
469469

470470
if val == level.BLOCKS and p.tilemaps[plane].useBlocks ~= true then
471471
fltk.alert("You must first enable blocks in the plane editor")
472-
lvlSrc:value(lvlSrcOptions[(curLayerInfo.src & 3) + 1])
472+
lvlSrc:value(lvlSrcOptions[bit32.band(curLayerInfo.src, 3) + 1])
473473
return
474474
end
475475

476-
curLayerInfo.src=(curLayerInfo.src&(~3))|val
476+
curLayerInfo.src = bit32.replace(curLayerInfo.src, val, 0, 2)
477477
rgt.syncProject()--Needed for selSlideUpdateMax
478478
selSlideUpdateMax(val)
479479
setSizePer()
@@ -509,20 +509,20 @@ function loadS1layoutFname(fname)
509509
for j = 1, h do
510510
for i = 1, w do
511511
local info = layer[j][i]
512-
info.id=str:byte(idx)&127
513-
info.dat=str:byte(idx)&128
512+
info.id = bit32.band(str:byte(idx), 127)
513+
info.dat = bit32.band(str:byte(idx), 128)
514514
idx=idx+1
515515
end
516516
end
517517
end
518518
end
519519
function loadS1layout(unused)
520520
local p = projects.current
521-
if p:have(project.levelMask|project.chunksMask) then
521+
if p:have(project.levelMask + project.chunksMask) then
522522
local fname = fl.file_chooser("Load layout")
523523
loadS1layoutFname(fname)
524524
else
525-
project.haveMessage(project.levelMask|project.chunksMask)
525+
project.haveMessage(project.levelMask + project.chunksMask)
526526
end
527527
end
528528
function saveS1layout(unused)
@@ -541,7 +541,7 @@ function saveS1layout(unused)
541541
local layerRow = layer[j]
542542
for i=1, curLayerInfo.w do
543543
local info = layerRow[i]
544-
file:write(string.char((info.id&127)|(info.dat&128)))
544+
file:write(string.char(bit32.bor(bit32.band(info.id, 127), bit32.band(info.dat, 128))))
545545
end
546546
end
547547
file:close()

lua-compat-53

Submodule lua-compat-53 added at e00fd0a

0 commit comments

Comments
 (0)