196 lines
5.8 KiB
C++
196 lines
5.8 KiB
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <iostream>
|
|
|
|
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3/SDL_main.h>
|
|
#include <SDL3/SDL_opengl.h>
|
|
|
|
#include "ui/ui_main.h"
|
|
#include "adapter/adapter_texture.h"
|
|
#include "device/dvc_render_target.h"
|
|
#include "device/dvc_device.h"
|
|
#include "scene/dvc_scene.h"
|
|
|
|
int32_t g_screenWidth = 2048;
|
|
int32_t g_screenHeight = 1024;
|
|
static SDL_Window* window = NULL;
|
|
AdapterTexture g_adapterTexture;
|
|
SDL_GLContext gl_context;
|
|
davinci::RenderTarget g_renderTarget;
|
|
davinci::AntiAliasingTech g_aaTech = davinci::AntiAliasingTech::AA_None;
|
|
|
|
int32_t g_canvasWidth = 1024;
|
|
int32_t g_canvasHeight = 512;
|
|
davinci::Device g_device;
|
|
davinci::Scene g_scene;
|
|
int32_t g_mousePosX = 0;
|
|
int32_t g_mousePosY = 0;
|
|
davinci::fVector4 g_mouseColor = davinci::fVector4::BLACK;
|
|
|
|
//-------------------------------------------------------------------------------------
|
|
//int main(int argc, char* argv[])
|
|
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[])
|
|
{
|
|
// Setup SDL
|
|
SDL_SetAppMetadata("Davinci Console Program", "1.0", "com.thecodeway.davinci");
|
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
|
|
{
|
|
printf("Error: SDL_Init(): %s\n", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
// Decide GL+GLSL versions
|
|
#if defined(IMGUI_IMPL_OPENGL_ES2)
|
|
// GL ES 2.0 + GLSL 100 (WebGL 1.0)
|
|
const char* glsl_version = "#version 100";
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
#elif defined(IMGUI_IMPL_OPENGL_ES3)
|
|
// GL ES 3.0 + GLSL 300 es (WebGL 2.0)
|
|
const char* glsl_version = "#version 300 es";
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
#elif defined(__APPLE__)
|
|
// GL 3.2 Core + GLSL 150
|
|
const char* glsl_version = "#version 150";
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
#else
|
|
// GL 3.0 + GLSL 130
|
|
const char* glsl_version = "#version 130";
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
#endif
|
|
|
|
// Create window with graphics context
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
|
Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
|
|
window = SDL_CreateWindow("Davinci Console Program", g_screenWidth, g_screenHeight, window_flags);
|
|
if (window == nullptr)
|
|
{
|
|
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
|
gl_context = SDL_GL_CreateContext(window);
|
|
if (gl_context == nullptr)
|
|
{
|
|
printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
SDL_GL_MakeCurrent(window, gl_context);
|
|
SDL_GL_SetSwapInterval(1); // Enable vsync
|
|
SDL_ShowWindow(window);
|
|
|
|
//init main ui
|
|
initMainUI(window, gl_context, glsl_version);
|
|
|
|
// create adapter texture
|
|
g_adapterTexture.init(g_screenWidth, g_screenHeight);
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
// init davinci
|
|
g_scene.init();
|
|
g_renderTarget.init(g_canvasWidth, g_canvasHeight);
|
|
g_adapterTexture.updateTexture();
|
|
|
|
return SDL_APP_CONTINUE; /* carry on with the program! */
|
|
}
|
|
|
|
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
|
|
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event)
|
|
{
|
|
processUIEvent(event);
|
|
|
|
switch (event->type)
|
|
{
|
|
case SDL_EVENT_QUIT:
|
|
return SDL_APP_SUCCESS;
|
|
break;
|
|
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
|
|
{
|
|
if (event->window.windowID == SDL_GetWindowID(window))
|
|
{
|
|
return SDL_APP_SUCCESS;
|
|
}
|
|
}
|
|
break;
|
|
case SDL_EVENT_WINDOW_RESIZED:
|
|
{
|
|
g_screenWidth = event->window.data1;
|
|
g_screenHeight = event->window.data2;
|
|
g_adapterTexture.resize(g_screenWidth, g_screenHeight);
|
|
g_adapterTexture.updateTexture();
|
|
}
|
|
break;
|
|
}
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
/* This function runs once per frame, and is the heart of the program. */
|
|
SDL_AppResult SDL_AppIterate(void* appstate)
|
|
{
|
|
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
|
|
{
|
|
SDL_Delay(10);
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
// render
|
|
glViewport(0, 0, (int)g_screenWidth, (int)g_screenHeight);
|
|
|
|
glClearColor(0.f, 0.f, 0.f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glLoadIdentity();
|
|
|
|
//draw render result
|
|
g_adapterTexture.updateTexture(0, 0, g_renderTarget.getColorBuffer());
|
|
g_adapterTexture.updateTexture(g_canvasWidth, 0, g_renderTarget.getDepthBuffer());
|
|
g_adapterTexture.draw();
|
|
|
|
float mousePosX, mousePosY;
|
|
SDL_GetMouseState(&mousePosX, &mousePosY);
|
|
|
|
g_mousePosX = (int32_t)(mousePosX) % g_canvasWidth;
|
|
g_mousePosY = g_screenHeight - (int32_t)(mousePosY);
|
|
if(g_mousePosX>=0 && g_mousePosX < g_canvasWidth && g_mousePosY >=0 && g_mousePosY < g_canvasHeight)
|
|
{
|
|
g_mouseColor = g_renderTarget.getColorBuffer().getPixel(g_mousePosX, g_mousePosY);
|
|
}
|
|
else
|
|
{
|
|
g_mouseColor = davinci::fVector4::BLACK;
|
|
}
|
|
|
|
//draw ui
|
|
drawMainUI();
|
|
|
|
SDL_GL_SwapWindow(window);
|
|
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
/* This function runs once at shutdown. */
|
|
void SDL_AppQuit(void* appstate, SDL_AppResult result)
|
|
{
|
|
/* SDL will clean up the window/renderer for us. */
|
|
cleanMainUI();
|
|
SDL_GL_DestroyContext(gl_context);
|
|
}
|
|
|