Fumofumotris/source/main.c

206 lines
3.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>
#include "control.h"
#include "fumotris.h"
#include "term.h"
#include "tetr.h"
2024-04-03 23:31:47 +00:00
#include "event.h"
2024-03-25 05:34:59 +00:00
#ifdef _WIN32
#include "win.h"
#endif
2024-04-03 23:31:47 +00:00
struct Instance {
struct Terminal term;
struct Ctrl ctrl;
struct RecordBuffer rec_buf;
struct Delegate on_start;
struct Delegate on_draw;
struct Delegate on_update;
};
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] = {
{ LEFT, 0x25, KEY },
{ RIGHT, 0x27, KEY },
{ SOFT_DROP, 0x28, KEY },
{ HARD_DROP, 0x20, KEY },
{ ROTATE_CCW, 'Z', KEY },
{ ROTATE_CW, 'X', KEY },
{ ROTATE_180, 'A', KEY },
{ SWAP, 'C', KEY },
{ ESC, 0x1B, KEY },
{ VSCROLL, 0, AXIS },
{ HSCROLL, 1, AXIS },
{ MOUSE, 0, JOYSTICK }
};
2024-04-04 19:43:45 +00:00
bool DrawUpdate(struct Terminal *term, struct Instance *game, char *buf)
2024-04-02 20:06:14 +00:00
{
2024-04-04 19:43:45 +00:00
bool is_update;
TermLock(term);
{
is_update = TermWaitUpdate(term);
if(is_update) {
Invoke(&game->on_draw, game);
TermOut(term, buf);
}
}
TermUnlock(term);
return is_update;
}
void Draw(struct Terminal *term, struct Instance *game)
{
struct TChar4 buf0[term->area];
struct TChar4 buf1[term->area];
TermLock(term);
{
TermSetBufs(term, buf0, buf1);
}
TermUnlock(term);
TermSignalSafe(term);
char buf[term->buf_size];
2024-04-03 23:31:47 +00:00
while (true) {
2024-04-04 19:43:45 +00:00
if (!DrawUpdate(term, game, buf))
break;
puts(buf);
2024-04-03 23:31:47 +00:00
}
2024-04-04 19:43:45 +00:00
2024-04-03 23:31:47 +00:00
}
2024-03-25 05:34:59 +00:00
2024-04-04 13:27:54 +00:00
void Update(struct Instance *game)
2024-04-03 23:31:47 +00:00
{
2024-04-04 19:43:45 +00:00
WaitSafeTerm(&game->term);
2024-04-03 23:31:47 +00:00
while (true) {
2024-04-04 19:43:45 +00:00
// Input
CtrlPoll(&game->ctrl, &game->rec_buf);
// Game logic
Invoke(&game->on_update, game);
// Draw
UpdateTerm(&game->term);
2024-03-25 05:34:59 +00:00
}
}
2024-04-03 23:31:47 +00:00
void Loop(struct Ctrl *ctrl, struct RecordBuffer *rec_buf)
{
pthread_t draw_thread;
pthread_create(&draw_thread, nullptr, Draw, &term);
pthread_t update_thread;
pthread_create(&update_thread, nullptr, Update, &term);
}
2024-03-25 05:34:59 +00:00
int main()
{
2024-04-02 20:06:14 +00:00
#ifdef _WIN32
if(!WindowsInit())
exit(1);
#endif
2024-04-03 23:31:47 +00:00
struct ctrl_bkt code_bkts[code_count];
struct ctrl_dict codes = {
2024-04-02 20:06:14 +00:00
.capacity = code_count,
.filled = 0,
.bkts = code_bkts
};
2024-04-03 23:31:47 +00:00
struct ctrl_bkt bind_bkts[code_count];
struct ctrl_dict binds = {
2024-04-02 20:06:14 +00:00
.capacity = code_count,
.filled = 0,
.bkts = bind_bkts
};
struct Axis axes[code_count];
2024-04-03 23:31:47 +00:00
struct Ctrl ctrl = NewCtrl(&codes, &binds, axes);
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];
CtrlMap(&ctrl, bind->code, bind->bind, bind->type);
2024-03-25 05:34:59 +00:00
}
2024-04-03 23:31:47 +00:00
struct RecordBuffer rec_buf = {
2024-04-01 22:39:21 +00:00
.count = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
2024-04-03 23:31:47 +00:00
struct Instance game = {
2024-04-04 13:27:54 +00:00
.term = NewTerm(20, 20),
2024-04-03 23:31:47 +00:00
.ctrl = NewCtrl(),
.on_start = NewDelegate(16),
.on_draw = NewDelegate(16),
.on_update = NewDelegate(16)
};
StartInput(&ctrl, &rec_buf);
Loop(&ctrl, &rec_buf);
2024-04-01 22:39:21 +00:00
JoinInput(&ctrl);
2024-03-25 05:34:59 +00:00
return 0;
}