project_files/frontlib/frontlib.c
author Medo <smaxein@googlemail.com>
Sat, 02 Jun 2012 14:26:52 +0200
changeset 7162 fe76d24a25d7
parent 7160 c42949cfdd92
child 7171 906e72caea7b
permissions -rw-r--r--
Demo recording for the frontend library
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
#include "frontlib.h"
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
#include "logging.h"
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
     3
#include "nonblocksockets.h"
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
     4
#include "ipcconn.h"
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
#include <SDL.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
#include <SDL_net.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
#include <stdio.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
#include <stdint.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
#include <stdlib.h>
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
static int flib_initflags;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
int flib_init(int flags) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
	flib_initflags = flags;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
		if(SDL_Init(0)==-1) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    19
		    flib_log_e("Error in SDL_Init: %s", SDL_GetError());
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
		    return -1;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
	if(SDLNet_Init()==-1) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    25
		flib_log_e("Error in SDLNet_Init: %s", SDLNet_GetError());
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
		if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
			SDL_Quit();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
		return -1;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    32
	flib_ipcconn_init();
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	return 0;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
void flib_quit() {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    37
	flib_ipcconn_quit();
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	SDLNet_Quit();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
		SDL_Quit();
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
int main(int argc, char *argv[]) {
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    46
	flib_init(FRONTLIB_SDL_ALREADY_INITIALIZED);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    47
	flib_ipcconn_start(true);
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
	char data[256];
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    49
	while(flib_ipcconn_state() != IPC_NOT_CONNECTED) {
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    50
		flib_ipcconn_tick();
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    51
		int size = flib_ipcconn_recv_message(data);
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		if(size>0) {
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    53
			data[size]=0;
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    54
			flib_log_i("IPC IN: %s", data);
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    55
			switch(data[0]) {
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    56
			case 'C':
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    57
				flib_log_i("Sending config...");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    58
				flib_ipcconn_send_messagestr("TL");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    59
				flib_ipcconn_send_messagestr("eseed loremipsum");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    60
				flib_ipcconn_send_messagestr("escript Missions/Training/Basic_Training_-_Bazooka.lua");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    61
				break;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    62
			case '?':
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    63
				flib_log_i("Sending pong...");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    64
				flib_ipcconn_send_messagestr("!");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    65
				break;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    66
			case 'Q':
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    67
				flib_log_i("Game interrupted.");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    68
				break;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    69
			case 'q':
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    70
				flib_log_i("Game finished.");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    71
				flib_constbuffer demobuf = flib_ipcconn_getdemo();
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    72
				flib_log_i("Writing demo (%u bytes)...", demobuf.size);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    73
				FILE *file = fopen("testdemo.dem", "w");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    74
				fwrite(demobuf.data, 1, demobuf.size, file);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    75
				fclose(file);
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    76
				file = NULL;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    77
				break;
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    78
			case 'H':
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    79
				flib_log_i("Game halted.");
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    80
				break;
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
			}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
		}
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
	}
7158
a0573014ff4f Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents: 7155
diff changeset
    84
	flib_log_i("IPC connection lost.");
7162
fe76d24a25d7 Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents: 7160
diff changeset
    85
	flib_quit();
7155
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
	return 0;
273ad375d64e Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
}