comparison pytouhou/lib/sdl.pyx @ 512:b39ad30c6620

Add a pure SDL backend.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 05 Dec 2013 01:55:39 +0100
parents 4778c482f24a
children 7c3c90468996
comparison
equal deleted inserted replaced
511:2e8ceaa85d5c 512:b39ad30c6620
22 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER 22 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER
23 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE 23 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE
24 24
25 WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED 25 WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED
26 WINDOW_OPENGL = SDL_WINDOW_OPENGL 26 WINDOW_OPENGL = SDL_WINDOW_OPENGL
27 WINDOW_SHOWN = SDL_WINDOW_SHOWN
28 WINDOW_RESIZABLE = SDL_WINDOW_RESIZABLE 27 WINDOW_RESIZABLE = SDL_WINDOW_RESIZABLE
29 28
30 SCANCODE_Z = SDL_SCANCODE_Z 29 SCANCODE_Z = SDL_SCANCODE_Z
31 SCANCODE_X = SDL_SCANCODE_X 30 SCANCODE_X = SDL_SCANCODE_X
32 SCANCODE_LSHIFT = SDL_SCANCODE_LSHIFT 31 SCANCODE_LSHIFT = SDL_SCANCODE_LSHIFT
94 cdef void gl_create_context(self) except *: 93 cdef void gl_create_context(self) except *:
95 self.context = SDL_GL_CreateContext(self.window) 94 self.context = SDL_GL_CreateContext(self.window)
96 if self.context == NULL: 95 if self.context == NULL:
97 raise SDLError(SDL_GetError()) 96 raise SDLError(SDL_GetError())
98 97
99 cdef void gl_swap_window(self) nogil: 98 cdef void present(self) nogil:
100 SDL_GL_SwapWindow(self.window) 99 if self.renderer == NULL:
100 SDL_GL_SwapWindow(self.window)
101 else:
102 SDL_RenderPresent(self.renderer)
101 103
102 cdef void set_window_size(self, int width, int height) nogil: 104 cdef void set_window_size(self, int width, int height) nogil:
103 SDL_SetWindowSize(self.window, width, height) 105 SDL_SetWindowSize(self.window, width, height)
106
107 # The following functions are there for the pure SDL backend.
108 cdef void create_renderer(self, Uint32 flags):
109 self.renderer = SDL_CreateRenderer(self.window, -1, flags)
110 if self.renderer == NULL:
111 raise SDLError(SDL_GetError())
112
113 cdef void render_clear(self):
114 ret = SDL_RenderClear(self.renderer)
115 if ret == -1:
116 raise SDLError(SDL_GetError())
117
118 cdef void render_copy(self, Texture texture, Rect srcrect, Rect dstrect):
119 ret = SDL_RenderCopy(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect)
120 if ret == -1:
121 raise SDLError(SDL_GetError())
122
123 cdef void render_copy_ex(self, Texture texture, Rect srcrect, Rect dstrect, double angle, bint flip):
124 ret = SDL_RenderCopyEx(self.renderer, texture.texture, &srcrect.rect, &dstrect.rect, angle, NULL, flip)
125 if ret == -1:
126 raise SDLError(SDL_GetError())
127
128 cdef void render_set_clip_rect(self, Rect rect):
129 ret = SDL_RenderSetClipRect(self.renderer, &rect.rect)
130 if ret == -1:
131 raise SDLError(SDL_GetError())
132
133 cdef void render_set_viewport(self, Rect rect):
134 ret = SDL_RenderSetViewport(self.renderer, &rect.rect)
135 if ret == -1:
136 raise SDLError(SDL_GetError())
137
138 cdef Texture create_texture_from_surface(self, Surface surface):
139 texture = Texture()
140 texture.texture = SDL_CreateTextureFromSurface(self.renderer, surface.surface)
141 if texture.texture == NULL:
142 raise SDLError(SDL_GetError())
143 return texture
144
145
146 cdef class Texture:
147 cpdef set_color_mod(self, Uint8 r, Uint8 g, Uint8 b):
148 ret = SDL_SetTextureColorMod(self.texture, r, g, b)
149 if ret == -1:
150 raise SDLError(SDL_GetError())
151
152 cpdef set_alpha_mod(self, Uint8 alpha):
153 ret = SDL_SetTextureAlphaMod(self.texture, alpha)
154 if ret == -1:
155 raise SDLError(SDL_GetError())
156
157 cpdef set_blend_mode(self, SDL_BlendMode blend_mode):
158 ret = SDL_SetTextureBlendMode(self.texture, blend_mode)
159 if ret == -1:
160 raise SDLError(SDL_GetError())
161
162
163 cdef class Rect:
164 def __init__(self, int x, int y, int w, int h):
165 self.rect.x = x
166 self.rect.y = y
167 self.rect.w = w
168 self.rect.h = h
169
170
171 cdef class Color:
172 def __init__(self, Uint8 b, Uint8 g, Uint8 r, Uint8 a=255):
173 self.color.r = r
174 self.color.g = g
175 self.color.b = b
176 self.color.a = a
104 177
105 178
106 cdef class Surface: 179 cdef class Surface:
107 def __dealloc__(self): 180 def __dealloc__(self):
108 if self.surface != NULL: 181 if self.surface != NULL: