0

I'm a beginner and i want to start learning sdl2 with opengl. I followed a tutorial on how to
setup them, but i have problem compiling the test code: i actually don't know how to compile it. In the tutorial it uses a vs code extension that apparently doesn't work anymore so i didn't manage to replicate that. Also i'd like to understand how compiling works.

this is the code:

// std
#include <stdio.h>

// opengl
#include <GL/glew.h>

// sdl
#include <SDL2/SDL.h>

#define SCREEN_SIZE_X 800
#define SCREEN_SIZE_Y 600

int main (int argc, char* argv[])
{
    // ----- Initialize SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        fprintf(stderr, "SDL could not initialize\n");
        return 1;
    }

    // ----- Create window
    SDL_Window* window = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_SIZE_X, SCREEN_SIZE_Y, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (!window)
    {
        fprintf(stderr, "Error creating window.\n");
        return 2;
    }

    // ----- SDL OpenGL settings
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

    // ----- SDL OpenGL context
    SDL_GLContext glContext = SDL_GL_CreateContext(window);

    // ----- SDL v-sync
    SDL_GL_SetSwapInterval(1);

    // ----- GLEW
    glewInit();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // ----- Game loop
    bool quit = false;
    while (quit == false)
    {
        SDL_Event windowEvent;
        while (SDL_PollEvent(&windowEvent))
        {
            if (windowEvent.type == SDL_QUIT)
            {
                quit = true;
                break;
            }
        }

        /*
            do drawing here
        */

        SDL_GL_SwapWindow(window);
    }

    // ----- Clean up
    SDL_GL_DeleteContext(glContext);

    return 0;
}

I'm working on visual studio code with wsl2. My best try so far (even i if i don't know exactly what i did) was:

g++ -o myapp main.cpp -I/usr/include/SDL2 -L/usr/lib/x86_64-linux-gnu -lSDL -lGL -lglut -lGL -lGLEW

but i get this errors:

/usr/bin/ld: /tmp/ccJhGZe1.o: in function main': main.cpp:(.text+0x82): undefined reference to SDL_CreateWindow' /usr/bin/ld: main.cpp:(.text+0x11d): undefined reference to SDL_GL_CreateContext' /usr/bin/ld: main.cpp:(.text+0x12b): undefined reference to SDL_GL_SetSwapInterval' /usr/bin/ld: main.cpp:(.text+0x198): undefined reference to SDL_GL_SwapWindow' /usr/bin/ld: main.cpp:(.text+0x1a6): undefined reference to SDL_GL_DeleteContext' collect2: error: ld returned 1 exit status

My question is: what is the best way to compile such a file (like makefile or from terminal) and how to do it ?

Thanks for the patience!

0 Answers0