1
0
Fork 0
2019-nes-emulator/Source/Platform.cpp

158 lines
2.6 KiB
C++

#include "Platform.hpp"
#include "Input.hpp"
#include <SDL2/SDL.h>
Platform::Platform(char const* title, int windowWidth, int windowHeight, int textureWidth, int textureHeight)
{
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(title, 0, 0, windowWidth, windowHeight, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(
renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, textureWidth, textureHeight);
}
Platform::~Platform()
{
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void Platform::Update(void const* buffer, int pitch)
{
SDL_UpdateTexture(texture, nullptr, buffer, pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
bool Platform::ProcessInput(uint8_t* buttons)
{
bool quit = false;
SDL_Event event{};
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
{
quit = true;
break;
}
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
{
quit = true;
break;
}
case SDLK_UP:
{
{buttons[Buttons::Up] = 1u;}
break;
}
case SDLK_DOWN:
{
{buttons[Buttons::Down] = 1u;}
break;
}
case SDLK_LEFT:
{
{buttons[Buttons::Left] = 1u;}
break;
}
case SDLK_RIGHT:
{
{buttons[Buttons::Right] = 1u;}
break;
}
case SDLK_LSHIFT:
{
buttons[Buttons::Select] = 1u;
break;
}
case SDLK_RETURN:
{
{buttons[Buttons::Start] = 1u;}
break;
}
case SDLK_z:
{
{buttons[Buttons::A] = 1u;}
break;
}
case SDLK_x:
{
{buttons[Buttons::B] = 1u;}
break;
}
}
break;
}
case SDL_KEYUP:
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
{
{buttons[Buttons::Up] = 0u;}
break;
}
case SDLK_DOWN:
{
{buttons[Buttons::Down] = 0u;}
break;
}
case SDLK_LEFT:
{
{buttons[Buttons::Left] = 0u;}
break;
}
case SDLK_RIGHT:
{
{buttons[Buttons::Right] = 0u;}
break;
}
case SDLK_LSHIFT:
{
{buttons[Buttons::Select] = 0u;}
break;
}
case SDLK_RETURN:
{
{buttons[Buttons::Start] = 0u;}
break;
}
case SDLK_z:
{
{buttons[Buttons::A] = 0u;}
break;
}
case SDLK_x:
{
{buttons[Buttons::B] = 0u;}
break;
}
}
break;
}
}
}
return quit;
}