Fast tilemap example.
This commit is contained in:
@@ -43,6 +43,7 @@ DETAILED CHANGES:
|
|||||||
- FIXED: Added rlDisableShader for SetShaderValue* functions to prevent bugs.
|
- FIXED: Added rlDisableShader for SetShaderValue* functions to prevent bugs.
|
||||||
Should be removed if added back in raylib.
|
Should be removed if added back in raylib.
|
||||||
- ADDED: SDL3 Events.
|
- ADDED: SDL3 Events.
|
||||||
|
- ADDED: Fast tilemap example.
|
||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0
|
Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ function RL.init()
|
|||||||
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
|
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
|
||||||
|
|
||||||
RL.SetTextLineSpacing( 24 )
|
RL.SetTextLineSpacing( 24 )
|
||||||
|
|
||||||
-- RL.EnableEventWaiting()
|
|
||||||
-- RL.DisableEventWaiting()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getEventType( event )
|
local function getEventType( event )
|
||||||
@@ -70,54 +67,54 @@ end
|
|||||||
local mousePos = { 0, 0 }
|
local mousePos = { 0, 0 }
|
||||||
local cursorMode = 1
|
local cursorMode = 1
|
||||||
|
|
||||||
local pen = {
|
-- local pen = {
|
||||||
state = RL.SDL_EVENT_PEN_UP,
|
-- state = RL.SDL_EVENT_PEN_UP,
|
||||||
pos = { 0, 0 },
|
-- pos = { 0, 0 },
|
||||||
pressure = 0,
|
-- pressure = 0,
|
||||||
down = false,
|
-- down = false,
|
||||||
eraser = false,
|
-- eraser = false,
|
||||||
}
|
-- }
|
||||||
|
|
||||||
function RL.event( event )
|
function RL.event( event )
|
||||||
-- text = "Event: "..getEventType( event ).."\n"
|
text = "Event: "..getEventType( event ).."\n"
|
||||||
|
|
||||||
-- if event.type == RL.GLFW_WINDOW_SIZE_EVENT then
|
if event.type == RL.GLFW_WINDOW_SIZE_EVENT then
|
||||||
-- text = text.."width: "..event.width.." height: "..event.height
|
text = text.."width: "..event.width.." height: "..event.height
|
||||||
-- elseif event.type == RL.GLFW_WINDOW_MAXIMIZE_EVENT then
|
elseif event.type == RL.GLFW_WINDOW_MAXIMIZE_EVENT then
|
||||||
-- text = text.."maximized: "..event.maximized
|
text = text.."maximized: "..event.maximized
|
||||||
-- elseif event.type == RL.GLFW_WINDOW_ICONYFY_EVENT then
|
elseif event.type == RL.GLFW_WINDOW_ICONYFY_EVENT then
|
||||||
-- text = text.."iconified: "..event.iconified
|
text = text.."iconified: "..event.iconified
|
||||||
-- elseif event.type == RL.GLFW_WINDOW_FOCUS_EVENT then
|
elseif event.type == RL.GLFW_WINDOW_FOCUS_EVENT then
|
||||||
-- text = text.."focused: "..event.focused
|
text = text.."focused: "..event.focused
|
||||||
-- elseif event.type == RL.GLFW_WINDOW_DROP_EVENT then
|
elseif event.type == RL.GLFW_WINDOW_DROP_EVENT then
|
||||||
-- text = text.."count: "..event.count.."\n"
|
text = text.."count: "..event.count.."\n"
|
||||||
-- for _, path in ipairs( event.paths ) do
|
for _, path in ipairs( event.paths ) do
|
||||||
-- text = text..path.."\n"
|
text = text..path.."\n"
|
||||||
-- end
|
end
|
||||||
-- elseif event.type == RL.GLFW_KEY_EVENT then
|
elseif event.type == RL.GLFW_KEY_EVENT then
|
||||||
-- text = text.."key: "..event.key.." scancode: "..event.scancode.." action: "..getAction( event.action ).." mods: "..event.mods
|
text = text.."key: "..event.key.." scancode: "..event.scancode.." action: "..getAction( event.action ).." mods: "..event.mods
|
||||||
-- text = text .."\nkeyName: "..keyName( event.key )
|
text = text .."\nkeyName: "..keyName( event.key )
|
||||||
-- elseif event.type == RL.GLFW_CHAR_EVENT then
|
elseif event.type == RL.GLFW_CHAR_EVENT then
|
||||||
-- text = text.."key: "..event.key
|
text = text.."key: "..event.key
|
||||||
-- -- text = text .."\nchar: "..string.char( event.key )
|
-- text = text .."\nchar: "..string.char( event.key )
|
||||||
-- text = text .."\nchar: "..utf8.char( event.key )
|
text = text .."\nchar: "..utf8.char( event.key )
|
||||||
-- elseif event.type == RL.GLFW_MOUSE_BUTTON_EVENT then
|
elseif event.type == RL.GLFW_MOUSE_BUTTON_EVENT then
|
||||||
-- text = text.."button: "..event.button.." action: "..getAction( event.action ).." mods: "..event.mods
|
text = text.."button: "..event.button.." action: "..getAction( event.action ).." mods: "..event.mods
|
||||||
-- elseif event.type == RL.GLFW_MOUSE_CURSOR_POS_EVENT then
|
elseif event.type == RL.GLFW_MOUSE_CURSOR_POS_EVENT then
|
||||||
-- text = text.."x: "..event.x.." y: "..event.y
|
text = text.."x: "..event.x.." y: "..event.y
|
||||||
-- elseif event.type == RL.GLFW_MOUSE_SCROLL_EVENT then
|
elseif event.type == RL.GLFW_MOUSE_SCROLL_EVENT then
|
||||||
-- text = text.."xoffset: "..event.xoffset.." yoffset: "..event.yoffset
|
text = text.."xoffset: "..event.xoffset.." yoffset: "..event.yoffset
|
||||||
-- elseif event.type == RL.GLFW_CURSOR_ENTER_EVENT then
|
elseif event.type == RL.GLFW_CURSOR_ENTER_EVENT then
|
||||||
-- text = text.."enter: "..event.enter
|
text = text.."enter: "..event.enter
|
||||||
-- cursorIn = event.enter
|
cursorIn = event.enter
|
||||||
-- elseif event.type == RL.EVENT_JOYSTICK then
|
elseif event.type == RL.EVENT_JOYSTICK then
|
||||||
-- text = text.."jid: "..event.jid.." event: "..event.event
|
text = text.."jid: "..event.jid.." event: "..event.event
|
||||||
-- if event.event == RL.GLFW_CONNECTED then
|
if event.event == RL.GLFW_CONNECTED then
|
||||||
-- text = text.."\nConnected"
|
text = text.."\nConnected"
|
||||||
-- elseif event.event == RL.GLFW_DISCONNECTED then
|
elseif event.event == RL.GLFW_DISCONNECTED then
|
||||||
-- text = text.."\nDisconnected"
|
text = text.."\nDisconnected"
|
||||||
-- end
|
end
|
||||||
-- end
|
end
|
||||||
|
|
||||||
-- Some SDL events.
|
-- Some SDL events.
|
||||||
|
|
||||||
@@ -162,43 +159,43 @@ function RL.event( event )
|
|||||||
|
|
||||||
-- Some SDL3 events.
|
-- Some SDL3 events.
|
||||||
|
|
||||||
text = event.type.."\n\n"
|
-- text = event.type.."\n\n"
|
||||||
|
|
||||||
if event.type == RL.SDL_EVENT_KEY_DOWN or event.type == RL.SDL_EVENT_KEY_UP then
|
-- if event.type == RL.SDL_EVENT_KEY_DOWN or event.type == RL.SDL_EVENT_KEY_UP then
|
||||||
text = text.."key: "..event.key.." repeat: "..tostring( event.repeating )
|
-- text = text.."key: "..event.key.." repeat: "..tostring( event.repeating )
|
||||||
elseif event.type == RL.SDL_EVENT_PEN_AXIS then
|
-- elseif event.type == RL.SDL_EVENT_PEN_AXIS then
|
||||||
text = text.."pen_state: "..event.pen_state.." axis: "..event.axis.." value: "..event.value
|
-- text = text.."pen_state: "..event.pen_state.." axis: "..event.axis.." value: "..event.value
|
||||||
pen.pressure = event.value
|
-- pen.pressure = event.value
|
||||||
pen.state = event.pen_state
|
-- pen.state = event.pen_state
|
||||||
pen.pos[1] = event.x
|
-- pen.pos[1] = event.x
|
||||||
pen.pos[2] = event.y
|
-- pen.pos[2] = event.y
|
||||||
elseif event.type == RL.SDL_EVENT_PEN_MOTION then
|
-- elseif event.type == RL.SDL_EVENT_PEN_MOTION then
|
||||||
text = text.."pen_state: "..event.pen_state.." pos: "..event.x..", "..event.y
|
-- text = text.."pen_state: "..event.pen_state.." pos: "..event.x..", "..event.y
|
||||||
pen.pos[1] = event.x
|
-- pen.pos[1] = event.x
|
||||||
pen.pos[2] = event.y
|
-- pen.pos[2] = event.y
|
||||||
elseif event.type == RL.SDL_EVENT_PEN_DOWN or event.type == RL.SDL_EVENT_PEN_UP then
|
-- elseif event.type == RL.SDL_EVENT_PEN_DOWN or event.type == RL.SDL_EVENT_PEN_UP then
|
||||||
pen.down = event.down
|
-- pen.down = event.down
|
||||||
pen.eraser = event.eraser
|
-- pen.eraser = event.eraser
|
||||||
elseif event.type == RL.SDL_EVENT_CLIPBOARD_UPDATE then
|
-- elseif event.type == RL.SDL_EVENT_CLIPBOARD_UPDATE then
|
||||||
print( "SDL_EVENT_CLIPBOARD_UPDATE:" )
|
-- print( "SDL_EVENT_CLIPBOARD_UPDATE:" )
|
||||||
for i, t in ipairs( event.mime_types ) do
|
-- for i, t in ipairs( event.mime_types ) do
|
||||||
print( i, t )
|
-- print( i, t )
|
||||||
end
|
-- end
|
||||||
elseif event.type == RL.SDL_EVENT_GAMEPAD_AXIS_MOTION then
|
-- elseif event.type == RL.SDL_EVENT_GAMEPAD_AXIS_MOTION then
|
||||||
text = text.."axis: "..event.axis.."value: "..event.value
|
-- text = text.."axis: "..event.axis.."value: "..event.value
|
||||||
print( "axis: "..event.axis.." value: "..event.value )
|
-- print( "axis: "..event.axis.." value: "..event.value )
|
||||||
elseif event.type == RL.SDL_EVENT_GAMEPAD_ADDED then
|
-- elseif event.type == RL.SDL_EVENT_GAMEPAD_ADDED then
|
||||||
print( "SDL_EVENT_GAMEPAD_ADDED" )
|
-- print( "SDL_EVENT_GAMEPAD_ADDED" )
|
||||||
end
|
-- end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function drawSDL3PenCircle()
|
-- local function drawSDL3PenCircle()
|
||||||
RL.DrawCircleLines( pen.pos, 32, RL.GREEN )
|
-- RL.DrawCircleLines( pen.pos, 32, RL.GREEN )
|
||||||
|
|
||||||
if pen.down then
|
-- if pen.down then
|
||||||
RL.DrawCircle( pen.pos, pen.pressure * 32, pen.eraser and RL.YELLOW or RL.BLUE )
|
-- RL.DrawCircle( pen.pos, pen.pressure * 32, pen.eraser and RL.YELLOW or RL.BLUE )
|
||||||
end
|
-- end
|
||||||
end
|
-- end
|
||||||
|
|
||||||
function RL.draw()
|
function RL.draw()
|
||||||
if 0 < cursorIn then
|
if 0 < cursorIn then
|
||||||
@@ -207,7 +204,7 @@ function RL.draw()
|
|||||||
RL.ClearBackground( RL.RED )
|
RL.ClearBackground( RL.RED )
|
||||||
end
|
end
|
||||||
|
|
||||||
drawSDL3PenCircle()
|
-- drawSDL3PenCircle()
|
||||||
|
|
||||||
RL.DrawText( text, textPos, 20, RL.BLACK )
|
RL.DrawText( text, textPos, 20, RL.BLACK )
|
||||||
end
|
end
|
||||||
|
|||||||
103
examples/fast_tilemap/main.lua
Normal file
103
examples/fast_tilemap/main.lua
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
--[[
|
||||||
|
We can use mesh to draw tilemap in one draw call.
|
||||||
|
On large maps you could devide the tilemap in sections to cull tiles that are not
|
||||||
|
in view.
|
||||||
|
--]]
|
||||||
|
|
||||||
|
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
|
||||||
|
|
||||||
|
Util = require( "utillib" )
|
||||||
|
Vector2 = require( "vector2" )
|
||||||
|
Vector3 = require( "vector3" )
|
||||||
|
|
||||||
|
QUAD = {
|
||||||
|
VERTICES = {
|
||||||
|
Vector3:new( 0, 0, 0 ), Vector3:new( 0, 1, 0 ), Vector3:new( 1, 1, 0 ),
|
||||||
|
Vector3:new( 0, 0, 0 ), Vector3:new( 1, 1, 0 ), Vector3:new( 1, 0, 0 )
|
||||||
|
},
|
||||||
|
TEXCOORDS = {
|
||||||
|
Vector2:new( 0, 0 ), Vector2:new( 0, 1 ), Vector2:new( 1, 1 ),
|
||||||
|
Vector2:new( 0, 0 ), Vector2:new( 1, 1 ), Vector2:new( 1, 0 )
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
local res = Vector2:new( 1024, 720 )
|
||||||
|
local winScale = 1
|
||||||
|
local winSize = res:scale( winScale )
|
||||||
|
local monitor = 0
|
||||||
|
|
||||||
|
local tilemap = {
|
||||||
|
texture = nil,
|
||||||
|
texSize = Vector2:new(),
|
||||||
|
mesh = nil,
|
||||||
|
material = nil,
|
||||||
|
tileSize = 32,
|
||||||
|
tilecount = 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
local function setTile( meshData, pos, texcoord )
|
||||||
|
local texelSize = Vector2:new( 1 / tilemap.texSize.x, 1 / tilemap.texSize.y )
|
||||||
|
|
||||||
|
for i, v in ipairs( QUAD.VERTICES ) do
|
||||||
|
table.insert( meshData.vertices, ( pos + v ):scale( tilemap.tileSize ) )
|
||||||
|
table.insert( meshData.texcoords, ( QUAD.TEXCOORDS[i] + texcoord ) * texelSize:scale( tilemap.tileSize ) )
|
||||||
|
table.insert( meshData.colors, RL.WHITE )
|
||||||
|
end
|
||||||
|
|
||||||
|
tilemap.tilecount = tilemap.tilecount + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local function genTilemap()
|
||||||
|
local meshData = { vertices = {}, texcoords = {}, colors = {} }
|
||||||
|
-- Ground.
|
||||||
|
for x = 0, winSize.x / tilemap.tileSize do
|
||||||
|
for y = 0, winSize.y / tilemap.tileSize do
|
||||||
|
setTile( meshData, Vector2:new( x, y ), Vector2:new( 4, 0 ) )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- House. Will be overdrawn to ground but we don't care we are so efficient! (You should though.)
|
||||||
|
for x = 0, 6 do
|
||||||
|
for y = 0, 10 do
|
||||||
|
local tilePos = Vector2:new( x, y ) + Vector2:new( 8, 6 )
|
||||||
|
|
||||||
|
if ( x == 0 or x == 6 or y == 0 or y == 10 ) and y ~= 4 then
|
||||||
|
setTile( meshData, tilePos, Vector2:new( 0, 0 ) )
|
||||||
|
else
|
||||||
|
setTile( meshData, tilePos, Vector2:new( 1, 0 ) )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Characters. You probably would not do this, but we do it here to get some variation.
|
||||||
|
setTile( meshData, Vector2:new( 4, 3 ), Vector2:new( 3, 0 ) )
|
||||||
|
setTile( meshData, Vector2:new( 8, 4 ), Vector2:new( 3, 1 ) )
|
||||||
|
setTile( meshData, Vector2:new( 10, 8 ), Vector2:new( 1, 1 ) )
|
||||||
|
|
||||||
|
tilemap.mesh = RL.GenMeshCustom( meshData, false )
|
||||||
|
end
|
||||||
|
|
||||||
|
function RL.init()
|
||||||
|
local monitorPos = Vector2:newT( RL.GetMonitorPosition( monitor ) )
|
||||||
|
local monitorSize = Vector2:newT( RL.GetMonitorSize( monitor ) )
|
||||||
|
|
||||||
|
RL.SetWindowTitle( "Fast tilemap" )
|
||||||
|
RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
|
||||||
|
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
|
||||||
|
RL.SetWindowSize( winSize )
|
||||||
|
RL.SetWindowPosition( { monitorPos.x + monitorSize.x / 2 - winSize.x / 2, monitorPos.y + monitorSize.y / 2 - winSize.y / 2 } )
|
||||||
|
|
||||||
|
local path = RL.GetBasePath().."../resources/images/tiles.png"
|
||||||
|
|
||||||
|
tilemap.texture = RL.LoadTexture( path )
|
||||||
|
tilemap.texSize:setT( RL.GetTextureSize( tilemap.texture ) )
|
||||||
|
|
||||||
|
tilemap.material = RL.LoadMaterialDefault()
|
||||||
|
RL.SetMaterialTexture( tilemap.material, RL.MATERIAL_MAP_ALBEDO, tilemap.texture )
|
||||||
|
|
||||||
|
genTilemap()
|
||||||
|
end
|
||||||
|
|
||||||
|
function RL.draw()
|
||||||
|
RL.ClearBackground( RL.BLACK )
|
||||||
|
RL.DrawMesh( tilemap.mesh, tilemap.material, RL.MatrixIdentity() )
|
||||||
|
RL.DrawText( "Tile count: "..tostring( tilemap.tilecount ), { 3, 3 }, 20, RL.GREEN )
|
||||||
|
end
|
||||||
@@ -166,6 +166,8 @@ static void platformSendEvents() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lua_pop( L, -1 );
|
||||||
|
|
||||||
for ( int i = 0; i < state->SDL_eventQueueLen; i++ ) {
|
for ( int i = 0; i < state->SDL_eventQueueLen; i++ ) {
|
||||||
bool call = false;
|
bool call = false;
|
||||||
lua_getglobal( L, "RL" );
|
lua_getglobal( L, "RL" );
|
||||||
|
|||||||
Reference in New Issue
Block a user