summaryrefslogtreecommitdiff
path: root/graphics.cpp
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2022-10-20 01:10:34 +0300
committerOxore <oxore@protonmail.com>2022-10-20 01:10:34 +0300
commit911f72b2299ec654178af0b4e4fbbd3f870779b3 (patch)
tree35590d16197a05a79872118d2837cb3a9b5a9f21 /graphics.cpp
parenteddd0a826b1720c26b0ac5df0269db3982bc8f35 (diff)
Fix buffer overflow access, continue implementing graphics
Diffstat (limited to 'graphics.cpp')
-rw-r--r--graphics.cpp25
1 files changed, 15 insertions, 10 deletions
diff --git a/graphics.cpp b/graphics.cpp
index 2455630..03379c5 100644
--- a/graphics.cpp
+++ b/graphics.cpp
@@ -18,7 +18,8 @@ Graphics::Graphics()
_window = SDL_CreateWindow("Gut (SEGA MD/G emulator)",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
- VDP().render_width, VDP().render_height,
+ 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());
@@ -49,29 +50,33 @@ Graphics::Graphics()
Graphics::~Graphics()
{
#if HAS_GRAPHICS == 1
- SDL_DestroyTexture(_render_texture);
- SDL_DestroyRenderer(_renderer);
- SDL_DestroyWindow(_window);
+ 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 uint8_t* buffer = vdp.GetRenderedBuffer();
+ 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 (int row = 0; (size_t)row < VDP().render_height; row++) {
+ for (size_t row = 0; 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;
- }
+ memcpy(dst, buffer, VDP().render_width * sizeof(*dst));
}
SDL_UnlockTexture(_render_texture);
SDL_RenderClear(_renderer);