Make aspect global

This commit is contained in:
2025-10-18 17:43:22 +01:00
parent 5604675ca6
commit 26b9e1da94
7 changed files with 14 additions and 21 deletions

6
game.c
View File

@@ -119,7 +119,6 @@ static shape_t shapes[MAX_SHAPES];
static unsigned entity_count; static unsigned entity_count;
static unsigned shape_count; static unsigned shape_count;
static float aspect;
static unsigned level; static unsigned level;
static bool dead; static bool dead;
@@ -516,12 +515,11 @@ static void pause()
counter = 0; counter = 0;
} }
void game_init(float _aspect) void game_init()
{ {
input_on_shoot(shoot); input_on_shoot(shoot);
input_on_restart(reset); input_on_restart(reset);
input_on_pause(pause); input_on_pause(pause);
aspect = _aspect;
reset(); reset();
} }
@@ -578,7 +576,7 @@ void game_draw()
shapes[i].connect); shapes[i].connect);
} }
text_draw_score(aspect, score); text_draw_score(score);
if (paused && !(counter & COUNTER_MASK)) { if (paused && !(counter & COUNTER_MASK)) {
text_draw_centre("PAUSED"); text_draw_centre("PAUSED");

2
game.h
View File

@@ -1,7 +1,7 @@
#ifndef GAME_H #ifndef GAME_H
#define GAME_H #define GAME_H
void game_init(float aspect); void game_init();
void game_update(); void game_update();
void game_draw(); void game_draw();

5
main.c
View File

@@ -18,16 +18,15 @@ static void quit()
int main() int main()
{ {
const int input_fd = input_init(); const int input_fd = input_init();
const renderer_params_t renderer_params = renderer_init(); const int drm_fd = renderer_init();
rng_init(); rng_init();
game_init(renderer_params.aspect); game_init();
input_on_quit(quit); input_on_quit(quit);
renderer_clear(); renderer_clear();
renderer_swap(); renderer_swap();
const int drm_fd = renderer_params.drm_fd;
const int max_fd = MAX(input_fd, drm_fd); const int max_fd = MAX(input_fd, drm_fd);
fd_set set; fd_set set;

View File

@@ -9,6 +9,8 @@
#include <unistd.h> #include <unistd.h>
#include <xf86drm.h> #include <xf86drm.h>
float aspect;
static int drm_fd; static int drm_fd;
static drmModeConnector *conn; static drmModeConnector *conn;
static drmModeCrtc *crtc; static drmModeCrtc *crtc;
@@ -74,7 +76,7 @@ static void draw_line(vec3_t v1, vec3_t v2)
} }
} }
renderer_params_t renderer_init() int renderer_init()
{ {
drm_fd = open("/dev/dri/card1", O_RDWR | O_CLOEXEC); drm_fd = open("/dev/dri/card1", O_RDWR | O_CLOEXEC);
assert(drm_fd != -1); assert(drm_fd != -1);
@@ -104,7 +106,7 @@ renderer_params_t renderer_init()
wrap = true; wrap = true;
const float aspect = (float)width / (float)height; aspect = (float)width / (float)height;
const float scale = (float)height / 2.0f; const float scale = (float)height / 2.0f;
view = (mat3_t) { view = (mat3_t) {
{ scale, 0, 0 }, { scale, 0, 0 },
@@ -112,10 +114,7 @@ renderer_params_t renderer_init()
{ aspect * scale, scale, 1 }, { aspect * scale, scale, 1 },
}; };
return (renderer_params_t) { return drm_fd;
.drm_fd = drm_fd,
.aspect = aspect,
};
} }
void renderer_cleanup() void renderer_cleanup()

View File

@@ -7,12 +7,9 @@
#define MAX_VERTS 8U #define MAX_VERTS 8U
typedef struct { extern float aspect;
int drm_fd;
float aspect;
} renderer_params_t;
renderer_params_t renderer_init(); int renderer_init();
void renderer_cleanup(); void renderer_cleanup();
void renderer_handle(); void renderer_handle();

2
text.c
View File

@@ -255,7 +255,7 @@ void text_draw_centre(const char *s)
draw_text(s, pos); draw_text(s, pos);
} }
void text_draw_score(float aspect, unsigned score) void text_draw_score(unsigned score)
{ {
char buf[MAX_SCORE_CHARS + 1]; char buf[MAX_SCORE_CHARS + 1];
buf[MAX_SCORE_CHARS] = '\0'; buf[MAX_SCORE_CHARS] = '\0';

2
text.h
View File

@@ -2,6 +2,6 @@
#define TEXT_H #define TEXT_H
void text_draw_centre(const char *s); void text_draw_centre(const char *s);
void text_draw_score(float aspect, unsigned score); void text_draw_score(unsigned score);
#endif #endif