Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "frontlib.h"
- #include "util/logging.h"
- #include "model/map.h"
- #include "ipc/mapconn.h"
- #include <stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
- // Callback function that will be called when the map is rendered
- static void handleMapSuccess(void *context, const uint8_t *bitmap, int numHedgehogs) {
- printf("Drawing map for %i brave little hogs...", numHedgehogs);
- // Draw the map as ASCII art
- for(int y=0; y<MAPIMAGE_HEIGHT; y++) {
- for(int x=0; x<MAPIMAGE_WIDTH; x++) {
- int pixelnum = x + y*MAPIMAGE_WIDTH;
- bool pixel = bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7)));
- printf(pixel ? "#" : " ");
- }
- printf("\n");
- }
- // Destroy the connection object (this will end the "tick" loop below)
- flib_mapconn **connptr = context;
- flib_mapconn_destroy(*connptr);
- *connptr = NULL;
- }
- // Callback function that will be called on error
- static void handleMapFailure(void *context, const char *errormessage) {
- flib_log_e("Map rendering failed: %s", errormessage);
- // Destroy the connection object (this will end the "tick" loop below)
- flib_mapconn **connptr = context;
- flib_mapconn_destroy(*connptr);
- *connptr = NULL;
- }
- // Helper code to start the engine (Windows)
- static void startEngine(int port) {
- char commandbuffer[255];
- const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
- const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
- snprintf(commandbuffer, 255, "start %s\\hwengine.exe %s %i landpreview", enginePath, configPath, port);
- system(commandbuffer);
- }
- int main(int argc, char *argv[]) {
- flib_init(0);
- flib_log_setLevel(FLIB_LOGLEVEL_ALL);
- // Create a map description and check that there was no error
- flib_map *map = flib_map_create_maze("Jungle", MAZE_SIZE_SMALL_TUNNELS);
- assert(map);
- // Create a new connection to the engine and check that there was no error
- flib_mapconn *mapConnection = flib_mapconn_create("This is the seed value", map);
- assert(mapConnection);
- // We don't need the map description anymore
- flib_map_destroy(map);
- map = NULL;
- // Register the callback functions
- flib_mapconn_onFailure(mapConnection, &handleMapFailure, &mapConnection);
- flib_mapconn_onSuccess(mapConnection, &handleMapSuccess, &mapConnection);
- // Start the engine process and tell it which port the frontlib is listening on
- startEngine(flib_mapconn_getport(mapConnection));
- // Usually, flib_mapconn_tick will be called in an event loop that runs several
- // times per second. It handles I/O operations and progress, and calls
- // callbacks when something interesting happens.
- while(mapConnection) {
- flib_mapconn_tick(mapConnection);
- }
- flib_quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement