Mercurial > touhou
comparison pytouhou/lib/sdl.pyx @ 420:3a7b36324611
Replace Pyglet’s image loader with our SDL2_image-based one.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 16 Jul 2013 21:07:15 +0200 |
parents | 1c92721f8e49 |
children | b1248bab2d0f |
comparison
equal
deleted
inserted
replaced
419:1c92721f8e49 | 420:3a7b36324611 |
---|---|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 ## GNU General Public License for more details. | 12 ## GNU General Public License for more details. |
13 ## | 13 ## |
14 | 14 |
15 INIT_VIDEO = SDL_INIT_VIDEO | 15 INIT_VIDEO = SDL_INIT_VIDEO |
16 INIT_PNG = IMG_INIT_PNG | |
16 | 17 |
17 GL_CONTEXT_MAJOR_VERSION = SDL_GL_CONTEXT_MAJOR_VERSION | 18 GL_CONTEXT_MAJOR_VERSION = SDL_GL_CONTEXT_MAJOR_VERSION |
18 GL_CONTEXT_MINOR_VERSION = SDL_GL_CONTEXT_MINOR_VERSION | 19 GL_CONTEXT_MINOR_VERSION = SDL_GL_CONTEXT_MINOR_VERSION |
19 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER | 20 GL_DOUBLEBUFFER = SDL_GL_DOUBLEBUFFER |
20 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE | 21 GL_DEPTH_SIZE = SDL_GL_DEPTH_SIZE |
61 | 62 |
62 def gl_delete_context(self): | 63 def gl_delete_context(self): |
63 SDL_GL_DeleteContext(self.context) | 64 SDL_GL_DeleteContext(self.context) |
64 | 65 |
65 | 66 |
67 cdef class Surface: | |
68 cdef SDL_Surface *surface | |
69 | |
70 def __dealloc__(self): | |
71 if self.surface != NULL: | |
72 SDL_FreeSurface(self.surface) | |
73 | |
74 property width: | |
75 def __get__(self): | |
76 return self.surface.w | |
77 | |
78 property height: | |
79 def __get__(self): | |
80 return self.surface.h | |
81 | |
82 property pixels: | |
83 def __get__(self): | |
84 return bytes(self.surface.pixels[:self.surface.w * self.surface.h * 4]) | |
85 | |
86 def blit(self, Surface other): | |
87 if SDL_BlitSurface(other.surface, NULL, self.surface, NULL) < 0: | |
88 raise SDLError(SDL_GetError()) | |
89 | |
90 def set_alpha(self, Surface alpha_surface): | |
91 nb_pixels = self.surface.w * self.surface.h | |
92 image = self.surface.pixels | |
93 alpha = alpha_surface.surface.pixels | |
94 | |
95 for i in xrange(nb_pixels): | |
96 # Only use the red value, assume the others are equal. | |
97 image[3+4*i] = alpha[3*i] | |
98 | |
99 | |
66 def init(Uint32 flags): | 100 def init(Uint32 flags): |
67 if SDL_Init(flags) < 0: | 101 if SDL_Init(flags) < 0: |
68 raise SDLError(SDL_GetError()) | 102 raise SDLError(SDL_GetError()) |
69 | 103 |
70 | 104 |
105 def img_init(Uint32 flags): | |
106 if IMG_Init(flags) != flags: | |
107 raise SDLError(SDL_GetError()) | |
108 | |
109 | |
71 def quit(): | 110 def quit(): |
72 SDL_Quit() | 111 SDL_Quit() |
112 | |
113 | |
114 def img_quit(): | |
115 IMG_Quit() | |
73 | 116 |
74 | 117 |
75 def gl_set_attribute(SDL_GLattr attr, int value): | 118 def gl_set_attribute(SDL_GLattr attr, int value): |
76 if SDL_GL_SetAttribute(attr, value) < 0: | 119 if SDL_GL_SetAttribute(attr, value) < 0: |
77 raise SDLError(SDL_GetError()) | 120 raise SDLError(SDL_GetError()) |
94 cdef const Uint8 *state | 137 cdef const Uint8 *state |
95 state = SDL_GetKeyboardState(&numkeys) | 138 state = SDL_GetKeyboardState(&numkeys) |
96 return tuple([k is not False for k in state[:numkeys]]) | 139 return tuple([k is not False for k in state[:numkeys]]) |
97 | 140 |
98 | 141 |
142 def load_png(file_): | |
143 data = file_.read() | |
144 rwops = SDL_RWFromConstMem(<char*>data, len(data)) | |
145 surface = Surface() | |
146 surface.surface = IMG_LoadPNG_RW(rwops) | |
147 SDL_RWclose(rwops) | |
148 if surface.surface == NULL: | |
149 raise SDLError(SDL_GetError()) | |
150 return surface | |
151 | |
152 | |
153 def create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0): | |
154 surface = Surface() | |
155 surface.surface = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask) | |
156 if surface.surface == NULL: | |
157 raise SDLError(SDL_GetError()) | |
158 return surface | |
159 | |
160 | |
99 def get_ticks(): | 161 def get_ticks(): |
100 return SDL_GetTicks() | 162 return SDL_GetTicks() |
101 | 163 |
102 | 164 |
103 def delay(Uint32 ms): | 165 def delay(Uint32 ms): |