@@ -69,7 +69,7 @@ typedef struct
6969
7070 struct
7171 {
72- sg_image image ;
72+ sg_image screen ;
7373 sg_shader shader ;
7474 sgl_context ctx ;
7575 sgl_pipeline pip ;
@@ -82,8 +82,9 @@ typedef struct
8282 } player ;
8383
8484 sgl_pipeline alphapip ;
85- sg_image image ;
86- sg_image fontmap ;
85+ sg_image screen ;
86+ sg_image sprites ;
87+ sg_image tiles ;
8788 sg_sampler linear ;
8889 sg_sampler nearest ;
8990
@@ -478,6 +479,27 @@ static Clay_Dimensions measureText(Clay_StringSlice text, Clay_TextElementConfig
478479 };
479480}
480481
482+ static sg_image initTiles (App * app , const tic_tile * src )
483+ {
484+ u32 sprites [TIC_SPRITESHEET_SIZE * TIC_SPRITESHEET_SIZE ];
485+
486+ tic_blitpal pal = tic_tool_palette_blit (& studio_config (app -> studio )-> cart -> bank0 .palette .vbank0 ,
487+ TIC80_PIXEL_COLOR_RGBA8888 );
488+
489+ for (s32 i = 0 ; i < TIC_SPRITESHEET_SIZE * TIC_SPRITESHEET_SIZE ; i ++ )
490+ {
491+ u8 color = getSpritePixel (src , i % TIC_SPRITESHEET_SIZE , i / TIC_SPRITESHEET_SIZE );
492+ sprites [i ] = color ? pal .data [color ] : 0 ;
493+ }
494+
495+ return sg_make_image (& (sg_image_desc )
496+ {
497+ .width = TIC_SPRITESHEET_SIZE ,
498+ .height = TIC_SPRITESHEET_SIZE ,
499+ .data .subimage [0 ][0 ] = SG_RANGE (sprites ),
500+ });
501+ }
502+
481503static void init (void * userdata )
482504{
483505 App * app = userdata ;
@@ -499,29 +521,15 @@ static void init(void *userdata)
499521#endif
500522 });
501523
502- app -> image = sg_make_image (& (sg_image_desc )
524+ app -> screen = sg_make_image (& (sg_image_desc )
503525 {
504526 .width = TIC80_FULLWIDTH ,
505527 .height = TIC80_FULLHEIGHT ,
506528 .usage .stream_update = true,
507529 });
508530
509- u32 font [TIC_SPRITESHEET_SIZE * TIC_SPRITESHEET_SIZE ];
510-
511- for (s32 i = 0 ; i < TIC_SPRITESHEET_SIZE * TIC_SPRITESHEET_SIZE ; i ++ )
512- {
513- u8 color = getSpritePixel (studio_config (app -> studio )-> cart -> bank0 .sprites .data ,
514- i % TIC_SPRITESHEET_SIZE , i / TIC_SPRITESHEET_SIZE );
515-
516- font [i ] = color ? 0xffffffff : 0 ;
517- }
518-
519- app -> fontmap = sg_make_image (& (sg_image_desc )
520- {
521- .width = TIC_SPRITESHEET_SIZE ,
522- .height = TIC_SPRITESHEET_SIZE ,
523- .data .subimage [0 ][0 ] = SG_RANGE (font ),
524- });
531+ app -> tiles = initTiles (app , studio_config (app -> studio )-> cart -> bank0 .tiles .data );
532+ app -> sprites = initTiles (app , studio_config (app -> studio )-> cart -> bank0 .sprites .data );
525533
526534 app -> linear = sg_make_sampler (& (sg_sampler_desc )
527535 {
@@ -550,7 +558,7 @@ static void init(void *userdata)
550558 .sample_count = 1 ,
551559 });
552560
553- app -> crt .image = sg_make_image (& (sg_image_desc )
561+ app -> crt .screen = sg_make_image (& (sg_image_desc )
554562 {
555563 .usage .render_attachment = true,
556564 .width = TIC80_FULLWIDTH * CRT_SCALE ,
@@ -561,7 +569,7 @@ static void init(void *userdata)
561569
562570 app -> crt .att = sg_make_attachments (& (sg_attachments_desc )
563571 {
564- .colors [0 ].image = app -> crt .image ,
572+ .colors [0 ].image = app -> crt .screen ,
565573 });
566574
567575 app -> crt .shader = sg_make_shader (crt_shader_desc (sg_query_backend ()));
@@ -682,7 +690,7 @@ static void playerCommand(App *app, Clay_BoundingBox b)
682690 bool crt = studio_config (app -> studio )-> options .crt ;
683691 if (crt )
684692 {
685- renderImage (b , app -> crt .image , app -> linear );
693+ renderImage (b , app -> crt .screen , app -> linear );
686694
687695 // draw crt
688696 sg_begin_pass (& (sg_pass )
@@ -695,7 +703,7 @@ static void playerCommand(App *app, Clay_BoundingBox b)
695703 }
696704 else
697705 {
698- renderImage (b , app -> image , app -> nearest );
706+ renderImage (b , app -> screen , app -> nearest );
699707 }
700708}
701709
@@ -748,10 +756,27 @@ static void textCommand(App *app, Clay_BoundingBox b, Clay_TextRenderData data)
748756 (c % TIC_SPRITESHEET_COLS ) * TIC_SPRITESIZE , (c / TIC_SPRITESHEET_COLS ) * TIC_SPRITESIZE ,
749757 TIC_SPRITESIZE , TIC_SPRITESIZE
750758 },
751- app -> fontmap , app -> nearest );
759+ app -> sprites , app -> nearest );
752760 }
753761}
754762
763+ static void imageCommand (App * app , Clay_BoundingBox b , Clay_ImageRenderData data )
764+ {
765+ Clay_Color c = data .backgroundColor ;
766+ sgl_c4b (c .r , c .g , c .b , c .a );
767+
768+ // 8x8 tile index
769+ s32 t = (s32 )(intptr_t )data .imageData ;
770+
771+ renderTile (b ,
772+ (Clay_BoundingBox )
773+ {
774+ (t % TIC_SPRITESHEET_COLS ) * TIC_SPRITESIZE , (t / TIC_SPRITESHEET_COLS ) * TIC_SPRITESIZE ,
775+ TIC_SPRITESIZE , TIC_SPRITESIZE
776+ },
777+ app -> tiles , app -> nearest );
778+ }
779+
755780static void renderCommands (App * app , Clay_RenderCommandArray renderCommands )
756781{
757782 sgl_set_context (sgl_default_context ());
@@ -798,6 +823,12 @@ static void renderCommands(App *app, Clay_RenderCommandArray renderCommands)
798823 }
799824 break ;
800825
826+ case CLAY_RENDER_COMMAND_TYPE_IMAGE :
827+ {
828+ imageCommand (app , b , renderCommand -> renderData .image );
829+ }
830+ break ;
831+
801832 default :
802833 printf ("unhandled command: %i\n" , renderCommand -> commandType );
803834 break ;
@@ -807,6 +838,7 @@ static void renderCommands(App *app, Clay_RenderCommandArray renderCommands)
807838
808839static void renderLayout (App * app )
809840{
841+ const Clay_Color COLOR_WHITE = (Clay_Color ) {.r = 255 , .g = 255 , .b = 255 , .a = 255 };
810842 const Clay_Color COLOR_BLACK = (Clay_Color ) {.a = 255 };
811843 const Clay_Color COLOR_RED = (Clay_Color ) {.a = 255 , .r = 255 };
812844 const Clay_Color COLOR_BLUE = (Clay_Color ) {.a = 255 , .b = 255 };
@@ -864,13 +896,15 @@ static void renderLayout(App *app)
864896 {
865897 CLAY_AUTO_ID (
866898 {
899+ // tile index of pad buttons
900+ .image = (void * )8 + i ,
901+
867902 .layout =
868903 {
869904 .sizing = {CLAY_SIZING_FIXED (100 ), CLAY_SIZING_FIXED (100 )},
870905 },
871906
872- .backgroundColor = COLOR_RED ,
873- .border = { .width = { 16 , 16 , 16 , 16 }, .color = COLOR_BLUE },
907+ .backgroundColor = COLOR_WHITE ,
874908 }){}
875909 }
876910 }
@@ -889,13 +923,15 @@ static void renderLayout(App *app)
889923 {
890924 CLAY_AUTO_ID (
891925 {
926+ // tile index of ABXY buttons
927+ .image = (void * )12 + i ,
928+
892929 .layout =
893930 {
894931 .sizing = {CLAY_SIZING_FIXED (100 ), CLAY_SIZING_FIXED (100 )},
895932 },
896933
897- .backgroundColor = COLOR_BLUE ,
898- .border = { .width = { 16 , 16 , 16 , 16 }, .color = COLOR_RED },
934+ .backgroundColor = COLOR_WHITE ,
899935 }){}
900936 }
901937 }
@@ -935,7 +971,7 @@ static void frame(void *userdata)
935971 {
936972 const tic80 * product = & studio_mem (app -> studio )-> product ;
937973
938- sg_update_image (app -> image , & (sg_image_data )
974+ sg_update_image (app -> screen , & (sg_image_data )
939975 {
940976 .subimage [0 ][0 ] =
941977 {
@@ -961,7 +997,7 @@ static void frame(void *userdata)
961997 TIC80_FULLWIDTH * CRT_SCALE ,
962998 TIC80_FULLHEIGHT * CRT_SCALE ,
963999 },
964- app -> image , app -> nearest );
1000+ app -> screen , app -> nearest );
9651001 }
9661002
9671003 renderLayout (app );
@@ -1270,11 +1306,13 @@ static void cleanup(void *userdata)
12701306 sgl_destroy_pipeline (app -> crt .pip );
12711307 sg_destroy_shader (app -> crt .shader );
12721308 sg_destroy_attachments (app -> crt .att );
1273- sg_destroy_image (app -> crt .image );
1309+ sg_destroy_image (app -> crt .screen );
1310+ sg_destroy_image (app -> tiles );
1311+ sg_destroy_image (app -> sprites );
12741312 sgl_destroy_context (app -> crt .ctx );
12751313 }
12761314
1277- sg_destroy_image (app -> image );
1315+ sg_destroy_image (app -> screen );
12781316 sg_destroy_sampler (app -> linear );
12791317 sg_destroy_sampler (app -> nearest );
12801318
0 commit comments