Get basic graph rendering functionality setup and a basic test
This commit is contained in:
parent
b40c175e75
commit
161e5c885a
|
|
@ -84,12 +84,12 @@ bitmap_file init_bitmap_file(const bitmap_header *bitmap_in, const char *filenam
|
|||
return new_bitmap_file;
|
||||
};
|
||||
|
||||
int write_bitmap_pixel(const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in) {
|
||||
int write_bitmap_pixel(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, const int32_t x_in, const int32_t y_in) {
|
||||
int8_t write_status;
|
||||
int32_t byte_position_start = calc_bitmap_row_byte_position_start(bitmap_file_in->bitmap_metadata, y_in) + ((x_in - 1) * 3);
|
||||
uint8_t *bitmap_pixel_in_byte_ptr = (uint8_t *)bitmap_pixel_in;
|
||||
int32_t byte_position_start = calc_bitmap_row_byte_position_start(bitmap_file_in.bitmap_metadata, y_in) + ((x_in - 1) * 3);
|
||||
uint8_t *bitmap_pixel_in_byte_ptr = (uint8_t *)&bitmap_pixel_in;
|
||||
|
||||
write_status = pwrite(bitmap_file_in->fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in->bitmap_metadata->image_data_start_offset + byte_position_start);
|
||||
write_status = pwrite(bitmap_file_in.fd, bitmap_pixel_in_byte_ptr, 3, bitmap_file_in.bitmap_metadata->image_data_start_offset + byte_position_start);
|
||||
|
||||
return 0;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -67,6 +67,6 @@ uint32_t calc_bitmap_pixel_array_size_bytes(const bitmap_header *bitmap_in);
|
|||
uint32_t calc_bitmap_header_size_bytes();
|
||||
|
||||
/* Write bitmap pixel at particular coordinate */
|
||||
int write_bitmap_pixel(const bitmap_file *bitmap_file_in, const bitmap_pixel_color *bitmap_pixel_in, const int32_t x_in, const int32_t y_in);
|
||||
int write_bitmap_pixel(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, const int32_t x_in, const int32_t y_in);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -2,26 +2,24 @@
|
|||
#include "bmp.h"
|
||||
#include "math.h"
|
||||
|
||||
void grapher_draw_line(bitmap_file *bitmap_file_in, int32_t x_origin_in, int32_t y_origin_in, int32_t x_end_in, int32_t y_end_in) {
|
||||
float x_step = 0;
|
||||
float y_step = 0;
|
||||
int32_t x_origin_lerped;
|
||||
int32_t y_origin_lerped;
|
||||
bitmap_pixel_color target_pixel;
|
||||
target_pixel.red = 0;
|
||||
target_pixel.green = 0;
|
||||
target_pixel.blue = 0;
|
||||
|
||||
void grapher_draw_line_linear(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, int32_t x_origin_in, int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in) {
|
||||
while (x_origin_in < x_end_in || y_origin_in < y_end_in) {
|
||||
x_origin_lerped = lerp(x_origin_in, x_end_in, x_step);
|
||||
y_origin_lerped = lerp(y_origin_in, y_end_in, y_step);
|
||||
|
||||
x_step += 0.1;
|
||||
y_step += 0.1;
|
||||
|
||||
if (x_step >= 1 || y_step >= 1)
|
||||
return;
|
||||
|
||||
write_bitmap_pixel(bitmap_file_in, &target_pixel, x_origin_in, y_origin_in);
|
||||
write_bitmap_pixel(bitmap_file_in, bitmap_pixel_in, x_origin_in, y_origin_in);
|
||||
if (x_origin_in < x_end_in)
|
||||
x_origin_in++;
|
||||
if (y_origin_in < y_end_in)
|
||||
y_origin_in++;
|
||||
};
|
||||
};
|
||||
|
||||
void grapher_draw_line_lerp(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, int32_t x_origin_in, int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in, const float lerp_step_in) {
|
||||
int32_t x_origin_lerped = 1;
|
||||
int32_t y_origin_lerped = 1;
|
||||
float position = 0;
|
||||
|
||||
for (position = 0; x_origin_lerped < x_end_in || y_origin_lerped < y_end_in; position += lerp_step_in) {
|
||||
x_origin_lerped = lerp(x_origin_in, x_end_in, position);
|
||||
y_origin_lerped = lerp(y_origin_in, y_end_in, position);
|
||||
write_bitmap_pixel(bitmap_file_in, bitmap_pixel_in, x_origin_lerped, y_origin_lerped);
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "bmp.h"
|
||||
#include <stdint.h>
|
||||
|
||||
void grapher_draw_line(const bitmap_file *bitmap_file_in, const int32_t x_origin_in, const int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in);
|
||||
void grapher_draw_line_linear(const bitmap_file bitmap_file_in, const bitmap_pixel_color pixel_in, const int32_t x_origin_in, const int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in);
|
||||
void grapher_draw_line_lerp(const bitmap_file bitmap_file_in, const bitmap_pixel_color bitmap_pixel_in, int32_t x_origin_in, int32_t y_origin_in, const int32_t x_end_in, const int32_t y_end_in, const float lerp_step_in);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
24
main.c
24
main.c
|
|
@ -59,13 +59,31 @@ int main() {
|
|||
/* Simple skybox generator */
|
||||
target_pixel.red = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
|
||||
target_pixel.green = ((float)height / (float)test_file.bitmap_metadata->image_height) * 255;
|
||||
target_pixel.blue = 255;
|
||||
target_pixel.blue = 150;
|
||||
|
||||
write_bitmap_pixel(&test_file, &target_pixel, width, height);
|
||||
write_bitmap_pixel(test_file, target_pixel, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
grapher_draw_line(&test_file, 50, 50, 200, 50);
|
||||
target_pixel.red = 255;
|
||||
target_pixel.green = 0;
|
||||
target_pixel.blue = 0;
|
||||
grapher_draw_line_lerp(test_file, target_pixel, 10, 10, 100, 245, 0.01);
|
||||
target_pixel.red = 0;
|
||||
target_pixel.green = 255;
|
||||
target_pixel.blue = 0;
|
||||
grapher_draw_line_lerp(test_file, target_pixel, 10, 10, 245, 245, 0.01);
|
||||
target_pixel.red = 0;
|
||||
target_pixel.green = 0;
|
||||
target_pixel.blue = 255;
|
||||
grapher_draw_line_lerp(test_file, target_pixel, 10, 10, 245, 100, 0.01);
|
||||
|
||||
/* Draw graph */
|
||||
target_pixel.red = 0;
|
||||
target_pixel.green = 0;
|
||||
target_pixel.blue = 0;
|
||||
grapher_draw_line_linear(test_file, target_pixel, 10, 10, 245, 10);
|
||||
grapher_draw_line_linear(test_file, target_pixel, 10, 10, 10, 245);
|
||||
|
||||
close(test_file.fd);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue