diff options
| author | jussi | 2022-02-18 18:27:10 +0200 |
|---|---|---|
| committer | jussi | 2022-02-18 18:27:10 +0200 |
| commit | 6e4fdd3b3ae4e4656e151f098c40cfe551a36e8c (patch) | |
| tree | 37e30d371ebd44dfc8bab0d33c26f0294bda5ae4 /examples | |
| parent | 345cc1d5aa3b3c97e2cce453dc65a62c3e05427b (diff) | |
| download | reilua-enhanced-6e4fdd3b3ae4e4656e151f098c40cfe551a36e8c.tar.gz reilua-enhanced-6e4fdd3b3ae4e4656e151f098c40cfe551a36e8c.tar.bz2 reilua-enhanced-6e4fdd3b3ae4e4656e151f098c40cfe551a36e8c.zip | |
Added initial files.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/dungeon_crawler/main.lua | 147 | ||||
| -rw-r--r-- | examples/gui/main.lua | 70 | ||||
| -rw-r--r-- | examples/image_draw/main.lua | 30 | ||||
| -rw-r--r-- | examples/pixelated/main.lua | 55 | ||||
| -rw-r--r-- | examples/ray/main.lua | 47 | ||||
| -rw-r--r-- | examples/resources/images/LICENCE | 5 | ||||
| -rw-r--r-- | examples/resources/images/apple.png | bin | 0 -> 239 bytes | |||
| -rw-r--r-- | examples/resources/images/cat.png | bin | 0 -> 388467 bytes | |||
| -rw-r--r-- | examples/resources/images/grass.png | bin | 0 -> 1065 bytes | |||
| -rw-r--r-- | examples/resources/images/snake.png | bin | 0 -> 1933 bytes | |||
| -rw-r--r-- | examples/resources/images/tiles.png | bin | 0 -> 32917 bytes | |||
| -rw-r--r-- | examples/resources/shaders/glsl100/wave.fs | 36 | ||||
| -rw-r--r-- | examples/resources/shaders/glsl330/wave.fs | 37 | ||||
| -rw-r--r-- | examples/shaders/main.lua | 60 | ||||
| -rw-r--r-- | examples/snake/main.lua | 219 |
15 files changed, 706 insertions, 0 deletions
diff --git a/examples/dungeon_crawler/main.lua b/examples/dungeon_crawler/main.lua new file mode 100644 index 0000000..979b553 --- /dev/null +++ b/examples/dungeon_crawler/main.lua @@ -0,0 +1,147 @@ +local pos = { 2, 0.5, 6 } +local speed = 5.0 +local camera = -1 +local texture = -1 +local mesh = -1 +local textureSize = { 128, 128 } +local res = { 384, 216 } +local winSize = RL_GetWindowSize() +local winScale = 5 +local framebuffer = -1 + +local TILE_SIZE = 32 +local COLOR_WHITE = { 255, 255, 255 } + +local FLOOR = 1 +local CEILING = 2 +local WALL_N = 3 +local WALL_S = 4 +local WALL_W = 5 +local WALL_E = 6 + +local sprites = { + { pos = { 0.5, 0.5 }, tile = { 0, 1 }, dis = 0, size = 0.7 }, + { pos = { 3.5, 0.5 }, tile = { 0, 1 }, dis = 0, size = 0.7 }, +} + +local function getTexCoords( x, y ) + return { + { x * TILE_SIZE / textureSize[1], y * TILE_SIZE / textureSize[2] }, + { x * TILE_SIZE / textureSize[1], ( y * TILE_SIZE + TILE_SIZE ) / textureSize[2] }, + { ( x * TILE_SIZE + TILE_SIZE ) / textureSize[1], ( y * TILE_SIZE + TILE_SIZE ) / textureSize[2] }, + { ( x * TILE_SIZE + TILE_SIZE ) / textureSize[1], y * TILE_SIZE / textureSize[2] }, + } +end + +local function getTileVer( x, y, type ) + local types = { + { { 0, 0, 0 }, { 0, 0, 1 }, { 1, 0, 1 }, { 1, 0, 0 } }, -- Floor. + { { 1, 1, 0 }, { 1, 1, 1 }, { 0, 1, 1 }, { 0, 1, 0 } }, -- Ceiling. + { { 0, 1, 0 }, { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 } }, -- Wall North. + { { 1, 1, 1 }, { 1, 0, 1 }, { 0, 0, 1 }, { 0, 1, 1 } }, -- Wall South. + { { 0, 1, 1 }, { 0, 0, 1 }, { 0, 0, 0 }, { 0, 1, 0 } }, -- Wall West. + { { 1, 1, 0 }, { 1, 0, 0 }, { 1, 0, 1 }, { 1, 1, 1 } }, -- Wall East. + } + local verts = types[ type ] + + for i = 1, 4 do + verts[i][1] = verts[i][1] + x + verts[i][3] = verts[i][3] + y + end + + return verts +end + +function drawSprites() + for _, sprite in ipairs( sprites ) do + sprite.dis = RL_Vector2Distance( { pos[1], pos[3] }, { sprite.pos[1], sprite.pos[2] } ) + end + + table.sort( sprites, function( a, b ) return a.dis > b.dis end ) + + for _, sprite in ipairs( sprites ) do + RL_DrawBillboardRec( camera, texture, { sprite.tile[1] * TILE_SIZE, sprite.tile[2] * TILE_SIZE, TILE_SIZE, TILE_SIZE }, + { sprite.pos[1], 0.5 * sprite.size, sprite.pos[2] }, { sprite.size, sprite.size }, COLOR_WHITE ) + end +end + +function init() + local monitor = 0 + local mPos = RL_GetMonitorPosition( monitor ) + local mSize = RL_GetMonitorSize( monitor ) + -- RL_SetWindowSize( { 1920, 1080 } ) + winSize = { res[1] * winScale, res[2] * winScale } + -- winSize = { 1920, 1080 } + RL_SetWindowSize( winSize ) + RL_SetExitKey( KEY_ESCAPE ) + -- framebuffer = RL_LoadRenderTexture( res ) + RL_SetWindowState( FLAG_WINDOW_RESIZABLE ) + RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) + + texture = RL_LoadTexture( RL_GetBasePath().."../resources/images/tiles.png" ) + camera = RL_CreateCamera3D() + mesh = RL_GenMeshCube( { 1, 2, 1 } ) + RL_SetCamera3DPosition( camera, pos ) + RL_SetCamera3DTarget( camera, { 0, 0, 0 } ) + RL_SetCamera3DUp( camera, { 0, 1, 0 } ) + RL_SetCamera3DMode( camera, CAMERA_FIRST_PERSON ) + -- RL_SetCamera3DMode( camera, CAMERA_ORBITAL ) + + -- for x = 0, 3 do + -- for y = 0, 9 do + -- table.insert( sprites, { pos = { x + 0.5, y + 0.5 + 1 }, tile = { 1, 1 }, dis = 0, size = 0.8 } ) + -- end + -- end + table.insert( sprites, { pos = { 2.5, 2.5 }, tile = { 1, 1 }, dis = 0, size = 0.8 } ) + + -- for x = 0, 1 do + -- for y = 0, 1 do + -- table.insert( sprites, { pos = { 1.25 + x * 0.5, 2.25 + y * 0.5 }, tile = { 3, 0 }, dis = 0, size = 0.6 } ) + -- end + -- end + table.insert( sprites, { pos = { 1.5, 3.5 }, tile = { 3, 1 }, dis = 0, size = 0.5 } ) + table.insert( sprites, { pos = { 0.5, 3.5 }, tile = { 3, 0 }, dis = 0, size = 0.7 } ) +end + +function process( delta ) + -- RL_SetCamera3DPosition( camera, pos ) +end + +function draw() + RL_UpdateCamera3D( camera ) + pos = RL_GetCamera3DPosition( camera ) + + -- RL_BeginTextureMode( framebuffer ) + RL_ClearBackground( { 100, 150, 150 } ) + + RL_BeginMode3D( camera ) + + -- Floor and ceiling. + for x = 0, 3 do + for y = 0, 10 do + RL_DrawQuad3DTexture( texture, getTileVer( x, y, FLOOR ), getTexCoords( 1, 0 ), COLOR_WHITE ) + RL_DrawQuad3DTexture( texture, getTileVer( x, y, CEILING ), getTexCoords( 2, 0 ), COLOR_WHITE ) + end + end + -- Walls. + RL_DrawQuad3DTexture( texture, getTileVer( 0, 0, WALL_N ), getTexCoords( 0, 0 ), COLOR_WHITE ) + RL_DrawQuad3DTexture( texture, getTileVer( 1, 0, WALL_N ), getTexCoords( 0, 2 ), COLOR_WHITE ) + RL_DrawQuad3DTexture( texture, getTileVer( 2, 0, WALL_N ), getTexCoords( 2, 2 ), COLOR_WHITE ) + RL_DrawQuad3DTexture( texture, getTileVer( 3, 0, WALL_N ), getTexCoords( 0, 0 ), COLOR_WHITE ) + + for x = 0, 3 do + RL_DrawQuad3DTexture( texture, getTileVer( x, 10, WALL_S ), getTexCoords( 0, 0 ), COLOR_WHITE ) + end + for y = 0, 10 do + RL_DrawQuad3DTexture( texture, getTileVer( 0, y, WALL_W ), getTexCoords( 0, 0 ), COLOR_WHITE ) + RL_DrawQuad3DTexture( texture, getTileVer( 3, y, WALL_E ), getTexCoords( 0, 0 ), COLOR_WHITE ) + end + + drawSprites() + RL_EndMode3D() + -- RL_EndTextureMode() + + -- RL_SetTextureSource( TEXTURE_SOURCE_RENDER_TEXTURE ) + -- RL_DrawTexturePro( framebuffer, { 0, 0, res[1], -res[2] }, { 0, 0, winSize[1], winSize[2] }, { 0, 0 }, 0.0, COLOR_WHITE ) + -- RL_SetTextureSource( TEXTURE_SOURCE_TEXTURE ) +end diff --git a/examples/gui/main.lua b/examples/gui/main.lua new file mode 100644 index 0000000..079bd95 --- /dev/null +++ b/examples/gui/main.lua @@ -0,0 +1,70 @@ +local windowOpen = true +local toggled = false +local checkbox = false +local textBoxText = "Edit" +local textBoxActive = false +local spinnerValue = 3 +local spinnerActive = false +local spinnerValueRange = { 0, 10 } +local sliderValue = 5.0 +local sliderValueRange = { 0.0, 10.0 } +local scrollbarValue = 0.0 +local dropdownValue = 0 +local dropdownActive = false + +function init() + local monitor = 0 + local mPos = RL_GetMonitorPosition( monitor ) + local mSize = RL_GetMonitorSize( monitor ) + local winSize = RL_GetWindowSize() + + RL_GuiSetFont( 0 ) + RL_SetWindowState( FLAG_WINDOW_RESIZABLE ) + RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) +end + +function process( delta ) +end + +function draw() + RL_ClearBackground( { 50, 20, 75 } ) + + if RL_GuiButton( { 112, 16, 96, 32 }, "Button" ) then + print( "Button pressed!" ) + end + + if windowOpen and RL_GuiWindowBox( { 300, 16, 200, 320 }, "Window" ) then + windowOpen = false + end + + RL_GuiPanel( { 60, 260, 100, 100 } ) + + toggled = RL_GuiToggle( { 200, 260, 64, 32 }, "Toggle", toggled ) + checkbox = RL_GuiCheckBox( { 200, 300, 16, 16 }, "CheckBox", checkbox ) + + local textBoxToggle = false + textBoxToggle, textBoxText = RL_GuiTextBox( { 32, 400, 120, 32 }, textBoxText, 32, textBoxActive ) + -- textBoxToggle, textBoxText = RL_GuiTextBoxMulti( { 32, 400, 120, 64 }, textBoxText, 120, textBoxActive ) + + if textBoxToggle then + textBoxActive = not textBoxActive + end + + local spinnerToggle = false + spinnerToggle, spinnerValue = RL_GuiSpinner( { 64, 450, 96, 32 }, "Value", spinnerValue, spinnerValueRange[1], spinnerValueRange[2], spinnerActive ) + -- spinnerToggle, spinnerValue = RL_GuiValueBox( { 64, 450, 96, 32 }, "Value", spinnerValue, spinnerValueRange[1], spinnerValueRange[2], spinnerActive ) + + if spinnerToggle then + spinnerActive = not spinnerActive + end + + sliderValue = RL_GuiSliderBar( { 64, 510, 96, 32 }, "min", "max", sliderValue, sliderValueRange[1], sliderValueRange[2] ) + scrollbarValue = RL_GuiScrollBar( { 64, 550, 130, 32 }, scrollbarValue, 0, 10 ) + + local dropdownToggle = false + dropdownToggle, dropdownValue = RL_GuiDropdownBox( { 2, 2, 96, 16 }, "Cat\nDog\nMonkey", dropdownValue, dropdownActive ) + + if dropdownToggle then + dropdownActive = not dropdownActive + end +end diff --git a/examples/image_draw/main.lua b/examples/image_draw/main.lua new file mode 100644 index 0000000..8855fce --- /dev/null +++ b/examples/image_draw/main.lua @@ -0,0 +1,30 @@ +local monitor = 0 +local texture = -1 +local image = -1 +local catImage = -1 + +function init() + local mPos = RL_GetMonitorPosition( monitor ) + local mSize = RL_GetMonitorSize( monitor ) + local winSize = RL_GetWindowSize() + + RL_SetWindowState( FLAG_WINDOW_RESIZABLE ) + RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) + image = RL_GenImageColor( winSize[1], winSize[2], WHITE ) + catImage = RL_LoadImage( RL_GetBasePath().."../resources/images/cat.png" ) + RL_ImageClearBackground( image, { 150, 60, 100 } ) + RL_ImageDrawPixel( image, { 32, 32 }, WHITE ) + RL_ImageDrawLine( image, { 32, 45 }, { 100, 60 }, GREEN ) + RL_ImageDrawCircle( image, { 64, 32 }, 16, BLUE ) + RL_ImageDrawRectangle( image, { 120, 64, 32, 64 }, BLUE ) + RL_ImageDrawRectangleLines( image, { 160, 64, 32, 64 }, 2.0, BLUE ) + RL_ImageDraw( image, catImage, { 143, 25, 230, 250 }, { 200, 200, 230, 250 }, WHITE ) + RL_ImageDrawTextEx( image, 0, "Hello", { 300, 32 }, 48.0, 1.0, WHITE ) + + texture = RL_LoadTextureFromImage( image ) +end + +function draw() + RL_ClearBackground( { 100, 150, 100 } ) + RL_DrawTexture( texture, { 0, 0 }, WHITE ) +end diff --git a/examples/pixelated/main.lua b/examples/pixelated/main.lua new file mode 100644 index 0000000..c0de666 --- /dev/null +++ b/examples/pixelated/main.lua @@ -0,0 +1,55 @@ +local tex = -1 +local pos = { 32, 32 } +local speed = 60.0 +local sound = -1 +local monitor = 0 +local mPos = RL_GetMonitorPosition( monitor ) +local mSize = RL_GetMonitorSize( monitor ) +local framebuffer = -1 +local res = { 320, 180 } +local scale = 5 +local winSize = { res[1] * scale, res[2] * scale } + +function init() + RL_SetWindowState( FLAG_WINDOW_RESIZABLE ) + RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) + RL_SetWindowSize( winSize ) + tex = RL_LoadTexture( RL_GetBasePath().."../resources/images/cat.png" ) + -- Create framebuffer. + framebuffer = RL_LoadRenderTexture( res ) +end + +function process( delta ) + if RL_IsKeyDown( KEY_RIGHT ) then + pos[1] = pos[1] + delta * speed + elseif RL_IsKeyDown( KEY_LEFT ) then + pos[1] = pos[1] - delta * speed + end + + if RL_IsKeyDown( KEY_UP ) then + pos[2] = pos[2] - delta * speed + elseif RL_IsKeyDown( KEY_DOWN ) then + pos[2] = pos[2] + delta * speed + end + + if RL_IsWindowResized() then + winSize = RL_GetWindowSize() + end +end + +function draw() + RL_ClearBackground( { 0, 0, 0 } ) + + RL_BeginTextureMode( framebuffer ) + RL_ClearBackground( { 100, 150, 100 } ) + RL_DrawPixel( { 100, 100 }, { 255, 50, 100 } ) + RL_DrawLine( { 120, 100 }, { 140, 150 }, 2.4, { 255, 150, 255 } ) + RL_DrawRectangle( { 200, 120, 40, 50 }, { 100, 170, 255 } ) + RL_DrawTexturePro( tex, { 166, 138, 128, 128 }, { pos[1], pos[2], 128, 128 }, { 16, 16 }, 0.0, WHITE ) + RL_DrawText( 0, "Cat MIAU!!", { 16, 32 }, 10, 1, { 255, 180, 155 } ) + RL_EndTextureMode() + + RL_SetTextureSource( TEXTURE_SOURCE_RENDER_TEXTURE ) + RL_DrawTexturePro( framebuffer, { 0, 0, res[1], -res[2] }, { 0, 0, winSize[1], winSize[2] }, { 0, 0 }, 0.0, { 255, 255, 255 } ) + RL_SetTextureSource( TEXTURE_SOURCE_TEXTURE ) +end diff --git a/examples/ray/main.lua b/examples/ray/main.lua new file mode 100644 index 0000000..f0f5c2f --- /dev/null +++ b/examples/ray/main.lua @@ -0,0 +1,47 @@ +local camera = -1 +local sphereMesh = -1 +local ray = { { 0.5, 0, 4 }, { 0.1, 0, -1 } } + +local function setupWindow() + local monitor = 0 + local mPos = RL_GetMonitorPosition( monitor ) + local mSize = RL_GetMonitorSize( monitor ) + local winSize = RL_GetWindowSize() + + RL_SetWindowState( FLAG_WINDOW_RESIZABLE ) + RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) +end + +function init() + setupWindow() + + camera = RL_CreateCamera3D() + RL_SetCamera3DPosition( camera, { 0, 2, 4 } ) + RL_SetCamera3DTarget( camera, { 0, 0, 0 } ) + RL_SetCamera3DUp( camera, { 0, 2, 0 } ) + RL_SetCamera3DMode( camera, CAMERA_FREE ) + + sphereMesh = RL_GenMeshSphere( 1.0, 8, 10 ) + + -- local rayCol = RL_GetRayCollisionSphere( { { 0.5, 0, 4 }, { 0, 0, -1 } }, { 0, 0, 0 }, 1.0 ) + local rayCol = RL_GetRayCollisionMesh( ray, sphereMesh, RL_MatrixIdentity() ) + + if rayCol ~= nil and rayCol.hit then + print( "hit", rayCol.hit ) + print( "distance", rayCol.distance ) + print( "point", rayCol.point[1], rayCol.point[2], rayCol.point[3] ) + print( "normal", rayCol.normal[1], rayCol.normal[2], rayCol.normal[3] ) + end +end + +function draw() + RL_ClearBackground( { 100, 150, 100 } ) + RL_UpdateCamera3D( camera ) + + RL_BeginMode3D( camera ) + RL_DrawGrid( 8, 1 ) + RL_DrawRay( ray, { 255, 100, 100 } ) + + RL_DrawMesh( sphereMesh, 0, RL_MatrixIdentity() ) + RL_EndMode3D() +end diff --git a/examples/resources/images/LICENCE b/examples/resources/images/LICENCE new file mode 100644 index 0000000..fca0d39 --- /dev/null +++ b/examples/resources/images/LICENCE @@ -0,0 +1,5 @@ +Resource Author Licence Source +tiles.png Chris Hamons (maintainer) CC0 https://opengameart.org/content/dungeon-crawl-32x32-tiles +apple.png Jussi Viitala CC0 +grass.png Jussi Viitala CC0 +snake.png Jussi Viitala CC0
\ No newline at end of file diff --git a/examples/resources/images/apple.png b/examples/resources/images/apple.png Binary files differnew file mode 100644 index 0000000..9526aa6 --- /dev/null +++ b/examples/resources/images/apple.png diff --git a/examples/resources/images/cat.png b/examples/resources/images/cat.png Binary files differnew file mode 100644 index 0000000..db56b9e --- /dev/null +++ b/examples/resources/images/cat.png diff --git a/examples/resources/images/grass.png b/examples/resources/images/grass.png Binary files differnew file mode 100644 index 0000000..4ba7263 --- /dev/null +++ b/examples/resources/images/grass.png diff --git a/examples/resources/images/snake.png b/examples/resources/images/snake.png Binary files differnew file mode 100644 index 0000000..e1b0091 --- /dev/null +++ b/examples/resources/images/snake.png diff --git a/examples/resources/images/tiles.png b/examples/resources/images/tiles.png Binary files differnew file mode 100644 index 0000000..4e3e9cc --- /dev/null +++ b/examples/resources/images/tiles.png diff --git a/examples/resources/shaders/glsl100/wave.fs b/examples/resources/shaders/glsl100/wave.fs new file mode 100644 index 0000000..50c4e02 --- /dev/null +++ b/examples/resources/shaders/glsl100/wave.fs @@ -0,0 +1,36 @@ +#version 100 + +precision mediump float; + +// Input vertex attributes (from vertex shader) +varying vec2 fragTexCoord; +varying vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +uniform float secondes; + +uniform vec2 size; + +uniform float freqX; +uniform float freqY; +uniform float ampX; +uniform float ampY; +uniform float speedX; +uniform float speedY; + +void main() { + float pixelWidth = 1.0 / size.x; + float pixelHeight = 1.0 / size.y; + float aspect = pixelHeight / pixelWidth; + float boxLeft = 0.0; + float boxTop = 0.0; + + vec2 p = fragTexCoord; + p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + + gl_FragColor = texture2D(texture0, p)*colDiffuse*fragColor; +} diff --git a/examples/resources/shaders/glsl330/wave.fs b/examples/resources/shaders/glsl330/wave.fs new file mode 100644 index 0000000..43efee2 --- /dev/null +++ b/examples/resources/shaders/glsl330/wave.fs @@ -0,0 +1,37 @@ +#version 330 + +// Input vertex attributes (from vertex shader) +in vec2 fragTexCoord; +in vec4 fragColor; + +// Input uniform values +uniform sampler2D texture0; +uniform vec4 colDiffuse; + +// Output fragment color +out vec4 finalColor; + +uniform float secondes; + +uniform vec2 size; + +uniform float freqX; +uniform float freqY; +uniform float ampX; +uniform float ampY; +uniform float speedX; +uniform float speedY; + +void main() { + float pixelWidth = 1.0 / size.x; + float pixelHeight = 1.0 / size.y; + float aspect = pixelHeight / pixelWidth; + float boxLeft = 0.0; + float boxTop = 0.0; + + vec2 p = fragTexCoord; + p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + + finalColor = texture(texture0, p)*colDiffuse*fragColor; +} diff --git a/examples/shaders/main.lua b/examples/shaders/main.lua new file mode 100644 index 0000000..eea9344 --- /dev/null +++ b/examples/shaders/main.lua @@ -0,0 +1,60 @@ +local monitor = 0 +local shader = -1 +local texture = -1 +local textureSize + +local GLSL_VERSION = "330" -- PLATFORM_DESKTOP +-- local GLSL_VERSION = "100" -- PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB + +local secondsLoc + +function init() + local mPos = RL_GetMonitorPosition( monitor ) + local mSize = RL_GetMonitorSize( monitor ) + local winSize = RL_GetWindowSize() + + RL_SetWindowState( FLAG_WINDOW_RESIZABLE ) + RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) + + texture = RL_LoadTexture( RL_GetBasePath().."../resources/images/cat.png" ) + textureSize = RL_GetTextureSize( texture ) + shader = RL_LoadShader( nil, RL_GetBasePath().."../resources/shaders/glsl"..GLSL_VERSION.."/wave.fs" ) + + secondsLoc = RL_GetShaderLocation( shader, "secondes" ) + local sizeLoc = RL_GetShaderLocation( shader, "size" ) + local freqXLoc = RL_GetShaderLocation( shader, "freqX" ) + local freqYLoc = RL_GetShaderLocation( shader, "freqY" ) + local ampXLoc = RL_GetShaderLocation( shader, "ampX" ) + local ampYLoc = RL_GetShaderLocation( shader, "ampY" ) + local speedXLoc = RL_GetShaderLocation( shader, "speedX" ) + local speedYLoc = RL_GetShaderLocation( shader, "speedY" ) + + local freqX = 25.0 + local freqY = 25.0 + local ampX = 5.0 + local ampY = 5.0 + local speedX = 8.0 + local speedY = 8.0 + + RL_SetShaderValue( shader, sizeLoc, textureSize, SHADER_UNIFORM_VEC2 ) + RL_SetShaderValue( shader, freqXLoc, { freqX }, SHADER_UNIFORM_FLOAT ) + RL_SetShaderValue( shader, freqYLoc, { freqY }, SHADER_UNIFORM_FLOAT ) + RL_SetShaderValue( shader, ampXLoc, { ampX }, SHADER_UNIFORM_FLOAT ) + RL_SetShaderValue( shader, ampYLoc, { ampY }, SHADER_UNIFORM_FLOAT ) + RL_SetShaderValue( shader, speedXLoc, { speedX }, SHADER_UNIFORM_FLOAT ) + RL_SetShaderValue( shader, speedYLoc, { speedY }, SHADER_UNIFORM_FLOAT ) +end + +local seconds = 0.0 + +function draw() + seconds = seconds + RL_GetFrameTime(); + + RL_SetShaderValue( shader, secondsLoc, { seconds }, SHADER_UNIFORM_FLOAT ); + + RL_ClearBackground( { 100, 150, 100 } ) + + RL_BeginShaderMode( shader ) + RL_DrawTexture( texture, { 0, 0 }, WHITE ); + RL_EndShaderMode() +end diff --git a/examples/snake/main.lua b/examples/snake/main.lua new file mode 100644 index 0000000..74b7191 --- /dev/null +++ b/examples/snake/main.lua @@ -0,0 +1,219 @@ +-- Defines +local RESOLUTION = { 128, 128 } +local TILE_SIZE = 8 +local LEVEL_SIZE = RESOLUTION[1] / TILE_SIZE +local STATE = { TITLE = 0, GAME = 1, OVER = 2 } -- Enum wannabe. + +-- Resources +local framebuffer = -1 +local monitor = 0 +local monitorPos = RL_GetMonitorPosition( monitor ) +local monitorSize = RL_GetMonitorSize( monitor ) +local winScale = 6 +local winSize = { RESOLUTION[1] * winScale, RESOLUTION[2] * winScale } +local gameState = STATE.GAME +local grassTexture = -1 +local snakeTexture = -1 +local appleTexture = -1 +local gameSpeed = 7.0 +local moveTimer = 1.0 +local snake = {} +local applePos = {} + +local function setSnake() + snake = { + heading = { 1, 0 }, + control = { 1, 0 }, + headPos = { LEVEL_SIZE / 2, LEVEL_SIZE / 2 }, + segments = {}, + grow = 2, + } +end + +local function vector2IsEqual( v1, v2 ) + return v1[1] == v2[1] and v1[2] == v2[2] +end + +local function addSegment() + -- If first segment, grow from head and otherwise from tail. New segments are inserted firts. + if #snake.segments == 0 then + table.insert( snake.segments, 1, { pos = snake.headPos, heading = snake.heading } ) + else + table.insert( snake.segments, 1, { pos = snake.segments[ #snake.segments ].pos, + heading = snake.segments[ #snake.segments ].heading } ) + end +end + +local function setApplePos() + applePos = { math.random( 0, LEVEL_SIZE - 1 ), math.random( 0, LEVEL_SIZE - 1 ) } + local search = true + + while search do + search = false + applePos = { math.random( 0, LEVEL_SIZE - 1 ), math.random( 0, LEVEL_SIZE - 1 ) } + + for _, seg in ipairs( snake.segments ) do + search = vector2IsEqual( applePos, seg.pos ) + + if search then + break + end + end + end +end + +-- Init. + +function init() + RL_SetWindowState( FLAG_WINDOW_RESIZABLE ) + RL_SetWindowSize( winSize ) + RL_SetWindowPosition( { monitorPos[1] + monitorSize[1] / 2 - winSize[1] / 2, monitorPos[2] + monitorSize[2] / 2 - winSize[2] / 2 } ) + RL_SetWindowTitle( "Snake" ) + RL_SetWindowIcon( RL_LoadImage( RL_GetBasePath().."../resources/images/apple.png" ) ) + + framebuffer = RL_LoadRenderTexture( RESOLUTION ) + grassTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/grass.png" ) + snakeTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/snake.png" ) + appleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/apple.png" ) + + setSnake() + setApplePos() +end + +-- Process. + +local function moveSnake() + -- Check if snake has eaten and should grow. + if 0 < snake.grow then + addSegment() + snake.grow = snake.grow - 1 + end + -- Move body. + for i, seg in ipairs( snake.segments ) do + if i < #snake.segments then + seg.pos = snake.segments[ i+1 ].pos + seg.heading = snake.segments[ i+1 ].heading + else + seg.pos = snake.headPos + seg.heading = snake.heading + end + end + -- Move head. + snake.heading = { snake.control[1], snake.control[2] } + snake.headPos = { snake.headPos[1] + snake.heading[1], snake.headPos[2] + snake.heading[2] } + + -- Check appple eating. + if vector2IsEqual( snake.headPos, applePos ) then + snake.grow = snake.grow + 1 + setApplePos() + end + -- Check if hit to body. + for _, seg in ipairs( snake.segments ) do + if vector2IsEqual( snake.headPos, seg.pos ) then + gameState = STATE.OVER + end + end + -- Check if outside or level. + if snake.headPos[1] < 0 or LEVEL_SIZE <= snake.headPos[1] or snake.headPos[2] < 0 or LEVEL_SIZE <= snake.headPos[2] then + gameState = STATE.OVER + end + + moveTimer = moveTimer + 1.0 +end + +function process( delta ) + if gameState == STATE.GAME then -- Run game. + -- Controls. + if RL_IsKeyPressed( KEY_RIGHT ) and 0 <= snake.heading[1] then + snake.control = { 1, 0 } + elseif RL_IsKeyPressed( KEY_LEFT ) and snake.heading[1] <= 0 then + snake.control = { -1, 0 } + elseif RL_IsKeyPressed( KEY_DOWN ) and 0 <= snake.heading[2] then + snake.control = { 0, 1 } + elseif RL_IsKeyPressed( KEY_UP ) and snake.heading[2] <= 0 then + snake.control = { 0, -1 } + end + + moveTimer = moveTimer - gameSpeed * delta + + if moveTimer <= 0.0 then + moveSnake() + end + elseif gameState == STATE.OVER and RL_IsKeyPressed( KEY_ENTER ) then -- Reset game. + setSnake() + setApplePos() + gameState = STATE.GAME + end +end + +-- Drawing. + +local function drawGrass() + for y = 0, LEVEL_SIZE - 1 do + for x = 0, LEVEL_SIZE - 1 do + RL_DrawTexture( grassTexture, { x * TILE_SIZE, y * TILE_SIZE }, WHITE ) + end + end +end + +--[[ Check if next segment is on left side. There are more mathematically elegant solution to this, but there is +only four possibilities so we can just check them all. ]]-- +local function onLeft( this, next ) + return ( vector2IsEqual( this, { 0, -1 } ) and vector2IsEqual( next, { -1, 0 } ) ) + or ( vector2IsEqual( this, { -1, 0 } ) and vector2IsEqual( next, { 0, 1 } ) ) + or ( vector2IsEqual( this, { 0, 1 } ) and vector2IsEqual( next, { 1, 0 } ) ) + or ( vector2IsEqual( this, { 1, 0 } ) and vector2IsEqual( next, { 0, -1 } ) ) +end + +local function drawSnake() + for i, seg in ipairs( snake.segments ) do + local angle = math.deg( RL_Vector2Angle( { 0, 0 }, seg.heading ) ) + local source = { 16, 0, 8, 8 } + + if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment. + source[1] = 8 + + if 1 < #snake.segments then + angle = math.deg( RL_Vector2Angle( { 0, 0 }, snake.segments[ 2 ].heading ) ) + end + elseif i < #snake.segments and not vector2IsEqual( seg.heading, snake.segments[ i+1 ].heading ) then -- Turned middle segments. + source[1] = 0 + -- Mirror turned segment to other way. + if onLeft( seg.heading, snake.segments[ i+1 ].heading ) then + source[4] = -8 + end + elseif i == #snake.segments and not vector2IsEqual( seg.heading, snake.heading ) then -- Turned segment before head. + source[1] = 0 + + if onLeft( seg.heading, snake.heading ) then + source[4] = -8 + end + end + -- Notice that we set the origin to center { 4, 4 } that acts as pivot point. We also have to adjust our dest position by 4. + RL_DrawTexturePro( snakeTexture, source, { seg.pos[1] * TILE_SIZE + 4, seg.pos[2] * TILE_SIZE + 4, 8, 8 }, { 4, 4 }, angle, WHITE ) + end + -- Let's draw the head last to keep it on top. + local angle = math.deg( RL_Vector2Angle( { 0, 0 }, snake.heading ) ) + RL_DrawTexturePro( snakeTexture, { 24, 0, 8, 8 }, { snake.headPos[1] * TILE_SIZE + 4, snake.headPos[2] * TILE_SIZE + 4, 8, 8 }, { 4, 4 }, angle, WHITE ) +end + +local function drawApple() + RL_DrawTexture( appleTexture, { applePos[1] * TILE_SIZE, applePos[2] * TILE_SIZE }, WHITE ) +end + +function draw() + -- Clear the window to black. + RL_ClearBackground( BLACK ) + -- Draw to framebuffer. + RL_BeginTextureMode( framebuffer ) + RL_ClearBackground( BLACK ) + drawGrass() + drawSnake() + drawApple() + RL_EndTextureMode() + + -- Draw framebuffer to window. + RL_SetTextureSource( TEXTURE_SOURCE_RENDER_TEXTURE ) + RL_DrawTexturePro( framebuffer, { 0, 0, RESOLUTION[1], -RESOLUTION[2] }, { 0, 0, winSize[1], winSize[2] }, { 0, 0 }, 0.0, WHITE ) + RL_SetTextureSource( TEXTURE_SOURCE_TEXTURE ) +end |
