#include <SDL3/SDL.h>
#include "type.h"
#include "Renderer.h"
#include <iostream>
static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL;
static Types::vec2 windowDimensions(800, 600);
static Rena3D_Renderer r3dgpu;

Uint64 lastTime = SDL_GetPerformanceCounter();
r3d_Variant::RenderTime deltaTime = 0.0;

int main(Types::uint_32 argc, char* argv) {
	if (!SDL_Init(SDL_INIT_VIDEO)) {
		std::cerr << "cannot init sdl window: " << SDL_GetError() << std::endl;
		return -1;
	}

	window = SDL_CreateWindow(
		"rena3d",
		windowDimensions.X,
		windowDimensions.Y,
		0
	);

	if (!window) {
		std::cerr << "cannot create sdl window: " << SDL_GetError() << std::endl;
		SDL_Quit();
		return -3;
	}

	renderer = SDL_CreateRenderer(window, nullptr);
	if (!renderer) {
		std::cerr << "renderer create failed: " << SDL_GetError() << std::endl;
		SDL_DestroyWindow(window);
		return -1;
	}

	Types::BOOLEAN isRunning = true;

	SDL_Event event;
	r3dgpu.InitTree();

	while (isRunning) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_EVENT_QUIT) {
				isRunning = false;
			}
		}

		Uint64 curTime = SDL_GetPerformanceCounter();

		deltaTime = (r3d_Variant::RenderTime)(curTime - lastTime) / (r3d_Variant::RenderTime)SDL_GetPerformanceFrequency();

		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
		SDL_RenderClear(renderer);

		lastTime = curTime;

		if (deltaTime > 0.1) {
			deltaTime = 0.1;
		}

		r3dgpu.RenderFrame(deltaTime);

		SDL_RenderPresent(renderer);
	}

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}