TIC-80-guile/examples/sokol/main.c

120 lines
2.5 KiB
C
Raw Normal View History

2018-09-28 17:18:54 +02:00
#include "system/sokol.h"
2018-09-20 11:39:34 +02:00
2018-09-20 15:04:59 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2018-09-24 21:09:23 +02:00
#include <limits.h>
2018-09-20 15:04:59 +02:00
#include <tic80.h>
2018-09-21 16:32:02 +02:00
static tic80* tic = NULL;
2018-09-20 15:04:59 +02:00
2018-09-20 11:39:34 +02:00
static void app_init(void)
{
2018-09-20 18:48:14 +02:00
saudio_desc desc = {0};
2018-09-24 21:09:23 +02:00
desc.num_channels = 2;
2018-09-20 18:48:14 +02:00
saudio_setup(&desc);
2018-09-20 15:04:59 +02:00
FILE* file = fopen("cart.tic", "rb");
if(file)
{
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, 0, SEEK_SET);
void* cart = malloc(size);
if(cart) fread(cart, size, 1, file);
fclose(file);
if(cart)
{
printf("%s\n", "cart loaded");
2018-09-20 18:48:14 +02:00
tic = tic80_create(saudio_sample_rate());
2018-09-20 11:39:34 +02:00
2018-09-20 15:04:59 +02:00
if(tic)
{
printf("%s\n", "tic created");
tic80_load(tic, cart, size);
}
}
}
2018-12-01 11:05:31 +01:00
sokol_gfx_init(TIC80_FULLWIDTH, TIC80_FULLHEIGHT, 1, 1, false, false);
2018-09-20 11:39:34 +02:00
}
2018-09-20 15:04:59 +02:00
static tic80_input tic_input;
2018-09-20 11:39:34 +02:00
static void app_frame(void)
{
2018-09-20 15:04:59 +02:00
if(tic)
tic80_tick(tic, tic_input);
2018-09-20 11:39:34 +02:00
2018-09-28 17:18:54 +02:00
sokol_gfx_draw(tic->screen);
2018-09-20 18:48:14 +02:00
2018-09-24 21:09:23 +02:00
static float floatSamples[44100 / 60 * 2];
2018-09-20 18:48:14 +02:00
2018-09-24 21:09:23 +02:00
for(s32 i = 0; i < tic->sound.count; i++)
floatSamples[i] = (float)tic->sound.samples[i] / SHRT_MAX;
2018-09-21 16:17:12 +02:00
saudio_push(floatSamples, tic->sound.count / 2);
2018-09-20 11:39:34 +02:00
}
static void app_input(const sapp_event* event)
{
2018-09-20 16:43:30 +02:00
static const sapp_keycode Keys[] =
{
SAPP_KEYCODE_UP,
SAPP_KEYCODE_DOWN,
SAPP_KEYCODE_LEFT,
SAPP_KEYCODE_RIGHT,
SAPP_KEYCODE_Z,
SAPP_KEYCODE_X,
SAPP_KEYCODE_A,
SAPP_KEYCODE_S,
};
switch (event->type)
{
case SAPP_EVENTTYPE_KEY_DOWN:
case SAPP_EVENTTYPE_KEY_UP:
2018-09-20 11:39:34 +02:00
2018-09-20 16:43:30 +02:00
for (int i = 0; i < sizeof Keys / sizeof Keys[0]; i++)
{
if (event->key_code == Keys[i])
{
if(event->type == SAPP_EVENTTYPE_KEY_DOWN)
tic_input.gamepads.first.data |= (1 << i);
else
tic_input.gamepads.first.data &= ~(1 << i);
}
}
break;
default:
break;
}
2018-09-20 11:39:34 +02:00
}
static void app_cleanup(void)
{
2018-09-20 15:04:59 +02:00
if(tic)
tic80_delete(tic);
sg_shutdown();
2018-09-20 11:39:34 +02:00
}
sapp_desc sokol_main(int argc, char* argv[]) {
// args_init(argc, argv);
return (sapp_desc) {
.init_cb = app_init,
.frame_cb = app_frame,
.event_cb = app_input,
.cleanup_cb = app_cleanup,
2018-09-20 15:04:59 +02:00
.width = 3 * TIC80_FULLWIDTH,
.height = 3 * TIC80_FULLHEIGHT,
.window_title = "TIC-80 with Sokol renderer",
2018-09-20 11:39:34 +02:00
.ios_keyboard_resizes_canvas = true
};
2018-09-20 15:04:59 +02:00
}