Resolution switching

Implement actual resolution switching!
Not as good as I'd like, but it works for now
Also destroy everything on shutdown instead of relying on sdl.quit for
that
This commit is contained in:
gamax92 2015-06-26 17:45:44 -06:00
parent 4e9d49bce7
commit 2f11080f1f

View File

@ -98,35 +98,57 @@ function elsa.mousewheel(event)
table.insert(machine.signals,{"scroll",address,math.floor(x[0]/8)+1,math.floor(y[0]/16)+1,mwevent.y}) table.insert(machine.signals,{"scroll",address,math.floor(x[0]/8)+1,math.floor(y[0]/16)+1,mwevent.y})
end end
local window = SDL.createWindow("OCEmu - screen@" .. address, SDL.WINDOWPOS_CENTERED, SDL.WINDOWPOS_CENTERED, width*8, height*16, SDL.WINDOW_SHOWN) local window, renderer, texture, copytexture
if window == ffi.C.NULL then local function createWindow()
error(ffi.string(SDL.getError())) if not window then
end window = SDL.createWindow("OCEmu - screen@" .. address, SDL.WINDOWPOS_CENTERED, SDL.WINDOWPOS_CENTERED, width*8, height*16, SDL.WINDOW_SHOWN)
local flags = SDL.RENDERER_TARGETTEXTURE if window == ffi.C.NULL then
if ffi.os == "Windows" then -- TODO: Investigate why error(ffi.string(SDL.getError()))
flags = flags + SDL.RENDERER_SOFTWARE end
end end
local renderer = SDL.createRenderer(window, -1, flags) local flags = SDL.RENDERER_TARGETTEXTURE
if renderer == ffi.C.NULL then if ffi.os == "Windows" then -- TODO: Investigate why
error(ffi.string(SDL.getError())) flags = flags + SDL.RENDERER_SOFTWARE
end end
local texture = SDL.createTexture(renderer, SDL.PIXELFORMAT_ARGB8888, SDL.TEXTUREACCESS_TARGET, width*8, height*16); renderer = SDL.createRenderer(window, -1, flags)
if texture == ffi.C.NULL then if renderer == ffi.C.NULL then
error(ffi.string(SDL.getError())) error(ffi.string(SDL.getError()))
end end
local copytexture = SDL.createTexture(renderer, SDL.PIXELFORMAT_ARGB8888, SDL.TEXTUREACCESS_TARGET, width*8, height*16); texture = SDL.createTexture(renderer, SDL.PIXELFORMAT_ARGB8888, SDL.TEXTUREACCESS_TARGET, width*8, height*16);
if copytexture == ffi.C.NULL then if texture == ffi.C.NULL then
error(ffi.string(SDL.getError())) error(ffi.string(SDL.getError()))
end
copytexture = SDL.createTexture(renderer, SDL.PIXELFORMAT_ARGB8888, SDL.TEXTUREACCESS_TARGET, width*8, height*16);
if copytexture == ffi.C.NULL then
error(ffi.string(SDL.getError()))
end
-- Initialize all the textures to black
SDL.setRenderDrawColor(renderer, 0, 0, 0, 255)
SDL.renderFillRect(renderer, ffi.C.NULL)
SDL.setRenderTarget(renderer, texture);
SDL.renderFillRect(renderer, ffi.C.NULL)
SDL.setRenderTarget(renderer, copytexture);
SDL.renderFillRect(renderer, ffi.C.NULL)
SDL.setRenderTarget(renderer, ffi.C.NULL);
end end
-- Initialize all the textures to black local function cleanUpWindow(wind)
SDL.setRenderDrawColor(renderer, 0, 0, 0, 255) SDL.destroyTexture(texture)
SDL.renderFillRect(renderer, ffi.C.NULL) SDL.destroyTexture(copytexture)
SDL.setRenderTarget(renderer, texture); SDL.destroyRenderer(renderer)
SDL.renderFillRect(renderer, ffi.C.NULL) if wind then
SDL.setRenderTarget(renderer, copytexture); SDL.destroyWindow(window)
SDL.renderFillRect(renderer, ffi.C.NULL) window = nil
SDL.setRenderTarget(renderer, ffi.C.NULL); end
texture, copytexture, renderer = nil
end
createWindow()
elsa.cleanup[#elsa.cleanup+1] = function()
cleanUpWindow(true)
end
function elsa.draw() function elsa.draw()
SDL.renderCopy(renderer, texture, ffi.C.NULL, ffi.C.NULL) SDL.renderCopy(renderer, texture, ffi.C.NULL, ffi.C.NULL)
@ -412,7 +434,42 @@ function cec.setResolution(newwidth, newheight) -- Set the screen resolution. Re
newwidth,newheight = math.floor(newwidth),math.floor(newheight) newwidth,newheight = math.floor(newwidth),math.floor(newheight)
local oldwidth, oldheight = width, height local oldwidth, oldheight = width, height
width, height = newwidth, newheight width, height = newwidth, newheight
return oldwidth ~= width or oldheight ~= height local changed = oldwidth ~= width or oldheight ~= height
if changed then
-- TODO: What magical SDL hacks can I do to make this faster?
cleanUpWindow()
SDL.setWindowSize(window, width*8, height*16)
local xpos, ypos = ffi.new("int[1]"), ffi.new("int[1]")
SDL.getWindowPosition(window, xpos, ypos)
SDL.setWindowPosition(window, xpos[0] - (width-oldwidth)*4, ypos[0] - (height-oldheight)*4)
createWindow()
for y = 1,math.min(oldheight,height) do
for x = 1,math.min(oldwidth,width) do
if (screen.txt[y][x] ~= " " and screen.fg[y][x] ~= 0) or screen.bg[y][x] ~= 0 then
renderChar(utf8.byte(screen.txt[y][x]),(x-1)*8,(y-1)*16,screen.fg[y][x],screen.bg[y][x])
end
end
end
for y = 1,height do
for x = oldwidth+1,width do
screen.txt[y][x] = " "
screen.fg[y][x] = scrfgc
screen.bg[y][x] = scrbgc
screen.fgp[y][x] = scrfgp
screen.bgp[y][x] = scrbgp
end
end
for y = oldheight+1,height do
for x = 1,oldwidth do
screen.txt[y][x] = " "
screen.fg[y][x] = scrfgc
screen.bg[y][x] = scrbgc
screen.fgp[y][x] = scrfgp
screen.bgp[y][x] = scrbgp
end
end
end
return changed
end end
function cec.maxResolution() -- Get the maximum screen resolution. function cec.maxResolution() -- Get the maximum screen resolution.
cprint("(cec) screen.maxResolution") cprint("(cec) screen.maxResolution")