37 lines
1.5 KiB
C++
37 lines
1.5 KiB
C++
//
|
|
// Created by Мустафаев Селим Мустафаевич on 24.08.2023.
|
|
//
|
|
|
|
#include "Window.h"
|
|
|
|
namespace nes {
|
|
|
|
SdlWindow::SdlWindow(uint16_t width, uint16_t height): _width(width), _height(height) {
|
|
int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD);
|
|
if(res < 0) throw std::runtime_error("Error initializing SDL");
|
|
|
|
Uint32 flags = SDL_WINDOW_HIGH_PIXEL_DENSITY; //| SDL_WINDOW_VULKAN | SDL_WINDOW_METAL;
|
|
_wnd.reset(SDL_CreateWindow("NES", width, height, flags));
|
|
if(!_wnd) throw std::runtime_error("Error creating SDL window");
|
|
|
|
SDL_SetWindowResizable(_wnd.get(), true);
|
|
|
|
_renderer.reset(SDL_CreateRenderer(_wnd.get(), nullptr));
|
|
if(!_renderer) throw std::runtime_error("Error creating SDL renderer");
|
|
|
|
_texture.reset(SDL_CreateTexture(_renderer.get(), SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, width, height));
|
|
if(!_texture) throw std::runtime_error("Error creating SDL texture");
|
|
}
|
|
|
|
void SdlWindow::drawFrame(const Pixel *buffer) {
|
|
int pitch = static_cast<int>(_width*sizeof(Pixel));
|
|
SDL_UpdateTexture(_texture.get(), nullptr, buffer, pitch);
|
|
SDL_RenderClear(_renderer.get());
|
|
SDL_RenderTexture(_renderer.get(), _texture.get(), nullptr, nullptr);
|
|
SDL_RenderPresent(_renderer.get());
|
|
}
|
|
|
|
void SdlWindow::setSize(int width, int height) {
|
|
SDL_SetWindowSize(_wnd.get(), width, height);
|
|
}
|
|
} |