Skip to content

Commit 941bed5

Browse files
authored
Merge pull request #17 from minigdx/full-shaders
Fix game sample
2 parents cbedb36 + 1d212bb commit 941bed5

File tree

12 files changed

+169
-190
lines changed

12 files changed

+169
-190
lines changed

tiny-doc/src/docs/asciidoc/sample/pong.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ function Ball:valid_move()
305305
return self.y > 256
306306
end
307307

308+
local player = nil
309+
308310
function _init()
309311
transition = new(GameOut)
310312

@@ -315,13 +317,10 @@ function _init()
315317
cooldown = 0
316318
}
317319

318-
dt = 1 / 60
319-
longueurCode = 100
320-
longueurSegment = 10
321-
numSegments = longueurCode / longueurSegment
322-
gravite = 29.8
323-
rigidite = 1 -- 0.8
324-
amortissement = 0.9
320+
player = new(Player)
321+
for y=216,248,8 do
322+
player:createPaddle(y)
323+
end
325324

326325
raquettes = {
327326
create_raquette(216),

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameEngine.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ class GameEngine(
506506
}
507507

508508
/**
509-
* Will render the actual operations on the screen.
509+
* Will render the actual operations in the frame buffer
510510
*/
511511
fun render() {
512512
val last = ops.lastOrNull() ?: return
@@ -531,7 +531,7 @@ class GameEngine(
531531
ops.add(op)
532532
}
533533

534-
// Render operations on the screen.
534+
// Render operations in the GPU frame buffer
535535
platform.render(renderContext, ops)
536536

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

558+
override fun readFrame(): FrameBuffer {
559+
if (renderFrame == null) {
560+
render()
561+
renderFrame = platform.readRender(renderContext)
562+
}
563+
val copy = FrameBuffer(frameBuffer.width, frameBuffer.height, frameBuffer.gamePalette)
564+
renderFrame?.copyInto(copy.colorIndexBuffer)
565+
return copy
566+
}
567+
558568
override fun end() {
559569
soundManager.destroy()
560570
}

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameResourceAccess.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ interface GameResourceAccess {
5454
y: Int,
5555
): ColorIndex
5656

57+
/**
58+
* Read the full screen
59+
*/
60+
fun readFrame(): FrameBuffer
61+
5762
/**
5863
* Access a sprite sheet by its index.
5964
*/

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/graphic/ColorPalette.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ class ColorPalette(colors: List<HexColor>) {
189189

190190
companion object {
191191
private val TRANSPARENT = byteArrayOf(0, 0, 0, 0)
192-
val TRANSPARENT_INDEX = 0
192+
const val TRANSPARENT_INDEX = 0
193193
}
194194
}

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLib.kt

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.github.mingdx.tiny.doc.TinyFunction
66
import com.github.mingdx.tiny.doc.TinyLib
77
import com.github.minigdx.tiny.engine.GameOptions
88
import com.github.minigdx.tiny.engine.GameResourceAccess
9-
import com.github.minigdx.tiny.graphic.PixelArray
109
import com.github.minigdx.tiny.render.operations.CameraOperation
1110
import com.github.minigdx.tiny.render.operations.ClipOperation
1211
import com.github.minigdx.tiny.render.operations.DitheringOperation
@@ -72,9 +71,9 @@ class GfxLib(private val resourceAccess: GameResourceAccess, private val gameOpt
7271
internal inner class pset : ThreeArgFunction() {
7372
@TinyCall("set the color index at the coordinate (x,y).")
7473
override fun call(
75-
@TinyArg("x")arg1: LuaValue,
76-
@TinyArg("y")arg2: LuaValue,
77-
@TinyArg("color")arg3: LuaValue,
74+
@TinyArg("x") arg1: LuaValue,
75+
@TinyArg("y") arg2: LuaValue,
76+
@TinyArg("color") arg3: LuaValue,
7877
): LuaValue {
7978
resourceAccess.frameBuffer.pixel(arg1.checkint(), arg2.checkint(), arg3.checkint())
8079
resourceAccess.addOp(FrameBufferOperation)
@@ -86,8 +85,8 @@ class GfxLib(private val resourceAccess: GameResourceAccess, private val gameOpt
8685
internal inner class pget : TwoArgFunction() {
8786
@TinyCall("get the color index at the coordinate (x,y).")
8887
override fun call(
89-
@TinyArg("x")arg1: LuaValue,
90-
@TinyArg("y")arg2: LuaValue,
88+
@TinyArg("x") arg1: LuaValue,
89+
@TinyArg("y") arg2: LuaValue,
9190
): LuaValue {
9291
val x = min(max(0, arg1.checkint()), gameOptions.width - 1)
9392
val y = min(max(0, arg2.checkint()), gameOptions.height - 1)
@@ -111,30 +110,25 @@ class GfxLib(private val resourceAccess: GameResourceAccess, private val gameOpt
111110
override fun call(
112111
@TinyArg("sheet") arg: LuaValue,
113112
): LuaValue {
114-
val frameBuffer = resourceAccess.frameBuffer
115-
val copy =
116-
PixelArray(frameBuffer.width, frameBuffer.height).apply {
117-
copyFrom(frameBuffer.colorIndexBuffer) { index, _, _ -> index }
118-
}
113+
val (index, name) = if (arg.isstring()) {
114+
val index = resourceAccess.spritesheet(arg.tojstring()) ?: resourceAccess.newSpritesheetIndex()
115+
index to arg.tojstring()
116+
} else {
117+
val spriteSheet = resourceAccess.spritesheet(arg.checkint())
118+
arg.toint() to (spriteSheet?.name ?: "frame_buffer_${arg.toint()}")
119+
}
120+
121+
val frameBuffer = resourceAccess.readFrame()
122+
val sheet = SpriteSheet(
123+
0,
124+
index,
125+
name,
126+
ResourceType.GAME_SPRITESHEET,
127+
frameBuffer.colorIndexBuffer,
128+
frameBuffer.width,
129+
frameBuffer.height,
130+
)
119131

120-
val (index, name) =
121-
if (arg.isstring()) {
122-
val index = resourceAccess.spritesheet(arg.tojstring()) ?: resourceAccess.newSpritesheetIndex()
123-
index to arg.tojstring()
124-
} else {
125-
val spriteSheet = resourceAccess.spritesheet(arg.checkint())
126-
arg.toint() to (spriteSheet?.name ?: "frame_buffer_${arg.toint()}")
127-
}
128-
val sheet =
129-
SpriteSheet(
130-
0,
131-
index,
132-
name,
133-
ResourceType.GAME_SPRITESHEET,
134-
copy,
135-
copy.width,
136-
copy.height,
137-
)
138132
resourceAccess.spritesheet(sheet)
139133
return arg
140134
}

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/platform/Platform.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ interface Platform {
130130
ops: List<RenderOperation>,
131131
)
132132

133+
/**
134+
* Read the full rendered screen into a frame.
135+
*/
133136
fun readRender(renderContext: RenderContext): RenderFrame
134137

135138
/**

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/render/gl/OpenGLFrame.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.github.minigdx.tiny.engine.GameOptions
77
import com.github.minigdx.tiny.graphic.PixelArray
88
import com.github.minigdx.tiny.graphic.PixelFormat
99
import com.github.minigdx.tiny.render.RenderFrame
10+
import kotlin.math.max
1011

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

3334
readBytes(buffer, tmp)
3435

3536
val colorIndex = gameOptions.colors().getColorIndex(tmp)
36-
pixelArray.set(x, (gameOptions.height - 1) - y, colorIndex)
37+
pixelArray.set(x, y, colorIndex)
3738
}
3839
}
3940

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/render/gl/OperationsShader.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class OperationsShader(
128128
// Get the straight color
129129
index
130130
}
131+
131132
val color = colors.getRGBA(pal)
132133
colorPaletteBuffer[pos++] = color[0]
133134
colorPaletteBuffer[pos++] = color[1]

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/render/operations/FrameBufferOperation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.github.minigdx.tiny.render.operations
22

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

5-
object FrameBufferOperation : RenderOperation {
5+
data object FrameBufferOperation : RenderOperation {
66
override val target: RenderUnit = RenderUnit.CPU
77

88
override fun mergeWith(previousOperation: RenderOperation?): Boolean {

tiny-sample/_tiny.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"#FF77A8",
2929
"#FFCCAA"
3030
],
31-
"sounds": ["test.mid"],
3231
"scripts": [
3332
"pong.lua"
3433
],

0 commit comments

Comments
 (0)