From 0df6d919341295dfe8b51bfe0870847a585b2558 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Thu, 10 Aug 2023 02:10:09 +0200 Subject: [PATCH] Add 3D example --- mods/main/script/game.lua | 215 ++++++++++++++++++++++++++++ mods/main/script/main.lua | 98 +++++-------- mods/main/texture/char/0.rgba | Bin 0 -> 256 bytes mods/main/texture/char/1.rgba | Bin 0 -> 256 bytes mods/main/texture/char/2.rgba | Bin 0 -> 256 bytes mods/main/texture/char/3.rgba | Bin 0 -> 256 bytes mods/main/texture/char/4.rgba | Bin 0 -> 256 bytes mods/main/texture/char/5.rgba | Bin 0 -> 256 bytes mods/main/texture/char/6.rgba | Bin 0 -> 256 bytes mods/main/texture/char/7.rgba | Bin 0 -> 256 bytes mods/main/texture/char/8.rgba | Bin 0 -> 256 bytes mods/main/texture/char/9.rgba | Bin 0 -> 256 bytes mods/main/texture/char/dot.rgba | Bin 0 -> 256 bytes mods/main/texture/char/invalid.rgba | Bin 0 -> 256 bytes mods/main/texture/char/minus.rgba | Bin 0 -> 256 bytes mods/main/texture/char/space.rgba | Bin 0 -> 256 bytes 16 files changed, 251 insertions(+), 62 deletions(-) create mode 100644 mods/main/script/game.lua create mode 100644 mods/main/texture/char/0.rgba create mode 100644 mods/main/texture/char/1.rgba create mode 100644 mods/main/texture/char/2.rgba create mode 100644 mods/main/texture/char/3.rgba create mode 100644 mods/main/texture/char/4.rgba create mode 100644 mods/main/texture/char/5.rgba create mode 100644 mods/main/texture/char/6.rgba create mode 100644 mods/main/texture/char/7.rgba create mode 100644 mods/main/texture/char/8.rgba create mode 100644 mods/main/texture/char/9.rgba create mode 100644 mods/main/texture/char/dot.rgba create mode 100644 mods/main/texture/char/invalid.rgba create mode 100644 mods/main/texture/char/minus.rgba create mode 100644 mods/main/texture/char/space.rgba diff --git a/mods/main/script/game.lua b/mods/main/script/game.lua new file mode 100644 index 0000000..3fc0527 --- /dev/null +++ b/mods/main/script/game.lua @@ -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() \ No newline at end of file diff --git a/mods/main/script/main.lua b/mods/main/script/main.lua index d5dd4ee..dc3c70d 100644 --- a/mods/main/script/main.lua +++ b/mods/main/script/main.lua @@ -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 -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) \ No newline at end of file diff --git a/mods/main/texture/char/0.rgba b/mods/main/texture/char/0.rgba new file mode 100644 index 0000000000000000000000000000000000000000..fd9a0009af2d169cc06bcea8169591147fe2ab0b GIT binary patch literal 256 wcmZQzARhdO0&F@!QZR93Ha0PA@-TI{Xk%3{00kp2(EtDd literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/1.rgba b/mods/main/texture/char/1.rgba new file mode 100644 index 0000000000000000000000000000000000000000..e12800d3f169deb0efc9b9b2bcc81e7a26c20493 GIT binary patch literal 256 dcmZQzpg#By1o%uPB#%!oNRE(xLh{Jw0|2735Ags1 literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/2.rgba b/mods/main/texture/char/2.rgba new file mode 100644 index 0000000000000000000000000000000000000000..a6761dd1fcbe4cf1d0e8274eeb2dbb3db6e74b00 GIT binary patch literal 256 lcmZQzARhdO0zx`K%J8YdB@ZY&EsH3y~+W-c}wmtEM*2LOa?C)of1 literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/6.rgba b/mods/main/texture/char/6.rgba new file mode 100644 index 0000000000000000000000000000000000000000..bd9b0f48f43bdd73a170d69a600d4af942ba571f GIT binary patch literal 256 pcmZQzARhdO0&F@!QrN^{Vua*D>Y&DBH3wNgG8>!u*yM4U4*<4RFVO%1 literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/7.rgba b/mods/main/texture/char/7.rgba new file mode 100644 index 0000000000000000000000000000000000000000..e006b8169ada2f0cc4b102d2b9150725356b72d3 GIT binary patch literal 256 kcmZQzARhdO0zx`K%J8YdB@ZVPqze4$l!1;O4ONu?NfVDg*!LadUkM>kKNok8#CwhI2@TBh0Hg} literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/9.rgba b/mods/main/texture/char/9.rgba new file mode 100644 index 0000000000000000000000000000000000000000..d5203a63f86abb5547e336ed0f5fbfb162dc3fc1 GIT binary patch literal 256 tcmZQzARhdO0&F@!QZR93Ha0PA@*s5}aS(v%!9^2NgG(MH4g$#L0svFuFVO%1 literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/dot.rgba b/mods/main/texture/char/dot.rgba new file mode 100644 index 0000000000000000000000000000000000000000..eca4477f4317bf94c65ff4de2d62206d833d23bc GIT binary patch literal 256 QcmZQz7&+iS5TN@O07|w4{Qv*} literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/invalid.rgba b/mods/main/texture/char/invalid.rgba new file mode 100644 index 0000000000000000000000000000000000000000..cd84f0c983e747d7ad43207b80a4f22d0a5bde4e GIT binary patch literal 256 scmZQzpbq#C1Td43+1SLe$%E8^#6bY22bo3|M`pv+Ad4ZhVe+_W0EBBN*#H0l literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/minus.rgba b/mods/main/texture/char/minus.rgba new file mode 100644 index 0000000000000000000000000000000000000000..bebf49e1fd5dcf3433309cc187679699870ad7ae GIT binary patch literal 256 QcmZQz7(C!V6wuQ}0D-9o`2YX_ literal 0 HcmV?d00001 diff --git a/mods/main/texture/char/space.rgba b/mods/main/texture/char/space.rgba new file mode 100644 index 0000000000000000000000000000000000000000..65f57c2ee985713476ac0b6e3483e6fe472e2176 GIT binary patch literal 256 LcmZQz7})>-0RR92 literal 0 HcmV?d00001