Skip to content

Commit

Permalink
Fix SDL2 backend
Browse files Browse the repository at this point in the history
changing a key on controls screen working again
  • Loading branch information
xtreme8000 committed Sep 2, 2020
1 parent 7af1568 commit 89c13ff
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -121,8 +121,8 @@ if(ENABLE_GLFW)
target_compile_definitions(client PRIVATE USE_GLFW)
endif()
if(ENABLE_SDL)
find_package(SDL REQUIRED)
target_link_libraries(client ${SDL_LIBRARY})
find_package(SDL2 REQUIRED)
target_link_libraries(client ${SDL2_LIBRARY})
target_compile_definitions(client PRIVATE USE_SDL)
endif()
if(ENABLE_SOUND)
Expand Down
9 changes: 5 additions & 4 deletions src/common.h
Expand Up @@ -27,17 +27,18 @@
#ifdef USE_SDL
#include <SDL2/SDL_opengles.h>
#endif
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 glClearDepth(float x);
#define glColor3f(r, g, b) glColor4f(r, g, b, 1.0F)
#define glColor3ub(r, g, b) glColor4ub(r, g, b, 255)
#define glDepthRange(a, b) glDepthRangef(a, b)
#define glClearDepth(a) glClearDepthf(a)
#endif

#ifdef USE_GLFW
#include <GLFW/glfw3.h>
#endif

#ifdef USE_SDL
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#endif

Expand Down
18 changes: 0 additions & 18 deletions src/main.c
Expand Up @@ -47,24 +47,6 @@
#include "chunk.h"
#include "main.h"

#ifdef OPENGL_ES
void glColor3f(float r, float g, float b) {
glColor4f(r, g, b, 1.0F);
}

void glColor3ub(unsigned char r, unsigned char g, unsigned char b) {
glColor4ub(r, g, b, 255);
}

void glDepthRange(float near, float far) {
glDepthRangef(near, far);
}

void glClearDepth(float x) {
glClearDepthf(x);
}
#endif

int fps = 0;

int ms_seed = 1;
Expand Down
93 changes: 63 additions & 30 deletions src/window.c
Expand Up @@ -76,25 +76,26 @@ static void window_impl_textinput(GLFWwindow* window, unsigned int codepoint) {
static void window_impl_keys(GLFWwindow* window, int key, int scancode, int action, int mods) {
int count = config_key_translate(key, 0, NULL);

if(count > 0) {
int a = -1;
switch(action) {
case GLFW_RELEASE: a = WINDOW_RELEASE; break;
case GLFW_PRESS: a = WINDOW_PRESS; break;
case GLFW_REPEAT: a = WINDOW_REPEAT; break;
}
int a = -1;
switch(action) {
case GLFW_RELEASE: a = WINDOW_RELEASE; break;
case GLFW_PRESS: a = WINDOW_PRESS; break;
case GLFW_REPEAT: a = WINDOW_REPEAT; break;
}

if(a >= 0) {
int results[count];
config_key_translate(key, 0, results);
if(count > 0) {
int results[count];
config_key_translate(key, 0, results);

for(int k = 0; k < count; k++) {
keys(hud_window, results[k], scancode, a, mods & GLFW_MOD_CONTROL);
for(int k = 0; k < count; k++) {
keys(hud_window, results[k], scancode, a, mods & GLFW_MOD_CONTROL);

if(hud_active->input_keyboard)
hud_active->input_keyboard(results[k], action, mods & GLFW_MOD_CONTROL, key);
}
if(hud_active->input_keyboard)
hud_active->input_keyboard(results[k], action, mods & GLFW_MOD_CONTROL, key);
}
} else {
if(hud_active->input_keyboard)
hud_active->input_keyboard(WINDOW_KEY_UNKNOWN, action, mods & GLFW_MOD_CONTROL, key);
}
}

Expand Down Expand Up @@ -161,6 +162,10 @@ void window_init() {
glfwWindowHint(GLFW_VISIBLE, 0);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
#ifdef OPENGL_ES
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
#endif

glfwSetErrorCallback(window_impl_error);

Expand Down Expand Up @@ -243,7 +248,19 @@ void window_textinput(int allow) {
SDL_StopTextInput();
}

void window_fromsettings() { }
void window_fromsettings() {
SDL_SetWindowSize(hud_window->impl, settings.window_width, settings.window_height);

if(settings.vsync < 2)
window_swapping(settings.vsync);
if(settings.vsync > 1)
window_swapping(0);

if(settings.fullscreen)
SDL_SetWindowFullscreen(hud_window->impl, SDL_WINDOW_FULLSCREEN);
else
SDL_SetWindowFullscreen(hud_window->impl, 0);
}

void window_keyname(int keycode, char* output, size_t length) {
strncpy(output, SDL_GetKeyName(keycode), length);
Expand Down Expand Up @@ -299,13 +316,15 @@ void window_init() {
static struct window_instance i;
hud_window = &i;

#ifdef USE_TOUCH
SDL_SetHintWithPriority(SDL_HINT_ANDROID_SEPARATE_MOUSE_AND_TOUCH, "1", SDL_HINT_OVERRIDE);
#endif

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER);

hud_window->impl
= SDL_CreateWindow("BetterSpades " BETTERSPADES_VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
settings.window_width, settings.window_height, SDL_WINDOW_OPENGL);
settings.window_width, settings.window_height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
Expand Down Expand Up @@ -334,12 +353,12 @@ void window_update() {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT: quit = 1; break;
case SDL_KEYDOWN:
int count = config_key_translate(key, 0, NULL);
case SDL_KEYDOWN: {
int count = config_key_translate(event.key.keysym.sym, 0, NULL);

if(count > 0) {
int results[count];
config_key_translate(key, 0, results);
config_key_translate(event.key.keysym.sym, 0, results);

for(int k = 0; k < count; k++) {
keys(hud_window, results[k], event.key.keysym.sym, WINDOW_PRESS,
Expand All @@ -349,14 +368,19 @@ void window_update() {
hud_active->input_keyboard(results[k], WINDOW_PRESS, event.key.keysym.mod & KMOD_CTRL,
event.key.keysym.sym);
}
} else {
if(hud_active->input_keyboard)
hud_active->input_keyboard(WINDOW_KEY_UNKNOWN, WINDOW_PRESS, event.key.keysym.mod & KMOD_CTRL,
event.key.keysym.sym);
}
break;
case SDL_KEYUP:
int count = config_key_translate(key, 0, NULL);
}
case SDL_KEYUP: {
int count = config_key_translate(event.key.keysym.sym, 0, NULL);

if(count > 0) {
int results[count];
config_key_translate(key, 0, results);
config_key_translate(event.key.keysym.sym, 0, results);

for(int k = 0; k < count; k++) {
keys(hud_window, results[k], event.key.keysym.sym, WINDOW_RELEASE,
Expand All @@ -366,8 +390,13 @@ void window_update() {
hud_active->input_keyboard(results[k], WINDOW_RELEASE, event.key.keysym.mod & KMOD_CTRL,
event.key.keysym.sym);
}
} else {
if(hud_active->input_keyboard)
hud_active->input_keyboard(WINDOW_KEY_UNKNOWN, WINDOW_RELEASE, event.key.keysym.mod & KMOD_CTRL,
event.key.keysym.sym);
}
break;
}
case SDL_MOUSEBUTTONDOWN: {
int a = 0;
switch(event.button.button) {
Expand Down Expand Up @@ -396,10 +425,14 @@ void window_update() {
break;
case SDL_MOUSEWHEEL: mouse_scroll(hud_window, event.wheel.x, event.wheel.y); break;
case SDL_MOUSEMOTION: {
static int x_sum = 0, y_sum = 0;
x_sum += event.motion.xrel;
y_sum += event.motion.yrel;
mouse(hud_window, x_sum, y_sum);
if(SDL_GetRelativeMouseMode()) {
static int x, y;
x += event.motion.xrel;
y += event.motion.yrel;
mouse(hud_window, x, y);
} else {
mouse(hud_window, event.motion.x, event.motion.y);
}
break;
}
case SDL_TEXTINPUT: text_input(hud_window, event.text.text[0]); break;
Expand All @@ -413,7 +446,7 @@ void window_update() {
fingers[k].start.y = event.tfinger.y * settings.window_height;
fingers[k].down_time = window_time();
fingers[k].full = 1;
f = &fingers[k];
f = fingers + k;
break;
}
}
Expand All @@ -430,7 +463,7 @@ void window_update() {
for(int k = 0; k < 8; k++) {
if(fingers[k].full && fingers[k].finger == event.tfinger.fingerId) {
fingers[k].full = 0;
f = &fingers[k];
f = fingers + k;
break;
}
}
Expand All @@ -444,7 +477,7 @@ void window_update() {
struct window_finger* f;
for(int k = 0; k < 8; k++) {
if(fingers[k].full && fingers[k].finger == event.tfinger.fingerId) {
f = &fingers[k];
f = fingers + k;
break;
}
}
Expand Down

0 comments on commit 89c13ff

Please sign in to comment.