ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
assets.cpp
Go to the documentation of this file.
2
3#include <SDL3/SDL.h>
4#include <SDL3_image/SDL_image.h>
5#include <SDL3_mixer/SDL_mixer.h>
6#include <SDL3_ttf/SDL_ttf.h>
7#include <string>
8
10#include "./asw/modules/types.h"
11#include "./asw/modules/util.h"
12
13asw::Texture asw::assets::loadTexture(const std::string& filename) {
14 SDL_Texture* temp = IMG_LoadTexture(asw::display::renderer, filename.c_str());
15
16 if (temp == nullptr) {
17 asw::util::abortOnError("Failed to load texture: " + filename);
18 }
19
20 SDL_SetTextureScaleMode(temp, SDL_SCALEMODE_NEAREST);
21 SDL_SetTextureBlendMode(temp, SDL_BLENDMODE_BLEND);
22
23 return {temp, SDL_DestroyTexture};
24}
25
26asw::Font asw::assets::loadFont(const std::string& filename, float size) {
27 TTF_Font* temp = TTF_OpenFont(filename.c_str(), size);
28
29 if (temp == nullptr) {
30 asw::util::abortOnError("Failed to load font: " + filename);
31 }
32
33 return {temp, TTF_CloseFont};
34}
35
36asw::Sample asw::assets::loadSample(const std::string& filename) {
37 Mix_Chunk* temp = Mix_LoadWAV(filename.c_str());
38
39 if (temp == nullptr) {
40 asw::util::abortOnError("Failed to load sample: " + filename);
41 }
42
43 return {temp, Mix_FreeChunk};
44}
45
46asw::Music asw::assets::loadMusic(const std::string& filename) {
47 Mix_Music* temp = Mix_LoadMUS(filename.c_str());
48
49 if (temp == nullptr) {
50 asw::util::abortOnError("Failed to load music: " + filename);
51 }
52
53 return {temp, Mix_FreeMusic};
54}
55
57 SDL_Texture* text =
58 SDL_CreateTexture(asw::display::renderer, SDL_PIXELFORMAT_RGBA8888,
59 SDL_TEXTUREACCESS_TARGET, w, h);
60
61 return {text, SDL_DestroyTexture};
62}
Asset routines for the ASW library.
Display and window routines for the ASW library.
asw::Sample loadSample(const std::string &filename)
Loads a sample from a file. Formats supported are WAV, AIFF, RIFF, OGG and VOC. This will abort if th...
Definition assets.cpp:36
asw::Font loadFont(const std::string &filename, float size)
Loads a TTF font from a file. This will abort if the file is not found.
Definition assets.cpp:26
asw::Music loadMusic(const std::string &filename)
Loads a music file from a file. Formats supported are WAV, AIFF, RIFF, OGG and VOC....
Definition assets.cpp:46
asw::Texture loadTexture(const std::string &filename)
Loads a texture from a file. Formats supported are PNG, ICO, CUR, BMP, GIF, JPG, LBM,...
Definition assets.cpp:13
asw::Texture createTexture(int w, int h)
Create a Texture given the specified dimensions.
Definition assets.cpp:56
asw::Renderer * renderer
The renderer for the display module.
Definition display.cpp:9
void abortOnError(const std::string &message)
Abort program and display error message.
Definition util.cpp:6
std::shared_ptr< Mix_Chunk > Sample
Alias for a shared pointer to an Mix_Chunk.
Definition types.h:26
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:23
std::shared_ptr< Mix_Music > Music
Alias for a shared pointer to an Mix_Music.
Definition types.h:29
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:20
Types used throughout the ASW library.
General utility functions.