Skip to content

Commit

Permalink
actually use window_cpucores() for chunk gen
Browse files Browse the repository at this point in the history
  • Loading branch information
xtreme8000 committed Nov 6, 2018
1 parent f130421 commit a5ce575
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 88 deletions.
8 changes: 6 additions & 2 deletions src/chunk.c
Expand Up @@ -37,6 +37,8 @@ struct chunk_d {

struct chunk_worker chunk_workers[CHUNK_WORKERS_MAX];

int chunk_enabled_cores;

pthread_rwlock_t* chunk_map_locks;
pthread_mutex_t chunk_minimap_lock;

Expand All @@ -48,12 +50,14 @@ static int chunk_sort(const void* a, const void* b) {
}

void chunk_init() {
chunk_enabled_cores = min(max(window_cpucores()/2,1),CHUNK_WORKERS_MAX);
log_info("%i cores enabled for chunk generation",chunk_enabled_cores);
pthread_mutex_init(&chunk_minimap_lock,NULL);
chunk_map_locks = malloc(map_size_x*map_size_z*sizeof(pthread_rwlock_t));
CHECK_ALLOCATION_ERROR(chunk_map_locks);
for(int k=0;k<map_size_x*map_size_z;k++)
pthread_rwlock_init(&chunk_map_locks[k],NULL);
for(int k=0;k<CHUNK_WORKERS_MAX;k++) {
for(int k=0;k<chunk_enabled_cores;k++) {
chunk_workers[k].state = CHUNK_WORKERSTATE_IDLE;
chunk_workers[k].mem_size = 0;
chunk_workers[k].vertex_data = NULL;
Expand Down Expand Up @@ -1027,7 +1031,7 @@ void chunk_generate_naive(struct chunk_worker* worker) {
}

void chunk_update_all() {
for(int j=0;j<CHUNK_WORKERS_MAX;j++) {
for(int j=0;j<chunk_enabled_cores;j++) {
pthread_mutex_lock(&chunk_workers[j].state_lock);
if(chunk_workers[j].state==CHUNK_WORKERSTATE_FINISHED) {
chunk_workers[j].state = CHUNK_WORKERSTATE_IDLE;
Expand Down
4 changes: 3 additions & 1 deletion src/chunk.h
Expand Up @@ -36,7 +36,9 @@ extern int chunk_lighting_changed_lenght;

extern int chunk_render_mode;

#define CHUNK_WORKERS_MAX 3
extern int chunk_enabled_cores;

#define CHUNK_WORKERS_MAX 16

#define CHUNK_WORKERSTATE_BUSY 0
#define CHUNK_WORKERSTATE_IDLE 1
Expand Down
3 changes: 2 additions & 1 deletion src/common.h
Expand Up @@ -39,7 +39,7 @@
void glColor3f(float r, float g, float b);
void glColor3ub(unsigned char r, unsigned char g, unsigned char b);
void glDepthRange(float near, float far);
void void glClearDepth(float x);
void glClearDepth(float x);
#endif

#ifdef USE_GLFW
Expand Down Expand Up @@ -75,6 +75,7 @@

#ifdef __linux__
#define OS_LINUX
#include <sys/sysinfo.h>
#endif

#ifdef __APPLE__
Expand Down
161 changes: 77 additions & 84 deletions src/window.c
Expand Up @@ -3,76 +3,76 @@
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/>.
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"

#ifdef USE_GLFW

static void window_impl_mouseclick(GLFWwindow* window, int button, int action, int mods) {
int b = 0;
switch(button) {
case GLFW_MOUSE_BUTTON_LEFT:
b = WINDOW_MOUSE_LMB;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
b = WINDOW_MOUSE_RMB;
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
b = WINDOW_MOUSE_MMB;
break;
}
int a = -1;
switch(action) {
case GLFW_RELEASE:
a = WINDOW_RELEASE;
break;
case GLFW_PRESS:
a = WINDOW_PRESS;
break;
}
mouse_click(hud_window,b,a,mods>0);
int b = 0;
switch(button) {
case GLFW_MOUSE_BUTTON_LEFT:
b = WINDOW_MOUSE_LMB;
break;
case GLFW_MOUSE_BUTTON_RIGHT:
b = WINDOW_MOUSE_RMB;
break;
case GLFW_MOUSE_BUTTON_MIDDLE:
b = WINDOW_MOUSE_MMB;
break;
}
int a = -1;
switch(action) {
case GLFW_RELEASE:
a = WINDOW_RELEASE;
break;
case GLFW_PRESS:
a = WINDOW_PRESS;
break;
}
mouse_click(hud_window,b,a,mods>0);
}
static void window_impl_mouse(GLFWwindow* window, double x, double y) {
mouse(hud_window,x,y);
mouse(hud_window,x,y);
}
static void window_impl_mousescroll(GLFWwindow* window, double xoffset, double yoffset) {
mouse_scroll(hud_window,xoffset,yoffset);
mouse_scroll(hud_window,xoffset,yoffset);
}
static void window_impl_error(int i, const char* s) {
on_error(i,s);
on_error(i,s);
}
static void window_impl_reshape(GLFWwindow* window, int width, int height) {
reshape(hud_window,width,height);
reshape(hud_window,width,height);
}
static void window_impl_textinput(GLFWwindow* window, unsigned int codepoint) {
text_input(hud_window,codepoint);
text_input(hud_window,codepoint);
}
static void window_impl_keys(GLFWwindow* window, int key, int scancode, int action, int mods) {
int a = -1;
switch(action) {
case GLFW_RELEASE:
a = WINDOW_RELEASE;
break;
case GLFW_PRESS:
a = WINDOW_PRESS;
break;
}
int tr = window_key_translate(key,0);
if(tr>=0)
keys(hud_window,tr,scancode,a,mods>0);
int a = -1;
switch(action) {
case GLFW_RELEASE:
a = WINDOW_RELEASE;
break;
case GLFW_PRESS:
a = WINDOW_PRESS;
break;
}
int tr = window_key_translate(key,0);
if(tr>=0)
keys(hud_window,tr,scancode,a,mods>0);
else
tr = WINDOW_KEY_UNKNOWN;
if(hud_active->input_keyboard)
Expand All @@ -84,48 +84,47 @@ char* window_keyname(int keycode) {
}

float window_time() {
return glfwGetTime();
return glfwGetTime();
}

int window_pressed_keys[64] = {0};

const char* window_clipboard() {
return glfwGetClipboardString(hud_window->impl);
return glfwGetClipboardString(hud_window->impl);
}

int window_key_translate(int key, int dir) {
return config_key_translate(key,dir);
return config_key_translate(key,dir);
}

int window_key_down(int key) {
return window_pressed_keys[key];
return window_pressed_keys[key];
}

void window_mousemode(int mode) {
int s = glfwGetInputMode(hud_window->impl,GLFW_CURSOR);
if((s==GLFW_CURSOR_DISABLED && mode==WINDOW_CURSOR_ENABLED)
|| (s==GLFW_CURSOR_NORMAL && mode==WINDOW_CURSOR_DISABLED)) {
glfwSetInputMode(hud_window->impl,GLFW_CURSOR,mode==WINDOW_CURSOR_ENABLED?GLFW_CURSOR_NORMAL:GLFW_CURSOR_DISABLED);
}
int s = glfwGetInputMode(hud_window->impl,GLFW_CURSOR);
if((s==GLFW_CURSOR_DISABLED && mode==WINDOW_CURSOR_ENABLED)
|| (s==GLFW_CURSOR_NORMAL && mode==WINDOW_CURSOR_DISABLED))
glfwSetInputMode(hud_window->impl,GLFW_CURSOR,mode==WINDOW_CURSOR_ENABLED?GLFW_CURSOR_NORMAL:GLFW_CURSOR_DISABLED);
}

void window_mouseloc(double* x, double* y) {
glfwGetCursorPos(hud_window->impl,x,y);
glfwGetCursorPos(hud_window->impl,x,y);
}

void window_swapping(int value) {
glfwSwapInterval(value);
glfwSwapInterval(value);
}

void window_init() {
static struct window_instance i;
hud_window = &i;
static struct window_instance i;
hud_window = &i;

glfwWindowHint(GLFW_VISIBLE,0);
glfwWindowHint(GLFW_VISIBLE,0);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,1);

glfwSetErrorCallback(window_impl_error);
glfwSetErrorCallback(window_impl_error);

if(!glfwInit()) {
log_fatal("GLFW3 init failed");
Expand Down Expand Up @@ -178,12 +177,12 @@ void window_deinit() {
}

void window_update() {
glfwSwapBuffers(hud_window->impl);
glfwPollEvents();
glfwSwapBuffers(hud_window->impl);
glfwPollEvents();
}

int window_closed() {
return glfwWindowShouldClose(hud_window->impl);
return glfwWindowShouldClose(hud_window->impl);
}

#endif
Expand All @@ -200,28 +199,27 @@ char* window_keyname(int keycode) {
}

float window_time() {
return ((double)SDL_GetTicks())/1000.0F;
return ((double)SDL_GetTicks())/1000.0F;
}

int window_pressed_keys[64] = {0};

const char* window_clipboard() {
return SDL_HasClipboardText()?SDL_GetClipboardText():NULL;
return SDL_HasClipboardText()?SDL_GetClipboardText():NULL;
}

int window_key_translate(int key, int dir) {
return config_key_translate(key,dir);
return config_key_translate(key,dir);
}

int window_key_down(int key) {
return window_pressed_keys[key];
return window_pressed_keys[key];
}

void window_mousemode(int mode) {
int s = SDL_GetRelativeMouseMode();
if((s && mode==WINDOW_CURSOR_ENABLED) || (!s && mode==WINDOW_CURSOR_DISABLED)) {
if((s && mode==WINDOW_CURSOR_ENABLED) || (!s && mode==WINDOW_CURSOR_DISABLED))
SDL_SetRelativeMouseMode(mode==WINDOW_CURSOR_ENABLED?0:1);
}
}

void window_mouseloc(double* x, double* y) {
Expand All @@ -232,7 +230,7 @@ void window_mouseloc(double* x, double* y) {
}

void window_swapping(int value) {
SDL_GL_SetSwapInterval(value);
SDL_GL_SetSwapInterval(value);
}

void window_init() {
Expand All @@ -248,7 +246,7 @@ void window_init() {
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
//SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
SDL_GLContext* ctx = SDL_GL_CreateContext(hud_window->impl);
}
Expand All @@ -269,8 +267,6 @@ void window_update() {
break;
case SDL_KEYDOWN:
{
if(event.key.keysym.sym==SDLK_p)
exit(0);
int tr = window_key_translate(event.key.keysym.sym,0);
if(tr>=0)
keys(hud_window,tr,event.key.keysym.sym,WINDOW_PRESS,0);
Expand Down Expand Up @@ -350,17 +346,14 @@ void window_update() {
}

int window_closed() {
return quit;
return quit;
}

#endif

int window_cpucores() {
#ifdef OS_LINUX
int count;
int size = sizeof(int);
sysctlbyname("hw.ncpu",&count,&size,NULL,0);
return count;
return get_nprocs();
#endif
#ifdef OS_WINDOWS
SYSTEM_INFO info;
Expand Down

0 comments on commit a5ce575

Please sign in to comment.