testoverlay2.c File Reference
`#include "SDL.h"`
`#include "testyuv_cvt.h"`
Include dependency graph for testoverlay2.c:

Go to the source code of this file.

## Macros

#define | MOOSEPIC_W 64
#define | MOOSEPIC_H 88
#define | MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
#define | MOOSEFRAMES_COUNT 10

## Functions

static void | quit (int rc)
static void | PrintUsage (char *argv0)
void | loop ()
int | main (int argc, char **argv)

## Variables

SDL_Color | MooseColors [84]
Uint8 | MooseFrame [MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
SDL_Texture * | MooseTexture
SDL_Rect | displayrect
int | window_w
int | window_h
SDL_Window * | window
SDL_Renderer * | renderer
int | paused = 0
int | i
SDL_bool | done = SDL_FALSE
static int | fpsdelay

## Macro Definition Documentation

## MOOSEFRAME_SIZE

#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)

Definition at line 30 of file testoverlay2.c.

Referenced by main().

## MOOSEFRAMES_COUNT

#define MOOSEFRAMES_COUNT 10

Definition at line 31 of file testoverlay2.c.

Referenced by loop(), and main().

## MOOSEPIC_H

#define MOOSEPIC_H 88

Definition at line 28 of file testoverlay2.c.

Referenced by main().

## MOOSEPIC_W

#define MOOSEPIC_W 64

Definition at line 27 of file testoverlay2.c.

Referenced by loop(), and main().

## Function Documentation

## loop()

void loop | ( |  | ) |

Definition at line 178 of file testoverlay2.c.

References done, SDL_WindowEvent::event, fpsdelay, SDL_Rect::h, i, SDL_Event::key, SDL_KeyboardEvent::keysym, MooseFrame, MOOSEFRAMES_COUNT, MOOSEPIC_W, SDL_Event::motion, NULL, paused, SDL_Delay, SDL_KEYDOWN, SDL_MOUSEBUTTONDOWN, SDL_MOUSEMOTION, SDL_PollEvent, SDL_QUIT, SDL_RenderClear, SDL_RenderCopy, SDL_RenderPresent, SDL_RenderSetViewport, SDL_TRUE, SDL_UpdateTexture, SDL_WINDOWEVENT, SDL_WINDOWEVENT_RESIZED, SDLK_ESCAPE, SDLK_SPACE, SDL_MouseMotionEvent::state, SDL_Keysym::sym, SDL_Event::type, SDL_Rect::w, SDL_Event::window, window_h, window_w, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_RenderSetViewport(renderer, NULL);
displayrect.w = window_w = event.window.data1;
displayrect.h = window_h = event.window.data2;
}
break;
case SDL_MOUSEBUTTONDOWN:
displayrect.x = event.button.x - window_w / 2;
displayrect.y = event.button.y - window_h / 2;
break;
case SDL_MOUSEMOTION:
if (event.motion.state) {
displayrect.x = event.motion.x - window_w / 2;
displayrect.y = event.motion.y - window_h / 2;
}
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_SPACE) {
paused = !paused;
break;
}
if (event.key.keysym.sym != SDLK_ESCAPE) {
break;
}
case SDL_QUIT:
done = SDL_TRUE;
break;
}
}
#ifndef __EMSCRIPTEN__
SDL_Delay(fpsdelay);
#endif
if (!paused) {
i = (i + 1) % MOOSEFRAMES_COUNT;
SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W);
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
SDL_RenderPresent(renderer);
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
SDL_RenderSetViewport
#define SDL_RenderSetViewport
Definition: SDL_dynapi_overrides.h:324
SDL_Event::motion
SDL_MouseMotionEvent motion
Definition: SDL_events.h:533
MOOSEPIC_W
#define MOOSEPIC_W
Definition: testoverlay2.c:27
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_KEYDOWN
Definition: SDL_events.h:93
SDL_TRUE
Definition: SDL_stdinc.h:142
SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:103
SDL_QUIT
Definition: SDL_events.h:60
MooseTexture
SDL_Texture * MooseTexture
Definition: testoverlay2.c:141
done
SDL_bool done
Definition: testoverlay2.c:149
SDL_UpdateTexture
#define SDL_UpdateTexture
Definition: SDL_dynapi_overrides.h:315
SDL_Event::window
SDL_WindowEvent window
Definition: SDL_events.h:529
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:343
MooseFrame
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
Definition: testoverlay2.c:140
renderer
SDL_Renderer * renderer
Definition: testoverlay2.c:146
MOOSEFRAMES_COUNT
#define MOOSEFRAMES_COUNT
Definition: testoverlay2.c:31
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_WINDOWEVENT
Definition: SDL_events.h:89
fpsdelay
static int fpsdelay
Definition: testoverlay2.c:150
paused
int paused
Definition: testoverlay2.c:147
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_WindowEvent::event
Uint8 event
Definition: SDL_events.h:179
SDL_KeyboardEvent::keysym
SDL_Keysym keysym
Definition: SDL_events.h:199
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
NULL
#define NULL
Definition: begin_code.h:164
SDL_MOUSEMOTION
Definition: SDL_events.h:102
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
SDL_Event::key
SDL_KeyboardEvent key
Definition: SDL_events.h:530
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_MouseMotionEvent::state
Uint32 state
Definition: SDL_events.h:238
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
window_h
int window_h
Definition: testoverlay2.c:144
i
int i
Definition: testoverlay2.c:148
SDL_Event
General event structure.
Definition: SDL_events.h:525
displayrect
SDL_Rect displayrect
Definition: testoverlay2.c:142
SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:155
SDLK_ESCAPE
Definition: SDL_keycode.h:55
SDL_Rect::y
int y
Definition: SDL_rect.h:66
window_w
int window_w
Definition: testoverlay2.c:143
SDLK_SPACE
Definition: SDL_keycode.h:58
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_Event::type
Uint32 type
Definition: SDL_events.h:527

## main()

int main | ( | int | argc,
|  | char ** | argv
| ) |  |

Definition at line 236 of file testoverlay2.c.

References SDL_Color::b, ConvertRGBtoYUV(), done, fpsdelay, frame, free, SDL_Color::g, SDL_Rect::h, i, j, loop(), malloc, MooseFrame, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT, MOOSEPIC_H, MOOSEPIC_W, NULL, PrintUsage(), quit(), SDL_Color::r, SDL_atoi, SDL_CreateRenderer, SDL_CreateTexture, SDL_CreateWindow, SDL_DestroyRenderer, SDL_EventState, SDL_GetError, SDL_GetYUVConversionModeForResolution, SDL_IGNORE, SDL_Init, SDL_INIT_VIDEO, SDL_KEYUP, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PIXELFORMAT_YV12, SDL_RWclose, SDL_RWFromFile, SDL_RWread, SDL_TEXTUREACCESS_STREAMING, SDL_WINDOW_RESIZABLE, SDL_WINDOWPOS_UNDEFINED, SDL_Rect::w, window, window_h, window_w, SDL_Rect::x, and SDL_Rect::y.

{
Uint8 *RawMooseData;
SDL_RWops *handle;
SDL_Window *window;
int j;
int fps = 12;
int nodelay = 0;
int scale = 5;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 3;
}
while (argc > 1) {
if (strcmp(argv[1], "-fps") == 0) {
if (argv[2]) {
fps = SDL_atoi(argv[2]);
if (fps == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"The -fps option requires an argument [from 1 to 1000], default is 12.\n");
quit(10);
}
if ((fps < 0) || (fps > 1000)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"The -fps option must be in range from 1 to 1000, default is 12.\n");
quit(10);
}
argv += 2;
argc -= 2;
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"The -fps option requires an argument [from 1 to 1000], default is 12.\n");
quit(10);
}
} else if (strcmp(argv[1], "-nodelay") == 0) {
nodelay = 1;
argv += 1;
argc -= 1;
} else if (strcmp(argv[1], "-scale") == 0) {
if (argv[2]) {
scale = SDL_atoi(argv[2]);
if (scale == 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"The -scale option requires an argument [from 1 to 50], default is 5.\n");
quit(10);
}
if ((scale < 0) || (scale > 50)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"The -scale option must be in range from 1 to 50, default is 5.\n");
quit(10);
}
argv += 2;
argc -= 2;
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"The -fps option requires an argument [from 1 to 1000], default is 12.\n");
quit(10);
}
} else if ((strcmp(argv[1], "-help") == 0)
|| (strcmp(argv[1], "-h") == 0)) {
PrintUsage(argv[0]);
quit(0);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unrecognized option: %s.\n", argv[1]);
quit(10);
}
break;
}
RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
if (RawMooseData == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
quit(1);
}
/* load the trojan moose images */
handle = SDL_RWFromFile("moose.dat", "rb");
if (handle == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
free(RawMooseData);
quit(2);
}
SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
SDL_RWclose(handle);
/* Create the window and renderer */
window_w = MOOSEPIC_W * scale;
window_h = MOOSEPIC_H * scale;
window = SDL_CreateWindow("Happy Moose",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
window_w, window_h,
SDL_WINDOW_RESIZABLE);
if (!window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
free(RawMooseData);
quit(4);
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
free(RawMooseData);
quit(4);
}
MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
if (!MooseTexture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
free(RawMooseData);
quit(5);
}
/* Uncomment this to check vertex color with a YUV texture */
/* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
Uint8 *rgb;
Uint8 *frame;
rgb = MooseFrameRGB;
frame = RawMooseData + i * MOOSEFRAME_SIZE;
for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
rgb[0] = MooseColors[frame[j]].r;
rgb[1] = MooseColors[frame[j]].g;
rgb[2] = MooseColors[frame[j]].b;
rgb += 3;
}
ConvertRGBtoYUV(SDL_PIXELFORMAT_YV12, MooseFrameRGB, MOOSEPIC_W*3, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H,
SDL_GetYUVConversionModeForResolution(MOOSEPIC_W, MOOSEPIC_H),
0, 100);
}
free(RawMooseData);
/* set the start frame */
i = 0;
if (nodelay) {
fpsdelay = 0;
} else {
fpsdelay = 1000 / fps;
}
displayrect.x = 0;
displayrect.y = 0;
displayrect.w = window_w;
displayrect.h = window_h;
/* Ignore key up events, they don't even get filtered */
SDL_EventState(SDL_KEYUP, SDL_IGNORE);
/* Loop, waiting for QUIT or RESIZE */
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
#else
while (!done) {
loop();
}
#endif
SDL_DestroyRenderer(renderer);
quit(0);
return 0;
}
SDL_CreateTexture
#define SDL_CreateTexture
Definition: SDL_dynapi_overrides.h:306
scale
GLenum GLenum GLenum GLenum GLenum scale
Definition: SDL_opengl_glext.h:9375
MOOSEPIC_W
#define MOOSEPIC_W
Definition: testoverlay2.c:27
loop
void loop()
Definition: testoverlay2.c:178
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_Color::g
Uint8 g
Definition: SDL_pixels.h:298
quit
static void quit(int rc)
Definition: testoverlay2.c:154
SDL_RWops
Definition: SDL_rwops.h:52
MOOSEFRAME_SIZE
#define MOOSEFRAME_SIZE
Definition: testoverlay2.c:30
free
SDL_EventEntry * free
Definition: SDL_events.c:84
MooseTexture
SDL_Texture * MooseTexture
Definition: testoverlay2.c:141
done
SDL_bool done
Definition: testoverlay2.c:149
SDL_RWread
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
SDL_Color::b
Uint8 b
Definition: SDL_pixels.h:299
SDL_CreateWindow
#define SDL_CreateWindow
Definition: SDL_dynapi_overrides.h:514
SDL_WINDOWPOS_UNDEFINED
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:130
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
MOOSEPIC_H
#define MOOSEPIC_H
Definition: testoverlay2.c:28
MooseFrame
Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]
Definition: testoverlay2.c:140
SDL_RWFromFile
#define SDL_RWFromFile
Definition: SDL_dynapi_overrides.h:351
renderer
SDL_Renderer * renderer
Definition: testoverlay2.c:146
MOOSEFRAMES_COUNT
#define MOOSEFRAMES_COUNT
Definition: testoverlay2.c:31
SDL_Color::r
Uint8 r
Definition: SDL_pixels.h:297
handle
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
frame
int frame
Definition: teststreaming.c:60
SDL_KEYUP
Definition: SDL_events.h:94
j
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
SDL_TEXTUREACCESS_STREAMING
Definition: SDL_render.h:94
fpsdelay
static int fpsdelay
Definition: testoverlay2.c:150
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:410
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
NULL
#define NULL
Definition: begin_code.h:164
SDL_WINDOW_RESIZABLE
Definition: SDL_video.h:105
SDL_RWclose
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
SDL_PIXELFORMAT_YV12
Definition: SDL_pixels.h:277
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_EventState
#define SDL_EventState
Definition: SDL_dynapi_overrides.h:131
window
SDL_Window * window
Definition: testoverlay2.c:145
window_h
int window_h
Definition: testoverlay2.c:144
i
int i
Definition: testoverlay2.c:148
MooseColors
SDL_Color MooseColors[84]
Definition: testoverlay2.c:33
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
displayrect
SDL_Rect displayrect
Definition: testoverlay2.c:142
SDL_DestroyRenderer
#define SDL_DestroyRenderer
Definition: SDL_dynapi_overrides.h:348
SDL_GetYUVConversionModeForResolution
#define SDL_GetYUVConversionModeForResolution
Definition: SDL_dynapi_overrides.h:665
PrintUsage
static void PrintUsage(char *argv0)
Definition: testoverlay2.c:161
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_Rect::y
int y
Definition: SDL_rect.h:66
window_w
int window_w
Definition: testoverlay2.c:143
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:78
SDL_CreateRenderer
#define SDL_CreateRenderer
Definition: SDL_dynapi_overrides.h:301
malloc
#define malloc
Definition: SDL_qsort.c:47
SDL_IGNORE
#define SDL_IGNORE
Definition: SDL_events.h:720
ConvertRGBtoYUV
SDL_bool ConvertRGBtoYUV(Uint32 format, Uint8 *src, int pitch, Uint8 *out, int w, int h, SDL_YUV_CONVERSION_MODE mode, int monochrome, int luminance)
Definition: testyuv_cvt.c:262

## PrintUsage()

static void PrintUsage | ( | char * | argv0 | ) |

static

Definition at line 161 of file testoverlay2.c.

References SDL_Log.

Referenced by main().

{
SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
SDL_Log("\n");
SDL_Log("Where 'arg' is any of the following options:\n");
SDL_Log("\n");
SDL_Log(" -fps <frames per second>\n");
SDL_Log(" -nodelay\n");
SDL_Log(" -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
SDL_Log(" -scale <scale factor> (initial scale of the overlay)\n");
SDL_Log(" -help (shows this help)\n");
SDL_Log("\n");
SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
SDL_Log("\n");
}
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31

## quit()

static void quit | ( | int | rc | ) |

static

Definition at line 154 of file testoverlay2.c.

References SDL_Quit.

Referenced by main().

{
SDL_Quit();
exit(rc);
}
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58

## Variable Documentation

## displayrect

SDL_Rect displayrect

Definition at line 142 of file testoverlay2.c.

## done

SDL_bool done = SDL_FALSE

Definition at line 149 of file testoverlay2.c.

Referenced by loop(), and main().

## fpsdelay

int fpsdelay

static

Definition at line 150 of file testoverlay2.c.

Referenced by loop(), and main().

## i

int i

Definition at line 148 of file testoverlay2.c.

Referenced by loop(), and main().

## MooseColors

SDL_Color MooseColors[84]

Definition at line 33 of file testoverlay2.c.

## MooseFrame

Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE *2]

Definition at line 140 of file testoverlay2.c.

Referenced by loop(), and main().

## MooseTexture

SDL_Texture* MooseTexture

Definition at line 141 of file testoverlay2.c.

## paused

int paused = 0

Definition at line 147 of file testoverlay2.c.

Referenced by loop().

## renderer

SDL_Renderer* renderer

Definition at line 146 of file testoverlay2.c.

## window

SDL_Window* window

Definition at line 145 of file testoverlay2.c.

Referenced by main().

## window_h

int window_h

Definition at line 144 of file testoverlay2.c.

Referenced by loop(), main(), MoveSprites(), and SDL_CreateRenderer().

## window_w

int window_w

Definition at line 143 of file testoverlay2.c.

Referenced by loop(), main(), MoveSprites(), and SDL_CreateRenderer().