Fumofumotris/source/main.c

154 lines
2.4 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-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-09 07:00:48 +00:00
struct Instance {
struct Ctrl ctrl;
struct Delegate on_start;
struct Delegate on_update;
struct Delegate on_draw;
struct Terminal term;
};
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-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-09 07:00:48 +00:00
struct TChar4 blks[game->term.area];
game->term.blks = blks;
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-04 19:43:45 +00:00
// Game logic
Invoke(&game->on_update, game);
// Draw
2024-04-09 07:00:48 +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
{
2024-04-09 07:00:48 +00:00
if (!NewCtrl(&game->ctrl, code_count, code_count))
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;
return true;
}
2024-04-01 22:39:21 +00:00
2024-04-09 07:00:48 +00:00
int main()
{
struct Instance game;
Start(&game);
2024-04-03 23:31:47 +00:00
2024-04-09 07:00:48 +00:00
#ifdef _WIN32
if(!WindowsInit(&game.term)) {
printf("FUCK");
exit(1);
}
#endif
printf("does it work");
2024-04-03 23:31:47 +00:00
2024-04-09 07:00:48 +00:00
StartInput(&game.ctrl);
Loop(&game);
2024-04-03 23:31:47 +00:00
2024-04-09 07:00:48 +00:00
JoinInput(&game.ctrl);
2024-03-25 05:34:59 +00:00
return 0;
}