work on TestTriangle
This commit is contained in:
@@ -51,7 +51,7 @@
|
||||
buildPhase = ''
|
||||
clang++ \
|
||||
src/example_apps/ImguiEmbed/*.cpp \
|
||||
modules/Archimedes-Modules/TestImgui/*.cpp \
|
||||
modules/MainGUI/*.cpp \
|
||||
modules/WindowModule/*.cpp \
|
||||
modules/ImguiModule/*.cpp \
|
||||
$imgui/backends/imgui_impl_glfw.cpp \
|
||||
@@ -61,7 +61,7 @@
|
||||
-DRENDERER=1 \
|
||||
-DWINDOW=1 \
|
||||
-I include -I $imgui -I . \
|
||||
-lEGL -lglfw -lGLEW \
|
||||
-lGL -lEGL -lglfw -lGLEW \
|
||||
-Wall \
|
||||
-o $name -DIMGUI_IMPL_GLFW_DISABLE_X11
|
||||
'';
|
||||
|
||||
@@ -158,7 +158,6 @@
|
||||
buildPhase = ''
|
||||
clang++ \
|
||||
modules/Archimedes-Modules/TestTriangle/*.cpp \
|
||||
modules/WindowModule/*.cpp \
|
||||
-DRENDERER=1 \
|
||||
-DWINDOW=1 \
|
||||
-DTESTTRIANGLE_DYNAMIC \
|
||||
|
||||
@@ -37,6 +37,11 @@ namespace Archimedes {
|
||||
std::cout << "glfwInit failed!\n";
|
||||
std::abort();
|
||||
}
|
||||
#if RENDRER == 1
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
#endif
|
||||
w = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
|
||||
|
||||
if(!w) {
|
||||
|
||||
@@ -1,69 +1,131 @@
|
||||
#include "TestTriangle.h"
|
||||
#include "modules/WindowModule/WindowModule.h"
|
||||
|
||||
#define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
|
||||
|
||||
TestTriangle::TestTriangle(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
||||
|
||||
name = "TestTriangle";
|
||||
|
||||
WindowModule* wm = new WindowModule(a, h);
|
||||
deps[*wm] = wm;
|
||||
}
|
||||
|
||||
TestTriangle::~TestTriangle() {
|
||||
|
||||
if(app) {
|
||||
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
|
||||
app->unregisterEvent(Archimedes::ResizeWindowEvent());
|
||||
app->unregisterEvent(Archimedes::CloseWindowEvent());
|
||||
app->unregisterEvent(Archimedes::KeyPressedWindowEvent());
|
||||
app->unregisterEvent(Archimedes::KeyReleasedWindowEvent());
|
||||
app->unregisterEvent(Archimedes::MouseButtonPressedWindowEvent());
|
||||
app->unregisterEvent(Archimedes::MouseButtonReleasedWindowEvent());
|
||||
app->unregisterEvent(Archimedes::ScrollWindowEvent());
|
||||
app->unregisterEvent(Archimedes::MouseMovedWindowEvent());
|
||||
app->unregisterEvent(Archimedes::FocusedWindowEvent());
|
||||
app->unregisterEvent(Archimedes::FocusLostWindowEvent());
|
||||
app->unregisterEvent(Archimedes::MovedWindowEvent());
|
||||
|
||||
|
||||
wm->getRenderer()->getCmdList().erase(rcmd_it);
|
||||
|
||||
wm->releaseWindow(window);
|
||||
glfwTerminate();
|
||||
}
|
||||
}
|
||||
|
||||
void TestTriangle::onLoad() {
|
||||
////////////////////////////register events
|
||||
app->registerEvent(Archimedes::ResizeWindowEvent());
|
||||
app->registerEvent(Archimedes::CloseWindowEvent());
|
||||
app->registerEvent(Archimedes::KeyPressedWindowEvent());
|
||||
app->registerEvent(Archimedes::KeyReleasedWindowEvent());
|
||||
app->registerEvent(Archimedes::MouseButtonPressedWindowEvent());
|
||||
app->registerEvent(Archimedes::MouseButtonReleasedWindowEvent());
|
||||
app->registerEvent(Archimedes::ScrollWindowEvent());
|
||||
app->registerEvent(Archimedes::MouseMovedWindowEvent());
|
||||
app->registerEvent(Archimedes::FocusedWindowEvent());
|
||||
app->registerEvent(Archimedes::FocusLostWindowEvent());
|
||||
app->registerEvent(Archimedes::MovedWindowEvent());
|
||||
//////////////////////glfw
|
||||
|
||||
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
|
||||
std::cout << "events registered" << std::endl;
|
||||
|
||||
if(!wm) {
|
||||
std::cout << "No WindowModule for TestTriangle!\n";
|
||||
std::abort();
|
||||
}
|
||||
data.window = nullptr;
|
||||
data.sendEvent = [this](Archimedes::Event* e) {
|
||||
app->emitEvent(e);
|
||||
};
|
||||
glfwSetErrorCallback([](int e, const char* m){
|
||||
std::cout << "GLFW Error " << e << ": " << m << std::endl;
|
||||
});
|
||||
|
||||
window = wm->aquireWindow();
|
||||
if(!glfwInit()) {
|
||||
std::cout << "glfwInit failed!\n";
|
||||
std::abort();
|
||||
}
|
||||
#if RENDRER == 1
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
#endif
|
||||
w = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
|
||||
|
||||
wm->getRenderer()->getCmdList().push_back([this](){
|
||||
// 0. copy our vertices array in a buffer for OpenGL to use
|
||||
//glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
//glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
// 1. then set the vertex attributes pointers
|
||||
//glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
//glEnableVertexAttribArray(0);
|
||||
// 2. use our shader program when we want to render an object
|
||||
glUseProgram(program);
|
||||
glBindVertexArray(vao);
|
||||
// 3. now draw the object
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
//glBindVertexArray(0);
|
||||
});
|
||||
if(!w) {
|
||||
std::cout << "glfwCreateWindow failed!\n";
|
||||
glfwTerminate();
|
||||
std::abort();
|
||||
}
|
||||
glfwMakeContextCurrent(w);
|
||||
glfwSwapInterval(1);
|
||||
|
||||
rcmd_it = --wm->getRenderer()->getCmdList().end()++;
|
||||
glfwSetWindowUserPointer(w, &data);
|
||||
|
||||
////////////////////////
|
||||
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
|
||||
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
|
||||
|
||||
glGenBuffers(1, &vbo);
|
||||
glGenVertexArrays(1, &vao);
|
||||
d.sendEvent(new Archimedes::ResizeWindowEvent(w, h));
|
||||
});
|
||||
|
||||
glBindVertexArray(vao);
|
||||
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
|
||||
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
d.sendEvent(new Archimedes::CloseWindowEvent(d.window));
|
||||
});
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){
|
||||
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
|
||||
|
||||
switch(action) {
|
||||
case GLFW_PRESS:
|
||||
d.sendEvent(new Archimedes::KeyPressedWindowEvent(key, 0));
|
||||
break;
|
||||
case GLFW_RELEASE:
|
||||
d.sendEvent(new Archimedes::KeyReleasedWindowEvent(key));
|
||||
break;
|
||||
case GLFW_REPEAT:
|
||||
d.sendEvent(new Archimedes::KeyPressedWindowEvent(key, 1));
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){
|
||||
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
|
||||
|
||||
switch(action) {
|
||||
case GLFW_PRESS:
|
||||
d.sendEvent(new Archimedes::MouseButtonPressedWindowEvent(button));
|
||||
break;
|
||||
case GLFW_RELEASE:
|
||||
d.sendEvent(new Archimedes::MouseButtonReleasedWindowEvent(button));
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){
|
||||
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
|
||||
|
||||
d.sendEvent(new Archimedes::ScrollWindowEvent(dx, dy));
|
||||
});
|
||||
|
||||
glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){
|
||||
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
|
||||
|
||||
d.sendEvent(new Archimedes::MouseMovedWindowEvent(dx, dy));
|
||||
});
|
||||
|
||||
//////////////////////gl
|
||||
std::cout << "glfw setup" << std::endl;
|
||||
|
||||
vs = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vs, 1, &vertexShaderSource, NULL);
|
||||
@@ -102,11 +164,84 @@ void TestTriangle::onLoad() {
|
||||
std::abort();
|
||||
};
|
||||
|
||||
std::cout << "shader program complete" << std::endl;
|
||||
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
|
||||
std::cout << "shaders destroyed" << std::endl;
|
||||
|
||||
glGenBuffers(1, &vbo);
|
||||
glGenVertexArrays(1, &vao);
|
||||
|
||||
glBindVertexArray(vao);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
|
||||
std::cout << "load success" << std::endl;
|
||||
}
|
||||
|
||||
void TestTriangle::run() {
|
||||
|
||||
std::cout << "run" << std::endl;
|
||||
glClearColor(0.2, 0.2, 0.4, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(this->program);
|
||||
glBindVertexArray(this->vao);
|
||||
// 3. now draw the object
|
||||
int f = 0, c = 3;
|
||||
glMultiDrawArrays(GL_TRIANGLES, &f, &c, 1);
|
||||
//glBindVertexArray(0);
|
||||
|
||||
glfwSwapBuffers(w);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
bool TestTriangle::onEvent(const Archimedes::Event& e) {
|
||||
|
||||
unsigned int type = app->getEventType(e);
|
||||
|
||||
if(type == app->getEventType(Archimedes::ResizeWindowEvent())) {
|
||||
Archimedes::ResizeWindowEvent& event = (Archimedes::ResizeWindowEvent&) e;
|
||||
glViewport(0, 0, event.width, event.height);
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::CloseWindowEvent())) {
|
||||
app->emitEvent(new Archimedes::DoUnloadModuleEvent(name));
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::KeyPressedWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::KeyReleasedWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::MouseButtonPressedWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::MouseButtonReleasedWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::ScrollWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::MouseMovedWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::FocusedWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::FocusLostWindowEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == app->getEventType(Archimedes::MovedWindowEvent())) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
#include "Archimedes.h"
|
||||
|
||||
#include "modules/WindowModule/WindowModule.h"
|
||||
#include "utils/Window/WindowEvents.h"
|
||||
|
||||
#define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
|
||||
class TestTriangle : public Archimedes::Module {
|
||||
|
||||
@@ -11,16 +17,17 @@ class TestTriangle : public Archimedes::Module {
|
||||
|
||||
~TestTriangle();
|
||||
|
||||
void onLoad();
|
||||
void onLoad() override;
|
||||
|
||||
void run();
|
||||
void run() override;
|
||||
|
||||
bool onEvent(const Archimedes::Event& e) override;
|
||||
|
||||
private:
|
||||
|
||||
Archimedes::Window* window;
|
||||
GLFWwindow* w;
|
||||
|
||||
std::list<std::function<void()>>::iterator rcmd_it;
|
||||
Archimedes::WindowData data;
|
||||
|
||||
float vertices[3 * 3] = {
|
||||
-0.5f, -0.5f, 0.0f,
|
||||
|
||||
@@ -1,31 +1,71 @@
|
||||
#include "ImguiEmbed.h"
|
||||
|
||||
bool ImguiEmbed::onEvent(const Archimedes::Event& event) {
|
||||
|
||||
unsigned int type = getEventType(event);
|
||||
|
||||
if(type == getEventType(Archimedes::DoLoadModuleEvent())) {
|
||||
|
||||
Archimedes::DoLoadModuleEvent& e = (Archimedes::DoLoadModuleEvent&) event;
|
||||
|
||||
startModule(e.module);
|
||||
|
||||
return true;
|
||||
|
||||
} else if(type == getEventType(Archimedes::DoUnloadModuleEvent())) {
|
||||
|
||||
Archimedes::DoUnloadModuleEvent& e = (Archimedes::DoUnloadModuleEvent&) event;
|
||||
|
||||
stopModule(e.module);
|
||||
|
||||
return true;
|
||||
|
||||
} else if(type == getEventType(Archimedes::LoadModuleEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == getEventType(Archimedes::UnloadModuleEvent())) {
|
||||
|
||||
return true;
|
||||
} else if(type == getEventType(Archimedes::AnonymousEvent())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ImguiEmbed::run() {
|
||||
|
||||
for(auto m : runOrder)
|
||||
for(std::string m : runOrder) {
|
||||
modules[m]->onLoad();
|
||||
}
|
||||
|
||||
|
||||
// Main loop
|
||||
while (!done && !runOrder.empty()) {
|
||||
|
||||
for(auto m : runOrder) {
|
||||
for(std::string m : runOrder) {
|
||||
modules[m]->run();
|
||||
}
|
||||
|
||||
for(auto m : toClose) {
|
||||
handleEvents();
|
||||
|
||||
for(std::string m : toClose) {
|
||||
unload(m);
|
||||
}
|
||||
toClose.clear();
|
||||
|
||||
for(auto m : toOpen) {
|
||||
static Archimedes::Module* n;
|
||||
if(std::holds_alternative<std::string>(m)) {
|
||||
load(std::get<std::string>(m))->onLoad();
|
||||
n = load(std::get<std::string>(m));
|
||||
} else {
|
||||
load(std::get<Archimedes::Module*>(m))->onLoad();
|
||||
n = load(std::get<Archimedes::Module*>(m));
|
||||
}
|
||||
if(n) {
|
||||
n->onLoad();
|
||||
n = nullptr;
|
||||
}
|
||||
}
|
||||
toOpen.clear();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#define ENTRYPOINT
|
||||
#include "Archimedes.h"
|
||||
|
||||
#include "modules/Archimedes-Modules/TestImgui/TestImgui.h"
|
||||
#include "modules/MainGUI/MainGUI.h"
|
||||
|
||||
class ImguiEmbed : public Archimedes::App {
|
||||
|
||||
@@ -13,7 +13,7 @@ class ImguiEmbed : public Archimedes::App {
|
||||
|
||||
public:
|
||||
ImguiEmbed() {
|
||||
Archimedes::Module* m = (Archimedes::Module*) new TestImgui(Get(), nullptr);
|
||||
Archimedes::Module* m = (Archimedes::Module*) new MainGUI(Get(), nullptr);
|
||||
|
||||
load(m);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user