author | Medo <smaxein@googlemail.com> |
Mon, 04 Jun 2012 21:12:20 +0200 | |
changeset 7173 | 7c2eb284f9f1 |
parent 7171 | 906e72caea7b |
child 7175 | 038e3415100a |
permissions | -rw-r--r-- |
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" |
7171 | 3 |
#include "socket.h" |
7173
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
4 |
#include "ipc.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> |
7173
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
11 |
#include <assert.h> |
7155
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
|
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
static int flib_initflags; |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
|
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
int flib_init(int flags) { |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
flib_initflags = flags; |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
|
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) { |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
if(SDL_Init(0)==-1) { |
7158
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
7155
diff
changeset
|
20 |
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
|
21 |
return -1; |
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 |
|
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
if(SDLNet_Init()==-1) { |
7158
a0573014ff4f
Further work on the frontend library, restructuring, ...
Medo <smaxein@googlemail.com>
parents:
7155
diff
changeset
|
26 |
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
|
27 |
if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) { |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
SDL_Quit(); |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
} |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
return -1; |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
} |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
|
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() { |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
SDLNet_Quit(); |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) { |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
SDL_Quit(); |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
} |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
} |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
|
7173
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
43 |
static void onConfigQuery(void *context) { |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
44 |
flib_log_i("Sending config..."); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
45 |
flib_ipc ipc = (flib_ipc)context; |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
46 |
flib_ipc_send_messagestr(ipc, "TL"); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
47 |
flib_ipc_send_messagestr(ipc, "eseed loremipsum"); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
48 |
flib_ipc_send_messagestr(ipc, "escript Missions/Training/Basic_Training_-_Bazooka.lua"); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
49 |
} |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
50 |
|
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
51 |
static void onDisconnect(void *context) { |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
52 |
flib_log_i("Connection closed."); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
53 |
flib_ipc_destroy((flib_ipc*)context); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
54 |
} |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
55 |
|
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
56 |
static void onGameEnd(void *context, int gameEndType) { |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
57 |
switch(gameEndType) { |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
58 |
case GAME_END_FINISHED: |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
59 |
flib_log_i("Game finished."); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
60 |
flib_constbuffer demobuf = flib_ipc_getdemo(context); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
61 |
flib_log_i("Writing demo (%u bytes)...", demobuf.size); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
62 |
FILE *file = fopen("testdemo.dem", "wb"); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
63 |
fwrite(demobuf.data, 1, demobuf.size, file); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
64 |
fclose(file); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
65 |
file = NULL; |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
66 |
break; |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
67 |
case GAME_END_HALTED: |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
68 |
flib_log_i("Game halted."); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
69 |
break; |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
70 |
case GAME_END_INTERRUPTED: |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
71 |
flib_log_i("Game iterrupted."); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
72 |
break; |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
73 |
} |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
74 |
} |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
75 |
|
7155
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
76 |
int main(int argc, char *argv[]) { |
7171 | 77 |
flib_init(0); |
78 |
||
7173
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
79 |
flib_ipc ipc = flib_ipc_create(true, "Medo42"); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
80 |
assert(ipc); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
81 |
flib_ipc_onConfigQuery(ipc, &onConfigQuery, ipc); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
82 |
flib_ipc_onDisconnect(ipc, &onDisconnect, &ipc); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
83 |
flib_ipc_onGameEnd(ipc, &onGameEnd, ipc); |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
84 |
|
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
85 |
while(ipc) { |
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
86 |
flib_ipc_tick(ipc); |
7155
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
87 |
} |
7173
7c2eb284f9f1
Frontlib: Work on the callback mechanisms for IPC
Medo <smaxein@googlemail.com>
parents:
7171
diff
changeset
|
88 |
flib_log_i("Shutting down..."); |
7162
fe76d24a25d7
Demo recording for the frontend library
Medo <smaxein@googlemail.com>
parents:
7160
diff
changeset
|
89 |
flib_quit(); |
7155
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
90 |
return 0; |
273ad375d64e
Started work on the frontend networking library
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
91 |
} |