#include <stdio.h>
#include "SDL.h"
#include "escapi.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
SDL_Window *gSdlWindow;
SDL_Renderer *gSdlRenderer;
SDL_Texture *gSdlScreenTexture;
unsigned int *gSdlScreenPixels;
int font_x, font_y, font_comp;
unsigned int *font = 0;
struct SimpleCapParams capture[4];
int devices = 0;
char devicenames[4][24] = { "device 1", "device 2", "device 3", "device 4" };
unsigned int saturated_add(int aPix1, int aPix2)
{
int r = ((aPix1 >> 0) & 0xff) + ((aPix2 >> 0) & 0xff);
if (r > 0xff) r = 0xff;
int g = ((aPix1 >> 8) & 0xff) + ((aPix2 >> 8) & 0xff);
if (g > 0xff) g = 0xff;
int b = ((aPix1 >> 16) & 0xff) + ((aPix2 >> 16) & 0xff);
if (b > 0xff) b = 0xff;
return 0xff000000 | (b << 16) | (g << 8) | (r << 0);
}
unsigned int saturated_sub(int aPix1, int aPix2)
{
int r = ((aPix1 >> 0) & 0xff) - ((aPix2 >> 0) & 0xff);
if (r < 0) r = 0;
int g = ((aPix1 >> 8) & 0xff) - ((aPix2 >> 8) & 0xff);
if (g < 0) g = 0;
int b = ((aPix1 >> 16) & 0xff) - ((aPix2 >> 16) & 0xff);
if (b < 0) b = 0;
return 0xff000000 | (b << 16) | (g << 8) | (r << 0);
}
void drawchar(char ch, int x, int y)
{
int i, j;
for (i = 0; i < 24; i++)
{
for (j = 0; j < font_x; j++)
{
unsigned int p = font[(i + (ch - 32) * 24) * font_x + j];
unsigned int b = gSdlScreenPixels[(y + i) * 640 + x + j];
unsigned int c = gSdlScreenPixels[(y + i + 2) * 640 + x + j + 2];
gSdlScreenPixels[(y + i) * 640 + x + j] = saturated_add(b, p);
gSdlScreenPixels[(y + i + 2) * 640 + x + j + 2] = saturated_sub(c, p);
}
}
}
void drawstring(char * string, int x, int y)
{
if (!font) return;
while (*string)
{
drawchar(*string, x, y);
x += 14;
string++;
}
}
void render()
{
int i, j, k;
for (k = 0; k < devices; k++)
{
if (isCaptureDone(k))
{
int linofs = 0;
int xofs = (k & 1) ? 320 : 0;
int yofs = (k & 2) ? (240 * 640) : 0;
int ofs;
for (i = 0; i < 240; i++)
{
for (j = 0, ofs = yofs + xofs; j < 320; j++, ofs++, linofs++)
{
gSdlScreenPixels[ofs] = capture[k].mTargetBuf[linofs];
}
yofs += 640;
}
doCapture(k);
drawstring(devicenames[k], xofs, (k & 2) ? 240 : 0);
}
}
SDL_Delay(1);
SDL_UpdateTexture(gSdlScreenTexture, NULL, gSdlScreenPixels, 640 * sizeof(Uint32));
SDL_RenderCopy(gSdlRenderer, gSdlScreenTexture, NULL, NULL);
SDL_RenderPresent(gSdlRenderer);
}
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
devices = setupESCAPI();
if (devices < 1)
{
printf("Unable to init ESCAPI\n");
exit(1);
}
if (devices > 4)
devices = 4;
for (int i = 0; i < devices; i++)
{
capture[i].mWidth = 320;
capture[i].mHeight = 240;
capture[i].mTargetBuf = new int[320 * 240];
getCaptureDeviceName(i, devicenames[i], 24);
initCapture(i, &capture[i]);
doCapture(i);
}
gSdlWindow = SDL_CreateWindow(
"ESCAPI MultiCap",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480, 0);
if (gSdlWindow == NULL)
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
gSdlRenderer = SDL_CreateRenderer(gSdlWindow, -1, 0);
gSdlScreenTexture = SDL_CreateTexture(gSdlRenderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
640, 480);
gSdlScreenPixels = new unsigned int[640 * 480];
font = (unsigned int *)stbi_load("font14x24.png", &font_x, &font_y, &font_comp, 4);
while (1)
{
render();
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
int k;
for (k = 0; k < devices; k++)
deinitCapture(k);
return 0;
}
break;
case SDL_QUIT:
return(0);
}
}
}
return 0;
}