#include "arc/graphics/view.h"

#include "arc/std/errno.h"
#include <SDL.h>
#include <stdlib.h>

void ARC_View_Create(ARC_View **view, ARC_Renderer *renderer, ARC_Rect bounds){
    *view = (ARC_View *)malloc(sizeof(ARC_View));
    (*view)->renderer = renderer;
    (*view)->bounds = bounds;
}

void ARC_View_Destroy(ARC_View *view){
    free(view);
}

void ARC_View_Render(ARC_View *view, ARC_View_RenderFn renderFn, void *data){
    int err = SDL_RenderSetViewport((SDL_Renderer *)view->renderer, (const SDL_Rect *)&(view->bounds));
    if(err){
        ARC_DEBUG_LOG(ARC_ERRNO_DATA, "in src/graphics/sdl/view.c ARC_View_Render(view, renderFn), SDL_RenderSetViewport(...) returned: %d", err);
        return;
    }

    renderFn(data);

    err = SDL_RenderSetViewport((SDL_Renderer *)view->renderer, NULL);
    if(err){
        ARC_DEBUG_LOG(ARC_ERRNO_DATA, "in src/graphics/sdl/view.c ARC_View_Render(view, NULL), SDL_RenderSetViewport(...) returned: %d", err);
        return;
    }
}

ARC_Rect ARC_View_GetBounds(ARC_View *view){
    return view->bounds;
}