From 0334d396e4ff1b2f8a1e147b753a4de34f55c08b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 30 Jan 2021 05:17:55 -0500 Subject: Game now renders an image of Lucas --- src/game.h | 29 +++++++++ src/main.cpp | 8 +++ src/renderer.cpp | 186 +++++++------------------------------------------------ src/renderer.h | 8 ++- src/sprite.cpp | 6 ++ src/sprite.h | 33 ++++++++++ 6 files changed, 105 insertions(+), 165 deletions(-) create mode 100644 src/game.h create mode 100644 src/sprite.cpp create mode 100644 src/sprite.h (limited to 'src') diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..46f1ab2 --- /dev/null +++ b/src/game.h @@ -0,0 +1,29 @@ +#ifndef GAME_H_E6F1396E +#define GAME_H_E6F1396E + +#include +#include "sprite.h" + +class Game { +public: + + int addSprite(Sprite sprite) { + int id = sprites_.size(); + sprites_.push_back(std::move(sprite)); + return id; + } + + Sprite& getSprite(int id) { + return sprites_.at(id); + } + + std::vector& getSprites() { + return sprites_; + } + +private: + + std::vector sprites_; +}; + +#endif /* end of include guard: GAME_H_E6F1396E */ diff --git a/src/main.cpp b/src/main.cpp index 2da7d9a..e91f8f0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,13 @@ #include #include "renderer.h" +#include "game.h" void loop(Renderer& renderer) { + Game game; + game.addSprite(Sprite("../res/lucas.png", renderer)); + + renderer.render(game); + for (;;) { SDL_Event e; while (SDL_PollEvent(&e)) @@ -11,6 +17,8 @@ void loop(Renderer& renderer) { return; } } + + renderer.render(game); } } diff --git a/src/renderer.cpp b/src/renderer.cpp index e29d8cd..3bd0beb 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,8 +1,8 @@ #include "renderer.h" #include "consts.h" +#include "game.h" -Renderer::Renderer() -{ +Renderer::Renderer() { win_ = window_ptr( SDL_CreateWindow( "Tanetane", @@ -29,15 +29,14 @@ Renderer::Renderer() } } -/*void Renderer::render() -{ +void Renderer::render(Game& game) { texture_ptr canvas( SDL_CreateTexture( ren_.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, - TILE_WIDTH * game.map.getWidth(), - TILE_HEIGHT * game.map.getHeight())); + GAME_WIDTH, + GAME_HEIGHT)); if (!canvas) { @@ -46,171 +45,30 @@ Renderer::Renderer() SDL_SetRenderTarget(ren_.get(), canvas.get()); SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); - SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255); + SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); SDL_RenderClear(ren_.get()); - for (int y = game.map.getTop(); y < game.map.getBottom(); y++) - { - for (int x = game.map.getLeft(); x < game.map.getRight(); x++) - { - bool draw = true; - - if ((game.player_x == x && game.player_y == y) && game.renderPlayer) - { - SDL_SetRenderDrawColor(ren_.get(), 255, 255, 0, 255); - } else if (!game.map.at(x,y).lit) - { - if (drawDark) - { - SDL_SetRenderDrawColor(ren_.get(), 40, 40, 40, 255); - } else { - draw = false; - } - } else { - int alpha = 255; - - switch (game.map.at(x,y).tile) - { - case Tile::Floor: - { - SDL_SetRenderDrawColor(ren_.get(), 210, 210, 210, alpha); - break; - } - - case Tile::Wall: - { - SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, alpha); - break; - } - - case Tile::Dust: - { - SDL_SetRenderDrawColor(ren_.get(), 128, 40, 255, alpha); - break; - } - - case Tile::Lamp: - { - SDL_SetRenderDrawColor(ren_.get(), 0, 255, 255, alpha); - break; - } - } - } - - if (draw) - { - SDL_Rect rect { - game.map.getTrueX(x) * TILE_WIDTH, - game.map.getTrueY(y) * TILE_HEIGHT, - TILE_WIDTH, - TILE_HEIGHT}; - - SDL_RenderFillRect(ren_.get(), &rect); - } - } + for (Sprite& sprite : game.getSprites()) { + SDL_Rect dest { sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight() }; + SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest); } - texture_ptr mask( - SDL_CreateTexture( - ren_.get(), - SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_TARGET, - TILE_WIDTH * game.map.getWidth(), - TILE_HEIGHT * game.map.getHeight())); - - if (!mask) - { - throw sdl_error(); - } - - SDL_SetRenderTarget(ren_.get(), mask.get()); - SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); - SDL_RenderClear(ren_.get()); + SDL_SetRenderTarget(ren_.get(), nullptr); + SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); + SDL_RenderPresent(ren_.get()); +} - for (int y = game.map.getTop(); y < game.map.getBottom(); y++) +int Renderer::loadImageFromFile(std::string_view filename) { + surface_ptr pfs(IMG_Load(filename.data())); + if (!pfs) { - for (int x = game.map.getLeft(); x < game.map.getRight(); x++) - { - if (game.map.at(x,y).lightType != Source::None) - { - texture_ptr sourceMask( - SDL_CreateTexture( - ren_.get(), - SDL_PIXELFORMAT_RGBA8888, - SDL_TEXTUREACCESS_TARGET, - TILE_WIDTH * game.map.getWidth(), - TILE_HEIGHT * game.map.getHeight())); - - if (!sourceMask) - { - throw sdl_error(); - } - - SDL_SetRenderTarget(ren_.get(), sourceMask.get()); - SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); - SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); - SDL_RenderClear(ren_.get()); - - int fadeX = game.map.getTrueX(x) - game.map.at(x,y).lightRadius; - int fadeY = game.map.getTrueY(y) - game.map.at(x,y).lightRadius; - int fadeRight = game.map.getTrueX(x) + game.map.at(x,y).lightRadius; - int fadeBottom = game.map.getTrueY(y) + game.map.at(x,y).lightRadius; - - SDL_Rect fadeRect { - fadeX * TILE_WIDTH, - fadeY * TILE_HEIGHT, - (game.map.at(x,y).lightRadius * 2 + 1) * TILE_WIDTH, - (game.map.at(x,y).lightRadius * 2 + 1) * TILE_HEIGHT}; - - if (game.map.at(x,y).lightType == Source::Lamp) - { - SDL_SetTextureBlendMode(lampFade_.get(), SDL_BLENDMODE_NONE); - SDL_RenderCopy(ren_.get(), lampFade_.get(), nullptr, &fadeRect); - } else if (game.map.at(x,y).lightType == Source::Player) { - SDL_SetTextureBlendMode(playerFade_.get(), SDL_BLENDMODE_NONE); - SDL_RenderCopy(ren_.get(), playerFade_.get(), nullptr, &fadeRect); - } else if (game.map.at(x,y).lightType == Source::Dust) { - SDL_SetTextureBlendMode(dustFade_.get(), SDL_BLENDMODE_NONE); - SDL_RenderCopy(ren_.get(), dustFade_.get(), nullptr, &fadeRect); - } - - SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); - - for (int sy = fadeY; sy < fadeBottom; sy++) - { - for (int sx = fadeX; sx < fadeRight; sx++) - { - if (!game.map.at(x,y).litTiles.count({sx, sy})) - { - SDL_Rect rect { - game.map.getTrueX(sx) * TILE_WIDTH, - game.map.getTrueY(sy) * TILE_HEIGHT, - TILE_WIDTH, - TILE_HEIGHT}; - - SDL_RenderFillRect(ren_.get(), &rect); - } - } - } - - SDL_SetRenderTarget(ren_.get(), mask.get()); - SDL_SetTextureBlendMode(sourceMask.get(), SDL_BLENDMODE_ADD); - SDL_RenderCopy(ren_.get(), sourceMask.get(), nullptr, nullptr); - } - } + throw img_error(); } - SDL_SetRenderTarget(ren_.get(), canvas.get()); - SDL_SetTextureBlendMode(mask.get(), SDL_BLENDMODE_MOD); - SDL_RenderCopy(ren_.get(), mask.get(), nullptr, nullptr); + texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); - SDL_SetRenderTarget(ren_.get(), nullptr); + int texId = textures_.size(); + textures_.push_back(std::move(tex)); - SDL_Rect zoomRect; - - std::tie(zoomRect.x, zoomRect.y, zoomRect.w, zoomRect.h) = - calculateZoomRect(game); - - SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); - SDL_RenderPresent(ren_.get()); -}*/ + return texId; +} diff --git a/src/renderer.h b/src/renderer.h index 78856ed..abedb62 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include class Game; @@ -113,7 +115,9 @@ public: Renderer(); - //void render(); + void render(Game& game); + + int loadImageFromFile(std::string_view filename); private: @@ -121,6 +125,8 @@ private: img_wrapper img_; window_ptr win_; renderer_ptr ren_; + + std::vector textures_; }; #endif /* end of include guard: RENDERER_H_6A58EC30 */ diff --git a/src/sprite.cpp b/src/sprite.cpp new file mode 100644 index 0000000..0059f70 --- /dev/null +++ b/src/sprite.cpp @@ -0,0 +1,6 @@ +#include "sprite.h" +#include + +Sprite::Sprite(std::string_view filename, Renderer& renderer) { + textureId_ = renderer.loadImageFromFile(filename); +} \ No newline at end of file diff --git a/src/sprite.h b/src/sprite.h new file mode 100644 index 0000000..3af9341 --- /dev/null +++ b/src/sprite.h @@ -0,0 +1,33 @@ +#ifndef SPRITE_H_70503825 +#define SPRITE_H_70503825 + +#include +#include "renderer.h" + +class Sprite { +public: + + Sprite(std::string_view filename, Renderer& renderer); + + int getTextureId() const { + return textureId_; + } + + int getX() { return x_; } + + int getY() { return y_; } + + int getWidth() { return width_; } + + int getHeight() { return height_; } + +private: + + int textureId_; + int x_ = 0; + int y_ = 0; + int width_ = 17*4; + int height_ = 27*4; +}; + +#endif /* end of include guard: SPRITE_H_70503825 */ -- cgit 1.4.1