Skip to content

Commit

Permalink
Merge pull request #38 from BSBussell/TextureManager
Browse files Browse the repository at this point in the history
Texture manager
  • Loading branch information
BSBussell authored Mar 10, 2023
2 parents 85b8c4f + 633fb35 commit 07fb5ee
Show file tree
Hide file tree
Showing 22 changed files with 283 additions and 346 deletions.
14 changes: 7 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ include_directories(${SDL2_INCLUDE_DIRS})

# Grabbing BML .cpp files
set (LIBRARY_SOURCES
src/BML.cpp
src/bEvent.cpp
src/bFontManager.cpp
src/BML.cpp
src/bRect.cpp
src/bRenderer.cpp
src/bSheet.cpp
src/bSound.cpp
src/bTexture.cpp
src/bWindow.cpp
src/bFontManager.cpp
src/bRenderer.cpp
)

# Grabbing BML .h files
set (LIBRARY_HEADERS
inc/BML.h
inc/bEvent.h
inc/bFontManager.h
inc/BML.h
inc/bRect.h
inc/bRenderer.h
inc/bSheet.h
inc/bSound.h
inc/bTexture.h
inc/bWindow.h
inc/bFontManager.h
inc/bRenderer.h
inc/json.h
)

Expand Down Expand Up @@ -112,7 +112,7 @@ if(TESTS)

get_filename_component(test_name ${test_src} NAME_WE)
add_executable(${test_name} ${test_src})
target_link_libraries(${test_name} BML ${SDL2_LIBRARIES} -lSDL2_image -lSDL2_mixer -lSDL2_ttf)
target_link_libraries(${test_name} -lBML ${SDL2_LIBRARIES} -lSDL2_image -lSDL2_mixer -lSDL2_ttf)
target_include_directories(${test_name} PRIVATE inc/)
target_compile_options(
${test_name}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CFLAGS := -Wall -Wextra -std=c++17
IFLAGS := -Iinc

#LFLAGS specify the libraries we're linking against
LFLAGS := -lSDL2 -lSDL2_image -lSDL2_mixer -lBML
LFLAGS := -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -lBML

#WFLAGS specifies that we know what we are doing with ar
WFLAGS := -no_warning_for_no_symbols
Expand Down
1 change: 0 additions & 1 deletion inc/BML.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#define BML_H

#include <string>
#include <cstdint>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
Expand Down
14 changes: 7 additions & 7 deletions inc/bEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ class bEvent {

public:

const static uint8_t b_KEYDOWN = 0b0001;
const static uint8_t b_KEYSTILLDOWN = 0b0010;
const static uint8_t b_KEYUP = 0b0100;
const static uint8_t b_KEYSTILLUP = 0b1000;
const static Uint8 b_KEYDOWN = 0b0001;
const static Uint8 b_KEYSTILLDOWN = 0b0010;
const static Uint8 b_KEYUP = 0b0100;
const static Uint8 b_KEYSTILLUP = 0b1000;

static bool eventLoop();

static bool keyDown(uint8_t key );
static bool keyDown(Uint8 key );
static bool keyDown(const char key);
static bool keyStillDown(const char *key );
static bool keyUp(uint8_t key );
static bool keyUp(Uint8 key );
static bool keyStillUp(const char *key );

/*
Expand All @@ -41,7 +41,7 @@ class bEvent {

bEvent();

inline static uint8_t b_KEYSTATE[82];
inline static Uint8 b_KEYSTATE[82];

};

Expand Down
10 changes: 5 additions & 5 deletions inc/bRect.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

struct bPoint {

uint32_t x, y;
Uint32 x, y;

// Easy conversion to SDL_Point
operator SDL_Point() const;
};

struct bRect {

uint32_t x;
uint32_t y;
uint32_t width;
uint32_t height;
Uint32 x;
Uint32 y;
Uint32 width;
Uint32 height;

// Easy conversion to SDL_Rect
operator SDL_Rect() const;
Expand Down
18 changes: 13 additions & 5 deletions inc/bRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class bRenderer {

public:

// Create the renderer using the given flags
bRenderer(SDL_Window *window, Uint32 _render_flags);
~bRenderer();

// Change the background color
void background(uint8_t r, uint8_t g, uint8_t b, uint8_t a);

void background(Uint8 r, Uint8 g, Uint8 b, Uint8 a);

// Buffer Presentation
void clearBuffer();
Expand All @@ -37,9 +37,10 @@ class bRenderer {

// Drawing Textures and Rectangles
void drawTexture(bTexture texture, bRect dest);
void drawTexture(bTexture texture, bPoint dest);
void drawTexture(const char* source, bRect src, bRect dest);
void drawSprite(bSheet &sheet, bRect dest);
void drawRect(bRect location, uint8_t r, uint8_t g, uint8_t b);
void drawRect(bRect location, Uint8 r, Uint8 g, Uint8 b);

// Freeing Textures
void unloadTexture(bTexture &texture);
Expand All @@ -51,13 +52,20 @@ class bRenderer {

private:

/*
We want our managers to be stored in the heap because
they will have a long life time, and we don't want to
overfill the stack with things
*/
bTextureManager *_texture_manager;
bFontManager *_font_manager;

SDL_Color _bkg_color;

SDL_Window *_context;
SDL_Renderer *_sdl_renderer;

// The background color
SDL_Color _bkg_color;

};


Expand Down
20 changes: 10 additions & 10 deletions inc/bSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ using json = nlohmann::json;
*/


/// @brief Datastructure used for information about an animation
// @brief Datastructure used for information about an animation
struct bAnimation {

std::queue<uint16_t> frames;
std::queue<uint32_t> duration;
std::queue<Uint16> frames;
std::queue<Uint32> duration;

std::string name;

// How often to update animations
//uint32_t speed;
uint64_t tickCount;
//Uint32 speed;
Uint64 tickCount;
};

/// @brief Data structure used for storing spritesheet info
// @brief Data structure used for storing spritesheet info
struct bSheet {

std::string imagePath;
bTexture sourceTexture;

bool animated = false;

uint32_t totalWidth;
uint32_t totalHeight;
Uint32 totalWidth;
Uint32 totalHeight;

uint16_t totalSprites;
uint16_t currentSprite = 0;
Uint16 totalSprites;
Uint16 currentSprite = 0;

std::vector<bRect> sprites;

Expand Down
4 changes: 2 additions & 2 deletions inc/bSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class bSound {

// Only one file considered "music" can play at a time
static bool loadMUS(const char* src);
static bool playMUS(uint8_t loops);
static bool playMUS(Uint8 loops);
static void setMUSVOL(double perc);
static void freeMUS();

// Sound effects can play on many channels
bool loadSFX(const char* src);
bool playSFX(uint8_t channel, uint8_t loops);
bool playSFX(Uint8 channel, Uint8 loops);
void setSFXVOL(double perc);
void freeSFX();

Expand Down
49 changes: 44 additions & 5 deletions inc/bTexture.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@

// Bee Bussell
// Sept 6, 2021
// Header
// bTexture/manager

#ifndef BML_TEXTURE_H
#define BML_TEXTURE_H

#include <SDL2/SDL.h>
#include <memory>
#include <string>
#include <unordered_map>

// Just a header file for now
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

// TODO: Implement texture freeing and destructer for that
#include "bRect.h"

// @brief Data structure used for storing texture info
struct bTexture {

SDL_Texture* texture;

std::string path;

SDL_Texture *texture;
SDL_Rect src;

};

// @brief Class for mananging usage of textures
class bTextureManager {

public:

bTextureManager(SDL_Renderer *renderer);
~bTextureManager();

// Load the texture either by creating a new one or grabbing it from the cache
bTexture loadTexture(const char *path, bRect dim);

// Unloads the texture, if it's not being used by anything else
void unloadTexture(bTexture texture);

// Does not delete bTextures, User still has to do that on ther own
void clearCache();

// Rendering the Texture either using a rect or a point
void renderTexture(bTexture &texture, bRect dest);
void renderTexture(bTexture &texture, bPoint dest);


private:

SDL_Renderer *_sdl_renderer;

// Cache
std::unordered_map<std::string, SDL_Texture*> _loaded_textures;
std::unordered_map<SDL_Texture*, Uint8> _refs;

};

#endif
22 changes: 11 additions & 11 deletions inc/bWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class bWindow {
public:

// Constructors and Destroyer
bWindow(const char *windowTitle, uint16_t xPos, uint16_t yPos, uint16_t width, uint16_t height)
bWindow(const char *windowTitle, Uint16 xPos, Uint16 yPos, Uint16 width, Uint16 height)
: windowTitle(windowTitle)
, xPos(xPos)
, yPos(yPos)
Expand All @@ -24,19 +24,19 @@ class bWindow {
~bWindow();

// Window Dimensions and Position Setters
void move(uint16_t xPos, uint16_t yPos) {
void move(Uint16 xPos, Uint16 yPos) {
this->xPos = xPos;
this->yPos = yPos;
}

void resize(uint16_t width, uint16_t height) {
void resize(Uint16 width, Uint16 height) {
this->width = width;
this->height = height;
}

// Window Dimensions Getters
uint16_t getWidth() { return width; }
uint16_t getHeight() { return height; }
Uint16 getWidth() { return width; }
Uint16 getHeight() { return height; }

// Window Flag Toggles
void toggleFullScreen() { windowFlags ^= SDL_WINDOW_FULLSCREEN; }
Expand Down Expand Up @@ -71,14 +71,14 @@ class bWindow {
const char *windowTitle;

// Not a bRect because Uint16 is good enough :3
uint16_t xPos;
uint16_t yPos;
Uint16 xPos;
Uint16 yPos;

uint16_t width;
uint16_t height;
Uint16 width;
Uint16 height;

uint32_t windowFlags = 0;
uint32_t _render_flags = 0;
Uint32 windowFlags = 0;
Uint32 _render_flags = 0;

};

Expand Down
4 changes: 2 additions & 2 deletions src/bEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "bEvent.h"

bool bEvent::keyDown(uint8_t key) {
bool bEvent::keyDown(Uint8 key) {


return (b_KEYSTATE[key] == b_KEYDOWN);
Expand All @@ -19,7 +19,7 @@ bool bEvent::keyDown(const char key) {
return false;
}

bool bEvent::keyUp(uint8_t key) {
bool bEvent::keyUp(Uint8 key) {

return (b_KEYSTATE[key]== b_KEYUP);
}
Expand Down
16 changes: 7 additions & 9 deletions src/bRect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@
bRect::operator SDL_Rect() const {

SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;

return rect;
rect.x = static_cast<int>(x);
rect.y = static_cast<int>(y);
rect.w = static_cast<int>(width);
rect.h = static_cast<int>(height);
return rect;

}

bPoint::operator SDL_Point() const {

SDL_Point point;
point.x = x;
point.y = y;

point.x = static_cast<int>(x);
point.y = static_cast<int>(y);
return point;
}

Loading

0 comments on commit 07fb5ee

Please sign in to comment.