generate data in thread

This commit is contained in:
Sebastien Bourdeauducq 2023-08-23 15:53:50 +08:00
parent 26af4416c2
commit 9c47f72298
2 changed files with 40 additions and 12 deletions

View File

@ -4,7 +4,7 @@ SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui
SOURCES += $(IMGUI_DIR)/backends/imgui_impl_glfw.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl3.cpp
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
CXXFLAGS = -std=c++11 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends
CXXFLAGS += -g -Wall -Wformat -O2
LIBS =

View File

@ -1,4 +1,8 @@
#include <cstdlib>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
#include <GLFW/glfw3.h>
@ -6,6 +10,28 @@
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
static std::atomic<bool> terminate_dsp;
static std::mutex waterfall_data_mutex;
static int waterfall_width = 1600;
static int waterfall_height = 700;
static unsigned int waterfall_data[1600*700];
static void dsp_thread() {
using namespace std::chrono_literals;
int t = 0;
while(!terminate_dsp) {
t++;
{
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
for(int y=0;y<waterfall_height;y++)
for(int x=0;x<waterfall_width;x++)
waterfall_data[y*waterfall_width+x] = (x+y+t) | 0xff000000;
}
std::this_thread::sleep_for(20ms);
};
}
int main(int, char**)
{
if (!glfwInit())
@ -38,9 +64,6 @@ int main(int, char**)
ImGui_ImplOpenGL3_Init("#version 130");
std::atexit(ImGui_ImplOpenGL3_Shutdown);
int waterfall_width = 1600;
int waterfall_height = 700;
unsigned int waterfall_data[1600*700];
GLuint waterfall;
glGenTextures(1, &waterfall);
glBindTexture(GL_TEXTURE_2D, waterfall);
@ -49,23 +72,28 @@ int main(int, char**)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int t = 0;
terminate_dsp = false;
static std::thread dsp_thread_h = std::thread(dsp_thread);
static auto TerminateDSP = []() {
terminate_dsp = true;
dsp_thread_h.join();
};
std::atexit(TerminateDSP);
bool exit = false;
while (!exit && !glfwWindowShouldClose(window)) {
while(!exit && !glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
t++;
for(int y=0;y<waterfall_height;y++)
for(int x=0;x<waterfall_width;x++)
waterfall_data[y*waterfall_width+x] = (x+y+t) | 0xff000000;
glBindTexture(GL_TEXTURE_2D, waterfall);
#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
{
std::lock_guard<std::mutex> guard(waterfall_data_mutex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, waterfall_width, waterfall_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, waterfall_data);
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();