Skip to content

Commit

Permalink
See desc
Browse files Browse the repository at this point in the history
* add discord integration
* remove default open news config
* remove static link flags
* prevent list.c realloc error
* speed up floating block test case at ground level
* use faster hash/compare funcs
* remove duplicate beep2.wav load
  • Loading branch information
xtreme8000 committed Oct 22, 2019
1 parent 9dc9e97 commit 7bfe628
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 25 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Expand Up @@ -11,6 +11,8 @@ option(ENABLE_OPENGLES "Build for OpenGL ES" OFF)
option(ENABLE_SDL "Build against SDL backend" OFF)
option(ENABLE_GLFW "Build against GLFW3 backend" ON)
option(ENABLE_SOUND "Enable sound support using OpenAL" ON)
option(ENABLE_RPC "Enable Discord Rich Presence support" OFF)


if((ENABLE_ANDROID_FILE OR ENABLE_TOUCH OR ENABLE_OPENGLES) AND NOT ENABLE_SDL)
message(FATAL_ERROR "Enable SDL to use ENABLE_ANDROID_FILE, ENABLE_TOUCH or ENABLE_OPENGLES")
Expand All @@ -22,4 +24,12 @@ if(NOT ENABLE_SDL AND NOT ENABLE_GLFW)
message(FATAL_ERROR "Enable SDL or GLFW")
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_options(-O3)
else()
add_compile_options(-O3 -g -march=native)
endif()
endif()

add_subdirectory(src)
2 changes: 1 addition & 1 deletion resources/config.ini
Expand Up @@ -8,7 +8,7 @@ inverty = 0
windowed = 1
language = 0
mouse_sensitivity = 5.000000
show_news = 1
show_news = 0
multisamples = 0
greedy_meshing = 0
vsync = 1
Expand Down
33 changes: 15 additions & 18 deletions src/CMakeLists.txt
Expand Up @@ -62,6 +62,7 @@ list(APPEND CLIENT_SOURCES ping.c)
list(APPEND CLIENT_SOURCES log.c)
list(APPEND CLIENT_SOURCES minheap.c)
list(APPEND CLIENT_SOURCES hashtable.c)
list(APPEND CLIENT_SOURCES rpc.c)
list(APPEND CLIENT_SOURCES ${BetterSpades_SOURCE_DIR}/resources/icon.rc)

add_executable(client ${CLIENT_SOURCES})
Expand All @@ -74,18 +75,6 @@ endif()
if(ENABLE_ANDROID_FILE)
target_compile_definitions(client PRIVATE USE_ANDROID_FILE)
endif()
if(ENABLE_OPENGLES)
target_compile_definitions(client PRIVATE OPENGL_ES)
endif()
if(ENABLE_SDL)
target_compile_definitions(client PRIVATE USE_SDL)
endif()
if(ENABLE_GLFW)
target_compile_definitions(client PRIVATE USE_GLFW)
endif()
if(ENABLE_SOUND)
target_compile_definitions(client PRIVATE USE_SOUND)
endif()

target_compile_definitions(client PRIVATE BETTERSPADES_MAJOR=${BETTERSPADES_MAJOR})
target_compile_definitions(client PRIVATE BETTERSPADES_MINOR=${BETTERSPADES_MINOR})
Expand All @@ -104,29 +93,37 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${BetterSpades_SOURCE_DIR}/cmake/Modu
set(OpenGL_GL_PREFERENCE LEGACY)
if(ENABLE_GLFW)
find_package(glfw3 REQUIRED)
target_link_libraries(client glfw3::glfw3 -static)
target_link_libraries(client glfw3::glfw3)
target_compile_definitions(client PRIVATE USE_GLFW)
endif()
if(ENABLE_SDL)
find_package(SDL REQUIRED)
target_link_libraries(client ${SDL_LIBRARY} -static)
target_link_libraries(client ${SDL_LIBRARY})
target_compile_definitions(client PRIVATE USE_SDL)
endif()
if(ENABLE_SOUND)
find_package(OpenAL REQUIRED)
target_link_libraries(client ${OPENAL_LIBRARY} -static)
target_link_libraries(client ${OPENAL_LIBRARY})
target_compile_definitions(client PRIVATE USE_SOUND)
endif()
find_package(enet REQUIRED)
find_package(deflate REQUIRED)
find_package(Threads REQUIRED)
if(NOT ENABLE_OPENGLES)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
target_link_libraries(client GLEW::GLEW -static)
target_link_libraries(client GLEW::GLEW)
else()
find_package(OpenGL REQUIRED COMPONENTS EGL)
target_link_libraries(client OpenGL::EGL -static)
target_link_libraries(client OpenGL::EGL)
target_compile_definitions(client PRIVATE OPENGL_ES)
endif()
if(ENABLE_RPC)
target_link_libraries(client discord-rpc stdc++)
target_compile_definitions(client PRIVATE USE_RPC DISCORD_DISABLE_IO_THREAD)
endif()

target_link_libraries(client ${CMAKE_THREAD_LIBS_INIT} ${OPENGL_LIBRARIES} enet::enet deflate::deflate m -static)
target_link_libraries(client ${CMAKE_THREAD_LIBS_INIT} ${OPENGL_LIBRARIES} enet::enet deflate::deflate m)
include_directories(client ${OPENAL_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIR} ${OPENGL_EGL_INCLUDE_DIRS})

add_custom_command(
Expand Down
5 changes: 5 additions & 0 deletions src/common.h
Expand Up @@ -56,6 +56,10 @@
#include <limits.h>
#include <dirent.h>

#ifdef USE_RPC
#include <discord_rpc.h>
#endif

#include "lodepng/lodepng.h"
#include "libdeflate.h"
#include "ini.h"
Expand Down Expand Up @@ -128,6 +132,7 @@
#include "tracer.h"
#include "config.h"
#include "hud.h"
#include "rpc.h"

void reshape(struct window_instance* window, int width, int height);
void text_input(struct window_instance* window, unsigned int codepoint);
Expand Down
11 changes: 6 additions & 5 deletions src/list.c
Expand Up @@ -39,11 +39,12 @@ void list_remove(struct list* l, int i) {
}

void list_clear(struct list* l) {
l->mem_size = l->element_size*64;
l->data = realloc(l->data,l->mem_size);
CHECK_ALLOCATION_ERROR(l->data)
l->elements = 0;
l->mem_size = 0;
void* new = realloc(l->data,l->element_size*64);
if(new) {
l->data = new;
l->mem_size = l->element_size*64;
}
l->elements = 0;
}

int list_size(struct list* l) {
Expand Down
5 changes: 5 additions & 0 deletions src/main.c
Expand Up @@ -461,6 +461,8 @@ void init() {
chunk_init();

weapon_set();

rpc_init();
}

void reshape(struct window_instance* window, int width, int height) {
Expand Down Expand Up @@ -556,6 +558,7 @@ void mouse_scroll(struct window_instance* window, double xoffset, double yoffset
}

void deinit() {
rpc_deinit();
ping_deinit();
if(settings.show_news)
file_url("https://www.buildandshoot.com/news/");
Expand Down Expand Up @@ -692,6 +695,8 @@ int main(int argc, char** argv) {
network_update();
window_update();

rpc_update();

if(settings.vsync>1 && (window_time()-last_frame_start)<(1.0F/settings.vsync)) {
double sleep_s = 1.0F/settings.vsync-(window_time()-last_frame_start);
struct timespec ts;
Expand Down
5 changes: 5 additions & 0 deletions src/map.c
Expand Up @@ -194,10 +194,15 @@ static int stack_contains(struct voxel* stack2, int len, int x, int y, int z) {
}

static void map_update_physics_sub(int x, int y, int z) {
if(y<=1)
return;

struct minheap openlist;
minheap_create(&openlist);
HashTable closedlist;
ht_setup(&closedlist,sizeof(uint32_t),sizeof(uint32_t),256);
closedlist.compare = int_cmp;
closedlist.hash = int_hash;

struct minheap_block start;
start.pos = pos_key(x,y,z);
Expand Down
14 changes: 14 additions & 0 deletions src/minheap.c
Expand Up @@ -26,6 +26,18 @@
#include "hashtable.h"
#include "minheap.h"

int int_cmp(void* first_key, void* second_key, size_t key_size) {
return (*(uint32_t*)first_key)!=(*(uint32_t*)second_key);
}

size_t int_hash(void* raw_key, size_t key_size) {
uint32_t x = *(uint32_t*)raw_key;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
return x;
}

static void nodes_swap(struct minheap* h, int a, int b) {
struct minheap_block tmp;
tmp = h->nodes[a];
Expand All @@ -41,6 +53,8 @@ void minheap_create(struct minheap* h) {
h->length = 256;
h->nodes = malloc(sizeof(struct minheap_block)*h->length);
ht_setup(&h->contains,sizeof(uint32_t),sizeof(uint32_t),256);
h->contains.compare = int_cmp;
h->contains.hash = int_hash;
}

void minheap_clear(struct minheap* h) {
Expand Down
3 changes: 3 additions & 0 deletions src/minheap.h
Expand Up @@ -42,6 +42,9 @@ struct minheap {
struct minheap_block* nodes;
};

int int_cmp(void* first_key, void* second_key, size_t key_size);
size_t int_hash(void* raw_key, size_t key_size);

void minheap_create(struct minheap* h);
void minheap_clear(struct minheap* h);
void minheap_destroy(struct minheap* h);
Expand Down
129 changes: 129 additions & 0 deletions src/rpc.c
@@ -0,0 +1,129 @@
/*
Copyright (c) 2017-2019 ByteBit
This file is part of BetterSpades.
BetterSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BetterSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"

struct rpc {
int needs_update;
int players;
int slots;
char server_url[64];
char server_name[64];
} rpc_state;

#ifdef USE_RPC
static void rpc_joingame(const char* joinSecret) {
log_info("discord join %s!",joinSecret);
}

static void rpc_joinrequest(const DiscordUser* request) {
Discord_Respond(request->userId,DISCORD_REPLY_YES);
}
#endif

void rpc_init() {
#ifdef USE_RPC
DiscordEventHandlers handlers;
memset(&handlers,0,sizeof(handlers));
handlers.joinGame = rpc_joingame;
handlers.joinRequest = rpc_joinrequest;
Discord_Initialize("DISCORD TOKEN HERE",&handlers,1,NULL);
#endif
rpc_state.needs_update = 1;
rpc_state.players = 0;
rpc_state.slots = 0;
*rpc_state.server_url = 0;
*rpc_state.server_name = 0;
}

void rpc_deinit() {
#ifdef USE_RPC
Discord_Shutdown();
#endif
}

void rpc_setv(enum RPC_VALUE v, char* x) {
switch(v) {
case RPC_VALUE_SERVERNAME:
if(strcmp(rpc_state.server_name,x)!=0) {
strncpy(rpc_state.server_name,x,sizeof(rpc_state.server_name)-1);
rpc_state.needs_update = 1;
}
break;
case RPC_VALUE_SERVERURL:
if(strcmp(rpc_state.server_url,x)!=0) {
strncpy(rpc_state.server_url,x,sizeof(rpc_state.server_url)-1);
rpc_state.needs_update = 1;
}
break;
}
}

void rpc_seti(enum RPC_VALUE v, int x) {
switch(v) {
case RPC_VALUE_PLAYERS:
if(rpc_state.players!=x) {
rpc_state.players = x;
rpc_state.needs_update = 1;
}
break;
case RPC_VALUE_SLOTS:
if(rpc_state.slots!=x) {
rpc_state.slots = x;
rpc_state.needs_update = 1;
}
break;
}
}

void rpc_update() {
#ifdef USE_RPC

int online = 0;
for(int k=0;k<PLAYERS_MAX;k++) {
if(players[k].connected)
online++;
}
rpc_seti(RPC_VALUE_PLAYERS,online);

if(rpc_state.needs_update) {
DiscordRichPresence discordPresence;
memset(&discordPresence,0,sizeof(discordPresence));
discordPresence.largeImageKey = "pic03";
discordPresence.smallImageKey = "logo";
discordPresence.smallImageText = BETTERSPADES_VERSION;
discordPresence.instance = 1;
if(rpc_state.slots>0) {
discordPresence.state = "Playing";
discordPresence.partyId = "42";
discordPresence.partySize = max(rpc_state.players,1);
discordPresence.partyMax = rpc_state.slots;
discordPresence.details = rpc_state.server_name;
discordPresence.joinSecret = rpc_state.server_url;
} else {
discordPresence.state = "Waiting";
}
Discord_UpdatePresence(&discordPresence);
rpc_state.needs_update = 0;
}

Discord_UpdateConnection();
Discord_RunCallbacks();
#endif
}
31 changes: 31 additions & 0 deletions src/rpc.h
@@ -0,0 +1,31 @@
/*
Copyright (c) 2017-2019 ByteBit
This file is part of BetterSpades.
BetterSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BetterSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/

enum RPC_VALUE {
RPC_VALUE_SERVERNAME,
RPC_VALUE_PLAYERS,
RPC_VALUE_SLOTS,
RPC_VALUE_SERVERURL,
};

void rpc_init(void);
void rpc_deinit(void);
void rpc_setv(enum RPC_VALUE v, char* x);
void rpc_seti(enum RPC_VALUE v, int x);
void rpc_update(void);
1 change: 0 additions & 1 deletion src/sound.c
Expand Up @@ -288,7 +288,6 @@ void sound_init() {
sound_load(&sound_death,"wav/death.wav",0.1F,24.0F);
sound_load(&sound_beep1,"wav/beep1.wav",0.1F,1024.0F);
sound_load(&sound_beep2,"wav/beep2.wav",0.1F,1024.0F);
sound_load(&sound_beep2,"wav/beep2.wav",0.1F,1024.0F);
sound_load(&sound_switch,"wav/switch.wav",0.1F,1024.0F);
sound_load(&sound_empty,"wav/empty.wav",0.1F,1024.0F);
sound_load(&sound_intro,"wav/intro.wav",0.1F,1024.0F);
Expand Down

0 comments on commit 7bfe628

Please sign in to comment.