work on TestTriangle

This commit is contained in:
2026-02-04 14:37:06 -06:00
parent 5ab345460c
commit 3f102638b0
7 changed files with 257 additions and 71 deletions

View File

@@ -1,70 +1,132 @@
#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()]; }
wm->getRenderer()->getCmdList().erase(rcmd_it);
wm->releaseWindow(window);
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());
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
std::cout << "events registered" << std::endl;
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;
});
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);
if(!w) {
std::cout << "glfwCreateWindow failed!\n";
glfwTerminate();
std::abort();
}
glfwMakeContextCurrent(w);
glfwSwapInterval(1);
glfwSetWindowUserPointer(w, &data);
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new Archimedes::ResizeWindowEvent(w, h));
});
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new Archimedes::CloseWindowEvent(d.window));
});
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;
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
if(!wm) {
std::cout << "No WindowModule for TestTriangle!\n";
std::abort();
}
window = wm->aquireWindow();
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);
});
rcmd_it = --wm->getRenderer()->getCmdList().end()++;
////////////////////////
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);
vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertexShaderSource, NULL);
glCompileShader(vs);
@@ -101,12 +163,85 @@ void TestTriangle::onLoad() {
std::cout << "shader linking failed!" << std::endl;
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;
}