Advertisement
Guest User

LuaJIT-FFI ShivaVG Proof of Concept Test

a guest
Oct 17th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1.  
  2. package.preload['extern.sdl'] = function()
  3.   local ffi = require 'ffi'
  4.  
  5.   ffi.cdef [[
  6.     enum {
  7.       SDL_INIT_VIDEO = 0x00000020,
  8.       SDL_OPENGL = 0x00000002,
  9.       SDL_RESIZABLE = 0x00000010,
  10.       SDL_TIMESLICE = 10
  11.     };
  12.     typedef enum {
  13.       SDL_GL_RED_SIZE, SDL_GL_GREEN_SIZE, SDL_GL_BLUE_SIZE, SDL_GL_ALPHA_SIZE,
  14.       SDL_GL_BUFFER_SIZE,
  15.       SDL_GL_DOUBLEBUFFER,
  16.       SDL_GL_DEPTH_SIZE,
  17.       SDL_GL_STENCIL_SIZE,
  18.       SDL_GL_ACCUM_RED_SIZE,
  19.       SDL_GL_ACCUM_GREEN_SIZE,
  20.       SDL_GL_ACCUM_BLUE_SIZE,
  21.       SDL_GL_ACCUM_ALPHA_SIZE,
  22.       SDL_GL_STEREO,
  23.       SDL_GL_MULTISAMPLEBUFFERS,
  24.       SDL_GL_MULTISAMPLESAMPLES,
  25.       SDL_GL_ACCELERATED_VISUAL,
  26.       SDL_GL_SWAP_CONTROL
  27.     } SDL_GLattr;
  28.     typedef enum {
  29.        SDL_NOEVENT = 0,
  30.        SDL_ACTIVEEVENT = 1,
  31.        SDL_KEYDOWN = 2,
  32.        SDL_KEYUP = 3,
  33.        SDL_MOUSEMOTION = 4,
  34.        SDL_MOUSEBUTTONDOWN = 5,
  35.        SDL_MOUSEBUTTONUP = 6,
  36.        SDL_QUIT = 12,
  37.        SDL_SYSWMEVENT = 13,
  38.        SDL_VIDEORESIZE = 16,
  39.        SDL_VIDEOEXPOSE = 17
  40.     } SDL_EventType;
  41.     typedef enum {
  42.       SDLK_ESCAPE = 27,
  43.       SDLK__FORCESIZE = 323
  44.     } SDLKey;
  45.     typedef enum {
  46.       KMOD__FORCESIZE = 0x8000
  47.     } SDLMod;
  48.     typedef union SDL_Event {
  49.       uint8_t type;
  50.       struct { uint8_t type, gain, state; } active;
  51.       struct {
  52.         uint8_t type, which, state;
  53.         struct { uint8_t scancode; SDLKey sym; SDLMod mod; uint16_t unicode; } keysym;
  54.       } key;
  55.       struct {
  56.         uint8_t type, which, state;
  57.         uint16_t x, y; int16_t xrel, yrel;
  58.       } motion;
  59.       struct {
  60.         uint8_t type, which, button, state;
  61.         uint16_t x, y;
  62.       } button;
  63.       struct { uint8_t type; int w, h; } resize;
  64.       struct { uint8_t type; } expose;
  65.       struct { uint8_t type; } quit;
  66.       struct { uint8_t type; int code; void* data1; void* data2; } user;
  67.       struct { uint8_t type; void* msg; } syswm;
  68.     } SDL_Event;
  69.     int SDL_Init(uint32_t flags);
  70.     void* SDL_SetVideoMode(int width, int height, int bpp, uint32_t flags);
  71.     int SDL_GL_SetAttribute(SDL_GLattr, int value);
  72.     void SDL_Delay(uint32_t ms);
  73.     int SDL_PollEvent(SDL_Event*);
  74.     void SDL_GL_SwapBuffers();
  75.   ]]
  76.  
  77.   return ffi.load 'SDL'
  78.  
  79. end
  80.  
  81. package.preload['extern.shivavg'] = function()
  82.  
  83.   local ffi = require 'ffi'
  84.  
  85.   ffi.cdef [[
  86.     typedef int VGint;
  87.     typedef float VGfloat;
  88.     typedef enum {
  89.       VG_FALSE = 0,
  90.       VG_TRUE  = 1
  91.     } VGboolean;
  92.  
  93.     VGboolean vgCreateContextSH(VGint width, VGint height);
  94.     void vgResizeSurfaceSH(VGint width, VGint height);
  95.     void vgDestroyContextSH();
  96.  
  97.     typedef enum VGParamType {
  98.       VG_CLEAR_COLOR = 0x1121,
  99.       VGPARAMTYPE__FORCESIZE = 0x116A
  100.     } VGParamType;
  101.    
  102.     void vgSetfv(VGParamType, VGint count, const VGfloat* values);
  103.     void vgClear(VGint x, VGint y, VGint width, VGint height);
  104.   ]]
  105.  
  106.   return ffi.load 'libOpenVG'
  107.  
  108. end
  109.  
  110. local bit = require 'bit'
  111. local ffi = require 'ffi'
  112. local sdl = require 'extern.sdl'
  113. local vg = require 'extern.shivavg'
  114.  
  115. local screenWidth = 640;
  116. local screenHeight = 480;
  117. local screen;
  118. local blue = ffi.new('VGfloat[4]', 0, 0, 1, 1);
  119.  
  120. function main()
  121.   local e = ffi.new 'SDL_Event'
  122.   local finished = false
  123.  
  124.   sdl.SDL_Init(sdl.SDL_INIT_VIDEO);
  125.   sdl.SDL_GL_SetAttribute(sdl.SDL_GL_DOUBLEBUFFER, 1);
  126.   sdl.SDL_GL_SetAttribute(sdl.SDL_GL_STENCIL_SIZE, 1);
  127.   screen = sdl.SDL_SetVideoMode(screenWidth, screenHeight, 0, bit.bor(sdl.SDL_OPENGL, sdl.SDL_RESIZABLE));
  128.  
  129.   vg.vgCreateContextSH(screenWidth, screenHeight);
  130.   vg.vgSetfv(vg.VG_CLEAR_COLOR, 4, blue);
  131.  
  132.   while not finished do
  133.     while (sdl.SDL_PollEvent(e) ~= 0) do
  134.       if (e.type == sdl.SDL_QUIT) then
  135.         finished = true;
  136.       elseif (e.type == sdl.SDL_KEYDOWN) then
  137.         if (e.key.keysym.sym == sdl.SDLK_ESCAPE) then
  138.           finished = true;
  139.         end
  140.       elseif (e.type == sdl.SDL_VIDEORESIZE) then
  141.         screenWidth = e.resize.w;
  142.         screenHeight = e.resize.h;
  143.         screen = sdl.SDL_SetVideoMode(screenWidth, screenHeight, 0, bit.bor(sdl.SDL_OPENGL, sdl.SDL_RESIZABLE));
  144.         vg.vgResizeSurfaceSH(screenWidth, screenHeight);
  145.       end
  146.     end
  147.     vg.vgClear(0, 0, screenWidth, screenHeight);
  148.     sdl.SDL_GL_SwapBuffers();
  149.       sdl.SDL_Delay(sdl.SDL_TIMESLICE);
  150.   end
  151.   vg.vgDestroyContextSH();
  152. end
  153.  
  154. main()
  155.  
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement