From 7a4924f404c2b7b1bc2140bdcf52d7658b867ca2 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Mon, 17 Jun 2024 17:30:15 +0200 Subject: [PATCH] Initial commit --- LICENSE | 9 ++++++ src/graphics.h | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/input.h | 0 src/main.h | 9 ++++++ src/window.h | 17 +++++++++++ 5 files changed, 116 insertions(+) create mode 100644 LICENSE create mode 100644 src/graphics.h create mode 100644 src/input.h create mode 100644 src/main.h create mode 100644 src/window.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6a6f3d8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/graphics.h b/src/graphics.h new file mode 100644 index 0000000..02f6143 --- /dev/null +++ b/src/graphics.h @@ -0,0 +1,81 @@ +uint8_t * unigi_platform_palette1; +uint16_t * unigi_platform_palette2; +uint32_t * unigi_platform_palette4; +uint8_t unigi_platform_info_depth_bytes; + +static inline void unigi_platform_color_16_to_32(unigi_type_color color,uint8_t * a,uint8_t * r, uint8_t * g, uint8_t * b) { + *a = (color >> 12) & 0xF; + *a = *a << 4; + *r = (color >> 8) & 0xF; + *r = *r << 4; + *g = (color >> 4) & 0xF; + *g = *g << 4; + *b = color & 0xF; + *b = *b << 4; +} + + +unigi_type_error unigi_graphics_init() { + uint8_t r; uint8_t g; uint8_t b; + unigi_type_color color; + uint32_t color_platform; + uint8_t * color_platform8 = (uint8_t *)&color_platform; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("[unigi] Could not initialize video: SDL -> %s\n",SDL_GetError()); + return 1; + } + + const SDL_VideoInfo * video_info = SDL_GetVideoInfo(); + SDL_PixelFormat * video_format = video_info->vfmt; + unigi_type_resolution_depth bpp = video_format->BitsPerPixel; + unigi_platform_info_depth_bytes = bpp; + while (unigi_platform_info_depth_bytes % 8 != 0) { ++unigi_platform_info_depth_bytes; } + unigi_platform_info_depth_bytes = unigi_platform_info_depth_bytes / 8; + + unigi_platform_palette1 = malloc(4096 * unigi_platform_info_depth_bytes); + if (unigi_platform_palette1 == NULL) { + printf("[unigi] Could not initialize video: unigi -> palette malloc failed\n"); + return 1; + } + unigi_platform_palette2 = (uint16_t *)unigi_platform_palette1; + unigi_platform_palette4 = (uint32_t *)unigi_platform_palette1; + + color = 0; + while (color < 4096) { + unigi_platform_color_16_to_32(color,&r,&r,&g,&b); + color_platform = SDL_MapRGBA(video_format,r,g,b,255); + unigi_type_resolution_depth i; + for(i = 0; i < unigi_platform_info_depth_bytes; i++) + { + unigi_platform_palette1[(color * unigi_platform_info_depth_bytes) + i] = color_platform8[i]; + } + ++color; + } + return 0; +} + +static inline void unigi_graphics_draw(unigi_type_resolution_1d_coord pixel, unigi_type_color color) { + color = color & 0x0FFF; + if (unigi_platform_info_depth_bytes < 3) { + if (unigi_platform_info_depth_bytes == 1) { + unigi_platform_pixels1[pixel] = unigi_platform_palette1[color]; + } else { + unigi_platform_pixels2[pixel] = unigi_platform_palette2[color]; + } + } else { + if (unigi_platform_info_depth_bytes == 3) { + memcpy( + unigi_platform_pixels1 + (pixel * 3), + unigi_platform_palette1 + (color * 3), + 3 + ); + } else { + unigi_platform_pixels4[pixel] = unigi_platform_palette4[color]; + } + } +} + +void unigi_graphics_flush() { + SDL_Flip(unigi_platform_surface); +} diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..006a797 --- /dev/null +++ b/src/main.h @@ -0,0 +1,9 @@ +#include +#include +#include "window.h" +#include "graphics.h" +#include "input.h" + +unigi_type_error unigi_init() { + return 0; +} diff --git a/src/window.h b/src/window.h new file mode 100644 index 0000000..55b6912 --- /dev/null +++ b/src/window.h @@ -0,0 +1,17 @@ +SDL_Surface * unigi_platform_surface; +uint8_t * unigi_platform_pixels1; +uint16_t * unigi_platform_pixels2; +uint32_t * unigi_platform_pixels4; + +unigi_type_error unigi_window_create(unigi_type_resolution resolution, char * title) { + unigi_platform_surface = SDL_SetVideoMode(resolution.width,resolution.height,resolution.depth,SDL_SWSURFACE); + if (unigi_platform_surface == NULL) { + printf("[unigi] Could not initialize window: SDL -> %s\n",SDL_GetError()); + return 1; + } + unigi_platform_pixels1 = (uint8_t *)(unigi_platform_surface->pixels); + unigi_platform_pixels2 = (uint16_t *)(unigi_platform_surface->pixels); + unigi_platform_pixels4 = (uint32_t *)(unigi_platform_surface->pixels); + SDL_WM_SetCaption(title,NULL); + return 0; +}