Indices and tables

SBDL (Sadegh & Borjian Directmedia Layer)

A wrapper around SDL2 which make use of SDL much simpler

Motivation

For years in Shahid Beheshti University, ITP final project was a game written using SDL library. Since the bare SDL library is too verbose and hard to use for first-year students in college, each year TAs create their own wrapper for SDL to ease the SDL for students. This library is successor of Hash<Written for ITP96Fall> and Genio2<Written for ITP95Fall> with great experience which we learned from those libraries.

Mohammad Sadegh Dehghan & Amin Borjian wrote this library to fulfill all the needs of a first-term student for ITP97Fall course and all upcoming ITP courses in future.

Basic Usage

  1. Put include directories of SDL2,SDL2_image,SDL2_ttf,SDL2_mixer in your compiler’s include directory.
  2. Put lib directories of SDL2,SDL2_image,SDL2_ttf,SDL2_mixer in your linker’s path.
  3. Put SDL2Main.lib,SDL2.lib,SDL2_image.lib,SDL2_mixer.lib,SDL2_ttf.lib in linker’s dependencies.
  4. Start Coding:
#include "SBDL.h"

using namespace std;

int main(int argc, char *argv[])
{
        const int window_width = 500;
        const int window_height = 500;
        SBDL::InitEngine("SBDL Test", window_width, window_height);

        const int FPS = 60; //frame per second
        const int delay = 1000 / FPS; //delay we need at each frame


        while (SBDL::isRunning()) {
                int startTime = SBDL::getTime();

                SBDL::updateEvents();
                SBDL::clearRenderScreen();

                //Game logic code

                SBDL::updateRenderScreen();

                int elapsedTime = SBDL::getTime() - startTime;
                if (elapsedTime < delay)
                        SBDL::delay(delay - elapsedTime);

        }

}

Contribution

If you find any bugs,need a new feature,etc feel free to create an issue[https://github.com/MSDehghan/SBDL/issues]

Pull requests are also appreciated.

License

This library is released under GPL v3

Function Refrence

namespace SBDL

Functions

bool operator==(const SDL_Rect &x, const SDL_Rect &y)

comparator of SDL_Rect

bool isRunning()

used for checking state of program

Return
state of SDL

void InitEngine(const std::string &windowsTitle, int windowsWidth, int windowsHeight, Uint8 r = 255, Uint8 g = 255, Uint8 b = 255)

initialize SDL and show a simple empty window for drawing texture on it before start using SDL functions and types, first initialize engine

Parameters
  • windowsTitle: title of window
  • windowsWidth: width of window
  • windowsHeight: height of window
  • r: red color of default background
  • g: green color of default background
  • b: blue color of default background

void updateEvents()

update state of keyboard buttons (release or push) and mouse if ESCAPE pressed, application will stop call this function in a loop after initialize engine for get updated state all times

bool key(SDL_Scancode scanCode)

indicate whether a key with specific scanCode is pressed or not

Return
true if sepcific keyboard button pressed
Parameters

unsigned int getTime()

get Milliseconds since program was started.

void clearRenderScreen()

clear the current rendering target

void updateRenderScreen()

update the screen and apply all changes

void delay(Uint32 frameRate)

wait a few milliseconds before continue process of application

Parameters
  • frameRate: set the dalay (milisecond)

Font *loadFont(const std::string &path, int size)

load the font from a file

Return
font which is loaded
Parameters
  • path: path of the font file to load
  • size: size of font

Texture loadTexture(const std::string &path, Uint8 alpha = 255)

load the texture from a file on disk

Return
texture which is loaded
Parameters
  • path: path of the image file to load
  • alpha: transparency level (0 - 255)

Texture loadTexture(const std::string &path, Uint8 r, Uint8 g, Uint8 b, Uint8 alpha = 255)

load the texture from a file on disk and replace transparency of image with specific color

Return
texture which is loaded
Parameters
  • path: path of the image file to load
  • r: red color
  • g: green color
  • b: blue color
  • alpha: transparency level (0 - 255)

void playSound(Sound *sound, int count)

play sound multiple sound can play concurrently

See
loadSound
Parameters
  • sound: sound which is loaded before
  • count: frequency of sound (-1 to play all time)

void playMusic(Music *music, int count)

play music only one music file can play

See
loadMusic
Parameters
  • music: music which is loaded before
  • count: frequency of msuic (-1 to play all time)

Sound *loadSound(const std::string &path)

load sound from a file in disk (use .ogg or .wav)

Return
sound which is loaded
Parameters
  • path: path of the sound file to load

Music *loadMusic(const std::string &path)

load music from a file in disk (use .ogg or .wav)

Return
music which is loaded
Parameters
  • path: path of the music file to load

void stopMusic()

stop music

void stopAllSounds()

stop all sounds

void freeSound(Sound *sound)

free memory which is used for load sound from file

Parameters
  • sound: strcuture

void freeMusic(Music *music)

free memory which is used for load music from file

Parameters
  • music: strcuture

void freeTexture(Texture &texture)

free memory which is used for texture After call this function, texture is not usable anymore and any using has undefined behavior

Parameters
  • texture: structure

void showTexture(const Texture &texture, double angle, const SDL_Rect &destRect, SDL_RendererFlip flip = SDL_FLIP_NONE)

texture showed in render screen in position destRect with angle and flip

Parameters
  • texture: the source texture
  • angle: an angle in degrees that indicates the rotation that will be applied to texture, rotating it in a clockwise direction around center of texture
  • destRect: custom rect to draw texture
  • flip: flipping actions performed on the texture (SDL_FLIP_NONE or SDL_FLIP_HORIZONTAL or SDL_FLIP_VERTICAL)

void showTexture(const Texture &texture, int x, int y, double angle, SDL_RendererFlip flip = SDL_FLIP_NONE)

texture showed in render screen in position texture.rect with angle and flip

Parameters
  • texture: the source texture
  • x: position x
  • y: position y
  • angle: an angle in degrees that indicates the rotation that will be applied to texture, rotating it in a clockwise direction around center of texture
  • flip: flipping actions performed on the texture (SDL_FLIP_NONE or SDL_FLIP_HORIZONTAL or SDL_FLIP_VERTICAL)

void showTexture(const Texture &texture, const SDL_Rect &destRect)

texture showed in render screen in position destRect

Parameters
  • texture: the source texture
  • destRect: custom rect to draw texture

void showTexture(const Texture &texture, int x, int y)

texture showed in render screen in position texture.rect

Parameters
  • texture: the source texture
  • x: position x
  • y: position y

Texture createFontTexture(Font *font, const std::string &text, Uint8 r, Uint8 g, Uint8 b)

create a texture from a font for a special string with specific color whcih can be drawed in render window

Return
texture which created with that font
Parameters
  • font: font which is loaded
  • text: text that convert to texture
  • r: red color
  • g: green color
  • b: blue color

bool hasIntersectionRect(const SDL_Rect &x, const SDL_Rect &y)

check intersection of two SDL_Rect

Return
true if intersect each other
Parameters
  • x: first rectangle
  • y: second rectangle

void drawRectangle(const SDL_Rect &rect, Uint8 r, Uint8 g, Uint8 b, Uint8 alpha = 255)

Draw rectangle on renderer screen.

Parameters
  • rect: rectangle position
  • r: red
  • g: green
  • b: blue
  • alpha: transparency

bool pointInRect(int x, int y, const SDL_Rect &rect)

Check if a point is inside a Rect

Return
true if point is inside the rect
Parameters
  • x:
  • y:
  • rect: the Rectangle to check with

bool mouseInRect(const SDL_Rect &rect)

Check if mouse is inside a rect

Return
true if mouse is inside the rect
Parameters
  • rect: the Rectangle to check with

Variables

Mouse Mouse

Main mouse object which should be used to check mouse state during the program

struct Mouse
#include <SBDL.h>

a structure which give useful information about mouse state update method must be call before using

Public Functions

bool clicked(Uint8 button = SDL_BUTTON_LEFT, Uint8 clicks = 1, Uint8 state = SDL_PRESSED)

Check if Mouse clicked with given conditions.

Return
true if Mouse clicked with given conditions
Parameters
  • button: button to check <SDL_BUTTON_LEFT,SDL_BUTTON_RIGHT,SDL_BUTTON_MIDDLE>
  • clicks: number of clicksons
  • state: sate of mouse <SDL_PRESSED,SDL_RELEASED>

Public Members

int x

Mouse x position

int y

Mouse y position

bool left

Mouse left button state

bool right

Mouse right button state

bool middle

Mouse left button state

Uint8 state

Mouse button state <SDL_PRESSED,SDL_RELEASED>

Uint8 clicks

Mouse button clicks

Uint8 button

Mouse button for SDL backward compatibility

namespace Core

don’t import this namespace this namespace used for handle underneath SDL functions for you

Functions

Texture loadTextureUnderneath(const std::string &path, bool changeColor, Uint8 r, Uint8 g, Uint8 b, Uint8 alpha = 255)

Variables

bool running = true

current state of SDL

const Uint8 *keystates = nullptr

SDL keyboard state

SDL_Event event

SDL current event

SDL_Window *window = nullptr

SDL windows

SDL_Renderer *renderer = nullptr

SDL renderer