# HG changeset patch # User Emmanuel Gil Peyrot # Date 1378993628 -7200 # Node ID feecdb4a89281b75b2d2d54d13e1764ff7edd48e # Parent 5bb7d2c0ff468ae31d32e18e2b6604a179324a22 Add “except *” to cdef void functions, and type some more. diff --git a/pytouhou/game/bullet.pxd b/pytouhou/game/bullet.pxd --- a/pytouhou/game/bullet.pxd +++ b/pytouhou/game/bullet.pxd @@ -13,15 +13,15 @@ cdef class Bullet(Element): cdef public double dx, dy, angle, speed cdef public bint player_bullet, was_visible, grazed cdef public object target, _bullet_type - cdef public tuple hitbox cdef public list attributes + cdef double hitbox[2] cdef Interpolator speed_interpolator cdef Game _game - cdef bint is_visible(self, unsigned int screen_width, unsigned int screen_height) + cdef bint is_visible(self, unsigned int screen_width, unsigned int screen_height) except? False cpdef set_anim(self, sprite_idx_offset=*) - cdef void launch(self) - cpdef collide(self) - cpdef cancel(self) - cpdef update(self) + cdef void launch(self) except * + cdef void collide(self) except * + cdef void cancel(self) except * + cdef void update(self) except * diff --git a/pytouhou/game/bullet.pyx b/pytouhou/game/bullet.pyx --- a/pytouhou/game/bullet.pyx +++ b/pytouhou/game/bullet.pyx @@ -21,7 +21,7 @@ from pytouhou.game.sprite cimport Sprite cdef class Bullet(Element): def __init__(self, pos, bullet_type, unsigned long sprite_idx_offset, double angle, double speed, attributes, unsigned long flags, target, Game game, - bint player_bullet=False, unsigned long damage=0, hitbox=None): + bint player_bullet=False, unsigned long damage=0, tuple hitbox=None): cdef double launch_mult Element.__init__(self, pos) @@ -32,9 +32,9 @@ cdef class Bullet(Element): self.was_visible = True if hitbox is not None: - self.hitbox = hitbox + self.hitbox[:] = [hitbox[0], hitbox[1]] else: - self.hitbox = (bullet_type.hitbox_size, bullet_type.hitbox_size) + self.hitbox[:] = [bullet_type.hitbox_size, bullet_type.hitbox_size] self.speed_interpolator = None self.frame = 0 @@ -121,12 +121,12 @@ cdef class Bullet(Element): (self.speed,), 16) - cpdef collide(self): + cdef void collide(self): self.cancel() self._game.new_particle((self.x, self.y), 10, 256) #TODO: find the real size. - cpdef cancel(self): + cdef void cancel(self): # Cancel animation bt = self._bullet_type self.sprite = Sprite() @@ -141,7 +141,7 @@ cdef class Bullet(Element): self.state = CANCELLED - cpdef update(self): + cdef void update(self): cdef int frame, count, game_width, game_height cdef double length, angle, speed, acceleration, angular_speed diff --git a/pytouhou/game/enemy.pxd b/pytouhou/game/enemy.pxd --- a/pytouhou/game/enemy.pxd +++ b/pytouhou/game/enemy.pxd @@ -1,5 +1,6 @@ from pytouhou.game.element cimport Element from pytouhou.game.game cimport Game +from pytouhou.game.player cimport Player from pytouhou.utils.interpolator cimport Interpolator cdef class Enemy(Element): @@ -27,16 +28,16 @@ cdef class Enemy(Element): start_duration, duration, end_duration, grazing_delay, grazing_extra_duration, unknown, offset=*) - cpdef select_player(self, players=*) - cpdef get_player_angle(self, player=*, pos=*) + cpdef Player select_player(self, list players=*) + cpdef double get_player_angle(self, tuple pos=*, Player player=*) except 42 cpdef set_anim(self, index) - cdef void die_anim(self) - cdef void drop_particles(self, long number, long color) + cdef void die_anim(self) except * + cdef void drop_particles(self, long number, long color) except * cpdef set_aux_anm(self, long number, long index) cpdef set_pos(self, x, y, z) cpdef move_to(self, duration, x, y, z, formula) cpdef stop_in(self, duration, formula) - cdef bint is_visible(self, long screen_width, long screen_height) - cdef void check_collisions(self) - cdef void handle_callbacks(self) - cpdef update(self) + cdef bint is_visible(self, long screen_width, long screen_height) except? False + cdef void check_collisions(self) except * + cdef void handle_callbacks(self) except * + cdef void update(self) except * diff --git a/pytouhou/game/enemy.pyx b/pytouhou/game/enemy.pyx --- a/pytouhou/game/enemy.pyx +++ b/pytouhou/game/enemy.pyx @@ -159,7 +159,7 @@ cdef class Enemy(Element): player = self.select_player() if type_ in (67, 69, 71): - launch_angle += self.get_player_angle(player, launch_pos) + launch_angle += self.get_player_angle(launch_pos, player) if type_ == 71 and bullets_per_shot % 2 or type_ in (69, 70) and not bullets_per_shot % 2: launch_angle += pi / bullets_per_shot if type_ != 75: @@ -200,7 +200,7 @@ cdef class Enemy(Element): ox, oy = offset or self.bullet_launch_offset launch_pos = self.x + ox, self.y + oy if variant == 86: - angle += self.get_player_angle(self.select_player(), launch_pos) + angle += self.get_player_angle(launch_pos) laser = Laser(launch_pos, self._game.laser_types[laser_type], sprite_idx_offset, angle, speed, start_offset, end_offset, max_length, width, @@ -210,14 +210,18 @@ cdef class Enemy(Element): self.laser_by_id[self.current_laser_id] = laser - cpdef select_player(self, players=None): - return (players or self._game.players)[0] #TODO + cpdef Player select_player(self, list players=None): + if players is None: + players = self._game.players + return players[0] #TODO - cpdef get_player_angle(self, player=None, pos=None): - player_state = (player or self.select_player()).state + cpdef double get_player_angle(self, tuple pos=None, Player player=None): + cdef double x, y + if player is None: + player = self.select_player() x, y = pos or (self.x, self.y) - return atan2(player_state.y - y, player_state.x - x) + return atan2(player.state.y - y, player.state.x - x) cpdef set_anim(self, index): @@ -295,8 +299,10 @@ cdef class Enemy(Element): cdef void check_collisions(self): + cdef Bullet bullet + cdef Player player cdef long damages - cdef double half_size[2], bx, by, lx, ly, px, py, phalf_size + cdef double half_size[2], lx, ly, phalf_size # Check for collisions ex, ey = self.x, self.y @@ -451,10 +457,10 @@ cdef class Enemy(Element): raise Exception('What the hell, man!') - cpdef update(self): + cdef void update(self): cdef double x, y, speed - if self.process: + if self.process is not None: self.process.run_iteration() x, y = self.x, self.y diff --git a/pytouhou/game/game.pxd b/pytouhou/game/game.pxd --- a/pytouhou/game/game.pxd +++ b/pytouhou/game/game.pxd @@ -11,18 +11,34 @@ cdef class Game: cdef public bint time_stop, msg_wait cdef public unsigned short deaths_count, next_bonus - cpdef modify_difficulty(self, long diff) + cdef list msg_sprites(self) + cdef list lasers_sprites(self) + cdef void modify_difficulty(self, long diff) except * + cpdef enable_spellcard_effect(self) + cpdef disable_spellcard_effect(self) + cdef void set_player_bomb(self) except * + cdef void unset_player_bomb(self) except * cpdef drop_bonus(self, double x, double y, long _type, end_pos=*) - cdef void autocollect(self, Player player) - cpdef cancel_bullets(self) + cdef void autocollect(self, Player player) except * + cdef void cancel_bullets(self) except * + cpdef change_bullets_into_star_items(self) + cpdef change_bullets_into_bonus(self) + cpdef kill_enemies(self) + cpdef new_effect(self, pos, long anim, anm=*, long number=*) cpdef new_particle(self, pos, long anim, long amp, long number=*, bint reverse=*, long duration=*) - cpdef new_label(self, pos, str text) - cdef void update_background(self) - cdef void update_enemies(self) + cpdef new_enemy(self, pos, life, instr_type, bonus_dropped, die_score) + cpdef new_msg(self, sub) + cdef new_label(self, pos, str text) + cpdef new_native_text(self, pos, text, align=*) + cpdef new_hint(self, hint) + cpdef new_face(self, side, effect) + cpdef run_iter(self, long keystate) + cdef void update_background(self) except * + cdef void update_enemies(self) except * cdef void update_msg(self, long keystate) except * cdef void update_players(self, long keystate) except * - cdef void update_effects(self) - cdef void update_hints(self) - cdef void update_faces(self) - cdef void update_bullets(self) + cdef void update_effects(self) except * + cdef void update_hints(self) except * + cdef void update_faces(self) except * + cdef void update_bullets(self) except * cpdef cleanup(self) diff --git a/pytouhou/game/game.pyx b/pytouhou/game/game.pyx --- a/pytouhou/game/game.pyx +++ b/pytouhou/game/game.pyx @@ -75,15 +75,15 @@ cdef class Game: self.last_keystate = 0 - def msg_sprites(self): + cdef list msg_sprites(self): return [face for face in self.faces if face is not None] if self.msg_runner is not None and not self.msg_runner.ended else [] - def lasers_sprites(self): + cdef list lasers_sprites(self): return [laser for laser in self.players_lasers if laser is not None] - cpdef modify_difficulty(self, long diff): + cdef void modify_difficulty(self, long diff): self.difficulty_counter += diff while self.difficulty_counter < 0: self.difficulty -= 1 @@ -97,7 +97,7 @@ cdef class Game: self.difficulty = self.difficulty_max - def enable_spellcard_effect(self): + cpdef enable_spellcard_effect(self): pos = (-32, -16) self.spellcard_effect = Effect(pos, 0, self.spellcard_effect_anm) @@ -111,19 +111,19 @@ cdef class Game: self.texts[5] = self.new_native_text((384-24, 24), self.spellcard[1], align='right') - def disable_spellcard_effect(self): + cpdef disable_spellcard_effect(self): self.spellcard_effect = None self.texts[5] = None - def set_player_bomb(self): + cdef void set_player_bomb(self): face = Effect((-32, -16), 1, self.msg_anm[0][0][0]) face.sprite.allow_dest_offset = True self.effects.append(face) self.texts[4] = self.new_native_text((24, 24), u'Player Spellcard') - def unset_player_bomb(self): + cdef void unset_player_bomb(self): self.texts[4] = None @@ -143,7 +143,7 @@ cdef class Game: item.autocollect(player) - cpdef cancel_bullets(self): + cdef void cancel_bullets(self): cdef Bullet bullet #TODO: cdef Laser laser @@ -153,22 +153,22 @@ cdef class Game: laser.cancel() - def change_bullets_into_star_items(self): + cpdef change_bullets_into_star_items(self): cdef Player player cdef Bullet bullet player = self.players[0] #TODO item_type = self.item_types[6] - self.items.extend(Item((bullet.x, bullet.y), 6, item_type, self, player=player) - for bullet in self.bullets) + self.items.extend([Item((bullet.x, bullet.y), 6, item_type, self, player=player) + for bullet in self.bullets]) for laser in self.lasers: - self.items.extend(Item(pos, 6, item_type, self, player=player) - for pos in laser.get_bullets_pos()) + self.items.extend([Item(pos, 6, item_type, self, player=player) + for pos in laser.get_bullets_pos()]) laser.cancel() self.bullets = [] - def change_bullets_into_bonus(self): + cpdef change_bullets_into_bonus(self): cdef Player player cdef Bullet bullet @@ -184,7 +184,7 @@ cdef class Game: #TODO: display the final bonus score. - def kill_enemies(self): + cpdef kill_enemies(self): cdef Enemy enemy for enemy in self.enemies: @@ -198,7 +198,7 @@ cdef class Game: enemy.death_callback = -1 - def new_effect(self, pos, long anim, anm=None, long number=1): + cpdef new_effect(self, pos, long anim, anm=None, long number=1): number = min(number, self.nb_bullets_max - len(self.effects)) for i in xrange(number): self.effects.append(Effect(pos, anim, anm or self.etama[1])) @@ -210,30 +210,30 @@ cdef class Game: self.effects.append(Particle(pos, anim, self.etama[1], amp, self, reverse=reverse, duration=duration)) - def new_enemy(self, pos, life, instr_type, bonus_dropped, die_score): + cpdef new_enemy(self, pos, life, instr_type, bonus_dropped, die_score): enemy = Enemy(pos, life, instr_type, bonus_dropped, die_score, self.enm_anm, self) self.enemies.append(enemy) return enemy - def new_msg(self, sub): + cpdef new_msg(self, sub): self.msg_runner = MSGRunner(self.msg, sub, self) self.msg_runner.run_iteration() - cpdef new_label(self, pos, str text): + cdef new_label(self, pos, str text): label = Text(pos, self.interface.ascii_anm, text=text, xspacing=8, shift=48) label.set_timeout(60, effect='move') self.labels.append(label) return label - def new_native_text(self, pos, text, align='left'): + cpdef new_native_text(self, pos, text, align='left'): label = NativeText(pos, text, shadow=True, align=align) return label - def new_hint(self, hint): + cpdef new_hint(self, hint): pos = hint['Pos'] #TODO: Scale @@ -246,13 +246,13 @@ cdef class Game: return label - def new_face(self, side, effect): + cpdef new_face(self, side, effect): face = Face(self.msg_anm, effect, side) self.faces[side] = face return face - def run_iter(self, long keystate): + cpdef run_iter(self, long keystate): # 1. VMs. for runner in self.ecl_runners: runner.run_iter() @@ -317,7 +317,7 @@ cdef class Game: enemy.update() - cdef void update_msg(self, long keystate) except *: + cdef void update_msg(self, long keystate): cdef long k for k in (1, 256): @@ -329,7 +329,7 @@ cdef class Game: self.msg_runner.run_iteration() - cdef void update_players(self, long keystate) except *: + cdef void update_players(self, long keystate): cdef Bullet bullet cdef Player player @@ -371,7 +371,6 @@ cdef class Game: cdef Player player cdef Bullet bullet cdef Item item - cdef double bhalf_width, bhalf_height if self.time_stop: return @@ -419,7 +418,8 @@ cdef class Game: if bullet.state != LAUNCHED: continue - bhalf_width, bhalf_height = bullet.hitbox + bhalf_width = bullet.hitbox[0] + bhalf_height = bullet.hitbox[1] bx, by = bullet.x, bullet.y bx1, bx2 = bx - bhalf_width, bx + bhalf_width by1, by2 = by - bhalf_height, by + bhalf_height diff --git a/pytouhou/game/item.pxd b/pytouhou/game/item.pxd --- a/pytouhou/game/item.pxd +++ b/pytouhou/game/item.pxd @@ -7,7 +7,7 @@ from pytouhou.utils.interpolator cimport cdef class Indicator(Element): cdef Item _item - cpdef update(self) + cdef void update(self) nogil cdef class Item(Element): @@ -21,6 +21,6 @@ cdef class Item(Element): cdef Indicator indicator cdef Interpolator speed_interpolator, pos_interpolator - cpdef autocollect(self, Player player) - cpdef on_collect(self, Player player) + cdef void autocollect(self, Player player) except * + cdef void on_collect(self, Player player) except * cpdef update(self) diff --git a/pytouhou/game/item.pyx b/pytouhou/game/item.pyx --- a/pytouhou/game/item.pyx +++ b/pytouhou/game/item.pyx @@ -26,7 +26,7 @@ cdef class Indicator(Element): self.y = self.sprite.texcoords[2] / 2. - cpdef update(self): + cdef void update(self) nogil: #TODO: alpha self.x = self._item.x @@ -69,13 +69,13 @@ cdef class Item(Element): return [self] - cpdef autocollect(self, Player player): + cdef void autocollect(self, Player player): if self.player is None: self.player = player self.speed = player.sht.autocollection_speed - cpdef on_collect(self, Player player): + cdef void on_collect(self, Player player): cdef long level, poc player_state = player.state diff --git a/pytouhou/game/player.pxd b/pytouhou/game/player.pxd --- a/pytouhou/game/player.pxd +++ b/pytouhou/game/player.pxd @@ -20,8 +20,8 @@ cdef class Player(Element): cdef tuple speeds cdef long fire_time, bomb_time, direction - cdef void set_anim(self, index) - cpdef play_sound(self, str name) - cpdef collide(self) - cdef void fire(self) + cdef void set_anim(self, index) except * + cdef void play_sound(self, str name) except * + cdef void collide(self) except * + cdef void fire(self) except * cpdef update(self, long keystate) diff --git a/pytouhou/game/player.pyx b/pytouhou/game/player.pyx --- a/pytouhou/game/player.pyx +++ b/pytouhou/game/player.pyx @@ -82,11 +82,11 @@ cdef class Player(Element): self.anmrunner = ANMRunner(self.anm, index, self.sprite) - cpdef play_sound(self, str name): + cdef void play_sound(self, str name): self._game.sfx_player.play('%s.wav' % name) - cpdef collide(self): + cdef void collide(self): if not self.state.invulnerable_time and not self.death_time and self.state.touchable: # Border Between Life and Death self.death_time = self._game.frame self._game.new_effect((self.state.x, self.state.y), 17) diff --git a/pytouhou/ui/background.pxd b/pytouhou/ui/background.pxd --- a/pytouhou/ui/background.pxd +++ b/pytouhou/ui/background.pxd @@ -13,5 +13,5 @@ cdef class BackgroundRenderer: cdef unsigned int use_fixed_pipeline, vbo cdef object background - cdef void render_background(self) - cdef void load(self, background) + cdef void render_background(self) except * + cdef void load(self, background) except * diff --git a/pytouhou/ui/gamerenderer.pxd b/pytouhou/ui/gamerenderer.pxd --- a/pytouhou/ui/gamerenderer.pxd +++ b/pytouhou/ui/gamerenderer.pxd @@ -1,7 +1,9 @@ from pytouhou.utils.matrix cimport Matrix +from pytouhou.game.game cimport Game from .background cimport BackgroundRenderer from .renderer cimport Renderer, Framebuffer from .shader cimport Shader +from .window cimport Window cdef class GameRenderer(Renderer): cdef Matrix game_mvp, interface_mvp, proj @@ -9,9 +11,9 @@ cdef class GameRenderer(Renderer): cdef Framebuffer framebuffer cdef BackgroundRenderer background_renderer - cdef void load_background(self, background) - cdef void start(self, game) - cdef void render(self, game, window) - cdef void render_game(self, game) - cdef void render_text(self, texts) - cdef void render_interface(self, interface, game_boss) + cdef void load_background(self, background) except * + cdef void start(self, Game game) except * + cdef void render(self, Game game, Window window) except * + cdef void render_game(self, Game game) except * + cdef void render_text(self, texts) except * + cdef void render_interface(self, interface, game_boss) except * diff --git a/pytouhou/ui/gamerenderer.pyx b/pytouhou/ui/gamerenderer.pyx --- a/pytouhou/ui/gamerenderer.pyx +++ b/pytouhou/ui/gamerenderer.pyx @@ -52,7 +52,7 @@ cdef class GameRenderer(Renderer): self.background_renderer = None - cdef void start(self, game): + cdef void start(self, Game game): self.proj = perspective(30, float(game.width) / float(game.height), 101010101./2010101., 101010101./10101.) game_view = setup_camera(0, 0, 1) @@ -60,7 +60,7 @@ cdef class GameRenderer(Renderer): self.interface_mvp = ortho_2d(0., float(game.interface.width), float(game.interface.height), 0.) - cdef void render(self, game, window): + cdef void render(self, Game game, Window window): if not self.use_fixed_pipeline: self.framebuffer.bind() @@ -74,7 +74,7 @@ cdef class GameRenderer(Renderer): self.render_framebuffer(self.framebuffer, window) - cdef void render_game(self, game): + cdef void render_game(self, Game game): cdef long game_x, game_y cdef float x, y, z, dx, dy, dz, fog_data[4], fog_start, fog_end cdef unsigned char fog_r, fog_g, fog_b @@ -167,7 +167,7 @@ cdef class GameRenderer(Renderer): game.cancelled_bullets, game.items, game.labels)) - if game.msg_runner: + if game.msg_runner is not None: rect = Rect(48, 368, 288, 48) color1 = Color(0, 0, 0, 192) color2 = Color(0, 0, 0, 128) diff --git a/pytouhou/ui/renderer.pxd b/pytouhou/ui/renderer.pxd --- a/pytouhou/ui/renderer.pxd +++ b/pytouhou/ui/renderer.pxd @@ -24,9 +24,9 @@ cdef class Renderer: cdef unsigned short last_indices[2 * MAX_TEXTURES] cdef PyObject *elements[640*3] - cpdef render_elements(self, elements) - cpdef render_quads(self, rects, colors, texture) - cpdef render_framebuffer(self, Framebuffer fb, Window window) + cdef void render_elements(self, elements) except * + cdef void render_quads(self, rects, colors, texture) except * + cdef void render_framebuffer(self, Framebuffer fb, Window window) except * cdef class Framebuffer: diff --git a/pytouhou/ui/renderer.pyx b/pytouhou/ui/renderer.pyx --- a/pytouhou/ui/renderer.pyx +++ b/pytouhou/ui/renderer.pyx @@ -47,7 +47,7 @@ logger = get_logger(__name__) DEF MAX_ELEMENTS = 640*4*3 -cdef long find_objects(Renderer self, object elements): +cdef long find_objects(Renderer self, object elements) except -1: # Don’t type element as Element, or else the overriding of objects won’t work. cdef Element obj cdef long i = 0 @@ -100,7 +100,7 @@ cdef class Renderer: free(self.indices[i][texture]) - cpdef render_elements(self, elements): + cdef void render_elements(self, elements): cdef int key cdef int x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, ox, oy cdef float left, right, bottom, top @@ -177,7 +177,7 @@ cdef class Renderer: glBindBuffer(GL_ARRAY_BUFFER, 0) - cpdef render_quads(self, rects, colors, texture): + cdef void render_quads(self, rects, colors, texture): # There is nothing that batch more than two quads on the same texture, currently. cdef Vertex buf[8] cdef unsigned short indices[12] @@ -217,7 +217,7 @@ cdef class Renderer: glBindBuffer(GL_ARRAY_BUFFER, 0) - cpdef render_framebuffer(self, Framebuffer fb, Window window): + cdef void render_framebuffer(self, Framebuffer fb, Window window): cdef PassthroughVertex[4] buf cdef unsigned short indices[6] indices[:] = [0, 1, 2, 2, 3, 0] diff --git a/pytouhou/ui/shader.pxd b/pytouhou/ui/shader.pxd --- a/pytouhou/ui/shader.pxd +++ b/pytouhou/ui/shader.pxd @@ -6,10 +6,10 @@ cdef class Shader: cdef bint linked cdef dict location_cache - cdef void create_shader(self, const GLchar *string, GLenum shader_type) - cdef void link(self) - cdef GLint get_uniform_location(self, name) + cdef void create_shader(self, const GLchar *string, GLenum shader_type) except * + cdef void link(self) except * + cdef GLint get_uniform_location(self, name) except -1 cdef void bind(self) nogil - cdef void uniform_1(self, name, GLfloat val) - cdef void uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d) - cdef void uniform_matrix(self, name, Matrix mat) + cdef void uniform_1(self, name, GLfloat val) except * + cdef void uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d) except * + cdef void uniform_matrix(self, name, Matrix mat) except * diff --git a/pytouhou/utils/bitstream.pyx b/pytouhou/utils/bitstream.pyx --- a/pytouhou/utils/bitstream.pyx +++ b/pytouhou/utils/bitstream.pyx @@ -47,7 +47,7 @@ cdef class BitStream: return self.io.tell(), self.bits - cpdef unsigned char read_bit(self): + cpdef unsigned char read_bit(self) except? -1: if not self.bits: self.bytes = self.io.read(1) self.byte = ( self.bytes)[0] @@ -56,7 +56,7 @@ cdef class BitStream: return (self.byte >> self.bits) & 0x01 - cpdef unsigned int read(self, unsigned int nb_bits): + cpdef unsigned int read(self, unsigned int nb_bits) except? -1: cdef unsigned int value = 0, read = 0 cdef unsigned int nb_bits2 = nb_bits