summaryrefslogtreecommitdiff
path: root/src/game.c
blob: 946d53ab932e36abf976dc30748ebabeb1914ee6 (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
116
117
118
119
120
121
122
123
124
125
126
127
#include "game.h"
// TODO: remove this
#include <math.h>

#include <stdbool.h>
#include <stdio.h>

bool PAUSED = false;


void game_init(struct GameState *game_state, int field_width, int field_height)
{
    if (!game_state)
    {
        // TODO: handle invalid pointer error
        return;
    }

    game_state->view.dx = 0;
    game_state->view.dy = 0;
    game_state->view.zoom = 1;
    game_state->player.angle = 0;
    game_state->player.speed = 0;
    game_state->player.mass = 10;
    game_state->player.size = 100;
    game_state->player.x = field_width / 2;
    game_state->player.y = field_height / 2;
    game_state->player.color = 0x888888;
}


void game_update(struct GameState *game_state, struct GameControllerInput game_input, int field_width, int field_height)
{
    if (!game_state)
    {
        // TODO: handle invalid pointer error
        return;
    }

    game_state->thrust_vector.angle = atan2f(game_input.left_stick_y, game_input.left_stick_x);
    game_state->thrust_vector.length = 100 * hypotf(game_input.left_stick_x, game_input.left_stick_y);
    printf("(lx: %f, ly: %f, thrust_a: %f, thrust_l: %f)\n",
            game_input.left_stick_x,
            game_input.left_stick_y,
            game_state->thrust_vector.angle,
            game_state->thrust_vector.length);

    struct Entity *player = &game_state->player;
    entity_accelerate(player, game_state->thrust_vector.angle, game_state->thrust_vector.length * 0.001);
    player->x += player->speed * cos(player->angle);
    player->y += player->speed * sin(player->angle);
}


void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *game_state)
{
    if (!buffer || !game_state)
    {
        // TODO: handle invalid pointer error
        return;
    }
    struct Entity player = game_state->player;
    game_render_circle(buffer, player.x, player.y, player.size, player.color);
    game_render_circle(buffer,
            player.x + (game_state->thrust_vector.length * cos(game_state->thrust_vector.angle)),
            player.y + (game_state->thrust_vector.length * sin(game_state->thrust_vector.angle)),
            10,
            0x0000FF);
    game_render_circle(buffer,
            player.x + 100 * (player.speed * cos(player.angle)),
            player.y + 100 * (player.speed * sin(player.angle)),
            10,
            0xFF00FF);
}


void game_render_circle(struct OffscreenBuffer *buffer, float center_x, float center_y, float radius, uint32_t color)
{
    for(int y = -radius; y <= radius; y++)
    {
        for(int x = -radius; x <= radius; x++)
        {
            if(x * x + y * y <= radius * radius)
            {
                game_set_pixel(buffer, center_x + x, center_y + y, color);
            }
        }
    }
}


void game_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint32_t color)
{
    if (!buffer)
    {
        // TODO: handle invalid pointer error
        return;
    }

    /* Origin is (0, 0) on the upper left.
     * To go one pixel right, increment by 32 bits.
     * To go one pixel down, increment by (buffer.width * 32) bits.
     */
    if (x < buffer->width && y < buffer->height)
    {
        uint8_t *pixel_pos = (uint8_t *)buffer->memory;
        pixel_pos += ((BYTES_PER_PIXEL*x) + (buffer->pitch * y));
        uint32_t *pixel = (uint32_t *)pixel_pos;
        *pixel = color;
    }
}


float game_calc_render_offset(float zoom, float delta, float pos, float center)
{
    return ((1 - zoom) * center) + (pos + delta) * zoom;
}


void game_cleanup(struct GameState *game_state)
{
    if (!game_state)
    {
        // TODO: handle invalid pointer error
        return;
    }
}