Skip to content

Commit

Permalink
pointer update
Browse files Browse the repository at this point in the history
some shared pointers will become weak pointers after further testing.
  • Loading branch information
durkisneer1 committed Dec 7, 2023
1 parent 762612b commit f0a5c37
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 48 deletions.
3 changes: 2 additions & 1 deletion example/include/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

#include <KrakenEngine.hpp>
#include <vector>
#include <memory>


class Player : public kn::Sprite {
public:
Player(kn::RenderWindow& window, kn::Texture& texture);
Player(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture);
~Player() = default;

void process(double deltaTime) override;
Expand Down
7 changes: 4 additions & 3 deletions example/include/Tracker.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#pragma once

#include <KrakenEngine.hpp>
#include <memory>


class Tracker {
public:
Tracker(kn::RenderWindow& window, kn::Texture& texture);
void process(double deltaTime);
Tracker(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture);
void update(double deltaTime, kn::math::Vector2 target);

private:
kn::RenderWindow& window;
kn::Texture& texture;
std::shared_ptr<kn::Texture> texture;
kn::Rect rect;

kn::math::Vector2 position;
Expand Down
3 changes: 2 additions & 1 deletion example/include/Wall.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include <KrakenEngine.hpp>
#include <memory>


class Wall : public kn::Sprite {
public:
Wall(kn::RenderWindow& window, kn::Texture& texture, kn::math::Vector2 position);
Wall(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture, kn::math::Vector2 position);
~Wall() = default;

void process(double deltaTime) override;
Expand Down
18 changes: 11 additions & 7 deletions example/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ int main() {
kn::Rect hwRect = hwTexture->getRect();
hwRect.setCenter(WIN_SIZE / 2.0f);

Player player(window, *textureCache.get("player"));
Tracker tracker(window, *textureCache.get("tracker"));
Player player(window, textureCache.get("player"));
Tracker tracker(window, textureCache.get("tracker"));

for (float x = 0.0f; x <= WIN_SIZE.x - 50.0f; x += 50.0f) {
kn::Sprite::addSprite(std::make_unique<Wall>(window, *textureCache.get("wall"), kn::math::Vector2(x, WIN_SIZE.y)));
kn::Sprite::addSprite(
std::make_unique<Wall>(window, textureCache.get("wall"), kn::math::Vector2(x, WIN_SIZE.y))
);
}
kn::Sprite::addSprite(std::make_unique<Wall>(window, *textureCache.get("wall"), kn::math::Vector2(WIN_SIZE.x - 150.0f, WIN_SIZE.y - 50.0f)));
kn::Sprite::addSprite(
std::make_unique<Wall>(window, textureCache.get("wall"), kn::math::Vector2(WIN_SIZE.x - 150.0f, WIN_SIZE.y - 50.0f))
);

bool done = false;
while (!done) {
Expand All @@ -52,14 +56,14 @@ int main() {
}

window.fill({ 40, 40, 40 });
window.blit(*bgTexture, bgTexture->getRect());
window.blit(bgTexture, bgTexture->getRect());

for (const auto& sprite : kn::Sprite::getSprites()) {
sprite->process(deltaTime);
}
window.blit(*hwTexture, hwRect); // FIXME: This is not working
window.blit(hwTexture, hwRect); // FIXME: This is not working
player.process(deltaTime);
tracker.process(deltaTime);
tracker.update(deltaTime, player.getPosition());

window.flip();
}
Expand Down
2 changes: 1 addition & 1 deletion example/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <iostream>


Player::Player(kn::RenderWindow& window, kn::Texture& texture)
Player::Player(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture)
: kn::Sprite(window, texture) {
position = WIN_SIZE / 2.0f;
}
Expand Down
10 changes: 4 additions & 6 deletions example/src/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
#include <iostream>


Tracker::Tracker(kn::RenderWindow& window, kn::Texture& texture)
: window(window), texture(texture), rect(texture.getRect()) {
position = WIN_SIZE / 2.0f;
}
Tracker::Tracker(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture)
: window(window), texture(texture), rect(texture->getRect()), position(WIN_SIZE / 2.0f) {}

void Tracker::process(double deltaTime) {
position = kn::math::lerpVec(position, kn::input::getMousePos(), speed * deltaTime);
void Tracker::update(double deltaTime, kn::math::Vector2 target) {
position = kn::math::lerpVec(position, target, speed * deltaTime);
rect.setCenter(position);
window.blit(texture, rect);
}
2 changes: 1 addition & 1 deletion example/src/wall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <iostream>


Wall::Wall(kn::RenderWindow& window, kn::Texture& texture, kn::math::Vector2 position)
Wall::Wall(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture, kn::math::Vector2 position)
: kn::Sprite(window, texture) {
this->position = position;
rect.setBottomLeft(position);
Expand Down
1 change: 1 addition & 0 deletions include/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <SDL.h>

typedef SDL_Scancode KN_KEYS;
typedef SDL_Event KN_Event;

const SDL_Scancode KNK_w = SDL_SCANCODE_W;
const SDL_Scancode KNK_a = SDL_SCANCODE_A;
Expand Down
14 changes: 8 additions & 6 deletions include/RenderWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#include <vector>
#include <memory>
#include "Texture.hpp"
#include "Math.hpp"
#include "Constants.hpp"


namespace kn {
Expand All @@ -17,19 +19,19 @@ namespace kn {

void fill(SDL_Color color);
void flip();
void blit(kn::Texture& texture, kn::Rect rect);
void blit(kn::Texture& texture, kn::math::Vector2 position);
void blitEx(kn::Texture& texture, kn::Rect rect, double angle = 0.0, bool flipX = false, bool flipY = false);
void blitEx(kn::Texture& texture, kn::math::Vector2 position, double angle = 0.0, bool flipX = false, bool flipY = false);
void blit(const std::shared_ptr<kn::Texture> texture, kn::Rect rect);
void blit(const std::shared_ptr<kn::Texture> texture, kn::math::Vector2 position);
void blitEx(const std::shared_ptr<kn::Texture>, kn::Rect rect, double angle = 0.0, bool flipX = false, bool flipY = false);
void blitEx(const std::shared_ptr<kn::Texture>, kn::math::Vector2 position, double angle = 0.0, bool flipX = false, bool flipY = false);

SDL_Renderer* getRenderer() { return renderer; }
const std::vector<SDL_Event>& getEvents();
const std::vector<KN_Event>& getEvents();

private:
SDL_Renderer* renderer = nullptr;
SDL_Window* window = nullptr;

SDL_Event event;
KN_Event event;
std::vector<SDL_Event> events;

void init();
Expand Down
6 changes: 3 additions & 3 deletions include/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace kn {
class Sprite {
public:
Sprite(kn::RenderWindow& window, kn::Texture& texture);
Sprite(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture);
~Sprite() = default;

kn::math::Vector2 getPosition() const;
Expand All @@ -24,7 +24,7 @@ namespace kn {

protected:
kn::RenderWindow& window;
kn::Texture& texture;
std::shared_ptr<kn::Texture> texture;
kn::Rect rect;

kn::math::Vector2 position;
Expand All @@ -41,4 +41,4 @@ namespace kn {
void rectHorizontalCollision();
void rectVerticalCollision();
};
}
}
2 changes: 1 addition & 1 deletion include/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace kn {
class Texture {
public:
Texture(SDL_Texture* texture);
explicit Texture(SDL_Texture* texture);
~Texture() { if (texture) SDL_DestroyTexture(texture); }

kn::math::Vector2 getSize() const { return { rect.w, rect.h }; }
Expand Down
4 changes: 2 additions & 2 deletions include/TextureCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace kn {
class TextureCache {
public:
TextureCache(kn::RenderWindow& window) : window(window) {}
~TextureCache() { unloadAll();};
explicit TextureCache(kn::RenderWindow& window) : window(window) {}
~TextureCache() { unloadAll(); };

void load(const char* name, const char* path);
void create(const char* name, kn::math::Vector2 size, SDL_Color color);
Expand Down
20 changes: 10 additions & 10 deletions src/render_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ namespace kn {
SDL_RenderPresent(renderer);
}

void RenderWindow::blit(kn::Texture& texture, kn::Rect rect) {
SDL_RenderCopyF(renderer, texture.getSDLTexture(), nullptr, &rect);
void RenderWindow::blit(std::shared_ptr<kn::Texture> texture, kn::Rect rect) {
SDL_RenderCopyF(renderer, texture->getSDLTexture(), nullptr, &rect);
}

void RenderWindow::blitEx(kn::Texture& texture, kn::Rect rect, double angle, bool flipX, bool flipY) {
void RenderWindow::blitEx(std::shared_ptr<kn::Texture> texture, kn::Rect rect, double angle, bool flipX, bool flipY) {
SDL_RendererFlip flip = SDL_FLIP_NONE;
if (flipX) {
flip = (SDL_RendererFlip)(flip | SDL_FLIP_HORIZONTAL);
} else {}
if (flipY) {
flip = (SDL_RendererFlip)(flip | SDL_FLIP_VERTICAL);
} else {}
SDL_RenderCopyExF(renderer, texture.getSDLTexture(), nullptr, &rect, angle, nullptr, flip);
SDL_RenderCopyExF(renderer, texture->getSDLTexture(), nullptr, &rect, angle, nullptr, flip);
}

void RenderWindow::blitEx(kn::Texture& texture, kn::math::Vector2 position, double angle, bool flipX, bool flipY) {
void RenderWindow::blitEx(std::shared_ptr<kn::Texture> texture, kn::math::Vector2 position, double angle, bool flipX, bool flipY) {
SDL_RendererFlip flip = SDL_FLIP_NONE;
if (flipX) {
flip = (SDL_RendererFlip)(flip | SDL_FLIP_HORIZONTAL);
Expand All @@ -105,16 +105,16 @@ namespace kn {
} else {}
SDL_FRect rect = {
position.x, position.y,
texture.getSize().x, texture.getSize().y
texture->getSize().x, texture->getSize().y
};
SDL_RenderCopyExF(renderer, texture.getSDLTexture(), nullptr, &rect, angle, nullptr, flip);
SDL_RenderCopyExF(renderer, texture->getSDLTexture(), nullptr, &rect, angle, nullptr, flip);
}

void RenderWindow::blit(kn::Texture& texture, kn::math::Vector2 position) {
void RenderWindow::blit(std::shared_ptr<kn::Texture> texture, kn::math::Vector2 position) {
SDL_FRect rect = {
position.x, position.y,
texture.getSize().x, texture.getSize().y
texture->getSize().x, texture->getSize().y
};
SDL_RenderCopyF(renderer, texture.getSDLTexture(), nullptr, &rect);
SDL_RenderCopyF(renderer, texture->getSDLTexture(), nullptr, &rect);
}
}
4 changes: 2 additions & 2 deletions src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


namespace kn {
Sprite::Sprite(kn::RenderWindow& window, kn::Texture& texture)
: window(window), texture(texture), rect(texture.getRect()) {}
Sprite::Sprite(kn::RenderWindow& window, std::shared_ptr<kn::Texture> texture)
: window(window), texture(texture), rect(texture->getRect()) {}

kn::math::Vector2 Sprite::getPosition() const {
return position;
Expand Down
4 changes: 0 additions & 4 deletions src/texture_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@ namespace kn {
}

void TextureCache::unload(const char* name) {
SDL_DestroyTexture(textures[name]->getSDLTexture());
textures.erase(name);
}

void TextureCache::unloadAll() {
for (auto& texture : textures) {
SDL_DestroyTexture(texture.second->getSDLTexture());
}
textures.clear();
}

Expand Down

0 comments on commit f0a5c37

Please sign in to comment.