Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions tiny-doc/src/docs/asciidoc/sample/pong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ function Ball:valid_move()
return self.y > 256
end

local player = nil

function _init()
transition = new(GameOut)

Expand All @@ -315,13 +317,10 @@ function _init()
cooldown = 0
}

dt = 1 / 60
longueurCode = 100
longueurSegment = 10
numSegments = longueurCode / longueurSegment
gravite = 29.8
rigidite = 1 -- 0.8
amortissement = 0.9
player = new(Player)
for y=216,248,8 do
player:createPaddle(y)
end

raquettes = {
create_raquette(216),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ class GameEngine(
}

/**
* Will render the actual operations on the screen.
* Will render the actual operations in the frame buffer
*/
fun render() {
val last = ops.lastOrNull() ?: return
Expand All @@ -531,7 +531,7 @@ class GameEngine(
ops.add(op)
}

// Render operations on the screen.
// Render operations in the GPU frame buffer
platform.render(renderContext, ops)

// The framebuffer has been rendered.
Expand All @@ -555,6 +555,16 @@ class GameEngine(
return renderFrame?.getPixel(x, y) ?: ColorPalette.TRANSPARENT_INDEX
}

override fun readFrame(): FrameBuffer {
if (renderFrame == null) {
render()
renderFrame = platform.readRender(renderContext)
}
val copy = FrameBuffer(frameBuffer.width, frameBuffer.height, frameBuffer.gamePalette)
renderFrame?.copyInto(copy.colorIndexBuffer)
return copy
}

override fun end() {
soundManager.destroy()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ interface GameResourceAccess {
y: Int,
): ColorIndex

/**
* Read the full screen
*/
fun readFrame(): FrameBuffer

/**
* Access a sprite sheet by its index.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,6 @@ class ColorPalette(colors: List<HexColor>) {

companion object {
private val TRANSPARENT = byteArrayOf(0, 0, 0, 0)
val TRANSPARENT_INDEX = 0
const val TRANSPARENT_INDEX = 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.github.mingdx.tiny.doc.TinyFunction
import com.github.mingdx.tiny.doc.TinyLib
import com.github.minigdx.tiny.engine.GameOptions
import com.github.minigdx.tiny.engine.GameResourceAccess
import com.github.minigdx.tiny.graphic.PixelArray
import com.github.minigdx.tiny.render.operations.CameraOperation
import com.github.minigdx.tiny.render.operations.ClipOperation
import com.github.minigdx.tiny.render.operations.DitheringOperation
Expand Down Expand Up @@ -72,9 +71,9 @@ class GfxLib(private val resourceAccess: GameResourceAccess, private val gameOpt
internal inner class pset : ThreeArgFunction() {
@TinyCall("set the color index at the coordinate (x,y).")
override fun call(
@TinyArg("x")arg1: LuaValue,
@TinyArg("y")arg2: LuaValue,
@TinyArg("color")arg3: LuaValue,
@TinyArg("x") arg1: LuaValue,
@TinyArg("y") arg2: LuaValue,
@TinyArg("color") arg3: LuaValue,
): LuaValue {
resourceAccess.frameBuffer.pixel(arg1.checkint(), arg2.checkint(), arg3.checkint())
resourceAccess.addOp(FrameBufferOperation)
Expand All @@ -86,8 +85,8 @@ class GfxLib(private val resourceAccess: GameResourceAccess, private val gameOpt
internal inner class pget : TwoArgFunction() {
@TinyCall("get the color index at the coordinate (x,y).")
override fun call(
@TinyArg("x")arg1: LuaValue,
@TinyArg("y")arg2: LuaValue,
@TinyArg("x") arg1: LuaValue,
@TinyArg("y") arg2: LuaValue,
): LuaValue {
val x = min(max(0, arg1.checkint()), gameOptions.width - 1)
val y = min(max(0, arg2.checkint()), gameOptions.height - 1)
Expand All @@ -111,30 +110,25 @@ class GfxLib(private val resourceAccess: GameResourceAccess, private val gameOpt
override fun call(
@TinyArg("sheet") arg: LuaValue,
): LuaValue {
val frameBuffer = resourceAccess.frameBuffer
val copy =
PixelArray(frameBuffer.width, frameBuffer.height).apply {
copyFrom(frameBuffer.colorIndexBuffer) { index, _, _ -> index }
}
val (index, name) = if (arg.isstring()) {
val index = resourceAccess.spritesheet(arg.tojstring()) ?: resourceAccess.newSpritesheetIndex()
index to arg.tojstring()
} else {
val spriteSheet = resourceAccess.spritesheet(arg.checkint())
arg.toint() to (spriteSheet?.name ?: "frame_buffer_${arg.toint()}")
}

val frameBuffer = resourceAccess.readFrame()
val sheet = SpriteSheet(
0,
index,
name,
ResourceType.GAME_SPRITESHEET,
frameBuffer.colorIndexBuffer,
frameBuffer.width,
frameBuffer.height,
)

val (index, name) =
if (arg.isstring()) {
val index = resourceAccess.spritesheet(arg.tojstring()) ?: resourceAccess.newSpritesheetIndex()
index to arg.tojstring()
} else {
val spriteSheet = resourceAccess.spritesheet(arg.checkint())
arg.toint() to (spriteSheet?.name ?: "frame_buffer_${arg.toint()}")
}
val sheet =
SpriteSheet(
0,
index,
name,
ResourceType.GAME_SPRITESHEET,
copy,
copy.width,
copy.height,
)
resourceAccess.spritesheet(sheet)
return arg
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ interface Platform {
ops: List<RenderOperation>,
)

/**
* Read the full rendered screen into a frame.
*/
fun readRender(renderContext: RenderContext): RenderFrame

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.minigdx.tiny.engine.GameOptions
import com.github.minigdx.tiny.graphic.PixelArray
import com.github.minigdx.tiny.graphic.PixelFormat
import com.github.minigdx.tiny.render.RenderFrame
import kotlin.math.max

class OpenGLFrame(
private val buffer: ByteBuffer,
Expand All @@ -28,12 +29,12 @@ class OpenGLFrame(
// the bottom line is the first line in the buffer
for (x in 0 until gameOptions.width) {
for (y in 0 until gameOptions.height) {
buffer.position = (x + y * gameOptions.width) * PixelFormat.RGBA
buffer.position = (x + (max(0, gameOptions.height - 1 - y)) * gameOptions.width) * PixelFormat.RGBA

readBytes(buffer, tmp)

val colorIndex = gameOptions.colors().getColorIndex(tmp)
pixelArray.set(x, (gameOptions.height - 1) - y, colorIndex)
pixelArray.set(x, y, colorIndex)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class OperationsShader(
// Get the straight color
index
}

val color = colors.getRGBA(pal)
colorPaletteBuffer[pos++] = color[0]
colorPaletteBuffer[pos++] = color[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.minigdx.tiny.render.operations

import com.github.minigdx.tiny.render.RenderUnit

object FrameBufferOperation : RenderOperation {
data object FrameBufferOperation : RenderOperation {
override val target: RenderUnit = RenderUnit.CPU

override fun mergeWith(previousOperation: RenderOperation?): Boolean {
Expand Down
1 change: 0 additions & 1 deletion tiny-sample/_tiny.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"#FF77A8",
"#FFCCAA"
],
"sounds": ["test.mid"],
"scripts": [
"pong.lua"
],
Expand Down
Loading