memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.07k stars 767 forks source link

SDL example? #529

Open letarg0 opened 5 years ago

letarg0 commented 5 years ago

Is possible to add sdl2+OpenGL example

I need draw on background, this is possible?

BrodaJarek commented 5 years ago
SDL_Init(SDL_INIT_EVERYTHING);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

SDL_Window* window = SDL_CreateWindow("window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);

glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK) {
    printf("Could not init glew.\n");
    return -1;
}

NVGcontext* vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);

int windowW = 0, windowH = 0;
while(!quit) {
    while(SDL_PollEvent(&event)) {
        if(event.type == SDL_QUIT) {
            quit = true;
        }
    }
    SDL_GetWindowSize(window, &windowW , &windowH);

    glViewport(0, 0, windowW , windowH);
    if (premult)
        glClearColor(0,0,0,0);
    else
        glClearColor(0.3f, 0.3f, 0.32f, 1.0f);

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

    nvgBeginFrame(vg, windowW , windowH, (float)windowW / ( float)windowH);

    NVGpaint gradient = nvgLinearGradient(vg, 0.0f, 0.0f, 100.0f, 0.0f, nvgRGBA(255, 255, 255, 255), 
    nvgRGBA(0, 0, 0, 0));
    nvgBeginPath(vg);
    nvgRect(vg, 0.0f, 0.0f, 100.0f, 100.0f);
    nvgFillPaint(vg, gradient);
    nvgFill(vg);

    nvgEndFrame(vg);

    SDL_GL_SwapWindow(window);
}
nvgDeleteGL3(vg);
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);

that should work. Lemme know if it does not.

letarg0 commented 5 years ago

I compile on mac in build directory:

gcc -lSDL2 -lm -lGLEW -framework OpenGL -I../src -I../example -g -Wall -o "example_sdl2_gl3" ./libnanovg.a "../example/example_sdl2_gl3.c"

strasznie duzo 'glClearColor' has been explicitly marked deprecated here Starsznie duzo musialem poprawiac i w końcu nie wiem czy nie wywaliłem za dużo. Kompiluje się ale potem

Shader shader/vert error:

Segmentation fault: 11

https://pastebin.com/RRFqDXna

rogerdahl commented 5 years ago

Try to reduce the SDL GL context (4.6) to match the NanoVG context (3.x).

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);

and

NVGcontext* vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
BrodaJarek commented 5 years ago

Yeah, you are right. version should be lower. I cannot see difference between my and your context.

PL: Co do kodu, glewExperimental na true ustaw. To coś tam dawało, już nie pamiętam co dokładnie. No i jak zmienić wersje to powinno działać. ewentualnie jak w shaderach zmienisz troche tak żeby było kompatybilne z 4.x, no chyba, że twój hardware nie obsługuje 4.x

ENG: About the code: you should set glewExperimental to true. I do not remember what this option exactly does, but it meant to be true. And when you change gl version it should work.

rogerdahl commented 5 years ago

@BrodaJarek Sorry, those were just a copy of your lines. I think changing major and minor 4.6 to 3.1 will work.

I don't understand Polish, unfortunately. I think it's best practice to stick to English here.