11-06-2024, 04:07 PM
I checked the code I wrote a while ago and replaced the texture with one generated by code. But this truely isn't much to look at, it was just a test for creating a road from a curve (polyline library, which is full of bugs btw).
I have never made a racing game, except for a mode7 experiment. And I frankly don't know what is the best way to store a track in memory or how to render it in a good way. It probably depends on what type of racing game you're doing.
Anyhow, as I wrote earlier, the thing I wrote this spring is not good for anything. I'll see if I can think of a good way to make a racing game, either based on road segments (like above, but with heights) or a heightmap.
Sorry for not being to any help here, it's an unexplored area for me.
Code:
include "s3d.n7"
include "file.n7"
include "polyline.n7"
visible vRes = 480
set window "Futuracer", vRes*screenw()/screenh(), vRes, false
set redraw off
texture = createimage(128, 128)
set image texture
for y = 0 to 127 for x = 0 to 127
i = 16 + rnd(64)
set color i, i, i
set pixel x, y
next
set image primary
BoxBlur(texture, 2, 2)
set image texture
set color 255, 255, 255, 128
draw rect 60, 0, 8, 64, true
set image primary
BoxBlur(texture, 1, 1)
p = PolyLine([[-50, 50], [50, 50], [50, -50], [-50, -50]], true)
mesh = S3D_BeginMesh()
S3D_Begin(S3D_QUADS)
hrw = 8
stp = 15
i = 0
while i < p.GetLength()
pos = p.GetPoint(i, true)
dir = p.GetDirection(i, true)
x0 = pos[0]; y0 = pos[1]
tx0 = dir[1]; ty0 = -dir[0]
j = i + stp
if j > p.GetLength() j = 0
pos = p.GetPoint(j, true)
dir = p.GetDirection(j, true)
x1 = pos[0]; y1 = pos[1]
tx1 = dir[1]; ty1 = -dir[0]
S3D_Vertex(x0 - tx0*hrw, 0, y0 - ty0*hrw, 0, 1)
S3D_Vertex(x1 - tx1*hrw, 0, y1 - ty1*hrw, 0, 0)
S3D_Vertex(x1 + tx1*hrw, 0, y1 + ty1*hrw, 1, 0)
S3D_Vertex(x0 + tx0*hrw, 0, y0 + ty0*hrw, 1, 1)
i = i + stp
wend
S3D_End()
S3D_EndMesh()
S3D_SetView(primary, rad(74), 0.1, 100)
x = -70
z = 0
a = 0
lastTick = clock()
while not keydown(KEY_ESCAPE, true)
t = clock()
dt = (min(t - lastTick, 100))/1000
lastTick = t
if keydown(KEY_LEFT) a = a - 90*dt
if keydown(KEY_RIGHT) a = a + 90*dt
if keydown(KEY_UP)
x = x + sin(rad(a))*10*dt
z = z + cos(rad(a))*10*dt
elseif keydown(KEY_DOWN)
x = x - sin(rad(a))*10*dt
z = z - cos(rad(a))*10*dt
endif
set color 0, 0, 0
cls
S3D_Clear()
S3D_RotateY(-rad(a))
S3D_Translate(-x, 2, -z)
S3D_Texture(texture)
S3D_Mesh(mesh, 0)
S3D_Render()
S3D_RenderFog(0, 0, 0, false)
set caret width(primary) - fwidth(" "), height(primary) - fheight()
set justification right
set color 0, 255, 0
write "FPS: " + str(round(1/dt))
set justification left
redraw
wait 1
wend
' BoxBlur
' -------
function BoxBlur(img, rx, ry)
rx = max(int(rx), 0); ry = max(int(ry), 0)
set image img
w = width(img); h = height(img)
data = dim(w, h)
' Blur vertically
for y = 0 to h - 1 for x = 0 to w - 1 data[x][y] = pixeli(img, x, y)
count = ry*2 + 1
for x = 0 to w - 1
sr = 0; sg = 0; sb = 0; sa = 0
for y = -ry to ry
p = data[x][y%h];
sr = sr + Red(p); sg = sg + Green(p); sb = sb + Blue(p); sa = sa + Alpha(p)
next
for y = 0 to h - 1
set color sr/count, sg/count, sb/count, sa/count
set pixel x, y
p = data[x][(y - ry)%h]
sr = sr - Red(p); sg = sg - Green(p); sb = sb - Blue(p); sa = sa - Alpha(p)
p = data[x][(y + ry + 1)%h]
sr = sr + Red(p); sg = sg + Green(p); sb = sb + Blue(p); sa = sa + Alpha(p)
next
next
' Blur horizontally.
for y = 0 to h - 1 for x = 0 to w - 1 data[x][y] = pixeli(img, x, y)
count = rx*2 + 1
for y = 0 to h - 1
sr = 0; sg = 0; sb = 0; sa = 0
for x = -rx to rx
p = data[x%w][y]
sr = sr + Red(p); sg = sg + Green(p); sb = sb + Blue(p); sa = sa + Alpha(p)
next
for x = 0 to w - 1
set color sr/count, sg/count, sb/count, sa/count
set pixel x, y
p = data[(x - rx)%w][y]
sr = sr - Red(p); sg = sg - Green(p); sb = sb - Blue(p); sa = sa - Alpha(p)
p = data[(x + rx + 1)%w][y]
sr = sr + Red(p); sg = sg + Green(p); sb = sb + Blue(p); sa = sa + Alpha(p)
next
next
set image primary
' Pixeli helpers.
function Alpha(c); return int(c/16777216); endfunc
function Red(c); return int((c/65536))%256; endfunc
function Green(c); return int((c/256))%256; endfunc
function Blue(c); return c%256; endfunc
endfunc
I have never made a racing game, except for a mode7 experiment. And I frankly don't know what is the best way to store a track in memory or how to render it in a good way. It probably depends on what type of racing game you're doing.
Anyhow, as I wrote earlier, the thing I wrote this spring is not good for anything. I'll see if I can think of a good way to make a racing game, either based on road segments (like above, but with heights) or a heightmap.
Sorry for not being to any help here, it's an unexplored area for me.