Skip to content

Commit

Permalink
Merge pull request #3 from alfonsmagd/feature/CreateContextD3D11
Browse files Browse the repository at this point in the history
Feature/create context d3 d11
  • Loading branch information
alfonsmagd authored Aug 23, 2024
2 parents a812545 + 37d0509 commit 94c994f
Show file tree
Hide file tree
Showing 17 changed files with 1,389 additions and 239 deletions.
239 changes: 130 additions & 109 deletions Ifnity/Ifnity/src/Ifnity/App.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include "pch.h"

#include "App.h"
#include "GraphicsDeviceManager.h"
#include <glad\glad.h>
#include <GLFW/glfw3.h>
#include "Platform/ImguiRender/ImguiOpenglRender.h"
#include "Ifnity/Layers/ExampleLayer.h" //TODO BORRAR , LO SUYO SU CPP CORRESPONDIENTE
#include "GraphicsDeviceManager.hpp"
#include "Ifnity/Layers/ExampleLayer.h" //TODO: BORRAR , your own cpp
#include "Ifnity/Layers/NVML_Layer.hpp"
#include "Platform/ImguiRender/ImguiOpenglRender.h"
#include "Platform/ImguiRender/ImguiD3D11Render.h"
#include <GLFW/glfw3.h>
#include <glad\glad.h>
#include <Platform/Windows/DeviceOpengl.h>
#include <Platform/Windows/DeviceD3D11.h>


namespace IFNITY {



namespace IFNITY
{


static const char* shaderCodeVertex = R"(
Expand Down Expand Up @@ -52,24 +55,22 @@ void main()
)";


//Static member declaration
// Static member declaration
App* App::s_Instance = nullptr;
//Default Constructor;
// Default Constructor;
App::App()
{
s_Instance = this;
// Create windows props
// Create windows props
WindowData props;

m_Window = std::unique_ptr<GraphicsDeviceManager>(GraphicsDeviceManager::Create(rhi::GraphicsAPI::OPENGL));

m_Window = std::unique_ptr<GraphicsDeviceManager>(
GraphicsDeviceManager::Create(rhi::GraphicsAPI::D3D11));

m_Window->CreateWindowSurface(props);
//Intialize the EventListenerControler
// Intialize the EventListenerControler
m_GLFWEventListener = std::make_unique<GLFWEventListener>();



SetEventBus(m_Window->GetGLFWEventSource());

CONNECT_EVENT(WindowResize);
Expand All @@ -80,154 +81,174 @@ void main()
CONNECT_EVENT(ScrollMouseMove);
CONNECT_EVENT(MouseClick);


// Initialize ImGui

// Initialize ImGui and set m_graphicsAPI;
InitializeImGui();
m_graphicsAPI = GraphicsDeviceManager::GetStaticGraphicsAPI();

SetImguiAPI(GraphicsDeviceManager::GetStaticGraphicsAPI());

ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiBackendFlags_HasMouseCursors; // Enable SetMousePos.
io.ConfigFlags |= ImGuiBackendFlags_HasSetMousePos; // Enable SetMousePos.
io.ConfigFlags |= ImGuiBackendFlags_HasSetMousePos; // Enable SetMousePos.
io.FontGlobalScale = 1.0f;
ImGui::StyleInfity(); // Clasic color style.

//Classic version 1.87 see IMGUI_DISABLE_OBSOLETE_KEYIO in new version
// not necessary intialization maps for keys.
//io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;

ImGui_ImplOpenGL3_Init("#version 450");
ImGui::StyleInfity(); // Clasic color style.

// Classic version 1.87 see IMGUI_DISABLE_OBSOLETE_KEYIO in new version
// not necessary intialization maps for keys.
// io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;

m_ImguiRenderFunctionMap[rhi::GraphicsAPI::OPENGL] = []()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
ImPlot::CreateContext();
};
m_ImguiRenderFunctionMap[rhi::GraphicsAPI::D3D11] = []()
{
ImGui_ImplDX11_NewFrame();
ImGui::NewFrame();
ImPlot::CreateContext();

};
m_ImguiRenderFunctionMap[rhi::GraphicsAPI::D3D12] = []() {};
m_ImguiRenderFunctionMap[rhi::GraphicsAPI::VULKAN] = []() {};
}
App::~App()
{

//OnDetach all layers
for(Layer* layer : m_LayerStack)
// OnDetach all layers
for ( Layer* layer : m_LayerStack )
{
layer->OnDetach();
}
s_Instance = nullptr;
IFNITY_LOG(LogApp, INFO, "App is destroyed");

}


void App::run()
{
InitiateEventBusLayers();

const GLuint shaderVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shaderVertex, 1, &shaderCodeVertex, nullptr);
glCompileShader(shaderVertex);

const GLuint shaderFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shaderFragment, 1, &shaderCodeFragment, nullptr);
glCompileShader(shaderFragment);

const GLuint program = glCreateProgram();
glAttachShader(program, shaderVertex);
glAttachShader(program, shaderFragment);

glLinkProgram(program);
glUseProgram(program);

GLuint vao;
glCreateVertexArrays(1, &vao);
glBindVertexArray(vao);
// TODO: CHange this logic, now is usefull to debug this should be in a layer.
if ( m_graphicsAPI == rhi::GraphicsAPI::OPENGL )
{
DeviceOpengl::DemoTriangle(shaderCodeVertex, shaderCodeFragment);

//// Cargar y crear el programa de shader
//GLuint shaderProgram = CreateShaderProgram("D:\\IFNITY-ENGINE\\Ifnity\\Ifnity\\shaders_main_vs.bin", "D:\\IFNITY-ENGINE\\Ifnity\\Ifnity\\shaders_main_ps.bin");
//if(shaderProgram == 0)
//{
// std::cerr << "Error al crear el programa de shader" << std::endl;
//
//}

}
while ( isRunning() )
{
//if use D3D11 resize the swapchain this is in other place.
/*auto d3d11Manager = dynamic_cast<DeviceD3D11*>(m_Window.get());
if ( d3d11Manager )
{
d3d11Manager->ResizeSwapChain();
}*/

//// Usar el programa de shader
//glUseProgram(shaderProgram);
m_Window->RenderDemo(m_Window->GetWidth(), m_Window->GetHeight());


glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
while(isRunning())
{
// Iniciar una sección de depuración
glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Frame Start");
glViewport(0, 0, m_Window->GetWidth(), m_Window->GetHeight());
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glPopDebugGroup();

//Imgui Render Frame
{}
ImGuiIO& io = ImGui::GetIO();
App& app = App::GetApp();

io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight());
IFNITY_LOG(LogApp, INFO, "Width: " + std::to_string(app.GetWindow().GetWidth()) + " Height: " + std::to_string(app.GetWindow().GetHeight()));

float time = (float)glfwGetTime();

io.DeltaTime = m_Time > 0.0 ? (float)(time - m_Time) : (float)(1.0f / 60.0f);
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
ImPlot::CreateContext();
ImPlot::ShowDemoWindow();
ImGui::Text("Hello, world %d", 123);
if (ImGui::Button("Save"))
static bool show = true;
//Mostrar numeros aleatorios en imgui en texto

//Crear aleatorio
int random = rand() % 100 + 1;
ImGui::Text("Random number: %d", random);




for(Layer* layer : m_LayerStack)

// Render ImGui Frame
RenderImGuiFrame();
ImGui::ShowDemoWindow();
//Layer Renders.
for ( Layer* layer : m_LayerStack )
{
layer->OnUpdate();
}



m_Window->OnUpdate();
}


m_Window->Shutdown();
}

void App::PushLayer(Layer* layer)
{

m_LayerStack.PushLayer(layer);
layer->OnAttach();


}

void App::PushOverlay(Layer* overlay)
{
m_LayerStack.PushOverlay(overlay);
overlay->OnAttach();

}

void App::InitiateEventBusLayers()
{
for(Layer* layer : m_LayerStack)
for ( Layer* layer : m_LayerStack )
{
layer->ConnectToEventBus(m_EventBus);
}
}

bool App::isRunning() const { return m_GLFWEventListener->getRunning(); }

void App::SetImguiAPI(const rhi::GraphicsAPI& api) const
{
DeviceD3D11* d3d11Manager; //

switch ( api )
{
case IFNITY::rhi::GraphicsAPI::OPENGL:

ImGui_ImplOpenGL3_Init("#version 450"); // TODO: Change version 450 to a variable that can be
// changed in the future.
IFNITY_LOG(LogCore, TRACE, "Imgui API is set to OpenGL");
break;
case IFNITY::rhi::GraphicsAPI::D3D11:
d3d11Manager = dynamic_cast<DeviceD3D11*>(m_Window.get());
if ( d3d11Manager )
{
ImGui_ImplDX11_Init(d3d11Manager->GetDevice(), d3d11Manager->GetDeviceContext());
IFNITY_LOG(LogApp, TRACE, "Imgui API is set to D3D11");
}
else
{
IFNITY_LOG(LogApp, ERROR, "Imgui API istn initialize in IMGUI d3d1manner imposible to cast.");

}

break;
case IFNITY::rhi::GraphicsAPI::D3D12:
break;
case IFNITY::rhi::GraphicsAPI::VULKAN:
break;
case IFNITY::rhi::GraphicsAPI::MAX_GRAPHICS_API:
break;
default:
break;
}
}

bool App::isRunning() const
void App::RenderImGuiFrame() const
{
return m_GLFWEventListener->getRunning();
ImGuiIO& io = ImGui::GetIO();
App& app = App::GetApp();

io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight());
IFNITY_LOG(LogApp, INFO,
"Width imgui : " + std::to_string(app.GetWindow().GetWidth()) +
" Height imgui : " + std::to_string(app.GetWindow().GetHeight()));

float time = (float)glfwGetTime();

io.DeltaTime = m_Time > 0.0 ? (float)(time - m_Time) : (float)(1.0f / 60.0f);

// Render ImguiFrame
auto it = m_ImguiRenderFunctionMap.find(m_graphicsAPI);

//Todo change this because its not optimal , better a function pointer and setting and the initialize .
if ( it != m_ImguiRenderFunctionMap.end() )
{
it->second();
}
else
{
IFNITY_LOG(LogApp, ERROR, "Imgui API not found, impossible to render");
}
}


}
} // namespace IFNITY
12 changes: 12 additions & 0 deletions Ifnity/Ifnity/src/Ifnity/App.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "App.h"

IFNITY_NAMESPACE

// Implementación de la función template
template<typename EvenType>
void App::ConnectEvent() const
{
events::connect<EvenType>(*m_Window->GetGLFWEventSource(), *m_GLFWEventListener);
}

IFNITY_END_NAMESPACE
Loading

0 comments on commit 94c994f

Please sign in to comment.