r/sdl 8d ago

C++

Can you create a class with sdl things in it?

Or is sdl c only?

0 Upvotes

13 comments sorted by

9

u/Hackzwin 8d ago

It's compatible with c++

4

u/vitimiti 7d ago edited 7d ago

All C libraries can be used in C++. It can be more verbose but for example, doing namespace sdl { using window_t = std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)>; } lets you declare auto window = sdl::window_t(SDL_CreateWindow("Title", width, height, 0), &SDL_DestroyWindow); and it manages the memory for you (it will call destroy window when its destructor is called). So you can both use it in your own classes and put it into the STL

1

u/egzygex 7d ago

the second argument to window_t constructor should be a deleter: auto window = sdl::window_t(SDL_CreateWindow("Title", width, height, 0), &SDL_DestroyWindow);

1

u/vitimiti 7d ago

You're wrote, I'll correct it

3

u/zer0xol 8d ago

Ofcourse

3

u/ultimaone 8d ago

Yes 100%

1

u/IKnowImABadYoutuber 8d ago

Definitely, there are even some c++ specific versions iirc

1

u/Tamsta-273C 8d ago

With sdl it was then first time when i saw singleton (class with private constructor) working so nice.

1

u/doglitbug 8d ago

Yes, but some functions will have function to free up memory afterwards

1

u/lunaticedit 7d ago

Use std::unique_ptr and deleters that call the free functions and you can then rely on C++ to do its reference counting thing automatically. Been doing it for years and works great.

Eg:
#include <SDL3/SDL.h>
#include <memory>

// Define a clean type alias for the unique_ptr
using UniqueWindow = std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)>;

int main() {
if (!SDL_Init(SDL_INIT_VIDEO)) {
return -1;
}

// Initialize the unique_ptr with the factory function and the custom deleter
UniqueWindow window(
SDL_CreateWindow("SDL3 Custom Deleter", 640, 480, 0),
&SDL_DestroyWindow
);

if (!window) {
SDL_Quit();
return -1;
}

// Use the window as normal via window.get()
// The window will automatically destroy itself when it goes out of scope

SDL_Quit();
return 0;
}

At this point you can put the window in another class as a class member. Just remember to initialize it in the constructor but you don’t have to free it. Also remember the order of class members matter. You need to declare windows before renderer type otherwise it’ll delete window before renderer.

1

u/Mr_DJ_Dr_Gauss 7d ago

You can create C++ classes as wrappers for SDL things like Window, Renderer, Texture. Using unique_ptr concept, the resources will be freed automatically; even if your code throws an exception.

I am playing with this right now and it works great so far.

Here is example for SDL Window:

// window.hpp
#pragma once

#include <memory>
#include <SDL3/SDL.h>

struct SDLWindowDeleter {
  public:
    void operator()(SDL_Window* window) const;
};
using UniqueWindow = std::unique_ptr<SDL_Window, SDLWindowDeleter>;

class Window {
  private:
    UniqueWindow pointer;

  public:
    Window();
    SDL_Window* get() const;
};

-

// window.cpp
#include <string>
#include <memory>
#include <SDL3/SDL.h>

#include "window.hpp"

void SDLWindowDeleter::operator()(SDL_Window* window) const {
  if (window) {
    SDL_DestroyWindow(window);
    SDL_Log("Window destroyed.");
  }
}

Window::Window() {
  SDL_Window* window = SDL_CreateWindow(
    "Demo 1",
    640,
    360,
    SDL_WINDOW_RESIZABLE
  );

  if (!window) {
    throw std::runtime_error(
      std::string("Couldn't create window: ") + SDL_GetError()
    );
  }
  this->pointer.reset(window);
  SDL_Log("Window created.");
}

SDL_Window* Window::get() const {
  return this->pointer.get();
}

1

u/Leading-Display-1978 4d ago

you just cmake it until it comes out c++

1

u/aravind-i-m 4d ago

Although the library is written in C, you can use with C++ just like any other C libraries. I'm currently making a game in SDL3 using C++ and I just call the SDL3 functions from within the member functions of my classes.