-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdl_viewer.cpp
77 lines (63 loc) · 2.13 KB
/
sdl_viewer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "sdl_viewer.h"
#include <mutex>
#include <string>
#include <SDL2/SDL.h>
#include "common.h"
SDLViewer::SDLViewer(const std::string& title, int width, int height, int window_scale) :
title_(title) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
throw std::runtime_error(SDL_GetError());
}
window_ = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width * window_scale, height * window_scale, SDL_WINDOW_SHOWN);
if (!window_) {
throw std::runtime_error(SDL_GetError());
}
renderer_ = SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer_) {
throw std::runtime_error(SDL_GetError());
}
SDL_SetRenderDrawColor(renderer_, 0xFF, 0xFF, 0xFF, 0xFF);
window_tex_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGB24,
SDL_TEXTUREACCESS_STREAMING, width, height);
if (!window_tex_) {
throw std::runtime_error(SDL_GetError());
}
timer_.Start();
}
SDLViewer::~SDLViewer() {
SDL_DestroyTexture(window_tex_);
SDL_DestroyRenderer(renderer_);
SDL_DestroyWindow(window_);
SDL_Quit();
}
std::vector<SDL_Event> SDLViewer::Update() {
const std::lock_guard<std::mutex> lock(mu_);
std::vector<SDL_Event> events;
if (!window_tex_) {
throw std::runtime_error("Need to set the frame before calling Update().");
}
SDL_Event e;
while (SDL_PollEvent(&e)) { events.push_back(e); }
SDL_RenderCopy(renderer_, window_tex_, NULL, NULL );
SDL_RenderPresent(renderer_);
++num_updates_;
// Compute fps and set window title.
float avg_fps = num_updates_ / (timer_.Ms() / 1000.0f);
SDL_SetWindowTitle(window_,
(title_ + " - " + std::to_string(static_cast<int>(avg_fps)) + "fps").c_str());
if (timer_.Ms() >= 1'000) {
num_updates_ = 0;
timer_.Start();
}
return events;
}
void SDLViewer::SetFrameRGB24(uint8_t* rgb24, int height) {
const std::lock_guard<std::mutex> lock(mu_);
void* pixeldata;
int pitch;
// Lock the texture and upload the image to the GPU.
SDL_LockTexture(window_tex_, nullptr, &pixeldata, &pitch);
std::memcpy(pixeldata, rgb24, pitch * height);
SDL_UnlockTexture(window_tex_);
}