summaryrefslogtreecommitdiff
path: root/src/target/tetris.c
blob: 2419c8dade9af31deb9c38f37cd8dc77d0161ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <SFML/System/Clock.h>
#include <SFML/Window/Keyboard.h>
#include <SFML/Graphics/RenderWindow.h>
#include <SFML/Graphics/Font.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "common.h"
#include "idlist.h"
#include "vector.h"
#include "text.h"
#include "field.h"
#include "painter.h"
#include "engine.h"
#include "tet_conf.h"

sfRenderWindow *window;
struct idlist *texts;

struct field  fld, nxt;
struct game   game = {
    .started = 0,
    .paused = 0,
    .scoreCurrent = 0,
    .level = 1,
    .moveLatency = L00LATENCY,
    .lines = 0
};

static void handleWindowEvents() {
    sfEvent event;
    while (sfRenderWindow_pollEvent(window, &event))
        if (event.type == sfEvtClosed)
            sfRenderWindow_close(window);
}

static void register_text(void *obj)
{
    struct text *text = obj;
    text->id = painter_register_text(text);
}

int main()
{
    srand(time(NULL));
    game.gameTick = sfClock_create();
    game.putTick = sfClock_create();
    game.mTick = sfClock_create();
    game.repPushDown = sfClock_create();
    game.repKeyLeft = sfClock_create();
    game.repKeyRight = sfClock_create();
    painter_load_font("dat/arial.ttf");

    sfVideoMode mode = (sfVideoMode){450, 570, 32};
    window = sfRenderWindow_create(mode, windowName_conf, sfResize | sfClose, NULL);
    if (!window)
        exit(EXIT_FAILURE);
    sfRenderWindow_setFramerateLimit(window, 60);
    painter_set_window(window);

    fld.pos = FLD_POS;
    fld.size = (struct vector2ui){.x = FLD_SIZE_X, .y = FLD_SIZE_Y};
    fld.bound = (struct vector2ui){.x = FLD_BOUND_X, .y = FLD_BOUND_Y};
    fld.shape_cnt = 2;
    field_init(&fld);
    fld.shape[0].attr |= SHP_ATTR_GHOST;

    nxt.pos = NXT_POS;
    nxt.size = NXT_SIZE;
    nxt.bound = NXT_SIZE;
    nxt.shape_cnt = 3;
    nxt.attr |= FLD_ATTR_HIDE_EMPTY_CELLS | FLD_ATTR_INVISIBLE;
    field_init(&nxt);
    nxt.shape[0].y = 4;
    nxt.shape[1].y = 1;
    nxt.shape[2].y = -2;

    fld.id = painter_register_field(&fld);
    nxt.id = painter_register_field(&nxt);
    field_fill_random(&fld);
    painter_update_field(fld.id, &fld);
    painter_update_field(nxt.id, &nxt);

    texts = load_texts("dat/texts.yaml");

    list_foreach(texts, register_text);

    transition_init();
    while (sfRenderWindow_isOpen(window)) {
        handleWindowEvents();
        main_loop();
    }

    list_foreach(texts, text_destroy);
    list_destroy(texts);

    painter_destroy_drawables();
    field_deinit(&fld);
    field_deinit(&nxt);

    if (window) {
        sfRenderWindow_destroy(window);
        window = 0;
    }
    painter_destroy_font();
    sfClock_destroy(game.gameTick);
    sfClock_destroy(game.putTick);
    sfClock_destroy(game.mTick);
    sfClock_destroy(game.repPushDown);
    sfClock_destroy(game.repKeyLeft);
    sfClock_destroy(game.repKeyRight);
    return EXIT_SUCCESS;
}