Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / py_simple / gfx.py
blob0ee8dbe6517e4ffa16034c4b2909f84895324321
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.pixmap.RGBToPixel(0xee, 0xee, 0xee)
12 bk.pixmap.gfx.Fill(color)
13 bk.Flip()
15 def hline(bk):
16 fg = bk.pixmap.RGBToPixel(0xee, 0xee, 0xee)
17 bg = bk.pixmap.RGBToPixel(0, 0, 0);
19 bk.pixmap.gfx.Fill(bg)
20 for i in range(0, bk.pixmap.h, 10):
21 bk.pixmap.gfx.HLine(0, bk.pixmap.w, i, fg)
22 bk.Flip()
24 def vline(bk):
25 fg = bk.pixmap.RGBToPixel(0xee, 0xee, 0xee)
26 bg = bk.pixmap.RGBToPixel(0, 0, 0);
28 bk.pixmap.gfx.Fill(bg)
30 for i in range(0, bk.pixmap.w, 10):
31 bk.pixmap.gfx.VLine(i, 0, bk.pixmap.h, fg)
33 bk.Flip()
35 def line(bk):
36 fg = bk.pixmap.RGBToPixel(0xee, 0xee, 0xee)
37 bg = bk.pixmap.RGBToPixel(0, 0, 0);
39 bk.pixmap.gfx.Fill(bg)
41 for i in range(0, 2 * max(bk.pixmap.w, bk.pixmap.h), 13):
42 bk.pixmap.gfx.Line(0, i, i, 0, fg)
44 bk.Flip()
46 def rect(bk):
47 fg = bk.pixmap.RGBToPixel(0xee, 0xee, 0xee)
48 bg = bk.pixmap.RGBToPixel(0, 0, 0);
50 bk.pixmap.gfx.Fill(bg)
52 for i in range(10, 130, 10):
53 bk.pixmap.gfx.Rect(i, i, bk.pixmap.w - i, bk.pixmap.h - i, fg)
55 bk.Flip()
57 def triangle(bk):
58 fg = bk.pixmap.RGBToPixel(0xee, 0xee, 0xee)
59 bg = bk.pixmap.RGBToPixel(0, 0, 0);
61 bk.pixmap.gfx.Fill(bg)
63 w = bk.pixmap.w
64 h = bk.pixmap.h
66 for i in range(10, 90, 10):
67 bk.pixmap.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.pixmap.RGBToPixel(0xee, 0xee, 0xee)
73 bg = bk.pixmap.RGBToPixel(0, 0, 0);
75 bk.pixmap.gfx.Fill(bg)
77 w = bk.pixmap.w
78 h = bk.pixmap.h
80 for i in range(10, 70, 10):
81 bk.pixmap.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.pixmap.RGBToPixel(0xee, 0xee, 0xee)
87 bg = bk.pixmap.RGBToPixel(0, 0, 0);
89 bk.pixmap.gfx.Fill(bg)
91 w = bk.pixmap.w
92 h = bk.pixmap.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.pixmap.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)
165 elif (ev.code == input.EV_SYS_RESIZE):
166 bk.ResizeAck()
167 fill(bk)
168 bk.Flip()
170 if __name__ == '__main__':
171 main()