TestTriangle works

This commit is contained in:
2026-02-04 17:31:17 -06:00
parent 9dfabc6dae
commit 8581589bfe
3 changed files with 79 additions and 165 deletions

View File

@@ -140,6 +140,41 @@
}; };
TestTrianglesdl = pkgs.stdenvNoCC.mkDerivation {
name = "TestTriangle";
src = ./.;
nativeBuildInputs = with pkgs; [
clang
];
buildInputs = with pkgs; [
sdl3
glew
];
buildPhase = ''
clang++ \
modules/Archimedes-Modules/TestTriangle/*.cpp \
modules/WindowModule/*.cpp \
-DRENDERER=1 \
-DWINDOW=2 \
-DTESTTRIANGLE_DYNAMIC \
-fpic -shared \
-I include -I . \
-lEGL -lSDL3 -lGLEW \
-Wall \
-o $name
'';
installPhase = ''
mkdir -p $out/bin
cp $name $out/bin
'';
};
TestTriangle = pkgs.stdenvNoCC.mkDerivation { TestTriangle = pkgs.stdenvNoCC.mkDerivation {
name = "TestTriangle"; name = "TestTriangle";
@@ -158,6 +193,7 @@
buildPhase = '' buildPhase = ''
clang++ \ clang++ \
modules/Archimedes-Modules/TestTriangle/*.cpp \ modules/Archimedes-Modules/TestTriangle/*.cpp \
modules/WindowModule/*.cpp \
-DRENDERER=1 \ -DRENDERER=1 \
-DWINDOW=1 \ -DWINDOW=1 \
-DTESTTRIANGLE_DYNAMIC \ -DTESTTRIANGLE_DYNAMIC \

View File

@@ -1,3 +1,6 @@
// This only works with opengl!!!!
#include "TestTriangle.h" #include "TestTriangle.h"
@@ -5,132 +8,57 @@
TestTriangle::TestTriangle(Archimedes::App* a, void* h) : Archimedes::Module(a, h) { TestTriangle::TestTriangle(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
name = "TestTriangle"; name = "TestTriangle";
WindowModule* wm = new WindowModule(a, h);
deps[*wm] = wm;
} }
TestTriangle::~TestTriangle() { TestTriangle::~TestTriangle() {
if(app) {
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(); if(app) {
WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
wm->getRenderer()->getCmdList().erase(rcmd_it);
/*
#if WINDOW == 2
wm->removeEventFn(ecmd_it);
#endif
*/
wm->releaseWindow(window);
} }
} }
void TestTriangle::onLoad() { void TestTriangle::onLoad() {
////////////////////////////register events // get window
app->registerEvent(Archimedes::ResizeWindowEvent()); WindowModule* wm; { wm = (WindowModule*) moduleInstances[WindowModule()]; }
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; if(!wm) {
std::cout << "No WindowModule for ImguiModule!\n";
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(); 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) { window = wm->aquireWindow();
std::cout << "glfwCreateWindow failed!\n";
glfwTerminate();
std::abort();
}
glfwMakeContextCurrent(w);
glfwSwapInterval(1);
glfwSetWindowUserPointer(w, &data);
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){ wm->getRenderer()->getCmdList().push_back([this](){
Archimedes::WindowData& d = *(Archimedes::WindowData*) glfwGetWindowUserPointer(window); // draw our first triangle
glUseProgram(shaderProgram);
d.sendEvent(new Archimedes::ResizeWindowEvent(w, h)); glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); // no need to unbind it every time
}); });
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){ rcmd_it = --wm->getRenderer()->getCmdList().end()++;
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){ //////////////////////glew
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;
/*
if(glewInit() != GLEW_OK) { if(glewInit() != GLEW_OK) {
std::cout << "glew is not ok!" << std::endl; std::cout << "glew is not ok!" << std::endl;
std::abort(); std::abort();
} }
*/
///////////////////////////////NUCLEAR ///////////////////////////////NUCLEAR
@@ -202,62 +130,10 @@ void TestTriangle::onLoad() {
} }
void TestTriangle::run() { void TestTriangle::run() {
// render
// ------
glClearColor(0.2f, 0.2f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(w);
glfwPollEvents();
} }
bool TestTriangle::onEvent(const Archimedes::Event& e) { 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; return false;
} }

View File

@@ -1,12 +1,14 @@
// This only works with opengl!!!!
#include "Archimedes.h" #include "Archimedes.h"
#include "utils/Window/WindowEvents.h"
#include "modules/WindowModule/WindowModule.h"
#define GLEW_STATIC #define GLEW_STATIC
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h>
class TestTriangle : public Archimedes::Module { class TestTriangle : public Archimedes::Module {
@@ -25,9 +27,9 @@ class TestTriangle : public Archimedes::Module {
private: private:
GLFWwindow* w; Archimedes::Window* window;
Archimedes::WindowData data; std::list<std::function<void()>>::iterator rcmd_it;
const char *vertexShaderSource = "#version 330 core\n" const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" "layout (location = 0) in vec3 aPos;\n"