|
- #include "IZ_video.h"
-
- IZ_ProcedureResult IZ_VideoSaveConfig(IZ_VideoState* state, const char* config_path) {
- if (!ini_putl("Video", "Width", state->config.width, config_path)) {
- return -1;
- }
- if (!ini_putl("Video", "Height", state->config.height, config_path)) {
- return -1;
- }
- if (!ini_putl("Video", "MaxFps", state->config.max_fps, config_path)) {
- return -1;
- }
-
- return 0;
- }
-
- void IZ_VideoLoadConfig(IZ_VideoState* state, const char* config_path) {
- state->config.width = ini_getl("Video", "Width", IZ_DEFAULT_VIDEO_STATE.config.width, config_path);
- state->config.height = ini_getl("Video", "Height", IZ_DEFAULT_VIDEO_STATE.config.height, config_path);
- state->config.max_fps = ini_getl("Video", "MaxFps", IZ_DEFAULT_VIDEO_STATE.config.max_fps, config_path);
- }
-
- void IZ_VideoOverrideConfig(IZ_VideoState* state, u8 argc, const char* argv[]) {
- const char* cmdline_buffer;
- if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-f"))) {
- state->config.max_fps = atoi(cmdline_buffer);
- }
- }
-
- IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, const char* config_path, u8 argc, const char* argv[]) {
- SDL_memcpy(state, &IZ_DEFAULT_VIDEO_STATE, sizeof(IZ_VideoState));
-
- IZ_VideoLoadConfig(state, config_path);
- if (IZ_VideoSaveConfig(state, config_path)) {
- // fprintf_s(stderr, "Error committing video config.\n");
- }
- IZ_VideoOverrideConfig(state, argc, argv);
- state->last_update_at = 0u;
-
- 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 1;
- }
- state->window = window;
- state->renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
-
- return 0;
- }
-
- void IZ_VideoUpdateForDebugTicks(IZ_VideoState* video_state, uint64_t ticks) {
- SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0xff, 0xff, 0xff);
- u64 the_ticks = ticks;
- u8 column;
- u8 row;
- const u8 size = 4;
-
- u8 i;
- for (i = 0; i < 64; i += 1) {
- column = i % 32;
- row = i / 32;
-
- if (the_ticks & 0x1) {
- SDL_RenderFillRectF(video_state->renderer, &(SDL_FRect) {
- (f32) (video_state->config.width - ((column + 1) * size)),
- (f32) (video_state->config.height - ((row + 1) * size)),
- size,
- size
- });
- }
- the_ticks >>= 1;
- }
- }
-
- void IZ_VideoUpdateForDebugInput(IZ_VideoState* video_state, IZ_InputState* input_state) {
- SDL_SetRenderDrawColor(video_state->renderer, 0xff, 0xff, 0x00, 0xff);
- u8 column;
- u8 row;
- const u8 size = 4;
-
- u8 p;
- u8 i;
- for (p = 0; p < IZ_PLAYERS; p += 1) {
- IZ_Action the_action = input_state->action[p];
- for (i = 0; i < CONTROLS; i += 1) {
- column = (i % 4) + (p * 4);
- row = i / 4;
-
- if (the_action & 0x1) {
- SDL_RenderFillRectF(video_state->renderer, &(SDL_FRect) {
- (f32) (column * size),
- (f32) (row * size),
- size,
- size
- });
- }
- the_action >>= 1;
- }
- }
- }
-
- void IZ_VideoUpdateForDebug(IZ_VideoState* video_state, IZ_InputState* input_state, u64 ticks) {
- IZ_VideoUpdateForDebugTicks(video_state, ticks);
- IZ_VideoUpdateForDebugInput(video_state, input_state);
- }
-
- void IZ_VideoUpdate(IZ_VideoState* video_state, u64 ticks, IZ_InputState* input_state) {
- if (ticks - video_state->last_update_at > 1000 / video_state->config.max_fps) {
- // Update window
- SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0x00, 0x00, 0xff);
- SDL_RenderClear(video_state->renderer);
- for (u8 i = 0; i < MAX_ACTIVE_SPRITES; i += 1) {
- if (!video_state->active_sprites[i]) {
- continue;
- }
- // TODO draw sprites
- }
- IZ_VideoUpdateForDebug(video_state, input_state, ticks);
- SDL_RenderPresent(video_state->renderer);
- video_state->last_update_at = ticks;
- }
- }
-
- void IZ_VideoTeardown(IZ_VideoState* state) {
- SDL_DestroyWindow(state->window);
- }
|