doc: Fix typos in GP_TempAlloc() documentation.
[gfxprim/pasky.git] / demos / py_simple / gfx.py
blob81d78b32d1b90152271ea63637cd5b552387a3b3
1 #!/usr/bin/env python
3 import sys
5 import gfxprim.core as core
6 import gfxprim.gfx as gfx
7 import gfxprim.backends as backends
8 import gfxprim.input as input
10 def fill(bk):
11 color = bk.context.RGBToPixel(0xee, 0xee, 0xee)
12 bk.context.gfx.Fill(color)
13 bk.Flip()
15 def hline(bk):
16 fg = bk.context.RGBToPixel(0xee, 0xee, 0xee)
17 bg = bk.context.RGBToPixel(0, 0, 0);
19 bk.context.gfx.Fill(bg)
20 for i in range(0, bk.context.h, 10):
21 bk.context.gfx.HLine(0, bk.context.w, i, fg)
22 bk.Flip()
24 def vline(bk):
25 fg = bk.context.RGBToPixel(0xee, 0xee, 0xee)
26 bg = bk.context.RGBToPixel(0, 0, 0);
28 bk.context.gfx.Fill(bg)
30 for i in range(0, bk.context.w, 10):
31 bk.context.gfx.VLine(i, 0, bk.context.h, fg)
33 bk.Flip()
35 def line(bk):
36 fg = bk.context.RGBToPixel(0xee, 0xee, 0xee)
37 bg = bk.context.RGBToPixel(0, 0, 0);
39 bk.context.gfx.Fill(bg)
41 for i in range(0, 2 * max(bk.context.w, bk.context.h), 13):
42 bk.context.gfx.Line(0, i, i, 0, fg)
44 bk.Flip()
46 def rect(bk):
47 fg = bk.context.RGBToPixel(0xee, 0xee, 0xee)
48 bg = bk.context.RGBToPixel(0, 0, 0);
50 bk.context.gfx.Fill(bg)
52 for i in range(10, 130, 10):
53 bk.context.gfx.Rect(i, i, bk.context.w - i, bk.context.h - i, fg)
55 bk.Flip()
57 def triangle(bk):
58 fg = bk.context.RGBToPixel(0xee, 0xee, 0xee)
59 bg = bk.context.RGBToPixel(0, 0, 0);
61 bk.context.gfx.Fill(bg)
63 w = bk.context.w
64 h = bk.context.h
66 for i in range(10, 90, 10):
67 bk.context.gfx.Triangle(2*i, i, w - 2*i, i, w//2, h - 2*i, fg)
69 bk.Flip()
71 def tetragon(bk):
72 fg = bk.context.RGBToPixel(0xee, 0xee, 0xee)
73 bg = bk.context.RGBToPixel(0, 0, 0);
75 bk.context.gfx.Fill(bg)
77 w = bk.context.w
78 h = bk.context.h
80 for i in range(10, 70, 10):
81 bk.context.gfx.Tetragon(i, i, w-2*i, i, w-i, h-i, 2*i, h-i, fg)
83 bk.Flip()
85 def polygon(bk):
86 fg = bk.context.RGBToPixel(0xee, 0xee, 0xee)
87 bg = bk.context.RGBToPixel(0, 0, 0);
89 bk.context.gfx.Fill(bg)
91 w = bk.context.w
92 h = bk.context.h
94 polygon = [(10, 10), (10, (h-10)//3), ((w-10)//3, (h-10)//2),
95 (10, 2*(h-10)//3), (10, h-10), ((w-10)//3, h-10),
96 ((w-10)//2, 2*(h-10)//3), (2*(w-10)//3, h-10),
97 (w-10, h-10), (w-10, 2*(h-10)//3), (2*(w-10)//3, (h-10)//2),
98 (w-10, (h-10)//3), (w-10, 10), (2*(w-10)//3, 10),
99 ((w-10)//2, (h-10)//3), ((w-10)//3, 10)]
101 bk.context.gfx.Polygon(polygon, fg)
103 bk.Flip()
105 def next(bk, i):
107 if (i == 0):
108 fill(bk)
110 if (i == 1):
111 hline(bk)
113 if (i == 2):
114 vline(bk)
116 if (i == 3):
117 line(bk)
119 if (i == 4):
120 rect(bk)
122 if (i == 5):
123 triangle(bk)
125 if (i == 6):
126 tetragon(bk)
128 if (i == 7):
129 polygon(bk)
131 i = i + 1;
133 if (i >= 8):
134 i = 0
136 return i
138 def main():
139 # Create X11 window
140 bk = backends.BackendX11Init(None, 0, 0, 320, 240, "GFX demo", 0)
141 assert(bk)
143 bk.Flip()
145 i = 0
147 print("Press SPACE to draw different shape, ESC to exit")
149 # Event loop
150 while True:
151 ev = bk.WaitEvent()
153 input.EventDump(ev)
155 if (ev.type == input.EV_KEY and ev.code == input.EV_KEY_DOWN):
157 if (ev.val.val == input.KEY_ESC):
158 sys.exit(0)
160 i = next(bk, i)
162 if (ev.type == input.EV_SYS):
163 if (ev.code == input.EV_SYS_QUIT):
164 sys.exit(0)
166 if __name__ == '__main__':
167 main()