Пример #1
0
static bool openMazeFile() {
    char filename[MAX_PATH];
    memset(filename, 0, MAX_PATH);

	OPENFILENAME ofn;
	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = nullptr;
	ofn.lpstrFile = filename;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrFilter = "Mazes (*.pmz)\0*.pmz\0All files (*.*)\0*.*\0";
	ofn.nFilterIndex = 1;
	ofn.lpstrFileTitle = nullptr;
	ofn.nMaxFileTitle = 0;
	ofn.lpstrInitialDir = nullptr;
	ofn.Flags |= OFN_NOCHANGEDIR;

    if (GetOpenFileName(&ofn)) {
		unique_ptr<maze> m = openMaze(filename);
        if (m == nullptr) {
            MessageBox(nullptr, "Cannot open this file", "Error", MB_OK | MB_ICONERROR);
            return false;
        } else {
			lastFilename = filename;
            setMaze(move(m));
            return true;
        }
    }
    return false;
}
Пример #2
0
bool Pathfinder::importMaze(string file_name)
{
    string piece;
    stringstream ss;
    ifstream myfile(file_name.c_str());
    
    if (myfile.is_open())
    {
        string curr_maze;
        ss << myfile.rdbuf();
        
        while (ss >> piece)
            curr_maze += piece;

        if (!validMaze(curr_maze))
        {
            return false;
        }
        setMaze(curr_maze);
        return true;
    }
Пример #3
0
int main (int argc, char **argv) {
	if (SDL_Init(SDL_INIT_VIDEO) == -1)
		return -1;

	if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
		return -1;

    srand(SDL_GetTicks());

    window = SDL_CreateWindow("PinoMaze level editor",
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		windowWidth, windowHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    if (window == nullptr)
        return 2;

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

	if (!openResourceFile("editor.dat") || !loadResources(renderer)) {
		fprintf(stderr, "Could not load resources\n");
		return 3;
	}

	editor = make_unique<mazeEditor>();
	mainGrid = make_unique<a_star>();

	if (argc > 1) {
		setMaze(openMaze(argv[1]));
	}

	if (!mainMaze) {
		setMaze(make_unique<maze>(windowWidth / RES_TILES_SIZE, windowHeight / RES_TILES_SIZE));
	}

    setStatus("Welcome to PinoMaze level editor");

    SDL_Event event;

    bool quit = false;

    int lastTime = SDL_GetTicks();
    float unprocessed = 0;
    float msPerTick = 1000.0 / TICKRATE;
    int frames = 0;
    int ticks = 0;
    int secondsTimer = SDL_GetTicks();

    char fpsCounter[STATUS_LENGTH];
    memset(fpsCounter, 0, STATUS_LENGTH);

    while(!quit) {
        int now = SDL_GetTicks();
        unprocessed += (now - lastTime) / msPerTick;
        lastTime = now;
        bool shouldRender = true;
        while (unprocessed >= 1) {
            tick();

            ++ticks;
            --unprocessed;
            shouldRender = true;
        }

        SDL_Delay(2);

        if (shouldRender) {
            render(renderer, fpsCounter);
            ++frames;
        }

        while(SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_WINDOWEVENT:
                switch (event.window.event) {
                case SDL_WINDOWEVENT_SIZE_CHANGED:
                case SDL_WINDOWEVENT_RESIZED:
                    windowWidth = event.window.data1;
                    windowHeight = event.window.data2;
                    break;
                }
                break;
            case SDL_QUIT:
                quit = true;
                break;
            default:
                handleEvent(event);
            }
        }

        if (SDL_GetTicks() - secondsTimer > 1000) {
            secondsTimer += 1000;
            snprintf(fpsCounter, STATUS_LENGTH, "Frames: %d\nTicks: %d", frames, ticks);
            frames = 0;
            ticks = 0;
        }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}
Пример #4
0
static void handleEvent(SDL_Event &e) {
    switch(e.type) {
    case SDL_KEYDOWN:
        switch (e.key.keysym.scancode) {
        case SDL_SCANCODE_R:
            setMaze(generateRandomMaze(windowWidth / RES_TILES_SIZE, windowHeight / RES_TILES_SIZE));
            setStatus("Random maze generated");
            break;
        case SDL_SCANCODE_O:
            if (openMazeFile()) {
                setStatus("Maze file opened");
				printf("%s\n", lastFilename.c_str());
            }
            break;
        case SDL_SCANCODE_A:
            switch (currentAlgorithm) {
            case ALGORITHM_ASTAR:
                currentAlgorithm = ALGORITHM_GBFS;
                setStatus("Selected algorithm: Greedy Best First Search");
                break;
            case ALGORITHM_GBFS:
                currentAlgorithm = ALGORITHM_DIJKSTRA;
                setStatus("Selected algorithm: Dijkstra");
                break;
            case ALGORITHM_DIJKSTRA:
                currentAlgorithm = ALGORITHM_ASTAR;
                setStatus("Selected algorithm: A*");
                break;
            }
            break;
        case SDL_SCANCODE_S:
            if (saveMazeFile()) {
                setStatus("Maze file saved");
				printf("%s\n", lastFilename.c_str());
            }
            break;
        case SDL_SCANCODE_F:
            showFpsCounter = !showFpsCounter;
            break;
        case SDL_SCANCODE_G:
			mainMaze->renderGrid = !mainMaze->renderGrid;
            break;
        case SDL_SCANCODE_C:
			mainMaze->clearTemp();
            mainGrid->stop();
            setStatus("Path finding stopped");
            break;
        case SDL_SCANCODE_N:
            setMaze(unique_ptr<maze>(new maze(windowWidth / RES_TILES_SIZE, windowHeight / RES_TILES_SIZE)));
            setStatus("Created empty new maze");
            break;
        case SDL_SCANCODE_SPACE:
            mainGrid->start();
            setStatus("Path finding started");
            break;
        default:
            break;
        }
        break;
    case SDL_USEREVENT:
		resetMaze();
        setStatus("Maze has been cropped");
        break;
    }
    editor->handleEvent(e);
}