-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
140 lines (110 loc) · 5.45 KB
/
render.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pygame
import math,os
class Defs:
colorMax = 255
colorMin = 0
class RenderDesc:
winWidth = 800
winHeight = 600
class Camera:
x = 0
y = 0
class Render:
renderTarget = 0
rtWidthHalf = 0
rtHeightHalf = 0
camera = 0
affection = 1
matId = 0
materials = []
color = (Defs.colorMax, Defs.colorMax, Defs.colorMax)
def __init__(self, Width,Height,FullSc):
pygame.display.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
if(FullSc==False):
self.rtWidthHalf = Width / 2
self.rtHeightHalf = Height / 2
self.renderTarget = pygame.display.set_mode((Width, Height))
self.camera = Camera
else:
#screen_width,screen_height = info.current_w,info.current_h
self.rtWidthHalf = Width / 2
self.rtHeightHalf = Height / 2
self.renderTarget = pygame.display.set_mode((Width, Height),pygame.FULLSCREEN)
self.camera = Camera
def loadMaterial(self, filename):
img = pygame.image.load(filename)
img = img.convert()
self.materials.append(img)
return len(self.materials) - 1
def drawRectangle(self, x, y, hw, hh, useMat = False, ang = 0):
if useMat:
self.__renderMat(x, y, hw, hh, ang)
else:
self.__renderRect(x, y, hw, hh)
def drawRect(self,vert):
xc = (vert[0][0]+vert[1][0]+vert[2][0]+vert[3][0])/4
yc = (vert[0][1]+vert[1][1]+vert[2][1]+vert[3][1])/4
width = xc - vert[0][0]
height = yc - vert[0][1]
self.__renderRect(xc,-yc,width,height)
def drawTriangle(self, x1, y1, x2, y2, x3, y3):
pygame.draw.polygon(self.renderTarget, self.color,
[(x1 + self.rtWidthHalf - self.camera.x*self.affection,
y1 + self.rtHeightHalf - self.camera.y*self.affection),
(x2 + self.rtWidthHalf - self.camera.x*self.affection,
y2 + self.rtHeightHalf - self.camera.y*self.affection),
(x3 + self.rtWidthHalf - self.camera.x*self.affection,
y3 + self.rtHeightHalf - self.camera.y*self.affection)])
def drawLine(self, x1, y1, x2, y2):
pygame.draw.line(self.renderTarget, self.color, (int(x1 + self.rtWidthHalf - self.camera.x*self.affection),
int(y1 + self.rtHeightHalf - self.camera.y*self.affection)),
(int(x2 + self.rtWidthHalf - self.camera.x*self.affection),
int(y2 + self.rtHeightHalf - self.camera.y*self.affection)))
def drawPolygon(self, x1, y1, x2, y2, x3, y3, x4, y4):
pygame.draw.polygon(self.renderTarget, self.color,
[(x1 + self.rtWidthHalf - self.camera.x*self.affection,
-y1 + self.rtHeightHalf - self.camera.y*self.affection),
(x2 + self.rtWidthHalf - self.camera.x*self.affection,
-y2 + self.rtHeightHalf - self.camera.y*self.affection),
(x3 + self.rtWidthHalf - self.camera.x*self.affection,
-y3 + self.rtHeightHalf - self.camera.y*self.affection),
(x4 + self.rtWidthHalf - self.camera.x*self.affection,
-y4 + self.rtHeightHalf - self.camera.y*self.affection)])
def get_render_target(self):
return self.renderTarget
def drawCircle(self, x, y, r):
pygame.draw.circle(self.renderTarget, self.color,
(int(x + self.rtWidthHalf - self.camera.x*self.affection),
int(y + self.rtHeightHalf - self.camera.y*self.affection)), r)
def setCameraAffection(self, affect):
assert (affect >= 0 and affect <= 1), "Camera affection must be in range [0 ... 1]"
self.affection = affect
def setCameraPos(self, x, y):
self.camera.x = int(x)
self.camera.y = int(y)
def setMaterial(self, mat):
self.matId = mat
def setColor(self, r, g, b):
assert (r >= 0 and g >= 0 and b >= 0), "Color component must be >= 0"
r = min(Defs.colorMax, r)
g = min(Defs.colorMax, g)
b = min(Defs.colorMax, b)
self.color = (r, g, b)
def draw(self,running):
pygame.display.flip()
if(running==True):
self.renderTarget.fill((255,255,255))
else:
self.renderTarget.fill((223,73,60))
#pygame.event.get()
def __renderMat(self, x, y, hw, hh, ang):
self.renderTarget.blit(pygame.transform.rotate(self.materials[self.matId], ang),
(x - hw + self.rtWidthHalf - self.camera.x*self.affection,
y - hh + self.rtHeightHalf - self.camera.y*self.affection, hw*2, hh*2))
def __renderRect(self, x, y, hw, hh):
pygame.draw.rect(self.renderTarget, self.color,
pygame.Rect(x - hw + self.rtWidthHalf - self.camera.x*self.affection, y - hh
+ self.rtHeightHalf - self.camera.y*self.affection, hw*2, hh*2))
def getDimHalf(self):
return (self.rtWidthHalf,self.rtHeightHalf)