Skip to content
This repository
Browse code

Add bugs example

  • Loading branch information...
commit 431e712f570f52ac668ee88fa8ce72e4c59965d1 1 parent ee2b8f4
Ezequiel Garcia authored
2  .gitignore
@@ -2,3 +2,5 @@ directfb.so
2 2
 tags
3 3
 .gen.stamp
4 4
 src
  5
+examples/canvas/*.so
  6
+examples/canvas/*.ttf
121  examples/bugs.lua
... ...
@@ -0,0 +1,121 @@
  1
+
  2
+require 'directfb'
  3
+
  4
+local bugs = {}
  5
+local dfb, keybuffer, primary, xres, yres, font, fontheight, provider, sprite, desc, fps, frame, temp
  6
+
  7
+local step = 0.0001
  8
+local timestep = 0.0
  9
+local temp = 0
  10
+
  11
+fps = 0
  12
+
  13
+-- Draw the population string in upper left corner
  14
+local function drawPopulation()
  15
+
  16
+	primary:SetColor(0, 0, 60, 0xff)
  17
+	primary:FillRectangle(0, 0, 640, fontheight+5)
  18
+
  19
+	primary:SetColor(180, 200, 255, 0xff)
  20
+	primary:DrawString('Bug population: ' .. #bugs, -1, 10, 0, 'DSTF_LEFT | DSTF_TOP' )
  21
+
  22
+	primary:SetColor(190, 210, 255, 0xff )
  23
+	primary:DrawString('FPS: ' .. math.floor(fps), -1, 300, 0, 'DSTF_LEFT | DSTF_TOP')
  24
+
  25
+	primary:SetColor(255, 210, 120, 0xff )
  26
+	primary:DrawString('Temp: ' .. math.floor(temp), -1, 500, 0, 'DSTF_LEFT | DSTF_TOP')
  27
+end
  28
+
  29
+local function drawBugs()
  30
+
  31
+	primary:SetBlittingFlags('DSBLIT_SRC_COLORKEY')
  32
+
  33
+	for k,v in pairs(bugs) do
  34
+		primary:Blit(sprite, nil, v.x, v.y );
  35
+	end
  36
+end
  37
+
  38
+local function moveBugs()
  39
+
  40
+	for k,v in pairs(bugs) do
  41
+
  42
+		temp = v.vx*v.vx + v.vy*v.vy
  43
+
  44
+		-- TODO: next frame
  45
+		v.x = v.x + v.vx*timestep
  46
+		v.y = v.y + v.vy*timestep
  47
+
  48
+		if v.x > xres or v.x < 0 then v.vx = -v.vx end
  49
+		if v.y > yres or v.y < 0 then v.vy = -v.vy end
  50
+		
  51
+	end
  52
+
  53
+end
  54
+
  55
+local function spawn(count)
  56
+	for i=1,count do
  57
+		table.insert(bugs, {x=xres/2, y=yres/2, vx=math.random(5)-3, vy=math.random(5)-3})
  58
+	end
  59
+end
  60
+
  61
+-- DFB Initialization
  62
+directfb.DirectFBInit()
  63
+dfb = directfb.DirectFBCreate()
  64
+
  65
+-- Create input buffer
  66
+keybuffer = dfb:CreateInputEventBuffer(DICAPS_KEYS, DFB_FALSE)
  67
+
  68
+-- Create primary surface, double buffered
  69
+primary = dfb:CreateSurface {caps='DSCAPS_PRIMARY|DSCAPS_DOUBLE'}
  70
+xres, yres = primary:GetSize()
  71
+
  72
+-- load font
  73
+font = dfb:CreateFont('DejaVuSans.ttf', {height=24})
  74
+fontheight = font:GetHeight()
  75
+primary:SetFont(font)
  76
+
  77
+-- load animation
  78
+provider = dfb:CreateImageProvider('bug.gif')
  79
+sprite = dfb:CreateSurface(provider:GetSurfaceDescription())
  80
+provider:RenderTo(sprite, nil)
  81
+
  82
+-- white color key
  83
+sprite:SetSrcColorKey(0xff, 0xff, 0xff)
  84
+
  85
+-- load background, (I hope) GC will take care of previous provider
  86
+provider = dfb:CreateImageProvider('background.jpg')
  87
+desc = provider:GetSurfaceDescription()
  88
+desc.width = xres
  89
+desc.height = yres
  90
+background = dfb:CreateSurface(desc)
  91
+provider:RenderTo(background, nil)
  92
+
  93
+spawn(25)
  94
+
  95
+frame = 1
  96
+timestep = 0.0
  97
+local start = os.time()
  98
+local now, prev = start, start
  99
+-- Main loop
  100
+while not quit do
  101
+
  102
+	primary:SetBlittingFlags('DSBLIT_NOFX')
  103
+        
  104
+	primary:Blit(background, nil, 0, 0)
  105
+
  106
+	moveBugs()
  107
+
  108
+	drawBugs()
  109
+
  110
+	drawPopulation()
  111
+          
  112
+	primary:Flip(nil, 0)
  113
+
  114
+	now = os.time()
  115
+	if now - prev > 0 then
  116
+		fps = frame / (now - start)
  117
+		prev = now
  118
+	end
  119
+	frame = frame + 1
  120
+	timestep = timestep + step
  121
+end
3  gendfb-lua.pl
@@ -73,7 +73,6 @@
73 73
 $blacklist{GetPalette}				= 1;
74 74
 $blacklist{SetPalette}				= 1;
75 75
 $blacklist{SetDstGeometry}			= 1;
76  
-$blacklist{CreateInputEventBuffer}	= 1;
77 76
 $blacklist{SendEvent}				= 1;
78 77
 $blacklist{GetScreen}				= 1;
79 78
 $blacklist{GetKeymapEntry}			= 1;
@@ -931,7 +930,7 @@ sub parse_func {
931 930
 c_create( STRUCTS_C, "structs.c", "#include \"common.h\"\n#include \"enums.h\"\n" );
932 931
 
933 932
 h_create( ENUMS_H, "enums.h", "#include \"common.h\"\n" );
934  
-c_create( ENUMS_C, "enums.c", "#include \"enums.h\"\n#include \"common.h\"\n" );
  933
+c_create( ENUMS_C, "enums.c", "#include \"ctype.h\"\n#include \"enums.h\"\n#include \"common.h\"\n" );
935 934
 
936 935
 print COMMON_H	"#if defined(__GNUC__) && __GNUC__ >= 4\n",
937 936
 				"\t#define DLL_EXPORT __attribute__((visibility(\"default\")))\n",

0 notes on commit 431e712

Please sign in to comment.
Something went wrong with that request. Please try again.