summaryrefslogtreecommitdiff
path: root/graphics.cpp
blob: 03379c5ef71567be7a8276c2155158f5ea9b91b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* 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
    if (_window) {
        if (_renderer) {
            if (_render_texture) {
                SDL_DestroyTexture(_render_texture);
            }
            SDL_DestroyRenderer(_renderer);
        }
        SDL_DestroyWindow(_window);
    }
    SDL_Quit();
#endif
}

void Graphics::Render(const VDP& vdp)
{
    const uint32_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());
        // TODO probably should return and propagate error
        abort();
    }
    for (size_t row = 0; row < VDP().render_height; row++) {
        uint32_t *dst = (uint32_t*)((uint8_t*)pixels + row * pitch);
        memcpy(dst, buffer, VDP().render_width * sizeof(*dst));
    }
    SDL_UnlockTexture(_render_texture);
    SDL_RenderClear(_renderer);
    SDL_RenderCopy(_renderer, _render_texture, NULL, NULL);
    SDL_RenderPresent(_renderer);
#else
    (void) buffer;
#endif
}