|
- #include "IZ_video.h"
-
- bool IZ_VideoIsValidWidth(u16 width) {
- // TODO check screen size
- return (320 <= width && width <= 16384);
- }
-
- bool IZ_VideoIsValidHeight(u16 height) {
- // TODO check screen size
- return (240 <= height && height <= 8192);
- }
-
- bool IZ_VideoIsValidMaxFPS(u8 max_fps) {
- return (10 <= max_fps && max_fps <= 200);
- }
-
- static IZ_ConfigItem video_config_items[] = {
- {
- IZ_CONFIG_TYPE_U16,
- sizeof(u16),
- "Video",
- "Width",
- NULL,
- &IZ_VIDEO_DEFAULT_STATE.config.width,
- IZ_VideoIsValidWidth,
- },
- {
- IZ_CONFIG_TYPE_U16,
- sizeof(u16),
- "Video",
- "Height",
- NULL,
- &IZ_VIDEO_DEFAULT_STATE.config.height,
- IZ_VideoIsValidHeight,
- },
- {
- IZ_CONFIG_TYPE_U8,
- sizeof(u8),
- "Video",
- "MaxFps",
- "-f",
- &IZ_VIDEO_DEFAULT_STATE.config.max_fps,
- IZ_VideoIsValidMaxFPS,
- },
- IZ_CONFIG_ITEM_NULL,
- };
-
- void IZ_VideoBindStateToConfig(IZ_VideoState* state, IZ_ConfigItem config_items[]) {
- config_items[0].dest = &state->config.width;
- config_items[1].dest = &state->config.height;
- config_items[2].dest = &state->config.max_fps;
- }
-
- IZ_ProcedureResult IZ_VideoSaveConfig(IZ_VideoState* state, const char* config_path) {
- IZ_VideoBindStateToConfig(state, video_config_items);
- return IZ_ConfigSave(video_config_items, config_path);
- }
-
- IZ_ProcedureResult IZ_VideoInitializeConfig(IZ_VideoState* state, const char* config_path, u8 argc, const char* argv[]) {
- IZ_VideoBindStateToConfig(state, video_config_items);
- if (IZ_ConfigInitialize(video_config_items, config_path, argc, argv) < 0) {
- return -1;
- }
- return 0;
- }
-
- IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, void* user_data, const char* config_path, u8 argc, const char* argv[]) {
- SDL_memcpy(state, &IZ_VIDEO_DEFAULT_STATE, sizeof(IZ_VideoState));
- if (IZ_VideoInitializeConfig(state, config_path, argc, argv) < 0) {
- return -2;
- }
-
- state->last_update_at = 0u;
- state->user_data = user_data;
-
- SDL_Window* window = SDL_CreateWindow(
- IZ_APP_NAME,
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- state->config.width,
- state->config.height,
- SDL_WINDOW_SHOWN
- );
- if (window == NULL) {
- // fprintf_s(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
- return -3;
- }
- state->window = window;
- state->renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
- return 0;
- }
-
- void IZ_VideoTeardown(IZ_VideoState* state) {
- SDL_DestroyWindow(state->window);
- }
-
- void IZ_VideoLoadTexture(IZ_VideoState* state, const char* dir, const char* filename, IZ_LoadedSprite* out) {
- char full_path[2048];
- sprintf(full_path, "%s/%s", dir, filename);
- FILE* f = fopen(full_path, "r");
- u32 sprite_length_bytes = ini_getl(dir, filename, 0, "assets.ini");
- u8* sprite = malloc(sprite_length_bytes + 1);
- fread(sprite, 1, sprite_length_bytes, f);
- SDL_SetRenderDrawBlendMode(state->renderer, SDL_BLENDMODE_ADD);
- SDL_Surface* test_surface = IMG_LoadSVG_RW(SDL_RWFromConstMem(sprite, sprite_length_bytes));
- free(sprite);
- if (test_surface) {
- out->texture = SDL_CreateTextureFromSurface(state->renderer, test_surface);
- out->original_width = test_surface->w;
- out->original_height = test_surface->h;
- SDL_FreeSurface(test_surface);
- }
- }
-
- void IZ_VideoTeardownTexture(IZ_LoadedSprite* sprite) {
- if (!sprite->texture) {
- return;
- }
- SDL_DestroyTexture(sprite->texture);
- sprite->original_width = 0;
- sprite->original_height = 0;
- }
|