Add 3D example
This commit is contained in:
parent
411de3d7c7
commit
0df6d91934
215
mods/main/script/game.lua
Normal file
215
mods/main/script/game.lua
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
package.path = basepath.. "/mods/main/script/?.lua;" ..basepath.. "/mods/main/script/?/main.lua;" ..package.path
|
||||||
|
|
||||||
|
function bp(pth)
|
||||||
|
return basepath .. "/mods/main/" ..pth
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local width = 320
|
||||||
|
local height = 240
|
||||||
|
local px = -1
|
||||||
|
local py = -0.4
|
||||||
|
local pz = 0.5
|
||||||
|
local prx = 0
|
||||||
|
local pry = 0
|
||||||
|
|
||||||
|
function main()
|
||||||
|
window = engine_window_create(width,height,"engine")
|
||||||
|
|
||||||
|
-- Load chars
|
||||||
|
loadchar("invalid","invalid")
|
||||||
|
for ch=0,9,1 do loadchar(tostring(ch),tostring(ch)) end
|
||||||
|
loadchar("space"," ")
|
||||||
|
loadchar("dot",".")
|
||||||
|
loadchar("minus","-")
|
||||||
|
engine_rendertarget_set(engine_window_texture_get(window))
|
||||||
|
|
||||||
|
-- Clear window
|
||||||
|
engine_color_set(0,0,0,255)
|
||||||
|
engine_rendertarget_fill(0,0,width - 1,height - 1)
|
||||||
|
|
||||||
|
while true do
|
||||||
|
event = engine_event_get()
|
||||||
|
eventData = {engine_lua_event_get_data(event)}
|
||||||
|
engine_event_free(event)
|
||||||
|
if eventData[1] ~= ENGINE_EVENT_TYPE_NONE then
|
||||||
|
handleEvent(event,eventData)
|
||||||
|
else
|
||||||
|
tick()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function logic()
|
||||||
|
engine_color_set(0,0,20,255)
|
||||||
|
engine_rendertarget_fill(0,0,width - 1,height - 1)
|
||||||
|
|
||||||
|
if pressedKeys[26] then -- w
|
||||||
|
prx = prx + (0.1 * frametime)
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[22] then -- s
|
||||||
|
prx = prx - (0.1 * frametime)
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[7] then -- d
|
||||||
|
pry = pry + (0.1 * frametime)
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[4] then -- a
|
||||||
|
pry = pry - (0.1 * frametime)
|
||||||
|
end
|
||||||
|
|
||||||
|
local fwdX,fwdY,fwdZ = engine_3d_rotate_position(1,0,0,0,-pry,0)
|
||||||
|
local rgtX,rgtY,rgtZ = engine_3d_rotate_position(0,0,1,0,-pry,0)
|
||||||
|
|
||||||
|
if pressedKeys[82] then -- up
|
||||||
|
px = px + (fwdX * (0.01 * frametime))
|
||||||
|
py = py + (fwdY * (0.01 * frametime))
|
||||||
|
pz = pz + (fwdZ * (0.01 * frametime))
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[81] then -- down
|
||||||
|
px = px - (fwdX * (0.01 * frametime))
|
||||||
|
py = py - (fwdY * (0.01 * frametime))
|
||||||
|
pz = pz - (fwdZ * (0.01 * frametime))
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[80] then -- left
|
||||||
|
px = px - (rgtX * (0.01 * frametime))
|
||||||
|
py = py - (rgtY * (0.01 * frametime))
|
||||||
|
pz = pz - (rgtZ * (0.01 * frametime))
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[79] then -- right
|
||||||
|
px = px + (rgtX * (0.01 * frametime))
|
||||||
|
py = py + (rgtY * (0.01 * frametime))
|
||||||
|
pz = pz + (rgtZ * (0.01 * frametime))
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[75] then -- pgup
|
||||||
|
py = py - (0.01 * frametime)
|
||||||
|
end
|
||||||
|
|
||||||
|
if pressedKeys[78] then -- pgdown
|
||||||
|
py = py + (0.01 * frametime)
|
||||||
|
end
|
||||||
|
|
||||||
|
engine_color_set(255,0,255,255)
|
||||||
|
for i=0,1,0.1 do draw3dpoint(i,0,0) end
|
||||||
|
for i=0,1,0.1 do draw3dpoint(i,1,0) end
|
||||||
|
engine_color_set(64,0,64,255)
|
||||||
|
for i=0,1,0.1 do draw3dpoint(i,0,1) end
|
||||||
|
for i=0,1,0.1 do draw3dpoint(i,1,1) end
|
||||||
|
|
||||||
|
engine_color_set(255,0,255,255)
|
||||||
|
for i=0,1,0.1 do draw3dpoint(0,i,0) end
|
||||||
|
for i=0,1,0.1 do draw3dpoint(1,i,0) end
|
||||||
|
engine_color_set(64,0,64,255)
|
||||||
|
for i=0,1,0.1 do draw3dpoint(0,i,1) end
|
||||||
|
for i=0,1,0.1 do draw3dpoint(1,i,1) end
|
||||||
|
|
||||||
|
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(0,0,i) end
|
||||||
|
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(1,0,i) end
|
||||||
|
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(0,1,i) end
|
||||||
|
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(1,1,i) end
|
||||||
|
|
||||||
|
if framerate ~= nil then drawText(framerate,0,0) end
|
||||||
|
|
||||||
|
drawText(prettyNumber(px),0,16)
|
||||||
|
drawText(prettyNumber(py),0,24)
|
||||||
|
drawText(prettyNumber(pz),0,32)
|
||||||
|
|
||||||
|
drawText(prettyNumber(prx),0,48)
|
||||||
|
drawText(prettyNumber(pry),0,56)
|
||||||
|
end
|
||||||
|
|
||||||
|
lastFrame = engine_time_get()
|
||||||
|
frametime = 16
|
||||||
|
function tick()
|
||||||
|
logic()
|
||||||
|
|
||||||
|
engine_window_present(window)
|
||||||
|
trackFps()
|
||||||
|
local curFrame = engine_time_get()
|
||||||
|
frametime = (curFrame - lastFrame)
|
||||||
|
local wait = 16 - frametime
|
||||||
|
if wait > 0 then engine_time_sleep(wait) end
|
||||||
|
lastFrame = engine_time_get()
|
||||||
|
end
|
||||||
|
|
||||||
|
pressedKeys = {}
|
||||||
|
function handleEvent()
|
||||||
|
if eventData[1] == ENGINE_EVENT_TYPE_EXIT then
|
||||||
|
os.exit()
|
||||||
|
end
|
||||||
|
|
||||||
|
if eventData[1] == ENGINE_EVENT_TYPE_INPUT_KB then
|
||||||
|
if eventData[3] == 1 then
|
||||||
|
print(tostring(eventData[2]).. " pressed")
|
||||||
|
pressedKeys[eventData[2]] = true
|
||||||
|
else
|
||||||
|
print(tostring(eventData[2]).. " released")
|
||||||
|
pressedKeys[eventData[2]] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
framerate = 0
|
||||||
|
local frameSec = 0
|
||||||
|
local lastSec = 0
|
||||||
|
function trackFps()
|
||||||
|
frameSec = frameSec + 1
|
||||||
|
t = engine_time_get()
|
||||||
|
if t - lastSec >= 1000 then
|
||||||
|
framerate = frameSec
|
||||||
|
lastSec = t
|
||||||
|
frameSec = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function draw3dpoint(x,y,z)
|
||||||
|
x = px - x
|
||||||
|
y = py - y
|
||||||
|
z = pz - z
|
||||||
|
local sx,sy = engine_3d_project(x,y,z,prx,pry,0,90);
|
||||||
|
sx = math.floor((width * 0.5) + (sx * (width * 0.5)))
|
||||||
|
sy = math.floor((height * 0.5) + (sy * (height * 0.5)))
|
||||||
|
|
||||||
|
-- Clip point within bounds (debugging)
|
||||||
|
if sx < 0 then sx = 0 end
|
||||||
|
if sx > width - 1 then sx = width - 1 end
|
||||||
|
if sy < 0 then sy = 0 end
|
||||||
|
if sy > height - 1 then sy = height - 1 end
|
||||||
|
|
||||||
|
if sx < 0 or sx > width - 1 then return end
|
||||||
|
if sy < 0 or sy > height - 1 then return end
|
||||||
|
engine_rendertarget_draw_point(sx,sy)
|
||||||
|
end
|
||||||
|
|
||||||
|
chars = {}
|
||||||
|
function loadchar(fi,ch)
|
||||||
|
local tex = engine_texture_create(8,8)
|
||||||
|
engine_rendertarget_set(tex)
|
||||||
|
engine_rendertarget_draw_file(bp("texture/char/" ..fi.. ".rgba"))
|
||||||
|
chars[ch] = tex
|
||||||
|
end
|
||||||
|
|
||||||
|
function drawText(text,x,y)
|
||||||
|
local len = string.len(text)
|
||||||
|
local index = 1
|
||||||
|
while index <= len do
|
||||||
|
local char = string.sub(text,index,index)
|
||||||
|
if chars[char] == nil then char = "invalid" end
|
||||||
|
engine_rendertarget_draw_texture(chars[char],x + (8 * (index - 1)),y)
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function prettyNumber(nr)
|
||||||
|
local out = string.format("%.2f",nr)
|
||||||
|
if string.sub(out,1,1) ~= "-" then out = " " ..out end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
main()
|
@ -1,63 +1,37 @@
|
|||||||
function main()
|
|
||||||
math.randomseed(os.time())
|
|
||||||
window = engine_window_create(96,64,"Game")
|
|
||||||
texture = engine_texture_create(8,8)
|
|
||||||
engine_rendertarget_set(texture)
|
|
||||||
engine_rendertarget_draw_file(bp("texture/fier.rgba"))
|
|
||||||
engine_rendertarget_set(engine_window_texture_get(window))
|
|
||||||
|
|
||||||
while true do
|
|
||||||
event = engine_event_get()
|
|
||||||
eventData = {engine_lua_event_get_data(event)}
|
|
||||||
if eventData[1] ~= ENGINE_EVENT_TYPE_NONE then
|
|
||||||
handleEvent(event,eventData)
|
|
||||||
else
|
|
||||||
tick()
|
|
||||||
end
|
|
||||||
|
|
||||||
engine_event_free(event)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
lastFrame = 0
|
|
||||||
function tick()
|
|
||||||
engine_rendertarget_draw_texture(texture,math.random(-8,104),math.random(-8,72))
|
|
||||||
engine_window_present(window)
|
|
||||||
trackFps()
|
|
||||||
local curFrame = engine_time_get()
|
|
||||||
local wait = 16 - (curFrame - lastFrame)
|
|
||||||
if wait > 0 then engine_time_sleep(wait) end
|
|
||||||
lastFrame = engine_time_get()
|
|
||||||
end
|
|
||||||
|
|
||||||
function handleEvent(event,eventData)
|
|
||||||
if eventData[1] == ENGINE_EVENT_TYPE_EXIT then
|
|
||||||
os.exit()
|
|
||||||
end
|
|
||||||
|
|
||||||
if eventData[1] == ENGINE_EVENT_TYPE_INPUT_KB then
|
|
||||||
for _,val in pairs(eventData) do
|
|
||||||
print(tostring(val))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local frameSec = 0
|
|
||||||
local lastSec = 0
|
|
||||||
function trackFps()
|
|
||||||
frameSec = frameSec + 1
|
|
||||||
t = engine_time_get()
|
|
||||||
if t - lastSec >= 1000 then
|
|
||||||
print("FPS: " ..tostring(frameSec))
|
|
||||||
lastSec = t
|
|
||||||
frameSec = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function bp(pth)
|
|
||||||
return basepath .. "/mods/main/" ..pth
|
|
||||||
end
|
|
||||||
|
|
||||||
basepath = (debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or "./"):sub(1,-2) .. "/../../.." -- Lazy, fix
|
basepath = (debug.getinfo(1, "S").source:sub(2):match("(.*[/\\])") or "./"):sub(1,-2) .. "/../../.." -- Lazy, fix
|
||||||
package.path = basepath.. "/mods/main/script/?.lua;" ..basepath.. "/mods/main/script/?/main.lua;" ..package.path
|
|
||||||
main()
|
function main()
|
||||||
|
dofile(basepath .. "/mods/main/script/game.lua")
|
||||||
|
end
|
||||||
|
|
||||||
|
xpcall(main,function(err)
|
||||||
|
local function splitString(str,sep)
|
||||||
|
if sep == nil then
|
||||||
|
sep = "%s"
|
||||||
|
end
|
||||||
|
local t={}
|
||||||
|
for strs in string.gmatch(str, "([^"..sep.."]+)") do
|
||||||
|
table.insert(t, strs)
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
local function joinString(t,sep)
|
||||||
|
local str = t[1]
|
||||||
|
local index = 2
|
||||||
|
local length = #t
|
||||||
|
while index < length do
|
||||||
|
str = str .. sep .. t[index]
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
local debugString = splitString(debug.traceback(),"\n")
|
||||||
|
debugString[#debugString] = nil
|
||||||
|
debugString[#debugString] = nil
|
||||||
|
debugString[#debugString] = nil
|
||||||
|
table.remove(debugString,2)
|
||||||
|
print("\n--- ERROR ---\n" .. err .. "\n\n" .. joinString(debugString,"\n"))
|
||||||
|
os.exit(1)
|
||||||
|
end)
|
BIN
mods/main/texture/char/0.rgba
Normal file
BIN
mods/main/texture/char/0.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/1.rgba
Normal file
BIN
mods/main/texture/char/1.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/2.rgba
Normal file
BIN
mods/main/texture/char/2.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/3.rgba
Normal file
BIN
mods/main/texture/char/3.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/4.rgba
Normal file
BIN
mods/main/texture/char/4.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/5.rgba
Normal file
BIN
mods/main/texture/char/5.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/6.rgba
Normal file
BIN
mods/main/texture/char/6.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/7.rgba
Normal file
BIN
mods/main/texture/char/7.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/8.rgba
Normal file
BIN
mods/main/texture/char/8.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/9.rgba
Normal file
BIN
mods/main/texture/char/9.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/dot.rgba
Normal file
BIN
mods/main/texture/char/dot.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/invalid.rgba
Normal file
BIN
mods/main/texture/char/invalid.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/minus.rgba
Normal file
BIN
mods/main/texture/char/minus.rgba
Normal file
Binary file not shown.
BIN
mods/main/texture/char/space.rgba
Normal file
BIN
mods/main/texture/char/space.rgba
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user