Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from os.path import Path
3c"""header-before-namespace
4#include <SDL.h>
5"""
7c"""source-before-namespace
8#include <SDL_ttf.h>
9#include <SDL_image.h>
10#include <SDL_mixer.h>
11"""
13class Rect:
14 x: i64
15 y: i64
16 w: i64
17 h: i64
19class Color:
20 r: u8
21 g: u8
22 b: u8
23 a: u8
25class SdlError(Error):
26 message: string
28class Window:
29 c"SDL_Window *window_p;"
30 title: bytes
32 func __init__(self):
33 pass
35 func set_title(self, title: string):
36 self.title = title.to_utf8()
37 self.title += 0
39 c"""
40 SDL_SetWindowTitle(this->window_p,
41 (const char *)this->title.m_bytes->data());
42 """
44class Renderer:
45 c"SDL_Renderer *renderer_p;"
47 func create_texture(self, width: i64, height: i64) -> Texture:
48 texture = Texture()
49 res = 0
51 c"""
52 texture->texture_p = SDL_CreateTexture(this->renderer_p,
53 SDL_PIXELFORMAT_ARGB8888,
54 SDL_TEXTUREACCESS_STREAMING,
55 width,
56 height);
58 if (texture->texture_p == NULL) {
59 res = 1;
60 }
61 """
63 if res != 0:
64 raise SdlError("create_texture()")
66 return texture
68 # ToDo: missing return value does not work.
69 func create_texture_from_surface(self, surface: Surface) -> Texture:
70 texture = Texture()
71 res = 0
73 c"""
74 texture->texture_p = SDL_CreateTextureFromSurface(
75 this->renderer_p,
76 surface->surface_p);
78 if (texture->texture_p == NULL) {
79 res = 1;
80 }
81 """
83 if res != 0:
84 raise SdlError("create_texture_from_surface()")
86 return texture
88 func clear(self):
89 c"SDL_RenderClear(this->renderer_p);"
91 func copy(self, texture: Texture, dst_rect: Rect?):
92 c"""
93 SDL_Rect dstrect;
94 SDL_Rect *dstrect_p = NULL;
96 if (dst_rect) {
97 dstrect.x = dst_rect->x;
98 dstrect.y = dst_rect->y;
99 dstrect.w = dst_rect->w;
100 dstrect.h = dst_rect->h;
101 dstrect_p = &dstrect;
102 }
103 SDL_RenderCopy(this->renderer_p,
104 texture->texture_p,
105 NULL,
106 dstrect_p);
107 """
109 func present(self):
110 c"SDL_RenderPresent(this->renderer_p);"
112class Surface:
113 c"SDL_Surface *surface_p;"
115 func __del__(self):
116 c"SDL_FreeSurface(this->surface_p);"
118 func map_rgb(self, r: u8, g: u8, b: u8) -> u32:
119 res: u32 = 0
121 c"res = SDL_MapRGB(this->surface_p->format, r, g, b);"
123 return res
125 func fill_rect(self, rect: Rect, color: u32):
126 c"""
127 SDL_Rect c_rect;
128 c_rect.x = rect->x;
129 c_rect.y = rect->y;
130 c_rect.w = rect->w;
131 c_rect.h = rect->h;
132 SDL_FillRect(this->surface_p, &c_rect, color);
133 """
135class Texture:
136 c"SDL_Texture *texture_p;"
138 func __del__(self):
139 c"SDL_DestroyTexture(this->texture_p);"
141 func query(self) -> (i64, i64):
142 width = 0
143 height = 0
145 c"""
146 int w;
147 int h;
148 SDL_QueryTexture(this->texture_p, NULL, NULL, &w, &h);
149 width = w;
150 height = h;
151 """
153 return (width, height)
155 func update(self, surface: Surface):
156 c"""
157 SDL_Surface *surface_p = surface->surface_p;
158 SDL_UpdateTexture(this->texture_p, NULL, surface_p->pixels, surface_p->pitch);
159 """
161func sdl_init():
162 res = 0
164 c"res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);"
166 if res != 0:
167 raise SdlError("sdl_init()")
169func ttf_init():
170 c"TTF_Init();"
172func mix_init():
173 c"Mix_Init(0);"
175func open_audio(frequency: i64, channels: i64, chunksize: i64):
176 res = 0
178 c"Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, channels, chunksize);"
180 if res != 0:
181 raise SdlError("Mix_OpenAudio()")
183func create_window_and_renderer(width: i64,
184 height: i64,
185 flags: u32) -> (Window, Renderer):
186 window = Window()
187 renderer = Renderer()
188 res = 0
190 c"""
191 res = SDL_CreateWindowAndRenderer(width,
192 height,
193 flags,
194 (SDL_Window **)&window->window_p,
195 (SDL_Renderer **)&renderer->renderer_p);
196 """
198 if res != 0:
199 raise SdlError("create_window_and_renderer()")
201 return window, renderer
203func create_rgb_surface(flags: u32,
204 width: i64,
205 height: i64,
206 bpp: i64,
207 rmask: u32,
208 gmask: u32,
209 bmask: u32,
210 amask: u32) -> Surface:
211 surface = Surface()
212 res = 0
214 c"""
215 surface->surface_p = SDL_CreateRGBSurface(flags,
216 width,
217 height,
218 bpp,
219 rmask,
220 gmask,
221 bmask,
222 amask);
224 if (surface->surface_p == NULL) {
225 res = 1;
226 }
227 """
229 if res != 0:
230 raise SdlError("create_rgb_surface()")
232 return surface
234func img_init():
235 res = 0
237 c"""
238 if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
239 res = 1;
240 }
241 """
243 if res != 0:
244 raise SdlError("img_init()")
246func img_load(path: Path) -> Surface:
247 surface = Surface()
248 res = 0
249 path_utf8 = path.to_string().to_utf8()
250 path_utf8 += 0
252 c"""
253 surface->surface_p = IMG_Load((const char *)path_utf8.m_bytes->data());
255 if (surface->surface_p == NULL) {
256 res = 1;
257 }
258 """
260 if res != 0:
261 raise SdlError("img_load()")
263 return surface
265trait Event:
266 pass
268class QuitEvent(Event):
269 pass
271class KeyDownEvent(Event):
272 keysym: KeySym
274class KeySym:
275 sym: KeyCode
277func poll_event() -> Event?:
278 res = 0
279 key_code = 0
281 c"""
282 SDL_Event event;
284 res = SDL_PollEvent(&event);
285 """
287 if res == 0:
288 return None
290 c"""
291 switch (event.type) {
293 case SDL_QUIT:
294 {
295 """
297 return QuitEvent()
299 c"""
300 }
302 case SDL_KEYDOWN:
303 {
304 key_code = event.key.keysym.sym;
305 """
307 try:
308 return KeyDownEvent(KeySym(KeyCode(key_code)))
309 except ValueError:
310 return None
312 c"""
313 }
314 }"""
316 return None
318enum KeyCode:
319 Space = c"SDLK_SPACE"
320 L = c"SDLK_l"
321 X = c"SDLK_x"
322 Z = c"SDLK_z"
323 Right = c"SDLK_RIGHT"
324 Left = c"SDLK_LEFT"
325 Down = c"SDLK_DOWN"
326 Up = c"SDLK_UP"
328func get_performance_counter() -> u64:
329 count: u64 = 0
331 c"count = SDL_GetPerformanceCounter();"
333 return count
335func get_performance_frequency() -> u64:
336 frequency: u64 = 0
338 c"frequency = SDL_GetPerformanceFrequency();"
340 return frequency
342class Font:
343 c"void *font_p;"
345 func __init__(self, path: Path, size: i64):
346 res = 0
347 path_utf8 = path.to_string().to_utf8()
348 path_utf8 += 0
350 c"""
351 this->font_p = TTF_OpenFont((const char *)path_utf8.m_bytes->data(), size);
353 if (this->font_p == NULL) {
354 res = 1;
355 }
356 """
358 if res != 0:
359 raise SdlError("Font()")
361 func render_solid(self, text: string, color: Color) -> Surface:
362 text_utf8 = text.to_utf8()
363 text_utf8 += 0
364 surface = Surface()
365 res = 0
367 c"""
368 SDL_Color c_color;
370 c_color.r = color->r;
371 c_color.g = color->g;
372 c_color.b = color->b;
373 c_color.a = color->a;
375 surface->surface_p = TTF_RenderText_Solid(
376 (TTF_Font *)this->font_p,
377 (const char *)text_utf8.m_bytes->data(),
378 c_color);
380 if (surface->surface_p == NULL) {
381 res = 1;
382 }
383 """
385 if res != 0:
386 raise SdlError("img_load()")
388 return surface
390class Music:
391 c"void *music_p;"
393 func __init__(self, path: Path):
394 path_utf8 = path.to_string().to_utf8()
395 path_utf8 += 0
396 res = 0
397 message: string? = None
399 c"""
400 this->music_p = Mix_LoadMUS((const char *)path_utf8.m_bytes->data());
402 if (this->music_p == NULL) {
403 message = String((char *)Mix_GetError());
404 res = 1;
405 }
406 """
408 if res != 0:
409 raise SdlError(message)
411 func play(self, loops: i64):
412 res = 0
414 c"""
415 res = Mix_PlayMusic((Mix_Music *)this->music_p, loops);
416 """
418 if res != 0:
419 raise SdlError("play music")
421class Wav:
422 c"void *wav_p;"
424 func __init__(self, path: Path):
425 path_utf8 = path.to_string().to_utf8()
426 path_utf8 += 0
427 res = 0
428 message: string? = None
430 c"""
431 this->wav_p = Mix_LoadWAV((const char *)path_utf8.m_bytes->data());
433 if (this->wav_p == NULL) {
434 message = String((char *)Mix_GetError());
435 res = 1;
436 }
437 """
439 if res != 0:
440 raise SdlError(message)
442 func play(self, channel: i64, loops: i64):
443 res = 0
445 c"""
446 res = Mix_PlayChannel(channel, (Mix_Chunk *)this->wav_p, loops);
447 """
449 if res == -1:
450 raise SdlError("play channel")
452func volume_music():
453 c"Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 16);"