|
1 | 1 | import struct
|
2 |
| -import pygame |
| 2 | +import pygame, os, sys |
3 | 3 | from pygame.locals import *
|
4 |
| -from settings import * |
| 4 | + |
5 | 5 | import moderngl
|
6 | 6 |
|
| 7 | +def resource_path(relative): |
| 8 | + if hasattr(sys, "_MEIPASS"): |
| 9 | + absolute_path = os.path.join(sys._MEIPASS, relative) |
| 10 | + else: |
| 11 | + absolute_path = os.path.join(relative) |
| 12 | + return absolute_path |
| 13 | + |
7 | 14 | class Graphic_engine:
|
8 |
| - # you need to give this class screen |
9 |
| - # screen is a Surface you declare to replace display |
10 |
| - def __init__(self, screen): |
| 15 | + def __init__(self, screen, style = 1, VIRTUAL_RES=(800, 600), cpu_only=False, fullscreen=False): |
11 | 16 | pygame.init()
|
| 17 | + self.VIRTUAL_RES = VIRTUAL_RES |
| 18 | + self.cpu_only = cpu_only |
12 | 19 | self.screen = screen
|
13 |
| - self.ctx = moderngl.create_context() |
14 |
| - self.texture_coordinates = [0, 1, 1, 1, |
15 |
| - 0, 0, 1, 0] |
16 |
| - self.world_coordinates = [-1, -1, 1, -1, |
17 |
| - -1, 1, 1, 1] |
18 |
| - self.render_indices = [0, 1, 2, |
19 |
| - 1, 2, 3] |
20 |
| - |
21 |
| - self.prog = self.ctx.program( |
22 |
| - vertex_shader=open('shaders/VERTEX_SHADER.glsl').read(), |
23 |
| - fragment_shader=open('shaders/FRAGMENT_SHADER.glsl').read(), |
24 |
| - ) |
25 |
| - |
26 |
| - self.screen_texture = self.ctx.texture( |
27 |
| - VIRTUAL_RES, 3, |
28 |
| - pygame.image.tostring(screen, "RGB", 1) |
| 20 | + self.fullscreen = fullscreen |
| 21 | + if not(self.cpu_only): |
| 22 | + self.ctx = moderngl.create_context() |
| 23 | + self.texture_coordinates = [0, 1, 1, 1, |
| 24 | + 0, 0, 1, 0] |
| 25 | + self.world_coordinates = [-1, -1, 1, -1, |
| 26 | + -1, 1, 1, 1] |
| 27 | + self.render_indices = [0, 1, 2, |
| 28 | + 1, 2, 3] |
| 29 | + |
| 30 | + self.style = style |
| 31 | + # shader style : 0, no shader. 1, crt. 2, flat_crt. |
| 32 | + self.prog = self.ctx.program( |
| 33 | + vertex_shader=open(resource_path('shaders/VERTEX_SHADER.glsl')).read(), |
| 34 | + fragment_shader=open(resource_path('shaders/FRAGMENT_SHADER.glsl')).read(), |
29 | 35 | )
|
| 36 | + self.prog['mode'] = self.style |
30 | 37 |
|
31 |
| - self.screen_texture.repeat_x = False |
32 |
| - self.screen_texture.repeat_y = False |
| 38 | + self.screen_texture = self.ctx.texture( |
| 39 | + self.VIRTUAL_RES, 3, |
| 40 | + pygame.image.tostring(screen, "RGB", 1) |
| 41 | + ) |
33 | 42 |
|
34 |
| - self.vbo = self.ctx.buffer(struct.pack('8f', *self.world_coordinates)) |
35 |
| - self.uvmap = self.ctx.buffer(struct.pack('8f', *self.texture_coordinates)) |
36 |
| - self.ibo= self.ctx.buffer(struct.pack('6I', *self.render_indices)) |
| 43 | + self.screen_texture.repeat_x = False |
| 44 | + self.screen_texture.repeat_y = False |
37 | 45 |
|
38 |
| - self.vao_content = [ |
39 |
| - (self.vbo, '2f', 'vert'), |
40 |
| - (self.uvmap, '2f', 'in_text') |
41 |
| - ] |
| 46 | + self.vbo = self.ctx.buffer(struct.pack('8f', *self.world_coordinates)) |
| 47 | + self.uvmap = self.ctx.buffer(struct.pack('8f', *self.texture_coordinates)) |
| 48 | + self.ibo= self.ctx.buffer(struct.pack('6I', *self.render_indices)) |
42 | 49 |
|
43 |
| - self.vao = self.ctx.vertex_array(self.prog, self.vao_content, self.ibo) |
| 50 | + self.vao_content = [ |
| 51 | + (self.vbo, '2f', 'vert'), |
| 52 | + (self.uvmap, '2f', 'in_text'), |
| 53 | + ] |
| 54 | + |
| 55 | + self.vao = self.ctx.vertex_array(self.prog, self.vao_content, index_buffer=self.ibo) |
| 56 | + else: |
| 57 | + self.diaplay = pygame.display.get_surface() |
| 58 | + |
| 59 | + def change_shader(self): |
| 60 | + if not self.cpu_only: |
| 61 | + self.__init__(self.screen, (self.style + 1) % 3, self.VIRTUAL_RES) |
44 | 62 |
|
45 | 63 | def render(self):
|
46 |
| - # render will do all the work and update the screen |
47 |
| - texture_data = self.screen.get_view('1') |
48 |
| - self.screen_texture.write(texture_data) |
49 |
| - self.ctx.clear(14/255,40/255,66/255) |
50 |
| - self.screen_texture.use() |
51 |
| - self.vao.render() |
52 |
| - pygame.display.flip() |
| 64 | + if not(self.cpu_only): |
| 65 | + texture_data = self.screen.get_view('1') |
| 66 | + self.screen_texture.write(texture_data) |
| 67 | + self.ctx.clear(14/255,40/255,66/255) |
| 68 | + self.screen_texture.use() |
| 69 | + self.vao.render() |
| 70 | + pygame.display.flip() |
| 71 | + else: |
| 72 | + self.diaplay.blit(self.screen, (0, 0)) |
| 73 | + pygame.display.update() |
| 74 | + |
| 75 | + def Full_screen(self, REAL_RES): |
| 76 | + if not(self.cpu_only): |
| 77 | + if not(self.fullscreen): |
| 78 | + pygame.display.set_mode(REAL_RES, pygame.DOUBLEBUF|pygame.OPENGL) |
| 79 | + else: |
| 80 | + pygame.display.set_mode(REAL_RES, pygame.DOUBLEBUF|pygame.OPENGL|pygame.FULLSCREEN) |
| 81 | + else: |
| 82 | + if not(self.fullscreen): |
| 83 | + pygame.display.set_mode(self.VIRTUAL_RES) |
| 84 | + else: |
| 85 | + pygame.display.set_mode(self.VIRTUAL_RES, pygame.FULLSCREEN) |
53 | 86 |
|
54 | 87 | def __call__(self):
|
55 | 88 | return self.render()
|
0 commit comments