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

158 lines
2.6 KiB
C++
Raw Normal View History

2019-09-06 23:58:57 +00:00
#include "Platform.hpp"
2019-09-11 23:20:30 +00:00
#include "Input.hpp"
2019-09-06 23:58:57 +00:00
#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);
}
2019-09-11 23:20:30 +00:00
bool Platform::ProcessInput(uint8_t* buttons)
2019-09-06 23:58:57 +00:00
{
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;
}
2019-09-11 23:20:30 +00:00
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;
}
2019-09-06 23:58:57 +00:00
}
break;
}
}
}
2019-09-11 23:20:30 +00:00
2019-09-06 23:58:57 +00:00
return quit;
}