added a vipe coded tetris gui
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#ifndef GPU_H
|
||||
#define GPU_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define GPU_BOARD_H 20
|
||||
#define GPU_BOARD_W 10
|
||||
#define GPU_NEXT_H 4
|
||||
#define GPU_NEXT_W 4
|
||||
|
||||
#ifndef GPU_SCRIPT
|
||||
#define GPU_SCRIPT "/home/honney/Projects/tetris_gui/gpu.py"
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
FILE *pipe;
|
||||
} gpu_t;
|
||||
|
||||
static gpu_t *gpu_init(void) {
|
||||
gpu_t *g = (gpu_t *)malloc(sizeof(gpu_t));
|
||||
if (!g) return NULL;
|
||||
g->pipe = popen(GPU_SCRIPT, "w");
|
||||
if (!g->pipe) { free(g); return NULL; }
|
||||
return g;
|
||||
}
|
||||
|
||||
static void gpu_update(gpu_t *g,
|
||||
int b1[GPU_BOARD_H][GPU_BOARD_W],
|
||||
int b2[GPU_BOARD_H][GPU_BOARD_W],
|
||||
int n1[GPU_NEXT_H][GPU_NEXT_W],
|
||||
int n2[GPU_NEXT_H][GPU_NEXT_W]) {
|
||||
|
||||
FILE *p = g->pipe;
|
||||
|
||||
fputs("{\"b1\":[", p);
|
||||
for (int r = 0; r < GPU_BOARD_H; r++) {
|
||||
if (r) fputc(',', p);
|
||||
fputc('[', p);
|
||||
for (int c = 0; c < GPU_BOARD_W; c++) {
|
||||
if (c) fputc(',', p);
|
||||
fprintf(p, "%d", b1[r][c]);
|
||||
}
|
||||
fputc(']', p);
|
||||
}
|
||||
|
||||
fputs("],\"b2\":[", p);
|
||||
for (int r = 0; r < GPU_BOARD_H; r++) {
|
||||
if (r) fputc(',', p);
|
||||
fputc('[', p);
|
||||
for (int c = 0; c < GPU_BOARD_W; c++) {
|
||||
if (c) fputc(',', p);
|
||||
fprintf(p, "%d", b2[r][c]);
|
||||
}
|
||||
fputc(']', p);
|
||||
}
|
||||
|
||||
fputs("],\"n1\":[", p);
|
||||
for (int r = 0; r < GPU_NEXT_H; r++) {
|
||||
if (r) fputc(',', p);
|
||||
fputc('[', p);
|
||||
for (int c = 0; c < GPU_NEXT_W; c++) {
|
||||
if (c) fputc(',', p);
|
||||
fprintf(p, "%d", n1[r][c]);
|
||||
}
|
||||
fputc(']', p);
|
||||
}
|
||||
|
||||
fputs("],\"n2\":[", p);
|
||||
for (int r = 0; r < GPU_NEXT_H; r++) {
|
||||
if (r) fputc(',', p);
|
||||
fputc('[', p);
|
||||
for (int c = 0; c < GPU_NEXT_W; c++) {
|
||||
if (c) fputc(',', p);
|
||||
fprintf(p, "%d", n2[r][c]);
|
||||
}
|
||||
fputc(']', p);
|
||||
}
|
||||
|
||||
fputs("]}\n", p);
|
||||
fflush(p);
|
||||
}
|
||||
|
||||
static void gpu_close(gpu_t *g) {
|
||||
if (g) {
|
||||
if (g->pipe) fclose(g->pipe);
|
||||
free(g);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user