Permalink
Browse files

Add bugs example

  • Loading branch information...
1 parent ee2b8f4 commit 431e712f570f52ac668ee88fa8ce72e4c59965d1 @ezequielgarcia committed Jan 5, 2012
Showing with 124 additions and 2 deletions.
  1. +2 −0 .gitignore
  2. +121 −0 examples/bugs.lua
  3. +1 −2 gendfb-lua.pl
View
@@ -2,3 +2,5 @@ directfb.so
tags
.gen.stamp
src
+examples/canvas/*.so
+examples/canvas/*.ttf
View
@@ -0,0 +1,121 @@
+
+require 'directfb'
+
+local bugs = {}
+local dfb, keybuffer, primary, xres, yres, font, fontheight, provider, sprite, desc, fps, frame, temp
+
+local step = 0.0001
+local timestep = 0.0
+local temp = 0
+
+fps = 0
+
+-- Draw the population string in upper left corner
+local function drawPopulation()
+
+ primary:SetColor(0, 0, 60, 0xff)
+ primary:FillRectangle(0, 0, 640, fontheight+5)
+
+ primary:SetColor(180, 200, 255, 0xff)
+ primary:DrawString('Bug population: ' .. #bugs, -1, 10, 0, 'DSTF_LEFT | DSTF_TOP' )
+
+ primary:SetColor(190, 210, 255, 0xff )
+ primary:DrawString('FPS: ' .. math.floor(fps), -1, 300, 0, 'DSTF_LEFT | DSTF_TOP')
+
+ primary:SetColor(255, 210, 120, 0xff )
+ primary:DrawString('Temp: ' .. math.floor(temp), -1, 500, 0, 'DSTF_LEFT | DSTF_TOP')
+end
+
+local function drawBugs()
+
+ primary:SetBlittingFlags('DSBLIT_SRC_COLORKEY')
+
+ for k,v in pairs(bugs) do
+ primary:Blit(sprite, nil, v.x, v.y );
+ end
+end
+
+local function moveBugs()
+
+ for k,v in pairs(bugs) do
+
+ temp = v.vx*v.vx + v.vy*v.vy
+
+ -- TODO: next frame
+ v.x = v.x + v.vx*timestep
+ v.y = v.y + v.vy*timestep
+
+ if v.x > xres or v.x < 0 then v.vx = -v.vx end
+ if v.y > yres or v.y < 0 then v.vy = -v.vy end
+
+ end
+
+end
+
+local function spawn(count)
+ for i=1,count do
+ table.insert(bugs, {x=xres/2, y=yres/2, vx=math.random(5)-3, vy=math.random(5)-3})
+ end
+end
+
+-- DFB Initialization
+directfb.DirectFBInit()
+dfb = directfb.DirectFBCreate()
+
+-- Create input buffer
+keybuffer = dfb:CreateInputEventBuffer(DICAPS_KEYS, DFB_FALSE)
+
+-- Create primary surface, double buffered
+primary = dfb:CreateSurface {caps='DSCAPS_PRIMARY|DSCAPS_DOUBLE'}
+xres, yres = primary:GetSize()
+
+-- load font
+font = dfb:CreateFont('DejaVuSans.ttf', {height=24})
+fontheight = font:GetHeight()
+primary:SetFont(font)
+
+-- load animation
+provider = dfb:CreateImageProvider('bug.gif')
+sprite = dfb:CreateSurface(provider:GetSurfaceDescription())
+provider:RenderTo(sprite, nil)
+
+-- white color key
+sprite:SetSrcColorKey(0xff, 0xff, 0xff)
+
+-- load background, (I hope) GC will take care of previous provider
+provider = dfb:CreateImageProvider('background.jpg')
+desc = provider:GetSurfaceDescription()
+desc.width = xres
+desc.height = yres
+background = dfb:CreateSurface(desc)
+provider:RenderTo(background, nil)
+
+spawn(25)
+
+frame = 1
+timestep = 0.0
+local start = os.time()
+local now, prev = start, start
+-- Main loop
+while not quit do
+
+ primary:SetBlittingFlags('DSBLIT_NOFX')
+
+ primary:Blit(background, nil, 0, 0)
+
+ moveBugs()
+
+ drawBugs()
+
+ drawPopulation()
+
+ primary:Flip(nil, 0)
+
+ now = os.time()
+ if now - prev > 0 then
+ fps = frame / (now - start)
+ prev = now
+ end
+ frame = frame + 1
+ timestep = timestep + step
+end
View
@@ -73,7 +73,6 @@
$blacklist{GetPalette} = 1;
$blacklist{SetPalette} = 1;
$blacklist{SetDstGeometry} = 1;
-$blacklist{CreateInputEventBuffer} = 1;
$blacklist{SendEvent} = 1;
$blacklist{GetScreen} = 1;
$blacklist{GetKeymapEntry} = 1;
@@ -931,7 +930,7 @@ sub parse_func {
c_create( STRUCTS_C, "structs.c", "#include \"common.h\"\n#include \"enums.h\"\n" );
h_create( ENUMS_H, "enums.h", "#include \"common.h\"\n" );
-c_create( ENUMS_C, "enums.c", "#include \"enums.h\"\n#include \"common.h\"\n" );
+c_create( ENUMS_C, "enums.c", "#include \"ctype.h\"\n#include \"enums.h\"\n#include \"common.h\"\n" );
print COMMON_H "#if defined(__GNUC__) && __GNUC__ >= 4\n",
"\t#define DLL_EXPORT __attribute__((visibility(\"default\")))\n",

0 comments on commit 431e712

Please sign in to comment.