]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - music_sampler.py
enhancing locks
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler.py
1 import sys
2 import pygame
3 import helpers
4 import threading
5
6 pygame.mixer.pre_init(frequency = 44100)
7 pygame.init()
8
9 size = width, height = 1024, 600
10 screen = pygame.display.set_mode(size)
11 screen.fill((250, 250, 250))
12
13 draw_lock = helpers.Lock("draw")
14
15 mapping = helpers.Mapping(screen, draw_lock)
16 helpers.parse_config(mapping)
17
18 mapping.draw()
19
20 draw_lock.acquire()
21 pygame.display.flip()
22 draw_lock.release()
23
24 contexts = [
25 'normal'
26 ]
27
28 context = 'normal'
29
30 while 1:
31 event = pygame.event.wait()
32
33 if event.type == pygame.QUIT or (
34 event.type == pygame.KEYDOWN and
35 event.mod == 4160 and
36 event.key == pygame.K_c):
37 for thread in threading.enumerate():
38 if thread is threading.current_thread():
39 continue
40 thread.join()
41
42 pygame.quit()
43 sys.exit()
44
45 if context == 'normal':
46 if event.type == pygame.KEYDOWN:
47 key = mapping.find_by_key_num(event.key)
48 if key is not None:
49 threading.Thread(target=key.do_actions).start()
50 elif event.type == pygame.MOUSEBUTTONUP:
51 key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
52 if key is not None:
53 threading.Thread(target=key.list_actions, args = [screen]).start()
54
55 draw_lock.acquire()
56 pygame.display.flip()
57 draw_lock.release()
58