comparison pytouhou/ui/sdl/gamerenderer.py @ 512:b39ad30c6620

Add a pure SDL backend.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 05 Dec 2013 01:55:39 +0100
parents
children 577c3a88fb67
comparison
equal deleted inserted replaced
511:2e8ceaa85d5c 512:b39ad30c6620
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 from itertools import chain
16
17 from pytouhou.lib.sdl import Rect
18 from .sprite import get_sprite_rendering_data
19
20 from pytouhou.utils.helpers import get_logger
21 logger = get_logger(__name__)
22
23
24 class GameRenderer(object):
25 def __init__(self, resource_loader, window):
26 self.window = window
27 self.texture_manager = TextureManager(resource_loader, self.window.win)
28
29
30 def load_textures(self, anms):
31 self.texture_manager.load(anms)
32
33
34 def load_background(self, background):
35 if background is not None:
36 logger.error('Background rendering unavailable in the SDL backend.')
37
38
39 def start(self, common):
40 pass
41
42
43 def render(self, game):
44 self.render_game(game)
45 self.render_text(game.texts + game.native_texts)
46 self.render_interface(game.interface, game.boss)
47
48
49 def render_game(self, game):
50 x, y = game.interface.game_pos
51 self.window.win.render_set_viewport(Rect(x, y, game.width, game.height))
52 self.window.win.render_set_clip_rect(Rect(x, -y, game.width, game.height))
53
54 if game is not None:
55 if game.spellcard_effect is not None:
56 self.render_elements([game.spellcard_effect])
57 else:
58 self.window.win.render_clear()
59
60 self.render_elements([enemy for enemy in game.enemies if enemy.visible])
61 self.render_elements(game.effects)
62 self.render_elements(chain(game.players_bullets,
63 game.lasers_sprites(),
64 game.players,
65 game.msg_sprites()))
66 self.render_elements(chain(game.bullets, game.lasers,
67 game.cancelled_bullets, game.items,
68 game.labels))
69
70
71 def render_interface(self, interface, boss):
72 interface.labels['framerate'].set_text('%.2ffps' % self.window.clock.get_fps())
73
74 self.window.win.render_set_viewport(Rect(0, 0, interface.width, interface.height))
75 self.window.win.render_set_clip_rect(Rect(0, 0, interface.width, interface.height))
76
77 items = [item for item in interface.items if item.anmrunner and item.anmrunner.running]
78 labels = interface.labels.values()
79
80 if items:
81 # Redraw all the interface
82 self.render_elements(items)
83 else:
84 # Redraw only changed labels
85 labels = [label for label in labels if label.changed]
86
87 self.render_elements(interface.level_start)
88
89 if boss:
90 self.render_elements(interface.boss_items)
91
92 self.render_elements(labels)
93 for label in labels:
94 label.changed = False
95
96
97 def render_elements(self, elements):
98 nb_vertices = 0
99
100 objects = chain(*[element.objects for element in elements])
101 for element in objects:
102 if nb_vertices >= MAX_ELEMENTS - 4:
103 break
104
105 sprite = element.sprite
106 if sprite and sprite.visible:
107 ox, oy = element.x, element.y
108 blendfunc, (vertices, uvs, colors, rotation, flip) = get_sprite_rendering_data(sprite)
109
110 # Pack data in buffer
111 x, y, width, height = vertices
112 left, right, bottom, top = uvs
113 r, g, b, a = colors #TODO: use it.
114
115 #XXX
116 texture_width = 256
117 texture_height = 256
118
119 source = Rect(left * texture_width, bottom * texture_height, (right - left) * texture_width, (top - bottom) * texture_height)
120 dest = Rect(ox + x, oy + y, width, height)
121
122 texture = sprite.anm.texture
123 texture.set_color_mod(r, g, b)
124 texture.set_alpha_mod(a)
125 texture.set_blend_mode(2 if blendfunc else 1)
126
127 if rotation or flip:
128 self.window.win.render_copy_ex(texture, source, dest, rotation, flip)
129 else:
130 self.window.win.render_copy(texture, source, dest)
131
132 nb_vertices += 4
133
134
135 def render_text(self, texts):
136 pass