project_files/frontlib/frontlib.c
changeset 7224 5143861c83bd
parent 7182 076aba32abd3
child 7314 6171f0bad318
equal deleted inserted replaced
7221:8d04e85ca204 7224:5143861c83bd
     1 #include "frontlib.h"
     1 #include "frontlib.h"
     2 #include "util/logging.h"
     2 #include "util/logging.h"
     3 #include "model/map.h"
       
     4 #include "ipc/mapconn.h"
       
     5 #include "ipc/gameconn.h"
       
     6 
       
     7 #include <SDL.h>
     3 #include <SDL.h>
     8 #include <SDL_net.h>
     4 #include <SDL_net.h>
     9 #include <stdio.h>
       
    10 #include <stdint.h>
       
    11 #include <stdlib.h>
       
    12 #include <assert.h>
       
    13 
     5 
    14 static int flib_initflags;
     6 static int flib_initflags;
    15 
     7 
    16 int flib_init(int flags) {
     8 int flib_init(int flags) {
    17 	flib_initflags = flags;
     9 	flib_initflags = flags;
    18 
    10 
       
    11 	flib_log_d("Initializing frontlib");
    19 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    12 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    20 		if(SDL_Init(0)==-1) {
    13 		if(SDL_Init(0)==-1) {
    21 		    flib_log_e("Error in SDL_Init: %s", SDL_GetError());
    14 		    flib_log_e("Error in SDL_Init: %s", SDL_GetError());
    22 		    return -1;
    15 		    return -1;
    23 		}
    16 		}
    33 
    26 
    34 	return 0;
    27 	return 0;
    35 }
    28 }
    36 
    29 
    37 void flib_quit() {
    30 void flib_quit() {
       
    31 	flib_log_d("Shutting down frontlib");
    38 	SDLNet_Quit();
    32 	SDLNet_Quit();
    39 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    33 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    40 		SDL_Quit();
    34 		SDL_Quit();
    41 	}
    35 	}
    42 }
    36 }
    43 
       
    44 static void onDisconnect(void *context, int reason) {
       
    45 	flib_log_i("Connection closed. Reason: %i", reason);
       
    46 	flib_gameconn **connptr = context;
       
    47 	flib_gameconn_destroy(*connptr);
       
    48 	*connptr = NULL;
       
    49 }
       
    50 
       
    51 static void onGameRecorded(void *context, const uint8_t *record, int size, bool isSavegame) {
       
    52 	flib_log_i("Writing %s (%i bytes)...", isSavegame ? "savegame" : "demo", size);
       
    53 	FILE *file = fopen(isSavegame ? "testsave.42.hws" : "testdemo.42.hwd", "wb");
       
    54 	fwrite(record, 1, size, file);
       
    55 	fclose(file);
       
    56 }
       
    57 
       
    58 int main(int argc, char *argv[]) {
       
    59 	flib_init(0);
       
    60 
       
    61 	flib_cfg_meta *metaconf = flib_cfg_meta_from_ini("basicsettings.ini", "gamemods.ini");
       
    62 	assert(metaconf);
       
    63 	flib_gamesetup setup;
       
    64 	setup.gamescheme = flib_cfg_from_ini(metaconf, "scheme_shoppa.ini");
       
    65 	setup.map = flib_map_create_maze("Jungle", MAZE_SIZE_MEDIUM_TUNNELS);
       
    66 	setup.seed = "apsfooasdgnds";
       
    67 	setup.script = NULL;
       
    68 	setup.teamcount = 2;
       
    69 	setup.teams = calloc(2, sizeof(flib_team));
       
    70 	setup.teams[0].color = 0xffff0000;
       
    71 	setup.teams[0].flag = "australia";
       
    72 	setup.teams[0].fort = "Plane";
       
    73 	setup.teams[0].grave = "Bone";
       
    74 	setup.teams[0].hogsInGame = 2;
       
    75 	setup.teams[0].name = "Team Awesome";
       
    76 	setup.teams[0].voicepack = "British";
       
    77 	setup.teams[0].weaponset = flib_weaponset_create("Defaultweaps");
       
    78 	setup.teams[0].hogs[0].difficulty = 2;
       
    79 	setup.teams[0].hogs[0].hat = "NoHat";
       
    80 	setup.teams[0].hogs[0].initialHealth = 100;
       
    81 	setup.teams[0].hogs[0].name = "Harry 120";
       
    82 	setup.teams[0].hogs[1].difficulty = 2;
       
    83 	setup.teams[0].hogs[1].hat = "chef";
       
    84 	setup.teams[0].hogs[1].initialHealth = 100;
       
    85 	setup.teams[0].hogs[1].name = "Chefkoch";
       
    86 	setup.teams[1].color = 0xff0000ff;
       
    87 	setup.teams[1].flag = "germany";
       
    88 	setup.teams[1].fort = "Cake";
       
    89 	setup.teams[1].grave = "Cherry";
       
    90 	setup.teams[1].hogsInGame = 2;
       
    91 	setup.teams[1].name = "The Krauts";
       
    92 	setup.teams[1].voicepack = "Pirate";
       
    93 	setup.teams[1].weaponset = flib_weaponset_create("Defaultweaps");
       
    94 	setup.teams[1].hogs[0].difficulty = 0;
       
    95 	setup.teams[1].hogs[0].hat = "quotecap";
       
    96 	setup.teams[1].hogs[0].initialHealth = 100;
       
    97 	setup.teams[1].hogs[0].name = "Quote";
       
    98 	setup.teams[1].hogs[1].difficulty = 0;
       
    99 	setup.teams[1].hogs[1].hat = "chef";
       
   100 	setup.teams[1].hogs[1].initialHealth = 100;
       
   101 	setup.teams[1].hogs[1].name = "Chefkoch2";
       
   102 
       
   103 	flib_gameconn *gameconn = flib_gameconn_create("Medo42", metaconf, &setup, false);
       
   104 	assert(gameconn);
       
   105 
       
   106 	flib_gameconn_onDisconnect(gameconn, &onDisconnect, &gameconn);
       
   107 	flib_gameconn_onGameRecorded(gameconn, &onGameRecorded, &gameconn);
       
   108 
       
   109 	while(gameconn) {
       
   110 		flib_gameconn_tick(gameconn);
       
   111 	}
       
   112 	flib_log_i("Shutting down...");
       
   113 	flib_quit();
       
   114 	return 0;
       
   115 }