-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.lua
101 lines (78 loc) · 2.56 KB
/
app.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
local ffi = require("ffi")
--[[
https://gist.github.com/creationix/1213280/a97d7051decb2f1d3e8844186bbff49b6442700a
-- Parse the C API header
-- It's generated with:
--
-- echo '#include <SDL.h>' > stub.c
-- gcc -I /usr/include/SDL -E stub.c | grep -v '^#' > ffi_SDL.h
--]]
ffi.cdef( io.open('ffi_defs.h','r'):read('*a') )
local SDL = ffi.load('SDL2')
_G=setmetatable(_G, {
__index = function(self, index) -- index function CASE
if "SDL"==string.sub(index,1,3) then
return SDL[index]
end
end
})
SDL_Init(0)
local window = SDL_CreateWindow("title", 50,50, 400,300, 0)
local window_surface = SDL_GetWindowSurface(window)
function image_load(name)
local file = SDL_RWFromFile(name..".bmp", "rb")
return SDL_LoadBMP_RW(file, 1)
end
function image_transparency(image_surface,rgb)
local key = SDL_MapRGB(window_surface.format,rgb[1],rgb[2],rgb[3])
SDL_SetColorKey(image_surface, SDL_TRUE, key)
end
function rect_from_xywh(xywh)
if xywh == nil then return nil end
local rect = ffi.new('SDL_Rect')
rect.x = xywh[1]
rect.y = xywh[2]
rect.w = xywh[3]
rect.h = xywh[4]
return rect
end
function surface_draw_rect(rgb, xywh)
SDL_FillRect(window_surface, rect_from_xywh(xywh), SDL_MapRGB(window_surface.format,rgb[1],rgb[2],rgb[3]))
end
function surface_draw_image(image_surface, xywh)
SDL_UpperBlitScaled(image_surface, nil, window_surface, rect_from_xywh(xywh) )
end
local image_surface = image_load("transparence")
local rgb = {0,0,255} -- key color (blue is transparent)
image_transparency(image_surface,rgb)
local event = ffi.new("SDL_Event")
local looping = true
local movement = 0
while looping do
-- update (before draw)
while SDL_PollEvent(event) ~= 0 do
if event.type == SDL_KEYDOWN and event.key.keysym.sym == SDLK_RIGHT then
movement = movement + 10
elseif event.type == SDL_KEYDOWN and event.key.keysym.sym == SDLK_LEFT then
movement = movement - 10
end
if event.type == SDL_QUIT or
( event.type == SDL_KEYDOWN and event.key.keysym.sym == SDLK_ESCAPE )
then
-- exit
looping = false
break
end
end
if not looping then break end
-- draw (after update)
surface_draw_rect({255,255,255}) -- draw-begin: clear
local rgb={255,255,0}
local xywh={0,0, 100, 100}
surface_draw_rect(rgb, xywh)
xywh[1] = xywh[1] + xywh[3] + movement -- image put aside
surface_draw_image(image_surface, xywh)
SDL_UpdateWindowSurface(window) -- draw-end: present
end
SDL_FreeSurface(image_surface)
SDL_Quit()