-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgba.lua
115 lines (98 loc) · 2.7 KB
/
mgba.lua
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
local ffi = require "ffi"
local bit = require "bit"
local ext = ({Windows = 'dll', Linux = 'so', OSX = 'dylib'})[ffi.os]
if not ext then
error("Unknown operating system: "..ffi.os)
end
local unboxed = "native/unboxed/libmgba."..ext
if not love.filesystem.exists(unboxed) then
print("Unboxing native library...")
love.filesystem.createDirectory("native/unboxed")
local src, err = love.filesystem.newFile("native/"..ffi.arch.."/libmgba."..ext, "r")
if not src then
error("Unsupported arch: "..ffi.arch)
end
local dest = love.filesystem.newFile(unboxed, "w")
while true do
local data = src:read(4096)
if #data == 0 or not data then break end
dest:write(data)
end
src:close()
dest:close()
end
local native = ffi.load(love.filesystem.getSaveDirectory()..'/'..unboxed)
_MGBA_NATIVE = native -- Keep a global reference to prevent the native library from being unloaded under us
do
local def = love.filesystem.read("mgba.h")
ffi.cdef(def)
end
local game = ffi.new("const char *", _GAME)
local core = native.mCoreFind(game)
assert(core:init(), "Core failed to init")
native.mCoreInitConfig(core, "love")
native.mCoreLoadConfig(core)
assert(native.mCoreLoadFile(core, game), "Game failed to load")
local resolution = ffi.new("unsigned[2]")
core:desiredVideoDimensions(resolution, resolution+1)
local width, height = resolution[0], resolution[1]
local buffer = ffi.new("color_t[?]", width*height)
core:setVideoBuffer(buffer, width)
local imagedata = love.image.newImageData(width, height)
local image = love.graphics.newImage(imagedata)
core:reset()
local inputKeys = {}
local inputInfo = {
nKeys = 10,
keyId = {
[0]="A",
"B",
"Select",
"Start",
"Right",
"Left",
"Up",
"Down",
"R",
"L"
}
}
for i=0, inputInfo.nKeys-1 do
inputKeys[ffi.string(inputInfo.keyId[i]):lower()] = bit.lshift(1, i)
end
local keymap = {
up = inputKeys.up,
down = inputKeys.down,
left = inputKeys.left,
right = inputKeys.right,
x = inputKeys.a,
z = inputKeys.b,
["return"] = inputKeys.start,
rshift = inputKeys.select,
a = inputKeys.l,
s = inputKeys.r
}
function love.draw()
core:runFrame()
-- This version of Love2D doesn't support format types in imagedata
-- So we have to copy each pixel manually and set the alpha to 255... bummer...
local imagedataBuffer = ffi.cast("color_t*", imagedata:getPointer())
for i=1, width*height do
imagedataBuffer[i] = bit.bor(0xFF000000, buffer[i])
end
image:refresh()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(image)
end
function love.keypressed(key)
local kid = keymap[key]
if kid then
core:addKeys(kid)
end
end
function love.keyreleased(key)
local kid = keymap[key]
if kid then
core:clearKeys(kid)
end
end