mirror of https://github.com/mode777/rayjs.git
Add triangle demo
This commit is contained in:
parent
c12676693e
commit
6d60610a3a
|
@ -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)
|
||||
target_link_libraries(${CMAKE_PROJECT_NAME} Magnum::Magnum Magnum::GL Magnum::Sdl2Application Magnum::Shaders quickjs)
|
24
src/common.h
24
src/common.h
|
@ -5,31 +5,9 @@ extern "C"
|
|||
{
|
||||
#endif
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include <SDL.h>
|
||||
|
||||
|
||||
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 <string.h>
|
||||
|
||||
int app_init_quickjs(int argc, char** argv);
|
||||
int app_update_quickjs();
|
||||
|
|
41
src/main.cpp
41
src/main.cpp
|
@ -1,7 +1,18 @@
|
|||
#include <Magnum/GL/Buffer.h>
|
||||
#include <Magnum/GL/DefaultFramebuffer.h>
|
||||
#include <Magnum/GL/Mesh.h>
|
||||
#include <Magnum/Math/Color.h>
|
||||
#include <Magnum/Platform/Sdl2Application.h>
|
||||
#include <Magnum/Shaders/VertexColorGL.h>
|
||||
|
||||
#include <common.h>
|
||||
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#include <quickjs.h>
|
||||
#include "common.h"
|
||||
#include <quickjs-libc.h>
|
||||
|
||||
#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);
|
||||
|
|
58
src/sdl.cpp
58
src/sdl.cpp
|
@ -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();
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
#ifndef SHADERS_H
|
||||
#define SHADERS_H
|
||||
|
||||
#include <bgfx/c99/bgfx.h>
|
||||
|
||||
#if BX_PLATFORM_WINDOWS
|
||||
#include <vs.sc.dx9.bin.h>
|
||||
#include <vs.sc.dx11.bin.h>
|
||||
#include <fs.sc.dx9.bin.h>
|
||||
#include <fs.sc.dx11.bin.h>
|
||||
#elif BX_PLATFORM_OSX
|
||||
#include <generated/shaders/vs.sc.mtl.bin.h>
|
||||
#include <generated/shaders/fs.sc.mtl.bin.h>
|
||||
#elif (BX_PLATFORM_LINUX || BX_PLATFORM_EMSCRIPTEN)
|
||||
#include <vs.sc.glsl.bin.h>
|
||||
#include <fs.sc.glsl.bin.h>
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue