yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
renderer.h
Go to the documentation of this file.
1#ifndef YAZE_APP_CORE_PLATFORM_RENDERER_H
2#define YAZE_APP_CORE_PLATFORM_RENDERER_H
3
4#include <SDL.h>
5
6#include <memory>
7
8#include "absl/status/status.h"
9#include "absl/strings/str_format.h"
11#include "app/gfx/bitmap.h"
12
13namespace yaze {
14namespace app {
15namespace core {
16
25class Renderer {
26 public:
28 static Renderer instance;
29 return instance;
30 }
31
32 absl::Status CreateRenderer(SDL_Window *window) {
33 renderer_ = std::unique_ptr<SDL_Renderer, SDL_Deleter>(SDL_CreateRenderer(
34 window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED));
35 if (renderer_ == nullptr) {
36 return absl::InternalError(
37 absl::StrFormat("SDL_CreateRenderer: %s\n", SDL_GetError()));
38 }
39 SDL_SetRenderDrawBlendMode(renderer_.get(), SDL_BLENDMODE_BLEND);
40 SDL_SetRenderDrawColor(renderer_.get(), 0x00, 0x00, 0x00, 0x00);
41 return absl::OkStatus();
42 }
43
44 auto renderer() -> SDL_Renderer * { return renderer_.get(); }
45
49 void RenderBitmap(gfx::Bitmap *bitmap) {
50 bitmap->CreateTexture(renderer_.get());
51 }
52
56 void UpdateBitmap(gfx::Bitmap *bitmap) {
57 bitmap->UpdateTexture(renderer_.get());
58 }
59
60 absl::Status CreateAndRenderBitmap(int width, int height, int depth,
61 const std::vector<uint8_t> &data,
62 gfx::Bitmap &bitmap,
63 gfx::SnesPalette &palette) {
64 bitmap.Create(width, height, depth, data);
65 RETURN_IF_ERROR(bitmap.ApplyPalette(palette));
66 RenderBitmap(&bitmap);
67 return absl::OkStatus();
68 }
69
70 private:
71 Renderer() = default;
72
73 std::unique_ptr<SDL_Renderer, SDL_Deleter> renderer_;
74
75 Renderer(const Renderer &) = delete;
76 Renderer &operator=(const Renderer &) = delete;
77};
78
79} // namespace core
80} // namespace app
81} // namespace yaze
82
83#endif
The Renderer class represents the renderer for the Yaze application.
Definition renderer.h:25
absl::Status CreateRenderer(SDL_Window *window)
Definition renderer.h:32
auto renderer() -> SDL_Renderer *
Definition renderer.h:44
absl::Status CreateAndRenderBitmap(int width, int height, int depth, const std::vector< uint8_t > &data, gfx::Bitmap &bitmap, gfx::SnesPalette &palette)
Definition renderer.h:60
std::unique_ptr< SDL_Renderer, SDL_Deleter > renderer_
Definition renderer.h:73
void UpdateBitmap(gfx::Bitmap *bitmap)
Used to update a bitmap on the screen.
Definition renderer.h:56
Renderer(const Renderer &)=delete
static Renderer & GetInstance()
Definition renderer.h:27
void RenderBitmap(gfx::Bitmap *bitmap)
Used to render a bitmap to the screen.
Definition renderer.h:49
Renderer & operator=(const Renderer &)=delete
Represents a bitmap image.
Definition bitmap.h:67
absl::Status ApplyPalette(const SnesPalette &palette)
Copy color data from the SnesPalette into the SDL_Palette.
Definition bitmap.cc:327
void Create(int width, int height, int depth, const std::vector< uint8_t > &data)
Creates a bitmap object with the provided graphical data.
Definition bitmap.cc:230
void UpdateTexture(SDL_Renderer *renderer)
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:310
void CreateTexture(SDL_Renderer *renderer)
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:280
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
#define RETURN_IF_ERROR(expression)
Definition constants.h:62
Definition common.cc:22