# HG changeset patch # User Emmanuel Gil Peyrot # Date 1378415514 -7200 # Node ID 1b56d62250abf854b452677a005ebc73b888f1b7 # Parent 4ccc47828002faeebe466bc7aadfb17adf1e92da Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types. diff --git a/pytouhou/ui/gamerenderer.pxd b/pytouhou/ui/gamerenderer.pxd new file mode 100644 --- /dev/null +++ b/pytouhou/ui/gamerenderer.pxd @@ -0,0 +1,9 @@ +from pytouhou.utils.matrix cimport Matrix +from .renderer cimport Renderer +from .shader cimport Shader + +cdef class GameRenderer(Renderer): + cdef Matrix game_mvp, interface_mvp, proj + cdef Shader game_shader, background_shader, interface_shader + + cdef object game, background, background_renderer #XXX diff --git a/pytouhou/ui/gamerenderer.pyx b/pytouhou/ui/gamerenderer.pyx --- a/pytouhou/ui/gamerenderer.pyx +++ b/pytouhou/ui/gamerenderer.pyx @@ -20,21 +20,18 @@ from pytouhou.lib.opengl cimport \ GL_PROJECTION, GL_MODELVIEW, GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END, GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat) -from pytouhou.utils.matrix cimport Matrix from pytouhou.utils.maths cimport setup_camera -from .renderer import Renderer - - -class GameRenderer(Renderer): +cdef class GameRenderer(Renderer): def __init__(self, resource_loader): Renderer.__init__(self, resource_loader) def render(self): - cdef float fog_data[4] - cdef Matrix view, mvp + cdef float x, y, z, dx, dy, dz, fog_data[4], fog_start, fog_end + cdef unsigned char fog_r, fog_g, fog_b + cdef Matrix mvp back = self.background game = self.game @@ -46,7 +43,7 @@ class GameRenderer(Renderer): if game is not None and game.spellcard_effect is not None: if self.use_fixed_pipeline: glMatrixMode(GL_MODELVIEW) - glLoadMatrixf((self.game_mvp).data) + glLoadMatrixf(self.game_mvp.data) glDisable(GL_FOG) else: self.game_shader.bind() @@ -103,7 +100,7 @@ class GameRenderer(Renderer): if game is not None: if self.use_fixed_pipeline: glMatrixMode(GL_MODELVIEW) - glLoadMatrixf((self.game_mvp).data) + glLoadMatrixf(self.game_mvp.data) glDisable(GL_FOG) else: self.game_shader.bind() diff --git a/pytouhou/ui/gamerunner.pyx b/pytouhou/ui/gamerunner.pyx --- a/pytouhou/ui/gamerunner.pyx +++ b/pytouhou/ui/gamerunner.pyx @@ -21,9 +21,9 @@ from pytouhou.lib.opengl cimport \ from pytouhou.utils.helpers import get_logger from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d -from pytouhou.utils.matrix cimport Matrix -from .gamerenderer import GameRenderer +from .window cimport Window +from .gamerenderer cimport GameRenderer from .background import BackgroundRenderer from .music import MusicPlayer, SFXPlayer, NullPlayer from .shaders.eosd import GameShader, BackgroundShader @@ -35,8 +35,13 @@ Color = namedtuple('Color', 'r g b a') logger = get_logger(__name__) -class GameRunner(GameRenderer): - def __init__(self, window, resource_loader, replay=None, skip=False): +cdef class GameRunner(GameRenderer): + cdef Window window + cdef object replay_level, save_keystates + cdef long width, height, keystate + cdef bint skip + + def __init__(self, window, resource_loader, bint skip=False): self.use_fixed_pipeline = window.use_fixed_pipeline #XXX GameRenderer.__init__(self, resource_loader) @@ -114,6 +119,8 @@ class GameRunner(GameRenderer): def update(self): + cdef long keystate + if self.background: self.background.update(self.game.frame) for event in sdl.poll_events(): @@ -125,7 +132,7 @@ class GameRunner(GameRenderer): elif type_ == sdl.QUIT: return False if self.game: - if not self.replay_level: + if self.replay_level is None: #TODO: allow user settings keys = sdl.get_keyboard_state() keystate = 0 @@ -217,7 +224,7 @@ class GameRunner(GameRenderer): if self.use_fixed_pipeline: glMatrixMode(GL_MODELVIEW) - glLoadMatrixf((self.interface_mvp).data) + glLoadMatrixf(self.interface_mvp.data) glDisable(GL_FOG) else: self.interface_shader.bind() diff --git a/pytouhou/ui/renderer.pxd b/pytouhou/ui/renderer.pxd --- a/pytouhou/ui/renderer.pxd +++ b/pytouhou/ui/renderer.pxd @@ -7,10 +7,12 @@ cdef struct Vertex: cdef class Renderer: - cdef public texture_manager + cdef public texture_manager, font_manager cdef unsigned int vbo cdef Vertex *vertex_buffer + cdef bint use_fixed_pipeline #XXX + cdef unsigned short *indices[2][MAX_TEXTURES] cdef unsigned short last_indices[2 * MAX_TEXTURES] cdef PyObject *elements[640*3] diff --git a/pytouhou/ui/shader.pxd b/pytouhou/ui/shader.pxd new file mode 100644 --- /dev/null +++ b/pytouhou/ui/shader.pxd @@ -0,0 +1,15 @@ +from pytouhou.lib.opengl cimport GLuint, GLint, GLchar, GLenum, GLfloat +from pytouhou.utils.matrix cimport Matrix + +cdef class Shader: + cdef GLuint handle + 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 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) diff --git a/pytouhou/ui/shader.pyx b/pytouhou/ui/shader.pyx --- a/pytouhou/ui/shader.pyx +++ b/pytouhou/ui/shader.pyx @@ -15,11 +15,9 @@ from pytouhou.lib.opengl cimport \ GL_COMPILE_STATUS, GL_INFO_LOG_LENGTH, glGetShaderInfoLog, glAttachShader, glLinkProgram, glGetProgramiv, glGetProgramInfoLog, GL_LINK_STATUS, glUseProgram, glGetUniformLocation, glUniform1fv, - glUniform4fv, glUniformMatrix4fv, glBindAttribLocation, GLint, - GLuint, GLchar, GLfloat, GLenum) + glUniform4fv, glUniformMatrix4fv, glBindAttribLocation) from libc.stdlib cimport malloc, free -from pytouhou.utils.matrix cimport Matrix class GLSLException(Exception): @@ -27,10 +25,6 @@ class GLSLException(Exception): cdef class Shader: - cdef GLuint handle - cdef bint linked - cdef dict location_cache - # vert and frag take arrays of source strings the arrays will be # concattenated into one string by OpenGL def __init__(self, vert=None, frag=None): @@ -120,17 +114,17 @@ cdef class Shader: self.location_cache[name] = loc return self.location_cache[name] - def bind(self): + cdef void bind(self) nogil: # bind the program glUseProgram(self.handle) # upload a floating point uniform # this program must be currently bound - def uniform_1(self, name, GLfloat val): + cdef void uniform_1(self, name, GLfloat val): glUniform1fv(self.get_uniform_location(name), 1, &val) # upload a vec4 uniform - def uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d): + cdef void uniform_4(self, name, GLfloat a, GLfloat b, GLfloat c, GLfloat d): cdef GLfloat vals[4] vals[0] = a vals[1] = b @@ -141,7 +135,7 @@ cdef class Shader: # upload a uniform matrix # works with matrices stored as lists, # as well as euclid matrices - def uniform_matrix(self, name, Matrix mat): + cdef void uniform_matrix(self, name, Matrix mat): # obtain the uniform location loc = self.get_uniform_location(name) # uplaod the 4x4 floating point matrix diff --git a/pytouhou/ui/window.pxd b/pytouhou/ui/window.pxd new file mode 100644 --- /dev/null +++ b/pytouhou/ui/window.pxd @@ -0,0 +1,25 @@ +from pytouhou.lib cimport sdl + + +cdef class Clock: + cdef long _target_fps, _ref_tick, _ref_frame, _fps_tick, _fps_frame + cdef double _rate + + cdef void set_target_fps(self, long fps) nogil + cdef double get_fps(self) nogil + cdef void tick(self) nogil except * + + +cdef class Window: + cdef sdl.Window win + cdef long fps_limit + cdef public long width, height + cdef public bint use_fixed_pipeline + cdef object runner + cdef Clock clock + + cdef void set_size(self, int width, int height) nogil + cpdef set_runner(self, runner=*) + cpdef run(self) + cdef bint run_frame(self) except? False + cpdef double get_fps(self) diff --git a/pytouhou/ui/window.pyx b/pytouhou/ui/window.pyx --- a/pytouhou/ui/window.pyx +++ b/pytouhou/ui/window.pyx @@ -13,8 +13,6 @@ ## -from pytouhou.lib cimport sdl - from pytouhou.lib.opengl cimport \ (glEnable, glHint, glEnableClientState, GL_TEXTURE_2D, GL_BLEND, GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, @@ -25,9 +23,6 @@ IF USE_GLEW: cdef class Clock: - cdef long _target_fps, _ref_tick, _ref_frame, _fps_tick, _fps_frame - cdef double _rate - def __init__(self, long fps=-1): self._target_fps = 0 self._ref_tick = 0 @@ -80,14 +75,7 @@ cdef class Clock: cdef class Window: - cdef sdl.Window win - cdef long fps_limit - cdef public long width, height - cdef public bint use_fixed_pipeline - cdef object runner - cdef Clock clock - - def __init__(self, size=None, bint double_buffer=True, long fps_limit=-1, + def __init__(self, tuple size=None, bint double_buffer=True, long fps_limit=-1, bint fixed_pipeline=False, bint sound=True): self.fps_limit = fps_limit self.use_fixed_pipeline = fixed_pipeline