Update set pixel to use integers instead of floats

This commit is contained in:
Abdelrahman Said 2023-12-18 14:15:18 +00:00
parent e6a3b73f99
commit 8a30e64696
2 changed files with 34 additions and 16 deletions

View File

@ -13,19 +13,21 @@ typedef struct {
} colour_t;
typedef struct {
u64 width;
u64 height;
u32 width;
u32 height;
u32 half_width;
u32 half_height;
const char *title;
SDL_Window *window;
SDL_Surface *front_buffer;
SDL_Surface *back_buffer;
} window_t;
bool init_window(window_t *wnd, u64 width, u64 height, const char *title);
bool init_window(window_t *wnd, u32 width, u32 height, const char *title);
void close_window(window_t *wnd);
void clear_window(window_t *wnd, colour_t colour);
void set_pixel(window_t *wnd, f32 x, f32 y, colour_t colour);
void set_pixel(window_t *wnd, i32 x, i32 y, colour_t colour);
void swap_buffers(window_t *wnd);
#endif // !WINDOW_H

View File

@ -7,21 +7,26 @@
#include <stdbool.h>
#include <stdio.h>
#define NORMALISED_MIN -1.0f
#define NORMALISED_MAX 1.0f
u32 colour_to_u32(colour_t colour);
u32 index_from_coordinates(window_t *wnd, u32 x, u32 y);
i32 denormalise(f32 value, u32 max);
i32 denormalise(i32 value, u32 max, u32 abs_normalised);
void set_screen_pixel(window_t *wnd, u32 x, u32 y, colour_t colour);
bool init_window(window_t *wnd, u64 width, u64 height, const char *title) {
bool init_window(window_t *wnd, u32 width, u32 height, const char *title) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("Failed to initialise SDL: %s\n", SDL_GetError());
return false;
}
if (width % 2 != 0) {
++width;
}
if (height % 2 != 0) {
++height;
}
wnd->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, 0);
if (!(wnd->window)) {
@ -53,6 +58,8 @@ bool init_window(window_t *wnd, u64 width, u64 height, const char *title) {
wnd->width = width;
wnd->height = height;
wnd->half_width = width / 2;
wnd->half_height = height / 2;
wnd->title = title;
return true;
@ -95,9 +102,9 @@ void clear_window(window_t *wnd, colour_t colour) {
SDL_UnlockSurface(wnd->back_buffer);
}
void set_pixel(window_t *wnd, f32 x, f32 y, colour_t colour) {
i32 screen_x = denormalise(x, wnd->width);
i32 screen_y = denormalise(-y, wnd->height);
void set_pixel(window_t *wnd, i32 x, i32 y, colour_t colour) {
i32 screen_x = denormalise(x, wnd->width, wnd->half_width);
i32 screen_y = denormalise(-y, wnd->height, wnd->half_height);
if (screen_x < 0 || screen_y < 0) {
return;
@ -125,13 +132,22 @@ u32 index_from_coordinates(window_t *wnd, u32 x, u32 y) {
return y * wnd->width + x;
}
i32 denormalise(f32 value, u32 max) {
if (max == 0 || value < NORMALISED_MIN || value >= NORMALISED_MAX) {
i32 denormalise(i32 value, u32 max, u32 abs_normalised) {
i32 normalised_min = -abs_normalised;
i32 normalised_max = abs_normalised;
if (max == 0) {
return -1;
}
return (i32)(((value - NORMALISED_MIN) * max) /
(NORMALISED_MAX - NORMALISED_MIN));
i32 denormalised = (i32)(((value - normalised_min) * max) /
(normalised_max - normalised_min));
if (denormalised < 0 || denormalised >= max) {
return -1;
}
return denormalised;
}
void set_screen_pixel(window_t *wnd, u32 x, u32 y, colour_t colour) {