Skip to content

Commit

Permalink
Merge pull request #2 from ianw3214/dev
Browse files Browse the repository at this point in the history
Texture Manager fix for OpenGL Texture memory issues
  • Loading branch information
ianw3214 committed Nov 3, 2018
2 parents 31b14ef + 0c5ec95 commit ed499c8
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 20 deletions.
2 changes: 2 additions & 0 deletions WyvernsNest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<ClCompile Include="src\engine\renderer.cpp" />
<ClCompile Include="src\engine\sprite.cpp" />
<ClCompile Include="src\engine\state.cpp" />
<ClCompile Include="src\engine\textureManager.cpp" />
<ClCompile Include="src\engine\text\font.cpp" />
<ClCompile Include="src\engine\text\textRenderer.cpp" />
<ClCompile Include="src\game\combat.cpp" />
Expand Down Expand Up @@ -166,6 +167,7 @@
<ClInclude Include="src\engine\renderer.hpp" />
<ClInclude Include="src\engine\sprite.hpp" />
<ClInclude Include="src\engine\state.hpp" />
<ClInclude Include="src\engine\textureManager.hpp" />
<ClInclude Include="src\engine\text\font.hpp" />
<ClInclude Include="src\engine\text\textRenderer.hpp" />
<ClInclude Include="src\game\combat.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions WyvernsNest.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
<ClCompile Include="src\game\menu.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\engine\textureManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\engine\engine.hpp">
Expand Down Expand Up @@ -173,5 +176,8 @@
<ClInclude Include="src\game\menu.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\engine\textureManager.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
10 changes: 8 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ LIBS = -lGLEW -lSDL2 -lfreetype -L/usr/local/Cellar/sdl2/2.0.8/lib/ -L/usr/local
INCLUDE = -I/usr/local/Cellar/sdl2/2.0.8/include/SDL2/ -I/usr/local/Cellar/glew/2.1.0/include/ -Ilibs/stb_image/ -I/usr/local/Cellar/freetype/2.9.1/include/freetype2/
LFLAGS = -std=c++11 -framework OpenGL -w $(LIBS) $(INCLUDE)
CFLAGS = $(LFLAGS) -o bin/objs/$@
OBJS = engine.o entity.o indexBuffer.o shader.o texture.o vertexArray.o vertexBuffer.o renderer.o sprite.o state.o combat.o customization.o cutscene.o vec.o
OBJS = engine.o entity.o indexBuffer.o shader.o texture.o vertexArray.o vertexBuffer.o renderer.o sprite.o state.o combat.o customization.o cutscene.o vec.o textRenderer.o font.o player.o grid.o enemy.o menu.o unit.o attack.o
BIN_OBJS = $(addprefix bin/objs/, $(OBJS))

VPATH = src src/engine src/engine/opengl src/engine/text src/game src/math bin bin/objs
VPATH = src src/engine src/engine/opengl src/engine/text src/game src/game/combat src/math bin bin/objs
TARGET = bin/game

all: $(TARGET)
Expand Down Expand Up @@ -38,6 +38,12 @@ combat.o: combat.cpp core.hpp
customization.o: customization.cpp core.hpp unitData.hpp
cutscene.o: cutscene.cpp core.hpp
vec.o: vec.cpp
player.o: player.cpp core.hpp vec.hpp attack.hpp
grid.o: grid.cpp core.hpp vec.hpp player.hpp enemy.hpp
enemy.o: enemy.cpp core.hpp vec.hpp
menu.o: menu.cpp core.hpp
unit.o: unit.cpp core.hpp attack.hpp unitData.hpp
attack.o: attack.cpp core.hpp vec.hpp

.cpp.o:
$(CXX) $(CFLAGS) -c $<
Expand Down
Binary file added res/assets/invalid_circle.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/music/track1.wav
Binary file not shown.
5 changes: 5 additions & 0 deletions src/engine/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "engine.hpp"
#include "renderer.hpp"
#include "text/textRenderer.hpp"
#include "textureManager.hpp"
#include "entity.hpp"
#include "state.hpp"
#include "sprite.hpp"
Expand Down Expand Up @@ -47,6 +48,10 @@ namespace Core {
return Engine::get_instance().getDebugMode();
}

inline Texture * getTexture(const std::string& fileName) {
return Engine::get_instance().getTextureManager()->getTexture(fileName);
}

// Wrappers around renderer functionalities
namespace Renderer {

Expand Down
12 changes: 12 additions & 0 deletions src/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Other project includes
#include "renderer.hpp"
#include "text/textRenderer.hpp"
#include "textureManager.hpp"
#include "state.hpp"

bool Engine::init(const char * name, int window_width, int window_height) {
Expand Down Expand Up @@ -64,6 +65,9 @@ bool Engine::init(const char * name, int window_width, int window_height) {
// Initialize the text renderer after the OpenGL context is created
m_textRenderer = new TextRenderer("res/test_font.ttf", 32, Vec2<int>(m_windowWidth, m_windowHeight));

// Initialize the texture manager
m_textureManager = new TextureManager();

// reset m_lastTick for a more accurate first tick
m_lastTick = SDL_GetTicks();

Expand Down Expand Up @@ -135,6 +139,10 @@ TextRenderer * Engine::getTextRenderer() {
return m_textRenderer;
}

TextureManager * Engine::getTextureManager() {
return m_textureManager;
}

void Engine::setState(State * state) {
if (m_state) delete m_state;
m_state = state;
Expand All @@ -161,6 +169,10 @@ Engine::Engine() :
Engine::~Engine() {
delete m_state;

delete m_renderer;
delete m_textRenderer;
delete m_textureManager;

SDL_GL_DeleteContext(m_context);
SDL_DestroyWindow(m_window);
SDL_Quit();
Expand Down
4 changes: 4 additions & 0 deletions src/engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Renderer;
class TextRenderer;
class TextureManager;

class State;

class Engine {
Expand All @@ -32,6 +34,7 @@ class Engine {
int getWindowHeight() const;
Renderer * getRenderer();
TextRenderer * getTextRenderer();
TextureManager * getTextureManager();

// State changing functions
void setState(State * state);
Expand All @@ -49,6 +52,7 @@ class Engine {
// System objects
Renderer * m_renderer;
TextRenderer * m_textRenderer;
TextureManager * m_textureManager;

// SDL/OpenGL specific objects
SDL_Window * m_window;
Expand Down
8 changes: 6 additions & 2 deletions src/engine/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Sprite::Sprite(const std::string & path) :
original_w(0),
original_h(0)
{
src_w = original_w = w = texture.getWidth();
src_h = original_h = h = texture.getHeight();
src_w = original_w = w = Core::getTexture(path)->getWidth();
src_h = original_h = h = Core::getTexture(path)->getHeight();
}

Sprite::~Sprite() {
Expand All @@ -24,6 +24,10 @@ void Sprite::render() {
Core::Renderer::drawSprite(*this);
}

const Texture & Sprite::getTexture() const {
return *Core::getTexture(texture);
}

void Sprite::setPos(int x, int y) {
this->x = x;
this->y = y;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Sprite {
// Sprite size
int w, h;

const Texture& getTexture() const { return texture; }
const Texture& getTexture() const;

// Texture source information
int src_x, src_y;
Expand All @@ -34,6 +34,6 @@ class Sprite {

private:

Texture texture;
std::string texture;

};
23 changes: 23 additions & 0 deletions src/engine/textureManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "textureManager.hpp"

#include "opengl/texture.hpp"

TextureManager::TextureManager() {

}

TextureManager::~TextureManager() {

}

Texture * TextureManager::getTexture(const std::string & fileName) {
if (m_textures.find(fileName) == m_textures.end()) {
m_textures[fileName] = new Texture(fileName);
}
return m_textures[fileName];
}

void TextureManager::deleteTexture(const std::string & fileName) {
delete m_textures[fileName];
m_textures.erase(fileName);
}
19 changes: 19 additions & 0 deletions src/engine/textureManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <string>
#include <unordered_map>

class Texture;

class TextureManager {
public:

TextureManager();
~TextureManager();

Texture* getTexture(const std::string& fileName);
void deleteTexture(const std::string& fileName);

private:
std::unordered_map<std::string, Texture*> m_textures;
};
2 changes: 1 addition & 1 deletion src/game/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void Combat::handleEvent(const SDL_Event& e) {
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_RETURN) {
// Move on to the next state

changeState(new Combat());
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/game/combat/attack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Attack::Attack(std::string name,
affect_self(affect_self),
validSprite("res/assets/valid.png"),
targetValidSprite("res/assets/valid_circle.png"),
targetInvalidSprite("res/test7.png")
targetInvalidSprite("res/assets/invalid_circle.png")
{

}
Expand All @@ -34,11 +34,14 @@ void Attack::attack(ScreenCoord pos, Combat& combat) {
attackAoE(source->position, combat);
} break;
case AttackType::MELEE: {
// TODO: Make sure the attack is valid before running it
effect->attack(pos, combat);
attackAoE(pos, combat);
} break;
case AttackType::RANGED: {

// TODO: Make sure the attack is valid before running it
effect->attack(pos, combat);
attackAoE(pos, combat);
} break;
}
// TODO: figure out how to apply the attack effect to the surrounding aoe
Expand All @@ -60,10 +63,6 @@ void Attack::renderValidGrid() {
validSprite.setPos(source->position.x() * tile_width, (source->position.y() + 1) * tile_height);
validSprite.render();
} break;
case AttackType::RANGED: {
// TODO: Display the sprites correctly
// TODO: Add a range property and figure out valid grid based off of range
} break;
case AttackType::RANGED: {
for (int y = -range; y < range + 1 ; y++) {
for (int x = -range; x < range + 1 ; x++) {
Expand Down Expand Up @@ -127,9 +126,6 @@ bool Attack::isValid(ScreenCoord pos) {
}
*/
} break;
case AttackType::RANGED: {
// TODO: Calculate whether the pos is valid based off a range property
} break;
case AttackType::RANGED: {
int x_diff = std::abs(pos.x() - source->position.x());
int y_diff = std::abs(pos.y() - source->position.y());
Expand Down
2 changes: 0 additions & 2 deletions src/game/combat/attack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ class Attack {
std::string name;
Unit * source;

int range;

AttackType type;
AttackRange range;
AttackEffect * effect;
Expand Down
1 change: 1 addition & 0 deletions src/game/combat/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ bool Grid::isMousePosValid()

bool Grid::isPosEmpty(Vec2<int> pos) const {
if (TILE_INDEX(pos.x(), pos.y()) < 0) return false;
if (TILE_INDEX(pos.x(), pos.y()) >= map_width * map_height) return false;
return !collisionmap[TILE_INDEX(pos.x(), pos.y())];
}

Expand Down
4 changes: 2 additions & 2 deletions src/game/combat/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Unit::Unit() :
sprite_height(DEFAULT_SPRITE_HEIGHT),
top_margin(0),
attack1("PUNCH", this, AttackType::MELEE, 0, new DamageEffect(5), 0),
attack2("RANGED", this, AttackType::RANGED, new DamageEffect(5), 0, 2),
attack2("RANGED", this, AttackType::RANGED, 2, new DamageEffect(5), 0),
shadow("res/assets/shadow.png")
{
generateDefaultUnitData();
Expand All @@ -24,7 +24,7 @@ Unit::Unit(UnitType type) :
sprite_height(DEFAULT_SPRITE_HEIGHT),
top_margin(0),
attack1("PUNCH", this, AttackType::MELEE, 0, new DamageEffect(5), 0),
attack2("RANGED", this, AttackType::RANGED, new DamageEffect(5), 0, 2),
attack2("RANGED", this, AttackType::RANGED, 2, new DamageEffect(5), 0),
shadow("res/assets/shadow.png")
{
generateDefaultUnitData();
Expand Down
15 changes: 15 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ int main(int argc, char* argv[]) {
Menu * state = new Menu();
Core::setState(state);

// ---------------TEMPORARY AUDIO CODE--------------------------
// load WAV file
SDL_AudioSpec wavSpec;
Uint32 wavLength;
Uint8 *wavBuffer;
if (SDL_LoadWAV("res/music/track1.wav", &wavSpec, &wavBuffer, &wavLength) == NULL) {
return 1;
}
// open audio device
SDL_AudioDeviceID deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0);
// play audio
int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength);
SDL_PauseAudioDevice(deviceId, 0);
// -------------------------------------------------------------

// Main engine loop
while (Core::running()) {
Core::update();
Expand Down

0 comments on commit ed499c8

Please sign in to comment.