init commit
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
#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 <scene/dvc_scene.h>
|
||||
|
||||
int32_t g_screenWidth = 1024;
|
||||
int32_t g_screenHeight = 768;
|
||||
static SDL_Window* window = NULL;
|
||||
AdapterTexture g_adapterTexture;
|
||||
SDL_GLContext gl_context;
|
||||
davinci::RenderTarget g_renderTarget;
|
||||
|
||||
davinci::Scene g_scene;
|
||||
|
||||
//-------------------------------------------------------------------------------------
|
||||
//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(512, 256);
|
||||
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(512, 0, g_renderTarget.getDepthBuffer());
|
||||
g_adapterTexture.draw();
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user