summaryrefslogtreecommitdiff
path: root/graphics.cpp
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2022-10-19 02:28:03 +0300
committerOxore <oxore@protonmail.com>2022-10-19 02:28:03 +0300
commiteddd0a826b1720c26b0ac5df0269db3982bc8f35 (patch)
tree94548f986eb190c734315db252e57a79c118031f /graphics.cpp
parentae9a7aef2f0422f6872e6ae27e4e4e2084d8ce8f (diff)
Begin implementing VDP rendering
Diffstat (limited to 'graphics.cpp')
-rw-r--r--graphics.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/graphics.cpp b/graphics.cpp
new file mode 100644
index 0000000..2455630
--- /dev/null
+++ b/graphics.cpp
@@ -0,0 +1,83 @@
+/* SPDX-License-Identifier: Unlicense
+ */
+
+#include "graphics.hpp"
+#include "vdp.hpp"
+
+#include <cstdlib>
+
+Graphics::Graphics()
+{
+#if HAS_GRAPHICS == 1
+ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
+
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
+ return;
+ }
+ _window = SDL_CreateWindow("Gut (SEGA MD/G emulator)",
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ VDP().render_width, VDP().render_height,
+ SDL_WINDOW_RESIZABLE);
+ if (!_window) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
+ SDL_Quit();
+ return;
+ }
+ _renderer = SDL_CreateRenderer(_window, -1, 0);
+ if (!_renderer) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
+ SDL_Quit();
+ return;
+ }
+ _render_texture = SDL_CreateTexture(
+ _renderer,
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_TEXTUREACCESS_STREAMING,
+ VDP().render_width,
+ VDP().render_height);
+ if (!_render_texture) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
+ SDL_Quit();
+ return;
+ }
+#endif
+ _initialized_ok = true;
+}
+
+Graphics::~Graphics()
+{
+#if HAS_GRAPHICS == 1
+ SDL_DestroyTexture(_render_texture);
+ SDL_DestroyRenderer(_renderer);
+ SDL_DestroyWindow(_window);
+ SDL_Quit();
+#endif
+}
+
+void Graphics::Render(const VDP& vdp)
+{
+ const uint8_t* buffer = vdp.GetRenderedBuffer();
+#if HAS_GRAPHICS == 1
+ void* pixels;
+ int pitch;
+ if (SDL_LockTexture(_render_texture, NULL, &pixels, &pitch) < 0) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
+ abort();
+ }
+ for (int row = 0; (size_t)row < VDP().render_height; row++) {
+ uint32_t *dst = (uint32_t*)((uint8_t*)pixels + row * pitch);
+ for (int col = 0; (size_t)col < VDP().render_width; col++, buffer += 4) {
+ const uint32_t color = ((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);
+ *dst++ = color;
+ }
+ }
+ SDL_UnlockTexture(_render_texture);
+ SDL_RenderClear(_renderer);
+ SDL_RenderCopy(_renderer, _render_texture, NULL, NULL);
+ SDL_RenderPresent(_renderer);
+#else
+ (void) buffer;
+#endif
+}