Skip to content

Commit 863494b

Browse files
committed
clay: draw images
1 parent b7e935b commit 863494b

File tree

2 files changed

+71
-66
lines changed

2 files changed

+71
-66
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -368,39 +368,6 @@ jobs:
368368
name: "tic80-nintendo-3ds"
369369
path: build/bin/tic80.3dsx
370370

371-
# === MacOS 13 ===
372-
macos:
373-
runs-on: macos-13
374-
375-
steps:
376-
- uses: actions/checkout@v4
377-
with:
378-
submodules: recursive
379-
fetch-depth: 0
380-
381-
- name: Install
382-
run: brew uninstall --ignore-dependencies libidn2
383-
384-
- name: Build
385-
run: |
386-
cd build
387-
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_IPO=ON -DBUILD_WITH_ALL=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
388-
cmake --build . --parallel
389-
390-
- name: Deploy
391-
uses: actions/upload-artifact@v4
392-
with:
393-
name: "tic80-macos"
394-
path: |
395-
build/bin/tic80
396-
build/bin/*.dylib
397-
398-
- name: Build Pro
399-
run: |
400-
cd build
401-
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_IPO=ON -DBUILD_PRO=ON -DBUILD_WITH_ALL=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
402-
cmake --build . --parallel
403-
404371
# === MacOS 14 / arm64 ===
405372
macos-arm:
406373
runs-on: macos-14

src/system/sokol/main.c

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
481503
static 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+
755780
static 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

808839
static 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

Comments
 (0)