Update set pixel to use integers instead of floats
This commit is contained in:
parent
e6a3b73f99
commit
8a30e64696
@ -13,19 +13,21 @@ typedef struct {
|
|||||||
} colour_t;
|
} colour_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u64 width;
|
u32 width;
|
||||||
u64 height;
|
u32 height;
|
||||||
|
u32 half_width;
|
||||||
|
u32 half_height;
|
||||||
const char *title;
|
const char *title;
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
SDL_Surface *front_buffer;
|
SDL_Surface *front_buffer;
|
||||||
SDL_Surface *back_buffer;
|
SDL_Surface *back_buffer;
|
||||||
} window_t;
|
} 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 close_window(window_t *wnd);
|
||||||
|
|
||||||
void clear_window(window_t *wnd, colour_t colour);
|
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);
|
void swap_buffers(window_t *wnd);
|
||||||
|
|
||||||
#endif // !WINDOW_H
|
#endif // !WINDOW_H
|
||||||
|
@ -7,21 +7,26 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define NORMALISED_MIN -1.0f
|
|
||||||
#define NORMALISED_MAX 1.0f
|
|
||||||
|
|
||||||
u32 colour_to_u32(colour_t colour);
|
u32 colour_to_u32(colour_t colour);
|
||||||
u32 index_from_coordinates(window_t *wnd, u32 x, u32 y);
|
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);
|
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) {
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||||
printf("Failed to initialise SDL: %s\n", SDL_GetError());
|
printf("Failed to initialise SDL: %s\n", SDL_GetError());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (width % 2 != 0) {
|
||||||
|
++width;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (height % 2 != 0) {
|
||||||
|
++height;
|
||||||
|
}
|
||||||
|
|
||||||
wnd->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
|
wnd->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED, width, height, 0);
|
SDL_WINDOWPOS_CENTERED, width, height, 0);
|
||||||
if (!(wnd->window)) {
|
if (!(wnd->window)) {
|
||||||
@ -53,6 +58,8 @@ bool init_window(window_t *wnd, u64 width, u64 height, const char *title) {
|
|||||||
|
|
||||||
wnd->width = width;
|
wnd->width = width;
|
||||||
wnd->height = height;
|
wnd->height = height;
|
||||||
|
wnd->half_width = width / 2;
|
||||||
|
wnd->half_height = height / 2;
|
||||||
wnd->title = title;
|
wnd->title = title;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -95,9 +102,9 @@ void clear_window(window_t *wnd, colour_t colour) {
|
|||||||
SDL_UnlockSurface(wnd->back_buffer);
|
SDL_UnlockSurface(wnd->back_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
i32 screen_x = denormalise(x, wnd->width);
|
i32 screen_x = denormalise(x, wnd->width, wnd->half_width);
|
||||||
i32 screen_y = denormalise(-y, wnd->height);
|
i32 screen_y = denormalise(-y, wnd->height, wnd->half_height);
|
||||||
|
|
||||||
if (screen_x < 0 || screen_y < 0) {
|
if (screen_x < 0 || screen_y < 0) {
|
||||||
return;
|
return;
|
||||||
@ -125,13 +132,22 @@ u32 index_from_coordinates(window_t *wnd, u32 x, u32 y) {
|
|||||||
return y * wnd->width + x;
|
return y * wnd->width + x;
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 denormalise(f32 value, u32 max) {
|
i32 denormalise(i32 value, u32 max, u32 abs_normalised) {
|
||||||
if (max == 0 || value < NORMALISED_MIN || value >= NORMALISED_MAX) {
|
i32 normalised_min = -abs_normalised;
|
||||||
|
i32 normalised_max = abs_normalised;
|
||||||
|
|
||||||
|
if (max == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (i32)(((value - NORMALISED_MIN) * max) /
|
i32 denormalised = (i32)(((value - normalised_min) * max) /
|
||||||
(NORMALISED_MAX - NORMALISED_MIN));
|
(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) {
|
void set_screen_pixel(window_t *wnd, u32 x, u32 y, colour_t colour) {
|
||||||
|
Loading…
Reference in New Issue
Block a user