summaryrefslogtreecommitdiff
path: root/src/game.h
blob: cf1fec96f7489dd9ef0e6eaebea22729285e4584 (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
#ifndef GAME_H

#include "entity.h"
#include "vector.h"

#include <stdint.h>

#define TITLE "Obsolete Human Space Pilot"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define BYTES_PER_PIXEL 4

#define SECOND 1000.0f
#define FPS 60
#define MS_PER_FRAME (SECOND / FPS)
#define UPDATES_PER_SECOND 120
#define MS_PER_UPDATE (SECOND / UPDATES_PER_SECOND)

#ifndef M_PI
#define M_PI 3.14159265358979f
#endif

#define COLOR_BACKGROUND 0x000000

struct GameBounds
{
    float center_x;
    float center_y;
    float side_length_x;
    float side_length_y;
};

struct GameControllerInput
{
    uint8_t controller_index;
    float left_stick_x;
    float left_stick_y;
    float right_stick_x;
    float right_stick_y;
};

struct GameView
{
    // TODO: Use a vec2d here
    float dx;
    float dy;
    float zoom;
};

struct GameState
{
    struct Entity player;
    struct Vec2d thrust_vector01;
    struct Vec2d thrust_vector02;
    struct Vec2d thrust_vector_sum;
    struct GameView view;
    struct Entities *entities;
};

struct OffscreenBuffer
{
    // NOTE(amin): pixels are always 32-bits wide. Memory order: BB GG RR XX.
    void *memory;
    unsigned int width;
    unsigned int height;
    unsigned int pitch;
};

// TODO: Consider using the #define trick from HMH
typedef void (game_update_t)(struct GameState*, struct GameControllerInput, int, int);
typedef void (game_render_t)(struct OffscreenBuffer*, float, struct GameState*);

void game_init(struct GameState *game_state, int field_width, int field_height);
void game_update(struct GameState *game_state, struct GameControllerInput game_input, int field_width, int field_height);
void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *game_state);
void game_render_circle(struct OffscreenBuffer *buffer, float x, float y, float radius, uint32_t color);
void game_render_line(struct OffscreenBuffer *buffer, float x0, float y0, float x1, float y1, uint32_t color);
void game_render_vector(struct OffscreenBuffer *buffer, struct Vec2d *v, float x0, float y0, float radius, uint32_t color);
float game_calc_render_offset(float zoom, float delta, float pos, float center);
void game_set_pixel(struct OffscreenBuffer *buffer, uint32_t x, uint32_t y, uint32_t color);
void game_cleanup(struct GameState *game_state);

#define GAME_H
#endif