Hide keyboard shortcuts

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 

2 

3c"""header-before-namespace 

4#include <SDL.h> 

5""" 

6 

7c"""source-before-namespace 

8#include <SDL_ttf.h> 

9#include <SDL_image.h> 

10#include <SDL_mixer.h> 

11""" 

12 

13class Rect: 

14 x: i64 

15 y: i64 

16 w: i64 

17 h: i64 

18 

19class Color: 

20 r: u8 

21 g: u8 

22 b: u8 

23 a: u8 

24 

25class SdlError(Error): 

26 message: string 

27 

28class Window: 

29 c"SDL_Window *window_p;" 

30 title: bytes 

31 

32 func __init__(self): 

33 pass 

34 

35 func set_title(self, title: string): 

36 self.title = title.to_utf8() 

37 self.title += 0 

38 

39 c""" 

40 SDL_SetWindowTitle(this->window_p, 

41 (const char *)this->title.m_bytes->data()); 

42 """ 

43 

44class Renderer: 

45 c"SDL_Renderer *renderer_p;" 

46 

47 func create_texture(self, width: i64, height: i64) -> Texture: 

48 texture = Texture() 

49 res = 0 

50 

51 c""" 

52 texture->texture_p = SDL_CreateTexture(this->renderer_p, 

53 SDL_PIXELFORMAT_ARGB8888, 

54 SDL_TEXTUREACCESS_STREAMING, 

55 width, 

56 height); 

57 

58 if (texture->texture_p == NULL) { 

59 res = 1; 

60 } 

61 """ 

62 

63 if res != 0: 

64 raise SdlError("create_texture()") 

65 

66 return texture 

67 

68 # ToDo: missing return value does not work. 

69 func create_texture_from_surface(self, surface: Surface) -> Texture: 

70 texture = Texture() 

71 res = 0 

72 

73 c""" 

74 texture->texture_p = SDL_CreateTextureFromSurface( 

75 this->renderer_p, 

76 surface->surface_p); 

77 

78 if (texture->texture_p == NULL) { 

79 res = 1; 

80 } 

81 """ 

82 

83 if res != 0: 

84 raise SdlError("create_texture_from_surface()") 

85 

86 return texture 

87 

88 func clear(self): 

89 c"SDL_RenderClear(this->renderer_p);" 

90 

91 func copy(self, texture: Texture, dst_rect: Rect?): 

92 c""" 

93 SDL_Rect dstrect; 

94 SDL_Rect *dstrect_p = NULL; 

95 

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 """ 

108 

109 func present(self): 

110 c"SDL_RenderPresent(this->renderer_p);" 

111 

112class Surface: 

113 c"SDL_Surface *surface_p;" 

114 

115 func __del__(self): 

116 c"SDL_FreeSurface(this->surface_p);" 

117 

118 func map_rgb(self, r: u8, g: u8, b: u8) -> u32: 

119 res: u32 = 0 

120 

121 c"res = SDL_MapRGB(this->surface_p->format, r, g, b);" 

122 

123 return res 

124 

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 """ 

134 

135class Texture: 

136 c"SDL_Texture *texture_p;" 

137 

138 func __del__(self): 

139 c"SDL_DestroyTexture(this->texture_p);" 

140 

141 func query(self) -> (i64, i64): 

142 width = 0 

143 height = 0 

144 

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 """ 

152 

153 return (width, height) 

154 

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 """ 

160 

161func sdl_init(): 

162 res = 0 

163 

164 c"res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);" 

165 

166 if res != 0: 

167 raise SdlError("sdl_init()") 

168 

169func ttf_init(): 

170 c"TTF_Init();" 

171 

172func mix_init(): 

173 c"Mix_Init(0);" 

174 

175func open_audio(frequency: i64, channels: i64, chunksize: i64): 

176 res = 0 

177 

178 c"Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, channels, chunksize);" 

179 

180 if res != 0: 

181 raise SdlError("Mix_OpenAudio()") 

182 

183func create_window_and_renderer(width: i64, 

184 height: i64, 

185 flags: u32) -> (Window, Renderer): 

186 window = Window() 

187 renderer = Renderer() 

188 res = 0 

189 

190 c""" 

191 res = SDL_CreateWindowAndRenderer(width, 

192 height, 

193 flags, 

194 (SDL_Window **)&window->window_p, 

195 (SDL_Renderer **)&renderer->renderer_p); 

196 """ 

197 

198 if res != 0: 

199 raise SdlError("create_window_and_renderer()") 

200 

201 return window, renderer 

202 

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 

213 

214 c""" 

215 surface->surface_p = SDL_CreateRGBSurface(flags, 

216 width, 

217 height, 

218 bpp, 

219 rmask, 

220 gmask, 

221 bmask, 

222 amask); 

223 

224 if (surface->surface_p == NULL) { 

225 res = 1; 

226 } 

227 """ 

228 

229 if res != 0: 

230 raise SdlError("create_rgb_surface()") 

231 

232 return surface 

233 

234func img_init(): 

235 res = 0 

236 

237 c""" 

238 if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) { 

239 res = 1; 

240 } 

241 """ 

242 

243 if res != 0: 

244 raise SdlError("img_init()") 

245 

246func img_load(path: Path) -> Surface: 

247 surface = Surface() 

248 res = 0 

249 path_utf8 = path.to_string().to_utf8() 

250 path_utf8 += 0 

251 

252 c""" 

253 surface->surface_p = IMG_Load((const char *)path_utf8.m_bytes->data()); 

254 

255 if (surface->surface_p == NULL) { 

256 res = 1; 

257 } 

258 """ 

259 

260 if res != 0: 

261 raise SdlError("img_load()") 

262 

263 return surface 

264 

265trait Event: 

266 pass 

267 

268class QuitEvent(Event): 

269 pass 

270 

271class KeyDownEvent(Event): 

272 keysym: KeySym 

273 

274class KeySym: 

275 sym: KeyCode 

276 

277func poll_event() -> Event?: 

278 res = 0 

279 key_code = 0 

280 

281 c""" 

282 SDL_Event event; 

283 

284 res = SDL_PollEvent(&event); 

285 """ 

286 

287 if res == 0: 

288 return None 

289 

290 c""" 

291 switch (event.type) { 

292 

293 case SDL_QUIT: 

294 { 

295 """ 

296 

297 return QuitEvent() 

298 

299 c""" 

300 } 

301 

302 case SDL_KEYDOWN: 

303 { 

304 key_code = event.key.keysym.sym; 

305 """ 

306 

307 try: 

308 return KeyDownEvent(KeySym(KeyCode(key_code))) 

309 except ValueError: 

310 return None 

311 

312 c""" 

313 } 

314 }""" 

315 

316 return None 

317 

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" 

327 

328func get_performance_counter() -> u64: 

329 count: u64 = 0 

330 

331 c"count = SDL_GetPerformanceCounter();" 

332 

333 return count 

334 

335func get_performance_frequency() -> u64: 

336 frequency: u64 = 0 

337 

338 c"frequency = SDL_GetPerformanceFrequency();" 

339 

340 return frequency 

341 

342class Font: 

343 c"void *font_p;" 

344 

345 func __init__(self, path: Path, size: i64): 

346 res = 0 

347 path_utf8 = path.to_string().to_utf8() 

348 path_utf8 += 0 

349 

350 c""" 

351 this->font_p = TTF_OpenFont((const char *)path_utf8.m_bytes->data(), size); 

352 

353 if (this->font_p == NULL) { 

354 res = 1; 

355 } 

356 """ 

357 

358 if res != 0: 

359 raise SdlError("Font()") 

360 

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 

366 

367 c""" 

368 SDL_Color c_color; 

369 

370 c_color.r = color->r; 

371 c_color.g = color->g; 

372 c_color.b = color->b; 

373 c_color.a = color->a; 

374 

375 surface->surface_p = TTF_RenderText_Solid( 

376 (TTF_Font *)this->font_p, 

377 (const char *)text_utf8.m_bytes->data(), 

378 c_color); 

379 

380 if (surface->surface_p == NULL) { 

381 res = 1; 

382 } 

383 """ 

384 

385 if res != 0: 

386 raise SdlError("img_load()") 

387 

388 return surface 

389 

390class Music: 

391 c"void *music_p;" 

392 

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 

398 

399 c""" 

400 this->music_p = Mix_LoadMUS((const char *)path_utf8.m_bytes->data()); 

401 

402 if (this->music_p == NULL) { 

403 message = String((char *)Mix_GetError()); 

404 res = 1; 

405 } 

406 """ 

407 

408 if res != 0: 

409 raise SdlError(message) 

410 

411 func play(self, loops: i64): 

412 res = 0 

413 

414 c""" 

415 res = Mix_PlayMusic((Mix_Music *)this->music_p, loops); 

416 """ 

417 

418 if res != 0: 

419 raise SdlError("play music") 

420 

421class Wav: 

422 c"void *wav_p;" 

423 

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 

429 

430 c""" 

431 this->wav_p = Mix_LoadWAV((const char *)path_utf8.m_bytes->data()); 

432 

433 if (this->wav_p == NULL) { 

434 message = String((char *)Mix_GetError()); 

435 res = 1; 

436 } 

437 """ 

438 

439 if res != 0: 

440 raise SdlError(message) 

441 

442 func play(self, channel: i64, loops: i64): 

443 res = 0 

444 

445 c""" 

446 res = Mix_PlayChannel(channel, (Mix_Chunk *)this->wav_p, loops); 

447 """ 

448 

449 if res == -1: 

450 raise SdlError("play channel") 

451 

452func volume_music(): 

453 c"Mix_VolumeMusic(SDL_MIX_MAXVOLUME / 16);"