view pytouhou/ui/gamerenderer.pyx @ 262:8fa660da5f0c

Automatically search data files using different names.
author Thibaut Girka <thib@sitedethib.com>
date Mon, 23 Jan 2012 00:58:03 +0100
parents 6bd565019f9a
children 80e73b8245a4
line wrap: on
line source

# -*- encoding: utf-8 -*-
##
## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##


from itertools import chain

from pyglet.gl import *

from .renderer cimport Renderer
from .background cimport get_background_rendering_data



cdef class GameRenderer(Renderer):
    cdef public game
    cdef public background


    def __init__(self, resource_loader, game=None, background=None):
        Renderer.__init__(self, resource_loader)

        self.game = game
        self.background = background

        if game:
            # Preload textures
            self.texture_manager.preload(game.resource_loader.instanced_anms.values())


    def render(self):
        glClear(GL_DEPTH_BUFFER_BIT)

        back = self.background
        game = self.game
        texture_manager = self.texture_manager

        if game is not None and game.effect is not None:
            self.setup_camera(0, 0, 1)

            glDisable(GL_FOG)
            self.render_elements([game.effect])
            glEnable(GL_FOG)
        elif back is not None:
            fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values
            x, y, z = back.position_interpolator.values
            dx, dy, dz = back.position2_interpolator.values

            glFogi(GL_FOG_MODE, GL_LINEAR)
            glFogf(GL_FOG_START, fog_start)
            glFogf(GL_FOG_END,  fog_end)
            glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.))

            self.setup_camera(dx, dy, dz)
            glTranslatef(-x, -y, -z)

            glEnable(GL_DEPTH_TEST)
            for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back):
                glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
                glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
                glVertexPointer(3, GL_FLOAT, 0, vertices)
                glTexCoordPointer(2, GL_FLOAT, 0, uvs)
                glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
                glDrawArrays(GL_QUADS, 0, nb_vertices)
            glDisable(GL_DEPTH_TEST)
        else:
            glClear(GL_COLOR_BUFFER_BIT)

        if game is not None:
            self.setup_camera(0, 0, 1)

            glDisable(GL_FOG)
            self.render_elements(chain(*(enemy.objects() for enemy in game.enemies)))
            self.render_elements(game.enemies)
            self.render_elements(game.effects)
            self.render_elements(chain(game.players_bullets,
                                       game.players,
                                       *(player.objects() for player in game.players)))
            self.render_elements(chain(game.bullets, game.cancelled_bullets, game.items))
            #TODO: display item indicators
            glEnable(GL_FOG)