Advertisement
Guest User

Hedgewars preview map rendering code

a guest
Jun 10th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "frontlib.h"
  2. #include "util/logging.h"
  3. #include "model/map.h"
  4. #include "ipc/mapconn.h"
  5.  
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <assert.h>
  9.  
  10. // Callback function that will be called when the map is rendered
  11. static void handleMapSuccess(void *context, const uint8_t *bitmap, int numHedgehogs) {
  12.     printf("Drawing map for %i brave little hogs...", numHedgehogs);
  13.  
  14.     // Draw the map as ASCII art
  15.     for(int y=0; y<MAPIMAGE_HEIGHT; y++) {
  16.         for(int x=0; x<MAPIMAGE_WIDTH; x++) {
  17.             int pixelnum = x + y*MAPIMAGE_WIDTH;
  18.             bool pixel = bitmap[pixelnum>>3] & (1<<(7-(pixelnum&7)));
  19.             printf(pixel ? "#" : " ");
  20.         }
  21.         printf("\n");
  22.     }
  23.  
  24.     // Destroy the connection object (this will end the "tick" loop below)
  25.     flib_mapconn **connptr = context;
  26.     flib_mapconn_destroy(*connptr);
  27.     *connptr = NULL;
  28. }
  29.  
  30. // Callback function that will be called on error
  31. static void handleMapFailure(void *context, const char *errormessage) {
  32.     flib_log_e("Map rendering failed: %s", errormessage);
  33.  
  34.     // Destroy the connection object (this will end the "tick" loop below)
  35.     flib_mapconn **connptr = context;
  36.     flib_mapconn_destroy(*connptr);
  37.     *connptr = NULL;
  38. }
  39.  
  40. // Helper code to start the engine (Windows)
  41. static void startEngine(int port) {
  42.     char commandbuffer[255];
  43.     const char *enginePath = "C:\\Programmieren\\Hedgewars\\bin";
  44.     const char *configPath = "C:\\Programmieren\\Hedgewars\\share\\hedgewars";
  45.     snprintf(commandbuffer, 255, "start %s\\hwengine.exe %s %i landpreview", enginePath, configPath, port);
  46.     system(commandbuffer);
  47. }
  48.  
  49. int main(int argc, char *argv[]) {
  50.     flib_init(0);
  51.     flib_log_setLevel(FLIB_LOGLEVEL_ALL);
  52.  
  53.     // Create a map description and check that there was no error
  54.     flib_map *map = flib_map_create_maze("Jungle", MAZE_SIZE_SMALL_TUNNELS);
  55.     assert(map);
  56.  
  57.     // Create a new connection to the engine and check that there was no error
  58.     flib_mapconn *mapConnection = flib_mapconn_create("This is the seed value", map);
  59.     assert(mapConnection);
  60.  
  61.     // We don't need the map description anymore
  62.     flib_map_destroy(map);
  63.     map = NULL;
  64.  
  65.     // Register the callback functions
  66.     flib_mapconn_onFailure(mapConnection, &handleMapFailure, &mapConnection);
  67.     flib_mapconn_onSuccess(mapConnection, &handleMapSuccess, &mapConnection);
  68.  
  69.     // Start the engine process and tell it which port the frontlib is listening on
  70.     startEngine(flib_mapconn_getport(mapConnection));
  71.  
  72.     // Usually, flib_mapconn_tick will be called in an event loop that runs several
  73.     // times per second. It handles I/O operations and progress, and calls
  74.     // callbacks when something interesting happens.
  75.     while(mapConnection) {
  76.         flib_mapconn_tick(mapConnection);
  77.     }
  78.  
  79.     flib_quit();
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement