Fumofumotris/source/main.c

151 lines
2.6 KiB
C
Raw Normal View History

2024-03-25 05:34:59 +00:00
#include <iso646.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-04-15 19:29:51 +00:00
#include "ctrl.h"
2024-04-18 05:04:44 +00:00
#include "input.h"
2024-03-25 05:34:59 +00:00
#include "fumotris.h"
#include "term.h"
#include "tetr.h"
2024-04-03 23:31:47 +00:00
#include "event.h"
2024-04-16 22:14:53 +00:00
#include "platform.h"
2024-03-25 05:34:59 +00:00
2024-04-09 22:10:30 +00:00
struct Instance {
struct Controller ctrl;
struct Terminal term;
struct Delegate on_start;
struct Delegate on_update;
struct Delegate on_draw;
};
2024-04-02 20:06:14 +00:00
const u8 I[16] = {
0, 0, 0, 0,
0, 0, 0, 0,
1, 1, 1, 1,
0, 0, 0, 0
};
2024-04-02 00:55:30 +00:00
2024-04-02 20:06:14 +00:00
const u8 O[4] = {
1, 1,
1, 1
2024-03-25 05:34:59 +00:00
};
2024-04-02 00:55:30 +00:00
2024-04-02 20:06:14 +00:00
const u8 T[9] = {
0, 1, 0,
1, 1, 1,
0, 0, 0
2024-03-25 05:34:59 +00:00
};
2024-04-02 20:06:14 +00:00
const u8 S[9] = {
0, 1, 1,
1, 1, 0,
0, 0, 0
};
const u8 Z[9] = {
1, 1, 0,
0, 1, 1,
0, 0, 0
};
const u8 J[9] = {
1, 0, 0,
1, 1, 1,
0, 0, 0
};
const u8 L[9] = {
0, 0, 1,
1, 1, 1,
0, 0, 0
2024-03-25 05:34:59 +00:00
};
2024-04-02 20:06:14 +00:00
struct CtrlBind {
enum CtrlCode code;
u16 bind;
u8 type;
};
const size_t code_count = 12;
const struct CtrlBind ctrl_binds[12] = {
2024-04-11 19:57:22 +00:00
{ LEFT, 0x25, BUTTON },
{ RIGHT, 0x27, BUTTON },
{ SOFT_DROP, 0x28, BUTTON },
{ HARD_DROP, 0x20, BUTTON },
{ ROTATE_CCW, 'Z', BUTTON },
{ ROTATE_CW, 'X', BUTTON },
{ ROTATE_180, 'A', BUTTON },
{ SWAP, 'C', BUTTON },
{ ESC, 0x1B, BUTTON },
2024-04-02 20:06:14 +00:00
{ VSCROLL, 0, AXIS },
{ HSCROLL, 1, AXIS },
{ MOUSE, 0, JOYSTICK }
};
2024-04-09 07:00:48 +00:00
void *Update(void *args)
2024-04-04 19:43:45 +00:00
{
2024-04-09 07:00:48 +00:00
struct Instance *game = args;
2024-04-04 19:43:45 +00:00
2024-04-03 23:31:47 +00:00
while (true) {
2024-04-04 19:43:45 +00:00
// Input
2024-04-09 07:00:48 +00:00
CtrlPoll(&game->ctrl);
2024-04-15 19:29:51 +00:00
if (CtrlGet(&game->ctrl, LEFT, BUTTON)->is_down)
2024-04-09 22:10:30 +00:00
printf("left down this frame\n");
2024-04-04 19:43:45 +00:00
// Game logic
2024-04-09 22:10:30 +00:00
//Invoke(&game->on_update, game);
2024-04-04 19:43:45 +00:00
// Draw
2024-04-09 22:10:30 +00:00
//TermOut(&game->term);
//puts(game->term.buf);
2024-03-25 05:34:59 +00:00
}
}
2024-04-09 07:00:48 +00:00
void Loop(struct Instance *game)
2024-04-03 23:31:47 +00:00
{
pthread_t update_thread;
2024-04-09 07:00:48 +00:00
pthread_create(&update_thread, nullptr, Update, (void *)game);
2024-04-03 23:31:47 +00:00
}
2024-04-09 07:00:48 +00:00
bool Start(struct Instance *game)
2024-03-25 05:34:59 +00:00
{
if (!NewCtrl(&game->ctrl))
2024-04-09 07:00:48 +00:00
return false;
2024-03-25 05:34:59 +00:00
2024-04-02 00:55:30 +00:00
for (size_t i = 0; i < code_count; i++) {
2024-04-02 20:06:14 +00:00
const struct CtrlBind *bind = &ctrl_binds[i];
2024-04-09 07:00:48 +00:00
CtrlMap(&game->ctrl, bind->code, bind->bind, bind->type);
2024-03-25 05:34:59 +00:00
}
2024-04-09 07:00:48 +00:00
if (!NewTerm(&game->term, 20, 20))
return false;
2024-04-09 22:10:30 +00:00
if (!NewDelegate(&game->on_start, 16))
return false;
if (!NewDelegate(&game->on_update, 16))
return false;
if (!NewDelegate(&game->on_draw, 16))
return false;
2024-04-09 07:00:48 +00:00
return true;
}
2024-04-01 22:39:21 +00:00
2024-04-09 07:00:48 +00:00
int main()
{
struct Instance game;
2024-04-09 22:10:30 +00:00
if (!Start(&game))
exit(1);
2024-04-03 23:31:47 +00:00
2024-04-18 05:04:44 +00:00
if(!PlatformInit())
2024-04-09 07:00:48 +00:00
exit(1);
2024-04-03 23:31:47 +00:00
InputStart(&game.ctrl.buf, );
2024-04-09 07:00:48 +00:00
Loop(&game);
2024-04-03 23:31:47 +00:00
2024-03-25 05:34:59 +00:00
return 0;
}