Initial commit

This commit is contained in:
Fierelier 2024-09-12 04:19:19 +02:00
commit f5adfe0495
7 changed files with 190 additions and 0 deletions

9
LICENSE Normal file
View File

@ -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.

1
README.md Normal file
View File

@ -0,0 +1 @@
SDL 2.x platform for unigi. Check out the [unigi main repository](https://git.lumen.sh/Fierelier/unigi) for more info and documentation.

40
events.c Normal file
View File

@ -0,0 +1,40 @@
void unigi_event_get(unigi_type_event * event) {
SDL_Event platform_event;
if (SDL_PollEvent(&platform_event)) {
if (
platform_event.type == SDL_KEYDOWN ||
platform_event.type == SDL_KEYUP
) {
event -> type = unigi_enum_event_input_keyboard;
event -> data.input_keyboard.device = 0;
event -> data.input_keyboard.button = platform_event.key.keysym.scancode;
event -> data.input_keyboard.down = (platform_event.type == SDL_KEYDOWN);
return;
}
if (platform_event.type == SDL_MOUSEMOTION) {
event -> type = unigi_enum_event_input_mouse_move;
event -> data.input_mouse_move.rel_x = platform_event.motion.xrel;
event -> data.input_mouse_move.rel_y = platform_event.motion.yrel;
event -> data.input_mouse_move.abs_x = platform_event.motion.x;
event -> data.input_mouse_move.abs_y = platform_event.motion.y;
return;
}
if (
platform_event.type == SDL_MOUSEBUTTONDOWN ||
platform_event.type == SDL_MOUSEBUTTONUP
) {
event -> type = unigi_enum_event_input_mouse_button;
event -> data.input_mouse_button.button = platform_event.button.button;
event -> data.input_mouse_button.down = (platform_event.type == SDL_MOUSEBUTTONDOWN);
return;
}
event -> type = unigi_enum_event_unknown;
return;
} else {
event -> type = unigi_enum_event_none;
return;
}
}

114
graphics.c Normal file
View File

@ -0,0 +1,114 @@
unigi_type_resolution_range * unigi_resolutions;
SDL_Surface * unigi_platform_surface;
SDL_Window * unigi_platform_window;
uint8_t * unigi_platform_pixels1;
uint16_t * unigi_platform_pixels2;
uint32_t * unigi_platform_pixels4;
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_window_create(unigi_type_resolution resolution, char * title) {
SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION,"0");
unigi_platform_window = SDL_CreateWindow(title,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,resolution.width,resolution.height,0);
if (unigi_platform_window == NULL) {
printf("[unigi] Could not initialize window: SDL -> %s\n",SDL_GetError());
return 1;
}
unigi_platform_surface = SDL_GetWindowSurface(unigi_platform_window);
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);
unigi_platform_info_depth_bytes = unigi_platform_surface->format->BytesPerPixel;
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;
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(unigi_platform_surface->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;
}
unigi_type_error unigi_graphics_init() {
return 0;
}
inline void unigi_graphics_draw(unigi_type_resolution_1d_coord index, unigi_type_color color) {
color = color & 0x0FFF;
if (unigi_platform_info_depth_bytes < 3) {
if (unigi_platform_info_depth_bytes == 1) {
unigi_platform_pixels1[index] = unigi_platform_palette1[color];
} else {
unigi_platform_pixels2[index] = unigi_platform_palette2[color];
}
} else {
if (unigi_platform_info_depth_bytes == 3) {
memcpy(
unigi_platform_pixels1 + (index * 3),
unigi_platform_palette1 + (color * 3),
3
);
} else {
unigi_platform_pixels4[index] = unigi_platform_palette4[color];
}
}
}
inline void unigi_graphics_blit(unigi_type_resolution_1d_coord index, unigi_type_color * pixels, unigi_type_resolution_1d_coord length) {
unigi_type_resolution_1d_coord i;
if (unigi_platform_info_depth_bytes == 1) {
for (i = 0; i < length; i++) {
unigi_platform_pixels1[index + i] = unigi_platform_palette1[pixels[i] & 0x0FFF];
}
} else if (unigi_platform_info_depth_bytes == 2) {
for (i = 0; i < length; i++) {
unigi_platform_pixels2[index + i] = unigi_platform_palette2[pixels[i] & 0x0FFF];
}
} else if (unigi_platform_info_depth_bytes == 3) {
for (i = 0; i < length; i++) {
memcpy(
unigi_platform_pixels1 + ((index + i) * 3),
unigi_platform_palette1 + ((pixels[i] & 0x0FFF) * 3),
3
);
}
} else if (unigi_platform_info_depth_bytes == 4) {
for (i = 0; i < length; i++) {
unigi_platform_pixels4[index + i] = unigi_platform_palette4[pixels[i] & 0x0FFF];
}
}
}
inline void unigi_graphics_flush() {
SDL_UpdateWindowSurface(unigi_platform_window);
}

7
input.c Normal file
View File

@ -0,0 +1,7 @@
void unigi_input_mouse_capture(unigi_type_bool captured) {
if (captured) {
SDL_SetRelativeMouseMode(SDL_TRUE);
} else {
SDL_SetRelativeMouseMode(SDL_FALSE);
}
}

12
main.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdint.h>
#include <SDL2/SDL.h>
#include "unigi/main.h"
#include "graphics.c"
#include "time.c"
#include "events.c"
#include "input.c"
unigi_type_error unigi_init() {
return 0;
}

7
time.c Normal file
View File

@ -0,0 +1,7 @@
unigi_type_time_span unigi_time_get() {
return (unigi_type_time_span)SDL_GetTicks() * 1000;
}
void unigi_time_sleep(unigi_type_time_span t) {
SDL_Delay((unigi_type_time_span)t / 1000);
}