Skip to content

Commit

Permalink
See desc
Browse files Browse the repository at this point in the history
* added logger (also to file)
* fixed chat input bug
* changed serverlist column layout
* account for NULL by bsearch in texture_flag_offset()
  • Loading branch information
xtreme8000 committed Jul 22, 2018
1 parent 5011177 commit 126fbf1
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 81 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -44,6 +44,7 @@ These libraries and files are needed:
| libdeflate | *MIT* | decompression of maps | [Link](https://github.com/ebiggers/libdeflate) |
| enet | *MIT* | networking library | [Link](https://github.com/lsalzman/enet) |
| parson | *MIT* | JSON parser | [Link](https://github.com/kgabis/parson) |
| log.c | *MIT* | logger | [Link](https://github.com/xtreme8000/log.c) |

You will need to compile the following by yourself, or get hold of precompiled binaries:

Expand Down
4 changes: 4 additions & 0 deletions resources/logs/.gitignore
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
5 changes: 2 additions & 3 deletions src/common.h
Expand Up @@ -71,6 +71,7 @@
#include "lodepng/lodepng.h"
#include "libdeflate.h"
#include "ini.h"
#include "log.h"

#ifdef _WIN32
#define OS_WINDOWS
Expand Down Expand Up @@ -167,12 +168,10 @@ const char* reason_disconnect(int code);
#define SCREEN_TEAM_SELECT 1
#define SCREEN_GUN_SELECT 2

extern char text_input_first;

extern int ms_seed;
int ms_rand(void);

#define CHECK_ALLOCATION_ERROR(ret) if (!ret) { \
printf("Critical error: memory allocation failed (%s:%d)", __func__, __LINE__); \
log_fatal("Critical error: memory allocation failed (%s:%d)", __func__, __LINE__); \
exit(1); \
}
2 changes: 1 addition & 1 deletion src/config.c
Expand Up @@ -129,7 +129,7 @@ static int config_read_key(void* user, const char* section, const char* name, co
for(int k=0;k<list_size(&config_keys);k++) {
struct config_key_pair* key = list_get(&config_keys,k);
if(!strcmp(name,key->name)) {
//printf("found override for %s, from %i to %i\n",key->name,key->def,atoi(value));
log_debug("found override for %s, from %i to %i",key->name,key->def,atoi(value));
key->def = strtol(value,NULL,0);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/file.c
Expand Up @@ -27,7 +27,7 @@ unsigned char* file_load(const char* name) {
FILE* f;
f = fopen(name,"rb");
if (!f) {
printf("ERROR: failed to open '%s', exiting\n", name);
log_fatal("ERROR: failed to open '%s', exiting", name);
exit(1);
}
fseek(f,0,SEEK_END);
Expand Down
10 changes: 9 additions & 1 deletion src/font.c
Expand Up @@ -80,6 +80,9 @@ static struct font_backed* font_find(float h) {
case FONT_SMALLFNT:
file = file_load("fonts/Terminal.ttf");
break;
default:
free(f.cdata);
return NULL;
}

while(1) {
Expand All @@ -95,7 +98,7 @@ static struct font_backed* font_find(float h) {
f.h *= 2;
}

printf("texsize: %i:%ipx [size %f] type: %i\n",f.w,f.h,f.size,f.type);
log_info("font texsize: %i:%ipx [size %f] type: %i",f.w,f.h,f.size,f.type);

glGenTextures(1,&f.texture_id);
glBindTexture(GL_TEXTURE_2D,f.texture_id);
Expand All @@ -112,6 +115,8 @@ static struct font_backed* font_find(float h) {
float font_length(float h, char* text) {
struct font_backed* font = font_find(h);

if(!font)
return 0.0F;

stbtt_aligned_quad q;
float y = h*0.75F;
Expand All @@ -137,6 +142,9 @@ void font_render(float x, float y, float h, char* text) {

struct font_backed* font = font_find(h);

if(!font)
return;

int chars_x = 16;
int chars_y = (font_type==FONT_SMALLFNT)?16:14;
float size_ratio = (font_type==FONT_SMALLFNT)?0.75F:0.49F;
Expand Down
21 changes: 9 additions & 12 deletions src/hud.c
Expand Up @@ -1064,7 +1064,7 @@ static void hud_ingame_keyboard(int key, int action, int mods, int internal) {
camera_mode = CAMERAMODE_BODYVIEW;
}
if(key==WINDOW_KEY_SNEAK) {
printf("%f,%f,%f,%f,%f\n",camera_x,camera_y,camera_z,camera_rot_x,camera_rot_y);
log_debug("%f,%f,%f,%f,%f",camera_x,camera_y,camera_z,camera_rot_x,camera_rot_y);
players[local_player_id].pos.x = 256.0F;
players[local_player_id].pos.y = 63.0F;
players[local_player_id].pos.z = 256.0F;
Expand All @@ -1086,13 +1086,11 @@ static void hud_ingame_keyboard(int key, int action, int mods, int internal) {

if(key==WINDOW_KEY_COMMAND) {
chat_input_mode = CHAT_ALL_INPUT;
text_input_first = 1;
strcpy(chat[0][0],"/");
}

if(key==WINDOW_KEY_CHAT) {
chat_input_mode = CHAT_ALL_INPUT;
text_input_first = 1;
chat[0][0][0] = 0;
}

Expand All @@ -1104,10 +1102,8 @@ static void hud_ingame_keyboard(int key, int action, int mods, int internal) {
if(key==WINDOW_KEY_YES) {
if(show_exit) {
hud_change(&hud_serverlist);
text_input_first = 1;
} else {
chat_input_mode = CHAT_TEAM_INPUT;
text_input_first = 1;
chat[0][0][0] = 0;
}
}
Expand Down Expand Up @@ -1374,7 +1370,6 @@ static void hud_serverlist_init() {
request_version = http_get("http://aos.party/bs/version/",NULL);

chat_input_mode = CHAT_ALL_INPUT;
text_input_first = 0;
chat[0][0][0] = 0;
serverlist_is_outdated = 0;
}
Expand Down Expand Up @@ -1432,8 +1427,8 @@ static void hud_serverlist_render(float scalex, float scaley) {

font_render((settings.window_width-600*scaley)/2.0F+0*scaley,450*scaley,18*scaley,"Players");
font_render((settings.window_width-600*scaley)/2.0F+75*scaley,450*scaley,18*scaley,"Name");
font_render((settings.window_width-600*scaley)/2.0F+350*scaley,450*scaley,18*scaley,"Map");
font_render((settings.window_width-600*scaley)/2.0F+480*scaley,450*scaley,18*scaley,"Mode");
font_render((settings.window_width-600*scaley)/2.0F+335*scaley,450*scaley,18*scaley,"Map");
font_render((settings.window_width-600*scaley)/2.0F+490*scaley,450*scaley,18*scaley,"Mode");
font_render((settings.window_width-600*scaley)/2.0F+560*scaley,450*scaley,18*scaley,"Ping");

//texture_draw(&texture_ui_reload,(settings.window_width-600*scaley)/2.0F+375*scaley,500*scaley,32*scaley,32*scaley);
Expand Down Expand Up @@ -1475,8 +1470,8 @@ static void hud_serverlist_render(float scalex, float scaley) {
strcpy(total_str,"-");
font_render((settings.window_width-600*scaley)/2.0F+0*scaley,450*scaley-20*scaley*(k+1)-serverlist_scroll,16*scaley,total_str);
font_render((settings.window_width-600*scaley)/2.0F+75*scaley,450*scaley-20*scaley*(k+1)-serverlist_scroll,16*scaley,serverlist[k].name);
font_render((settings.window_width-600*scaley)/2.0F+350*scaley,450*scaley-20*scaley*(k+1)-serverlist_scroll,16*scaley,serverlist[k].map);
font_render((settings.window_width-600*scaley)/2.0F+480*scaley,450*scaley-20*scaley*(k+1)-serverlist_scroll,16*scaley,serverlist[k].gamemode);
font_render((settings.window_width-600*scaley)/2.0F+335*scaley,450*scaley-20*scaley*(k+1)-serverlist_scroll,16*scaley,serverlist[k].map);
font_render((settings.window_width-600*scaley)/2.0F+490*scaley,450*scaley-20*scaley*(k+1)-serverlist_scroll,16*scaley,serverlist[k].gamemode);
sprintf(total_str,"%ims",serverlist[k].ping);

if(serverlist[k].current>=0) {
Expand Down Expand Up @@ -1528,8 +1523,8 @@ static void hud_serverlist_render(float scalex, float scaley) {
switch(http_process(request_version)) {
case HTTP_STATUS_COMPLETED:
serverlist_is_outdated = 1;
printf("newest game version: %s\n",request_version->response_data);
printf("current game version: %s\n",BETTERSPADES_VERSION);
log_info("newest game version: %s",request_version->response_data);
log_info("current game version: %s",BETTERSPADES_VERSION);
serverlist_is_outdated = strcmp(request_version->response_data,BETTERSPADES_VERSION)!=0;
http_release(request_version);
request_version = NULL;
Expand Down Expand Up @@ -1676,6 +1671,8 @@ static struct config_setting* hud_settings_edit = NULL;

static void hud_settings_init() {
memcpy(&settings_tmp,&settings,sizeof(struct RENDER_OPTIONS));
chat_input_mode = CHAT_ALL_INPUT;
chat[0][0][0] = 0;
}

static void hud_settings_render(float scalex, float scaley) {
Expand Down
39 changes: 22 additions & 17 deletions src/main.c
Expand Up @@ -59,6 +59,8 @@ void chat_add(int channel, unsigned int color, const char* msg) {
strcpy(chat[channel][1],msg);
chat_color[channel][1] = color;
chat_timer[channel][1] = window_time();
if(channel==0)
log_info(msg);
}
char chat_popup[256] = {};
float chat_popup_timer = 0.0F;
Expand Down Expand Up @@ -474,15 +476,9 @@ void reshape(struct window_instance* window, int width, int height) {
window_swapping(0);
}

char text_input_first;
void text_input(struct window_instance* window, unsigned int codepoint) {
if(chat_input_mode==CHAT_NO_INPUT) {
if(chat_input_mode==CHAT_NO_INPUT)
return;
}
if(text_input_first) {
text_input_first = 0;
return;
}

int len = strlen(chat[0][0]);
if(len<128) {
Expand Down Expand Up @@ -567,8 +563,8 @@ void deinit() {
}

void on_error(int i, const char* s) {
printf("Major error occured: [%i] %s\n",i,s);
getchar();
log_fatal("Major error occured: [%i] %s",i,s);
getchar();
}

int main(int argc, char** argv) {
Expand All @@ -589,13 +585,22 @@ int main(int argc, char** argv) {
settings.volume = 0;
strcpy(settings.name,"DEV_CLIENT");

log_set_level(LOG_INFO);

time_t t = time(NULL);
char buf[32];
strftime(buf,32,"logs/%m-%d-%Y.log",localtime(&t));
log_set_fp(fopen(buf,"a"));

log_info("Game started!");

config_reload();

window_init();

printf("Vendor: %s\n",glGetString(GL_VENDOR));
printf("Renderer: %s\n",glGetString(GL_RENDERER));
printf("Version: %s\n",glGetString(GL_VERSION));
log_info("Vendor: %s",glGetString(GL_VENDOR));
log_info("Renderer: %s",glGetString(GL_RENDERER));
log_info("Version: %s",glGetString(GL_VERSION));

if(settings.multisamples>0) {
glEnable(GL_MULTISAMPLE);
Expand All @@ -604,7 +609,7 @@ int main(int argc, char** argv) {
int iNumSamples = 0;
glGetIntegerv(GL_SAMPLE_BUFFERS,&iMultiSample);
glGetIntegerv(GL_SAMPLES,&iNumSamples);
printf("MSAA on, GL_SAMPLE_BUFFERS = %d, GL_SAMPLES = %d\n",iMultiSample,iNumSamples);
log_info("MSAA on, GL_SAMPLE_BUFFERS = %d, GL_SAMPLES = %d",iMultiSample,iNumSamples);
}

while(glGetError()!=GL_NO_ERROR);
Expand All @@ -619,16 +624,16 @@ int main(int argc, char** argv) {

if(argc>1) {
if(!strcmp(argv[1],"--help")) {
printf("Usage: client [server browser]\n");
printf(" client -aos://<ip>:<port> [custom address]\n");
log_info("Usage: client [server browser]");
log_info(" client -aos://<ip>:<port> [custom address]");
exit(0);
}

if(!network_connect_string(argv[1]+1)) {
printf("Error: Connection failed (use --help for instructions)\n");
log_error("Error: Connection failed (use --help for instructions)");
exit(1);
} else {
printf("Connection to %s successfull\n",argv[1]+1);
log_info("Connection to %s successful",argv[1]+1);
hud_change(&hud_ingame);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/map.c
Expand Up @@ -606,7 +606,7 @@ void map_set(int x, int y, int z, unsigned long long color) {

/*int err = pthread_rwlock_trywrlock(&chunk_map_lock);
if(err!=0) {
printf("pthread: %i\n",err);
log_debug("pthread: %i",err);
return;
}*/
pthread_rwlock_wrlock(&chunk_map_locks[x+z*map_size_x]);
Expand Down
4 changes: 2 additions & 2 deletions src/matrix.c
Expand Up @@ -129,15 +129,15 @@ void matrix_identity() {

void matrix_push() {
if(matrix_stack_index>=MATRIX_STACK_DEPTH) {
printf("Matrix stack overflow!\n");
log_fatal("Matrix stack overflow!");
return;
}
memcpy(matrix_stack[matrix_stack_index++],matrix_current,16*sizeof(float));
}

void matrix_pop() {
if(matrix_stack_index<1) {
printf("Matrix stack underflow!\n");
log_fatal("Matrix stack underflow!");
return;
}
memcpy(matrix_current,matrix_stack[--matrix_stack_index],16*sizeof(float));
Expand Down

0 comments on commit 126fbf1

Please sign in to comment.