SDL test in Nimrod
import math | |
import sdl | |
const | |
WIDTH = 1024 | |
HEIGHT = 768 | |
TIMESTEP = 1000 div 30 # 30 fps | |
THING_WIDTH = 32 | |
THING_HEIGHT = 32 | |
RADIUS = 32 | |
SPEED = 1 # per second | |
CENTER_X = WIDTH div 2 - THING_WIDTH | |
CENTER_Y = HEIGHT div 2 - THING_HEIGHT | |
var | |
screen: sdl.PSurface | |
thing = sdl.Rect(x:0, y:0, w:THING_WIDTH, h:THING_HEIGHT) | |
theta = 0.0 | |
black: int32 | |
red: int32 | |
thing.x = WIDTH div 2 - THING_WIDTH | |
thing.y = HEIGHT div 2 - THING_HEIGHT | |
thing.w = THING_WIDTH | |
thing.h = THING_HEIGHT | |
proc update(dt: float):void = | |
# update our thing | |
theta += SPEED * dt | |
thing.x = int16(CENTER_X + RADIUS * math.cos(theta)) | |
thing.y = int16(CENTER_Y + RADIUS * math.sin(theta)) | |
proc main():void = | |
var | |
event = sdl.TEvent() | |
lastTime: int32 | |
frameAccumulator: int32 | |
if sdl.Init(sdl.INIT_VIDEO) != 0: | |
quit "sdl init error" | |
screen = sdl.SetVideoMode(1024, 768, 32, 0) | |
if screen == nil: | |
quit "sdl video error" | |
# init colors | |
black = sdl.MapRGB(screen.format, 0, 0, 0) | |
red = sdl.MapRGB(screen.format, 255, 0, 0) | |
lastTime = sdl.GetTicks() | |
while true: | |
if sdl.PollEvent(addr event) != 0: | |
case event.kind | |
of sdl.QUITEV: break | |
else: discard | |
# update logic | |
let frameTime = SDL.GetTicks() | |
frameAccumulator += frameTime - lastTime # delta time | |
lastTime = frameTime | |
while frameAccumulator >= TIMESTEP: | |
update(TIMESTEP / 1000) # get timestep in seconds | |
frameAccumulator -= TIMESTEP | |
# clear screen to black | |
discard sdl.FillRect(screen, nil, black) | |
# draw our thing | |
discard sdl.FillRect(screen, addr thing, red) | |
discard sdl.Flip(screen) | |
sdl.Delay(1000 div 30) | |
# cleanup | |
sdl.Quit() | |
when isMainModule: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment