// Project: ShaderEditorV1
// Created: 2019-03-01
// show all errors
SetErrorMode(2)
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
// set window properties
SetWindowTitle( "ShaderEditorV1" )
SetWindowSize( screenwidth, screenheight, fullscreen )
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( screenrate, 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
global title,pstitle,vstitle,obj,vscode$,pscode$,framestep#,blink,blinkcolor
global shaderid
obj = CreateObjectPlane(5,5)
RotateObjectLocalY(obj,45)
SetObjectPosition(obj,5,0,0)
SetObjectVisible(obj,1)
//title=CreateText("AppGameKit Shader Builder/Designer")
vstitle = CreateText("Vertex VS Code")
pstitle = CreateText("Pixel PS Code")
//SetTextSize(title,32)
SetTextSize(vstitle,16)
SetTextSize(pstitle,16)
type _vslines
id
text as string
endtype
global vslines as _vslines[], pslines as _vslines[]
loadvscode(1)
loadpscode(1)
blink=1
blinkcolor=MakeColor(255,255,255)
framestep#=timer()
liney=1
do
drawlayout()
displayvscode()
displaypscode()
editor(liney)
if GetRawKeyPressed(116) // run it
//saveshader("test.vs","test.ps")
applyshadertoobject(obj, "test.vs","test.ps")
active=1
endif
if GetRawKeyPressed(83)
saveshader("test.vs","test.ps")
endif
if GetRawKeyPressed(76)
loadpscode(2)
loadvscode(2)
endif
if active=1
// RotateObjectLocalY(obj,.1)
SetObjectShaderConstantByName(obj,"time",timer(),0,0,0)
endif
print(GetRawLastKey())
Render2DFront()
// Print( ScreenFPS() )
Sync()
loop
function applyshadertoobject(objid,vsfilename$,psfilename$)
shaderid = LoadShader(vsfilename$,psfilename$)
SetObjectVisible(objid,1)
SetObjectShader(objid,shaderid)
SetObjectShaderConstantByName(obj,"mouse",getpointerx(),getpointery(),0,0)
SetObjectShaderConstantByName(obj,"resolution",screenwidth,screenheight,0,0)
endfunction
function saveshader(vsfilename$,psfilename$)
fw=OpenToWrite(vsfilename$)
for a=0 to vslines.length
WriteLine(fw,vslines[a].text)
next
CloseFile(fw)
fw=OpenToWrite(psfilename$)
for a=0 to pslines.length
WriteLine(fw,pslines[a].text)
next
CloseFile(fw)
endfunction
function editor(line)
for a=32 to 128 // go through the asci table and see a key is pressed, if so - add it to the lines
if GetRawKeyPressed(a)
if a<>116 then vslines[line].text = vslines[line].text + chr(a)
endif
next
SetTextString(vslines[line].id,vslines[line].text)
updatecursor(line)
endfunction
function updatecursor(line)
DrawBox(gettextx(vslines[line].id),gettexty(vslines[line].id),gettextx(vslines[line].id)+8,gettexty(vslines[line].id)+16,blinkcolor,blinkcolor,blinkcolor,blinkcolor,1)
if (timer()-framestep#>1)
framestep#=timer()
blink=-blink
if blink=-1
blinkcolor=MakeColor(255,255,255)
else
blinkcolor=MakeColor(0,100,0)
endif
else
// DrawBox(gettextx(vslines[line].id),gettexty(vslines[line].id),gettextx(vslines[line].id)+10,gettexty(vslines[line].id)+16,MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),MakeColor(255,255,255),1)
endif
endfunction
function loadfromshaderfile(filename$)
fr=OpenToRead(filename$)
while not fileEOF(fr)
result$=result$ + ReadLine(fr) + chr(10) + chr(13)
endwhile
closefile (fr)
endfunction result$
function loadvscode(mode)
vs as _vslines
if mode=1
vscode$= GetObjectMeshVSSource(obj,1)
endif
if mode=2
vscode$= loadfromshaderfile("test.vs")
endif
line=0
vslines.length=-1
for a=1 to len(vscode$)
vs.text = vs.text + mid(vscode$,a,1)
if mid(vscode$,a,1)=chr(10)
vs.id = CreateText(vs.text)
SetTextSize(vs.id,16)
vslines.insert(vs)
inc line
vs.text=""
endif
next
endfunction
function displayvscode()
for a=0 to vslines.length
SetTextPosition(vslines[a].id,15,55+(a*GetTextTotalHeight(vslines[a].id)+2))
next
endfunction
function loadpscode(mode)
ps as _vslines
if mode=1
pscode$= GetObjectMeshPSSource(obj,1)
endif
if mode=2
pscode$= loadfromshaderfile("test.ps")
endif
line=0
for a=0 to pslines.length
DeleteText(pslines[line].id)
next
pslines.length=-1
for a=1 to len(pscode$)
ps.text = ps.text + mid(pscode$,a,1)
if mid(pscode$,a,1)=chr(10)
ps.id = CreateText(ps.text)
SetTextSize(ps.id,16)
pslines.insert(ps)
inc line
ps.text=""
endif
next
endfunction
function displaypscode()
for a=0 to pslines.length
SetTextPosition(pslines[a].id,screenwidth/2+15,55+(a*GetTextTotalHeight(pslines[a].id)+2))
next
endfunction
function drawlayout()
DrawBox(10,50,screenwidth/2-10,screenheight-10,MakeColor(0,100,0),MakeColor(0,100,0),MakeColor(0,100,0),MakeColor(0,100,0),1)
SetTextPosition(vstitle,15,30)
DrawBox(screenwidth/2+10,50,screenwidth-10,screenheight-10,MakeColor(0,100,0),MakeColor(0,100,0),MakeColor(0,100,0),MakeColor(0,100,0),1)
SetTextPosition(pstitle,screenwidth/2+15,30)
endfunction
Coming soon
Tier 1 Developer