33from functools import reduce
44
55import pygame .color
6+ from pygame .rect import Rect
67
78from lochpython .core .debug import Debugger
89from lochpython .core .settings import *
@@ -89,7 +90,6 @@ def remove_visible_object(cls, sprite_prop):
8990 if sprite_prop in cls .stack [dst_layer ]:
9091 cls .stack [dst_layer ].remove (sprite_prop )
9192
92-
9393 @classmethod
9494 def sort_order (cls ):
9595 Debugger .print ('Renderer sorting' )
@@ -100,52 +100,68 @@ def render(cls):
100100 for layer in cls .stack :
101101 layer .attach_camera (cls .world .camera )
102102 layer .draw (MainRenderer .world_surface )
103+
103104 @classmethod
104105 @property
105106 def visible_objects_count (cls ):
106107 return reduce (lambda a , b : a + len (b ), cls .stack , 0 )
107108
109+
108110class SkyRenderer :
111+ POINT_LIGHT_PATH = 'data/lighting/point_light.png'
109112 camera = None
110113 sky_surface = None
111- point_light_image = None
114+ point_light_images = []
112115 light_sources = []
113116 darknes = 0
117+
114118 @classmethod
115119 def attach_camera (cls , camera ):
116120 cls .camera = camera
117121
122+ @classmethod
123+ def _load_point_light_graphics (cls , levels_count = 8 ):
124+ src_image = pygame .image .load (cls .POINT_LIGHT_PATH )
125+ src_size , src_depth = (src_image .get_width (), src_image .get_height ()), src_image .get_bitsize ()
126+ inv_surface = pygame .Surface (src_size , depth = src_depth )
127+ inv_surface .fill (pygame .color .Color ('White' ))
128+ inv_surface .blit (src_image , (0 , 0 ), special_flags = pygame .BLEND_RGB_SUB )
129+
130+ result = []
131+ min_size = POINT_LIGHT_MIN_IMAGE_SIZE
132+ levels_count = POINT_LIGHT_STRENGTH_LEVELS
133+ for level in range (levels_count - 1 ):
134+ scale_factor = level / levels_count
135+ img_width = int (min_size [0 ] + scale_factor * (src_size [0 ] - min_size [0 ]))
136+ img_height = int (min_size [1 ] + scale_factor * (src_size [1 ] - min_size [1 ]))
137+ result .append (pygame .transform .scale (inv_surface , (img_width , img_height )))
138+ result .append (inv_surface )
139+ return result
140+
118141 @classmethod
119142 def init (cls ):
120143 width , height = MainRenderer .rendering_width , MainRenderer .rendering_height
121144 cls .sky_surface = pygame .Surface ((width , height ), depth = MainRenderer .world_surface .get_bitsize ())
122- #aww
123- cls . point_light_image = pygame . image . load ( 'data/lighting/point_light.png' )
145+ cls . point_light_images = cls . _load_point_light_graphics ()
146+ # aww
124147 if not cls .light_sources :
125148 cls .light_sources = []
126149 # cls.light_sources.append()
127150 # print(cls.sky_surface)
128151
129-
130152 @classmethod
131153 def render (cls ):
132- cls .darknes = (math .sin (time .time ())+ 1 )/ 2
133- # print(cls.darknes)
134- channel_value = int (255 * cls .darknes )
135- channel_value = min (215 ,max (30 ,channel_value ))
136- cls .sky_surface .fill ((channel_value ,channel_value ,channel_value ))
137-
138- translation = sub_tuples_2D (cls .camera , (HALF_RENDERING_WIDTH , HALF_RENDERING_HEIGHT ))
139- translation = add_tuples_2D (translation , (cls .point_light_image .get_width ()// 2 , cls .point_light_image .get_height ()// 2 ))
154+ channel_value = 180
155+ cls .sky_surface .fill ((channel_value , channel_value , channel_value ))
140156 for ls_prop in cls .light_sources :
141- light_pos = sub_tuples_2D ( ls_prop .position , translation )
142-
143- # image_data = sprite_prop.image_data
144- # sprite_clip_rect = sprite_prop.clip_rect
145- # scaled_image = pygame.transform.scale(sprite_image, (w,h) )
146-
147- cls .sky_surface .blit (cls . point_light_image , light_pos )
148- MainRenderer .world_surface .blit (cls .sky_surface , (0 ,0 ), special_flags = pygame .BLEND_RGBA_SUB )
157+ light_strength = ls_prop .strength
158+ point_light_image = cls . point_light_images [ light_strength ]
159+ point_light_img_size = ( point_light_image . get_width () // 2 , point_light_image . get_height () // 2 )
160+ light_pos = sub_tuples_2D ( ls_prop . position , point_light_img_size )
161+ light_pos = sub_tuples_2D ( light_pos , cls . camera )
162+ light_pos = add_tuples_2D ( light_pos , ( HALF_RENDERING_WIDTH , HALF_RENDERING_HEIGHT ))
163+ cls .sky_surface .blit (point_light_image , light_pos , special_flags = pygame . BLEND_RGB_SUB )
164+ MainRenderer .world_surface .blit (cls .sky_surface , (0 , 0 ), special_flags = pygame .BLEND_RGB_SUB )
149165
150166 @classmethod
151167 def add_point_light (cls , light ):
0 commit comments