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¶
- Put
includedirectories ofSDL2,SDL2_image,SDL2_ttf,SDL2_mixerin your compiler’s include directory. - Put
libdirectories ofSDL2,SDL2_image,SDL2_ttf,SDL2_mixerin your linker’s path. - Put
SDL2Main.lib,SDL2.lib,SDL2_image.lib,SDL2_mixer.lib,SDL2_ttf.libin linker’s dependencies. - 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 windowwindowsWidth: width of windowwindowsHeight: height of windowr: red color of default backgroundg: green color of default backgroundb: 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
scanCode: specific code for each keyboard button (https://wiki.libsdl.org/SDL_Scancode)
-
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 loadsize: 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 loadalpha: 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 loadr: red colorg: green colorb: blue coloralpha: 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 beforecount: 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 beforecount: 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 textureangle: an angle in degrees that indicates the rotation that will be applied to texture, rotating it in a clockwise direction around center of texturedestRect: custom rect to draw textureflip: 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 texturex: position xy: position yangle: an angle in degrees that indicates the rotation that will be applied to texture, rotating it in a clockwise direction around center of textureflip: 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 texturedestRect: 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 texturex: position xy: 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 loadedtext: text that convert to texturer: red colorg: green colorb: 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 rectangley: 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 positionr: redg: greenb: bluealpha: 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
-
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 clicksonsstate: sate of mouse <SDL_PRESSED,SDL_RELEASED>
Public Members
Mouse button for SDL backward compatibility
-
bool
-
bool