@@ -462,54 +462,91 @@ fn loadTextset() bool {
462
462
return success ;
463
463
}
464
464
465
- fn loadTileset (path : [:0 ]const u8 , origin : ? * c.SDL_Texture ) bool {
466
- if (origin == null ) {
467
- @panic ("origin should never be null!" );
465
+ fn initTilesetLine (line : []const u8 , origin : ? * c.SDL_Texture ) ! void {
466
+ // Skip over empty lines.
467
+ if (std .mem .trim (u8 , line , " " ).len == 0 ) {
468
+ return ;
468
469
}
469
470
470
- const file = c .fopen (path , "r" );
471
- defer _ = c .fclose (file );
471
+ // Skip over comment lines.
472
+ if (std .mem .startsWith (u8 , line , "#" )) {
473
+ return ;
474
+ }
472
475
473
- if (file == null ) {
474
- std .log .err ("Couldn't find file at path: {s}" , .{path });
475
- return false ;
476
+ var seq = std .mem .splitSequence (u8 , line , " " );
477
+ const a0 = seq .next ();
478
+ const b0 = seq .next ();
479
+ const c0 = seq .next ();
480
+ const d0 = seq .next ();
481
+ const e0 = seq .next ();
482
+ const f0 = seq .next ();
483
+
484
+ // Stupid sanity check.
485
+ if (a0 == null or b0 == null or c0 == null or d0 == null or e0 == null or f0 == null ) {
486
+ @panic ("Expected a tileset line item to have 6 fields defined!" );
476
487
}
477
488
478
- var x : c_int = undefined ;
479
- var y : c_int = undefined ;
480
- var w : c_int = undefined ;
481
- var h : c_int = undefined ;
482
- var f : c_int = undefined ;
489
+ const name = a0 .? ;
490
+ const x = try std .fmt .parseInt (c_int , b0 .? , 10 );
491
+ const y = try std .fmt .parseInt (c_int , c0 .? , 10 );
492
+ const w = try std .fmt .parseInt (c_int , d0 .? , 10 );
493
+ const h = try std .fmt .parseInt (c_int , e0 .? , 10 );
494
+ // fc means frameCount
495
+ const fc = try std .fmt .parseInt (c_int , f0 .? , 10 );
496
+
497
+ const p = & textures [texturesCount ];
498
+ texturesCount += 1 ;
499
+ tps .initTexture (p , origin .? , w , h , fc );
500
+
501
+ var i : usize = 0 ;
502
+ while (i < fc ) : (i += 1 ) {
503
+ p .crops [i ].x = x + @as (c_int , @intCast (i )) * w ;
504
+ p .crops [i ].y = y ;
505
+ p .crops [i ].h = h ;
506
+ p .crops [i ].w = w ;
507
+ }
483
508
484
- var resName : [256 ]u8 = undefined ;
509
+ // Don't delete this: in debug mode, this line is useful to debug textureIds.
510
+ std .log .debug ("Texture Res: {d}). name: {s}, ptr:{*}, x:{d}, y:{d}, w:{d}, h:{d}, fc:{d}" , .{
511
+ texturesCount - 1 ,
512
+ name ,
513
+ p ,
514
+ x ,
515
+ y ,
516
+ w ,
517
+ h ,
518
+ fc ,
519
+ });
520
+ }
485
521
486
- // Convention of tileset: name, x, y, w, h, f (num of frames)
487
- var count : usize = 0 ;
488
- while (c .fscanf (file , "%s %d %d %d %d %d" , & resName , & x , & y , & w , & h , & f ) == 6 ) {
489
- const p = & textures [texturesCount ];
490
- texturesCount += 1 ;
491
- tps .initTexture (p , origin .? , w , h , f );
492
-
493
- var i : usize = 0 ;
494
- while (i < f ) : (i += 1 ) {
495
- p .crops [i ].x = x + @as (c_int , @intCast (i )) * w ;
496
- p .crops [i ].y = y ;
497
- p .crops [i ].h = h ;
498
- p .crops [i ].w = w ;
499
- }
522
+ fn loadTileset (path : [:0 ]const u8 , origin : ? * c.SDL_Texture ) ! bool {
523
+ if (origin == null ) {
524
+ @panic ("origin should never be null!" );
525
+ }
500
526
501
- std .log .debug ("Texture Res: {d}). name: {s}, ptr:{*}, x:{d}, y:{d}, w:{d}, h:{d}, f:{d}" , .{
502
- texturesCount - 1 ,
503
- std .mem .sliceTo (& resName , 0 ),
504
- p ,
505
- x ,
506
- y ,
507
- w ,
508
- h ,
509
- f ,
510
- });
511
- count += 1 ;
527
+ const file = try std .fs .cwd ().openFile (path , .{});
528
+ defer file .close ();
529
+
530
+ var bfReader = std .io .bufferedReader (file .reader ());
531
+ const reader = bfReader .reader ();
532
+
533
+ var line = std .ArrayList (u8 ).init (gAllocator );
534
+ defer line .deinit ();
535
+
536
+ const writer = line .writer ();
537
+ while (reader .streamUntilDelimiter (writer , '\n ' , null )) {
538
+ defer line .clearRetainingCapacity ();
539
+ try initTilesetLine (line .items , origin );
540
+ } else | err | switch (err ) {
541
+ error .EndOfStream = > {
542
+ // Don't forget the last line too!
543
+ defer line .clearRetainingCapacity ();
544
+ try initTilesetLine (line .items , origin );
545
+ std .log .debug ("end of stream found!" , .{});
546
+ },
547
+ else = > return err ,
512
548
}
549
+
513
550
return true ;
514
551
}
515
552
@@ -563,7 +600,7 @@ pub fn loadMedia(exePath: []const u8) !bool {
563
600
const imgFile = try std .fmt .bufPrintZ (& buf , "{s}.png" , .{imgPath });
564
601
originTextures [idx ] = loadSDLTexture (std .mem .sliceTo (imgFile , 0 ));
565
602
566
- _ = loadTileset (imgPath , originTextures [idx ]);
603
+ _ = try loadTileset (imgPath , originTextures [idx ]);
567
604
if (originTextures [idx ] == null ) {
568
605
return false ;
569
606
}
0 commit comments