// Project: TileSheet and UV Designer
// Created: 2018-07-22
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "TileSheetDesigner" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// Globals and stuff
global ang#, angx#, angy#, fDiffY#
global startx#
startx#=screenwidth/2
global starty#
starty#=screenheight/2
global camerax#,cameray#,cameraz#
global cubesize
global playerspeed#=.1
Type Vertex
x as float
y as float
z as float
nx as float
ny as float
nz as float
u as float
v as float
color as integer
endtype
Type Triangle
v1 as integer
v2 as integer
v3 as integer
endtype
Type Mesh
VertexList as Vertex[]
TriangleList as Triangle[]
endtype
global meshID as Mesh
cubeimage = LoadImage("Minetile.png") // for the cube textures
addcube(5,5,0,0)
chunkmesh = CreateObjectFromMeshTest(meshID)
SetObjectImage(chunkmesh, cubeimage, 0)
Meshid.VertexList.length=-1
meshid.TriangleList.length=-1
global texts as integer[10]
texts[0]=createtext("Cursor up to increase UVs")
texts[1]=createtext("Cursor down to decrease UVs")
texts[2]=createtext("Cursor left to decrease tiles")
texts[3]=createtext("Cursor right to decrease tiles")
texts[4]=createtext("Cursor L to load minetile.png")
texts[5]=createtext("Cursor S to save the current tilesheet")
texts[6]=createtext("Cursor A to reload a saved tilesheet")
texts[7]=createtext("Cursor M to map a tilesheet to the cube")
texts[8]=createtext("Cursor N to start a fresh tilesheet")
for a=0 to 8
SetTextSize(texts[a],30)
next
type _textures
r,g,b
endtype
global textures as _textures[50]
amountoftextures=5
for a=0 to amountoftextures
textures[a].r= random(0,255)
textures[a].g= random(0,255)
textures[a].b= random(0,255)
next
// UV
// 0
// 0
// For UV rounds to whole - tilex and tiley cannot exceed 10 on each cause UV mapping starts 0 to 1 and
// so we can work it out by using 0 to 100 instead to build the tilesheet
tilesx=4
tilesy=4
tilesize=100/tilesx
scale=2
gridstartx=100
gridstarty=100
do
for a=0 to 8
SetTextposition (texts[a], 20,450+GetTextTotalHeight(texts[a])*a)
next
RotateObjectLocaly(chunkmesh,1)
if (GetImageExists(textureimage)=0)
t=0
for y=1 to tilesy * tilesize step tilesize
for x=1 to tilesx * tilesize step tilesize
DrawBox((gridstartx+x)*scale+1,(gridstarty+y)*scale+1,(gridstartx+x+tilesize)*scale-1,(gridstarty+y+tilesize)*scale-1,MakeColor(textures[t].r,textures[t].g,textures[t].b),MakeColor(textures[t].r,textures[t].g,textures[t].b),MakeColor(textures[t].r,textures[t].g,textures[t].b),MakeColor(textures[t].r,textures[t].g,textures[t].b),1)
inc t
if t>amountoftextures then t=amountoftextures
next
next
else
SetSpritePosition(texturesprite,gridstartx*scale,gridstarty*scale)
endif
for x=1 to tilesx * tilesize step tilesize
for y=1 to tilesy * tilesize step tilesize
DrawBox((gridstartx+x)*scale,(gridstarty+y)*scale,((gridstartx+x+tilesize)*scale),((gridstarty+y+tilesize)*scale),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),0)
next
next
if GetRawKeyPressed(38) // UP
inc amountoftextures
for a=0 to amountoftextures
textures[a].r= random(0,255)
textures[a].g= random(0,255)
textures[a].b= random(0,255)
next
endif
if GetRawKeyPressed(40) // DOWN
dec amountoftextures
for a=0 to amountoftextures
textures[a].r= random(0,255)
textures[a].g= random(0,255)
textures[a].b= random(0,255)
next
endif
if GetRawKeyPressed(39) // RIGHT
inc tilesx
inc tilesy
tilesize = 100/tilesx
endif
if GetRawKeyPressed(37) // LEFT
dec tilesx
dec tilesy
tilesize = 100/tilesx
endif
//Map the generated texture to the cube
if GetRawKeyPressed(77) // M for map
textureimage= LoadImage("savedtile.png")
texturesprite = createsprite(textureimage)
SetSpriteSize(texturesprite,(tilesize*tilesx)*scale,(tilesize*tilesy)*scale)
SetObjectImage(chunkmesh, textureimage,0)
endif
// Start a new texture
if GetRawKeyPressed(78) // N
for a=0 to textures.length
textures[a].r= random(0,255)
textures[a].g= random(0,255)
textures[a].b= random(0,255)
next
if GetSpriteExists(texturesprite) then DeleteSprite(texturesprite)
if GetImageExists(textureimage) then DeleteImage(textureimage)
endif
// load the generated texture back in
if GetRawKeyPressed(65) // A
textureimage= LoadImage("savedtile.png")
texturesprite = createsprite(textureimage)
SetSpriteSize(texturesprite,(tilesize*tilesx)*scale,(tilesize*tilesy)*scale)
endif
// Save the generated texture
if GetRawKeyPressed(83) // S
saveimagedata =getimage(gridstartx*scale, gridstarty*scale, (tilesize*tilesx)*scale,(tilesize*tilesy)*scale)
SaveImage(saveimagedata,"savedtile.png")
endif
if GetRawKeyPressed(76) // L
textureimage= LoadImage("minetile.png")
texturesprite = createsprite(textureimage)
SetSpriteSize(texturesprite,(tilesize*tilesx)*scale,(tilesize*tilesy)*scale)
endif
print(GetFirstFolder())
print("Tile size will be " + str(100/tilesx) + " x " + str(100/tilesy))
print("Press L to load minetile.png")
// DrawBox(0,0,64,64,MakeColor(255,255,255),MakeColor(255,0,0),MakeColor(255,0,0),MakeColor(255,0,0),1)
Print( str(ScreenFPS()) + " "+ str(GetRawLastKey()))
Sync()
loop
// Project: mesh_addcolor
// Inspried by TomToads code snippet on the forums for Triangle Strip generation into array first then build
// Created: 2018-06-22
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
// set window properties
SetWindowTitle( "mesh_addcolor" )
SetWindowSize( screenwidth, screenheight, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 1200, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
function checkkeypresses()
if GetRawKeyState(37) then dec camerax#,playerspeed#//Left
if GetRawKeyState(39) then inc camerax#,playerspeed#//Right
if GetRawKeyState(38) then inc cameraz#,playerspeed#//Forward
if GetRawKeyState(40) then dec cameraz#,playerspeed#//Backward
if GetRawKeyState(87) then inc cameray#,playerspeed#//87 W
if GetRawKeyState(83) then dec cameray#,playerspeed#//83 S
// if cameray#<(cubesize) then cameray#=(cubesize)
SetCameraPosition(1,camerax#,cameray#,cameraz#)
if GetRawKeyPressed(27) then end
endfunction
function movecamerawithmouse()
fDiffX# = (GetPointerX() - startx#)/4.0
fDiffY# = (GetPointerY() - starty#)/4.0
newX# = angx# + fDiffY#
if ( newX# > 360 ) then newX# = 360
if ( newX# < -360 ) then newX# = -360
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endfunction
function addcube(cubesize,x#,y#,z#)
// Side
// Triangle 1
AddVertex(meshID,x# ,y#,z# ,0,1,0, 1,.5,MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize,z# ,0,1,0, 1,0,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y#,z# ,0,1,0, .5,.5,MakeColor(255,0,0))
//Triangle 2
AddVertex(meshID,x#+cubesize,y# ,z# ,0,1,0, .5,.5,MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize,z# ,0,1,0, 1,0,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y#+cubesize,z# ,0,1,0, .5,0,MakeColor(255,0,0))
/*
cubedata = 0 = just the x#,y#,z#
1 = then add the cubesize to that x#,y#,z#
0,0,0
0,1,0
1,0,0
1,0,0
0,1,0
1,1,0
*/
//Side 2
// Triangle 1
AddVertex(meshID,x#+cubesize,y#+cubesize,z# ,0,1,0 ,1 ,0, MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y#+cubesize,z#+-cubesize ,0,1,0 ,.5,0,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y# ,z# ,0,1,0 ,1,.5, MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y# ,z# ,0,1,0 ,1 ,.5, MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y#+cubesize,z#+-cubesize ,0,1,0 ,.5 ,0,MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y# ,z#+-cubesize ,0,1,0 ,.5 ,.5, MakeColor(255,0,0))
/*
cubedata = 0 = just the x#,y#,z#
1 = then add the cubesize to that x#,y#,z#
1,1,0
1,1,-1 // -1 = -cubesize
1,0,0
1,0,0
1,1,-1
1,0,-1
*/
//Bottom
// Triangle 1
AddVertex(meshID,x#+cubesize,y# ,z#+-cubesize ,0,1,0 ,1,.5 , MakeColor(255,0,0))
AddVertex(meshID,x# ,y# ,z# ,0,1,0 ,.5,1 , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y# ,z# ,0,1,0 ,1,1, MakeColor(255,0,0))
AddVertex(meshID,x# ,y# ,z# ,0,1,0 ,.5,1 , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y# ,z#+-cubesize ,0,1,0 ,1,.5 , MakeColor(255,0,0))
AddVertex(meshID,x# ,y# ,z#+-cubesize ,0,1,0 ,.5 ,.5 , MakeColor(255,0,0))
/*
cubedata = 0 = just the x#,y#,z#
1 = then add the cubesize to that x#,y#,z#
1,0,-1
0,0,0 // -1 = -cubesize
1,0,0
1,0,0
1,0,-1
0,0,-1
*/
// Side 4
// Triangle 1
AddVertex(meshID,x# ,y# ,z#+-cubesize ,0,1,0 ,.5 ,.5 , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y# ,z#+-cubesize ,0,1,0 ,1 ,.5 , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize,z#+-cubesize ,0,1,0 ,.5,0 , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y#+cubesize,z#+-cubesize ,0,1,0 ,1,0 , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+cubesize,z#+-cubesize ,0,1,0 ,.5 ,0 , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y# ,z#+-cubesize ,0,1,0 ,1 ,.5 , MakeColor(255,0,0))
/*
cubedata = 0 = just the x#,y#,z#
1 = then add the cubesize to that x#,y#,z#
0,0,-1
1,0,-1 // -1 = -cubesize
0,1,-1
1,1,-1
0,1,-1
1,0,-1
*/
// side 5
AddVertex(meshID,x#,y# ,z#+-cubesize ,0,1,0 ,1 ,.5 , MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize ,z# ,0,1,0 ,.5 ,0 , MakeColor(255,0,0))
AddVertex(meshID,x#,y# ,z# ,0,1,0 ,.5,.5 , MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize ,z# ,0,1,0 ,.5 ,0 , MakeColor(255,0,0))
AddVertex(meshID,x#,y# ,z#+-cubesize ,0,1,0 ,1 , .5 , MakeColor(255,0,0))
AddVertex(meshID,x#,y#+cubesize ,z#+-cubesize ,0,1,0 ,1 , 0 , MakeColor(255,0,0))
/*
cubedata = 0 = just the x#,y#,z#
1 = then add the cubesize to that x#,y#,z#
1,1,-1
0,1,0 // -1 = -cubesize
0,0,0
0,1,0
0,0,-1
0,1,-1
*/
//TOP
// Triangle 1
AddVertex(meshID,x#+cubesize,y#+Cubesize ,z#+-cubesize ,0,1,0 ,.5,0 , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y#+Cubesize ,z# ,0,1,0 ,0,0, MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+Cubesize ,z# ,0,1,0 ,0,.5 , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+Cubesize ,z#+-cubesize ,0,1,0 ,.5 ,.5 , MakeColor(255,0,0))
AddVertex(meshID,x#+cubesize,y#+Cubesize ,z#+-cubesize ,0,1,0 ,.5,0 , MakeColor(255,0,0))
AddVertex(meshID,x# ,y#+Cubesize ,z# ,0,1,0 ,0,.5 , MakeColor(255,0,0))
/*
cubedata = 0 = just the x#,y#,z#
1 = then add the cubesize to that x#,y#,z#
1,1,-1
1,1,0 // -1 = -cubesize
0,1,0
0,1,-1
1,1,-1
0,1,0
*/
//
endfunction
Function AddVertex(m ref as Mesh, x as float, y as float, z as float, nx as float, ny as float, nz as float, u as float, v as float, color as integer)
vert as vertex
vert.x = x
vert.y = y
vert.z = z
vert.nx = nx
vert.ny = ny
vert.nz = nz
vert.u = u
vert.v = v
vert.color = color
m.VertexList.Insert(vert)
endfunction
Function AddTriangle(m ref as Mesh, v1 as integer, v2 as integer, v3 as integer)
t as Triangle
t.v1 = v1
t.v2 = v2
t.v3 = v3
m.TriangleList.Insert(t)
endfunction
Function CreateObjectFromMeshTest(m ref as mesh)
VertexCount = m.VertexList.Length + 1
IndexCount = (m.TriangleList.Length + 1) * 3
IndexOffset = 60 + VertexCount*36
memblock = CreateMemblock(IndexOffset+IndexCount*4)
SetMemblockInt(memblock,0,VertexCount)
SetMemblockInt(memblock,4,IndexCount)
SetMemblockInt(Memblock,8,3)
SetMemblockInt(memblock,12,32) // no color - 36 if color
SetmemblockInt(memblock,16,60)
SetMemblockInt(memblock,20,IndexOffset)
SetMemblockInt(memblock,24,0x0c000300)
SetMemblockString(Memblock,28,"position")
SetMemblockInt(memblock,40,0x08000300)
SetMemblockString(memblock,44,"normal")
SetMemblockInt(memblock,52,0x04000200)
SetMemblockString(memblock,56,"uv")
// SetMemblockInt(memblock,60,0x08010401)
//SetMemblockString(memblock,64,"color")
for i = 0 to m.VertexList.Length
SetMemblockFloat(memblock,60+i*32,m.VertexList[i].x)
SetMemblockFloat(memblock,64+i*32,m.VertexList[i].y)
SetMemblockFloat(memblock,68+i*32,m.VertexList[i].z)
SetMemblockFloat(memblock,72+i*32,m.VertexList[i].nx)
SetMemblockFloat(memblock,76+i*32,m.VertexList[i].ny)
SetMemblockFloat(memblock,80+i*32,m.VertexList[i].nz)
SetMemblockFloat(memblock,84+i*32,m.VertexList[i].u)
SetMemblockFloat(memblock,88+i*32,m.VertexList[i].v)
// SetMemblockInt(memblock,92+i*36,m.VertexList[i].color)
next
for i = 0 to m.TriangleList.Length
SetMemblockInt(memblock,IndexOffset+i*12,m.TriangleList[i].v1)
SetMemblockInt(memblock,IndexOffset+i*12+4,m.TriangleList[i].v2)
SetMemblockInt(memblock,IndexOffset+i*12+8,m.TriangleList[i].v3)
next
id = CreateObjectFromMeshMemblock(memblock)
DeleteMemblock(memblock)
endfunction id
Lots more to do but for the time being enjoy!