From d92c7970df0d2e0a0c3f84037640336102c084a6 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 30 Jan 2021 04:46:44 -0500 Subject: Imported renderer stuff from Ether --- src/consts.h | 7 ++ src/main.cpp | 25 ++++++- src/renderer.cpp | 216 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/renderer.h | 126 ++++++++++++++++++++++++++++++++ 4 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 src/consts.h create mode 100644 src/renderer.cpp create mode 100644 src/renderer.h (limited to 'src') diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..2088f45 --- /dev/null +++ b/src/consts.h @@ -0,0 +1,7 @@ +#ifndef CONSTS_H_9561E49C +#define CONSTS_H_9561E49C + +const int GAME_WIDTH = 640; +const int GAME_HEIGHT = 480; + +#endif /* end of include guard: CONSTS_H_9561E49C */ diff --git a/src/main.cpp b/src/main.cpp index 22bdf92..2da7d9a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,29 @@ #include +#include "renderer.h" + +void loop(Renderer& renderer) { + for (;;) { + SDL_Event e; + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) + { + return; + } + } + } +} int main(int, char**) { - std::cout << "hi" << std::endl; + try + { + Renderer renderer; + + loop(renderer); + } catch (const sdl_error& ex) + { + std::cout << "SDL error (" << ex.what() << ")" << std::endl; + } + return 0; } diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..e29d8cd --- /dev/null +++ b/src/renderer.cpp @@ -0,0 +1,216 @@ +#include "renderer.h" +#include "consts.h" + +Renderer::Renderer() +{ + win_ = window_ptr( + SDL_CreateWindow( + "Tanetane", + 100, + 100, + GAME_WIDTH, + GAME_HEIGHT, + SDL_WINDOW_SHOWN)); + + if (!win_) + { + throw sdl_error(); + } + + ren_ = renderer_ptr( + SDL_CreateRenderer( + win_.get(), + -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)); + + if (!ren_) + { + throw sdl_error(); + } +} + +/*void Renderer::render() +{ + texture_ptr canvas( + SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + TILE_WIDTH * game.map.getWidth(), + TILE_HEIGHT * game.map.getHeight())); + + if (!canvas) + { + throw sdl_error(); + } + + 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_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); + } + } + } + + 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()); + + for (int y = game.map.getTop(); y < game.map.getBottom(); y++) + { + 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); + } + } + } + + SDL_SetRenderTarget(ren_.get(), canvas.get()); + SDL_SetTextureBlendMode(mask.get(), SDL_BLENDMODE_MOD); + SDL_RenderCopy(ren_.get(), mask.get(), nullptr, nullptr); + + SDL_SetRenderTarget(ren_.get(), nullptr); + + 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()); +}*/ diff --git a/src/renderer.h b/src/renderer.h new file mode 100644 index 0000000..78856ed --- /dev/null +++ b/src/renderer.h @@ -0,0 +1,126 @@ +#ifndef RENDERER_H_6A58EC30 +#define RENDERER_H_6A58EC30 + +#include +#include +#include +#include + +class Game; + +class sdl_error : public std::logic_error { +public: + + sdl_error() : std::logic_error(SDL_GetError()) + { + } +}; + +class img_error : public std::logic_error { +public: + + img_error() : std::logic_error(IMG_GetError()) + { + } +}; + +class sdl_wrapper { +public: + + sdl_wrapper() + { + if (SDL_Init(SDL_INIT_VIDEO) != 0) + { + sdl_error ex; + SDL_Quit(); + + throw ex; + } + } + + ~sdl_wrapper() + { + SDL_Quit(); + } +}; + +class img_wrapper { +public: + + img_wrapper() + { + if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) + { + img_error ex; + IMG_Quit(); + + throw ex; + } + } + + ~img_wrapper() + { + IMG_Quit(); + } +}; + +class window_deleter { +public: + + void operator()(SDL_Window* ptr) + { + SDL_DestroyWindow(ptr); + } +}; + +using window_ptr = std::unique_ptr; + +class renderer_deleter { +public: + + void operator()(SDL_Renderer* ptr) + { + SDL_DestroyRenderer(ptr); + } +}; + +using renderer_ptr = std::unique_ptr; + +class surface_deleter { +public: + + void operator()(SDL_Surface* ptr) + { + SDL_FreeSurface(ptr); + } +}; + +using surface_ptr = std::unique_ptr; + +class texture_deleter { +public: + + void operator()(SDL_Texture* ptr) + { + SDL_DestroyTexture(ptr); + } +}; + +using texture_ptr = std::unique_ptr; + +class Renderer { +public: + + Renderer(); + + //void render(); + +private: + + sdl_wrapper sdl_; + img_wrapper img_; + window_ptr win_; + renderer_ptr ren_; +}; + +#endif /* end of include guard: RENDERER_H_6A58EC30 */ -- cgit 1.4.1