From 4bf930688087731ff6089b12350a5fc9a87f9f82 Mon Sep 17 00:00:00 2001 From: James Cameron Date: Tue, 01 Oct 2013 10:41:22 +0000 Subject: return sequence of dirty rectangles for all drawing operations Every drawing operation is to return a sequence of rectangles that have been drawn on, for update optimisation. --- diff --git a/elements_painter.py b/elements_painter.py index 63e01a6..e32bf33 100644 --- a/elements_painter.py +++ b/elements_painter.py @@ -30,10 +30,13 @@ def paint_ball(ball, surface): Draws a ball onto the given PyGame surface. ball : the ball to draw => Ball instance surface : the destination surface => PyGame.Surface + (returns sequence of Rect, or an empty sequence) """ + s = [] if ball.is_visible(): - pygame.draw.circle(surface, ball.get_bg_color(), ball.get_center(), - int(ball.get_diameter() / 2)) + r = pygame.draw.circle(surface, ball.get_bg_color(), ball.get_center(), + int(ball.get_diameter() / 2)) + s.append(r) ball_center = ball.get_center() txt_width, txt_height = ball.get_txt_font().size(ball.get_operation(). get_text()) @@ -41,7 +44,9 @@ def paint_ball(ball, surface): int(ball_center[1] - txt_height / 2)) txt_surface = ball.get_txt_font().render( ball.get_operation().get_text(), 1, ball.get_txt_color()) - surface.blit(txt_surface, txt_position) + r = surface.blit(txt_surface, txt_position) + s.append(r) + return s def paint_time_bar(time_bar, surface): @@ -49,20 +54,25 @@ def paint_time_bar(time_bar, surface): Draws a time bar onto the given PyGame surface. time_bar : the time bar => TimeBar surface : the destination surface => PyGame.Surface + (returns sequence of Rect) """ + s = [] edge = time_bar.get_edge() dead_rect = (edge[0], edge[1], time_bar.get_width(), time_bar.get_height()) - pygame.draw.rect(surface, time_bar.get_dead_color(), dead_rect) + r = pygame.draw.rect(surface, time_bar.get_dead_color(), dead_rect) + s.append(r) try: value = time_bar.get_value() max_value = time_bar.get_max_value() active_rect = (edge[0], edge[1], int(time_bar.get_width() * value / max_value), time_bar.get_height()) - pygame.draw.rect(surface, time_bar.get_active_color(), active_rect) + r = pygame.draw.rect(surface, time_bar.get_active_color(), active_rect) except NameError: - pygame.draw.rect(surface, time_bar.get_active_color(), dead_rect) + r = pygame.draw.rect(surface, time_bar.get_active_color(), dead_rect) + s.append(r) + return s def paint_result_bar(result_bar, surface): @@ -70,20 +80,25 @@ def paint_result_bar(result_bar, surface): Draws a result bar onto a given PyGame surface. result_bar : the result bar => ResultBar surface : the destination surface => PyGame.Surface + (returns sequence of Rect) """ + s = [] edge = result_bar.get_edge() rect = (edge[0], edge[1], result_bar.get_width(), result_bar.get_height()) - pygame.draw.rect(surface, result_bar.get_background(), rect) + r = pygame.draw.rect(surface, result_bar.get_background(), rect) + s.append(r) try: text = result_bar.get_header() + str(result_bar.get_result()) except NameError: text = result_bar.get_header() text_surface = result_bar.get_text_font().render( text, 1, result_bar.get_foreground()) - surface.blit(text_surface, - (edge[0] + result_bar.get_insets(), - edge[1] + result_bar.get_insets())) + r = surface.blit(text_surface, + (edge[0] + result_bar.get_insets(), + edge[1] + result_bar.get_insets())) + s.append(r) + return s def paint_results(game_area, balls_list, surface): @@ -92,7 +107,9 @@ def paint_results(game_area, balls_list, surface): game_area : area of the balls => tuple of 4 integer balls_list : list of balls => list of Ball surface : the destination surface => PyGame.Surface + (returns sequence of Rect) """ + s = [] font = pygame.font.Font(None, 40) #font = PangoFont(family='Helvetica', size=16) LINE_HEIGHT = font.size("0123456789")[1] @@ -100,14 +117,17 @@ def paint_results(game_area, balls_list, surface): ball_index = 0 BLACK = (0, 0, 0) for ball in balls_list: - pygame.draw.circle(surface, ball.get_bg_color(), + r = pygame.draw.circle(surface, ball.get_bg_color(), (game_area[0] + CIRCLES_RADIUS, game_area[ 1] + ball_index * LINE_HEIGHT + CIRCLES_RADIUS), CIRCLES_RADIUS) + s.append(r) txt = ball.get_operation().get_text() + " = " + str(ball.get_operation(). get_result()) txt_surface = font.render(txt, 1, BLACK) - surface.blit(txt_surface, + r = surface.blit(txt_surface, (game_area[0] + 40, game_area[1] + ball_index * LINE_HEIGHT)) + s.append(r) ball_index += 1 + return s -- cgit v0.9.1