SDK Multiplataforma en C logo

SDK Multiplataforma en C

Bricks

❮ Anterior
Siguiente ❯

Briks es una imitación muy simplista del videojuego Atari Breakout, que nos permitirá realizar una introducción al mundo de las Aplicaciones síncronas. Cualquier aplicación en tiempo real debe estar constantemente actualizándose intervenga o no el usuario. El código fuente está en la caperta /src/demo/bricks de la distribución del SDK.

  • Utiliza osmain_sync para iniciar una aplicación síncrona, indicando un intervalo y función callback de actualización. NAppGUI lanzará periódicamente eventos de tiempo que actualizarán el programa.
  • Captura del juego bricks, imitación del Atari Breakout.
    Figura 1: Videojuego bricks en Windows, macOS y Linux.

Esta aplicación está dirigida por dos eventos (Figura 2). Por un lado el movimiento del slider, que puede producirse en cualquier momento (evento asíncrono), y actualizará la posición del jugador. Por otro un evento síncrono producido por osmain_sync cada 40 milisegundos y será notificado a través de i_update() para que se actualice el estado del juego y la vista gráfica.

Gráfico que muestra dos eventos, uno síncrono y otro asíncrono.
Figura 2: Eventos síncronos y asíncronos.
Listado 1: demo/bricks/bricks.c
  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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Simplistic Breakout-like game */

#include <nappgui.h>

#define NUM_BRICKS 40

typedef struct _brick_t Brick;
typedef struct _app_t App;

struct _brick_t
{
    real32_t x;
    real32_t y;
    uint8_t color;
    bool_t is_visible;
};

struct _app_t
{
    bool_t is_running;
    Brick bricks[NUM_BRICKS];
    color_t color[4];
    real32_t brick_width;
    real32_t player_pos;
    real32_t ball_x;
    real32_t ball_y;
    V2Df ball_dir;
    real32_t ball_speed;
    Cell *button;
    Slider *slider;
    View *view;
    Window *window;
};

/*---------------------------------------------------------------------------*/

static const real32_t i_BALL_RADIUS = .03f;
static const real32_t i_BRICK_HEIGHT = .03f;
static const real32_t i_BRICK_SEPARATION = .005f;
static const uint32_t i_BRICKS_PER_ROW = 10;
static const uint32_t i_NUM_ROWS = 4;

/*---------------------------------------------------------------------------*/

static void i_OnDraw(App *app, Event *e)
{
    const EvDraw *params = event_params(e, EvDraw);
    uint32_t i = 0;

    draw_clear(params->ctx, color_rgb(102, 153, 26));
    draw_line_color(params->ctx, kCOLOR_BLACK);

    for (i = 0; i < NUM_BRICKS; ++i)
    {
        if (app->bricks[i].is_visible == TRUE)
        {
            real32_t x = app->bricks[i].x * params->width;
            real32_t y = app->bricks[i].y * params->height;
            real32_t width = app->brick_width * params->width;
            real32_t height = i_BRICK_HEIGHT * params->height;
            draw_fill_color(params->ctx, app->color[app->bricks[i].color]);
            draw_rect(params->ctx, ekFILLSK, x, y, width, height);
        }
    }

    {
        real32_t x = (app->player_pos - app->brick_width) * params->width;
        real32_t y = (1 - i_BRICK_HEIGHT - i_BRICK_SEPARATION) * params->height;
        real32_t width = 2 * app->brick_width * params->width;
        real32_t height = i_BRICK_HEIGHT * params->height;
        draw_fill_color(params->ctx, kCOLOR_BLACK);
        draw_rect(params->ctx, ekFILL, x, y, width, height);
    }

    {
        real32_t x = app->ball_x * params->width;
        real32_t y = app->ball_y * params->height;
        real32_t rad = i_BALL_RADIUS * params->width;
        draw_fill_color(params->ctx, kCOLOR_WHITE);
        draw_circle(params->ctx, ekFILL, x, y, rad);
    }
}

/*---------------------------------------------------------------------------*/

static void i_OnSlider(App *app, Event *e)
{
    const EvSlider *params = event_params(e, EvSlider);
    app->player_pos = params->pos;
}

/*---------------------------------------------------------------------------*/

static void i_OnStart(App *app, Event *e)
{
    unref(e);
    app->is_running = TRUE;
    cell_enabled(app->button, FALSE);
}

/*---------------------------------------------------------------------------*/

 static Panel *i_panel(App *app)
 {
    Panel *panel = panel_create();
    Layout *layout = layout_create(1, 4);
    View *view = view_create();
    Slider *slider = slider_create();
    Label *label = label_create();
    Button *button = button_push();
    view_size(view, s2df(258, 344));
    view_OnDraw(view, listener(app, i_OnDraw, App));
    slider_OnMoved(slider, listener(app, i_OnSlider, App));
    label_text(label, "Use the slider!");
    button_text(button, "Start");
    button_OnClick(button, listener(app, i_OnStart, App));
    layout_view(layout, view, 0, 0);
    layout_slider(layout, slider, 0, 1);
    layout_label(layout, label, 0, 2);
    layout_button(layout, button, 0, 3);
    layout_vexpand(layout, 0);
    layout_vmargin(layout, 0, 10);
    layout_vmargin(layout, 2, 10);
    layout_margin(layout, 10);
    panel_layout(panel, layout);
    app->view = view;
    app->slider = slider;
    app->button = layout_cell(layout, 0, 3);
    return panel;
}

/*---------------------------------------------------------------------------*/

static void i_init_game(App *app)
{
    real32_t hoffset;
    Brick *brick = NULL;
    uint32_t j, i;

    app->color[0] = color_rgb(255, 0, 0);
    app->color[1] = color_rgb(0, 255, 0);
    app->color[2] = color_rgb(0, 0, 255);
    app->color[3] = color_rgb(0, 255, 255);

    hoffset = i_BRICK_SEPARATION;
    brick = app->bricks;

    app->is_running = FALSE;
    app->brick_width = (1 - ((real32_t)i_BRICKS_PER_ROW + 1) * i_BRICK_SEPARATION) / (real32_t)i_BRICKS_PER_ROW;

    for (j = 0; j < i_NUM_ROWS; ++j)
    {
        real32_t woffset = i_BRICK_SEPARATION;

        for (i = 0; i < i_BRICKS_PER_ROW; ++i)
        {
            brick->x = woffset;
            brick->y = hoffset;
            brick->is_visible = TRUE;
            brick->color = (uint8_t)j;
            woffset += app->brick_width + i_BRICK_SEPARATION;
            brick++;
        }

        hoffset += i_BRICK_HEIGHT + i_BRICK_SEPARATION;
    }

    app->player_pos = slider_get_value(app->slider);
    app->ball_x = .5f;
    app->ball_y = .5f;
    app->ball_dir.x = .3f;
    app->ball_dir.y = -.1f;
    app->ball_speed = .6f;
    v2d_normf(&app->ball_dir);
}

/*---------------------------------------------------------------------------*/

static void i_OnClose(App *app, Event *e)
{
    osapp_finish();
    unref(app);
    unref(e);
}

/*---------------------------------------------------------------------------*/

static App *i_create(void)
{
    App *app = heap_new0(App);
    Panel *panel = i_panel(app);
    app->window = window_create(ekWINDOW_STDRES);
    window_panel(app->window, panel);
    window_origin(app->window, v2df(200, 200));
    window_title(app->window, "Bricks - A 2D Game");
    window_OnClose(app->window, listener(app, i_OnClose, App));
    window_show(app->window);
    i_init_game(app);
    return app;
}

/*---------------------------------------------------------------------------*/

static void i_destroy(App **app)
{
    window_destroy(&(*app)->window);
    heap_delete(app, App);
}

/*---------------------------------------------------------------------------*/

static bool_t i_collision(Brick *brick, real32_t brick_width, real32_t ball_x, real32_t ball_y)
{
    if (ball_x + i_BALL_RADIUS < brick->x)
        return FALSE;
    if (ball_x - i_BALL_RADIUS > brick->x + brick_width)
        return FALSE;
    if (ball_y + i_BALL_RADIUS < brick->y)
        return FALSE;
    if (ball_y - i_BALL_RADIUS > brick->y + i_BRICK_HEIGHT)
        return FALSE;
    return TRUE;
}

/*---------------------------------------------------------------------------*/

static void i_update(App *app, const real64_t prtime, const real64_t ctime)
{
    if (app->is_running == TRUE)
    {
        real32_t step = (real32_t)(ctime - prtime);
        bool_t collide;
        uint32_t i;

        /* Update ball position */
        app->ball_x += step * app->ball_speed * app->ball_dir.x;
        app->ball_y += step * app->ball_speed * app->ball_dir.y;

        /* Collision with limits */
        if (app->ball_x + i_BALL_RADIUS >= 1.f && app->ball_dir.x >= 0.f)
            app->ball_dir.x = - app->ball_dir.x;

        if (app->ball_x - i_BALL_RADIUS <= 0.f && app->ball_dir.x <= 0.f)
            app->ball_dir.x = - app->ball_dir.x;

        if (app->ball_y - i_BALL_RADIUS <= 0.f && app->ball_dir.y <= 0.f)
            app->ball_dir.y = - app->ball_dir.y;

        /* Collision with bricks */
        collide = FALSE;
        for (i = 0; i < NUM_BRICKS; ++i)
        {
            if (app->bricks[i].is_visible == TRUE)
            {
                if (i_collision(&app->bricks[i], app->brick_width, app->ball_x, app->ball_y) == TRUE)
                {
                    app->bricks[i].is_visible = FALSE;
                    if (collide == FALSE)
                    {
                        real32_t brick_x = app->bricks[i].x + .5f * app->brick_width;
                        app->ball_dir.x = 5.f * (app->ball_x - brick_x);
                        app->ball_dir.y = - app->ball_dir.y;
                        v2d_normf(&app->ball_dir);
                        collide = TRUE;
                    }
                }
            }
        }

        /* Collision with player */
        {
            Brick player;
            player.x = app->player_pos - app->brick_width;
            player.y = 1.f - i_BRICK_HEIGHT - i_BRICK_SEPARATION;
            if (i_collision(&player, 2.f * app->brick_width, app->ball_x, app->ball_y) == TRUE)
            {
                app->ball_dir.x = 5.f * (app->ball_x - app->player_pos);
                app->ball_dir.y = - app->ball_dir.y;
                v2d_normf(&app->ball_dir);
            }
        }

        /* Game Over */
        if (app->ball_y + i_BALL_RADIUS >= 1.f)
        {
            i_init_game(app);
            cell_enabled(app->button, TRUE);
        }
    }

    view_update(app->view);
}

/*---------------------------------------------------------------------------*/

#include "osmain.h"
osmain_sync(.04, i_create, i_destroy, i_update, "", App)
❮ Anterior
Siguiente ❯