From 6d60610a3a5a3a6ec407adc37e88dff5396b3a1c Mon Sep 17 00:00:00 2001 From: Alexander Klingenbeck Date: Thu, 4 May 2023 00:01:55 +0200 Subject: [PATCH] Add triangle demo --- CMakeLists.txt | 4 ++-- src/common.h | 24 +------------------- src/main.cpp | 41 ++++++++++++++++++++++++++++++---- src/quickjs.cpp | 3 ++- src/sdl.cpp | 58 ------------------------------------------------- src/shaders.h | 19 ---------------- 6 files changed, 42 insertions(+), 107 deletions(-) delete mode 100644 src/sdl.cpp delete mode 100644 src/shaders.h diff --git a/CMakeLists.txt b/CMakeLists.txt index de883e8..c26525a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,8 +46,8 @@ target_include_directories(quickjs ) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) -file(GLOB files src/main.cpp) +file(GLOB files src/*.cpp) add_executable(${CMAKE_PROJECT_NAME} ${files}) target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE include) target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE src) -target_link_libraries(${CMAKE_PROJECT_NAME} Magnum::Magnum Magnum::GL Magnum::Sdl2Application quickjs) \ No newline at end of file +target_link_libraries(${CMAKE_PROJECT_NAME} Magnum::Magnum Magnum::GL Magnum::Sdl2Application Magnum::Shaders quickjs) \ No newline at end of file diff --git a/src/common.h b/src/common.h index 7baf15d..13eec18 100644 --- a/src/common.h +++ b/src/common.h @@ -5,31 +5,9 @@ extern "C" { #endif - -#include -#include #include #include -#define SDL_MAIN_HANDLED -#include - - -typedef struct { - uint32_t width; - uint32_t height; -} App_Config; - -typedef struct { - SDL_Window* window; - bool quit; -} App_State; - -extern App_Config app_config; -extern App_State app_state;; - -int app_init_sdl(); -int app_update_sdl(); -int app_dispose_sdl(); +#include int app_init_quickjs(int argc, char** argv); int app_update_quickjs(); diff --git a/src/main.cpp b/src/main.cpp index 0ff023e..e4d9739 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,18 @@ +#include #include +#include +#include #include +#include + +#include using namespace Magnum; +using namespace Magnum::GL; +using namespace Magnum::Shaders; +using namespace Magnum::Platform; +using namespace Math::Literals; + class MyApplication: public Platform::Application { public: @@ -9,18 +20,40 @@ class MyApplication: public Platform::Application { private: void drawEvent() override; + Mesh _mesh; + VertexColorGL2D _shader; }; MyApplication::MyApplication(const Arguments& arguments): - Platform::Application{arguments} + Application( + arguments, + Configuration() + .setTitle("Magnum Triangle Example") + .setSize(Vector2i{640,480}) + .setWindowFlags(Sdl2Application::Configuration::WindowFlag::Resizable)) { - // TODO: Add your initialization code here + app_init_quickjs(arguments.argc, arguments.argv); + + struct TriangleVertex { + Vector2 position; + Color3 color; + }; + const TriangleVertex vertices[]{ + {{-0.5f, -0.5f}, 0xff0000_rgbf}, /* Left vertex, red color */ + {{ 0.5f, -0.5f}, 0x00ff00_rgbf}, /* Right vertex, green color */ + {{ 0.0f, 0.5f}, 0x0000ff_rgbf} /* Top vertex, blue color */ + }; + _mesh.setCount(Containers::arraySize(vertices)) + .addVertexBuffer(Buffer(vertices), 0, + VertexColorGL2D::Position(), + VertexColorGL2D::Color3()); } void MyApplication::drawEvent() { - GL::defaultFramebuffer.clear(GL::FramebufferClear::Color); + defaultFramebuffer.clear(FramebufferClear::Color); + app_update_quickjs(); - // TODO: Add your drawing code here + _shader.draw(_mesh); swapBuffers(); } diff --git a/src/quickjs.cpp b/src/quickjs.cpp index 1cd1163..f21bf19 100644 --- a/src/quickjs.cpp +++ b/src/quickjs.cpp @@ -1,7 +1,8 @@ #include -#include "common.h" #include +#include "common.h" + static JSContext *JS_NewCustomContext(JSRuntime *rt); static int eval_buf(JSContext *ctx, const void *buf, int buf_len, const char *filename, int eval_flags); diff --git a/src/sdl.cpp b/src/sdl.cpp deleted file mode 100644 index 1e9d892..0000000 --- a/src/sdl.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "common.h" - -static SDL_Event event; -static bool is_initialized = false; -static bool is_window_created = false; -static Uint32 init_flags = SDL_INIT_VIDEO; - -int app_init_sdl(){ - // Initialize SDL - if (SDL_Init(init_flags) < 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL: %s", SDL_GetError()); - return -1; - } - is_initialized = true; - // Create a window and renderer - app_state.window = SDL_CreateWindow("hello bgfx", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - app_config.width, - app_config.height, - SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); - if (!app_state.window) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create window: %s", SDL_GetError()); - return -1; - } - is_window_created = true; - - SDL_GLContext context = SDL_GL_CreateContext(app_state.window); - if (context == nullptr) { - // handle error - return 1; - } - - return 0; -} - -int app_update_sdl(){ - // Handle events - while (SDL_PollEvent(&event)) { - SDL_KeyCode keycode; - switch (event.type) { - case SDL_QUIT: - app_state.quit = true; - break; - default: - break; - } - } - return 0; -} - -int app_dispose_sdl(){ - if(is_window_created) - SDL_DestroyWindow(app_state.window); - - if(is_initialized) - SDL_Quit(); -} \ No newline at end of file diff --git a/src/shaders.h b/src/shaders.h deleted file mode 100644 index a132514..0000000 --- a/src/shaders.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef SHADERS_H -#define SHADERS_H - -#include - -#if BX_PLATFORM_WINDOWS -#include -#include -#include -#include -#elif BX_PLATFORM_OSX -#include -#include -#elif (BX_PLATFORM_LINUX || BX_PLATFORM_EMSCRIPTEN) -#include -#include -#endif - -#endif \ No newline at end of file