forked from inspirit/PS3EYEDriver
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsdl.cxx
181 lines (145 loc) · 4.54 KB
/
sdl.cxx
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* PS3EYEDriver Simple SDL 2 example, using OpenGL where available.
* Thomas Perl <[email protected]>; 2014-01-10
* Joseph Howse <[email protected]>; 2014-12-26
**/
#include "ps3eye.hpp"
#include <iostream>
#include <sstream>
#include <iterator>
#include <thread>
#include <chrono>
#include <SDL.h>
static void print_renderer_info(SDL_Renderer* renderer)
{
SDL_RendererInfo renderer_info;
SDL_GetRendererInfo(renderer, &renderer_info);
printf("Renderer: %s\n", renderer_info.name);
}
static void run_camera(ps3eye::resolution res, int fps)
{
ps3eye::camera::set_debug(true);
auto cameras = ps3eye::list_devices();
if (cameras.empty())
{
fprintf(stderr, "no device\n");
return;
}
auto camera = cameras[0];
bool running = true;
running &= camera->init(res, fps, ps3eye::fmt_BGR);
running &= camera->start();
if (!running)
{
fprintf(stderr, "device init failed\n");
return;
}
//camera->set_flip_status(true); /* mirrored left-right */
auto [ w, h ] = camera->size();
char title[256];
sprintf(title, "%dx%d@%dHz\n", w, h, camera->framerate());
SDL_Window* window = SDL_CreateWindow(title,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
w, h, 0);
if (!window)
{
fprintf(stderr, "Failed to create window: %s\n", SDL_GetError());
return;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer)
{
fprintf(stderr, "Failed to create renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
return;
}
SDL_RenderSetLogicalSize(renderer, camera->width(), camera->height());
print_renderer_info(renderer);
SDL_Texture* video_tex =
SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGR24, SDL_TEXTUREACCESS_STREAMING,
camera->width(), camera->height());
if (!video_tex)
{
fprintf(stderr, "Failed to create video texture: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return;
}
fprintf(stderr, "camera mode: %dx%d@%dHz\n", w, h, camera->framerate());
SDL_Event e;
unsigned last_ticks = 0;
unsigned last_frames = 0;
while (running)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT ||
(e.type == SDL_KEYUP && e.key.keysym.scancode == SDL_SCANCODE_ESCAPE))
{
running = false;
}
}
{
unsigned now_ticks = SDL_GetTicks();
last_frames++;
if (ps3eye::camera::is_debugging() &&
now_ticks - last_ticks > 1000 * 10)
{
fprintf(stderr, "FPS: %.2f\n", last_frames * 1000 / double(now_ticks - last_ticks));
last_ticks = now_ticks;
last_frames = 0;
}
}
void* video_tex_pixels;
int pitch;
SDL_LockTexture(video_tex, nullptr, &video_tex_pixels, &pitch);
using namespace std::chrono_literals;
if (int fps = camera->framerate(); fps > 0 && fps < 60)
std::this_thread::sleep_for(1ms * fps/2);
bool status = camera->get_frame((uint8_t*)video_tex_pixels);
SDL_UnlockTexture(video_tex);
if (status)
SDL_RenderCopy(renderer, video_tex, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
camera->stop();
SDL_DestroyTexture(video_tex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
int main(int argc, char** argv)
{
ps3eye::resolution res = ps3eye::res_VGA;
int fps = 60;
for (int i = 1; i < argc; i++)
{
bool good_arg = false;
if (std::string(argv[i]) == "--qvga")
{
res = ps3eye::res_QVGA;
good_arg = true;
}
if ((std::string(argv[i]) == "--fps") && argc > i)
{
std::istringstream new_fps_ss(argv[i + 1]);
if (new_fps_ss >> fps)
{
good_arg = true;
}
i++;
}
if (!good_arg)
{
std::cerr << "Usage: " << argv[0]
<< " [--fps num] [--qvga]" << std::endl;
return 64 /* EX_USAGE */;
}
}
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Failed to initialize SDL: %s\n", SDL_GetError());
return EXIT_FAILURE;
}
run_camera(res, fps);
return EXIT_SUCCESS;
}