コード例 #1
0
ファイル: SdlDisplay.cpp プロジェクト: bogardt/mySnake
void		SdlDisplay::sdlLauncher(bool multi)
{
    sdlInit(multi);
    createApple();
    while (start)
    {
        sdlEvent(multi);
        if ((SDL_GetTicks() - startticks) >= 80)
        {
            if (multi)
                P2ON = true;
            Run();
        }
        SDL_BlitSurface(back, NULL, screen, NULL);
        sdlBlitApple();
        sdlBlitMushroom();
        sdlPlaySongWhenEat();
        sdlBlitPlayer1();
        sdlBlitPlayer2(multi);
        sdlChangeEvent(multi);
        SDL_Flip(screen);
    }
    if (!freeall)
        sdlClose();
}
コード例 #2
0
ファイル: scplay.cpp プロジェクト: Deltafire/SCPlayer
int main(int argc, char *argv[])
{
  SCPlayer player;

  if (argc < 2) {
    std::cerr << "Usage: scplay <filename>" << std::endl;
    exit(1);
  }

  if (!player.load(argv[1])) {
    std::cerr << "Cannot open file " << argv[1] << std::endl;
    exit(1);
  }

  if(sdlInit(&player) != 0) {
    std::cerr << "Failed to initialise audio:" << SDL_GetError() << std::endl;
    SDL_Quit();
    exit(1);
  }

  player.init(mixerFreq);
  std::cout << "Playing: " << argv[1] << std::endl;
  std::cout << "Hit the return key to exit." << std::endl;
  SDL_PauseAudio(0);
  getchar();
  SDL_PauseAudio(1);
  SDL_Quit();
  return 0;
}
コード例 #3
0
ファイル: jukebox.cpp プロジェクト: Benzopilot/libsdl-demos
extern "C" int SDL_main(int, char **)  // 2-arg form is required by SDL
{
    if (!sdlInit(250, 140, "../img/icon.png", "Music Test")) {
        return EXIT_FAILURE;
    }

    SdlSurface play = sdlLoadImage("../img/button-play.png");
    SdlSurface pause = sdlLoadImage("../img/button-pause.png");
    SdlSurface next = sdlLoadImage("../img/button-next.png");
    SdlSurface prev = sdlLoadImage("../img/button-prev.png");

    // Define the control buttons.
    GuiButton playButton{105, 90, play};
    GuiButton nextTrack{155, 90, next};
    GuiButton prevTrack{55, 90, prev};
    std::vector<GuiButton *> buttons = {&playButton, &nextTrack, &prevTrack};

    auto font = sdlLoadFont("../DejaVuSans.ttf", 14);
    auto white = SDL_Color{255, 255, 255, 0};
    sdlDrawText(font, "Now playing:", SDL_Rect{10, 10, 230, 20}, white);

    auto trackTitle = SDL_Rect{10, 30, 230, 50};
    sdlDrawText(font, "Nothing", trackTitle, white);

    SDL_UpdateRect(screen, 0, 0, 0, 0);

    auto musicFiles = getMusicFiles("../music");
    assert(!musicFiles.empty());

    int trackNum = 0;

    playButton.onClick([&] {
        if (!Mix_PlayingMusic()) {  // have we started playing music at all
            auto &track = musicFiles[trackNum];
            sdlPlayMusic(track.music);
            playButton.setImage(pause);
            sdlDrawText(font, track.path, trackTitle, white);
        }
        else {
            if (Mix_PausedMusic()) {
                playButton.setImage(pause);
                Mix_ResumeMusic();
            }
            else {
                playButton.setImage(play);
                Mix_PauseMusic();
            }
        }
    });

    nextTrack.onClick([&] {
        if (!Mix_PlayingMusic()) return;

        trackNum = (trackNum + 1) % musicFiles.size();
        auto &track = musicFiles[trackNum];
        sdlDrawText(font, track.path, trackTitle, white);
        if (Mix_PausedMusic()) {
            sdlPlayMusic(track.music);
            Mix_PauseMusic();
        }
        else {
            sdlPlayMusic(track.music);
        }
    });

    prevTrack.onClick([&] {
        if (!Mix_PlayingMusic()) return;

        trackNum = (trackNum - 1) % musicFiles.size();
        auto &track = musicFiles[trackNum];
        sdlDrawText(font, track.path, trackTitle, white);
        if (Mix_PausedMusic()) {
            sdlPlayMusic(track.music);
            Mix_PauseMusic();
        }
        else {
            sdlPlayMusic(track.music);
        }
    });

    bool isDone = false;
    SDL_Event event;
    while (!isDone) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_MOUSEBUTTONUP) {
                handleMouseUp(event.button, buttons);
            }
            else if (event.type == SDL_QUIT) {
                Mix_HaltMusic();
                isDone = true;
            }
        }

        SDL_UpdateRect(screen, 0, 0, 0, 0);
        SDL_Delay(1);
    }

    return EXIT_SUCCESS;
}
コード例 #4
0
ファイル: shapeme.c プロジェクト: antirez/shapeme
int main(int argc, char **argv)
{
    FILE *fp;
    int width, height, alpha;
    unsigned char *image, *fb;
    SDL_Surface *screen;
    struct triangles *triangles, *best, *absbest;
    long long diff;
    float percdiff, bestdiff;

    /* Initialization */
    srand(time(NULL));
    state.max_shapes = 64;
    state.max_shapes_incremental = 1;
    state.temperature = 0.10;
    state.generation = 0;
    state.absbestdiff = 100; /* 100% is worst diff possible. */

    /* Check arity and parse additional args if any. */
    if (argc < 4) {
        showHelp(argv[0]);
        exit(1);
    }

    if (argc > 4) {
        int j;
        for (j = 4; j < argc; j++) {
            int moreargs = j+1 < argc;

            if (!strcmp(argv[j],"--use-triangles") && moreargs) {
                opt_use_triangles = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--use-circles") && moreargs) {
                opt_use_circles = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--max-shapes") && moreargs) {
                state.max_shapes = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--initial-shapes") && moreargs) {
                state.max_shapes_incremental = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--mutation-rate") && moreargs) {
                opt_mutation_rate = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--restart")) {
                opt_restart = 1;
            } else if (!strcmp(argv[j],"--help")) {
                showHelp(argv[0]);
            } else {
                fprintf(stderr,"Invalid options.");
                showHelp(argv[0]);
                exit(1);
            }
        }
    }

    /* Sanity check. */
    if (state.max_shapes_incremental > state.max_shapes)
        state.max_shapes = state.max_shapes_incremental;
    if (opt_mutation_rate > 1000)
        opt_mutation_rate = 1000;

    /* Load the PNG in memory. */
    fp = fopen(argv[1],"rb");
    if (!fp) {
        perror("Opening PNG file");
        exit(1);
    }
    if ((image = PngLoad(fp,&width,&height,&alpha)) == NULL) {
        printf("Can't load the specified image.");
        exit(1);
    }

    printf("Image %d %d, alpha:%d at %p\n", width, height, alpha, image);
    fclose(fp);

    /* Initialize SDL and allocate our arrays of triangles. */
    screen = sdlInit(width,height,0);
    fb = malloc(width*height*3);
    triangles = mkRandomtriangles(state.max_shapes,width,height);
    best = mkRandomtriangles(state.max_shapes,width,height);
    absbest = mkRandomtriangles(state.max_shapes,width,height);
    state.absbestdiff = bestdiff = 100;

    /* Load the binary file if any. */
    if (!opt_restart) {
        loadBinary(argv[2],best);
    } else {
        best->inuse = state.max_shapes_incremental;
    }
    absbest->inuse = best->inuse;
    memcpy(absbest->triangles,best->triangles,
        sizeof(struct triangle)*best->count);

    /* Show the current evolved image and the real image for one scond each. */
    memset(fb,0,width*height*3);
    drawtriangles(fb,width,height,best);
    sdlShowRgb(screen,fb,width,height);
    sleep(1);
    sdlShowRgb(screen,image,width,height);
    sleep(1);

    /* Evolve the current solution using simulated annealing. */
    while(1) {
        state.generation++;
        if (state.temperature > 0 && !(state.generation % 10)) {
            state.temperature -= 0.00001;
            if (state.temperature < 0) state.temperature = 0;
        }

        /* From time to time allow the current solution to use one more
         * triangle, up to the configured max number. */
        if ((state.generation % 1000) == 0) {
            if (state.max_shapes_incremental < triangles->count &&
                triangles->inuse > state.max_shapes_incremental-1)
            {
                state.max_shapes_incremental++;
            }
        }

        /* Copy what is currenly the best solution, and mutate it. */
        memcpy(triangles->triangles,best->triangles,
            sizeof(struct triangle)*best->count);
        triangles->inuse = best->inuse;
        mutatetriangles(triangles,10,width,height);

        /* Draw the mutated solution, and check what is its fitness.
         * In our case the fitness is the difference bewteen the target
         * image and our image. */
        memset(fb,0,width*height*3);
        drawtriangles(fb,width,height,triangles);
        diff = computeDiff(image,fb,width,height);

        /* The percentage of difference is calculate taking the ratio between
         * the maximum difference and the current difference.
         * The magic constant 422 is actually the max difference between
         * two pixels as r,g,b coordinates in the space, so sqrt(255^2*3). */
        percdiff = (float)diff/(width*height*442)*100;
        if (percdiff < bestdiff ||
            (state.temperature > 0 &&
             ((float)rand()/RAND_MAX) < state.temperature &&
             (percdiff-state.absbestdiff) < 2*state.temperature))
        {
            /* Save what is currently our "best" solution, even if actually
             * this may be a jump backward depending on the temperature.
             * It will be used as a base of the next iteration. */
            best->inuse = triangles->inuse;
            memcpy(best->triangles,triangles->triangles,
                sizeof(struct triangle)*best->count);

            if (percdiff < bestdiff) {
                /* We always save a copy of the absolute best solution we found
                 * so far, after some generation without finding anything better
                 * we may jump back to that solution.
                 *
                 * We also use the absolute best solution to save the program
                 * state in the binary file, and as SVG output. */
                absbest->inuse = best->inuse;
                memcpy(absbest->triangles,best->triangles,
                    sizeof(struct triangle)*best->count);
                state.absbestdiff = percdiff;
            }

            printf("Diff is %f%% (inuse:%d, max:%d, gen:%lld, temp:%f)\n",
                percdiff,
                triangles->inuse,
                state.max_shapes_incremental,
                state.generation,
                state.temperature);

            bestdiff = percdiff;
            sdlShowRgb(screen,fb,width,height);
        }
        processSdlEvents();

        /* From time to time save the current state into a binary save
         * and produce an SVG of the current solution. */
        if ((state.generation % 100) == 0) {
            saveSvg(argv[3],absbest,width,height);
            saveBinary(argv[2],absbest);
        }
    }
    return 0;
}