// Project: constants FPS // Created: 2020-06-03// show all errorsSetErrorMode(2)// set window propertiesSetWindowTitle( "constants FPS" )SetWindowSize( 1024, 768, 0 )SetWindowAllowResize( 1 ) // allow the user to resize the window// set display propertiesSetVirtualResolution( 1024, 768 ) // doesn't have to match the windowSetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devicesSetSyncRate( 0, 0 ) // 30fps instead of 60 to save batterySetScissor( 0,0,0,0 ) // use the maximum available screen space, no black bordersUseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts// generate a simple way to load light or heavy scene, add more cubes to push the corefor i = 1 to 1000 cubo = createobjectbox(1,1,1) setobjectcolor (cubo,random(1,254),random(2,244),random(3,200),random(100,255)) SetObjectTransparency(cubo,1) a=100 SetObjectPosition(cubo,random2(-a,a),random2(-a,a),random2(-a,a))next i// SISTEMA DE FPS LOGICOS Y DE RENDERGlobal fps_logica = 60 //;cantidad de veces que se debe repertir la logica por segundo / how many logic frame i wan per secondGlobal fps_render = 30 // ;cantidad de veces que quisieramos que se renderee. / how many render frame i wan per secondGlobal fps_start = 0 //;cuando se inicio el loop / loop millisec startGlobal fps_frame //;el numero de frames que llevamos en este segundo / count framesGlobal fps_last //;millisec en el que se inicio el ultimo fps / last millisec frameGlobal fps = 0 //;cantidad de veces que se repitio el loop principal / frame per second main loopGlobal fps_logicos = 0 //;cantidad de veces que se repitio la logica en 1 segundo / logic frames per second countGlobal fps_renders = 0 //;cantidad de veces que se rendereo en un segundo / render frames per second countGlobal fps_do_render //;indica si hay que renderear / i must render?Global fps_do_logic //;indica si hay que procesar logica / i must do the logic?Global fps_frame_render //;cantidad de veces que se rendereo en el ultimo segundo / count how many renders in last secondGlobal fps_frame_logica //;cantidad de veces que se calculo la logica en el ultimo segundo / count how many logic have in the las secondGlobal fps_last_millisec_logic //;ultimo milisegundo en el que se calculo la logica / when was the last logic millisecsGlobal fps_last_millisec_render //;ultimo milisegundo en el que se calculo el RENDER / when was the last render millisectGlobal fps_last_render_delay = 0 // ; / count how many millisecs take to renderGlobal fps_last_logic_delay = 0 //; / count how many millisecs take to do the logicGlobal fps_step_render_per_frame //;cantidad de frames que tienen que esperar para renderear / how many steps logic to do the renderGlobal fps_frame_steped //;veces que llevo haciendo la logica sin renderear print ("this render image was make in the first and only one sync of this code")sync()SetPrintSize(23)do update_fps() If fps_do_logic = 1 Then logica() // < logic of the game 60 times per second If fps_do_render = 1 Then render_my_game() // < render when you can't but no delay the logic please. update2d(0) // THIS MUST BE LIKE UPDATEWORLD() IN BLITZ3d update update update update update update update update update update update3d(0) // update update update update update update update update update update update update update update update //render2dback() //ClearDepthBuffer() //render3d() //ClearDepthBuffer() //Render2DFront() //swap() loopFunction logica() // LOGIC Local time as integer time = GetMilliseconds() fps_frame_logica = fps_frame_logica + 1 fps_last_millisec_logic = GetMilliseconds() fps_do_logic = 0 fps_frame_steped = fps_frame_steped + 1 x# = sin(GetMilliseconds()*.05)*50 y# = cos((30+GetMilliseconds()*.02))*20 z# = cos((-30+GetMilliseconds()*.01))*10 SetCameraPosition(1,x#,y#,z#) SetCameraLookAt(1,x#,y#,z#,0) UPDATE_GAME() // HERE I JUST UPDATE THE LOGIC OF THE GAME //count how much millisecs i use to do this function fps_last_logic_delay = GetMilliseconds() - time EndFunction Function render_my_game() // ; R e n d e r Local time as integer time = GetMilliseconds() fps_frame_render = fps_frame_render + 1 fps_last_millisec_render = GetMilliseconds() fps_do_render = 0 fps_frame_steped = 0 Print( "FPS LOOP : " + str(ScreenFPS()) + " << this value must be very very high!! depending of speed of computer" ) /// can be very high. usefull to know about resources optimization Print( "RENDER FPS : " + str(fps_renders) + " << this value depends on 3d load and yout graphic card and computer " ) // render fps depending on the time Print( "LOGIC FPS : " + str(fps_logicos) + " << this must be constant setting " + str(fps_logica)) // update logic allways must be in 60 times per second // RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D // RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D // RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D // RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D // RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D // RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D RENDER 3D //render() //???????? /// i don't know where put this comandsSS!!!! hellppppppp!!!!!! render2dback() ClearDepthBuffer() render3d() ClearDepthBuffer() Render2DFront() swap() fps_last_render_delay = GetMilliseconds() - time EndFunction Function update_fps() Local render_step_time Local logic_step_time Local render_millisec_step_recomended as integer render_millisec_step_recomended = 1000/60 //veces maxima que se deberia renderear por segundo. If fps_start = 0 fps_start = GetMilliseconds() EndIf If GetMilliseconds() > fps_start + 1000 fps_renders = fps_frame_render fps_frame_render = 0 fps_logicos = fps_frame_logica fps_frame_logica = 0 fps_start = GetMilliseconds() fps = fps_frame fps_frame = 0 EndIf fps_frame = fps_frame + 1 logic_step_time = 1000 / fps_logica If fps_last_millisec_logic + logic_step_time < GetMilliseconds() fps_do_logic = 1 Else fps_do_logic = 0 EndIf fps_step_render_per_frame = 5 //;here i have the problem!!! in this conditions! fps_step_render_per_frame = 1+(((fps_last_render_delay * 60)/1000))*2 If fps_step_render_per_frame < 1 Then fps_step_render_per_frame = 1 If fps_logicos < fps_logica Then fps_step_render_per_frame = fps_step_render_per_frame //;this work but dont know how to do it //;fps_step_render_per_frame = 3 If fps_logicos < fps_logica Then fps_step_render_per_frame = fps_step_render_per_frame + fps_logica-fps_logicos-1 If fps_frame_steped => fps_step_render_per_frame And fps_logicos <= fps_logica fps_do_render = 1 Else fps_do_render = 0 EndIf EndFunctionFUNCTION UPDATE_GAME() //MOVE CARS //MOVE EVERTITHING ENDFUNCTION
SetSyncRate( 0, 0 ) is better
, so that's why I said delta time is your friend
Graphics3D 1920,1080,0,2 ;1024,768,0,2SeedRnd mimillisecs()Global fps_logica = 60 ;cantidad de veces que se debe repertir la logica por segundo / how many logic frame i wan per secondGlobal fps_render = 60 ;cantidad de veces que quisieramos que se renderee. / how many render frame i wan per secondGlobal fps_start = 0 ;cuando se inicio el loop / loop millisec startGlobal fps_frame ;el numero de frames que llevamos en este segundo / count framesGlobal fps_last ;millisec en el que se inicio el ultimo fps / last millisec frameGlobal fps = 0 ;cantidad de veces que se repitio el loop principal / frame per second main loopGlobal fps_logicos = 0 ;cantidad de veces que se repitio la logica en 1 segundo / logic frames per second countGlobal fps_renders = 0 ;cantidad de veces que se rendereo en un segundo / render frames per second countGlobal fps_do_render ;indica si hay que renderear / i must render?Global fps_do_logic ;indica si hay que procesar logica / i must do the logic?Global fps_frame_render ;cantidad de veces que se rendereo en el ultimo segundo / count how many renders in last secondGlobal fps_frame_logica ;cantidad de veces que se calculo la logica en el ultimo segundo / count how many logic have in the las secondGlobal fps_last_millisec_logic ;ultimo milisegundo en el que se calculo la logica / when was the last logic millisecsGlobal fps_last_millisec_render ;ultimo milisegundo en el que se calculo el RENDER / when was the last render millisectGlobal fps_last_render_delay = 0 ; / count how many millisecs take to renderGlobal fps_last_logic_delay = 0 ; / count how many millisecs take to do the logicGlobal fps_step_render_per_frame ;cantidad de frames que tienen que esperar para renderear / how many steps logic to do the renderGlobal fps_frame_steped ;veces que llevo haciendo la logica sin renderear Global cam = CreateCamera()Global light = CreateLight()MoveEntity cam,0,0,-100Type cubo Field objEnd Type For i = 1 To 1000 c.cubo = New cubo c\obj = CreateCube() cc=100 EntityColor c\obj,Rnd(100,100+cc),Rnd(cc)*.5,Rnd(cc)*0 EntityBlend c\obj,3 a= 50 PositionEntity c\obj,Rnd(-a,a),Rnd(-a,a),Rnd(-a,a) TurnEntity c\obj,Rnd(360),Rnd(360),Rnd(360) EntityShininess c\obj,1NextGlobal render_force_delay# = Abs(Sin(mimillisecs())*100)Global vsync = 0While Not KeyHit(1) update_fps() If fps_do_logic = 1 Then logica() If fps_do_render = 1 Then render() WendEndFunction logica() Local time = mimillisecs() fps_frame_logica = fps_frame_logica + 1 fps_last_millisec_logic = mimillisecs() fps_do_logic = 0 fps_frame_steped = fps_frame_steped + 1 ;game logic update_game() UpdateWorld() ;count how much millisecs i use to do this function fps_last_logic_delay = mimillisecs() - time End FunctionFunction render() ; R e n d e r Local time = mimillisecs() fps_frame_render = fps_frame_render + 1 fps_last_millisec_render = mimillisecs() fps_do_render = 0 fps_frame_steped = 0 ;force to delay the render to simulate old computers or slow render times render_force_delay# = (Sin((mimillisecs())*.01))*20 If render_force_delay# < 0 Then render_force_delay# = 0 Delay render_force_delay# RenderWorld f=5 ; scale for graph Color 255,255,255 Text 10,10,"System fps : " + fps Color 0,255,0 Text 10,25, "Logic fps : " + fps_logicos Text 200,25, " > time : " + fps_last_logic_delay + " ms" Rect 400,25,5+fps_logicos*f,5,1 For i = 0 To 100 Step 10 Color 255,255,255 Text 400+i*f,110,i,1,1 Next Color 255,10,0 Text 10,40, "Render fps : " + fps_renders Text 200,40, " > time : " + fps_last_render_delay + " ms" Rect 400,40,5+fps_renders*f,5,1 Color 255,100,0 Rect 400,46,5+fps_last_render_delay*f,5,1 Color 255,255,0 Text 10,55,"Gui_update/s " + fps_gui_update_per_sec + " / " + fps_gui_pudates_this_sec Text 200,55, " > time : " + last_fps_last_gui_update_delay + " ms" Rect 400,55,5+fps_gui_update_per_sec*f,5,1 Color 100,155,255 Text 10,80,"render_force_delay# " + render_force_delay# +" ms" Rect 400,80,5+ render_force_delay*f,5,1 Text 10,170, "Mem V. consumida : " +((sys_memoria_inicial - AvailVidMem() ) /(1024*1024)) + " mb" Color 225,0,225 Text 10,100,"Millisecs : " + MilliSecs() Text 10,210, "VSYNC : " + vsync If KeyHit(57) Then If vsync = 0 Then vsync = 1 Else vsync = 0 EndIf Flip 0 fps_last_render_delay = mimillisecs() - time End FunctionFunction update_fps() Local render_step_time Local logic_step_time Local render_millisec_step_recomended = 1000/60 ;veces maxima que se deberia renderear por segundo. If fps_start = 0 Then fps_start = mimillisecs() End If If mimillisecs() > fps_start + 1000 Then fps_renders = fps_frame_render fps_frame_render = 0 fps_logicos = fps_frame_logica fps_frame_logica = 0 fps_start = mimillisecs() fps = fps_frame fps_frame = 0 End If fps_frame = fps_frame + 1 ; debo calcular logica en este frame? ; Do logic? logic_step_time = 1000 / fps_logica If fps_last_millisec_logic + logic_step_time < mimillisecs() Then fps_do_logic = 1 Else fps_do_logic = 0 End If ; Debo calcular render? ; Render?? fps_step_render_per_frame = 5 ;here i have the problem!!! in this conditions! fps_step_render_per_frame = 1+Int(Float((fps_last_render_delay * 60)/1000))*2 If fps_step_render_per_frame < 1 Then fps_step_render_per_frame = 1 If fps_logicos < fps_logica Then fps_step_render_per_frame = fps_step_render_per_frame ;this work but dont know how to do it ;fps_step_render_per_frame = 3 If fps_logicos < fps_logica Then fps_step_render_per_frame = fps_step_render_per_frame + fps_logica-fps_logicos-1 If fps_frame_steped => fps_step_render_per_frame And fps_logicos <= fps_logica Then fps_do_render = 1 Else fps_do_render = 0 End If End FunctionFunction update_game() For c.cubo = Each cubo TurnEntity c\obj,1,1,1 MoveEntity c\obj,0,0,.1 x# = EntityX(c\obj,1) y# = EntityY(c\obj,1) z# = EntityZ(c\obj,1) v# = .999 x# = x# * v# y# = y# * v# z# = z# * v# PositionEntity c\obj,x,y,z,1 Next End Function Function mimillisecs() Return ( MilliSecs() And $7FFFFFFF) End Function
SetVsync( 1 )Local targetFrameRate As Float, deltaTime A Float, mySprite As Integer, xPos As Float, yPos As FloattargetFrameRate = 60mySprite = < load in a super duper sprite >xPos = 0yPos = 0Do DrawSprite mySprite, xPos, yPos Inc xPos, 1.0 * deltaTime Inc yPos, 0.5 * deltaTime Swap() deltaTime = GetFrameTime() * targetFrameRateLoop
The easiest solution in AGK is to use GetFrameTime() as a multiplicator for your update values in your game logic.