Add function to convert from window coordinates to viewport

This commit is contained in:
Abdelrahman Said 2023-12-20 00:04:49 +00:00
parent c5cc8643e1
commit f0b6771255
2 changed files with 23 additions and 4 deletions

View File

@ -2,6 +2,7 @@
#define WINDOW_H #define WINDOW_H
#include "c_cpp_aliases/aliases.h" #include "c_cpp_aliases/aliases.h"
#include "vector/vec.h"
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
#include <stdbool.h> #include <stdbool.h>
@ -30,4 +31,6 @@ void clear_window(window_t *wnd, colour_t colour);
void set_pixel(window_t *wnd, i32 x, i32 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);
vec3f_t window_to_viewport(window_t *wnd, i32 x, i32 y, vec3f_t viewport);
#endif // !WINDOW_H #endif // !WINDOW_H

View File

@ -1,5 +1,6 @@
#include "window/window.h" #include "window/window.h"
#include "c_cpp_aliases/aliases.h" #include "c_cpp_aliases/aliases.h"
#include "vector/vec.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_error.h> #include <SDL2/SDL_error.h>
#include <SDL2/SDL_pixels.h> #include <SDL2/SDL_pixels.h>
@ -10,6 +11,7 @@
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(i32 value, u32 max, u32 abs_half); i32 denormalise(i32 value, u32 max, u32 abs_half);
vec2i_t denormalised_coords(const window_t *wnd, i32 x, i32 y);
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, u32 width, u32 height, const char *title) { bool init_window(window_t *wnd, u32 width, u32 height, const char *title) {
@ -103,16 +105,15 @@ void clear_window(window_t *wnd, colour_t colour) {
} }
void set_pixel(window_t *wnd, i32 x, i32 y, colour_t colour) { void set_pixel(window_t *wnd, i32 x, i32 y, colour_t colour) {
i32 screen_x = denormalise(x, wnd->width, wnd->half_width); vec2i_t coords = denormalised_coords(wnd, x, y);
i32 screen_y = denormalise(-y, wnd->height, wnd->half_height);
if (screen_x < 0 || screen_y < 0) { if (coords.x < 0 || coords.y < 0) {
return; return;
} }
SDL_LockSurface(wnd->back_buffer); SDL_LockSurface(wnd->back_buffer);
set_screen_pixel(wnd, (u32)screen_x, (u32)screen_y, colour); set_screen_pixel(wnd, (u32)(coords.x), (u32)(coords.y), colour);
SDL_UnlockSurface(wnd->back_buffer); SDL_UnlockSurface(wnd->back_buffer);
} }
@ -150,6 +151,13 @@ i32 denormalise(i32 value, u32 max, u32 abs_half) {
return denormalised; return denormalised;
} }
vec2i_t denormalised_coords(const window_t *wnd, i32 x, i32 y) {
i32 screen_x = denormalise(x, wnd->width, wnd->half_width);
i32 screen_y = denormalise(-y, wnd->height, wnd->half_height);
return (vec2i_t){.x = screen_x, .y = screen_y};
}
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) {
u32 index = index_from_coordinates(wnd, x, y); u32 index = index_from_coordinates(wnd, x, y);
u32 c = colour_to_u32(colour); u32 c = colour_to_u32(colour);
@ -158,3 +166,11 @@ void set_screen_pixel(window_t *wnd, u32 x, u32 y, colour_t colour) {
pixels[index] = c; pixels[index] = c;
} }
vec3f_t window_to_viewport(window_t *wnd, i32 x, i32 y, vec3f_t viewport) {
return (vec3f_t){
.x = x * viewport.x / wnd->width,
.y = y * viewport.y / wnd->height,
.z = viewport.z,
};
}