-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcamera.nim
74 lines (57 loc) · 1.45 KB
/
camera.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import nico
import nico/vec
import std/math
import std/strformat
var cx,cy = 0
var useClipping = false
proc gameInit() =
# load all our assets
loadSpriteSheet(0, "rotation.png", 16, 16)
loadMap(0, "map.json")
setMap(0)
proc gameUpdate(dt: Pfloat) =
# allow moving the camera
if btn(pcLeft):
cx -= 1
if btn(pcRight):
cx += 1
if btn(pcUp):
cy -= 1
if btn(pcDown):
cy += 1
if btnp(pcA):
useClipping = not useClipping
proc gameDraw() =
cls()
if useClipping:
# clip rectangle is not affected by camera
clip(16, 16, screenWidth - 32, screenHeight - 32)
# you can create a parallax effect with multiple camera values
setCamera(cx div 2,cy div 2)
mapdraw(0, 0, 16, 16, 0, 0)
setColor(8)
printc("this should move slowly",64, 96)
# set the camera position, future drawing operations will be offset
setCamera(cx,cy)
spr(4, 92, 92, 2, 2)
setColor(8)
pset(64,64)
setColor(9)
circ(64, 64, 9)
setColor(9)
printc("this should move",64, 32)
clip()
# disable the camera to draw HUD elements
setCamera()
setColor(7)
printr("this should not move", 126, 2)
print(&"camera: {cx:3},{cy:3}", 2, 122)
# initialization
nico.init("nico", "test")
# we want a fixed sized screen with perfect square pixels
fixedSize(true)
integerScale(true)
# create the window
nico.createWindow("nico",128,128,4)
# start, say which functions to use for init, update and draw
nico.run(gameInit, gameUpdate, gameDraw)