Branch data Line data Source code
1 : : #include "gpio_sdl.h"
2 : :
3 : : #include <pigpio.h>
4 : :
5 : : #include <iostream>
6 : :
7 : 0 : auto initGPIO() -> bool {
8 : 0 : if (gpioInitialise() < 0) {
9 : 0 : std::cerr << "Failed to initialize pigpio." << '\n';
10 : 0 : return false;
11 : : }
12 : 0 : return true;
13 : : }
14 : :
15 : 0 : auto readButtonState(int gpioPin) -> int { return gpioRead(static_cast<unsigned int>(gpioPin)); }
16 : :
17 : 0 : auto initSDL(SDL_Window*& window, SDL_Renderer*& renderer) -> bool {
18 : 0 : if (SDL_Init(SDL_INIT_VIDEO) < 0) {
19 : 0 : std::cerr << "Failed to initialize SDL: " << SDL_GetError() << '\n';
20 : 0 : return false;
21 : : }
22 : 0 : window = SDL_CreateWindow("GPIO + SDL2 Example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
23 : : 640, 480, SDL_WINDOW_SHOWN);
24 : 0 : if (!window) {
25 : 0 : std::cerr << "Failed to create SDL window: " << SDL_GetError() << '\n';
26 : 0 : return false;
27 : : }
28 : 0 : renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
29 : 0 : if (!renderer) {
30 : 0 : std::cerr << "Failed to create SDL renderer: " << SDL_GetError() << '\n';
31 : 0 : SDL_DestroyWindow(window);
32 : 0 : return false;
33 : : }
34 : 0 : return true;
35 : : }
36 : :
37 : 0 : void cleanupSDL(SDL_Window* window, SDL_Renderer* renderer) {
38 : 0 : SDL_DestroyRenderer(renderer);
39 : 0 : SDL_DestroyWindow(window);
40 : 0 : SDL_Quit();
41 : 0 : }
42 : :
43 : 0 : void cleanupGPIO() { gpioTerminate(); }
|