lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


KHMan wrote:

So, as far as LuaSDL at LuaForge goes, you're on your own. Note that other SDL Lua bindings exists, so YMMV.

Maybe a little bit offtopic, but if you want to try SDL from lua, I just "ported" and tested a simple "hello world" SDL program using FFI (alien). Maybe it will help you start with SDL.

Will not work in murgalua unless you compile in support for alien.

Regards,
miko
-- Run with lua -l luarocks.require testsdl.lua
--
-- Ported to lua from:
-- http://gamedevgeek.com/tutorials/getting-started-with-sdl/

require "alien"
SDL=alien.load('/usr/lib/libSDL.so')

SCREEN_WIDTH,SCREEN_HEIGHT=640,480

local p,i,s,v="pointer","int","string", "void"

-- prepare ffi interface
SDL.SDL_Init:types(i,i)
SDL.SDL_WM_SetCaption:types(v, s, s)
SDL.SDL_SetVideoMode:types(p, i, i, i, i)
--SDL.SDL_LoadBMP:types(p,s)
SDL.SDL_LoadBMP_RW:types(p,s)
SDL.SDL_RWFromFile:types(p,s, s)
SDL.SDL_DisplayFormat:types(p,p)
SDL.SDL_FreeSurface:types(v,p)
SDL.SDL_PollEvent:types(i,p)
--SDL.SDL_BlitSurface:types(i,p,p,p,p)
SDL.SDL_UpperBlit:types(i,p,p,p,p)
SDL.SDL_UpdateRect:types(v, p, i, i, i, i)

-- wrapper functions
function SDL_LoadBMP(file)
  return SDL.SDL_LoadBMP_RW(SDL.SDL_RWFromFile(file, "rb"))
end

function SDL_BlitSurface(src, srcrect, dst, dstrect)
  return SDL.SDL_UpperBlit(src, srcrect, dst, dstrect)
end

-- Initialize SDL
local SDL_INIT_VIDEO=0x20
SDL.SDL_Init(SDL_INIT_VIDEO)

-- set the title bar
SDL.SDL_WM_SetCaption("SDL Test","SDL Test")

-- create window
local screen = SDL.SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0)

-- load bitmap to temp surface
local temp = SDL_LoadBMP('sdl_logo.bmp') -- get it from http://gamedevgeek.com/files/sdltest.tar.gz

-- convert bitmap to display format
local bg = SDL.SDL_DisplayFormat(temp)

-- free the temp surface
SDL.SDL_FreeSurface(temp);

local event=alien.buffer()
local gameover = false;
-- From /usr/include/SDL/SDL_events.h
SDL_KEYDOWN,SDL_QUIT=2,12
SDLK_q,SDLK_ESCAPE=113, 27

-- message pump
while not gameover do
  -- look for an event
  if SDL.SDL_PollEvent(event)==1 then
    -- an event was found
    local etype=event[1]
    if etype==SDL_QUIT then
      -- close button clicked
      gameover=true
      break
    end

    if etype==SDL_KEYDOWN then
      -- handle the keyboard
      sym=event[9]

      if sym==SDLK_q or sym==SDLK_ESCAPE then
        gameover=true
        break
      end
    end

  end
  SDL_BlitSurface(bg, nil, screen, nil)
  SDL.SDL_UpdateRect(screen, 0,0,0,0)
end