DEV Community


Posted on

create a window in SDL2 for any OS

step 1
first we need to import SDL2 into our main.c project. However there are different library's for every OS so we can allow the computer to choose the needed one by doing this:

#include <stdio.h> /* printf and fprintf */
#include <stdbool.h>
#ifdef _WIN32
#include <SDL/SDL.h> /* Windows-specific SDL2 library */
#include <SDL2/SDL.h> /* macOS- and GNU/Linux-specific */
Enter fullscreen mode Exit fullscreen mode

step 2
to make the window with a title and window size we can do:

int main() {

    SDL_Window* w = SDL_CreateWindow(
        /* If you flip this, SDL_SetWindowTitle() below starts working */
        #if 1
        "hello window!",
        "hello window!",
        400, 400, 0); // window size

    /* If you call this, the below stops working */
    //SDL_SetWindowTitle(w, "Another title without UTF-8");

    /* This works only if the previously set title had an em-dash as well */
    SDL_SetWindowTitle(w, "hello window!"); //title

    SDL_Event event;
        if(event.type == SDL_QUIT) break;

Enter fullscreen mode Exit fullscreen mode

this sets the window size and title.

to compile our main.c file with g++ we can do:

g++ -o main main.c -lSDL2
Enter fullscreen mode Exit fullscreen mode

and run it with:

Enter fullscreen mode Exit fullscreen mode

enjoy and have a good day

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git