parent
5dc45a75a6
commit
5efa8be1c4
@ -0,0 +1,55 @@ |
|||||||
|
#include "display.hpp" |
||||||
|
#include <SFML/Window/Event.hpp> |
||||||
|
#include <SFML/System.hpp> |
||||||
|
#include <SFML/Audio.hpp> |
||||||
|
|
||||||
|
Display::Display(sf::RenderWindow& window, sf::Vector2f pos, sf::Vector2f size) : |
||||||
|
window(window) |
||||||
|
{ |
||||||
|
float border_width = 2.0f; |
||||||
|
texture.setSmooth(false); |
||||||
|
|
||||||
|
float ratio = (size.x - border_width * 2) / 64.0f; |
||||||
|
// rectangle position doesn't include the border because everyone hates humans
|
||||||
|
sf::Vector2f border_pos{pos.x + border_width, pos.y + border_width}; |
||||||
|
|
||||||
|
sf::Vector2f scale{ratio, ratio}; |
||||||
|
// have to move in one more time for the border we just moved in for the border
|
||||||
|
texture_sprite.setPosition({border_pos.x + border_width, border_pos.y + border_width}); |
||||||
|
texture_sprite.setScale(scale); |
||||||
|
|
||||||
|
border.setPosition(border_pos); |
||||||
|
border.setSize({64 * ratio, 32 * ratio}); |
||||||
|
border.setOutlineColor({30,20,50,255}); |
||||||
|
border.setOutlineThickness(border_width); |
||||||
|
border.setFillColor({0,0,0,255}); |
||||||
|
} |
||||||
|
|
||||||
|
void Display::handle_inputs(Chip8& vm) { |
||||||
|
while (const auto event = window.pollEvent()) { |
||||||
|
if(event->is<sf::Event::Closed>()) { |
||||||
|
window.close(); |
||||||
|
} |
||||||
|
|
||||||
|
if(const auto* key = event->getIf<sf::Event::KeyPressed>()) { |
||||||
|
vm.handle_keyboard(key->scancode, true); |
||||||
|
} else if(const auto* key = event->getIf<sf::Event::KeyReleased>()) { |
||||||
|
vm.handle_keyboard(key->scancode, false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Display::update(Chip8& vm) { |
||||||
|
handle_inputs(vm); |
||||||
|
texture.update((uint8_t *)(vm.video), {64, 32}, {0,0}); |
||||||
|
} |
||||||
|
|
||||||
|
void Display::render() { |
||||||
|
window.draw(border); |
||||||
|
window.draw(texture_sprite); |
||||||
|
window.display(); |
||||||
|
} |
||||||
|
|
||||||
|
bool Display::active() { |
||||||
|
return window.isOpen(); |
||||||
|
} |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
#pragma once |
||||||
|
#include <SFML/Graphics/Sprite.hpp> |
||||||
|
#include <SFML/Graphics/Texture.hpp> |
||||||
|
#include <SFML/Graphics/RenderWindow.hpp> |
||||||
|
#include <SFML/Graphics/RectangleShape.hpp> |
||||||
|
#include "chip8.hpp" |
||||||
|
|
||||||
|
struct Display { |
||||||
|
sf::RenderWindow& window; |
||||||
|
sf::Texture texture{sf::Vector2u{64,32}}; |
||||||
|
sf::Sprite texture_sprite{texture}; |
||||||
|
sf::RectangleShape border; |
||||||
|
|
||||||
|
Display(sf::RenderWindow& window, sf::Vector2f pos, sf::Vector2f size); |
||||||
|
|
||||||
|
void handle_inputs(Chip8& vm); |
||||||
|
void update(Chip8& vm); |
||||||
|
void render(); |
||||||
|
bool active(); |
||||||
|
}; |
||||||
Loading…
Reference in new issue