98 lines
3.0 KiB
C++
98 lines
3.0 KiB
C++
#include "screen.hpp"
|
|
#include "SDL3/SDL_events.h"
|
|
#include "SDL3/SDL_log.h"
|
|
#include "SDL3/SDL_properties.h"
|
|
#include "SDL3/SDL_render.h"
|
|
#include "SDL3/SDL_video.h"
|
|
#include "utils.hpp"
|
|
#include <array>
|
|
#include <cstring>
|
|
|
|
Screen::Screen(int w, int h) {
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
|
|
SDL_Log("SDL not initialized, error: %s", SDL_GetError());
|
|
}
|
|
SDL_PropertiesID props{SDL_CreateProperties()};
|
|
|
|
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING,
|
|
"Chippotto");
|
|
SDL_SetBooleanProperty(props, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, true);
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, 1280);
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, 640);
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER,
|
|
SDL_WINDOWPOS_CENTERED);
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER,
|
|
SDL_WINDOWPOS_CENTERED);
|
|
|
|
if (window = SDL_CreateWindowWithProperties(props); window == nullptr) {
|
|
SDL_Log("Unable To create window");
|
|
}
|
|
if (renderer = SDL_CreateRenderer(window, nullptr); renderer == nullptr) {
|
|
SDL_Log("Window not created, error: %s", SDL_GetError());
|
|
}
|
|
|
|
if (texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
|
|
SDL_TEXTUREACCESS_STREAMING, w, h);
|
|
texture == nullptr) {
|
|
SDL_Log("Failed to create Texture, error: %s", SDL_GetError());
|
|
}
|
|
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_PIXELART);
|
|
SDL_AudioSpec audio_spec;
|
|
if (!SDL_LoadWAV("assets/audio.wav", &audio_spec, &buf, &len)) {
|
|
SDL_Log("Error Loading the beep sound");
|
|
}
|
|
|
|
stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK,
|
|
&audio_spec, nullptr, nullptr);
|
|
SDL_ResumeAudioStreamDevice(stream);
|
|
}
|
|
|
|
Screen::~Screen() {
|
|
SDL_DestroyTexture(texture);
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
}
|
|
|
|
void Screen::render(u32 *buffer, int pitch) {
|
|
void *pixels;
|
|
int r_pitch;
|
|
SDL_LockTexture(texture, nullptr, &pixels, &r_pitch);
|
|
r_pitch = {pitch};
|
|
memcpy(pixels, buffer, DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(u32));
|
|
SDL_UnlockTexture(texture);
|
|
|
|
SDL_RenderClear(renderer);
|
|
SDL_RenderTexture(renderer, texture, nullptr, nullptr);
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
void Screen::beep() {
|
|
if (SDL_GetAudioStreamQueued(stream) < (int)len) {
|
|
SDL_PutAudioStreamData(stream, buf, len);
|
|
}
|
|
}
|
|
|
|
void Screen::handler(bool &quit, Keypad &keypad) {
|
|
|
|
SDL_Event e;
|
|
SDL_zero(e);
|
|
while (SDL_PollEvent(&e)) {
|
|
if (e.type == SDL_EVENT_QUIT) {
|
|
quit = {true};
|
|
} else if (e.type == SDL_EVENT_KEY_DOWN) {
|
|
for (auto i{0}; i < 16; ++i) {
|
|
if (e.key.key == keys[i]) {
|
|
keypad[i] = true;
|
|
}
|
|
}
|
|
} else if (e.type == SDL_EVENT_KEY_UP) {
|
|
for (auto i{0}; i < 16; ++i) {
|
|
if (e.key.key == keys[i]) {
|
|
keypad[i] = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|