Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grid = {};
- iodispatch = {};
- vspacing = 4;
- hspacing = 4;
- cursor = 0;
- cellcount = 0;
- pageofs = 0;
- vertex_shader = [[
- uniform int n_ticks;
- void main(void)
- {
- gl_TexCoord[0] = gl_MultiTexCoord0;
- gl_TexCoord[0].s = gl_TexCoord[0].s + fract(n_ticks / 64.0);
- gl_TexCoord[0].t = gl_TexCoord[0].t + fract(n_ticks / 64.0);
- gl_Position = ftransform();
- }]];
- fragment_shader = [[
- #version 120
- uniform sampler2D tex;
- void main() {
- vec4 color = texture2D(tex, gl_TexCoord[0].st);
- gl_FragColor = color;
- }
- ]];
- local function ledconfig_iofun(iotbl)
- local restbl = keyconfig:match(iotbl);
- if (iotbl.active and restbl and restbl[1] and
- ledconfig:input(restbl[1]) == true) then
- gridle_input = keyconfig.iofun;
- end
- end
- function gridle()
- system_load("scripts/keyconf.lua")();
- system_load("scripts/keyconf_mame.lua")();
- system_load("scripts/ledconf.lua")();
- games = list_games( {} );
- if (#games == 0) then
- error "No games found";
- shutdown();
- end
- keyconfig = keyconf_create(1, {
- "rMENU_ESCAPE", "rMENU_LEFT", "rMENU_RIGHT", "rMENU_UP",
- "rMENU_DOWN", "rMENU_SELECT", "rGROW_ICONS", "rSHRINK_ICONS" } );
- keyconfig.iofun = gridle_input;
- if (keyconfig.active == false) then
- gridle_input = function(iotbl) -- keyconfig io function hook
- if (keyconfig:input(iotbl) == true) then
- keyconf_tomame(keyconfig, "_mame/cfg/default.cfg");
- ledconfig = ledconf_create( keyconfig:labels() );
- if (ledconfig.active == false) then
- gridle_input = ledconfig_iofun;
- else -- no LED controller present, or LED configuration already exists
- gridle_input = keyconfig.iofun;
- end
- end
- end
- else
- ledconfig = ledconf_create( keyconfig:labels() );
- if (ledconfig.active == false) then
- gridle_input = ledconf_iofun;
- end
- end
- iodispatch["GROW_ICONS"] = function(iotbl) resize_grid(16); end
- iodispatch["SHRINK_ICONS"] = function(iotbl) resize_grid(-16); end
- iodispatch["MENU_UP"] = function(iotbl) play_sample("click.wav"); move_cursor( -1 * ncw); end
- iodispatch["MENU_DOWN"] = function(iotbl) play_sample("click.wav"); move_cursor( ncw ); end
- iodispatch["MENU_LEFT"] = function(iotbl) play_sample("click.wav"); move_cursor( -1 ); end
- iodispatch["MENU_RIGHT"] = function(iotbl) play_sample("click.wav"); move_cursor( 1 ); end
- iodispatch["MENU_ESCAPE"] = function(iotbl) shutdown(); end
- iodispatch["MENU_SELECT"] = function(iotbl) if (games[cursor + pageofs + 1]) then
- launch_target( games[cursor + pageofs + 1].title, LAUNCH_EXTERNAL); end end
- build_grid(128, 128);
- switch_default_texmode( TEX_REPEAT, TEX_REPEAT );
- bgimage = load_image("background.png");
- resize_image(bgimage, VRESW, VRESH);
- image_scale_txcos(bgimage, VRESW / 32, VRESH / 32);
- image_program(bgimage, vertex_shader, fragment_shader);
- show_image(bgimage);
- switch_default_texmode( TEX_CLAMP, TEX_CLAMP );
- end
- function cell_coords(x, y)
- return (0.5 * borderw) + x * (cell_width + hspacing), (0.5 * borderh) + y * (cell_height + vspacing);
- end
- function blend_gridcell(val, dt)
- local cursor_row = math.floor(cursor / ncw);
- gridcell_vid = grid[ cursor_row ][ cursor - cursor_row * ncw ];
- if (gridcell_vid) then
- instant_image_transform(gridcell_vid);
- blend_image(gridcell_vid, val, dt);
- end
- end
- function resize_grid(step)
- local new_cellw = cell_width; local new_cellh = cell_width;
- -- find the next grid size that would involve a density change
- repeat
- new_cellw = new_cellw + step;
- until math.floor(VRESW / (new_cellw + hspacing)) ~= ncw;
- repeat
- new_cellh = new_cellh + step;
- until math.floor(VRESH / (new_cellh + vspacing)) ~= nch;
- -- safety checks
- if (new_cellw < 64 or new_cellw > VRESW * 0.75) then return; end
- if (new_cellh < 64 or new_cellh > VRESH * 0.75) then return; end
- cell_width = new_cellw;
- cell_height = new_cellh;
- local currgame = pageofs + cursor;
- local new_ncc = math.floor( VRESW / (new_cellw + hspacing) ) * math.floor( VRESH / (new_cellh + vspacing) );
- pageofs = math.floor( currgame / new_ncc ) * new_ncc;
- cursor = currgame - pageofs;
- if (cursor < 0) then cursor = 0; end
- -- remove the old grid
- erase_grid(true);
- build_grid(cell_width, cell_height);
- end
- function move_cursor( ofs )
- blend_gridcell(0.3, 10);
- local pageofs_cur = pageofs;
- cursor = cursor + ofs;
- -- paging calculations
- if (ofs > 0) then -- right/forward
- if (cursor >= ncc) then -- move right or "forward"
- cursor = cursor - ncc;
- pageofs_cur = pageofs_cur + ncc;
- end
- -- wrap around on overflow
- if (pageofs_cur + cursor >= #games) then
- pageofs_cur = 0;
- cursor = 0;
- end
- elseif (ofs < 0) then -- left/backward
- if (cursor < 0) then -- step back a page
- pageofs_cur = pageofs_cur - ncc;
- cursor = ncc - ( -1 * cursor);
- if (pageofs_cur < 0) then -- wrap page around
- pageofs_cur = math.floor(#games / ncc) * ncc;
- end
- if (cursor < 0 or cursor >= #games - pageofs_cur) then
- cursor = #games - pageofs_cur - 1;
- end
- end
- end
- local x,y = cell_coords(math.floor(cursor % ncw), math.floor(cursor / ncw));
- -- reload images of the page has changed
- if (pageofs_cur ~= pageofs) then
- erase_grid(false);
- pageofs = pageofs_cur;
- build_grid(cell_width, cell_height);
- end
- blend_gridcell(1.0, 10);
- local game = games[cursor + pageofs + 1];
- setname = game and game.setname or nil;
- if (movievid) then
- expire_image(movievid, 40);
- blend_image(movievid, 0.0, 40);
- audio_gain(movieaid, 0.0, 40);
- end
- if (game and ledconfig) then
- ledconfig:toggle(game.players, game.buttons);
- end
- -- look for a movie, if one exists, enable a timer that we'll check for later
- if (setname and resource( "movies/" .. setname .. ".avi")) then
- movievid, movieaid = load_movie( "movies/" .. setname .. ".avi" );
- if (movievid and movieaid and movievid ~= BADID and movieaid ~= BADID) then
- audio_gain(movieaid, 0.0);
- move_image(movievid, x, y);
- hide_image(movievid);
- order_image(movievid, 3);
- resize_image(movievid, cell_width, cell_height);
- movietimer = 10;
- end
- else
- moviefile = "";
- movietimer = nil;
- end
- end
- function get_image(romset)
- local rvid = BADID;
- if resource("screenshots/" .. romset .. ".png") then
- rvid = load_image("screenshots/" .. romset .. ".png");
- end
- if (rvid == BADID) then
- rvid = render_text( [[\#000088\ffonts/default.ttf,96 ]] .. romset );
- end
- return rvid;
- end
- function erase_grid(rebuild)
- cellcount = 0;
- for row=0, nch-1 do
- for col=0, ncw-1 do
- if (grid[row][col]) then
- if (rebuild) then
- delete_image(grid[row][col]);
- else
- local delay = 10 + math.random(10, 30);
- local x, y = cell_coords(row, col);
- expire_image(grid[row][col], delay);
- rotate_image(grid[row][col], 270.0, delay);
- scale_image(grid[row][col], 0.01, 0.01, delay);
- end
- grid[row][col] = nil;
- end
- end
- end
- end
- function gridle_clock_pulse()
- if (movietimer) then
- movietimer = movietimer - 1;
- if (movietimer <= 0) then
- play_movie(movievid);
- blend_image(movievid, 1.0, 10);
- audio_gain(movieaid, 1.0, 40);
- movietimer = nil;
- end
- end
- end
- function build_grid(width, height)
- -- figure out how many full cells we can fit with the current resolution
- ncw = math.floor(VRESW / (width + hspacing));
- nch = math.floor(VRESH / (height + vspacing));
- ncc = ncw * nch;
- cell_width = width;
- cell_height = height;
- -- figure out how much "empty" space we'll have to pad with
- borderw = VRESW % (width + hspacing);
- borderh = VRESH % (height + vspacing);
- for row=0, nch-1 do
- grid[row] = {};
- for col=0, ncw-1 do
- local gameno = (row * ncw + col + pageofs + 1); -- games is 1 indexed
- if (games[gameno] == nil) then break; end
- local vid = get_image(games[gameno]["setname"]);
- resize_image(vid, cell_width, cell_height);
- move_image(vid,cell_coords(col, row));
- order_image(vid, 2);
- blend_image(vid, 0.3);
- --
- local whitebg = fill_surface(cell_width, cell_height, 255, 255, 255);
- order_image(whitebg, 1);
- show_image(whitebg);
- link_image(whitebg, vid);
- image_mask_clear(whitebg, MASK_SCALE);
- image_mask_clear(whitebg, MASK_ORIENTATION);
- image_mask_clear(whitebg, MASK_OPACITY);
- grid[row][col] = vid;
- cellcount = cellcount + 1;
- end
- end
- move_cursor(0);
- end
- function gridle_input(iotbl)
- local restbl = keyconfig:match(iotbl);
- if (restbl and iotbl.active) then
- for ind,val in pairs(restbl) do
- if (iodispatch[val]) then
- iodispatch[val](restbl);
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement