static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep) { const char *textstart = text; uintptr_t value = 0; if ( radix == 16 && SDL_strncmp(text, "0x", 2) == 0 ) { text += 2; } for ( ; ; ) { int v; if ( SDL_isdigit((unsigned char) *text) ) { v = *text - '0'; } else if ( radix == 16 && SDL_isupperhex(*text) ) { v = 10 + (*text - 'A'); } else if ( radix == 16 && SDL_islowerhex(*text) ) { v = 10 + (*text - 'a'); } else { break; } value *= radix; value += v; ++text; } if ( valuep ) { *valuep = value; } return (text - textstart); }
/* read a non-negative integer from the source. return -1 upon error */ static int ReadNumber(SDL_RWops *src) { int number; unsigned char ch; /* Initialize return value */ number = 0; /* Skip leading whitespace */ do { if ( ! SDL_RWread(src, &ch, 1, 1) ) { return(0); } /* Eat comments as whitespace */ if ( ch == '#' ) { /* Comment is '#' to end of line */ do { if ( ! SDL_RWread(src, &ch, 1, 1) ) { return -1; } } while ( (ch != '\r') && (ch != '\n') ); } } while ( SDL_isspace(ch) ); /* Add up the number */ do { number *= 10; number += ch-'0'; if ( !SDL_RWread(src, &ch, 1, 1) ) { return -1; } } while ( SDL_isdigit(ch) ); return(number); }
static size_t SDL_ScanFloat(const char *text, double *valuep) { const char *textstart = text; unsigned long lvalue = 0; double value = 0.0; SDL_bool negative = SDL_FALSE; if ( *text == '-' ) { negative = SDL_TRUE; ++text; } text += SDL_ScanUnsignedLong(text, 10, &lvalue); value += lvalue; if ( *text == '.' ) { int mult = 10; ++text; while ( SDL_isdigit((unsigned char) *text) ) { lvalue = *text - '0'; value += (double)lvalue / mult; mult *= 10; ++text; } } if ( valuep ) { if ( negative && value ) { *valuep = -value; } else { *valuep = value; } } return (text - textstart); }
static size_t SDL_ScanLong(const char *text, int radix, long *valuep) { const char *textstart = text; long value = 0; SDL_bool negative = SDL_FALSE; if (*text == '-') { negative = SDL_TRUE; ++text; } if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) { text += 2; } for (;;) { int v; if (SDL_isdigit((unsigned char) *text)) { v = *text - '0'; } else if (radix == 16 && SDL_isupperhex(*text)) { v = 10 + (*text - 'A'); } else if (radix == 16 && SDL_islowerhex(*text)) { v = 10 + (*text - 'a'); } else { break; } value *= radix; value += v; ++text; } if (valuep) { if (negative && value) { *valuep = -value; } else { *valuep = value; } } return (text - textstart); }
/** * @brief The entry point of this force feedback demo. * @param[in] argc Number of arguments. * @param[in] argv Array of argc arguments. */ int main(int argc, char **argv) { int i; char *name; int index; /* Enable standard application logging */ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); name = NULL; index = -1; if (argc > 1) { size_t l; name = argv[1]; if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) { SDL_Log("USAGE: %s [device]\n" "If device is a two-digit number it'll use it as an index, otherwise\n" "it'll use it as if it were part of the device's name.\n", argv[0]); return 0; } l = SDL_strlen(name); if ((l < 3) && SDL_isdigit(name[0]) && ((l == 1) || SDL_isdigit(name[1]))) { index = SDL_atoi(name); name = NULL; } } /* Initialize the force feedbackness */ SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC); SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics()); if (SDL_NumHaptics() > 0) { /* We'll just use index or the first force feedback device found */ if (name == NULL) { i = (index != -1) ? index : 0; } /* Try to find matching device */ else { for (i = 0; i < SDL_NumHaptics(); i++) { if (strstr(SDL_HapticName(i), name) != NULL) break; } if (i >= SDL_NumHaptics()) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n", name); return 1; } } haptic = SDL_HapticOpen(i); if (haptic == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n", SDL_GetError()); return 1; } SDL_Log("Device: %s\n", SDL_HapticName(i)); } else { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n"); return 1; } /* We only want force feedback errors. */ SDL_ClearError(); if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!\n"); return 1; } if (SDL_HapticRumbleInit(haptic) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s\n", SDL_GetError()); return 1; } SDL_Log("Playing 2 second rumble at 0.5 magnitude.\n"); if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() ); return 1; } SDL_Delay(2000); SDL_Log("Stopping rumble.\n"); SDL_HapticRumbleStop(haptic); SDL_Delay(2000); SDL_Log("Playing 2 second rumble at 0.3 magnitude.\n"); if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() ); return 1; } SDL_Delay(2000); /* Quit */ if (haptic != NULL) SDL_HapticClose(haptic); SDL_Quit(); return 0; }
int main(int argc, char *argv[]) { int i; Uint32 then, now, frames; /* Enable standard application logging */ SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); /* Initialize parameters */ num_objects = NUM_OBJECTS; /* Initialize test framework */ state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } for (i = 1; i < argc;) { int consumed; consumed = SDLTest_CommonArg(state, i); if (consumed == 0) { consumed = -1; if (SDL_strcasecmp(argv[i], "--blend") == 0) { if (argv[i + 1]) { if (SDL_strcasecmp(argv[i + 1], "none") == 0) { blendMode = SDL_BLENDMODE_NONE; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { blendMode = SDL_BLENDMODE_BLEND; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { blendMode = SDL_BLENDMODE_ADD; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { blendMode = SDL_BLENDMODE_MOD; consumed = 2; } } } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { cycle_color = SDL_TRUE; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { cycle_alpha = SDL_TRUE; consumed = 1; } else if (SDL_isdigit(*argv[i])) { num_objects = SDL_atoi(argv[i]); consumed = 1; } } if (consumed < 0) { SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n", argv[0], SDLTest_CommonUsage(state)); return 1; } i += consumed; } if (!SDLTest_CommonInit(state)) { return 2; } /* Create the windows and initialize the renderers */ for (i = 0; i < state->num_windows; ++i) { SDL_Renderer *renderer = state->renderers[i]; SDL_SetRenderDrawBlendMode(renderer, blendMode); SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(renderer); } srand((unsigned int)time(NULL)); /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(loop, 0, 1); #else while (!done) { ++frames; loop(); } #endif SDLTest_CommonQuit(state); /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { double fps = ((double) frames * 1000) / (now - then); SDL_Log("%2.2f frames per second\n", fps); } return 0; }
int SDLTest_CommonArg(SDLTest_CommonState * state, int index) { char **argv = state->argv; if (SDL_strcasecmp(argv[index], "--video") == 0) { ++index; if (!argv[index]) { return -1; } state->videodriver = argv[index]; return 2; } if (SDL_strcasecmp(argv[index], "--renderer") == 0) { ++index; if (!argv[index]) { return -1; } state->renderdriver = argv[index]; return 2; } if (SDL_strcasecmp(argv[index], "--gldebug") == 0) { state->gl_debug = 1; return 1; } if (SDL_strcasecmp(argv[index], "--info") == 0) { ++index; if (!argv[index]) { return -1; } if (SDL_strcasecmp(argv[index], "all") == 0) { state->verbose |= (VERBOSE_VIDEO | VERBOSE_MODES | VERBOSE_RENDER | VERBOSE_EVENT); return 2; } if (SDL_strcasecmp(argv[index], "video") == 0) { state->verbose |= VERBOSE_VIDEO; return 2; } if (SDL_strcasecmp(argv[index], "modes") == 0) { state->verbose |= VERBOSE_MODES; return 2; } if (SDL_strcasecmp(argv[index], "render") == 0) { state->verbose |= VERBOSE_RENDER; return 2; } if (SDL_strcasecmp(argv[index], "event") == 0) { state->verbose |= VERBOSE_EVENT; return 2; } return -1; } if (SDL_strcasecmp(argv[index], "--log") == 0) { ++index; if (!argv[index]) { return -1; } if (SDL_strcasecmp(argv[index], "all") == 0) { SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE); return 2; } if (SDL_strcasecmp(argv[index], "error") == 0) { SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_VERBOSE); return 2; } if (SDL_strcasecmp(argv[index], "system") == 0) { SDL_LogSetPriority(SDL_LOG_CATEGORY_SYSTEM, SDL_LOG_PRIORITY_VERBOSE); return 2; } if (SDL_strcasecmp(argv[index], "audio") == 0) { SDL_LogSetPriority(SDL_LOG_CATEGORY_AUDIO, SDL_LOG_PRIORITY_VERBOSE); return 2; } if (SDL_strcasecmp(argv[index], "video") == 0) { SDL_LogSetPriority(SDL_LOG_CATEGORY_VIDEO, SDL_LOG_PRIORITY_VERBOSE); return 2; } if (SDL_strcasecmp(argv[index], "render") == 0) { SDL_LogSetPriority(SDL_LOG_CATEGORY_RENDER, SDL_LOG_PRIORITY_VERBOSE); return 2; } if (SDL_strcasecmp(argv[index], "input") == 0) { SDL_LogSetPriority(SDL_LOG_CATEGORY_INPUT, SDL_LOG_PRIORITY_VERBOSE); return 2; } return -1; } if (SDL_strcasecmp(argv[index], "--display") == 0) { ++index; if (!argv[index]) { return -1; } state->display = SDL_atoi(argv[index]); if (SDL_WINDOWPOS_ISUNDEFINED(state->window_x)) { state->window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display); state->window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(state->display); } if (SDL_WINDOWPOS_ISCENTERED(state->window_x)) { state->window_x = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display); state->window_y = SDL_WINDOWPOS_CENTERED_DISPLAY(state->display); } return 2; } if (SDL_strcasecmp(argv[index], "--fullscreen") == 0) { state->window_flags |= SDL_WINDOW_FULLSCREEN; state->num_windows = 1; return 1; } if (SDL_strcasecmp(argv[index], "--fullscreen-desktop") == 0) { state->window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; state->num_windows = 1; return 1; } if (SDL_strcasecmp(argv[index], "--windows") == 0) { ++index; if (!argv[index] || !SDL_isdigit(*argv[index])) { return -1; } if (!(state->window_flags & SDL_WINDOW_FULLSCREEN)) { state->num_windows = SDL_atoi(argv[index]); } return 2; } if (SDL_strcasecmp(argv[index], "--title") == 0) { ++index; if (!argv[index]) { return -1; } state->window_title = argv[index]; return 2; } if (SDL_strcasecmp(argv[index], "--icon") == 0) { ++index; if (!argv[index]) { return -1; } state->window_icon = argv[index]; return 2; } if (SDL_strcasecmp(argv[index], "--center") == 0) { state->window_x = SDL_WINDOWPOS_CENTERED; state->window_y = SDL_WINDOWPOS_CENTERED; return 1; } if (SDL_strcasecmp(argv[index], "--position") == 0) { char *x, *y; ++index; if (!argv[index]) { return -1; } x = argv[index]; y = argv[index]; while (*y && *y != ',') { ++y; } if (!*y) { return -1; } *y++ = '\0'; state->window_x = SDL_atoi(x); state->window_y = SDL_atoi(y); return 2; } if (SDL_strcasecmp(argv[index], "--geometry") == 0) { char *w, *h; ++index; if (!argv[index]) { return -1; } w = argv[index]; h = argv[index]; while (*h && *h != 'x') { ++h; } if (!*h) { return -1; } *h++ = '\0'; state->window_w = SDL_atoi(w); state->window_h = SDL_atoi(h); return 2; } if (SDL_strcasecmp(argv[index], "--min-geometry") == 0) { char *w, *h; ++index; if (!argv[index]) { return -1; } w = argv[index]; h = argv[index]; while (*h && *h != 'x') { ++h; } if (!*h) { return -1; } *h++ = '\0'; state->window_minW = SDL_atoi(w); state->window_minH = SDL_atoi(h); return 2; } if (SDL_strcasecmp(argv[index], "--max-geometry") == 0) { char *w, *h; ++index; if (!argv[index]) { return -1; } w = argv[index]; h = argv[index]; while (*h && *h != 'x') { ++h; } if (!*h) { return -1; } *h++ = '\0'; state->window_maxW = SDL_atoi(w); state->window_maxH = SDL_atoi(h); return 2; } if (SDL_strcasecmp(argv[index], "--logical") == 0) { char *w, *h; ++index; if (!argv[index]) { return -1; } w = argv[index]; h = argv[index]; while (*h && *h != 'x') { ++h; } if (!*h) { return -1; } *h++ = '\0'; state->logical_w = SDL_atoi(w); state->logical_h = SDL_atoi(h); return 2; } if (SDL_strcasecmp(argv[index], "--scale") == 0) { ++index; if (!argv[index]) { return -1; } state->scale = (float)SDL_atof(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--depth") == 0) { ++index; if (!argv[index]) { return -1; } state->depth = SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--refresh") == 0) { ++index; if (!argv[index]) { return -1; } state->refresh_rate = SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--vsync") == 0) { state->render_flags |= SDL_RENDERER_PRESENTVSYNC; return 1; } if (SDL_strcasecmp(argv[index], "--noframe") == 0) { state->window_flags |= SDL_WINDOW_BORDERLESS; return 1; } if (SDL_strcasecmp(argv[index], "--resize") == 0) { state->window_flags |= SDL_WINDOW_RESIZABLE; return 1; } if (SDL_strcasecmp(argv[index], "--minimize") == 0) { state->window_flags |= SDL_WINDOW_MINIMIZED; return 1; } if (SDL_strcasecmp(argv[index], "--maximize") == 0) { state->window_flags |= SDL_WINDOW_MAXIMIZED; return 1; } if (SDL_strcasecmp(argv[index], "--grab") == 0) { state->window_flags |= SDL_WINDOW_INPUT_GRABBED; return 1; } if (SDL_strcasecmp(argv[index], "--rate") == 0) { ++index; if (!argv[index]) { return -1; } state->audiospec.freq = SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--format") == 0) { ++index; if (!argv[index]) { return -1; } if (SDL_strcasecmp(argv[index], "U8") == 0) { state->audiospec.format = AUDIO_U8; return 2; } if (SDL_strcasecmp(argv[index], "S8") == 0) { state->audiospec.format = AUDIO_S8; return 2; } if (SDL_strcasecmp(argv[index], "U16") == 0) { state->audiospec.format = AUDIO_U16; return 2; } if (SDL_strcasecmp(argv[index], "U16LE") == 0) { state->audiospec.format = AUDIO_U16LSB; return 2; } if (SDL_strcasecmp(argv[index], "U16BE") == 0) { state->audiospec.format = AUDIO_U16MSB; return 2; } if (SDL_strcasecmp(argv[index], "S16") == 0) { state->audiospec.format = AUDIO_S16; return 2; } if (SDL_strcasecmp(argv[index], "S16LE") == 0) { state->audiospec.format = AUDIO_S16LSB; return 2; } if (SDL_strcasecmp(argv[index], "S16BE") == 0) { state->audiospec.format = AUDIO_S16MSB; return 2; } return -1; } if (SDL_strcasecmp(argv[index], "--channels") == 0) { ++index; if (!argv[index]) { return -1; } state->audiospec.channels = (Uint8) SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--samples") == 0) { ++index; if (!argv[index]) { return -1; } state->audiospec.samples = (Uint16) SDL_atoi(argv[index]); return 2; } if ((SDL_strcasecmp(argv[index], "-h") == 0) || (SDL_strcasecmp(argv[index], "--help") == 0)) { /* Print the usage message */ return -1; } if (SDL_strcmp(argv[index], "-NSDocumentRevisionsDebugMode") == 0) { /* Debug flag sent by Xcode */ return 2; } return 0; }
int main(int argc, char *argv[]) { int i; Uint32 then, now, frames; Uint64 seed; const char *icon = "icon.bmp"; /* Initialize parameters */ num_sprites = NUM_SPRITES; /* Initialize test framework */ state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } for (i = 1; i < argc;) { int consumed; consumed = SDLTest_CommonArg(state, i); if (consumed == 0) { consumed = -1; if (SDL_strcasecmp(argv[i], "--blend") == 0) { if (argv[i + 1]) { if (SDL_strcasecmp(argv[i + 1], "none") == 0) { blendMode = SDL_BLENDMODE_NONE; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { blendMode = SDL_BLENDMODE_BLEND; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { blendMode = SDL_BLENDMODE_ADD; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { blendMode = SDL_BLENDMODE_MOD; consumed = 2; } } } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) { if (argv[i + 1]) { iterations = SDL_atoi(argv[i + 1]); if (iterations < -1) iterations = -1; consumed = 2; } } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { cycle_color = SDL_TRUE; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { cycle_alpha = SDL_TRUE; consumed = 1; } else if (SDL_isdigit(*argv[i])) { num_sprites = SDL_atoi(argv[i]); consumed = 1; } else if (argv[i][0] != '-') { icon = argv[i]; consumed = 1; } } if (consumed < 0) { SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha] [--iterations N] [num_sprites] [icon.bmp]\n", argv[0], SDLTest_CommonUsage(state)); quit(1); } i += consumed; } if (!SDLTest_CommonInit(state)) { quit(2); } /* Create the windows, initialize the renderers, and load the textures */ sprites = (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites)); if (!sprites) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n"); quit(2); } for (i = 0; i < state->num_windows; ++i) { SDL_Renderer *renderer = state->renderers[i]; SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(renderer); } if (LoadSprite(icon) < 0) { quit(2); } /* Allocate memory for the sprite info */ positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect)); velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect)); if (!positions || !velocities) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n"); quit(2); } /* Position sprites and set their velocities using the fuzzer */ if (iterations >= 0) { /* Deterministic seed - used for visual tests */ seed = (Uint64)iterations; } else { /* Pseudo-random seed generated from the time */ seed = (Uint64)time(NULL); } SDLTest_FuzzerInit(seed); for (i = 0; i < num_sprites; ++i) { positions[i].x = SDLTest_RandomIntegerInRange(0, state->window_w - sprite_w); positions[i].y = SDLTest_RandomIntegerInRange(0, state->window_h - sprite_h); positions[i].w = sprite_w; positions[i].h = sprite_h; velocities[i].x = 0; velocities[i].y = 0; while (!velocities[i].x && !velocities[i].y) { velocities[i].x = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED); velocities[i].y = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED); } } /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; #ifdef __EMSCRIPTEN__ emscripten_set_main_loop(loop, 0, 1); #else while (!done) { ++frames; loop(); } #endif /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { double fps = ((double) frames * 1000) / (now - then); SDL_Log("%2.2f frames per second\n", fps); } quit(0); return 0; }
int CommonArg(CommonState * state, int index) { char **argv = state->argv; if (SDL_strcasecmp(argv[index], "--video") == 0) { ++index; if (!argv[index]) { return -1; } state->videodriver = argv[index]; return 2; } if (SDL_strcasecmp(argv[index], "--renderer") == 0) { ++index; if (!argv[index]) { return -1; } state->renderdriver = argv[index]; return 2; } if (SDL_strcasecmp(argv[index], "--info") == 0) { ++index; if (!argv[index]) { return -1; } if (SDL_strcasecmp(argv[index], "all") == 0) { state->verbose |= (VERBOSE_VIDEO | VERBOSE_MODES | VERBOSE_RENDER | VERBOSE_EVENT); return 2; } if (SDL_strcasecmp(argv[index], "video") == 0) { state->verbose |= VERBOSE_VIDEO; return 2; } if (SDL_strcasecmp(argv[index], "modes") == 0) { state->verbose |= VERBOSE_MODES; return 2; } if (SDL_strcasecmp(argv[index], "render") == 0) { state->verbose |= VERBOSE_RENDER; return 2; } if (SDL_strcasecmp(argv[index], "event") == 0) { state->verbose |= VERBOSE_EVENT; return 2; } return -1; } if (SDL_strcasecmp(argv[index], "--display") == 0) { ++index; if (!argv[index]) { return -1; } state->display = SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--fullscreen") == 0) { state->window_flags |= SDL_WINDOW_FULLSCREEN; state->num_windows = 1; return 1; } if (SDL_strcasecmp(argv[index], "--windows") == 0) { ++index; if (!argv[index] || !SDL_isdigit(*argv[index])) { return -1; } if (!(state->window_flags & SDL_WINDOW_FULLSCREEN)) { state->num_windows = SDL_atoi(argv[index]); } return 2; } if (SDL_strcasecmp(argv[index], "--title") == 0) { ++index; if (!argv[index]) { return -1; } state->window_title = argv[index]; return 2; } if (SDL_strcasecmp(argv[index], "--center") == 0) { state->window_x = SDL_WINDOWPOS_CENTERED; state->window_y = SDL_WINDOWPOS_CENTERED; return 1; } if (SDL_strcasecmp(argv[index], "--position") == 0) { char *x, *y; ++index; if (!argv[index]) { return -1; } x = argv[index]; y = argv[index]; while (*y && *y != ',') { ++y; } if (!*y) { return -1; } *y++ = '\0'; state->window_x = SDL_atoi(x); state->window_y = SDL_atoi(y); return 2; } if (SDL_strcasecmp(argv[index], "--geometry") == 0) { char *w, *h; ++index; if (!argv[index]) { return -1; } w = argv[index]; h = argv[index]; while (*h && *h != 'x') { ++h; } if (!*h) { return -1; } *h++ = '\0'; state->window_w = SDL_atoi(w); state->window_h = SDL_atoi(h); return 2; } if (SDL_strcasecmp(argv[index], "--depth") == 0) { ++index; if (!argv[index]) { return -1; } state->depth = SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--refresh") == 0) { ++index; if (!argv[index]) { return -1; } state->refresh_rate = SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--vsync") == 0) { state->render_flags |= SDL_RENDERER_PRESENTVSYNC; return 1; } if (SDL_strcasecmp(argv[index], "--noframe") == 0) { state->window_flags |= SDL_WINDOW_BORDERLESS; return 1; } if (SDL_strcasecmp(argv[index], "--resize") == 0) { state->window_flags |= SDL_WINDOW_RESIZABLE; return 1; } if (SDL_strcasecmp(argv[index], "--minimize") == 0) { state->window_flags |= SDL_WINDOW_MINIMIZED; return 1; } if (SDL_strcasecmp(argv[index], "--maximize") == 0) { state->window_flags |= SDL_WINDOW_MAXIMIZED; return 1; } if (SDL_strcasecmp(argv[index], "--grab") == 0) { state->window_flags |= SDL_WINDOW_INPUT_GRABBED; return 1; } if (SDL_strcasecmp(argv[index], "--rate") == 0) { ++index; if (!argv[index]) { return -1; } state->audiospec.freq = SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--format") == 0) { ++index; if (!argv[index]) { return -1; } if (SDL_strcasecmp(argv[index], "U8") == 0) { state->audiospec.format = AUDIO_U8; return 2; } if (SDL_strcasecmp(argv[index], "S8") == 0) { state->audiospec.format = AUDIO_S8; return 2; } if (SDL_strcasecmp(argv[index], "U16") == 0) { state->audiospec.format = AUDIO_U16; return 2; } if (SDL_strcasecmp(argv[index], "U16LE") == 0) { state->audiospec.format = AUDIO_U16LSB; return 2; } if (SDL_strcasecmp(argv[index], "U16BE") == 0) { state->audiospec.format = AUDIO_U16MSB; return 2; } if (SDL_strcasecmp(argv[index], "S16") == 0) { state->audiospec.format = AUDIO_S16; return 2; } if (SDL_strcasecmp(argv[index], "S16LE") == 0) { state->audiospec.format = AUDIO_S16LSB; return 2; } if (SDL_strcasecmp(argv[index], "S16BE") == 0) { state->audiospec.format = AUDIO_S16MSB; return 2; } return -1; } if (SDL_strcasecmp(argv[index], "--channels") == 0) { ++index; if (!argv[index]) { return -1; } state->audiospec.channels = (Uint8) SDL_atoi(argv[index]); return 2; } if (SDL_strcasecmp(argv[index], "--samples") == 0) { ++index; if (!argv[index]) { return -1; } state->audiospec.samples = (Uint16) SDL_atoi(argv[index]); return 2; } if ((SDL_strcasecmp(argv[index], "-h") == 0) || (SDL_strcasecmp(argv[index], "--help") == 0)) { /* Print the usage message */ return -1; } return 0; }
int main(int argc, char *argv[]) { int i, done; SDL_Event event; Uint32 then, now, frames; /* Initialize parameters */ num_sprites = NUM_SPRITES; /* Initialize test framework */ state = CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } for (i = 1; i < argc;) { int consumed; consumed = CommonArg(state, i); if (consumed == 0) { consumed = -1; if (SDL_strcasecmp(argv[i], "--blend") == 0) { if (argv[i + 1]) { if (SDL_strcasecmp(argv[i + 1], "none") == 0) { blendMode = SDL_BLENDMODE_NONE; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) { blendMode = SDL_BLENDMODE_MASK; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { blendMode = SDL_BLENDMODE_BLEND; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { blendMode = SDL_BLENDMODE_ADD; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { blendMode = SDL_BLENDMODE_MOD; consumed = 2; } } } else if (SDL_strcasecmp(argv[i], "--scale") == 0) { if (argv[i + 1]) { if (SDL_strcasecmp(argv[i + 1], "none") == 0) { scaleMode = SDL_TEXTURESCALEMODE_NONE; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) { scaleMode = SDL_TEXTURESCALEMODE_FAST; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) { scaleMode = SDL_TEXTURESCALEMODE_SLOW; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) { scaleMode = SDL_TEXTURESCALEMODE_BEST; consumed = 2; } } } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { cycle_color = SDL_TRUE; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { cycle_alpha = SDL_TRUE; consumed = 1; } else if (SDL_isdigit(*argv[i])) { num_sprites = SDL_atoi(argv[i]); consumed = 1; } } if (consumed < 0) { fprintf(stderr, "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n", argv[0], CommonUsage(state)); quit(1); } i += consumed; } if (!CommonInit(state)) { quit(2); } /* Create the windows, initialize the renderers, and load the textures */ sprites = (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites)); if (!sprites) { fprintf(stderr, "Out of memory!\n"); quit(2); } for (i = 0; i < state->num_windows; ++i) { SDL_SelectRenderer(state->windows[i]); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderRect(NULL); } if (LoadSprite("icon.bmp") < 0) { quit(2); } /* Allocate memory for the sprite info */ positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect)); velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect)); if (!positions || !velocities) { fprintf(stderr, "Out of memory!\n"); quit(2); } srand(time(NULL)); if (scaleMode != SDL_TEXTURESCALEMODE_NONE) { sprite_w += sprite_w / 2; sprite_h += sprite_h / 2; } for (i = 0; i < num_sprites; ++i) { positions[i].x = rand() % (state->window_w - sprite_w); positions[i].y = rand() % (state->window_h - sprite_h); positions[i].w = sprite_w; positions[i].h = sprite_h; velocities[i].x = 0; velocities[i].y = 0; while (!velocities[i].x && !velocities[i].y) { velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; } } /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; while (!done) { /* Check for events */ ++frames; while (SDL_PollEvent(&event)) { CommonEvent(state, &event, &done); switch (event.type) { case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_EXPOSED: SDL_SelectRenderer(event.window.windowID); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderRect(NULL); break; } break; default: break; } } for (i = 0; i < state->num_windows; ++i) { MoveSprites(state->windows[i], sprites[i]); } } /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { double fps = ((double) frames * 1000) / (now - then); printf("%2.2f frames per second\n", fps); } quit(0); }
int main(int argc, char *argv[]) { int i, done; SDL_Event event; Uint32 then, now, frames; /* Initialize parameters */ num_sprites = NUM_SPRITES; /* Initialize test framework */ state = CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } state->window_flags |= SDL_WINDOW_RESIZABLE; for (i = 1; i < argc;) { int consumed; consumed = CommonArg(state, i); if (consumed == 0) { consumed = -1; if (SDL_strcasecmp(argv[i], "--blend") == 0) { if (argv[i + 1]) { if (SDL_strcasecmp(argv[i + 1], "none") == 0) { blendMode = SDL_BLENDMODE_NONE; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { blendMode = SDL_BLENDMODE_BLEND; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { blendMode = SDL_BLENDMODE_ADD; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { blendMode = SDL_BLENDMODE_MOD; consumed = 2; } } } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { cycle_color = SDL_TRUE; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { cycle_alpha = SDL_TRUE; consumed = 1; } else if (SDL_isdigit(*argv[i])) { num_sprites = SDL_atoi(argv[i]); consumed = 1; } } if (consumed < 0) { fprintf(stderr, "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n", argv[0], CommonUsage(state)); quit(1); } i += consumed; } if (!CommonInit(state)) { quit(2); } /* Create the windows, initialize the renderers, and load the textures */ sprites = (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites)); if (!sprites) { fprintf(stderr, "Out of memory!\n"); quit(2); } for (i = 0; i < state->num_windows; ++i) { SDL_Renderer *renderer = state->renderers[i]; SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(renderer); } if (LoadSprite("icon.bmp") < 0) { quit(2); } /* Allocate memory for the sprite info */ positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect)); velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect)); if (!positions || !velocities) { fprintf(stderr, "Out of memory!\n"); quit(2); } srand((unsigned int)time(NULL)); for (i = 0; i < num_sprites; ++i) { positions[i].x = rand() % (state->window_w - sprite_w); positions[i].y = rand() % (state->window_h - sprite_h); positions[i].w = sprite_w; positions[i].h = sprite_h; velocities[i].x = 0; velocities[i].y = 0; while (!velocities[i].x && !velocities[i].y) { velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED; } } /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; while (!done) { /* Check for events */ ++frames; while (SDL_PollEvent(&event)) { CommonEvent(state, &event, &done); } for (i = 0; i < state->num_windows; ++i) { MoveSprites(state->renderers[i], sprites[i]); } } /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { double fps = ((double) frames * 1000) / (now - then); printf("%2.2f frames per second\n", fps); } quit(0); return 0; }
int main(int argc, char *argv[]) { int mouse_begin_x = -1, mouse_begin_y = -1; int i, done; SDL_Event event; Uint32 then, now, frames; /* Initialize parameters */ num_objects = NUM_OBJECTS; /* Initialize test framework */ state = CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } for (i = 1; i < argc;) { int consumed; consumed = CommonArg(state, i); if (consumed == 0) { consumed = -1; if (SDL_strcasecmp(argv[i], "--blend") == 0) { if (argv[i + 1]) { if (SDL_strcasecmp(argv[i + 1], "none") == 0) { blendMode = SDL_BLENDMODE_NONE; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) { blendMode = SDL_BLENDMODE_MASK; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { blendMode = SDL_BLENDMODE_BLEND; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { blendMode = SDL_BLENDMODE_ADD; consumed = 2; } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { blendMode = SDL_BLENDMODE_MOD; consumed = 2; } } } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { cycle_color = SDL_TRUE; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { cycle_alpha = SDL_TRUE; consumed = 1; } else if (SDL_isdigit(*argv[i])) { num_objects = SDL_atoi(argv[i]); consumed = 1; } } if (consumed < 0) { fprintf(stderr, "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n", argv[0], CommonUsage(state)); return 1; } i += consumed; } if (!CommonInit(state)) { return 2; } /* Create the windows and initialize the renderers */ for (i = 0; i < state->num_windows; ++i) { SDL_SelectRenderer(state->windows[i]); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(); } srand(time(NULL)); /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; while (!done) { /* Check for events */ ++frames; while (SDL_PollEvent(&event)) { CommonEvent(state, &event, &done); switch (event.type) { case SDL_MOUSEBUTTONDOWN: if (event.button.which == 0) { mouse_begin_x = event.button.x; mouse_begin_y = event.button.y; } break; case SDL_MOUSEBUTTONUP: if (event.button.which == 0) { if (event.button.button == 3) add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); if (event.button.button == 1) add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); } break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case 'l': if (event.key.keysym.mod & KMOD_SHIFT) num_lines = 0; else add_line(rand() % 640, rand() % 480, rand() % 640, rand() % 480); break; case 'r': if (event.key.keysym.mod & KMOD_SHIFT) num_rects = 0; else add_rect(rand() % 640, rand() % 480, rand() % 640, rand() % 480); break; } break; case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_EXPOSED: SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID)); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(); break; } break; default: break; } } for (i = 0; i < state->num_windows; ++i) { SDL_SelectRenderer(state->windows[i]); SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF); SDL_RenderClear(); DrawRects(state->windows[i]); DrawPoints(state->windows[i]); DrawRectRectIntersections(state->windows[i]); DrawLines(state->windows[i]); DrawRectLineIntersections(state->windows[i]); SDL_RenderPresent(); } } CommonQuit(state); /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { double fps = ((double) frames * 1000) / (now - then); printf("%2.2f frames per second\n", fps); } return 0; }