android
  #1  
Old 12-22-2009, 01:35 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default 'Offline' Application development -Windows : LuaSDL

I'm getting tiered with plugging and unplugging my Zen X-fi2 every time
I want to test or change a parameter. Looking for a graphic environment for
'Lua 5.1 for windows' did not go well. But i came across this: LuaSDL from
http://luaforge.net/frs/?group_id=242&release_id=781
FILE: http://luaforge.net/frs/download.php...in32-0.3.4.zip

With this i can now program in LUA on windows with graphic output
it has different names for most of the creative custom stuff, but it's very close.
And with the source code to luaSDL available, maybe it could be altered to fit.

2 examples.

X-fi2 - color_black = color.new(0,0,0);
win32 - color_black = SDL.SDL_MapRGB(screen.format,0,0,0);

X-fi2 - screen.drawpixel(x,y,pixelcolor)
win32 - SDL.SDL_PutPixel(screen,x,y,pixelcolor);


So ! I'm using the sciTE editor that comes with 'Lua 5.1 for Windows'
as a text editor with lua highlight and to clear up syntax error's.


I save the file when done, and click and drag it over the luasdl.exe file:



Graphic output, a windows at 400x240 pixels: Planets[1]



Here is a template file where the create window stuff in made into a function initscreen(); and cleaned up. There is more examples in the file __init__.lua it gets run when luasdl.exe is run without parms.



Code:
-- Template for demos.
-- This file is part of LuaSDL.
-- PUBLIC DOMAIN. Author: Kein-Hong Man <khman@users.sf.net> 2007
------------------------------------------------------------------------

function initscreen()

    local SDL = SDL -- local ref to SDL library
    -- test initialization of SDL
    if SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0 then
      error("Couldn't initialize SDL: "..SDL.SDL_GetError().."\n")
      os.exit(1)
    end

    SDL.SDL_WM_SetCaption("X-Fi2 Application Programming", "demo")
    screen = SDL.SDL_SetVideoMode(400, 240, 32, 0) --set window size
    if not screen then
       error("Couldn't set video mode: "..SDL.SDL_GetError().."\n");
    end;

end;


function putpixel(x,y,pixelcolor)
    local i=1;
    if ( x > 399 ) then i=0; end;
    if ( x <   0 ) then i=0; end;
    if ( y > 239 ) then i=0; end;
    if ( y <   0 ) then i=0; end;
    if ( i == 1 )  then
    SDL.SDL_PutPixel(screen,x,y,pixelcolor);
    end;
end;


function planets(refx,refy)
color_front = SDL.SDL_MapRGB(screen.format,255,255,0);
color_shaded = SDL.SDL_MapRGB(screen.format,240,0,0);
size = 100;
sizes = size*size;
for y=-size,size do
  aq=sizes-y*y;
  x=math.sqrt(aq);
  x2=x*2;
  for i=-x,x do
   if (math.random(0,x2)-x <i) then putpixel(refx+i,refy+y,color_front);
   else putpixel(refx+i,refy+y,color_shaded);
   end;
   end;
  end;
end;

initscreen();

color_black = SDL.SDL_MapRGB(screen.format,0,0,0);
color_white = SDL.SDL_MapRGB(screen.format,255,255,255);

putpixel(200,120,color_white);
planets(200,120);
SDL.SDL_UpdateRect(screen, 0, 0, 0, 0);




  local event = SDL.SDL_Event_local()
while true do
    while (SDL.SDL_PollEvent(event) == 0) do
      SDL.SDL_Delay(10)
    end
    local c = event.type
    if c == SDL.SDL_KEYDOWN then
      local key = event.key.keysym.sym
      if key == SDL.SDLK_ESCAPE then
        break
      end
    elseif c == SDL.SDL_QUIT then
      break
    end
end


SDL.SDL_Quit()

-- end of script
Planets[1]: The original Basic code for the function 'planets' comes from the book
Creative Graphics on the BBC Microcomputer by John Cownie.
http://members.aon.at/~musher/bbc/library.htm
- good book,lol,,, i bought it around 82-83 i think.


Last edited by Jan_DK; 12-22-2009 at 05:42 PM. Reason: Function putpixel in error and missing 2 limit, very importent
Reply With Quote

Advertisement [Remove Advertisement]

  #2  
Old 12-22-2009, 02:28 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

In the programming language C there is this way of switching
code blocks depending on the run environment.

#define RUN_ON_X-Fi2

#ifdef RUN_ON_X-Fi2
--code block for running on x-fi2
color_black = color.new(0,0,0);
#else
--code block for running on win32 luaSDL
color_black = SDL.SDL_MapRGB(screen.format,0,0,0);
#endif

Do anyone knows what LUA has to offer in that area. ?


Jan
Reply With Quote

  #3  
Old 12-25-2009, 04:14 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Now I'm trying to emulate the Creative custom function.
It's starting out slow, but it works.

Here is an example code of the substitute LuaSDL functions for screen.drawpixel, screen.update, color.new, os.sleep.

Code:
--X-Fi2 Emulation functions "screen."
--Creating new tables with sub tables

    screen = { drawpixel = self, update = self};

    function screen.drawpixel(x,y,pixelcolor)
       SDL.SDL_PutPixel(display,x,y,pixelcolor);
    end;

    function screen.update()
      SDL.SDL_UpdateRect(display, 0, 0, 0, 0);
    end;

--X-Fi2 Emulation functions "color."
--creating new tables with sub table
    color = { new = self };
    function color.new(r,g,b)
      return SDL.SDL_MapRGB(display.format,r,g,b);
    end;

--X-Fi2 Emulation functions "os." creating new sub tables
    function os.sleep(z)
      SDL.SDL_Delay(z);
    end;
The meaning is that I can remark the hole substitute function block out in one '[' or later include it as a lib.

I've made this classic starfield screensaver into a dual X-Fi2 and LuaSDL
program, dual in that regards that it take a few steps to make the switch
from the current X-Fi2 state, and into LuaSDL. Steps 3 & 4 are only necessary
right now be course I've not done the substitute func for control.read & control.isbutton yet.


screen-shot of windows starfield screen saver. It's to hard to take real pic's of this on the X-fi2.


steps for making the Starfield running on LuaSDL
1. Remove a '[' from line 7 after the --[
2. Remove a '[' from line 72 after the --[
3. Place a '[' at line 128 --[hold
4. Remove a '[' from line 134 --[[
5. Remove -- to start of line 150

The Starfield Code: ready to run on the X-fi2
Code:
-- Template for demos. This file is part of LuaSDL. luaSDL code Author: Kein-Hong Man 2007
-- lua adaptation by Jan_DK of the starfield code from:
-- http://freespace.virgin.net/hugo.elias/graphics/x_stars.htm
------------------------------------------------------------------------


--[[ SDL Creative X-Fi2 Emulation function block start.


--SDL window setup
    function init_SDL_screen()
        local SDL = SDL -- local ref to SDL library
        -- test initialization of SDL
        if SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0 then
          error("Couldn't initialize SDL: "..SDL.SDL_GetError().."\n")
          os.exit(1);end
        SDL.SDL_WM_SetCaption("X-Fi2 Application Programming", "demo")
        display = SDL.SDL_SetVideoMode(400,240, 32, 0) --set window size
        if not display then
           error("Couldn't set video mode: "..SDL.SDL_GetError().."\n");
        end;

    end;

--X-Fi2 Emulation functions "screen."
--Creating new tables with sub tables

    screen = { drawpixel = self, update = self};

    function screen.drawpixel(x,y,pixelcolor)
       SDL.SDL_PutPixel(display,x,y,pixelcolor);
    end;

    function screen.update()
      SDL.SDL_UpdateRect(display, 0, 0, 0, 0);
    end;

--X-Fi2 Emulation functions "color."
--creating new tables with sub table
    color = { new = self };
    function color.new(r,g,b)
      return SDL.SDL_MapRGB(display.format,r,g,b);
    end;

--X-Fi2 Emulation functions "os." creating new sub tables only, os. exist allready. 
    function os.sleep(z)
      SDL.SDL_Delay(z);
    end;

--X-Fi2 Emulation function block end.--]]



--Normal creative functions
function putpixel(x,y,pixelcolor)
    if (x>399) or (x<0) or (y>239) or (y<0)
    then return
    else
    screen.drawpixel(x,y,pixelcolor);
    end;
end;

function clearscreen()
    for y=0,239 do
      for x=0,399 do
       putpixel(x,y,color_black);
      end;
    end;
    screen.update();
end;

--[[ remove remarks to run on luaSDL
init_SDL_screen();
event = SDL.SDL_Event_local();
--]]
--background & forground colors | on LuaSDL init_SDL_screen has to happen before color definition
color_black = color.new(0,0,0);
color_white = color.new(255,255,255);
starcolor = 0;
--star pos range
star_pos_x_min = -700; star_pos_x_max = 700;
star_pos_y_min = -600; star_pos_y_max = 600;
star_pos_z_min =  400; star_pos_z_max = 700;
--Starfield
numberofstars = 250;
centerofscreen_x = 200;
centerofscreen_y = 120;
--tables
starfield = {} ;
star_x={}; star_y ={} ; star_z={}; star_screenx={};star_screeny={};

clearscreen();


-- initiliase tables and populate starfield
    for loop=1,numberofstars do
     starfield[loop]={ star_x=math.random(star_pos_x_min,star_pos_x_max),
                       star_y=math.random(star_pos_y_min,star_pos_y_max),
                       star_z=math.random(star_pos_z_min,star_pos_z_max),
                       star_screenx=1,star_screeny=1 };
    end;

while true do

    for i=1,numberofstars do
       putpixel(starfield[i].star_screenx, starfield[i].star_screeny,color_black);
      starfield[i].star_z = starfield[i].star_z - 5;--speed
      starfield[i].star_screenx = starfield[i].star_x / starfield[i].star_z * 50 + centerofscreen_x; --'50'FOV
      starfield[i].star_screeny = starfield[i].star_y / starfield[i].star_z * 50 + centerofscreen_y; --'50'FOV

          --if outside screen create new star, else draw star
      if (starfield[i].star_screenx>399) or (starfield[i].star_screenx<0) or
         (starfield[i].star_screeny>239) or (starfield[i].star_z < 1) then
         starfield[i].star_x= math.random(star_pos_x_min,star_pos_x_max);
         starfield[i].star_y= math.random(star_pos_y_min,star_pos_y_max);
         starfield[i].star_z= math.random(star_pos_z_min,star_pos_z_max);
      else
      starcolor = 500-( starfield[i].star_z*(500/700) );--set star brightness, far=dim close=bright
      if starcolor>255 then starcolor=255; end;
      putpixel(starfield[i].star_screenx, starfield[i].star_screeny,color.new(starcolor,starcolor,starcolor));
      end;
    end;

    screen.update();
    os.sleep(1);


    --[hold home button to exit
    if (control.read() == 1) and (control.isButton() == 1) and (button.home() == 1) and (button.hold() == 1) then
    break;
    end;
    --]]

    --[[
    poll=SDL.SDL_PollEvent(event);
    local c = event.type
    if c == SDL.SDL_KEYDOWN then
      local key = event.key.keysym.sym
      if key == SDL.SDLK_ESCAPE then
        break
      end
    elseif c == SDL.SDL_QUIT then
      break
    end
    --]]

end;

--remove remark from line below to run on LuaSDL
--SDL.SDL_Quit();
I learned a great deal about lua and lua tabels vs. C's typedef struct and arrays from making this starfield adaptation.

It also looks like around 200 stars in the field is maximum for the cpu power
of the X-Fi, in the current environment.

Jan_DK
Reply With Quote

  #4  
Old 01-05-2010, 11:46 AM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default

I am currently trying to write a translation tool that uses LuaSDL with an input file written in Zen LUA. That means you write a LUA app like you do on your Zen and you drag it into my translator which translates it and executes the code with Lua SDL.
However, there's nothing to show off yet, sorry. I hope I can offer a download soon.
Reply With Quote

  #5  
Old 01-07-2010, 09:47 PM
skybluedream skybluedream is offline
Member
 
Join Date: Oct 2009
Posts: 132
Default

Can y'all come up with an Application to view "GIF" images? I know it wouldn't make any sense but watching a couple off animations while listening to music would be great.
Reply With Quote

  #6  
Old 01-07-2010, 10:29 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Does the Xfi2 play music you've started during applications?
Reply With Quote

  #7  
Old 01-08-2010, 02:58 AM
skybluedream skybluedream is offline
Member
 
Join Date: Oct 2009
Posts: 132
Default

Yeah it music plays while using apps. Tried it with Boink, Calculator, Sudoku & the TicTacToe app which you programmed.
Reply With Quote

  #8  
Old 01-08-2010, 03:46 AM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

TicTacToe was ZaPx64 's application wasn't it? I'll look into a simple gif loading app.
Reply With Quote

  #9  
Old 01-08-2010, 09:46 PM
skybluedream skybluedream is offline
Member
 
Join Date: Oct 2009
Posts: 132
Default

Woops sorry! Thanks for reminding I forgot! My bad Zapx64 lol.

Yeah a "GIF" viewer would be great.
Reply With Quote

  #10  
Old 01-11-2010, 03:14 PM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default

Well, a GIF viewer should not be to hard to code since you just need to extract each frame of a gif file and draw it over the last one.
By the way I encountered problems with the translator and I don't think it will ever be completed. But I can see a GIF loading code floating by in front of my eyes...
Reply With Quote

  #11  
Old 01-11-2010, 03:29 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

It's not as easy as it seems. Since GIF drawing isn't supported we have to code the support for everything, header, color tables, image data, LZW data decompression, etc. The white papers for the format are annoyingly un-user-friendly and don't contain information for the animation part as that's something entirely different.

GIF White Papers
http://www.w3.org/Graphics/GIF/spec-gif89a.txt

Animation Information
http://en.wikipedia.org/wiki/Graphic...#Animated_.gif

LZW Data Compression Algorithm
http://marknelson.us/1989/10/01/lzw-data-compression/
Reply With Quote

  #12  
Old 01-12-2010, 05:34 AM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default

Quote:
Originally Posted by ThievingSix View Post
Since GIF drawing isn't supported we have to code the support for everything, header, color tables, image data, LZW data decompression, etc.
So where's the problem? White papers often look user unfriendly but there's always a hidden source on the internet that says everything you need in a short and clear way. I even coded a Quake 3 BSP loader one year ago (which might as well be possible for the Zen although realtime rendering will not be possible...)
Reply With Quote

  #13  
Old 01-12-2010, 06:52 AM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Well if you find such a hidden source be my guess in sharing =D.
Reply With Quote

  #14  
Old 01-17-2010, 11:27 AM
ZaPx64's Avatar
ZaPx64 ZaPx64 is offline
Member
 
Join Date: Nov 2009
Location: Germany
Posts: 136
Default

Here you go: http://www.daubnet.com/en/file-format-gif
Reply With Quote

  #15  
Old 01-17-2010, 12:17 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

=-O! Where did you find that haha. Nice one!
Reply With Quote

  #16  
Old 01-17-2010, 02:07 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

What about all the limitations in the current environment?

Please correct me if I'm wrong.

The only way right now, to write to the screen, is through the screen.pixel function. (That got to be slow vs. a framebuffer pointer)

And the X-Fi2 has not enough memory in the Creative Lua environment,
to even allocate a buffer the size of the screen 400x240x3 = 288k ? [1]

Maybe any Gif image/animation attempts should start backwards with
the memory available, and then calculate how many frames and what size
that amounts to.

Any thoughts ?

[1] I still need to try a 2d array the size 400x240 instead off 0-32768

Jan_DK
Reply With Quote

  #17  
Old 01-18-2010, 01:52 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

I've tried to allocate a screen size buffer using a 2d table, the program stops at around, 157 in the h loop: 157*400*4= 251200
Code:
BUFFER = {} ;

for h=1,240 do
    BUFFER[h] = {};
      for l=1,400 do
       BUFFER[h][l] = 255;
       end;
end;
I wonder how many full screen jpg/png images the X-Fi2 can hold in memory.

Jan_DK
Reply With Quote

  #18  
Old 01-18-2010, 02:30 PM
bzdbbb's Avatar
bzdbbb bzdbbb is offline
X-Fi2 Development Moderator
 
Join Date: Dec 2009
Location: Gloucestershire, UK
Posts: 94
Default

what about using a lower resolution? if you had an array of 120x200 and when drawing using fillrect for each square. It might be slower but it's worth having a look at, even if its down to only something tiny like 24x40 :P Once little Gif's like that can be displayed it might be easier to work out how to scale it up..
Reply With Quote

  #19  
Old 01-18-2010, 04:04 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Another ting just accrued to me, what happens in this code
Code:
io.input("read.txt"); 
x = io.read("*all");
Dos x just point to the file, or load it into a buffer, hmm
and what about binary files, can you eg. index x[1] to access the data hmm.
some stuff to study and test.

another thing if Lua variables are 4bytes floats then the buffer can be used like this maybe. ?
BUFFER[h][l] = 0xFFFFFFFF;

using the 0xFFFFFFFF still make the loop stop at 157,
but right now I don't know if all 4 bytes is stored.


Jan_DK
Reply With Quote

  #20  
Old 01-18-2010, 05:04 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Looks like all four bytes is stored.
when I allocated a single dimension array running 0-n it stopped at 32768
32768 *4 = 128K, now with a too dimensional array it goes to maybe 256K (157*400*4)

This code use of all four bytes in the Lua variable,
it allocate a buffer the size 240x240*4bytes = 230400 bytes,
and fills it with 0xFFFFFF00+ 1,2,3,4 so
BUFFER[1][1] = 4294967041
BUFFER[1][2] = 4294967042

Code:
color_black = color.new(0,0,0);
color_white = color.new(255,255,255);
text.color(color_white);
text.size(16);
screen.fillrect(0,0,400,240,color_black);
screen.update();

BUFFER = {} ;


for h=1,240 do
    BUFFER[h] = {};
      for l=1,240 do
         BUFFER[h][l] = 0xFFFFFF00+l;
      end;
      screen.fillrect(0,0,400,240,color_black);
      text.draw(20,20,"BUFFER = "..h );
      screen.update();
end;

text.draw(20,50,"BUFFER[1][1] = "..BUFFER[1][1]);
text.draw(20,70,"BUFFER[1][2] = "..BUFFER[1][2]);
text.draw(20,90,"BUFFER[1][3] = "..BUFFER[1][3]);
text.draw(20,110,"BUFFER[100][100] = "..BUFFER[100][100]);
screen.update();
os.sleep(1000);
Jan_DK
Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:40 PM.